ChartJS选项,刻度被完全忽略

wsewodh2  于 2022-11-07  发布在  Chart.js
关注(0)|答案(1)|浏览(266)

我试着制作一个图表,沿着x轴是年份,y轴是美元金额。我终于接近了我所寻找的,但我发现,因为x坐标是数字,ChartJS是把逗号在他们看起来真的很奇怪的年份。
经过一番研究,我使用了callbacks.options.plugin.tooltip.callbacks.label,它可以让我删除工具提示中的逗号,但是当我使用options.scales.x[0].ticks.callback尝试修复底部的标签时,它不仅不起作用,但是我没有看到console.log语句,所以它看起来甚至没有调用回调。根据我在网上和Stack Overflow上找到的内容,我尝试了几种不同的回调方法,我认为这与ChartJS在不同版本中执行此操作的不同方式相对应。(我使用的是3.5.1版本。)
然后,我意识到...... options.scales下的选项似乎都没有任何效果。我更改min、标题、刻度设置(颜色为红色、回调等),它们都没有任何效果。(这也解释了为什么我在使用折线图时遇到麻烦,不得不切换到散点图;显然,当我将其设置为type: 'date'或其他确切的工作方式时,type: 'linear'并没有被选中,也没有做任何不同的事情。)
与此同时,其他选项,如options.showLineoptions.elements确实有影响,我看到了图表,但在控制台中没有得到任何错误。因此,它选择了这些选项,只是忽略了我在options.scales中的所有内容。
下面是相关代码:

  1. // Sample data added to make this example self-contained
  2. // This is my internal data format
  3. let data = {
  4. "Series1": [ {x: 2001, y: 100 }, {x: 2002, y: 110 }, {x: 2003, y: 107 }, ],
  5. "Series2": [ {x: 2001, y: 107 }, {x: 2002, y: 102 }, {x: 2004, y: 95 }, ],
  6. }
  7. // Define data //////////////////////////////////////////////////////
  8. // I convert data to format ChartJS wants and add a few options
  9. let datasets = [];
  10. for(let label in data) {
  11. let c = colorIterator.next().value
  12. datasets.push({
  13. label: label,
  14. data: data[label],
  15. backgroundColor: c,
  16. borderColor: c,
  17. });
  18. }
  19. // Define options //////////////////////////////////////////////////////
  20. let chartConfig = {
  21. type: 'scatter',
  22. data: { datasets: datasets, },
  23. options: {
  24. title: { display: false },
  25. indexAxis: 'x', responsive: true, maintainAspectRatio: false,
  26. showLine: true,
  27. elements: {
  28. line: { display: true, tension: 0, borderWidth: 1, fill: false, },
  29. point: { radius: 3 }
  30. },
  31. interaction: { mode: 'x', },
  32. scales: {
  33. x: [{
  34. type: 'linear',
  35. min: 1995, max: (new Date()).getFullYear()+1, stepSize: 1,
  36. title: { display: true, text: 'Year' },
  37. ticks: {
  38. display: true,
  39. major: { enabled: true },
  40. color: 'red',
  41. callback: function(value, index, ticks) {
  42. console.log(value);
  43. return Chart.Ticks.formatters.numeric.apply(this, [value, index, ticks])
  44. .replace(",","");
  45. }
  46. }
  47. }],
  48. y: [{
  49. title: { display: true, text: '$' },
  50. ticks: {
  51. display: true,
  52. color: 'red',
  53. },
  54. }],
  55. },
  56. plugins: {
  57. tooltip: {
  58. callbacks: {
  59. label: function(context) {
  60. let label = context.dataset.label || '';
  61. if(label) {
  62. let x = context.label.replace(",","");
  63. let y = context.formattedValue;
  64. return 'Year ' + x + ' "' + label + '": $' + y;
  65. } else { return 'x'; }
  66. },
  67. },
  68. },
  69. },
  70. }
  71. };
  72. // MAKE CHART //////////////////////////////////////////////////////
  73. let mainChart = new Chart(document.getElementById(c.id), chartConfig);
rqqzpn5f

rqqzpn5f1#

docs中所述,刻度不是数组。所有刻度都是刻度对象中的对象。
因此,您需要将代码更改为:

  1. options: {
  2. scales: {
  3. x: {
  4. // x options
  5. },
  6. y: {
  7. // y options
  8. },
  9. }
  10. }

相关问题