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

[亡魂溺海] 提交于 2019-12-30 07:11:43

问题


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. insert some data in between two points;
  3. reload the whole chart without replacing the canvas with a completely new chart?

回答1:


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/




回答2:


I came to stackoverflow having the same question so what I have done is created a plugin that allows you to add a sort function to ChartJS

https://github.com/scotthsieh0503/chartjs-plugin-sort

it also allows you to pass in a 'pre-sorted' array as a reference for sorting



来源:https://stackoverflow.com/questions/31399991/how-can-i-sort-insert-in-between-and-update-full-dataset-in-chartjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!