无需使用Shift +单击/ Ctrl +单击即可进行多种选择

时间:2015-12-02 11:06:00

标签: javascript json highcharts tablet drilldown

我使用Highcharts创建了一个图表,在第二次点击中向下钻取以显示更多信息。在第一次点击中,我还可以使用Shift+clickCtrl+click选择多个图表。我还实现了一个div,当用户选择一个或多个列并显示一些信息时会显示。{/ p>

我的麻烦是:我想将此图表实施到iPad或任何其他便携式设备,因此用户不会拥有shiftctrl密钥。我想创建一个多选项,其中第一次单击选择用户单击的任何字符(并在再次单击时取消选择),第二次单击显示一个向下钻取,所有这些都保留了我的结构。我知道这是一个问题但我会感激任何帮助。

HTML code:

<html>
    <head>
        <title>Highcharts</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://code.highcharts.com/highcharts.js"></script>
        <script src="https://code.highcharts.com/modules/data.js"></script>
        <script src="http://github.highcharts.com/modules/exporting.js"></script>
        <script src="https://code.highcharts.com/modules/drilldown.js"></script>
        <script src="highcharts.js" type="text/javascript"></script>
        <!--Black theme
        <script src="black-theme.js" type="text/javascript"></script>
        -->
    </head>
    <body>
        <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>       
    </body>
</html>

Javascript代码:

$(function () {
    // Create the chart
    $('#container').highcharts({
        chart: {
            type: 'column',
            events: {
                drilldown: function (e) {
                    if (!e.seriesOptions && e.point.selected) {
                        var chart = this,
                                points = chart.getSelectedPoints(),
                                drilldowns = {
                                    'Animals': {
                                        name: 'Animals',
                                        data: [
                                            ['Cows', 2],
                                            ['Sheep', 3]
                                        ]
                                    },
                                    'Fruits': {
                                        name: 'Fruits',
                                        data: [
                                            ['Apples', 5],
                                            ['Oranges', 7],
                                            ['Bananas', 2]
                                        ]
                                    },
                                    'Cars': {
                                        name: 'Cars',
                                        data: [
                                            ['Toyota', 1],
                                            ['Volkswagen', 2],
                                            ['Opel', 5]
                                        ]
                                    }
                                };

                        Highcharts.each(points, function (p) {
                            chart.addSingleSeriesAsDrilldown(p, drilldowns[p.name]);
                        });
                        chart.applyDrilldown();
                    }

                },
                drillup: function (e) {
                    var chart = this,
                            points = [];
                    setTimeout(function () {
                        points = chart.getSelectedPoints();

                        Highcharts.each(points, function (p) {
                            // unselect points from previous drilldown
                            p.selected = false;
                            p.setState('', true);
                        });
                    }, 0);
                }
            }
        },
        title: {
            text: 'Async drilldown'
        },
        xAxis: {
            type: 'category',
            categories: []
        },
        yAxis: {
            title: {
                text: 'Values'
            }

        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f}</b></td></tr>',
            footerFormat: '</table>',
            shared: false,
            useHTML: true
        },
        legend: {
            enabled: false
        },
        plotOptions: {
            column: {
                allowPointSelect: true
            },
            series: {
                borderWidth: 0,
                dataLabels: {
                    enabled: true
                },
                point: {
                    events: {
                        select: function (event) {
                            var text = 'All selected points: <br/>',
                                    chart = this.series.chart,
                                    otherSelected = chart.getSelectedPoints();
                            if (event.accumulate) {
                                Highcharts.each(otherSelected, function (point) {
                                    text += point.name + ', value: ' + point.y + '<br/>';
                                });
                            }
                            var pretext = this.name + ', value: ' + this.y + ' (last selected)';
                            text += pretext.bold();
                            if (!chart.lbl) {
                                chart.lbl = chart.renderer.label(text, 100, 70)
                                        .attr({
                                            padding: 10,
                                            r: 5,
                                            fill: Highcharts.getOptions().colors[1],
                                            zIndex: 5
                                        })
                                        .css({
                                            color: '#FFFFFF'
                                        })
                                        .add();
                            } else {
                                chart.lbl.attr({
                                    text: text
                                });
                            }
                        }
                    }
                }
            }
        },
        series: [{
                allowPointSelect: true,
                name: 'Test things',
                colorByPoint: true,
                data: [{
                        name: 'Animals',
                        y: 2,
                        drilldown: true
                    }, {
                        name: 'Fruits',
                        y: 7,
                        drilldown: true
                    }, {
                        name: 'Cars',
                        y: 4,
                        drilldown: true
                    }]
            }],
        drilldown: {
            series: []
        }
    })
});

Jsfiddle: http://jsfiddle.net/ah7gzxth/

编辑:我想在我的图表中实现以下jsfiddle,但我无法实现这一目标。 http://jsfiddle.net/3zy8p/58/

要点:

我想执行多项选择(类似于allowPointSelect:true)而不使用第一个图表中的Shift + click / Ctrol +点击以及我的代码中实现的下钻。

感谢大家!

0 个答案:

没有答案