下面是一个使用“老派”弹出窗口的 Highcharts 示例:
索引.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>
弹出窗口.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,我无法重现您的错误。
EDIT :
好的,所以你已经拥有了处理这个问题的所有东西。
我看到两个选择:
您发送用户输入series
通过 AJAX 请求将 JSON 数据发送到您的服务器。然后您的服务器向您发送回一个视图或一堆 html/js,其中包含带有用户数据的高图。回到客户端,您可以执行任何您想要的操作(例如触发包含图表的弹出窗口)。我对主干不太满意,但我确信你可以生成一个模板并将其渲染回来(这可能会有所帮助http://japhr.blogspot.fr/2011/08/getting-started-with-backbonejs-view.html)
另一种解决方案是直接将模板(包含图形)设置到视图,但默认隐藏他。然后,当series
由用户正确设置,您只需显示模板(例如在弹出窗口中)。这个解决方案避免了服务器调用,所以我建议这样做。
EDIT 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);
。 (这在上面的链接中进行了解释)。
这两个链接也值得一读:
- 使用 Highcharts 通过 JSON 重新加载图表数据
-
http://www.highcharts.com/documentation/how-to-use(第 3 部分和第 4 部分)
现在您应该能够根据用户输入对图表执行任何您想要的操作。