Skip to content

Commit

Permalink
Fix some ES6 syntax issue and add support for getting axis keys from …
Browse files Browse the repository at this point in the history
…options

xAxis and yAxis keys are currently hard-coded as either 'x', or 't' and
'y'.

This change adds support to use the provided xAxisKey and yAxisKey
values from the chartjs options object that is provided by the user.

Fall back to the original behavior in case these are not provided.

This is a non-breaking change.

Also, add the 'const' keyword in front of the addFilter method.
  • Loading branch information
elesueur committed Oct 13, 2023
1 parent 4176303 commit c9fd2fb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ To configure the trendline plugin you simply add a new config options to your da
colorMax: "green",
lineStyle: "dotted|solid",
width: 2,
xAxisKey: "time" (optional),
yAxisKey: "usage" (optional),
projection: true|false (optional)
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/chartjs-plugin-trendline.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chartjs-plugin-trendline",
"version": "2.0.5",
"version": "2.1.0",
"description": "Trendline for Chart.js",
"main": "src/chartjs-plugin-trendline.js",
"scripts": {
Expand Down
15 changes: 10 additions & 5 deletions src/chartjs-plugin-trendline.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,37 @@ const pluginTrendlineLinear = {
},
};

addFitter = (datasetMeta, ctx, dataset, xScale, yScale) => {
const addFitter = (datasetMeta, ctx, dataset, xScale, yScale) => {
let defaultColor = dataset.borderColor || 'rgba(169,169,169, .6)';
let colorMin = dataset.trendlineLinear.colorMin || defaultColor;
let colorMax = dataset.trendlineLinear.colorMax || defaultColor;
let lineWidth = dataset.trendlineLinear.width || dataset.borderWidth;
let lineStyle = dataset.trendlineLinear.lineStyle || 'solid';
let fillColor = dataset.trendlineLinear.fillColor;

const parsing = typeof datasetMeta.controller.chart.options.parsing === "object" ?
datasetMeta.controller.chart.options.parsing : undefined;
const xAxisKey = dataset.trendlineLinear.xAxisKey || parsing ? parsing.xAxisKey : "x";
const yAxisKey = dataset.trendlineLinear.yAxisKey || parsing ? parsing.yAxisKey : "y";

lineWidth = lineWidth !== undefined ? lineWidth : 3;

let fitter = new LineFitter();
let firstIndex = dataset.data.findIndex((d) => {
return d !== undefined && d !== null;
});
let lastIndex = dataset.data.length - 1;
let startPos = datasetMeta.data[firstIndex].x;
let endPos = datasetMeta.data[lastIndex].x;
let startPos = datasetMeta.data[firstIndex][xAxisKey];
let endPos = datasetMeta.data[lastIndex][xAxisKey];
let xy = typeof dataset.data[firstIndex] === 'object';

dataset.data.forEach((data, index) => {
if (data == null) return;

if (['time', 'timeseries'].includes(xScale.options.type)) {
let x = data.x != null ? data.x : data.t;
let x = data[xAxisKey] != null ? data[xAxisKey] : data.t;
if (x !== undefined) {
fitter.add(new Date(x).getTime(), data.y);
fitter.add(new Date(x).getTime(), data[yAxisKey]);
} else {
fitter.add(index, data);
}
Expand Down

0 comments on commit c9fd2fb

Please sign in to comment.