<i id='F52tp'><tr id='F52tp'><dt id='F52tp'><q id='F52tp'><span id='F52tp'><b id='F52tp'><form id='F52tp'><ins id='F52tp'></ins><ul id='F52tp'></ul><sub id='F52tp'></sub></form><legend id='F52tp'></legend><bdo id='F52tp'><pre id='F52tp'><center id='F52tp'></center></pre></bdo></b><th id='F52tp'></th></span></q></dt></tr></i><div id='F52tp'><tfoot id='F52tp'></tfoot><dl id='F52tp'><fieldset id='F52tp'></fieldset></dl></div>

    1. <legend id='F52tp'><style id='F52tp'><dir id='F52tp'><q id='F52tp'></q></dir></style></legend>
      1. <small id='F52tp'></small><noframes id='F52tp'>

        <tfoot id='F52tp'></tfoot>
        • <bdo id='F52tp'></bdo><ul id='F52tp'></ul>

      2. chart.js 条形图根据值改变颜色

        时间:2023-11-02
        <tfoot id='PJc60'></tfoot>
          <tbody id='PJc60'></tbody>
      3. <i id='PJc60'><tr id='PJc60'><dt id='PJc60'><q id='PJc60'><span id='PJc60'><b id='PJc60'><form id='PJc60'><ins id='PJc60'></ins><ul id='PJc60'></ul><sub id='PJc60'></sub></form><legend id='PJc60'></legend><bdo id='PJc60'><pre id='PJc60'><center id='PJc60'></center></pre></bdo></b><th id='PJc60'></th></span></q></dt></tr></i><div id='PJc60'><tfoot id='PJc60'></tfoot><dl id='PJc60'><fieldset id='PJc60'></fieldset></dl></div>
          • <bdo id='PJc60'></bdo><ul id='PJc60'></ul>

                  <small id='PJc60'></small><noframes id='PJc60'>

                  <legend id='PJc60'><style id='PJc60'><dir id='PJc60'><q id='PJc60'></q></dir></style></legend>

                • 本文介绍了chart.js 条形图根据值改变颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  function argMax(array) {
                    return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] > r[0] ? a : r))[1];
                  }
                  
                  // dummy data
                  var data = [12, 19, 1, 14, 3, 10, 9];
                  var labels = data.map((x, i) => i.toString()); 
                  
                  // other data color
                  var color = data.map(x => 'rgba(75,192,192,0.4)');
                  
                  // change max color
                  color[argMax(data)] = 'red';
                  
                  var ctx = document.getElementById("myChart");
                  var myChart = new Chart(ctx, {
                          type: 'bar',
                          data: {
                              labels: labels,
                              datasets: [{
                                      label: 'value',
                                      data: data,
                                      backgroundColor: color,
                              }]
                          }
                  });
                  

                  大家好.我在 Chart.js 中找到了这段代码最大值栏 这个论坛.但我不知道如何更改它以显示最大的三个值.

                  Hey Guys. I found this code in Chart.js changing the color of the max value bar this forum. But i don't have a clue how to change it to show me the biggest three values.

                  如何更改此代码以不同颜色显示最大的三个值?

                  How can i change this code to show me the biggest three values in a different color?

                  推荐答案

                  给定一个名为data"的 number 值的 array,您可以从中创建一个排序数组.然后你 map 原始数据的值,根据其在已排序 array 中的位置返回适当的颜色.

                  Given an array of number values named "data", you create a sorted array out of it. Then you map the values of the original data, returning the appropriate color depending on its position in the sorted array.

                  const backgroundColors = data.map(v => sortedData.indexOf(v) >= data.length - 3 ? 'red' : 'green');
                  

                  请查看下面的可运行代码示例.

                  Please have a look at the runnable code sample below.

                  const labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O'];
                  const data = labels.map(l => Math.floor(Math.random() * 1000) + 1);
                  const sortedData = data.slice().sort((a, b) => a - b);
                  const backgroundColors = data.map(v => sortedData.indexOf(v) >= data.length - 3 ? 'red' : 'green');
                  
                  new Chart(document.getElementById('myChart'), {
                    type: 'bar',
                    data: {
                      labels: labels,
                      datasets: [{
                        label: "My Dataset",
                        data: data,
                        backgroundColor: backgroundColors
                      }]
                    },
                    options: {
                      legend: {
                        display: false
                      },
                      scales: {
                        yAxes: [{
                          ticks: {
                            beginAtZero: true
                          }
                        }]
                      }
                    }
                  });

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
                  <canvas id="myChart" height="90"></canvas>

                  这篇关于chart.js 条形图根据值改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Chart.JS 中显示饼图的数据子集,同时在悬停时仍显示超集? 下一篇:ChartJS – 有没有办法去除饼图周围的空白?

                  相关文章

                • <legend id='cyw1v'><style id='cyw1v'><dir id='cyw1v'><q id='cyw1v'></q></dir></style></legend>
                  • <bdo id='cyw1v'></bdo><ul id='cyw1v'></ul>
                  1. <small id='cyw1v'></small><noframes id='cyw1v'>

                  2. <tfoot id='cyw1v'></tfoot>

                  3. <i id='cyw1v'><tr id='cyw1v'><dt id='cyw1v'><q id='cyw1v'><span id='cyw1v'><b id='cyw1v'><form id='cyw1v'><ins id='cyw1v'></ins><ul id='cyw1v'></ul><sub id='cyw1v'></sub></form><legend id='cyw1v'></legend><bdo id='cyw1v'><pre id='cyw1v'><center id='cyw1v'></center></pre></bdo></b><th id='cyw1v'></th></span></q></dt></tr></i><div id='cyw1v'><tfoot id='cyw1v'></tfoot><dl id='cyw1v'><fieldset id='cyw1v'></fieldset></dl></div>