是否可以在运行时将导航器隐藏在高图中?

Dav*_*len 5 highcharts highstock

我正在开发一个highcharts项目,我们需要在运行时显示/隐藏导航器,具体取决于屏幕上过滤器的值.

我们已经添加/显示/隐藏了各种数据系列 - 但是我找不到api调用,它允许我在运行时动态隐藏导航器?有没有人知道这样做的方法 - 我不愿意重新加载整个图表,除非我必须这样做.

谢谢大家!

Seb*_*han 9

您可以通过hide()函数隐藏所有特定的SVG导航器元素.

http://jsfiddle.net/dJbZT/1

$('#btn').toggle(function () {
            chart.scroller.xAxis.labelGroup.hide();
            chart.scroller.xAxis.gridGroup.hide();
            chart.scroller.series.hide();
            chart.scroller.scrollbar.hide();
            chart.scroller.scrollbarGroup.hide();
            chart.scroller.navigatorGroup.hide();
            $.each(chart.scroller.elementsToDestroy, function (i, elem) {
                elem.hide();
            })
        }, function () {
            chart.scroller.xAxis.labelGroup.show();
            chart.scroller.xAxis.gridGroup.show();
            chart.scroller.series.show();
            chart.scroller.navigatorGroup.show();
            chart.scroller.scrollbar.show();
            chart.scroller.scrollbarGroup.show();
            $.each(chart.scroller.elementsToDestroy, function (i, elem) {
                elem.show();
            })
        });
Run Code Online (Sandbox Code Playgroud)


Fri*_*fer 6

这似乎是更新 navigator.enabled 选项的最简单方法:

chart.update({navigator: { enabled: false }})
Run Code Online (Sandbox Code Playgroud)


Vic*_*ani 5

塞巴斯蒂安的答案几乎是存在,但实际上并没有调整图表本身的大小,在这种情况下,我认为这是必需的,因为否则导航器的空间将被完全浪费(更不用说较大的空白看上去很奇怪)。

这是一个更简单的解决方案:使用添加一个“ clipping” div overflow: hidden,然后充分增加图表容器的高度以将导航器推出,使其隐藏起来

演示版

http://jsfiddle.net/vickychijwani/z4kgsfnp/

$(function () {
    
    var delta = 0;

    $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function (data) {
        // Create the chart
        var chart = $('#container').highcharts('StockChart', {
            
            chart: {
                events: {
                    load: function () {
                        // this is always constant after the chart is loaded
                        delta = this.scroller.navigatorGroup.getBBox().height + 30;
                    }
                }
            },

            rangeSelector: {
                selected: 1
            },

            title: {
                text: 'AAPL Stock Price'
            },

            series: [{
                name: 'AAPL',
                data: data,
                tooltip: {
                    valueDecimals: 2
                }
            }]
        }, function (chart) {
            $('#show-hide-nav-btn').click(function () {
                // to hide the navigator, increase chart height; the excess will be clipped by the outer clip div because its CSS is set to overflow: hidden
                var chartHeight = $('.highcharts-container').height();
                $('#container').height(chartHeight + delta);
                $('.highcharts-container').height(chartHeight + delta);
                
                // manually reflow
                chart.reflow();
                
                // negate delta for the next click
                delta = -delta;
            });

        });
    });

});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/stock/highstock.js"></script>

<button id="show-hide-nav-btn">Show / hide navigator</button>

<!-- this div clips the container so the navigator can be hidden from view -->
<div id="clip" style="height: 500px; overflow: hidden;">
    <div id="container" style="height: 500px; min-width: 500px"></div>
</div>
Run Code Online (Sandbox Code Playgroud)