Skip to content

Commit

Permalink
Merge pull request #56 from jmm13/Chartjs-3.x-support
Browse files Browse the repository at this point in the history
Added support for v3.x of chartJS
  • Loading branch information
Makanz authored Oct 17, 2021
2 parents efbbf2f + 8a2dd5f commit a21dae6
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/chartjs-plugin-trendline.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Mod by: vesal: accept also xy-data so works with scatter
*/
var pluginTrendlineLinear = {
id: "trendlineLinear",
id: "chartjs-plugin-trendline",
afterDraw: function(chartInstance) {
var yScale;
var xScale;
Expand All @@ -20,7 +20,7 @@ var pluginTrendlineLinear = {
yScale = chartInstance.scales[axis];
if ( xScale && yScale ) break;
}
var ctx = chartInstance.chart.ctx;
var ctx = chartInstance.ctx;

chartInstance.data.datasets.forEach(function(dataset, index) {
if (dataset.trendlineLinear && chartInstance.isDatasetVisible(index) && dataset.data.length != 0) {
Expand All @@ -43,8 +43,8 @@ function addFitter(datasetMeta, ctx, dataset, xScale, yScale) {

var fitter = new LineFitter();
var lastIndex = dataset.data.length - 1;
var startPos = datasetMeta.data[0]._model.x;
var endPos = datasetMeta.data[lastIndex]._model.x;
var startPos = datasetMeta.data[0].x;
var endPos = datasetMeta.data[lastIndex].x;

var xy = false;
if ( dataset.data && typeof dataset.data[0] === 'object') xy = true;
Expand Down Expand Up @@ -137,8 +137,13 @@ LineFitter.prototype = {
};

// If we're in the browser and have access to the global Chart obj, register plugin automatically
if (typeof window !== "undefined" && window.Chart)
window.Chart.plugins.register(pluginTrendlineLinear);
if (typeof window !== undefined && window.Chart) {
if (window.Chart.hasOwnProperty('register')) {
window.Chart.register(pluginTrendlineLinear);
} else {
window.Chart.plugins.register(pluginTrendlineLinear);
}
}

// Otherwise, try to export the plugin
try {
Expand Down

0 comments on commit a21dae6

Please sign in to comment.