b85411
b85411

Reputation: 10010

Morris js chart - changing settings dynamically

Is it possible to update a Morris chart dynamically? I know setData() will update the data, but I want to update the settings. Namely, the user being able to select if a bar chart is stacked or not.

I have tried:

bChart.stacked = true;
bChart.setData(response);

... because setData() will redraw. I also tried bChart.redraw();. There was no change.

Any ideas welcome.

Upvotes: 2

Views: 7486

Answers (1)

D4V1D
D4V1D

Reputation: 5849

You were 90% there. You would need to set bChart.options.stacked to true; then do bChart.redraw();.

Therefore, the code for toggling stacked bars is the following (if you are using jQuery):

jQuery(function($) {
    $('#stacked').on('change', function() {
        bChart.options.stacked = $(this).is(':checked');
        bChart.redraw();
    });
});

Providing that the checkbox toggling this option has a #stacked ID.

See this working JSFiddle.

Upvotes: 5

Related Questions