Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrating from canvas-prebuilt which was deprecated to canvas 2.6.1 and refactoring code #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.|
|enableAutoDispose|Boolean|true|Enable auto-dispose echarts after the image is created.|
148 changes: 97 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down