javascript - 在 Highcharts 上使用 XY 缩放时,如何隐藏未选定的系列和轴?

标签 javascript jquery highcharts

我们使用 highcharts(特别是 highstock)来绘制多种类型的数据的图表。我们希望利用 XY 缩放类型,但是,如果我们能够仅放大通过选择框选择的内容,那就太棒了。

但是,当使用 XY 进行放大时,如果您在另一轴中没有选择任何数据,则该轴将不会显示任何内容。如果突出显示/选定的区域在整个图表上重新绘制,那就太理想了。

我尝试在浏览器控制台窗口中查看图表对象,并且诸如“series[x].visible”之类的值对于缩放后的所有轴都是相同的。是否有可能实现我的要求?

编辑:我应该注意,我们不需要将图表彼此分层,只是因为我们将显示的数据量很大,这就是两个图表独立显示的原因。

fiddle :http://jsfiddle.net/cwc4em4w/

$('#container').highcharts({
    chart: {
        type: 'line',
        rightMargin: 80,
        zoomType: 'xy'
    },        

    yAxis: [{
        id: 'hi',
        height: '40%',
        top: '10%',
        title: { text: 'label 1', y: 0, x: -30, align: 'middle' },
        labels: { align: 'left', x: -25 }
    }, {
        id: 'ho',
        height: '40%',
        top: '60%',
        title: { text: 'label 2', y: 0, x: 0, align: 'middle' },
        labels: { align: 'left', x: 0 }
    }],

    xAxis: [{
        type: 'datetime',
        tickInterval: 24 * 3600 * 1000,
        yAxis: 'hi'
    },{
        type: 'datetime',
        tickInterval: 24 * 3600 * 1000,
        yAxis: 'ho'
    }],

    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4],
        pointStart: Date.UTC(2010, 0, 1),
        pointInterval: 24 * 3600 * 1000,
        yAxis: 'hi'
    },{
        data: [129.9, 171.5, 1106.4, 1129.2, 1144.0, 1176.0, 1135.6, 1148.5, 1216.4],
        pointStart: Date.UTC(2010, 0, 1),
        pointInterval: 24 * 3600 * 1000,
        yAxis: 'ho'
    }]
});

最佳答案

是的,这是可能的,但只需手动处理即可:see this fiddle .

要实现所描述的行为,您可以 update the yAxis properties关于以下legend item click handler :

plotOptions: {
    series: {
        events: {
            legendItemClick: function (e) {
                var thisIndex = this.index;
                var otherIndex = (this.index == 0) ? 1 : 0;
                var isOtherEnabled = false;
                if (this.chart.yAxis[otherIndex].options.labels.enabled) {
                    isOtherEnabled = true;
                }
                var isThisEnabled = false;
                if (this.chart.yAxis[thisIndex].options.labels.enabled) {
                    isThisEnabled = true;
                }

                if (isThisEnabled) {
                    this.chart.yAxis[thisIndex].update({
                        labels: {
                            enabled: false
                        },
                        title: {
                            text: null
                        }
                    });
                    if (isOtherEnabled) {
                        this.chart.yAxis[otherIndex].update({
                            height: '80%',
                            top: '10%'
                        });
                    }
                } else {
                    this.chart.yAxis[thisIndex].update({
                        labels: {
                            enabled: true
                        },
                        title: {
                            text: (thisIndex == 0) ? 'label 1' : 'label 2'
                        },
                        height: (isOtherEnabled) ? '40%' : '80%',
                        top: (isOtherEnabled) ? ((thisIndex == 0) ? '10%' : '60%') : '10%'
                    });
                    if (isOtherEnabled) {
                        this.chart.yAxis[otherIndex].update({
                            height: '40%',
                            top: (otherIndex == 0) ? '10%' : '60%'
                        }); 
                    }
                }
                this.chart.reflow();
            }
        }
    }
},

此外,我发现您试图通过更改 xy 来对齐 yAxis 标签的位置。但它使定位相对于另一个轴:因此,首先,当应用上面的代码片段时,隐藏第二个系列时您将看不到 label 1 标题,因为它将被定位在 highcharts 容器之外。要解决该问题,只需确保定位 yAxis.offset正确,以及its title relative offset :

yAxis: [{
    id: 'hi',
    height: '40%',
    top: '10%',
    offset: 30,
    title: { text: 'label 1', offset: 30, align: 'middle' },
    labels: { align: 'left' }
}, {
    id: 'ho',
    height: '40%',
    top: '60%',
    offset: 30,
    title: { text: 'label 2', offset: 30, align: 'middle' },
    labels: { align: 'left' }
}],

关于javascript - 在 Highcharts 上使用 XY 缩放时,如何隐藏未选定的系列和轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28244646/

相关文章:

javascript - Node redis 客户端 HSET 命令失败

javascript - 带有 'datetime' xAxis 的 Highcharts 图表 - 在向下钻取时使用类别

Highcharts x Axis 自定义缩放

javascript - 如何居中一个绝对位置的div?

javascript - 寻找一个JS库来移动文本

jQuery 自动完成插件不聚焦下一个单击的字段

javascript - 将jquery库函数写入typescript

javascript - 循环中的嵌套 Jquery 选择器当前元素

javascript - 在 highcharts 中使用 BigInt 数字

javascript - 使用 html 输入提供的信息