【问题标题】:Chartjs: Draw chart with only yAxis zero tickChartjs:仅使用 yAxis 零刻度绘制图表
【发布时间】:2020-07-21 10:44:44
【问题描述】:

见鬼去吧

我想画这样的图表:

目前,我可以使用 afterBuildTicks 仅获取零 yAxis。但问题是所有其他的蜱也被删除了。所以不确定我们是否有办法绘制零刻度并隐藏除标签之外的其他刻度。

var ctx = document.getElementById("myChart").getContext("2d");
var myChart = new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Value 1', 'Value 2', 'Value 3', 'Value 4'],
    datasets: [{
      label: "Dataset",
      data: [20, 15, 27, 18],
      backgroundColor: 'rgba(54, 162, 235,0.5)',
    }],
  },
  options: {
  responsive: true,
    elements: {
      line: {
        fill: false
      }
    },
  scales: {
    xAxes: [
      {
        position: 'top',
        display: true,
        gridLines: {
          display: true,
        },
        stacked: true,
        ticks: {
          fontColor: "#C4C4C4",
          fontSize: 10,
        }
      }
    ],
    yAxes: [
      {
        type: 'linear',
        display: true,
        position: 'right',
        id: 'y-axis-1',
        gridLines: {
          display: true,
        },
        labels: {
          show: true
        },
        stacked: false,
        ticks: {
          fontColor: "#C4C4C4",
          fontSize: 10,
          precision: 0,
          suggestedMax: 100,
          suggestedMin: -100
        },
        afterBuildTicks:function (axis) {
        axis.ticks = [0]
        }
      }
    ]
  },
  tooltips: {
    mode: 'label',
    bodySpacing: 10,
    titleMarginBottom: 10,
    titleFontSize: 14,
    titleFontStyle: 'bold',
    footerAlign: 'right',
    callbacks: {
      label: (tooltipItem, data) => {
        const label = data.datasets[tooltipItem.datasetIndex].label || ''
        const value = tooltipItem.value
        return `   ${label}: ${value}`
      }
    }
  }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

非常感谢

【问题讨论】:

  • 在问题中添加您当前的代码 sn-p 可能会有所帮助吗?
  • @DineshNadimpalli:我添加了 jsfiddle 的链接。很抱歉,因为我不知道如何在添加 jsfiddle 链接时绕过错误,所以我把它作为原始文本

标签: reactjs chart.js2


【解决方案1】:

我找到了使用的解决方案:gridLines options to draw chart with width is zero:

gridLines: {
  display: true,
  tickMarkLength: 10,
  lineWidth: 0
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多