如何使用chart.js(v2.0-dev)反转y轴

idfiyjo8  于 2023-02-04  发布在  Chart.js
关注(0)|答案(4)|浏览(203)

我正在创建具有2个y轴的折线图。其中一个轴具有两个数据集,但都在0-100的范围内运行。数字越大越好。第二个y轴的范围通常越小(通常为个位数),最佳结果为1。
如何反转第二个y轴,使1位于图表的顶部?
(我会试着自己找到解决方案,但在chart.js中的5 k+行,可能需要一段时间)
谢谢^_^

blpfk2vs

blpfk2vs1#

chart js的Dev 2.0支持在设置轴时反转刻度的选项,这样第二个轴的声明将变为

  1. {
  2. type: "invertedLinear", // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
  3. display: true,
  4. position: "right",
  5. id: "y-axis-2",
  6. ticks: {
  7. reverse: true
  8. },
  9. // grid line settings
  10. gridLines: {
  11. drawOnChartArea: false, // only want the grid lines for one axis to show up
  12. }
  13. }

这是https://jsfiddle.net/nwc8ys34/15/的实际应用

euoag5mw

euoag5mw2#

使用v 2.7.0,以下内容似乎可以正常工作:

  1. options: {
  2. scales: {
  3. yAxes: [{
  4. ticks: {
  5. reverse: true,
  6. }
  7. }]
  8. }
  9. }
egdjgwm8

egdjgwm83#

在v3中,天平配置已更改,因此可实现所需行为,如下所示:

  1. const options = {
  2. type: 'line',
  3. data: {
  4. labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
  5. datasets: [{
  6. label: '# of Votes',
  7. data: [40, 80, 100, 70, 60, 80],
  8. borderColor: 'pink'
  9. },
  10. {
  11. label: '# of Points',
  12. data: [7, 11, 5, 8, 3, 7],
  13. borderColor: 'orange',
  14. yAxisID: 'y2'
  15. }
  16. ]
  17. },
  18. options: {
  19. scales: {
  20. y: {},
  21. y2: {
  22. position: 'right',
  23. reverse: true
  24. }
  25. }
  26. }
  27. }
  28. const ctx = document.getElementById('chartJSContainer').getContext('2d');
  29. const chart = new Chart(ctx, options);
  1. <body>
  2. <canvas id="chartJSContainer" width="600" height="400"></canvas>
  3. <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.6.0/chart.js"></script>
  4. </body>
展开查看全部
j8yoct9x

j8yoct9x4#

图表js 4:

  1. datasets: [{
  2. label: "Strength",
  3. yAxisID: "y1",
  4. data: mappedData.sigStrength
  5. }, {
  6. label: "Bars",
  7. yAxisID: "y2",
  8. data: mappedData.sigBars
  9. }]
  1. scales: {
  2. y1: {
  3. type: "linear",
  4. display: true,
  5. position: "left",
  6. reverse: true
  7. },
  8. y2: {
  9. type: "linear",
  10. display: true,
  11. position: "right",
  12. ticks: {
  13. stepSize: 1
  14. }
  15. }
  16. },
展开查看全部

相关问题