How can I sort, insert in between and update full dataset in chartjs?

前端 未结 2 1306
清歌不尽
清歌不尽 2021-01-06 13:47

I have a few issues with chartjs which simple update method won\'t solve.

I wonder if there is an option to:

  1. painlessly sort the datasets;
  2. ins
2条回答
  •  迷失自我
    2021-01-06 14:12

    There is no option built in, but it is pretty easy to write your own using the addData, removeData methods that Chart.js provides.

    var MyBarChartMethods = {
        // sort a dataset
        sort: function (chart, datasetIndex) {
            var data = []
            chart.datasets.forEach(function (dataset, i) {
                dataset.bars.forEach(function (bar, j) {
                    if (i === 0) {
                        data.push({
                            label: chart.scale.xLabels[j],
                            values: [bar.value]
                        })
                    } else 
                        data[j].values.push(bar.value)
                });
            })
    
            data.sort(function (a, b) {
                if (a.values[datasetIndex] > b.values[datasetIndex])
                    return -1;
                else if (a.values[datasetIndex] < b.values[datasetIndex])
                    return 1;
                else
                    return 0;
            })
    
            chart.datasets.forEach(function (dataset, i) {
                dataset.bars.forEach(function (bar, j) {
                    if (i === 0)
                        chart.scale.xLabels[j] = data[j].label;
                    bar.label = data[j].label;
                    bar.value = data[j].values[i];
                })
            });
            chart.update();
        },
        // reload data
        reload: function (chart, datasetIndex, labels, values) {
            var diff = chart.datasets[datasetIndex].bars.length - values.length;
            if (diff < 0) {
                for (var i = 0; i < -diff; i++)
                    chart.addData([0], "");
            } else if (diff > 0) {
                for (var i = 0; i < diff; i++)
                    chart.removeData();
            }
    
            chart.datasets[datasetIndex].bars.forEach(function (bar, i) {
                chart.scale.xLabels[i] = labels[i];
                bar.value = values[i];
            })
            chart.update();
        }
    }
    

    which you call like so (where myBarChart is your chart)

    // sort
    MyBarChartMethods.sort(myBarChart, 0)
    // reload - same number of values
    MyBarChartMethods.reload(myBarChart, 0, ["J", "F", "M", "A", "M", "J", "J"], [1, 2, 3, 4, 5, 6, 7])
    // reload - more values
    MyBarChartMethods.reload(myBarChart, 0, ["J", "F", "M", "A", "M", "J", "J", "A"], [1, 2, 3, 4, 5, 6, 7, 8])
    // reload - less values
    MyBarChartMethods.reload(myBarChart, 0, ["J", "F", "M", "A", "M"], [1, 2, 3, 4, 5])
    

    Inserting 2 points is special case of reload, so you can use the same function (or write your own based on that easily)


    Fiddle - http://jsfiddle.net/Lkdxxkfa/

提交回复
热议问题