【问题标题】:Edit styles for axes in ChartJS bar chart, when gridlines are hidden隐藏网格线时,在 ChartJS 条形图中编辑轴的样式
【发布时间】:2017-11-07 07:24:40
【问题描述】:

我有一个条形图,其网格线显示为 false。现在,我需要为轴线(x 和 y)设置宽度和颜色。我可以将这些值设置为:

options:{
  scales: {
    xAxes: [{
      gridLines: {
          color: "rgba(0,0,0,1)",
          lineWidth: 1
      }
    }],
    yAxes: [{
      gridLines: {
          color: "rgba(0,0,0,1)",
          lineWidth: 1
      }
    }],
  }
}

但是,当我想隐藏网格线时,我无法设置样式(宽度或颜色),只能用于轴。

options:{
  scales: {
    xAxes: [{
      gridLines: {
        display:false
      }
    }],
    yAxes: [{
      gridLines: {
        display:false
      }
    }],
  }
}

我尝试过 scaleLineColor ,但似乎也没有用。这是我的完整代码:

    var q1 = document.getElementById("q1").getContext('2d');
    var q1Chart = new Chart(q1, {
      type: 'bar',
      data: {
        labels: ["Very Good(9-10)","Good(8-7)","OK(5-6)","Bad (3-4)","Cant be worse (0-2)"],
        datasets: [{
          label: "Our Performance",
          data: [19.56,37.90,1.29,30.36,10.89],
          backgroundColor: ["#d93030","#288fba","#367fa9","#367fa9","#367fa9"],
          datalabels: {
            align: 'top',
            anchor: 'end',
            font: {
              size: 18,
              weight: 900,
            },
            formatter: function(value) {
              var label = value;
              var keys, klen, k;
              return label + '%';
            }
          }
        }]
      },
      scaleLineColor: "rgba(0,0,0,1)",
      options:{
                scales: {
          xAxes: [{
            barThickness : 50,
            ticks: {
              fontColor: "black",
              fontSize: 20,
              autoSkip: false,
            },
            gridLines: {
              display:false
            }
          }],
          yAxes: [{
            ticks: {
              fontColor: "black",
              fontSize: 20,
              beginAtZero: true,
              stepSize: 10
            },
            gridLines: {
              display:false
            }
          }],
        },
        responsive: false,
        maintainAspectRatio: true,
        legend: {
            display: false
         },
         tooltips: {
            enabled: false
         },
         layout: {
          padding: {
              left: 0,
              right: 0,
              top: 30,
              bottom: 0
          }
        }
      },
    });

【问题讨论】:

    标签: javascript html css chart.js chartjs-2.6.0


    【解决方案1】:

    我知道这样做的唯一方法是编写一个插件来做到这一点

    Chart.pluginService.register({
      afterDraw: function (chart, easing) {
        if (chart.config.options && chart.config.options.scales) {
          if (chart.config.options.scales.xAxes)
            chart.config.options.scales.xAxes.forEach(function (xAxisConfig) {
              if (!xAxisConfig.color)
                return;
    
              var ctx = chart.chart.ctx;
              var chartArea = chart.chartArea;
              var xAxis = chart.scales[xAxisConfig.id];
    
              // just draw the scale again with different colors
              var color = xAxisConfig.gridLines.color;
              xAxisConfig.gridLines.color = xAxisConfig.color;
              xAxis.draw(chartArea);
              xAxisConfig.gridLines.color = color;
            });
    
          if (chart.config.options.scales.yAxes)
            chart.config.options.scales.yAxes.forEach(function (yAxisConfig) {
              if (!yAxisConfig.color)
                return;
    
              var ctx = chart.chart.ctx;
              var chartArea = chart.chartArea;
              var yAxis = chart.scales[yAxisConfig.id];
    
              var color = yAxisConfig.gridLines.color;
              yAxisConfig.gridLines.color = yAxisConfig.color;
              yAxis.draw(chartArea);
              yAxisConfig.gridLines.color = color;
    
              ctx.restore();
            });
        }
      }
    });
    

    Chart.pluginService.register({
      afterDraw: function (chart, easing) {
        if (chart.config.options && chart.config.options.scales) {
          if (chart.config.options.scales.xAxes)
            chart.config.options.scales.xAxes.forEach(function (xAxisConfig) {
              if (!xAxisConfig.color)
                return;
    
              var ctx = chart.chart.ctx;
              var chartArea = chart.chartArea;
              var xAxis = chart.scales[xAxisConfig.id];
    
              // just draw the scale again with different colors
              var color = xAxisConfig.gridLines.color;
              xAxisConfig.gridLines.color = xAxisConfig.color;
              xAxis.draw(chartArea);
              xAxisConfig.gridLines.color = color;
            });
    
          if (chart.config.options.scales.yAxes)
            chart.config.options.scales.yAxes.forEach(function (yAxisConfig) {
              if (!yAxisConfig.color)
                return;
    
              var ctx = chart.chart.ctx;
              var chartArea = chart.chartArea;
              var yAxis = chart.scales[yAxisConfig.id];
    
              var color = yAxisConfig.gridLines.color;
              yAxisConfig.gridLines.color = yAxisConfig.color;
              yAxis.draw(chartArea);
              yAxisConfig.gridLines.color = color;
    
              ctx.restore();
            });
        }
      }
    });
    
    
    var q1 = document.getElementById("q1").getContext('2d');
    var q1Chart = new Chart(q1, {
      type: 'bar',
      data: {
        labels: ["Very Good(9-10)", "Good(8-7)", "OK(5-6)", "Bad (3-4)", "Cant be worse (0-2)"],
        datasets: [{
          label: "Our Performance",
          data: [19.56, 37.90, 1.29, 30.36, 10.89],
          backgroundColor: ["#d93030", "#288fba", "#367fa9", "#367fa9", "#367fa9"],
          datalabels: {
            align: 'top',
            anchor: 'end',
            font: {
              size: 18,
              weight: 900,
            },
            formatter: function (value) {
              var label = value;
              var keys, klen, k;
              return label + '%';
            }
          }
        }]
      },
      scaleLineColor: "rgba(0,0,0,1)",
      options: {
        scales: {
          xAxes: [{
            color: 'yellow',
            barThickness: 100,
            ticks: {
              fontColor: "black",
              fontSize: 20,
              autoSkip: false,
            },
            gridLines: {
              display: false
            }
          }],
          yAxes: [{
            color: 'yellow',
            barThickness: 150,
            ticks: {
              fontColor: "black",
              fontSize: 20,
              beginAtZero: true,
              stepSize: 10
            },
            gridLines: {
              display: false
            }
          }],
        },
        responsive: true,
        maintainAspectRatio: true,
        legend: {
          display: false
        },
        tooltips: {
          enabled: false
        },
        layout: {
          padding: {
            left: 0,
            right: 0,
            top: 30,
            bottom: 0
          }
        }
      },
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.js"></script>
    <canvas id="q1" height=""></canvas>

    【讨论】:

    • 非常感谢。像魅力一样工作!
    猜你喜欢
    • 2022-12-08
    • 2016-11-30
    • 2020-04-30
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多