Updating chart.js chart with dataset of different size





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I have set up a chart.js chart which uses data from a mysql database. I have set up a rest service to provide tha data, with the ability to define the grouping ie. the data is measured a five minute intervals, but I group it in 3 hour segments with my SQL statement SELECT (75-round(avg(sensorValue),0)) as Value, Time FROM sensorpi.rawData GROUP BY UNIX_TIMESTAMP(time) DIV $default_interval ORDER BY time asc



I would like to be able to change the granularity of the data by defining a new granularity.



I created an update function to send the request to the backend, and update the chart.



The chart is updated, but the default has 61 entries (at the moment) and the most fine-grained data has 180 entries. What happens is that the data is just reduced to the first 61 items in the dataset.



My gut feeling is that it is the date label of the X-Axis which is causing the trouble. But i am at a loss as to how i can solve it.



Initial output



Expected output



Actual output



Here is a MVE (also on JSFiddle: https://jsfiddle.net/0agcu3ps/2/ )



var data05 = [1,  2,  5,  7,     16];
var data10 = [1,2,3,4,5,6,7,8,12,16];
var labels05 = [
"Mon Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];
var labels10 = [
"Tue Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 10:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 14:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Tue Nov 13 2018 20:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Wed Nov 14 2018 10:00:00 GMT+0100",
"Thu Nov 15 2018 12:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];


window.updateChartData = function() {
var newData = ;
var newLabels =;
var intrval = document.getElementById("interval").value;
if(intrval == 5){
newData = data05;
}else if (intrval == 10) {
newData = data10;
}
pilleChart.data.datasets[0].data = newData;
pilleChart.data.datasets[0].labels = newLabels;
console.log("updating chart");
pilleChart.update();
}


function drawLineChart() {

// Create the chart.js data structure using 'labels' and 'data'
var pilleData = {
labels: labels05,
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(200,200,205,0.7)",
borderColor: "rgba(151,187,205,0.9)",
borderWidth: 1,
fill: false,
label: 'Pilleniveau',
data: data05

}
]
};
var options = {
showLines: true,
responsive: true,
title: {
display: true,
text: 'Pilleniveau i beholder'
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 2,
borderColor: "rgba(200,170,150,1)",
borderWidth: 2,
label: {
enabled: false,
content: 'Test label'
}
}
]
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}
],
//Set X-Axis to date instead of labels
xAxes: [{
type: 'time',
time: {
unit: 'day'
}
}
]
}
}
var ctx = document.getElementById("pilleChart").getContext("2d");
// Instantiate a new chart
pilleChart = new Chart(ctx, {
type: 'line',
data: pilleData,
options: options
});
}

drawLineChart();


EDIT:
I have been reading on Chart.js -- using update() with time scale datasets and communicated with the poster, who shared his solution, which is essentially to destroy the chart and draw it anew. I am not satisfied with that approach...










share|improve this question

























  • You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

    – shawn
    Nov 22 '18 at 8:05













  • I boiled down the code....

    – JoSSte
    Nov 23 '18 at 17:28


















2















I have set up a chart.js chart which uses data from a mysql database. I have set up a rest service to provide tha data, with the ability to define the grouping ie. the data is measured a five minute intervals, but I group it in 3 hour segments with my SQL statement SELECT (75-round(avg(sensorValue),0)) as Value, Time FROM sensorpi.rawData GROUP BY UNIX_TIMESTAMP(time) DIV $default_interval ORDER BY time asc



I would like to be able to change the granularity of the data by defining a new granularity.



I created an update function to send the request to the backend, and update the chart.



The chart is updated, but the default has 61 entries (at the moment) and the most fine-grained data has 180 entries. What happens is that the data is just reduced to the first 61 items in the dataset.



My gut feeling is that it is the date label of the X-Axis which is causing the trouble. But i am at a loss as to how i can solve it.



Initial output



Expected output



Actual output



Here is a MVE (also on JSFiddle: https://jsfiddle.net/0agcu3ps/2/ )



var data05 = [1,  2,  5,  7,     16];
var data10 = [1,2,3,4,5,6,7,8,12,16];
var labels05 = [
"Mon Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];
var labels10 = [
"Tue Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 10:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 14:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Tue Nov 13 2018 20:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Wed Nov 14 2018 10:00:00 GMT+0100",
"Thu Nov 15 2018 12:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];


window.updateChartData = function() {
var newData = ;
var newLabels =;
var intrval = document.getElementById("interval").value;
if(intrval == 5){
newData = data05;
}else if (intrval == 10) {
newData = data10;
}
pilleChart.data.datasets[0].data = newData;
pilleChart.data.datasets[0].labels = newLabels;
console.log("updating chart");
pilleChart.update();
}


function drawLineChart() {

// Create the chart.js data structure using 'labels' and 'data'
var pilleData = {
labels: labels05,
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(200,200,205,0.7)",
borderColor: "rgba(151,187,205,0.9)",
borderWidth: 1,
fill: false,
label: 'Pilleniveau',
data: data05

}
]
};
var options = {
showLines: true,
responsive: true,
title: {
display: true,
text: 'Pilleniveau i beholder'
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 2,
borderColor: "rgba(200,170,150,1)",
borderWidth: 2,
label: {
enabled: false,
content: 'Test label'
}
}
]
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}
],
//Set X-Axis to date instead of labels
xAxes: [{
type: 'time',
time: {
unit: 'day'
}
}
]
}
}
var ctx = document.getElementById("pilleChart").getContext("2d");
// Instantiate a new chart
pilleChart = new Chart(ctx, {
type: 'line',
data: pilleData,
options: options
});
}

drawLineChart();


EDIT:
I have been reading on Chart.js -- using update() with time scale datasets and communicated with the poster, who shared his solution, which is essentially to destroy the chart and draw it anew. I am not satisfied with that approach...










share|improve this question

























  • You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

    – shawn
    Nov 22 '18 at 8:05













  • I boiled down the code....

    – JoSSte
    Nov 23 '18 at 17:28














2












2








2


1






I have set up a chart.js chart which uses data from a mysql database. I have set up a rest service to provide tha data, with the ability to define the grouping ie. the data is measured a five minute intervals, but I group it in 3 hour segments with my SQL statement SELECT (75-round(avg(sensorValue),0)) as Value, Time FROM sensorpi.rawData GROUP BY UNIX_TIMESTAMP(time) DIV $default_interval ORDER BY time asc



I would like to be able to change the granularity of the data by defining a new granularity.



I created an update function to send the request to the backend, and update the chart.



The chart is updated, but the default has 61 entries (at the moment) and the most fine-grained data has 180 entries. What happens is that the data is just reduced to the first 61 items in the dataset.



My gut feeling is that it is the date label of the X-Axis which is causing the trouble. But i am at a loss as to how i can solve it.



Initial output



Expected output



Actual output



Here is a MVE (also on JSFiddle: https://jsfiddle.net/0agcu3ps/2/ )



var data05 = [1,  2,  5,  7,     16];
var data10 = [1,2,3,4,5,6,7,8,12,16];
var labels05 = [
"Mon Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];
var labels10 = [
"Tue Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 10:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 14:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Tue Nov 13 2018 20:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Wed Nov 14 2018 10:00:00 GMT+0100",
"Thu Nov 15 2018 12:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];


window.updateChartData = function() {
var newData = ;
var newLabels =;
var intrval = document.getElementById("interval").value;
if(intrval == 5){
newData = data05;
}else if (intrval == 10) {
newData = data10;
}
pilleChart.data.datasets[0].data = newData;
pilleChart.data.datasets[0].labels = newLabels;
console.log("updating chart");
pilleChart.update();
}


function drawLineChart() {

// Create the chart.js data structure using 'labels' and 'data'
var pilleData = {
labels: labels05,
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(200,200,205,0.7)",
borderColor: "rgba(151,187,205,0.9)",
borderWidth: 1,
fill: false,
label: 'Pilleniveau',
data: data05

}
]
};
var options = {
showLines: true,
responsive: true,
title: {
display: true,
text: 'Pilleniveau i beholder'
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 2,
borderColor: "rgba(200,170,150,1)",
borderWidth: 2,
label: {
enabled: false,
content: 'Test label'
}
}
]
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}
],
//Set X-Axis to date instead of labels
xAxes: [{
type: 'time',
time: {
unit: 'day'
}
}
]
}
}
var ctx = document.getElementById("pilleChart").getContext("2d");
// Instantiate a new chart
pilleChart = new Chart(ctx, {
type: 'line',
data: pilleData,
options: options
});
}

