diff --git a/README.md b/README.md index dea46d4..8c79821 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ OS X | `brew install pkg-config cairo pango libpng jpeg giflib` Ubuntu | `sudo apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++` Fedora | `sudo yum install cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel` Solaris | `pkgin install cairo pango pkg-config xproto renderproto kbproto xextproto` -Windows | [Instructions on our wiki](https://github.com/Automattic/node-canvas/wiki/Installation---Windows) +Windows | `npm i` or `npm install` (canvas will be installed automatically) ``` npm install node-echarts @@ -37,4 +37,4 @@ node_echarts(config) |height|Number|500|Image height| |option|Object|{}|Echarts Options| |path|String|-|Path is filepath of the image which will be created. If the path is empty, return buffer.| -|enableAutoDispose|Boolean|true|Enable auto-dispose echarts after the image is created.| \ No newline at end of file +|enableAutoDispose|Boolean|true|Enable auto-dispose echarts after the image is created.| diff --git a/index.js b/index.js index 32a7965..30db462 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,69 @@ var echarts = require("echarts"); -var Canvas = require("canvas-prebuilt"); +var { createCanvas } = require("canvas"); var fs = require('fs'); var path = require('path'); +/** + * default echart option in case the client doesn't define + */ +function getDefaultOption() { + return { + title: { + text: 'test', + }, + tooltip: {}, + legend: { + data: ['test'], + }, + xAxis: { + data: ["a", "b", "c", "d", "f", "g"], + }, + yAxis: {}, + series: [{ + name: 'test', + type: 'bar', + data: [5, 20, 36, 10, 10, 20], + }] + }; +} + +/** + * default config for the option, in case the client doesn't + * define it. + * + * @param {*} option echart option + */ +function getDefaultConfig(option) { + return { + width: 500, + height: 500, + option, + enableAutoDispose: true, + } +} + +/** + * saves the chart passed in the provided path + * @param {*} chart chart in which thedom will be retrieved and saved + * as an image + * @param {*} path directory to the image that will be saved + */ +function saveChart(chart, path) { + try { + fs.writeFileSync(path, chart.getDom().toBuffer()); + console.log("Created image:" + path) + } catch (err) { + console.error("Error: write file failed: " + err.message) + } +} + +/** + * retrieves the buffer of the chart + * @param {*} chart chart in which the buffer will be retrieved + */ +function getBuffer(chart) { + return chart.getDom().toBuffer(); +} /** * @param config = { @@ -12,72 +73,57 @@ var path = require('path'); //If the path is not set, return the Buffer of image. path: '', // Path is filepath of the image which will be created. } - * */ module.exports = function (config) { - if (config.canvas) { - Canvas = config.canvas; - } - var ctx = new Canvas(128, 128); + const createdCanvas = createCanvas(128, 128); + const ctx = createdCanvas.getContext('2d'); + if (config.font) { ctx.font = config.font; } echarts.setCanvasCreator(function () { - return ctx; + return createdCanvas; }); - var chart, option = { - title: { - text: 'test' - }, - tooltip: {}, - legend: { - data: ['test'] - }, - xAxis: { - data: ["a", "b", "c", "d", "f", "g"] - }, - yAxis: {}, - series: [{ - name: 'test', - type: 'bar', - data: [5, 20, 36, 10, 10, 20] - }] - }; - - let defaultConfig = { - width: 500, - height: 500, - option, - enableAutoDispose: true - } + const option = getDefaultOption(); + const defaultConfig = getDefaultConfig(option); - config = Object.assign({}, defaultConfig, config) + config = Object.assign({}, defaultConfig, config); config.option.animation = false; - chart = echarts.init(new Canvas(parseInt(config.width, 10), parseInt(config.height, 10))); + + const chart = echarts.init( + createCanvas( + parseInt(config.width, 10), + parseInt(config.height, 10), + ), + ); + chart.setOption(config.option); - if (config.path) { - try { - fs.writeFileSync(config.path, chart.getDom().toBuffer()); - if(config.enableAutoDispose){ - chart.dispose(); - } - console.log("Create Img:" + config.path) - } catch (err) { - console.error("Error: Write File failed" + err.message) + + try { + /** + * if specified the path, the chart will be saved into it + */ + if (config.path) { + saveChart(chart, config.path); + return; } - - } else { - var buffer = chart.getDom().toBuffer(); - try{ - if(config.enableAutoDispose){ + + /** + * if not, the buffer will be returned + */ + return getBuffer(chart); + } finally { + + /** + * finally, dispose the chart if client defined so + */ + if (config.enableAutoDispose) { chart.dispose(); - } - }catch(e){} - return buffer; + } } } \ No newline at end of file diff --git a/package.json b/package.json index d19afd0..b529475 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ }, "homepage": "https://github.com/suxiaoxin/node-echarts#readme", "dependencies": { - "canvas-prebuilt": "^1.6.5-prerelease.1", + "canvas": "^2.6.1", "echarts": "^4.0.3" }, "devDependencies": {