score:-7
perhaps think of using google charts, instead of chart.js. google charts includes trendlines - including linear, exponential, and polynomial.
documentation here: google charts trendlines
score:2
update your chart definition by adding an empty dataset:
{
label: "trend 1",
bordercolor: 'rgba(200,0,0)',
backgroundcolor: 'rgba(200,0,0)',
data: [],
fill: false,
}
right after your chart definition (in the jsfiddle) add these lines:
arrayforregression= []; // declare the array which will contains calculated point of trend line
for (i=0; i < chartvisits.data.datasets[0].data.length; i++) {
arrayforregression.push([i, chartvisits.data.datasets[0].data[i]]); // fill the array with the "y" values to be approximated by regression
}
regr = regression("polynomial", arrayforregression, 2); // calculare polynomial regression
convertedregressionarray = []; // declare an array to hold the regression line in charts.js format
for (i=0; i < chartvisits.data.datasets[0].data.length; i++) { // fill the array with calculated values
convertedregressionarray.push(regr.points[i][1]);
}
chartvisits.config.data.datasets[2].data = convertedregressionarray; // put the regression array, converted to charts format, into chart
chartvisits.update();
in your html, add this line in body section to include the library:
<script type="text/javascript" src="js/regression.min.js"></script>
library from:
https://bl.ocks.org/daluu/5bb59ef3f3fed3de227535da367649ba
https://gist.github.com/daluu/5bb59ef3f3fed3de227535da367649ba
the code of the function is obfuscated/minified, anyway the call syntax is:
var linreg = regression('linear', data);
var polyreg = regression('polynomial', data, 2);
var exporeg = regression('exponential', data);
var powreg = regression('power', data);
var logreg = regression('logarithmic', data);
data format:
var data = [[500,2.5], [1000,3], [1500,3], [2000,3.3], [3000,3.6], [4000,4], [5500,4.8], [6000,5], [7000,5], [8000,5.5], [9000,6], [12000,7], [14000,8], [15000,8], [18000,9], [20000,10], [21000,10], [24000,11], [28000,12], [30000,13], [50000,18]];
but i don't know exactly how to implement it in charts.js or any other library.
this discovery led me to look for "javascript regression.min.js" , finding many resources.
note: there is an error in index.html:
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
should be:
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
note2:
i think there is a major bug in linear regression part: the returned equation object (an array of coefficients) is in reversed order w.r.t polynomial format.
examples:
3x^2+2x+1 is returned as array [1, 2, 3] , so each coefficient must be multiplied by math.pow(x, array position):
y = equation[0] * math.pow(x,0) + equation[1] * math.pow(x,1) + equation[1] * math.pow(x,1) ** correct **
2x + 1 should be returned as array [1,2] but it is returned as [2,1] --> bug?
y = equation[0] * math.pow(x,1) + equation[1] * math.pow(x,0)
score:5
currently, chart.js does not have a trendline capability at all (not even logarithmic). perhaps you were getting confused with the custom tick format example at the end of the common scale configuration section?
you could however use the chartjs-plugin-annotation plugin to draw a trendline on your chart, but keep in mind that you would have to implement your own logic to calculate the correct location of the line (and then just use the annotation plugin to actually draw it).
here is an example demonstrating how to use the plugin (the plugin provides a set of annotation
properties that you can add to your charts options
. you would then just need to create a function that calculates the trendline and use the result to set the annotation value
and endvalue
properties.
var mychart = new chart(ctx, {
type: 'line',
data: {
labels: [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1],
datasets: [{
label: 'dataset 1',
bordercolor: window.chartcolors.blue,
borderwidth: 2,
fill: false,
data: [19304,13433,9341,6931,5169, 3885,2927,2159,1853,1502, 1176,911,724,590,491, 400,335,280,239,200]
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'chart.js drsw line on chart'
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleid: 'y-axis-0',
value: 2225,
endvalue: 0,
bordercolor: 'rgb(75, 192, 192)',
borderwidth: 4,
label: {
enabled: true,
content: 'trendline',
yadjust: -16,
}
}]
}
}
});
you can see it in action at this codepen.
score:12
the chartjs-plugin-trendline
provides a straight-line trendline that plugs into chart.js.
npm: https://www.npmjs.com/package/chartjs-plugin-trendline/v/0.1.1
download: https://www.jsdelivr.com/package/npm/chartjs-plugin-trendline
usage is pretty straightforward - just add the following to your dataset:
datasets: [{
// other configurations
// ...
trendlinelinear: {
style: "rgb(43 ,66 ,255, 0.3)",
linestyle: "dotted|solid",
width: 2
}
}]
Source: stackoverflow.com
Related Query
- Adding trendlines to existing chart Chart.js
- Chart.js, adding footer to chart
- Adding Chart.js line chart to Jinja2/Flask html page from JS file
- Adding a label to a doughnut chart in Chart.js shows all values in each chart
- Extending Existing Chart Types angular chart js using chart js 2.0
- Adding custom text to Bar Chart label values using Chart.js
- chartjs Adding percentages to Pie Chart Legend
- Adding line over stacked line chart with ChartJS
- Charts.js: Load new data set to an existing chart
- Adding object data to data structure of chart.js chart does not show any data
- ChartJS chart not scaling after adding values
- Adding data to line chart using addData() method in Chart.js
- ChartJS 2 - Adding data to chart breaks after X number of items added
- Adding additional properties to a Chart JS dataset
- Append second dataset to existing chartjs chart
- Need help adding Chart.js chart to a Modal window (I'm Using AngularJS)
- what is wrong with my code ? java script chart position
- How to print a chart rendered by code
- ChartJS chart is bugged after adding new data
- Adding image on chart js
- Adding label inside multiseries doughnut chart through chart.js
- VueJS + Chartjs - Chart only renders after code change
- Adding responsive text inside chart in Charts Js
- How do I destroy/update Chart Data in this chart.js code example?
- Trigger axispointer function on existing chart js via console
- Chart js adding number
- Adding new data to empty Chart.js chart does not render new data correctly