• <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 中为条形设置默认颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)

                    <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>