Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)

I am trying to hot swap chart types based on select box changes. If data needs to be updated, it changes.

So for example, on page load I create a chart like this:

var config = {
     type: 'line',
     data: {
        labels: this.labels,
        datasets: [{
            label: 'Some Label',
            data: this.values
        }]
     },
     options: {
         responsive: true
     }
}
var context = document.getElementById('canvas').getContext('2d');
window.mychart = new Chart(context, config);

But then I change the combo box to a bar chart. I have tested the data with bar chart on page load, and it worked great.

Here's how I am trying to change the chart.

window.mychart.destroy();

// chartType = 'bar'
config.type = chartType;

var context = document.getElementById('canvas').getContext('2d');
window.mychart = new Chart(context, config);

window.mychart.update();
window.mychart.render();

But nothing happens. The line chart remains. How can I dynamically change the chart type? (Even if it means destroying & re-creating the chart canvas).

UPDATE

Note it looks like it is actually destroying the chart, but keeps redrawing a line chart, even though I do console.log(config.type); and it returns bar, not line

like image 977
Nathan Avatar asked Apr 30 '16 00:04

Nathan


People also ask

How do you change a chart type to a line graph?

To change the chart type in Excel, select a chart or one of the chart's elements. Then click the “Design” tab of the “Chart Tools” contextual tab in the Ribbon. Then click the “Change Chart Type” button in the “Type” button group.

What is dynamic charts in JavaScript?

JavaScript Live / Dynamic Charts & Graphs. Dynamic or Live charts are useful in displaying data that changes with time like stock price, temperature, real time sensor readings, etc. Dynamic Chart are also known as Real Time charts. Dynamic updates are supported by all chart types including line, area, column, bar, pie, etc.

How to create bar charts in HTML and JavaScript?

You can create bar charts in Chart.js by setting the type key to bar. By default, this will create charts with vertical bars. If you want to create charts with horizontal bars, you will have to set the type to horizontalBar.

How do I create a line chart in Node JS?

Chart.js allows you to create line charts by setting the type key to line. Here is an example: Here is an example: var lineChart = new Chart(speedCanvas, { type: 'line', data: speedData, options: chartOptions });

What is a bar chart used for?

A bar chart provides a way of showing data values represented as vertical bars. It is sometimes used to show trend data, and the comparison of multiple data sets side by side. The bar chart allows a number of properties to be specified for each dataset. These are used to set display properties for a specific dataset.


1 Answers

The Fix

  • Destroy the old chart (to remove event listeners and clear the canvas)
  • Make a deep copy of the config object
  • Change the type of the copy
  • Pass the copy instead of the original object.

Here is a working jsfiddle example

Example Overview:

var temp = jQuery.extend(true, {}, config);
temp.type = 'bar'; // The new chart type
myChart = new Chart(ctx, temp);

NOTE: Using version 2.0.1 of Chart.js

Why this works

Chart.js modifies the config object you pass in. Because of that you can not just change 'config.type'. You could go into the modified object and change everything to the type you want, but it is much easier to just save the original config object.

like image 128
L Bahr Avatar answered Sep 22 '22 15:09

L Bahr