Highcharts多个y轴和动态更新

时间:2015-01-16 20:59:52

标签: javascript dynamic highcharts

我正在尝试这种类型的图表,我修改并组合了两个示例。以下是jsfiddle中的结果:http://jsfiddle.net/cp0ux9qp/4/

$(function () {
    $(document).ready(function () {
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                zoomType: 'xy',
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {

                        // set up the updating of the chart each second
                        var serie1 = this.series[0];
                        var serie2 = this.series[1];
                        setInterval(function () {
                            var x = (new Date()).getTime(), // current time
                                y = Math.floor((Math.random() * 10) + 5),
                                z = Math.floor((Math.random() * 20) + 15);
                            serie1.addPoint([x, y], true, true);
                            serie2.addPoint([x, z], true, true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: 'Oxygen and Temperature'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: [{ // Primary yAxis
                labels: {
                    format: '{value}mg/L',
                    style: {
                        color: Highcharts.getOptions().colors[0]
                    }
                },
                title: {
                    text: 'Oxygen',
                    style: {
                        color: Highcharts.getOptions().colors[0]
                    }
                }
            }, { // Secondary yAxis
                title: {
                    text: 'Temperature',
                    style: {
                        color: Highcharts.getOptions().colors[1]
                    }
                },
                labels: {
                    format: '{value}C',
                    style: {
                        color: Highcharts.getOptions().colors[1]
                    }
                },
                opposite: true
            }],
            tooltip: {
            shared: true
        },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'Oxygen',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.floor((Math.random() * 10) + 5)
                        });
                    }
                    return data;
                }())
            },
                     {
                name: 'Temperature',
                data: (function () {
                    // generate an array of random data
                    var data = [],
                        time = (new Date()).getTime(),
                        i;

                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: Math.floor((Math.random() * 20) + 15)
                        });
                    }
                    return data;
                }()),
                yAxis:1
            }
            ]
        });
    });
});

它没有显示次要的Y轴,为什么? 当数据在一段时间内相同时会发生另一个问题,例如: 氧气:6.74 温度:16.7 X轴刻度被修改,图表只显示一行。

1 个答案:

答案 0 :(得分:0)

隐藏辅助轴的问题是因为在容器样式属性+你的chart.marginRight: 20中使用min-width声明,你可以从div中绘制轴。如果您注释掉边距,则会看到辅助轴。

至于具有相同的温度/氧含量 - 它应该如何表现?