javascript - 新窗口中的 Highcharts

标签 javascript backbone.js highcharts

我使用 highcharts 在我的页面中显示图表。 它工作正常,但有时图表中的数据过于“浓缩”,所以我应该找到一种方法来查看更大尺寸的图表。

我在互联网上阅读了几篇关于这个主题的帖子: - 通常他们建议使用 highslide,但我不想,因为我的页面已经被脚本覆盖 -有人试图在弹出窗口中弹出内容,它可能适合我,但我没有成功 - 唯一适合我的准工作示例如下: (在选项对象中我添加了这个属性)。

exporting: {
                    buttons: {
                        popUpBtn: {
                            symbol: 'square',
                            _titleKey: 'FullScreenButtonTitle',
                            x: -60,
                            symbolSize:18,
                            symbolFill: '#B5C9DF',
                            hoverSymbolFill: '#779ABF',
                            onclick: function () {
                                var win=window.open('','','location=0,titlebar=0,status=0,width=780,height=350');
                                win.focus();
                                var divtag = win.document.createElement("div");
                                divtag.id = "div1";
                                win.document.body.appendChild(divtag);
                                win.document.write('<script type="text/javascript" src="script/highcharts/js/highcharts.js"></script>\
                                                                    <script type="text/javascript" src="script/highcharts/js/modules/exporting.js"></script>');
                                this.options.renderTo=divtag;
                                var chart = new Highcharts.Chart(this.options);

                                win.document.close();
                            }
                        },
                        exportButton: {
                            enabled: true
                        },
                        printButton: {
                            enabled: true
                        }

                    }
                }

但是它不起作用,因为没有插入 div 标签,我得到的就是这个

<html>
 <head>
   <script type="text/javascript" src="script/highcharts/js/highcharts.js">  </script>
    <script type="text/javascript" src="script/highcharts/js/modules/exporting.js"></script>
    </head></html>

我无法理解这个错误。 我知道这应该很简单,但我不能一个人出去。

--编辑---

我终于明白这可能是一个可行的策略:创建一个“chartpopup.html”页面,并将构建我可视化的图表副本所需的参数传递给它。

所以现在我有:

index.html:

    chartOptions = {
            series: [
                {
                    name: 'example',
                    data: [83.6, 78.8, 98.5, 93.4, 106.0]
                }]    
          //// CUT SOME CODE

                exporting: {
                    buttons: {
                        popUpBtn: {
                            enabled:true,
                            symbol: 'square',
                            _titleKey: 'FullScreenButtonTitle',
                            x: -60,
                            symbolSize:18,
                            symbolFill: '#B5C9DF',
                            hoverSymbolFill: '#779ABF',
                            onclick: function () {
                                look this!--------> generalPurposeGlobalVar = this;
                                var win=window.open('./chartpopup.html','Full Size Chart','location=0,titlebar=0,status=0,width=780,height=650');

                            }
                        },
                        exportButton: {
                            enabled: true
                        },
                        printButton: {
                            enabled: true
                        }

                    }
                }
            };
        this.highChart=new Highcharts.Chart(this.chartOptions);

和 chartpopup.html:

   <!DOCTYPE html>
   <html>
   <head>
       <meta charset="utf-8">
       <title>Chart full Size</title>
   <script type="text/javascript" src="script/jquery-1.7.1-min.js"></script>
   <script type="text/javascript" src="script/highcharts/js/highcharts.js"></script>
   <script type="text/javascript" src="script/highcharts/js/modules/exporting.js">              </script>

   </head>
   <body>

   <div id="container" style="min-width: 400px; height: 650; margin: 0 auto"></div>

   <script>
   var chart;

   $(document).ready(function() {

       var mychart=window.opener.generalPurposeGlobalVar;

       mychart.options.chart.renderTo= 'container';
       chart = new Highcharts.Chart(mychart.options);


   });
   </script>
   </body>
   </html>

这两个页​​面实际上只适用于默认图表。如果我修改并重新呈现图表,我将无法在弹出页面上重现它!

我用来修改图表的代码基本上是这样的:

        this.chartOptions.series=[{name:field.split('_').join('\n')}];
        this.highChart.destroy();
        this.highChart=new Highcharts.Chart(this.chartOptions);
        this.highChart.xAxis[0].setCategories(_.isEmpty(mygroups) ? [] : mygroups);
        this.highChart.series[0].setData([]);
        this.highChart.setTitle({text: this.highChart.title.text},{text:(field.split('_').join(' ')),  });
        this.highChart.redraw();//
   [...]
        self.highChart.series[0].addPoint(result);//it's a point I calculated before

