第二个yAxes值无法使用Chart.js正确绘制图表

时间:2019-03-27 21:03:56

标签: javascript chart.js jsfiddle

我有一个数据集值未显示在double-yAxes图表中。

我已经使用了通用的Chart.js代码来处理双重数据集和双重yAxes。但是,一个数据集未在图表上正确显示。 “温度”值的范围在66到78之间,但是尽管我使用了最小/最大范围值,但看起来还是“不在图表之列”。

See my code here on JSFiddle

<div style="width:75%">
     <canvas class=".content" id="myChart" ></canvas>
</div>


<Script>
var canvas = document.getElementById('myChart').getContext('2d');

// @ts-ignore
var myChart = new Chart(canvas, {
    options: {
        scales: {
            yAxes: [
                {
                id: 'SG',
                //type: 'linear',
                position: 'left',
                ticks: 
                    {
                    min: 1,
                    max: 1.1
                    }             
                }, 
                {
                id: 'Temp',
                //type: 'linear',
                position: 'right',
                ticks: 
                    {
                    min: 32,
                    max: 100
                    }
                }
            ]
        }
    },
    type: 'bar', //not really needed, over-ridden below
    data: {
        labels: ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri'],
        datasets: [{
            label: 'Gravity',
            yAxesID: 'SG',
            data: [1.07, 1.045, 1.030, 1.014, 1.012],
            backgroundColor: '#ff6384' //red
        }, {
            //type: 'line',
            label: 'Temp F',
            yAxesID: 'Temp',
            data: [78, 66, 78, 66, 76],
            backgroundColor: '#36a2eb' //blue
        }]
    }
});

</Script>

也许我缺少针对Dual-yAxes图表的某些东西...

1 个答案:

答案 0 :(得分:2)

yAxesID替换为yAxisID时出现拼写错误,并且数据集填充应类似于以下代码,或者您可以看到小提琴供参考-> https://jsfiddle.net/69p7Lsth/1/

datasets: [{
      label: 'Gravity',
      yAxisID: 'SG',
      data: [1.07, 1.045, 1.030, 1.014, 1.012],
      backgroundColor: '#ff6384' //red
    }, {
      label: 'Temp F',
      yAxisID: 'Temp',
      data: [8, 66, 78, 66, 76],
      backgroundColor: '#36a2eb' //blue
    }]