question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Angular]How do I control the refresh timing with input binding other than with the numeric value

See original GitHub issue
@Input() a

...

this.chartObj = new Chart(this.el.nativeElement.getContext('2d'), {
	...
	options: {
		plugins: {
			streaming: {
				refresh: 1000, // onRefresh callback will be called every 1s
				onRefresh: (chart: Chart) => {
					// something
				}
			}
		}
	}
});

With the above snippet, I’d like to control the refresh the chart when I get the new input data a but apparently, the datasets only get updated every 1s.

I’ve tried using ngOnChanges cycle, pushing the new data to the datasets and update the chart instance but it didn’t seem to work.

this.chartObj.config.data.datasets.push({
  // label: 'Dataset ' + config.data.datasets.length,
  // backgroundColor: color(newColor).alpha(0.5).rgbString(),
  // borderColor: newColor,
  fill: false,
  lineTension: 0,
  data
});
this.chartObj.update();

What am I missing?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nagixcommented, Jun 21, 2018

If you are using strict type checking, run npm install @types/chart.js and use the following code:

import {Chart, ChartDataSets, ChartPoint} from 'chart.js';
import 'chartjs-plugin-streaming';

...

if (this.chartObj.data && this.chartObj.data.datasets) {
  this.chartObj.data.datasets.forEach(function(dataset: ChartDataSets, i: number) {
    (dataset.data as ChartPoint[]).push({
      x: Date.now(),
      y: data[i]
    });
  });
}
this.chartObj.update({
  duration: 0
});
1reaction
nagixcommented, Jun 21, 2018

A timestamp (for example, Date.now()) must be set to x property even if you don’t need to show the x-axis labels. To hide the x-axis labels, use the following code:

  options: {
    scales: {
      xAxes: [{
        type: 'realtime',
        display: false
      }]
    },
    ...
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Property binding best practices - Angular
Use the syntax for template expressions to help avoid side effects. In general, the correct syntax prevents you from assigning a value to...
Read more >
Angular 2 - [(ngModel)] not updating after [value] changes
1. The ngModelChange thing doesn't work either. It's really weird because the value in the input GETS UPDATED, but not the expression {{product....
Read more >
The "value" binding - Knockout.js
If this parameter is an observable value, the binding will update the element's value whenever the value changes. If the parameter isn't observable,...
Read more >
ngRepeat - AngularJS: API
When DOM elements are re-used, ngRepeat updates the scope for the element, which will automatically update any active bindings on the template. However,...
Read more >
Understanding Angular property binding and interpolation
String Interpolation · No visible side effects: The expression shouldn't attempt to modify the application's state in any way; it is only allowed ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found