gflot icon indicating copy to clipboard operation
gflot copied to clipboard

Setting visibility over a series in a chart that uses side-by-side throws an exception

Open lluppani opened this issue 9 years ago • 0 comments

I don't know how to do this (if there is a right way to do it to avoid the exception) but if I run this and click over a CheckBox to alter some series visibility an exception is thrown in the JS console and the plot doesn't get redraw.

Code (it's essentially the side-by-side sample of the GFlot Examples page but with some checkboxes added to toggle visibility of the series):

/** Sample */
public class GFlot implements EntryPoint {

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {
        final PlotModel model = new PlotModel();
        final PlotOptions plotOptions = PlotOptions.create();

        // create the plot
        final SimplePlot plot = new SimplePlot( model, plotOptions );

        final GlobalSeriesOptions globalSeriesOptions = GlobalSeriesOptions.create();
        globalSeriesOptions.setBarsSeriesOptions( BarSeriesOptions.create().setShow( true ).setBarWidth( 0.2 ).setLineWidth( 1 ) );
        plotOptions.addXAxisOptions( AxisOptions.create().setTicks( new AbstractAxisOptions.TickGenerator()
        {
            @Override
            public JsArray<Tick> generate( Axis axis )
            {
                final JsArray<Tick> array = JsArray.createArray().cast();
                for ( int i = 0; i < 10; i++ )
                {
                    array.push( Tick.of( i, "B" + i ) );
                }
                return array;
            }
        } ) );

        plotOptions.setGlobalSeriesOptions( globalSeriesOptions );

        plotOptions.setLegendOptions( LegendOptions.create().setMargin( -120, 0 ) );

        // create a series
        final List<SeriesHandler> series = new ArrayList<SeriesHandler>();
        final int nbSeries = 4;
        for ( int i = 0; i < nbSeries; i++ )
        {
            final Series s = Series.of("Random series " + (i + 1)).setBarsSeriesOptions(BarSeriesOptions.create().setOrder(i));
            series.add(model.addSeries(s));
            final CheckBox seriesCb = new CheckBox();
            seriesCb.setText("Series " + i);
            final int index = i;
            seriesCb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
                @Override
                public void onValueChange(ValueChangeEvent<Boolean> event) {
                    series.get(index).setVisible(!event.getValue());
                    plot.redraw();
                }
            });

            RootPanel.get("cbs").add(seriesCb);
        }

        final Markings markings = Markings.create();

        // add data
        for ( int i = 0; i < 10; i++ )
        {
            for ( final SeriesHandler serie : series )
            {
                serie.add( DataPoint.of(i, Random.nextInt(50)) );
            }
            if ( i % 2 != 0 )
            {
                markings.addMarking( Marking.create().setX( Range.of(i - 0.5, i + 0.5) ).setColor( "rgba(153, 153, 153, 0.3)" ) );
            }
        }
        plotOptions.setGridOptions(GridOptions.create().setMarkings(markings));

        RootPanel.get("slot1").add(plot);

    }

}
Exception:
Uncaught com.google.gwt.event.shared.UmbrellaException: Exception caught: Exception caught: (TypeError) : Cannot read property '0' of undefined
  fireEvent_0_g$    
  fireEvent_4_g$    
  fireNativeEvent_1_g$  
  onBrowserEvent_0_g$   
  dispatchEventImpl_0_g$    
  dispatchEvent_4_g$    
  dispatchEvent_6_g$    
  apply_0_g$    
  entry0_0_g$   
  (anonymous function)

It seems to be related with the fact that when I set a series to visible false, in the code what is done is to create an empty series as the new data and side-by-side charts doesn't handle that well.

lluppani avatar Sep 08 '15 16:09 lluppani