chartjs modify data

linkaiyi
Follow

Modify data for chartjs

(function () {
    "use strict";
    var randomScalingFactor = function() {
        return Math.round(Math.random() * 100);
    };

    var color = Chart.helpers.color;
    var config = {
        type: 'radar',
        data: {
            labels: ['拜访目的1', '拜访目的2', '拜访目的3', '拜访目的4', '拜访目的5'],
            datasets: [{
                label: '计划拜访次数',
                backgroundColor: color('#866eff').alpha(0.2).rgbString(),
                borderColor: '#866eff',
                pointBorderColor: '#866eff',
                data: [
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor()
                ]
            }, {
                label: '实际拜访次数',
                backgroundColor: color('#fe731c').alpha(0.2).rgbString(),
                borderColor: '#fe731c',
                pointBorderColor: '#fe731c',
                data: [
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor(),
                    randomScalingFactor()
                ]
            }]
        },
        options: {
            legend: {
                position: 'top',
            },
            title: {
                display: true,
                text: '拜访次数分布by拜访目的'
            },
            scale: {
                ticks: {
                    beginAtZero: true
                }
            }
        }
    };

    window.onload = function() {
        window.myRadar = new Chart(document.getElementById('radar-chart'), config);
    };

    document.getElementById('randomizeData').addEventListener('click', function() {
        config.data.datasets.forEach(function(dataset) {
            dataset.data = dataset.data.map(function() {
                return randomScalingFactor();
            });
        });

        window.myRadar.update();
    });

    var colorNames = Object.keys(window.chartColors);
    document.getElementById('addDataset').addEventListener('click', function() {
        var colorName = colorNames[config.data.datasets.length % colorNames.length];
        var newColor = window.chartColors[colorName];

        var newDataset = {
            label: 'Dataset ' + config.data.datasets.length,
            borderColor: newColor,
            backgroundColor: color(newColor).alpha(0.2).rgbString(),
            pointBackgroundColor: newColor,
            data: [],
        };

        for (var index = 0; index < config.data.labels.length; ++index) {
            newDataset.data.push(randomScalingFactor());
        }

        config.data.datasets.push(newDataset);
        window.myRadar.update();
    });

    document.getElementById('addData').addEventListener('click', function() {
        if (config.data.datasets.length > 0) {
            config.data.labels.push('dataset #' + config.data.labels.length);

            config.data.datasets.forEach(function(dataset) {
                dataset.data.push(randomScalingFactor());
            });

            window.myRadar.update();
        }
    });

    document.getElementById('removeDataset').addEventListener('click', function() {
        config.data.datasets.splice(0, 1);
        window.myRadar.update();
    });

    document.getElementById('removeData').addEventListener('click', function() {
        config.data.labels.pop(); // remove the label first

        config.data.datasets.forEach(function(dataset) {
            dataset.data.pop();
        });

        window.myRadar.update();
    });
})(jQuery)
Object has 0 attachments

Was this article helpful?

This article is viewed 57 times!

Recent Viewed Articles

Related Articles

0 Comments

Leave a Comment

Support