Makanz / chartjs-plugin-trendline

This plugin draws an linear trendline in your Chart. Made for Chart.js > 3.0
MIT License
87 stars 59 forks source link

Trendline for second Y axis #28

Closed YukonJJR closed 1 year ago

YukonJJR commented 4 years ago

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?

JWealthall commented 4 years ago

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); } });

YukonJJR commented 4 years ago

Thank you very much.  Worked perfectly….

Jeff Rogers IT Manager

419.873.9500 x1131

248.396.0289

419.873.9600

1300 Flagship Dr. Perrysburg, OH 43551

www.CECtops.com

    

On May 3, 2020, at 11:15 AM, John Wealthall notifications@github.com wrote:

CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

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); } }); — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Makanz commented 4 years ago

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.