Search in sources :

Example 1 with ChartSelectionEvent

use of com.vaadin.addon.charts.ChartSelectionEvent in project charts by vaadin.

the class WebXYChartSelection method createScatterChart.

private Chart createScatterChart() {
    final Chart scatterChart = new Chart(ChartType.SCATTER);
    scatterChart.setId("chart");
    scatterChart.getConfiguration().getChart().setZoomType(ZoomType.XY);
    scatterChart.getConfiguration().disableCredits();
    scatterChart.getConfiguration().setTitle("Selections as area ranges");
    scatterChart.getConfiguration().setSubTitle("Drag with mouse to make selections. Click the legend items to toggle visibility.");
    PlotOptionsScatter scatterOptions = new PlotOptionsScatter();
    scatterOptions.setAnimation(false);
    scatterOptions.setPointStart(1);
    DataSeries series = new DataSeries();
    series.setPlotOptions(scatterOptions);
    series.setName("Original");
    Random random = new Random(0);
    for (int i = 0; i < 20; i++) {
        DataSeriesItem dsi = new DataSeriesItem();
        dsi.setY(random.nextInt(10));
        dsi.setX(random.nextInt(10));
        series.add(dsi);
    }
    scatterChart.getConfiguration().addSeries(series);
    scatterChart.addChartSelectionListener(new ChartSelectionListener() {

        @Override
        public void onSelection(ChartSelectionEvent event) {
            double xStart = event.getSelectionStart();
            double xEnd = event.getSelectionEnd();
            double yStart = event.getValueStart();
            double yEnd = event.getValueEnd();
            Number[][] data = new Number[][] { { xStart, yStart, yEnd }, { xEnd, yStart, yEnd } };
            PlotOptionsArearange areaRangePlot = new PlotOptionsArearange();
            areaRangePlot.setFillOpacity(0.1f);
            areaRangePlot.setLineWidth(0);
            RangeSeries selectionSeries = new RangeSeries("Selection", data);
            selectionSeries.setPlotOptions(areaRangePlot);
            scatterChart.getConfiguration().addSeries(selectionSeries);
            scatterChart.drawChart();
            areaRangePlot.setAnimation(false);
        }
    });
    scatterChart.drawChart();
    return scatterChart;
}
Also used : PlotOptionsScatter(com.vaadin.addon.charts.model.PlotOptionsScatter) Random(java.util.Random) RangeSeries(com.vaadin.addon.charts.model.RangeSeries) DataSeries(com.vaadin.addon.charts.model.DataSeries) ChartSelectionEvent(com.vaadin.addon.charts.ChartSelectionEvent) ChartSelectionListener(com.vaadin.addon.charts.ChartSelectionListener) Chart(com.vaadin.addon.charts.Chart) DataSeriesItem(com.vaadin.addon.charts.model.DataSeriesItem) PlotOptionsArearange(com.vaadin.addon.charts.model.PlotOptionsArearange)

Example 2 with ChartSelectionEvent

use of com.vaadin.addon.charts.ChartSelectionEvent in project charts by vaadin.

the class MasterDetailChart method getChart.

@Override
protected Component getChart() {
    VerticalLayout lo = new VerticalLayout();
    lo.setSpacing(false);
    lo.setMargin(false);
    lo.setWidth("100%");
    lo.setHeight("600px");
    final Chart masterChart = getMasterChart();
    final Chart detailChart = getDetailChart();
    masterChart.addChartSelectionListener(new ChartSelectionListener() {

        @Override
        public void onSelection(ChartSelectionEvent event) {
            long start = event.getSelectionStart().longValue();
            long end = event.getSelectionEnd().longValue();
            // set plot band to highlight the selection on the master chart
            PlotBand plotBand1 = new PlotBand();
            PlotBand plotBand2 = new PlotBand();
            plotBand1.setColor(new SolidColor(0, 0, 0, 0.2));
            plotBand2.setColor(new SolidColor(0, 0, 0, 0.2));
            plotBand1.setFrom(Util.toHighchartsTS(DEMO_DATASET_START.atStartOfDay().toInstant(ZoneOffset.UTC)));
            plotBand1.setTo(start);
            plotBand2.setFrom(end);
            plotBand2.setTo(Util.toHighchartsTS(DEMO_DATASET_END.atStartOfDay().toInstant(ZoneOffset.UTC)));
            masterChart.getConfiguration().getxAxis().setPlotBands(plotBand1, plotBand2);
            masterChart.drawChart();
            List<Number> list = MasterDetailChart.this.getPartialList(start, end);
            Configuration configuration = detailChart.getConfiguration();
            configuration.getChart().setAnimation(false);
            ListSeries detailData = (ListSeries) configuration.getSeries().get(0);
            PlotOptionsLine plotOptionsLine = (PlotOptionsLine) detailData.getPlotOptions();
            plotOptionsLine.setPointStart(start);
            detailData.setData(list);
            detailChart.drawChart(configuration);
        }
    });
    lo.addComponent(detailChart);
    lo.addComponent(masterChart);
    return lo;
}
Also used : Configuration(com.vaadin.addon.charts.model.Configuration) ListSeries(com.vaadin.addon.charts.model.ListSeries) PlotOptionsLine(com.vaadin.addon.charts.model.PlotOptionsLine) VerticalLayout(com.vaadin.ui.VerticalLayout) SolidColor(com.vaadin.addon.charts.model.style.SolidColor) ArrayList(java.util.ArrayList) List(java.util.List) ChartSelectionEvent(com.vaadin.addon.charts.ChartSelectionEvent) ChartSelectionListener(com.vaadin.addon.charts.ChartSelectionListener) PlotBand(com.vaadin.addon.charts.model.PlotBand) Chart(com.vaadin.addon.charts.Chart)

Aggregations

Chart (com.vaadin.addon.charts.Chart)2 ChartSelectionEvent (com.vaadin.addon.charts.ChartSelectionEvent)2 ChartSelectionListener (com.vaadin.addon.charts.ChartSelectionListener)2 Configuration (com.vaadin.addon.charts.model.Configuration)1 DataSeries (com.vaadin.addon.charts.model.DataSeries)1 DataSeriesItem (com.vaadin.addon.charts.model.DataSeriesItem)1 ListSeries (com.vaadin.addon.charts.model.ListSeries)1 PlotBand (com.vaadin.addon.charts.model.PlotBand)1 PlotOptionsArearange (com.vaadin.addon.charts.model.PlotOptionsArearange)1 PlotOptionsLine (com.vaadin.addon.charts.model.PlotOptionsLine)1 PlotOptionsScatter (com.vaadin.addon.charts.model.PlotOptionsScatter)1 RangeSeries (com.vaadin.addon.charts.model.RangeSeries)1 SolidColor (com.vaadin.addon.charts.model.style.SolidColor)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Random (java.util.Random)1