0

Update:

var voltagedata1 = [];
    batterybank1.forEach(function(element){
        var voltage = {x: element.timestamp, y:element.voltage};
        voltagedata1.push(voltage);
})
data: voltagedata1


I have an array which I want to iterate through in the data field of the Chart.js chart.

This code works for example:

data: [
    {x: '2019-08-12 09:40:15', y:4}, {x: '2019-08-13 09:40:15', y:5}, {x: '2019-08-14 09:40:15', y:6},
],

Then trying to iterate through the array as follows:

batterybank1.forEach(function(element){
    console.log(element)
    "{x:\'element.timestamp', y:element.voltage},"
})

The console.log(element) gives a correct output, however the chart is not getting updated... on the console I am getting no warnings/errors - just the graph does not output.

The following does not work either, only the console.log is outputted but the graph is not updated.

batterybank1.forEach(function(element){
    console.log(element)
    "{x:\'2019-08-12 09:40:15', 1},"
})
4
  • 1
    why do you have those strings after console.log? Commented Aug 12, 2019 at 10:37
  • @MattEllen, the data field accepts data in the format as {x: , y: }, {x: , y: }, ... as far as I know - so I was trying to output that using the for loop in the data field. Refer to the first code part where I gave an example that works. Commented Aug 12, 2019 at 10:38
  • OK. Putting a string there will not output it to the console, or return it from a function. It will do nothing. How do you add data to a chart with chart.js? Commented Aug 12, 2019 at 10:45
  • I do not want that string to output to the console. Refer to my first example to see how data is added to chart.js - which is what I wanted to replicate then in the for loop Commented Aug 12, 2019 at 10:48

1 Answer 1

1

It's explained in the documentation, that you add your points to the chart one at a time, like so:

batterybank1.forEach(function(element){
       chart.data.datasets[0].data.push(element);
})

This is assuming that element is in the same format as the points in data.

After that you need to call chart.update(); to show the new data.

below is a working example:

let data = [{t: '2019-08-12 09:40:15', y:4}, {t: '2019-08-13 09:40:15', y:5}, {t: '2019-08-14 09:40:15', y:6}, {t: '2019-08-15 09:40:15', y:7}];

var chart = new Chart(document.getElementById('cht'), { type:'bar', data:
{
  datasets:[{
	label: 'CHRT - Chart.js Corporation',
	backgroundColor: '#ff0000',
  borderColor: '#ff0000',
  type: 'bar',
	pointRadius: 0,
	fill: false,
	lineTension: 0,
	borderWidth: 2,
  data: data}]
}, options:{
        scales: {
            xAxes: [
            {
              type: 'time',
						  distribution: 'series',
						  ticks: 
              {
							  source: 'data',
							  autoSkip: true
						  }
            }]
        }
    } });
    
document.getElementById('addPoints').addEventListener('click', function() {
      let extra = [{t: '2019-08-16 09:40:15', y:4}, {t: '2019-08-17 09:40:15', y:5}, {t: '2019-08-18 09:40:15', y:6}, {t: '2019-08-19 09:40:15', y:7}];
      extra.forEach(p =>
      {
        chart.data.datasets[0].data.push(p);
      });
      
      chart.update();
		});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.js"></script>

<button id="addPoints">Add Points</button>

<canvas id="cht" class="chartjs" width="770" height="385" style="display: block; width: 770px; height: 385px;">

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that! I adopted a similar method - check update in original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.