如何使用ChartJS显示虚网格线?

mcvgt66p  于 2023-02-04  发布在  Chart.js
关注(0)|答案(2)|浏览(326)


我正在chart.js中开发一个图表,我想显示虚线网格线,如图所示。

1u4esq0p

1u4esq0p1#

您可以在图表选项中编辑数据集显示:

  1. options: {
  2. scales: {
  3. // The following will affect the vertical lines (xAxe) of your dataset
  4. xAxes: [{
  5. gridLines: {
  6. // You can change the color, the dash effect, the main axe color, etc.
  7. borderDash: [8, 4],
  8. color: "#348632"
  9. }
  10. }],
  11. // And this will affect the horizontal lines (yAxe) of your dataset
  12. yAxes: [{
  13. gridLines: {
  14. borderDash: [8, 4],
  15. color: "#348632"
  16. }
  17. }]
  18. }
  19. }

现在你知道怎么做了,只要按照你想要的方式改变它就行了。
检查Chart.js文档中的网格线配置,查看哪些是可编辑的。
如果需要,下面是一个工作示例on this jsFiddle及其结果:

展开查看全部
fgw7neuy

fgw7neuy2#

我正在使用chart.js版本:9.2.0.对于我工作了以下代码:

  1. options: {
  2. cubicInterpolationMode: 'monotone',
  3. plugins: {legend: false},
  4. scales: {
  5. x: {
  6. grid:{
  7. color: 'magenta',
  8. },
  9. border: {
  10. dash: [2,4],
  11. },
  12. ticks: {display: false}
  13. },
  14. y: {
  15. grid: {
  16. color: 'magenta',
  17. },
  18. border: {
  19. dash: [2,4],
  20. },
  21. ticks: {
  22. callback: function(value) {
  23. return value + 'k';
  24. }
  25. }
  26. }
  27. }
  28. }

我得到的结果是:the result, the chart with dashed grid lines
主要部分是border: {dash: [2,4]},我是通过实验发现的,因为gridLines: {borderDash: [8, 4]} didt的解决方案对我有效,所以它可能不是最佳解决方案
我在chart.js页面找到了关于轴样式的信息

展开查看全部

相关问题