简体   繁体   English

如何在chart.js中获得多个y轴?

[英]How to get multiple y axis in chart.js?

I have tried the following to display two y axis on one line chart.我尝试了以下方法在一个折线图上显示两个 y 轴。 However, the charts don't seem to appear once I implement this.但是,一旦我实现了这个,图表似乎就不会出现。

var options = {
                type: "line",
                data: {
                    labels: timelist,
                    datasets: [
                        {label: "Ambient Temp", data: temp_amb_data, backgroundColor: "rgba(75, 192, 192, 0.2)", borderWidth: 1, borderColor: "rgba(75, 192, 192, 1)",id='left-y-axis'},
                        {label: "Ambient Humidity", data:amb_hum_data, backgroundColor: "rgba(255, 99, 132, 0.2)", borderWidth: 1, borderColor: "rgba(255, 99, 132, 1)",id='right-y-axis'},
                        {label: "Booth Temp", data: temp_col, backgroundColor: "rgba(153, 102, 255, 0.2)", borderWidth: 1, borderColor: "rgba(153, 102, 255, 1)",id='left-y-axis'},
                        {label: "Booth Humidity", data: hum_data, backgroundColor: "rgba(255, 159, 64, 0.2)", borderWidth: 1, borderColor: "rgba(255, 159, 64, 1)",id='right-y-axis'}
                    ]
                },
                options: {
                    legend:{position:'right'},
                    animation: {tension: {duration: 1000, easing: "linear", from: 1, to: 0, loop: true}},
                    scales: {yAxes:[{ id:'left-y-axis',type='linear',position:'left'},{id:'right-y-axis',type='linear',position:'right'}]}
                }
            };

            var ctx = document.getElementById("myChart").getContext("2d");
            var chart = new Chart(ctx, options);

Where am I going wrong?我哪里错了?

The issues I see on your snippet are:我在您的代码段中看到的问题是:

  1. You are missing a few settings on your Chart.js configuration.您在 Chart.js 配置中缺少一些设置。
  2. The provided JSON is not valid.提供的 JSON 无效。
  3. Additionally, it is not easy to reproduce the error.此外,重现错误并不容易。 Try providing a Codepen example.尝试提供一个 Codepen 示例。

I have created this Codepen snippet with a Chart and two Y axes: https://codepen.io/adelriosantiago/pen/wvGxeGB我用图表和两个 Y 轴创建了这个 Codepen 片段: https ://codepen.io/adelriosantiago/pen/wvGxeGB

See the second Y axis inside options.scales.yAxes .查看options.scales.yAxes的第二个 Y 轴。 Can you try it with your dataset data and see if it fits your needs?你能用你的dataset数据试试看它是否符合你的需求吗? If it doesn't please fork the Codepen and provide the link.如果没有,请 fork Codepen 并提供链接。

In the second CodePen you provided the only issue found is the invalid Chart.JS configuration.在您提供的第二个 CodePen 中,发现的唯一问题是无效的 Chart.JS 配置。 Specifically the key yAxisId which should be yAxisID .特别是键yAxisId应该是yAxisID Small but important difference.小而重要的区别。 The fixed Codepen is here: https://codepen.io/adelriosantiago/pen/MWyqWdr (I added a negative value on the second axis to easily differentiate min and max )固定的 Codepen 在这里: https ://codepen.io/adelriosantiago/pen/MWyqWdr(我在第二个轴上添加了一个负值以轻松区分minmax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM