Menu

Getting series / points by name from the Chart object

Jay
2012-06-19
2012-07-05
  • Jay

    Jay - 2012-06-19

    In the highcharts API, you can set an ID on a chart series or points, and then retrieve it later with the chart.get(id) method. In GWT Highcharts the framework sets the series id. You can set Point names but you can't get a point by name unless you loop through the points in a series. Is there any plan to replicate the functionality more closely to the Highcharts native chart.get(id) method? It seems like replicating this is clunky in GWT Highcharts right now, unless I'm missing something.

     
  • Shawn Quinn

    Shawn Quinn - 2012-07-05

    GWT Highcharts does currently support retrieving the series by id which is useful in event handlers that provide the series id instead of the native Series instance. E.g.

        chart.setSeriesPlotOptions(new SeriesPlotOptions()
            .setPointClickEventHandler(new PointClickEventHandler() {
                public boolean onClick(PointClickEvent pointClickEvent) {
                    Series series = chart.getSeries(pointClickEvent.getSeriesId());
                    if(series.getPoints().length > 1) {
                        pointClickEvent.getPoint().remove();
                    }
                    return true;
                }
            })
        );
    

    For most other use cases though GWT Highcharts handles the native referencing for you (such that you don't need the id) in that you can simply keep a reference to the Series instance around and just use it naturally later. E.g.

        final Series series = chart.createSeries()
            .setPoints(new Number[][] {{20, 20}, {80, 80}});
        chart.addSeries(series);
    
        chart.setClickEventHandler(new ChartClickEventHandler() {
            public boolean onClick(ChartClickEvent chartClickEvent) {
                // Just reference the series instance that you created previously
                series.addPoint(chartClickEvent.getXAxisValue(), chartClickEvent.getYAxisValue());
                return true;
            }
        });
    

    Regarding points: adding support for retrieving a Java wrapped "Point" instance to GWT Highcharts hasn't been pondered, but it's certainly logical. I haven't tested this, but you should be able do that on your own with a method like so though:

        private static native JavaScriptObject getNativePoint(JavaScriptObject chart, String pointId) /*-{
            return chart.get(pointId);
        }-*/; 
    

    Which could then be called like so:

        Point p = new Point(getNativePoint(chart.getNativeChart(), "pointXYZ"));
    

    Hope that helps, but just let us know if you're trying to accommodate a different scenario.

     

Log in to post a comment.