简体   繁体   English

删除 Chart.js 上的网格线和数字

[英]Remove grid lines and numbers on Chart.js

I am trying to remove the grid lines on the Radar chart on chart.js v2 for react.我正在尝试删除 chart.js v2 上雷达图表上的网格线以进行反应。

Desired Result but keeping the outermost line while removing the inner lines and numbers期望的结果,但保留最外层的行,同时删除内层的行和数字

I have attempted to use the following code but it only returns a runtime error saying "category" is not a registered scale.我试图使用下面的代码,但它只返回一个运行时错误,说"category" is not a registered scale.

const options = {
  scales: {
    x: {
      grid: {
        display: false
      }
    },
    y: {
      grid: {
        display: false
      }
    }
  }
}

This can be done through the following options.这可以通过以下选项来完成。

scale: {
  ticks: {
    stepSize: 1,
    callback: (v, i, values) => i + 1 < values.length ? '' : v
  },
  gridLines: {
    color: [0, 0, 0, 0, '#ccc']
  }
}  

For further details, please consult the Styling page from the Chart.js v2.9.4 documentation.有关更多详细信息,请参阅Chart.js v2.9.4文档中的样式页面。

Please take a look at below runnable code and see how it works.请看下面的可运行代码,看看它是如何工作的。 Note that it uses a generic approach for defining gridLines.color .请注意,它使用通用方法来定义gridLines.color

 const labels = ['Things 1', 'Things 2', 'Things 3', 'Things 4', 'Things 5']; const data = [0, 3, 5, 2, 5]; const max = Math.ceil(Math.max(...data)); new Chart('radar-chart', { type: 'radar', data: { labels: labels, datasets: [{ data: data, fill: true, backgroundColor: 'rgba(0, 0, 255,0.2)', borderColor: 'rgb(0, 0, 255)' }] }, options: { legend: { display: false }, scale: { ticks: { stepSize: 1, max: max, callback: (v, i, values) => i + 1 < values.length ? '' : v }, gridLines: { color: Array.from(Array(max).keys()).map((v, i) => i + 1 < max ? 0 : '#ccc') } } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script> <canvas id="radar-chart" height="80"></canvas>

From the error you are getting it seems like you are using Chart.js V3 and not V2.从您收到的错误来看,您似乎使用的是 Chart.js V3 而不是 V2。 To get what you want you need to import and register everything if you want to use treeshaking, I suggest you do this at the latest part and for development import everything like so:为了得到你想要的东西,如果你想使用 treeshaking,你需要导入和注册所有东西,我建议你在最新的部分这样做,并为开发导入所有东西,如下所示:

import Chart from 'chart.js/auto';

To hide and get the result what you want your config needs to look like this:要隐藏并获得你想要的结果,你的配置需要如下所示:

 const labels = ['Things 1', 'Things 1', 'Things 1', 'Things 1', 'Things 1']; const data = [0, 3, 5, 2, 3]; new Chart('radar-chart', { type: 'radar', data: { labels: labels, datasets: [{ data: data, fill: true, backgroundColor: 'rgba(0, 0, 255,0.2)', borderColor: 'rgb(0, 0, 255)' }] }, options: { plugins: { legend: { display: false } }, scales: { r: { ticks: { stepSize: 1, callback: (v, i, values) => i + 1 < values.length ? '' : v }, grid: { color: data.map((v, i) => i + 1 < data.length ? 0 : '#ccc') } } } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script> <canvas id="radar-chart" height="80"></canvas>

Notice that I specify a r scale instead of x and y请注意,我指定了r比例而不是xy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM