<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 条形图根据值改变颜色

        chart.js bar chart change color based on value(chart.js 条形图根据值改变颜色)
        <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 条形图根据值改变颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  ChartJS Version 3 - common legend for multiple line charts(ChartJS 版本 3 - 多个折线图的常用图例)
                  Charts.js scales yaxes ticks min max doesnt work(Charts.js 缩放 yaxes 刻度 min max 不起作用)
                  How to expand the slice of donut chart in chartjs?(如何在chartjs中扩展圆环图的切片?)
                  Chart.js yAxes Ticks stepSize not working (fiddle)(Chart.js yAxes Ticks stepSize 不起作用(小提琴))
                  Rounded corners on chartJS v.2 - bar charts (with negative values)(chartJS v.2 上的圆角 - 条形图(带负值))
                  How to hide value in Chart JS bar(如何在 Chart JS 栏中隐藏值)
                • <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>
                          <tbody id='cyw1v'></tbody>