简体   繁体   中英

chart.js mixed chart types

I have this example from chart.js docs :

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ['Item 1', 'Item 2', 'Item 3'],
        datasets: [
            {
                type: 'bar',
                label: 'Bar Component',
                data: [10, 20, 30],
            },
            {
                type: 'line',
                label: 'Line Component',
                data: [30, 20, 10],
            }
        ]
    }
});

It works fine. However, my intention is to change the type dynamically.

I am aware that there is getDatasetMeta() method, that you can apply to your chart instance, and if I output the result it returns to the console I get something like this:

{
    bar: true,
    controller: ChartElement,
    data: Array[...],
    dataset: null,
    hidden: false,
    type: 'bar',
    ...
}

I can control the appearance of a certain dataset via hidden property, though if I change type to line and invoke update() it won't re-render.

So, is there a way to dynamically change dataset type?

Found a workaround here: Chart.js: Dynamic Changing of Chart Type (Line to Bar as Example)

Sad that it's not yet supported officially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM