是否有可能重复的标签(和他们的DATAS)合并到图表的js一列?

问题描述 投票:1回答:1

例如,如果我有数组['05 /二月/ 2019' ,'05 /二月/ 2019' ,'05 /二月/ 2019' ,'01 /二月/ 2019 ']为标签和阵列[' 2' , '5', '7', '4']为各自的值。

是否有可能只显示一列的所有元素'05 /月/ 2019' ,此列他们的价值观总结?

CODE FOR CHART JS

myChart = new Chart(document.getElementById("canvas"),{
    "type":temp.type,
    "data":{
        "labels":column_a,
        "datasets":[{
            "label":label_b,
            "data":column_b,
            "spanGaps": true,
            "fill":false,//true for charts like radar and polar area
            "backgroundColor":["rgba(255, 99, 132, 0.4)","rgba(255, 159, 64, 0.4)","rgba(255, 205, 86, 0.4)","rgba(75, 192, 192, 0.4)","rgba(54, 162, 235, 0.4)","rgba(153, 102, 255, 0.4)","rgba(201, 203, 207, 0.4)"],
            "borderWidth":1
            }
            ]},
            "options":{
                tooltips: {
                    callbacks: {
                        label: (tooltipItem, myChart) => {
                            const realValue = myChart.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
                            //const customValue = realValue.toFixed(2).replace(".", ",") + '%';
                            const customValue = realValue.toFixed(2).replace(".", ",");
                            const label = myChart.labels[tooltipItem.index] + ':';
                            return label + customValue;
                            }
                        }
                    },
                "scales":{
                    "yAxes":[{
                        "gridLines":{display:false},
                        "ticks":{
                            "suggestedMin":0,
                            "max":max_value_1
                        }}]}
                        }});
window.myChart = myChart
javascript arrays charts chart.js
1个回答
1
投票

你可以使用减少这一点。愿这可以帮助你!

 var label = ['05/Feb/2019', '05/Feb/2019', '05/Feb/2019', '01/Feb/2019'];
    var value = ['2', '5', '7', '4'];

    var obj = label.reduce(function(a, b, i) {
        if(!a.hasOwnProperty(b)) {
            a[b] = 0;
        }
        console.log(a);
        a[b] += Number(value[i]);
         return a;
    },{});

    var lab = Object.keys(obj);
    var val = Object.values(obj);
© www.soinside.com 2019 - 2024. All rights reserved.