drawLineChart();


EDIT:
I have been reading on Chart.js -- using update() with time scale datasets and communicated with the poster, who shared his solution, which is essentially to destroy the chart and draw it anew. I am not satisfied with that approach...










share|improve this question
















I have set up a chart.js chart which uses data from a mysql database. I have set up a rest service to provide tha data, with the ability to define the grouping ie. the data is measured a five minute intervals, but I group it in 3 hour segments with my SQL statement SELECT (75-round(avg(sensorValue),0)) as Value, Time FROM sensorpi.rawData GROUP BY UNIX_TIMESTAMP(time) DIV $default_interval ORDER BY time asc



I would like to be able to change the granularity of the data by defining a new granularity.



I created an update function to send the request to the backend, and update the chart.



The chart is updated, but the default has 61 entries (at the moment) and the most fine-grained data has 180 entries. What happens is that the data is just reduced to the first 61 items in the dataset.



My gut feeling is that it is the date label of the X-Axis which is causing the trouble. But i am at a loss as to how i can solve it.



Initial output



Expected output



Actual output



Here is a MVE (also on JSFiddle: https://jsfiddle.net/0agcu3ps/2/ )



var data05 = [1,  2,  5,  7,     16];
var data10 = [1,2,3,4,5,6,7,8,12,16];
var labels05 = [
"Mon Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];
var labels10 = [
"Tue Nov 12 2018 02:00:00 GMT+0100",
"Mon Nov 12 2018 10:00:00 GMT+0100",
"Mon Nov 12 2018 12:00:00 GMT+0100",
"Tue Nov 13 2018 14:00:00 GMT+0100",
"Tue Nov 13 2018 18:00:00 GMT+0100",
"Tue Nov 13 2018 20:00:00 GMT+0100",
"Wed Nov 14 2018 02:00:00 GMT+0100",
"Wed Nov 14 2018 10:00:00 GMT+0100",
"Thu Nov 15 2018 12:00:00 GMT+0100",
"Thu Nov 15 2018 14:00:00 GMT+0100"];


window.updateChartData = function() {
var newData = ;
var newLabels =;
var intrval = document.getElementById("interval").value;
if(intrval == 5){
newData = data05;
}else if (intrval == 10) {
newData = data10;
}
pilleChart.data.datasets[0].data = newData;
pilleChart.data.datasets[0].labels = newLabels;
console.log("updating chart");
pilleChart.update();
}


function drawLineChart() {

// Create the chart.js data structure using 'labels' and 'data'
var pilleData = {
labels: labels05,
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(200,200,205,0.7)",
borderColor: "rgba(151,187,205,0.9)",
borderWidth: 1,
fill: false,
label: 'Pilleniveau',
data: data05

}
]
};
var options = {
showLines: true,
responsive: true,
title: {
display: true,
text: 'Pilleniveau i beholder'
},
legend: {
display: false
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 2,
borderColor: "rgba(200,170,150,1)",
borderWidth: 2,
label: {
enabled: false,
content: 'Test label'
}
}
]
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}
],
//Set X-Axis to date instead of labels
xAxes: [{
type: 'time',
time: {
unit: 'day'
}
}
]
}
}
var ctx = document.getElementById("pilleChart").getContext("2d");
// Instantiate a new chart
pilleChart = new Chart(ctx, {
type: 'line',
data: pilleData,
options: options
});
}

