简体   繁体   English

Highcharts / Highstock动态添加/删除多个系列

[英]Highcharts/Highstock dynamically add/remove multiple series

I'm a Java/PHP programmer with an unreasonable inability to grasp Javascript, so I need a hand with something that should be simple. 我是一个Java / PHP程序员,无法掌握Javascript,所以我需要一些简单的东西。

Take the chart here: http://www.highcharts.com/stock/demo/compare 请点击此处的图表: http//www.highcharts.com/stock/demo/compare

This shows 3 series, which are all predefined. 这显示了3个系列,它们都是预定义的。 I have the equivalent set up with my own data. 我有与我自己的数据相同的设置。

What I actually have is 150 series of data, and I want the user to be able to choose which to display without a page refresh. 我实际拥有的是150系列数据,我希望用户能够选择在没有页面刷新的情况下显示哪些数据。 I know I need to use Chart.addSeries() in some way, and I've looked at the demo which adds a series from a button click: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/stock/members/chart-addseries/ 我知道我需要以某种方式使用Chart.addSeries(),我看了一下从按钮点击添加系列的演示: http//jsfiddle.net/gh/get/jquery/1.7.2/ highslide软件/ highcharts.com /树/主/样品/股票/构件/图表-addseries /

The thing is, this button is hard coded in both the web page and the JS: 问题是,这个按钮在网页和JS中都是硬编码的:

<button id="button">Add series</button>

<script>
$('#button').click(function() {
    var chart = $('#container').highcharts();
    chart.addSeries({
        name: 'ADBE',
        data: ADBE
    });
    $(this).attr('disabled', true);
});
</script>

I don't fancy 150 buttons, but I certainly don't want a unique function per button. 我不喜欢150个按钮,但我当然不希望每个按钮有一个独特的功能。 Can someone rewrite a short example that I could cannibalise? 有人可以重写一个我可以蚕食的简短例子吗? My JS really is too rudimentary. 我的JS真的太简陋了。

Maybe check-boxes would be fine, but really any kind of toggle would work. 也许复选框会很好,但实际上任何类型的切换都可以。 Can series be removed from the chart (other than clicking on the legend)? 可以从图表中删除系列(除了点击图例)吗?

Can anyone provide some tips on tying the list of 150 series to the chart itself? 任何人都可以提供一些关于将150系列列表绑定到图表本身的技巧吗?

Each series must be loaded individually via JSON. 必须通过JSON单独加载每个系列。

Or, should I just load all 150 series and disable 149 of them, allowing the user to toggle via the legend itself? 或者,我应该只加载所有150系列并禁用其中的149个,允许用户通过图例本身切换?

I made you a solution using check-boxes. 我使用复选框为您制作了解决方案。 The targeted checkboxes are the ones with class "choice". 目标复选框是具有“选择”类的复选框。 The "foreach" function goes through all the checkboxes. “foreach”功能遍历所有复选框。 The name becomes the "value" of the current choice, and since you didn't provide any context for the data the data is currently being fed manually. 该名称成为当前选择的“值”,并且由于您没有为数据提供任何上下文,因此当前正在手动输入数据。 You can play with it and make it work for you. 你可以玩它,让它适合你。

$('#button').click(function() {
    var buttons = $('.choice');
    var chart = $('#container').highcharts();

    buttons.each(function(){
        var choice = this.value;

        chart.addSeries({
            name: choice,
            data: ADBE
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM