使用单选按钮高图表停止添加数据

stop data being added with radio button highchart

本文关键字:添加 数据 高图表 单选按钮      更新时间:2023-09-26

当我不断点击单选按钮时,它会不断添加一个系列,而我不想这样做,

我的代码:

  $(".test").change(function() {
      var value = this.getAttribute("value");
      if (value == 'a') {
          chart.yAxis[0].setTitle({
              text: "data"
          });
          if (chart.series.length >= 3) chart.series[1].remove();
          chart.addSeries({
              name: '2011',
              type: 'column',
              color: '#08F',
              data: [1, 0, 4]
          });
          chart.series[1].remove();
          chart.addSeries({
              name: '2012',
              type: 'column',
              color: '#808000',
              data: [8, 5, 3]
          });
          chart.addSeries({
              name: '2013',
              type: 'column',
              color: '#FFA500',
              data: [8, 1, 2]
          });
      }

单选按钮:

<input class="test" name="g" type="radio" value="a"> data</input>
<input class="test" name="g" type="radio" value="b"> data1</input>
<input class="test" name="g" type="radio" value="c"> data2</input>

问题是当我点击一个按钮时,它一直在添加系列

if (chart.series.length >= 3)
    chart.series[1].remove();

这让我很困惑,我认为这是错误的。。我希望这3个数据在我点击a时显示一次。但是它一直在添加数据。。。

在jfiddle中,当我单击按钮时,您可以看到它不断添加数据。当我只想让它显示我给每个按钮的数据时。。http://jsfiddle.net/Qkzx6/9/

if s并不能涵盖所有机会:例如,如果你有B(图表中有一个系列),并且你按A,你的第一个if不会开火(length不是2或更多),而你的另一个remove会移除第二个系列(series是零);如果你从C(有两个系列的图表)到A,你的第一个if将再次删除第二个系列,然后你再添加一个,然后你再次删除第2个,等等。

只需移除每个if中对remove的所有单独调用,并在移除所有系列的第一个if之前放置一个while块。像这样:

while (chart.series.length > 0) {
  chart.series[0].remove(true);
}

你的jsFiddle在这里更正了。