drawLineChart();


EDIT:
I have been reading on Chart.js -- using update() with time scale datasets and communicated with the poster, who shared his solution, which is essentially to destroy the chart and draw it anew. I am not satisfied with that approach...







javascript chart.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 20:53







JoSSte

















asked Nov 20 '18 at 19:50









JoSSteJoSSte

1,07921833




1,07921833













  • You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

    – shawn
    Nov 22 '18 at 8:05













  • I boiled down the code....

    – JoSSte
    Nov 23 '18 at 17:28



















  • You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

    – shawn
    Nov 22 '18 at 8:05













  • I boiled down the code....

    – JoSSte
    Nov 23 '18 at 17:28

















You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

– shawn
Nov 22 '18 at 8:05







You had better provide a Minimal, Complete, and Verifiable example (with a few mock data). stackoverflow.com/help/mcve

– shawn
Nov 22 '18 at 8:05















I boiled down the code....

– JoSSte
Nov 23 '18 at 17:28





I boiled down the code....

– JoSSte
Nov 23 '18 at 17:28












1 Answer
1






active

oldest

votes


















1





+50









It seems that there is a hacking method:



pilleChart.config.data.labels = newLabels


See: https://jsfiddle.net/0agcu3ps/3/



ps: there is a bug in your demo, the first item of var labels10 = ["Tue Nov 13 2018 02:00:00 GMT+0100", is wrong, it should be Nov 12 :D






share|improve this answer


























  • Oops. I fixed the "typo-bug". I am testing your hack atm.

    – JoSSte
    Nov 24 '18 at 20:55












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400538%2fupdating-chart-js-chart-with-dataset-of-different-size%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1





+50









It seems that there is a hacking method:



pilleChart.config.data.labels = newLabels


See: https://jsfiddle.net/0agcu3ps/3/



ps: there is a bug in your demo, the first item of var labels10 = ["Tue Nov 13 2018 02:00:00 GMT+0100", is wrong, it should be Nov 12 :D






share|improve this answer


























  • Oops. I fixed the "typo-bug". I am testing your hack atm.

    – JoSSte
    Nov 24 '18 at 20:55
















1





+50









It seems that there is a hacking method:



pilleChart.config.data.labels = newLabels


See: https://jsfiddle.net/0agcu3ps/3/



ps: there is a bug in your demo, the first item of var labels10 = ["Tue Nov 13 2018 02:00:00 GMT+0100", is wrong, it should be Nov 12 :D






share|improve this answer


























  • Oops. I fixed the "typo-bug". I am testing your hack atm.

    – JoSSte
    Nov 24 '18 at 20:55














1





+50







1





+50



1




+50





It seems that there is a hacking method:



pilleChart.config.data.labels = newLabels


See: https://jsfiddle.net/0agcu3ps/3/



ps: there is a bug in your demo, the first item of var labels10 = ["Tue Nov 13 2018 02:00:00 GMT+0100", is wrong, it should be Nov 12 :D






share|improve this answer















It seems that there is a hacking method:



pilleChart.config.data.labels = newLabels


See: https://jsfiddle.net/0agcu3ps/3/



ps: there is a bug in your demo, the first item of var labels10 = ["Tue Nov 13 2018 02:00:00 GMT+0100", is wrong, it should be Nov 12 :D







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 2:51

























answered Nov 24 '18 at 2:43









shawnshawn

3,375718




3,375718













  • Oops. I fixed the "typo-bug". I am testing your hack atm.

    – JoSSte
    Nov 24 '18 at 20:55



















  • Oops. I fixed the "typo-bug". I am testing your hack atm.

    – JoSSte
    Nov 24 '18 at 20:55

















Oops. I fixed the "typo-bug". I am testing your hack atm.

– JoSSte
Nov 24 '18 at 20:55





Oops. I fixed the "typo-bug". I am testing your hack atm.

– JoSSte
Nov 24 '18 at 20:55




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400538%2fupdating-chart-js-chart-with-dataset-of-different-size%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini