我如何用Chart.js在条形图上放置水平网格线?

z9smfwbn  于 2024-01-07  发布在  Chart.js
关注(0)|答案(2)|浏览(249)

我有一个Chart.js,输出看起来像第一个图像,我希望它看起来像第二个图像,其中y轴线将"交叉"酒吧和空间,它了一点,以更好的可见性,我不认为我们可以调用这个填充或我如何才能管理创建这种效果使用Chart.js?


的数据



这是我的代码,创建了像第一张图片一样的条形图,谢谢。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>$DOGI Listings Graph</title>
  7. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  8. <style>
  9. html,
  10. body {
  11. height: 100%;
  12. margin: 0;
  13. background-color: black;
  14. color: white;
  15. }
  16. canvas {
  17. width: 100% !important;
  18. height: 100% !important;
  19. }
  20. </style>
  21. </head>
  22. <body style="background-color: black;color: white;">
  23. <canvas id="myChart"></canvas>
  24. <script>
  25. const chartData = [{
  26. x: 1,
  27. y: 34504
  28. }, {
  29. x: 2,
  30. y: 47831
  31. },
  32. {
  33. x: 3,
  34. y: 7050
  35. }, {
  36. x: 4,
  37. y: 8009
  38. },
  39. {
  40. x: 5,
  41. y: 8400
  42. }, {
  43. x: 6,
  44. y: 1250
  45. },
  46. {
  47. x: 7,
  48. y: 2505
  49. }, {
  50. x: 8,
  51. y: 2001
  52. },
  53. {
  54. x: 9,
  55. y: 88
  56. }, {
  57. x: 10,
  58. y: 650
  59. },
  60. {
  61. x: 11,
  62. y: 1350
  63. }, {
  64. x: 12,
  65. y: 200
  66. },
  67. {
  68. x: 13,
  69. y: 12782
  70. }, {
  71. x: 14,
  72. y: 1000
  73. },
  74. {
  75. x: 15,
  76. y: 50
  77. }, {
  78. x: 16,
  79. y: 500
  80. },
  81. {
  82. x: 17,
  83. y: 204
  84. }, {
  85. x: 18,
  86. y: 15
  87. },
  88. {
  89. x: 19,
  90. y: 250
  91. }, {
  92. x: 20,
  93. y: 1200
  94. },
  95. {
  96. x: 21,
  97. y: 200
  98. }, {
  99. x: 22,
  100. y: 200
  101. },
  102. {
  103. x: 23,
  104. y: 100
  105. }, {
  106. x: 24,
  107. y: 200
  108. },
  109. {
  110. x: 25,
  111. y: 450
  112. }
  113. ]
  114. async function createGraphData() {
  115. const ctx = document.getElementById('myChart').getContext('2d');
  116. const myChart = new Chart(ctx, {
  117. type: 'bar',
  118. data: {
  119. datasets: [{
  120. label: '# of Tokens',
  121. data: chartData,
  122. backgroundColor: 'rgb(255,165,3,0.8)',
  123. }]
  124. },
  125. options: {
  126. legend: {
  127. display: false
  128. },
  129. scales: {
  130. x: {
  131. type: 'linear',
  132. position: 'bottom',
  133. min: 0,
  134. max: 25,
  135. ticks: {
  136. stepSize: 1,
  137. color: 'white',
  138. autoSkip: false,
  139. },
  140. title: {
  141. display: true,
  142. text: 'Price $ (USD)',
  143. color: 'white'
  144. }
  145. },
  146. y: {
  147. beginAtZero: true,
  148. grid: {
  149. color: 'rgba(255,255,255,0.1)'
  150. },
  151. title: {
  152. display: true,
  153. text: 'Number of Tokens',
  154. color: 'white'
  155. },
  156. ticks: {
  157. color: 'white'
  158. }
  159. }
  160. },
  161. plugins: {
  162. legend: {
  163. display: false,
  164. labels: {
  165. color: 'white'
  166. }
  167. },
  168. title: {
  169. display: true,
  170. text: 'Chart Title',
  171. color: 'white',
  172. font: {
  173. size: 24
  174. }
  175. }
  176. },
  177. maintainAspectRatio: false,
  178. responsive: true,
  179. barPercentage: 0.7,
  180. categoryPercentage: 0.8,
  181. }
  182. });
  183. }
  184. createGraphData().catch(console.error);
  185. </script>
  186. </body>
  187. </html>

字符串

lb3vh1jj

lb3vh1jj1#

只需将z属性添加到您的scale in选项的网格中并将其设置为1,而默认值为-1对于间距,您可以使用破折号属性添加边框选项并设置数组中的间距这里有一个示例,其中包含一些可能适合您的情况和颜色的值,您可以将它们添加到scales对象内的“y”对象:

  1. grid: {
  2. color: 'rgba(255,255,255,0.35)',
  3. z:1,
  4. },
  5. border:{
  6. dash:[3,3]
  7. },

字符串
还可以查看文档以了解更多ChartJs styling

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>$DOGI Listings Graph</title>
  7. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  8. <style>
  9. html,
  10. body {
  11. height: 100%;
  12. margin: 0;
  13. background-color: black;
  14. color: white;
  15. }
  16. canvas {
  17. width: 100% !important;
  18. height: 100% !important;
  19. }
  20. </style>
  21. </head>
  22. <body style="background-color: black;color: white;">
  23. <canvas id="myChart"></canvas>
  24. <script>
  25. const chartData = [
  26. { x: 1, y: 34504 }, { x: 2, y: 47831 },
  27. { x: 3, y: 7050 }, { x: 4, y: 8009 },
  28. { x: 5, y: 8400 }, { x: 6, y: 1250 },
  29. { x: 7, y: 2505 }, { x: 8, y: 2001 },
  30. { x: 9, y: 88 }, { x: 10, y: 650 },
  31. { x: 11, y: 1350 }, { x: 12, y: 200 },
  32. { x: 13, y: 12782 }, { x: 14, y: 1000 },
  33. { x: 15, y: 50 }, { x: 16, y: 500 },
  34. { x: 17, y: 204 }, { x: 18, y: 15 },
  35. { x: 19, y: 250 }, { x: 20, y: 1200 },
  36. { x: 21, y: 200 }, { x: 22, y: 200 },
  37. { x: 23, y: 100 }, { x: 24, y: 200 },
  38. { x: 25, y: 450 }
  39. ]
  40. async function createGraphData() {
  41. const ctx = document.getElementById('myChart').getContext('2d');
  42. const myChart = new Chart(ctx, {
  43. type: 'bar',
  44. data: {
  45. datasets: [{
  46. label: '# of Tokens',
  47. data: chartData,
  48. backgroundColor: 'rgb(255,165,3,0.8)',
  49. }]
  50. },
  51. options: {
  52. legend: {
  53. display: false
  54. },
  55. scales: {
  56. x: {
  57. type: 'linear',
  58. position: 'bottom',
  59. min: 0,
  60. max: 25,
  61. ticks: {
  62. stepSize: 1,
  63. color: 'white',
  64. autoSkip: false,
  65. },
  66. title: {
  67. display: true,
  68. text: 'Price $ (USD)',
  69. color: 'white'
  70. }
  71. },
  72. y: {
  73. beginAtZero: true,
  74. grid: {
  75. color: 'rgba(255,255,255,0.35)',
  76. z:1,
  77. },
  78. border:{
  79. dash:[3,3]
  80. },
  81. title: {
  82. display: true,
  83. text: 'Number of Tokens',
  84. color: 'white'
  85. },
  86. ticks: {
  87. color: 'white'
  88. },
  89. }
  90. },
  91. plugins: {
  92. legend: {
  93. display: false,
  94. labels: {
  95. color: 'white'
  96. }
  97. },
  98. title: {
  99. display: true,
  100. text: 'Chart Title',
  101. color: 'white',
  102. font: {
  103. size: 24
  104. }
  105. }
  106. },
  107. maintainAspectRatio: false,
  108. responsive: true,
  109. barPercentage: 0.7,
  110. categoryPercentage: 0.8,
  111. }
  112. });
  113. }
  114. createGraphData().catch(console.error);
  115. </script>
  116. </body>
  117. </html>

展开查看全部
drkbr07n

drkbr07n2#

你可以使用z属性将Y轴网格移动到条形图层的上方。我不确定这是否满足你的所有要求。我还调整了颜色。
https://www.chartjs.org/docs/latest/axes/styling.html

  1. const chartData = [{
  2. x: 1,
  3. y: 34504
  4. }, {
  5. x: 2,
  6. y: 47831
  7. },
  8. {
  9. x: 3,
  10. y: 7050
  11. }, {
  12. x: 4,
  13. y: 8009
  14. },
  15. {
  16. x: 5,
  17. y: 8400
  18. }, {
  19. x: 6,
  20. y: 1250
  21. },
  22. {
  23. x: 7,
  24. y: 2505
  25. }, {
  26. x: 8,
  27. y: 2001
  28. },
  29. {
  30. x: 9,
  31. y: 88
  32. }, {
  33. x: 10,
  34. y: 650
  35. },
  36. {
  37. x: 11,
  38. y: 1350
  39. }, {
  40. x: 12,
  41. y: 200
  42. },
  43. {
  44. x: 13,
  45. y: 12782
  46. }, {
  47. x: 14,
  48. y: 1000
  49. },
  50. {
  51. x: 15,
  52. y: 50
  53. }, {
  54. x: 16,
  55. y: 500
  56. },
  57. {
  58. x: 17,
  59. y: 204
  60. }, {
  61. x: 18,
  62. y: 15
  63. },
  64. {
  65. x: 19,
  66. y: 250
  67. }, {
  68. x: 20,
  69. y: 1200
  70. },
  71. {
  72. x: 21,
  73. y: 200
  74. }, {
  75. x: 22,
  76. y: 200
  77. },
  78. {
  79. x: 23,
  80. y: 100
  81. }, {
  82. x: 24,
  83. y: 200
  84. },
  85. {
  86. x: 25,
  87. y: 450
  88. }
  89. ]
  90. async function createGraphData() {
  91. const ctx = document.getElementById('myChart').getContext('2d');
  92. const myChart = new Chart(ctx, {
  93. type: 'bar',
  94. data: {
  95. datasets: [{
  96. label: '# of Tokens',
  97. data: chartData,
  98. backgroundColor: 'rgb(255,165,3,0.8)',
  99. }]
  100. },
  101. options: {
  102. legend: {
  103. display: false
  104. },
  105. scales: {
  106. x: {
  107. type: 'linear',
  108. position: 'bottom',
  109. min: 0,
  110. max: 25,
  111. ticks: {
  112. stepSize: 1,
  113. color: 'white',
  114. autoSkip: false,
  115. },
  116. title: {
  117. display: true,
  118. text: 'Price $ (USD)',
  119. color: 'white'
  120. }
  121. },
  122. y: {
  123. beginAtZero: true,
  124. grid: {
  125. color: 'rgba(50,50,50,0.5)',
  126. z: 1
  127. },
  128. title: {
  129. display: true,
  130. text: 'Number of Tokens',
  131. color: 'white'
  132. },
  133. ticks: {
  134. color: 'white'
  135. }
  136. }
  137. },
  138. plugins: {
  139. legend: {
  140. display: false,
  141. labels: {
  142. color: 'white'
  143. }
  144. },
  145. title: {
  146. display: true,
  147. text: 'Chart Title',
  148. color: 'white',
  149. font: {
  150. size: 24
  151. }
  152. }
  153. },
  154. maintainAspectRatio: false,
  155. responsive: true,
  156. barPercentage: 0.7,
  157. categoryPercentage: 0.8,
  158. }
  159. });
  160. }
  161. createGraphData().catch(console.error);

个字符

展开查看全部

相关问题