videv - 2012-08-06

Hello,

Run the following code, click the refresh button, followed by two prepend clicks.

As the highchart documentation notes (http://www.highcharts.com/stock/ref/#series-object) the data array might not contain all point data. It advises to use series.options.data instead - which always returns the x and y values. I have had to temporarily introduce an alternative JSNI call to fix this problem for our use case.

Could you please add the option to retrieve the data points from the series.options.data?

!/usr/bin/java

@Override
public void onModuleLoad()
{
    final StockChart chart = new StockChart();
    chart.setPersistent( true );
    chart.getXAxis().setType( Type.DATE_TIME );

    final Series series = chart.createSeries();

    chart.addSeries( series );


    SimplePanel sp = new SimplePanel( chart );

    RootPanel.get().add( sp );

    List<Point> points = new ArrayList<Point>();

    long time = System.currentTimeMillis();

    lastTime = time;

    for( int x=0; x<10; x++)
    {
        double rand = 100 + (int)(Math.random()*1000);

        lastTime = time + TimeUnit.MINUTES.toMillis( 10 * x );
        points.add(new Point(Long.valueOf( lastTime ) , Double.valueOf( rand )));
    }

    for (Point point : points)
    {
        series.addPoint( point, false, false, false );
    }

    Button b = new Button("refresh");
    b.addClickHandler( new ClickHandler()
    {
        @Override
        public void onClick(final ClickEvent eventparam)
        {
            chart.redraw();
        }
    });

    Button b2 = new Button("prepend");
    b2.addClickHandler( new ClickHandler()
    {

        private int counter = 1;

        @Override
        public void onClick(ClickEvent eventparam)
        {
            List<Point> points = new ArrayList<Point>();

            long time = System.currentTimeMillis() - TimeUnit.HOURS.toMillis( 10 * counter++ );

            long lastTime = time;

            for( int x=0; x<10; x++)
            {
                double rand = 100 + (int)(Math.random()*1000);

                lastTime = time + TimeUnit.MINUTES.toMillis( 10 * x );
                points.add(new Point(Long.valueOf( lastTime ) , Double.valueOf( rand )));
            }

            checkExisting( series.getPoints() );

            points.addAll( Arrays.asList( series.getPoints() ) );

            series.setPoints( points.toArray( new Point[]{} ) );
        }

        private void checkExisting(Point[] pointsparam)
        {
            for (int i = 0; i < pointsparam.length; i++)
            {
                Point point = pointsparam[i];

                if( point.getX() == null || point.getY() == null )
                {
                    System.out.println("existing point " + i + " is bad...");
                }
            }
        }
    });

    Button b3 = new Button("append");
    b3.addClickHandler( new ClickHandler()
    {
        @Override
        public void onClick(ClickEvent eventparam)
        {
            double rand = 100 + (int)(Math.random()*1000);

            lastTime = lastTime + TimeUnit.MINUTES.toMillis( 5 );

            series.addPoint( Long.valueOf( lastTime  ), Double.valueOf( rand ) );
        }
    });

    RootPanel.get().add( b );
    RootPanel.get().add( b2 );
    RootPanel.get().add( b3 );
}