Menu

Newly added points disappear after rerender

Mike H
2012-01-18
2012-07-13
  • Mike H

    Mike H - 2012-01-19

    Here's the example code:

    chart = new Chart()
            .setType(Series.Type.SPLINE)
            .setLinePlotOptions(new LinePlotOptions().setZIndex(500))
            .setHeight100()
            .setWidth100()
            .setZoomType(Chart.ZoomType.X)
            .setChartTitleText("")
            .setLegend(
                    new Legend().setAlign(Align.RIGHT).setLayout(Legend.Layout.VERTICAL)
                            .setVerticalAlign(Legend.VerticalAlign.MIDDLE));
    
    chart.getYAxis().setAxisTitleText("Value");
    chart.getXAxis().setType(Axis.Type.DATE_TIME);
    .
    .
    .
    Series series = chart.createSeries().setName(name);
    curveList.add(new Curves(name, series));
    // add for example 79 points
    for (DatapointData i : dpdataset) {
        series.addPoint(i.getTimestamp().getTime(), i.getValue());
    }
    chart.addSeries(series, true, false);
    Point[] points = chart.getSeries()[0].getPoints(); // get Point array with 79 
    // items, but the x and y values of every item is null
    chart.getSeries()[0].addPoint(new Date().getTime(), (Random.nextDouble() * 1000.0), true, false, false);            
    Point[] points2 = chart.getSeries()[0].getPoints(); //get Point array with 80 
    // items, but the x and y values of every item is null, even the last one
    // if the chart rerenders now (e.g. move the chart to another parent) only the
    // 79 points that has been added to the series,
    // before the series were added to the chart, remain
    
     
  • Shawn Quinn

    Shawn Quinn - 2012-01-19

    I'm not able to duplicate the problem you're seeing here using that particular code (the x/y values of the points always come back non-null). However, not sure if this is the cause of the problem you're seeing, but this led me to notice a bug in the Series class. Specifically, the last line of the getPoints() method is currently the following:

    return convertedPoints.toArray(new Point[points.size()]);
    

    But that should be the following:

    return convertedPoints.toArray(new Point[convertedPoints.size()]);
    

    Are you have trouble with that "getPoints()" call before or after the chart has been rendered? If after, that may be the cause of the problem. We'll get that fixed in the next release but you could try that patch in the meantime...

     
  • Mike H

    Mike H - 2012-02-01

    Hi,

    Thank's for your help, but unfortunately that couldn't solve the problem.

    But after testing and debugging I found a solution for my problem, that I will share with you.

    The problem occurred when I try to add the chart to another panel and only the points remain in the chart that were added to the series before the series were added to the chart.
    The reason for this is, that when you add it to another panel the function onUnload is called immediately before onLoad is called. The chart is destroyed in the onUnload function and that's the reason why he cannot revive the new points from the series in the onLoad function.
    The solution I found is to add a new variable to the BaseChart called persistent and only destroy the chart if I really want it destroyed. When I just want to move the chart between different panels the persistent variable protect the chart from beeing destroyed.

    Use in the program:

            but.addClickHandler(new ClickHandler() {
    
                public void onClick(ClickEvent event) {
                    if (abs1.getWidgetCount() > 0){
                        chart.setPersistent(true);
                        abs2.add(chart);
                        chart.setPersistent(false);
                    } else {
                        chart.setPersistent(true);
                        abs1.add(chart);
                        chart.setPersistent(false);
                    }
                }
            });
    

    Patch I used for change your source files:

    Index: BaseChart.java
    ===================================================================
    --- BaseChart.java  (revision 625)
    +++ BaseChart.java  (revision 626)
    @@ -1149,6 +1149,17 @@
         private SeriesPlotOptions seriesPlotOptions;
         private ScatterPlotOptions scatterPlotOptions;
         private SplinePlotOptions splinePlotOptions;
    +    
    +    //to keep data on unload
    +    private boolean persistent = false;
    +    
    +    public boolean isPersistent() {
    +       return persistent;
    +   }
    +
    +   public void setPersistent(boolean persistent) {
    +       this.persistent = persistent;
    +   }
    
         /**
          * Updates the options that all area type series within the chart will use by default.  The settings can then
    @@ -1915,7 +1926,7 @@
    
         @Override
         protected void onUnload() {
    -        if (isRendered()) {
    +       if (isRendered() && !isPersistent()) {
                 nativeDestroy(chart);
                 chart = null;
             }
    

    I attached the project I used for testing.

    Thanks,
    Mike

    P.S.: I still don't understand why points that were added to the series before the series were added to the chart remains in the chart after new creation, even when the chart is destroyed in the onUnload function.

     
  • Mike H

    Mike H - 2012-03-20

    Hi,

    Thanks for adding the persistent feature in the new release. That's really nice!
    But now I'm trying do remove points from the chart(or better a series) and again I encountered a "persistent" problem.

    Currently I'm using something like this, to remove points from the chart:

        public void delDataset(String name, Date from, Date to) {
            Series series;
            series = getCurve(name);
            int size = series.getPoints().length;
            for (int i = 0; i < size; i++) {
                long timestamp = (((Double) series.getPoints()[i].getX())
                        .longValue());
                if (timestamp > from.getTime() && timestamp < to.getTime()) {
                    series.getPoints()[i].remove(false, false);
                    size--;
                    i--;
                }
            }
            chart.redraw();
        }
    

    I know this is very ineffective and slow, but it works. But when I move the chart to another panel and it redraws, all the points I removed are back again.

    This is because of the points-Array in the Series-class. If I remove a point from the chart and the chart is set to persistent=true, the point is still in this array and I think it had to removed from this array as well. But I can't figure out a way to do this at the moment.

    So I wonder if there's a simple solution for this problem.

    Thanks,
    Mike

     

Log in to post a comment.