Makanz/chartjs-plugin-trendline

Trendline for second Y axis

Closed this issue · 3 comments

The trend line for a second Y axis is plotted on the first Y axis therefore it does not match the scale of the datapoint it represents. Is there a way to plot the trend line on a secondary (righthand) Y axis?

The beforeDraw function can be modified to select the yAxis for the dataset by changing the forEach function to

chartInstance.data.datasets.forEach(function (dataset, index) { if (dataset.trendlineLinear && chartInstance.isDatasetVisible(index)) { var datasetMeta = chartInstance.getDatasetMeta(index); // Check if a "named" yScale is being used var ySc = yScale if (dataset.yAxisID !== undefined && dataset.yAxisID !== null && chartInstance.scales[dataset.yAxisID] !== undefined) { ySc = chartInstance.scales[dataset.yAxisID]; } addFitter(datasetMeta, ctx, dataset, xScale, ySc); } });

The beforeDraw function can be modified to select the yAxis for the dataset by changing the forEach function to

chartInstance.data.datasets.forEach(function (dataset, index) { if (dataset.trendlineLinear && chartInstance.isDatasetVisible(index)) { var datasetMeta = chartInstance.getDatasetMeta(index); // Check if a "named" yScale is being used var ySc = yScale if (dataset.yAxisID !== undefined && dataset.yAxisID !== null && chartInstance.scales[dataset.yAxisID] !== undefined) { ySc = chartInstance.scales[dataset.yAxisID]; } addFitter(datasetMeta, ctx, dataset, xScale, ySc); } });

Please make a pull request.