【问题标题】:Darkening certain ticks in a chart使图表中的某些刻度变暗
【发布时间】:2018-10-22 18:20:11
【问题描述】:

到目前为止的图表:

我正在尝试使 0,5 和 11 所在的刻度变暗,但我似乎无法在 chart.js 网站上找到任何信息。我正在使用 chart.js2 和 angular 5。 我隐藏了其他标签,以便仅根据数据的长度显示某些标签。

  ngOnInit() {
    this.chart = new Chart('canvas', {
      type: 'line',
      data: {
        labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
        datasets: [
          {
            label: 'test',
            data: [
              100, 200, 300, 500, 100, 900, 100, 200, 500
            ],
            borderColor: '#549cef',
            backgroundColor: 'rgba(0,0,0,0.0)',
            pointBackgroundColor: 'white',
            pointRadius: 10,
            pointBorderWidth: this.getThick(),
            pointHoverBackgroundColor: '#549cef',
            borderWidth: 3
          }
        ]
      },
      options: {
        responsive: true,
        scales: {
          xAxes:[{
            gridLines: {
              drawBorder: true,
              drawOnChartArea: false,
            },
            ticks: {
              callback: function(dataLabel, index,data){
                return  data.length < 5? '':
                 data.length<12 && (index==0 || index==(data.length-1)) ? '':
                  (index==0 || index == 5 || index == 11)? dataLabel: '';
              }
            }
          }],},

【问题讨论】:

    标签: typescript charts linechart chart.js2


    【解决方案1】:

    加深 x 轴标签,
    您可以在ticks 下使用以下选项

    ticks: {
      fontColor: '#000000',
      fontStyle: 'bold',
    

    请参阅以下工作 sn-p...

    $(document).ready(function() {
      new Chart(document.getElementById('myChart').getContext('2d'), {
        type: 'line',
        data: {
          labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
          datasets: [
            {
              label: 'test',
              data: [
                100, 200, 300, 500, 100, 900, 100, 200, 500
              ],
              borderColor: '#549cef',
              backgroundColor: 'rgba(0,0,0,0.0)',
              pointBackgroundColor: 'white',
              pointRadius: 10,
              //pointBorderWidth: this.getThick(),
              pointHoverBackgroundColor: '#549cef',
              borderWidth: 3
            }
          ]
        },
        options: {
          responsive: true,
          scales: {
            xAxes:[{
              gridLines: {
                drawBorder: true,
                drawOnChartArea: false,
              },
              ticks: {
                fontColor: '#000000',
                fontStyle: 'bold',
                callback: function(dataLabel, index,data){
                  return  data.length < 5? '':
                   data.length<12 && (index==0 || index==(data.length-1)) ? '':
                    (index==0 || index == 5 || index == 11)? dataLabel: '';
                }
              }
            }],
          }
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
    <canvas id="myChart"></canvas>

    编辑

    不确定是否可以仅设置轴标签上方的破折号,
    但是您可以使用一组颜色来单独设置每个网格线的样式。

    请参阅下面的工作 sn-p 示例...

    $(document).ready(function() {
      new Chart(document.getElementById('myChart').getContext('2d'), {
        type: 'line',
        data: {
          labels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','10', '11'],
          datasets: [
            {
              label: 'test',
              data: [
                100, 200, 300, 500, 100, 900, 100, 200, 500
              ],
              borderColor: '#549cef',
              backgroundColor: 'rgba(0,0,0,0.0)',
              pointBackgroundColor: 'white',
              pointRadius: 10,
              //pointBorderWidth: this.getThick(),
              pointHoverBackgroundColor: '#549cef',
              borderWidth: 3
            }
          ]
        },
        options: {
          responsive: true,
          scales: {
            xAxes:[{
              gridLines: {
                drawBorder: true,
                drawOnChartArea: false,
              },
              gridLines: {
                color: ['#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)', '#000000'],
                drawBorder: false
              },
              ticks: {
                fontColor: '#000000',
                fontStyle: 'bold',
                callback: function(dataLabel, index,data){
                  return  data.length < 5? '':
                   data.length<12 && (index==0 || index==(data.length-1)) ? '':
                    (index==0 || index == 5 || index == 11)? dataLabel: '';
                }
              }
            }],
          }
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
    <canvas id="myChart"></canvas>

    【讨论】:

    • 希望这会有所帮助,不确定您所说的 darken 究竟是什么意思,假设您指的是标签...
    • 谢谢!我指的是更多关于标签顶部的蜱虫。我会更新问题使其更清楚。
    • 我不知道我能做到这一点!非常感谢你的工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多