Menu

Series.setPoints() not working

Henry Sue
2011-09-07
2012-07-13
  • Henry Sue

    Henry Sue - 2011-09-07

    I posted a topic earlier about the need of an API to reset the color for series. One workaround Shawn suggested is to keep the reference to the series object and call Series.setPoints() to update the chart when new data arrives. Unfortunately this approach doesn't work. There seems to be a bug in Series.setPoints(). Only one data point would show up after I make this call. The following program can reproduce this bug.

    `
    public class HighchartTest implements EntryPoint {
    private static final int DATA_LENGTH = 10;
    
    private Number[] randomData(){
        int length = DATA_LENGTH;
        Number[] data = new Number[length];
        for(int i = 0; i < length; i++){
            data[i] = Double.valueOf(Math.random() * 100);
        }
        return data;
    }
    
    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        Chart chart = new Chart();
        chart.setToolTip(new ToolTip().setFormatter(new ToolTipFormatter() {
            public String format(ToolTipData toolTipData) {
                return toolTipData.getXAsString() + ": "
                        + toolTipData.getYAsLong();
            }
        }));
    
        final Series series = chart.createSeries().setName("Random Data").setPoints(randomData());
        series.setType(Type.COLUMN);
        chart.addSeries(series);
        String[] categories = new String[DATA_LENGTH];
        for(int i = 0; i < categories.length; i++){
            categories[i] = "Day " + i;
        }
        chart.getXAxis().setCategories(categories);
    
        FlowPanel panel = new FlowPanel();
        panel.add(chart);
        Button refresh = new Button("Refresh");
        refresh.addClickHandler(new ClickHandler(){
            @Override
            public void onClick(ClickEvent event) {
                series.setPoints(randomData());
    
            }
    
        });
        panel.add(refresh);
    
        RootLayoutPanel.get().add(panel);       
    }
    }
    

    `

     

    Last edit: Henry Sue 2011-09-07
  • Shawn Quinn

    Shawn Quinn - 2011-09-29

    I've investigated this a bit, and can't seem to duplicate the problem you're seeing. The following is a trimmed down example (which uses your "randomData" function) which shows the "setPoints" method in operation:

    Chart chart = new Chart();
    final Series series = chart.createSeries()
        .setName("Random Data")
        .setPoints(randomData());
    chart.addSeries(series);
    chart.setClickEventHandler(new ChartClickEventHandler() {
        public boolean onClick(ChartClickEvent chartClickEvent) {
            series.setPoints(randomData());
            return true;
        }
    });
    

    I'm running against GWT Highcharts version 1.1.1 though, so you may want to try upgrading to see if that resolves the problem. If this still doesn't work for you though, it's possible that you're being bit by the issue with the "splat()" function in the core highcharts.js file. There's a related post on the Highcharts forums here:

    http://highslide.com/forum/viewtopic.php?f=9&t=11268&p=56062

    You'll see on that post I proposed replacing the splat() function in Highcharts with the following to work around the problem:

    function splat(obj) {
       if (!obj || (typeof obj.length == 'undefined')) {
          obj = [obj];
       }
       return obj;
    }
    

    Can you let us know if either of those ideas work for you?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.