npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-charts

v1.0.7

Published

NodeChart 是一个基于puppeteer的Node生成chart 图片的库,简单易用,扩展方便,内置了echarts 和 highcharts两种类型的图表库,使用者可根据自身需求灵活添加,配置非常方便。

Downloads

18

Readme

NodeChart 是一个基于puppeteer的Node生成chart 图片的库,简单易用,扩展方便,内置了echarts 和 highcharts两种类型的图表库,使用者可根据自身需求灵活添加,配置非常方便。

理论上可以支持所有图表库。

安装使用

npm install --save node-charts

使用教程

使用内置的图表

  1. echarts 实例
const fs = require('fs');
const NodeCharts = require('node-charts');
let nc = new NodeCharts();
//默认路径如果读取异常可以显示设置配置文件路径
nc.setExternalConfPath(configPath);

let option = {
    title: {
        text: 'ECharts 入门示例'
    },
    tooltip: {},
    legend: {
        data:['销量']
    },
    xAxis: {
        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
    },
    yAxis: {},
    series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
    }]
};
//监听全局异常事件
nc.on('error',(err)=>{
    console.log(err);
});
//如果不使用默认的模板


nc.render(option,(err,data)=>{
    fs.writeFileSync('test.png',data);
},{
    //修改图表渲染所用的模板,模板中约定必须包含 div#container
    renderTo:`<div id="container" style="width: 600px;height:400px;"></div>`
})
  1. highcharts 实例
const fs = require('fs');
const NodeCharts = require('node-charts');
let nc = new NodeCharts();
let options = {

    title: {
        text: '2010 ~ 2016 年太阳能行业就业人员发展情况'
    },
    subtitle: {
        text: '数据来源:thesolarfoundation.com'
    },
    yAxis: {
        title: {
            text: '就业人数'
        }
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'middle'
    },
    plotOptions: {
        series: {
            label: {
                connectorAllowed: false
            },
            pointStart: 2010
        }
    },
    series: [{
        name: '安装,实施人员',
        data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }, {
        name: '工人',
        data: [24916, 24064, 29742, 29851, 32490, 30282, 38121, 40434]
    }, {
        name: '销售',
        data: [11744, 17722, 16005, 19771, 20185, 24377, 32147, 39387]
    }, {
        name: '项目开发',
        data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
    }, {
        name: '其他',
        data: [12908, 5948, 8105, 11248, 8989, 11816, 18274, 18111]
    }],
    responsive: {
        rules: [{
            condition: {
                maxWidth: 500
            },
            chartOptions: {
                legend: {
                    layout: 'horizontal',
                    align: 'center',
                    verticalAlign: 'bottom'
                }
            }
        }]
    }
}
//监听全局异常事件
nc.on('error',(err)=>{
    console.log(err);
});
//如果不使用默认的模板

nc.render(options, (err, data) => {
    console.log(err);
    fs.writeFileSync('test.h.png', data);
}, {
    chartType: 'highcharts',
    waitfor: 1000,
    //修改图表渲染所用的模板,模板中约定必须包含 div#container
    renderTo:`<div id="container" style="width: 600px;height:400px;"></div>`
});

nc.render(options,callback,config),其中option为图表库渲染需要的配置项,callback中处理图片信息data 为buffer类型,config 主要包含两项内容{ type:''//图表类型,默认为echarts waitfor:0//等待渲染时间,默认为0 }

图表库扩展

如果内置图表无法满足或者不适合项目需求,可以添加外部图表库进来,配置也比较简单,首先需要在node项目根路径下创建nodechart.config.js 然后配置其中内容:

const path = require('path');
module.exports = [{
        srcPath: path.resolve(__dirname, '../clients/echarts.min.js'),
        initPath: path.resolve(__dirname, '../clients/echarts.config.js'),
        type: 'echarts'
    },
    {
        srcPath: path.resolve(__dirname, '../clients/highcharts.min.js'),
        initPath: path.resolve(__dirname, '../clients/highcharts.config.js'),
        type: 'highcharts'
    },
]

导出内容可以为对象和数组两种类型,主要参数说明:

srcPath:图表库文件

initPath:图表库调用初始化脚本文件

type:图表类型(唯一标识)比如echarts 、highcharts,推荐使用图表库名称

如echarts 初始化脚本文件配置如下:

(function (window) {
    //从window.chart中拿到图表需要的配置信息
    //约定container 为 容器ID
    let option =window.chart.options;
    var myChart = window.echarts.init(document.getElementById('container'), null, {
        renderer: 'svg'
    });
    myChart.setOption(option);
})(this);

Q&A

欢迎 issue && star