Grafico con capacità di aggiunta di punti

Abbiamo già visto la configurazione utilizzata per disegnare un grafico nel capitolo Sintassi della configurazione di Highcharts .

Di seguito viene fornito un esempio di grafico con capacità di aggiunta di punti.

Configurazioni

Vediamo ora le configurazioni / passaggi aggiuntivi effettuati.

chart.events

Aggiungi un metodo di clic al file chart.eventproprietà. Questo metodo aggiunge alla serie un nuovo punto utilizzando le coordinate x, y dell'area cliccata nel grafico.

chart.setClickEventHandler(new ChartClickEventHandler() {
   @Override
   public boolean onClick(ChartClickEvent chartClickEvent) {
      series.addPoint(chartClickEvent.getXAxisValue(), chartClickEvent.getYAxisValue());  
      return true;  
   }
});

Esempio

HelloWorld.java

package com.tutorialspoint.client;

import java.util.Date;

import org.moxieapps.gwt.highcharts.client.Chart;
import org.moxieapps.gwt.highcharts.client.Credits;
import org.moxieapps.gwt.highcharts.client.Exporting;
import org.moxieapps.gwt.highcharts.client.Legend;
import org.moxieapps.gwt.highcharts.client.Series;
import org.moxieapps.gwt.highcharts.client.ToolTip;
import org.moxieapps.gwt.highcharts.client.ToolTipData;
import org.moxieapps.gwt.highcharts.client.ToolTipFormatter;
import org.moxieapps.gwt.highcharts.client.YAxis;

import org.moxieapps.gwt.highcharts.client.events.ChartClickEvent;
import org.moxieapps.gwt.highcharts.client.events.ChartClickEventHandler;
import org.moxieapps.gwt.highcharts.client.events.PointClickEvent;
import org.moxieapps.gwt.highcharts.client.events.PointClickEventHandler;
import org.moxieapps.gwt.highcharts.client.Series.Type;

import org.moxieapps.gwt.highcharts.client.labels.DataLabels;
import org.moxieapps.gwt.highcharts.client.plotOptions.BarPlotOptions;
import org.moxieapps.gwt.highcharts.client.plotOptions.SeriesPlotOptions;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      final Chart chart = new Chart()  
         .setType(Series.Type.SCATTER)  
         .setMargin(70, 50, 60, 80)  
         .setChartTitleText("User supplied data")  
         .setChartSubtitleText("Click the plot area to add a point. Click a point to remove it.")  
         .setExporting(new Exporting()  
            .setEnabled(false)  
         )  
         .setLegend(new Legend()  
            .setEnabled(false)  
         );  

         chart.getXAxis()  
            .setMinPadding(0.2)  
            .setMaxPadding(0.2)  
            .setMaxZoom(60);  

         final YAxis yAxis = chart.getYAxis();  
         yAxis.setAxisTitleText("Value")  
            .setMinPadding(0.2)  
            .setMaxPadding(0.2)  
            .setMaxZoom(60)  
            .setPlotLines(yAxis.createPlotLine()  
               .setValue(0)  
               .setWidth(1)  
               .setColor("#808080")  
            );  

         final Series series = chart.createSeries()  
            .setPoints(new Number[][] {{20, 20}, {80, 80}});  
         chart.addSeries(series);  

         chart.setClickEventHandler(new ChartClickEventHandler() {
            @Override
            public boolean onClick(ChartClickEvent chartClickEvent) {
               series.addPoint(chartClickEvent.getXAxisValue(), chartClickEvent.getYAxisValue());  
               return true;  
            }
         });

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

Risultato

Verifica il risultato.