Highcharts |制作多个y轴刻度

时间:2017-05-30 18:41:29

标签: highcharts

我试图让这个图表有多个Y轴,所有Y轴都有不同的值和刻度间隔。像信号强度一样,在0%-100%的范围内,温度0F-100F和主电源是0V到25V,但似乎无法弄明白。这是我的jFiddle: http://jsfiddle.net/0k5k8ygz/

代码:

function createChart() {

    Highcharts.stockChart('container', {

        rangeSelector: {
            selected: 4
        },

        yAxis: [{ // Primary yAxis 
            labels: {
                format: '{value}°F',
                style: {
                    color: Highcharts.getOptions().colors[2]
                }
            },
            title: {
                text: 'Temperature',
                style: {
                    color: Highcharts.getOptions().colors[2]
                }
            },
            opposite: true

        }, { // Secondary yAxis
            gridLineWidth: 0,
            title: {
                text: 'Main Power',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} volts',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            }

        }, { // Tertiary yAxis
            gridLineWidth: 0,
            title: {
                text: 'Signal Strength',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            labels: {
                format: '{value} %',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            opposite: true
        }],

        plotOptions: {
            series: {
                compare: 'percent',
                showInNavigator: true
            }
        },

        tooltip: {
            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
            valueDecimals: 2,
            split: true
        },

        series: seriesOptions
    });
}


$.each(names, function(i, name) {

    $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=' + name.toLowerCase() + '-c.json&callback=?', function(data) {

        seriesOptions[i] = {
            name: name,
            data: data
        };

        // As we're loading the data asynchronously, we don't know what order it will arrive. So
        // we keep a counter and create the chart when all the data is loaded.
        seriesCounter += 1;

        if (seriesCounter === names.length) {
            createChart();
        }
    });
});

1 个答案:

答案 0 :(得分:8)

如果向每个y轴添加一些数据,它们将自动获得刻度线。您可以使用series.yAxis索引将数据分配给特定的y轴:

seriesOptions[i] = {
    name: name,
    data: data,
    yAxis: i,
};

如果您还想为y轴指定有效范围,可以在每个轴上设置minmax

...
labels: {
    format: '{value} volts',
},
min: 0,
max: 25,
...

http://jsfiddle.net/0k5k8ygz/5/

相关问题