最佳答案

这是一个使用“oldschool”弹出窗口的 Highcharts 示例:

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>highcharts foobar</title>
</head>
<body>
    <a href="javascript:open_chart_popup();">Open Chart Popup</a>
<script>
    function open_chart_popup() {
        window.open('popup.html', 'chart popup title', 'width=1680px height=1050px');
    }
</script>
</body>
</html>

popup.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>highcharts foobar</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

<script>
var chart;

$(document).ready(function() {
    chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container',
            type: 'line',
            marginRight: 130,
            marginBottom: 25
        },
        title: {
            text: 'Monthly Average Temperature',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
            x: -20
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            formatter: function() {
                return '<b>'+ this.series.name +'</b><br/>'+
                this.x +': '+ this.y +'°C';
            }
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: [{
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'New York',
            data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
        }, {
            name: 'Berlin',
            data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
});
</script>
</body>
</html>

如果此解决方案不适合您,能否告诉我们您使用了哪些 JavaScript 库(此示例依赖于 jQuery)。正如文档所说,highcharts 需要 jQuery、Mootools 或 Prototype:http://www.highcharts.com/documentation/how-to-use

如果您能够使用 jQuery,则可以使用一些更酷的效果来替换该弹出窗口,例如:http://jqueryui.com/demos/dialog/

尽管如此,如果您需要脚本方面的帮助,您可以考虑制作一个 jsfiddle 吗? ,我无法重现您的错误。

编辑:

好的,所以您拥有处理该问题的所有内容。

我看到两个选项:

  • 您通过 AJAX 请求将用户输入的 series JSON 数据发送到您的服务器。然后你的服务器发回给你一个 View 或一堆包含你的 Highcharts 和用户数据的 html/js。回到客户端,你可以随心所欲地做(比如触发一个包含图表的弹出窗口)。我对 backbone 不太满意,但我相信你可以生成一个模板并将其渲染回来(这可能会有所帮助 http://japhr.blogspot.fr/2011/08/getting-started-with-backbonejs-view.html )

  • 另一种解决方案是直接将模板(包含图表)设置为 View ,但默认情况下隐藏他。然后,当用户正确设置 series 时,您只需显示模板(例如在弹出窗口中)。此解决方案避免了服务器调用,因此我建议这样做。

编辑 2:

因此,我制作了一个 jsFiddle 来展示如何根据用户输入更新图表的基本示例:http://jsfiddle.net/MxtkM/

该示例更新图表上所有系列的最后一个值,方法如下:

$('#december_value').bind('keyup', function() {
    var user_input_value = parseFloat($(this).val()); // cast to float

    for (var s in chart.series) { // loop through the series
        var old_data = chart.series[s].data;
        var new_data = [];

        for (var d in old_data ) { // loop through data objects
           new_data.push(old_data[d].config); // config property contains the y value
        }

        new_data[new_data.length - 1] = user_input_value; // update the last value

        chart.series[s].setData(new_data); // use setData method to refresh the datas of the serie
    }
});

这个例子通过提供一个新的数据数组来使用 setData 方法。 如果这不符合您的需要,还有另一种方法可以刷新您的图表,您可以通过执行 var chart = new Highcharts.Chart(options); 重新呈现所有图表。 (这在上面的链接中有解释)。

这两个链接也很好读:

现在您应该能够根据用户输入对图形做任何您想做的事情了。

关于javascript - 新窗口中的 Highcharts ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9843941/

相关文章:

javascript - jquery=>发送数据

javascript - Rails 3.1 中的 EJS gem 无法正确编译 JavaScript 模板

javascript - Backbone 后退按钮滚动问题

javascript - Highcharts : trouble with axis Label

javascript - Highcharts,仅在选定点上显示标记

javascript - 在 PHP 或 JS 中将数字列表转换为带有后缀的特定价格格式?

javascript - 如何在angularJS中过滤多个动态值

javascript - 'el' 在 vi​​ew.render().el 中做了什么?

javascript - 将字符串转换为数组以用于 highcharts js

javascript - AngularJS 表单验证无法按预期工作