在Charts.Js v2.9.4中的对数图中添加次要网格线

hwamh0ep  于 2023-10-18  发布在  Chart.js
关注(0)|答案(1)|浏览(160)

我想在下面绘制的图表中绘制次要网格线,以获得类似于下图中x轴的结果(除了以简单数字而不是幂显示的刻度值,即而不是10^0,它应该显示1,而不是10^1,它应该显示10等等。图像是os意味着只显示x轴上的网格线):

  1. <!DOCTYPE html>
  2. <!-- saved from url=(0014)about:internet -->
  3. <html>
  4. <head>
  5. <title>Chart</title>
  6. <meta charset='utf-8'>
  7. <meta http-equiv='X-UA-Compatible' content='IE=Edge'>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
  9. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-colorschemes"></script>
  10. <style>
  11. body { margin: 0; padding: 0; }
  12. #container { width: 100%; }
  13. </style>
  14. </head>
  15. <body>
  16. <div id='container'>
  17. <canvas id='myChart'></canvas>
  18. </div>
  19. <script>
  20. Chart.defaults.global.animation.duration = 1000;
  21. Chart.defaults.global.animation.easing = 'linear';
  22. var ctx = document.getElementById('myChart').getContext('2d');
  23. var myChart = new Chart(ctx, {
  24. type: 'line',
  25. data: {
  26. labels: ['10','2000','30000','50000','60000','700000',],
  27. datasets: [
  28. {label: 'ABC' ,
  29. fill: true ,
  30. data: [
  31. { x: '10', y: 2 },
  32. { x: '2000', y: 4 },
  33. { x: '30000', y: 7 },
  34. { x: '50000', y: 8 },
  35. { x: '60000', y: 8.5 },
  36. { x: '700000', y: 9 }
  37. ],
  38. borderWidth: 1},
  39. ]
  40. },
  41. options: {
  42. aspectRatio: 1.6,
  43. title: {
  44. display: true,
  45. position: 'top',
  46. text: 'Ref Data'
  47. },
  48. legend: {
  49. display: true,
  50. position: 'right',
  51. },
  52. scales: {
  53. yAxes: [{
  54. id: 'first-y-Axis',
  55. display: true,
  56. scaleLabel: {
  57. display: true,
  58. labelString: 'Demo y-axis'
  59. }
  60. }],
  61. xAxes: [{
  62. id: 'first-x-Axis',
  63. display: true,
  64. type: 'logarithmic',
  65. scaleLabel: {
  66. display: true,
  67. labelString: 'Demo x-axis'
  68. },
  69. ticks: {
  70. //min: 0 ,
  71. //max: 700000
  72. callback: function (value, index, values) {
  73. if (Math.log10(value) % 1 === 0) { return value; }
  74. }
  75. }
  76. }]
  77. },
  78. plugins: {
  79. colorschemes: {
  80. scheme: 'brewer.Paired12'
  81. }
  82. }
  83. }
  84. });
  85. </script>
  86. </body>
  87. </html>

类似的解决方案也在帖子**linked here**中讨论过,但在那篇帖子中使用的代码非常复杂,我无法理解并使其在我的代码中工作,因为我不太了解JavaScript。只是想把这个图表应用到我的电脑上。如果这里有人可以修改我的代码或帮助我修改代码,以获得像图像或上面链接的帖子中的小网格线,我将不胜感激(除了在帖子中为y轴创建的小网格线,但我想为x轴创建)。
问候

t8e9dugd

t8e9dugd1#

与OP linked post(专为最新的chart.js v4设计)中的情况一样,如果ticks.callback返回空字符串而不是nullundefined(或者如果没有ticks.callback),则创建次要网格线对于chart.js v2+仍然有效。
但是,为将次要轴网线与主要轴网线区分开来而设置次要轴网线样式的方式存在差异。在chart.js v2.9.4中,scales的gridLines选项是 * 不可脚本化的 *。这需要一种不同的方法,在某些情况下可能对较新的版本有用:使用可索引选项,这意味着必须为每个项目创建一个(不同)值的数组-在这种情况下为每个网格线。由于节拍只在运行时才知道,因此可以在轴回调中创建该数组,例如在afterBuildTicks中:

  1. afterBuildTicks(scale){
  2. const nTicks = scale.ticks.length,
  3. isMajor = scale.ticks.map(value => ('' + value).replace(/0+$/, '') === '1');
  4. scale.options.gridLines.lineWidth =
  5. Array.from({length: nTicks}, (_, i) => isMajor[i] ? 1.2 : 0.5);
  6. scale.options.gridLines.color =
  7. Array.from({length: nTicks}, (_, i) => isMajor[i] ? '#000' : '#ddd');
  8. //scale.options.gridLines.borderDash - can't be set as an array of arrays
  9. }

不幸的是,这种方法似乎不适用于borderDash
我将给予一些关于这个函数究竟如何工作的细节:使用一个可索引的选项,比如gridLines.lineWidth,意味着我们将它的值设置为一个数组:

  1. gridLines:{
  2. lineWidth: [1.2, 0.5, 0.5, 0.5, 0.5, 1.2, 0.5, .......]
  3. }

问题是,tick是由chart.js从数据中构建的,我们事先不知道总共有多少个tick,以及有多少个和哪里是主要的tick。因此,我们必须等待chart.js构建这些tick,然后在构建tick后立即自动调用的处理程序afterBuildTicks中,我们根据实际的tick值设置网格线属性。这是有意义的,因为网格线是在构建刻度之后绘制的。
函数afterBuildTicks接收scale对象作为参数,从中提取刻度作为scale.ticks。然后我们决定哪些刻度是“主要的”--用粗线画出来--我使用的定义是字符串表示是1 fallowed by 0 s。另一种数学公式可以用Math.log10来设计。
现在,我们知道了有多少个刻度,以及主要刻度在哪里,因此我们可以为gridLines.lineWidth构建上面介绍的数组。使用函数Array.from;或者,我们可以使用具有相同功能的isMajor.map
代码片段:

  1. Chart.defaults.global.animation.duration = 1000;
  2. Chart.defaults.global.animation.easing = 'linear';
  3. const ctx = document.getElementById('myChart').getContext('2d');
  4. new Chart(ctx, {
  5. type: 'line',
  6. data: {
  7. labels: ['10', '2000', '30000', '50000', '60000', '700000',],
  8. datasets: [{
  9. label: 'ABC',
  10. fill: true,
  11. data: [{
  12. x: '10',
  13. y: 2
  14. },
  15. {
  16. x: '2000',
  17. y: 4
  18. },
  19. {
  20. x: '30000',
  21. y: 7
  22. },
  23. {
  24. x: '50000',
  25. y: 8
  26. },
  27. {
  28. x: '60000',
  29. y: 8.5
  30. },
  31. {
  32. x: '700000',
  33. y: 9
  34. }
  35. ],
  36. borderWidth: 1
  37. },]
  38. },
  39. options: {
  40. aspectRatio: 1.6,
  41. title: {
  42. display: true,
  43. position: 'top',
  44. text: 'Ref Data'
  45. },
  46. legend: {
  47. display: true,
  48. position: 'right',
  49. },
  50. scales: {
  51. yAxes: [{
  52. id: 'first-y-Axis',
  53. display: true,
  54. scaleLabel: {
  55. display: true,
  56. labelString: 'Demo y-axis'
  57. }
  58. }],
  59. xAxes: [{
  60. id: 'first-x-Axis',
  61. display: true,
  62. type: 'logarithmic',
  63. scaleLabel: {
  64. display: true,
  65. labelString: 'Demo x-axis'
  66. },
  67. gridLines: {
  68. borderDash: [10, 3],
  69. drawTicks: false,
  70. drawBorder: false
  71. },
  72. afterBuildTicks(scale){
  73. const nTicks = scale.ticks.length,
  74. isMajor = scale.ticks.map(value => ('' + value).charAt(0) === '1');
  75. scale.options.gridLines.lineWidth = Array.from({length: nTicks}, (_, i) => isMajor[i] ? 1.2 : 0.5);
  76. scale.options.gridLines.color = Array.from({length: nTicks}, (_, i) => isMajor[i] ? '#000' : '#ddd');
  77. //scale.options.gridLines.borderDash - can't be set as an array of arrays
  78. },
  79. ticks: {
  80. padding: 5,
  81. autoSkip: false,
  82. maxRotation: 0,
  83. callback: function(value, index, values){
  84. if(Math.log10(value) % 1 === 0){
  85. return value;
  86. }
  87. return '';
  88. }
  89. }
  90. }]
  91. },
  92. plugins: {
  93. colorschemes: {
  94. scheme: 'brewer.Paired12'
  95. }
  96. }
  97. }
  98. });
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"
  2. integrity="sha512-SuxO9djzjML6b9w9/I07IWnLnQhgyYVSpHZx0JV97kGBfTIsUYlWflyuW4ypnvhBrslz1yJ3R+S14fdCWmSmSA=="
  3. crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  4. <script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-colorschemes"></script>
  5. <div style="min-height: 60vh">
  6. <canvas id="myChart">
  7. </canvas>
  8. </div>

根据请求,应在Internet Explorer和旧浏览器中工作的版本:

  1. afterBuildTicks: function(scale){
  2. var nTicks = scale.ticks.length,
  3. isMajor = scale.ticks.map(function(value){return ('' + value).replace(/0+$/, '') === '1';});
  4. scale.options.gridLines.lineWidth =
  5. isMajor.map(function(_, i){return isMajor[i] ? 1.2 : 0.5;})
  6. scale.options.gridLines.color =
  7. isMajor.map(function(_, i){return isMajor[i] ? '#000' : '#ddd';});
  8. //scale.options.gridLines.borderDash - can't be set as an array of arrays
  9. }

jsFiddle中的完整代码

展开查看全部

相关问题