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

Add plugin support for ChartJS 3 #197

Open
wants to merge 1 commit 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
18 changes: 13 additions & 5 deletions lib/charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ async function renderChartJs(
}
return val;
},
display: ctx => ctx.datasetIndex === 0,
display: (ctx) => ctx.datasetIndex === 0,
},
},
},
Expand Down Expand Up @@ -321,7 +321,7 @@ async function renderChartJs(

if (chart.type === 'line') {
if (chart.data && chart.data.datasets && Array.isArray(chart.data.datasets)) {
chart.data.datasets.forEach(dataset => {
chart.data.datasets.forEach((dataset) => {
const data = dataset;
// Make line charts straight lines by default.
data.lineTension = data.lineTension || 0;
Expand Down Expand Up @@ -353,7 +353,7 @@ async function renderChartJs(
require('chartjs-plugin-doughnutlabel');
}
let userSpecifiedOutlabels = false;
chart.data.datasets.forEach(dataset => {
chart.data.datasets.forEach((dataset) => {
if (dataset.outlabels || chart.options.plugins.outlabels) {
userSpecifiedOutlabels = true;
} else {
Expand All @@ -379,8 +379,16 @@ async function renderChartJs(
require('chartjs-adapter-moment');
}
if (!chart.plugins) {
if (version.startsWith('3') || version.startsWith('4')) {
if (version.startsWith('4')) {
chart.plugins = [];
} else if (version.startsWith('3')) {
const chartAnnotations = require('chartjs-plugin-annotation-v3');
const chartDataLabels = require('chartjs-plugin-datalabels-v3');
const chartBoxViolinPlot = require('@sgratzl/chartjs-chart-boxplot');
chart.plugins = [chartDataLabels, chartAnnotations];
if (BOXPLOT_CHART_TYPES.has(chart.type)) {
chart.plugins.push(chartBoxViolinPlot);
}
} else {
const chartAnnotations = require('chartjs-plugin-annotation');
const chartBoxViolinPlot = require('chartjs-chart-box-and-violin-plot');
Expand All @@ -399,7 +407,7 @@ async function renderChartJs(
// Background color plugin
chart.plugins.push({
id: 'background',
beforeDraw: chartInstance => {
beforeDraw: (chartInstance) => {
if (backgroundColor) {
// Chart.js v3 provides `chartInstance.chart` as `chartInstance`
const chart = chartInstance.chart ? chartInstance.chart : chartInstance;
Expand Down
17 changes: 13 additions & 4 deletions logging.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
const bunyan = require('bunyan');
const pino = require('pino');

const logger = bunyan.createLogger({
name: 'quickchart',
streams: [{ stream: process.stdout, level: process.env.LOG_LEVEL }],
const logger = pino({
// setting this to undefined prevents pino from adding hostname
base: undefined,
messageKey: 'message',
// replace the # level with the level string
formatters: {
level(/** @type {any} */ level) {
return { level };
},
},
// eslint-disable-next-line turbo/no-undeclared-env-vars
mixin: () => ({ name: `quickcharts` }),
});

module.exports = {
Expand Down
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@
"canvas": "2.9.3"
},
"resolutions": {
"canvas": "2.9.3"
"canvas": "2.9.3",
"chartjs-plugin-datalabels-v3/chart.js": "3.9.1",
"chartjs-plugin-annotation-v3/chart.js": "3.9.1",
"@sgratzl/chartjs-chart-boxplot/chart.js": "3.9.1"
},
"dependencies": {
"bunyan": "^1.8.12",
"canvas": "2.9.3",
"canvas-5-polyfill": "^0.1.5",
"chart.js": "^2.9.4",
"chart.js-v3": "npm:chart.js@3.9.1",
"chart.js-v4": "npm:chart.js@4.0.1",
"chartjs-adapter-moment": "https://github.com/typpo/chartjs-adapter-moment.git#e9bc92ab6e0e500c91c4a9871db7b14d15b5c2e7",
"@sgratzl/chartjs-chart-boxplot": "^3.10.0",
"chartjs-chart-box-and-violin-plot": "^2.4.0",
"chartjs-chart-radial-gauge": "^1.0.3",
"chartjs-node-canvas": "^3.0.6",
"chartjs-plugin-annotation": "^0.5.7",
"chartjs-plugin-annotation-v3": "npm:chartjs-plugin-annotation@2.0.1",
"chartjs-plugin-colorschemes": "https://github.com/typpo/chartjs-plugin-colorschemes.git#979ef8e599265f65c85d5dae90b543d5589c734a",
"chartjs-plugin-datalabels": "^0.5.0",
"chartjs-plugin-datalabels-v3": "npm:chartjs-plugin-datalabels@2.2.0",
"chartjs-plugin-doughnutlabel": "^2.0.3",
"chartjs-plugin-piechart-outlabels": "^0.1.4",
"deepmerge": "^4.2.2",
Expand All @@ -44,6 +49,7 @@
"node-fetch": "^2.6.7",
"patternomaly": "^1.3.2",
"pdfkit": "^0.10.0",
"pino": "^8.20.0",
"qrcode": "^1.3.3",
"qs": "^6.7.0",
"sharp": "^0.32.6",
Expand Down
Loading