代码之家  ›  专栏  ›  技术社区  ›  Christian Gallarmin

如何在chart.js中将数据从高到低排序

  •  2
  • Christian Gallarmin  · 技术社区  · 7 年前

    我的代码

    var ctx_1 = document.getElementById('non_200_pages').getContext('2d');
    var myChart_1 = new Chart(ctx_1, {
    type: 'horizontalBar',
    data: {
        labels: ["Total","301 Redirect","Broken Pages (4xx Errors)","Uncategorised HTTP Response Codes","5xx Errors","Unauthorised Pages","Non-301 Redirects"],
        datasets: [{
    
            data: [ {{ $array_non_200_pages[0] }}, {{ $array_non_200_pages[1] }}, {{ $array_non_200_pages[2] }}, {{ $array_non_200_pages[3] }}, {{ $array_non_200_pages[4] }}, {{ $array_non_200_pages[5] }}, {{ $array_non_200_pages[6]}} ],
            backgroundColor: [ 
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)',
                'rgba(237, 56, 98, 1.0)'
            ]
    
        }]
    },
    options: {
        showAllTooltips: true,
        tooltips: {
              enabled: true,
              displayColors: false,
              yPadding: 20,
              xPadding: 30,
              caretSize: 10,
              backgroundColor: 'rgba(240, 240, 240, 1)',
              bodyFontSize: 16,
              bodyFontColor: 'rgb(50, 50, 50)',
              borderColor: 'rgba(0,0,0,1)',
              borderWidth: 1,
              cornerRadius: 0,
              yAlign: 'bottom',
              xAlign: 'center',
              position: 'custom',
              custom: function(tooltip) {
                if (!tooltip) return;
                // disable displaying the color box;
                tooltip.displayColors = false;
              },
              callbacks: {
                // use label callback to return the desired label
                label: function(tooltipItem, data) {
                  return tooltipItem.yLabel + " : " + tooltipItem.xLabel ;
                },
                // remove title
                title: function(tooltipItem, data) {
                  return;
                }
            }
        },
        responsive: false,
        legend: { display: false },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                },
                gridLines: {
                    display: false
                },
            }],
            xAxes: [{
                ticks: {
                    stepSize:5,
                    display: false
                },
                gridLines: {
                    drawBorder: false,
                }
            }],
        },
        plugins: {
            datalabels: {
        align: 'end',
        anchor: 'end',        
        backgroundColor: function(context) {
          return context.dataset.backgroundColor;
        },
        borderRadius: 4,
        color: 'white',
        formatter: Math.round
      }
    }
    }
    });
    

    My work

    我的预期产出 Expected work

    根据这张照片,我希望我的总数是这样的,并且有一个值 {{$array_non_200_pages[0] }}

    1 回复  |  直到 7 年前
        1
  •  4
  •   Aagam Jain    7 年前

    先排序,然后分配 newArrayData 数据属性和 newArrayLabel 为配置对象的属性添加标签。

    arrayLabel = ["Total", "301 Redirect", "Broken Pages (4xx Errors)", "Uncategorised HTTP Response Codes", "5xx Errors", "Unauthorised Pages", "Non-301 Redirects"]
    
    arrayData = [16, 1, 14, 0, 0, 0, 1];
    
    arrayOfObj = arrayLabel.map(function(d, i) {
      return {
        label: d,
        data: arrayData[i] || 0
      };
    });
    
    sortedArrayOfObj = arrayOfObj.sort(function(a, b) {
      return b.data>a.data;
    });
    
    newArrayLabel = [];
    newArrayData = [];
    sortedArrayOfObj.forEach(function(d){
      newArrayLabel.push(d.label);
      newArrayData.push(d.data);
    });
    
    console.log(newArrayLabel);
    console.log(newArrayData);