• <legend id='qytk0'><style id='qytk0'><dir id='qytk0'><q id='qytk0'></q></dir></style></legend>

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

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

        <tfoot id='qytk0'></tfoot>

      1. 如何在 Chart.js 中为条形设置默认颜色

        How to set default colour for bars in Chart.js(如何在 Chart.js 中为条形设置默认颜色)
              <tbody id='kB5lu'></tbody>

              • <tfoot id='kB5lu'></tfoot>

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

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

                  <bdo id='kB5lu'></bdo><ul id='kB5lu'></ul>

                  <legend id='kB5lu'><style id='kB5lu'><dir id='kB5lu'><q id='kB5lu'></q></dir></style></legend>
                  本文介绍了如何在 Chart.js 中为条形设置默认颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这似乎微不足道,但尽我所能尝试我似乎无法找到如何为 chart.js 中的条形设置默认颜色.

                  This seems stupidly trivial but try as I might I cannot seem to find how to set a default colour for bars in chart.js.

                  我的图表正在从 ajax 请求中获取数据,并且图表呈现得很好.但是,无论是更新 Chart.defaults.global.defaultColor 还是将 defaultColor 作为选项添加到数据集都没有任何效果.

                  My charts is taking its data from an ajax request and the chart is rendering just fine. However neither updating Chart.defaults.global.defaultColor nor adding defaultColor to the dataset as an option has any effect.

                  我非常感谢任何人在这里指出我正确的方向.

                  I would very much appreciate anyone pointing me in the right direction here.

                  $.ajax({
                  type: 'GET',
                  async: true,
                  url: "{{route('stats.monthlyData')}}",
                  dataType: 'json',
                  success: function (response) {
                      var labels = [];
                      var data = [];
                      $.each(response, function () {
                          labels.push(this.month_name);
                          data.push(this.record_count);
                      });
                      drawChart('# of Records', labels, data);
                  }
                  });
                  
                  function drawChart(label, labels, data){
                      var ctx = document.getElementById("chart");
                      //Chart.defaults.global.defaultColor = "#3498db"; Tried this but does not work
                      var myChart = new Chart(ctx, {
                          type: 'bar',
                          data: {
                              labels: labels,
                              datasets: [{
                                  label: label,
                                  data: data,
                                  //defaultColor: ['#3498db'], Tried this but does not work
                                  backgroundColor: ['#3498db'], //Only the first bar gets set
                                  borderColor: [],
                                  borderWidth: 1
                              }]
                          },
                          options: {
                              scales: {
                                  yAxes: [{
                                      ticks: {
                                          beginAtZero:true
                                      }
                                  }]
                              }
                          }
                      });
                  
                  }
                  

                  谢谢.

                  更新

                  如果它对任何人都有帮助,问题是我将 backgroundColor 属性设置为只有一个条目的数组.如果你想为所有列默认它,那么它应该只设置为一个字符串.感谢 @mp77 让我注意到这个问题.

                  In case it helps anyone, the issue was that I was setting the backgroundColor property to an array with only a single entry. If you want to default it for all columns then it should only be set to a string. Credit to @mp77 for leading me to notice this issue.

                  推荐答案

                  您需要像这样在 datasets 数组中使用 fillColor 属性 -(而不是 borderColor,尝试 strokeColor 如下所示)

                  You need to use fillColor property in your datasets array like this - (and instead of borderColor, try strokeColor like below)

                  datasets: [{
                      label: label,
                      data: data,
                      fillColor: "rgba(14,72,100,1)", //version >2 useus background color
                      strokeColor: "brown",
                      borderWidth: 1
                  }]
                  

                  可以从 chartjs 的一个演示中看到一个完整的工作示例这里

                  A full working example can be seen from one of the demos of chartjs here

                  这篇关于如何在 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='wwDqy'><style id='wwDqy'><dir id='wwDqy'><q id='wwDqy'></q></dir></style></legend>

                    <tfoot id='wwDqy'></tfoot>
                  • <small id='wwDqy'></small><noframes id='wwDqy'>

                    • <bdo id='wwDqy'></bdo><ul id='wwDqy'></ul>

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