点击图表缩放图表

时间:2015-08-26 00:40:29

标签: javascript highstock

我知道highstock不支持点击缩放图 http://www.highcharts.com/docs/chart-concepts/zooming 但我想点击图表,图表将显示所有屏幕,包括表格"时间更新"。我能怎么做? 例如:http://jsfiddle.net/DuyThoLe/58bzo3vg/17/ enter code here

1 个答案:

答案 0 :(得分:1)

从这个Hgihcharts演示开始 - http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/chart/events-container/

...可以让它与您的图表一起使用:http://jsfiddle.net/6jLg8fg5/1/

后来你想在弹出窗口中包含“时间更新”,所以它被添加到将改变其类的div(并且因为CSS将显示在顶部,如弹出窗口):http://jsfiddle.net/6jLg8fg5/4/

接下来,您希望能够打印,导出,点击图例而不会弹出或退回图表。您可以推迟(例如使用setTimeout)弹出效果并检查点击是否未触发图例或导出菜单。 系列有legendItemClick事件 - 所以首先我们要检查一下。虽然打印图表会触发afterPrinting和beforePrinting事件。对于导出 - 可以使用换行(扩展Highcharts)从导出模块重写getChartHTML - http://jsfiddle.net/6jLg8fg5/5/

JS:

$(function () {
    (function (H) {
        H.wrap(H.Chart.prototype, 'getChartHTML', function (proceed) {
            printing = true;
            // Run original proceed method
            return this.container.innerHTML;
        });
    }(Highcharts));

    Highcharts.setOptions({
        global: {
            useUTC: false
        }
    });

    var legendClicked = false,
        printing = false; //or exporting
    // Create the chart
    $('#chart1').highcharts('StockChart', {
        chart: {
            borderWidth: 0,
            height: 283,
            width: 660,
            events: {
                beforePrint: function() {
                    printing = true;
                },
                afterPrint: function(){
                    printing = false;
                }
            }
        },

        rangeSelector: {
            buttons: [{
                count: 1,
                type: 'minute',
                text: '1M'
            }, {
                count: 5,
                type: 'minute',
                text: '5M'
            }, {
                type: 'all',
                text: 'All'
            }],
            inputEnabled: false,
            selected: 0
        },

        title: {
            text: 'Live random data'
        },
        legend: {
            enabled: true
        },
        exporting: {
            enabled: true
        },
        navigator: {
            enabled: true
        },
        series: [{
            events: {
                legendItemClick: function () {
                    legendClicked = true;
                }
            },
            name: 'Random data',
            data: (function () {
                // generate an array of random data
                var data = [],
                    time = (new Date()).getTime(),
                    i;

                for (i = -999; i <= 0; i += 1) {
                    data.push([
                    time + i * 1000,
                    Math.round(Math.random() * 100)]);
                }
                return data;
            }())

        }]

    });

    //the form action
    var updateInterval = 10000;
    $("#updateInterval").val(updateInterval).change(function () {

        var v = $(this).val();
        if (v && !isNaN(+v)) {
            updateInterval = +v;
            if (updateInterval < 1) {
                updateInterval = 1;
            } else if (updateInterval > 50000) {
                updateInterval = 10000;
            }
            $(this).val("" + updateInterval);
        }
    });

    function update() {
        var series = $('#chart1').highcharts().series[0];
        x = (new Date()).getTime(); // current time
        y = Math.round(Math.random() * 100);
        series.addPoint([x, y], true, true);
        setTimeout(update, updateInterval);
    }

    update();



    $('.chart-container').bind('mousedown', function () {
        var source = this;
        if(!printing) {
        setTimeout(function () {
            //check if legend was clicked
            if (legendClicked) {
                legendClicked = false;
            } else if ($('.highcharts-contextmenu').css('display') === 'block' || printing) {
                //check if context menu is visible
                printing = false;
            } else {
                //else do popup or back to default
                $('#chart-with-time-update').toggleClass('modal');
                $('.chart', source).highcharts().reflow();
            }
        }, 150);
        }
    });

});

CSS:

.main {
    width: 400px;
    margin: 0 auto;
}
.chart {
    min-width: 300px;
    max-width: 800px;
    height: 300px;
    margin: 1em auto;
}
.modal {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: rgba(0, 0, 0, 0.7);
}
.modal .chart {
    height: 90%;
    width: 90%;
    max-width: none;
}

最后,您需要使用多个图表 - http://jsfiddle.net/6jLg8fg5/13/

(这个问题也在Highcharts论坛上进行了讨论:http://forum.highcharts.com/highstock-usage/click-on-the-chart-to-zoom-chart-t33714/

相关问题