Search in sources :

Example 1 with DataProviderSeries

use of com.vaadin.addon.charts.model.DataProviderSeries in project charts by vaadin.

the class DataProviderSeriesBeanSerializer method createDataArray.

private ArrayNode createDataArray(DataProviderSeries<?> chartDataProvider) {
    ArrayNode data = JsonNodeFactory.instance.arrayNode();
    Set<String> attributes = chartDataProvider.getChartAttributes();
    checkRequiredProperties(attributes);
    Mode mode = inferSerializationMode(attributes);
    for (final Map<String, Optional<Object>> chartAttributeToValue : chartDataProvider.getValues()) {
        Optional<Object> xValue = chartAttributeToValue.getOrDefault(xAttribute, Optional.empty());
        Optional<Object> yValue = chartAttributeToValue.getOrDefault(yAttribute, Optional.empty());
        Optional<Object> oValue = chartAttributeToValue.getOrDefault(OPEN_PROPERTY, Optional.empty());
        Optional<Object> lValue = chartAttributeToValue.getOrDefault(LOW_PROPERTY, Optional.empty());
        Optional<Object> hValue = chartAttributeToValue.getOrDefault(HIGH_PROPERTY, Optional.empty());
        Optional<Object> cValue = chartAttributeToValue.getOrDefault(CLOSE_PROPERTY, Optional.empty());
        switch(mode) {
            case ONLY_Y:
                final Optional<Object> value = chartAttributeToValue.get(yAttribute);
                addValue(data, value);
                break;
            case XY:
                if (xValue.isPresent() && yValue.isPresent()) {
                    final ArrayNode entryArray = JsonNodeFactory.instance.arrayNode();
                    data.add(entryArray);
                    addValue(entryArray, xValue);
                    addValue(entryArray, yValue);
                } else {
                    data.addNull();
                }
                break;
            case XLH:
                if (xValue.isPresent() && lValue.isPresent() && hValue.isPresent()) {
                    final ArrayNode entryArray = JsonNodeFactory.instance.arrayNode();
                    data.add(entryArray);
                    addValue(entryArray, xValue);
                    addValue(entryArray, lValue);
                    addValue(entryArray, hValue);
                } else {
                    data.addNull();
                }
                break;
            case XOHLC:
                if (xValue.isPresent() && oValue.isPresent() && hValue.isPresent() && lValue.isPresent() && cValue.isPresent()) {
                    final ArrayNode entryArray = JsonNodeFactory.instance.arrayNode();
                    data.add(entryArray);
                    addValue(entryArray, xValue);
                    addValue(entryArray, oValue);
                    addValue(entryArray, hValue);
                    addValue(entryArray, lValue);
                    addValue(entryArray, cValue);
                } else {
                    data.addNull();
                }
                break;
            default:
                // render as json object
                final ObjectNode entryObject = JsonNodeFactory.instance.objectNode();
                xValue.ifPresent(o -> addNamedValue(entryObject, xAttribute, xValue));
                yValue.ifPresent(o -> addNamedValue(entryObject, yAttribute, yValue));
                chartAttributeToValue.entrySet().stream().filter(e -> !e.getKey().equals(xAttribute)).filter(e -> !e.getKey().equals(yAttribute)).forEach(e -> addNamedValue(entryObject, e.getKey(), e.getValue()));
                data.add(entryObject);
                break;
        }
    }
    return data;
}
Also used : HIGH_PROPERTY(com.vaadin.addon.charts.model.DataProviderSeries.HIGH_PROPERTY) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) DataProviderSeries(com.vaadin.addon.charts.model.DataProviderSeries) OPEN_PROPERTY(com.vaadin.addon.charts.model.DataProviderSeries.OPEN_PROPERTY) Set(java.util.Set) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ValueNode(com.fasterxml.jackson.databind.node.ValueNode) LOW_PROPERTY(com.vaadin.addon.charts.model.DataProviderSeries.LOW_PROPERTY) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) PlotOptionsSeries(com.vaadin.addon.charts.model.PlotOptionsSeries) CLOSE_PROPERTY(com.vaadin.addon.charts.model.DataProviderSeries.CLOSE_PROPERTY) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Map(java.util.Map) Optional(java.util.Optional) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) Optional(java.util.Optional) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 2 with DataProviderSeries

use of com.vaadin.addon.charts.model.DataProviderSeries in project charts by vaadin.

the class ChartDataSeriesJSONSerializationTest method serialize_ContainerWithNameAndStack_NameAndStackSerialized.

@Test
public void serialize_ContainerWithNameAndStack_NameAndStackSerialized() {
    final Pair<ListDataProvider<TestItem>, DataProviderSeries<TestItem>> pair = createTuple();
    final DataProviderSeries<TestItem> dataProviderSeries = pair.getT2();
    dataProviderSeries.setY(TestItem::getY);
    dataProviderSeries.setName("foo");
    dataProviderSeries.setStack("bar");
    Configuration config = new Configuration();
    config.addSeries(dataProviderSeries);
    String actual = toJSON(dataProviderSeries);
    String expected = "{\"name\":\"foo\",\"stack\":\"bar\",\"data\":[]}";
    assertEquals(expected, actual);
}
Also used : ListDataProvider(com.vaadin.data.provider.ListDataProvider) DataProviderSeries(com.vaadin.addon.charts.model.DataProviderSeries) Configuration(com.vaadin.addon.charts.model.Configuration) Test(org.junit.Test)

Example 3 with DataProviderSeries

use of com.vaadin.addon.charts.model.DataProviderSeries in project charts by vaadin.

the class ChartTypes method chartTypesOhlcSnippet2.

public void chartTypesOhlcSnippet2() {
    Chart chart = new Chart(ChartType.OHLC);
    Configuration configuration = chart.getConfiguration();
    // Create a DataProvider filled with stock price data
    DataProvider<OhlcData, ?> dataProvider = initDataProvider();
    // Wrap the container in a data series
    DataProviderSeries<OhlcData> dataSeries = new DataProviderSeries<>(dataProvider);
    dataSeries.setX(OhlcData::getDate);
    dataSeries.setLow(OhlcData::getLow);
    dataSeries.setHigh(OhlcData::getHigh);
    dataSeries.setClose(OhlcData::getClose);
    dataSeries.setOpen(OhlcData::getOpen);
    PlotOptionsOhlc plotOptionsOhlc = new PlotOptionsOhlc();
    plotOptionsOhlc.setTurboThreshold(0);
    dataSeries.setPlotOptions(plotOptionsOhlc);
    configuration.setSeries(dataSeries);
}
Also used : DataProviderSeries(com.vaadin.addon.charts.model.DataProviderSeries) Configuration(com.vaadin.addon.charts.model.Configuration) PlotOptionsOhlc(com.vaadin.addon.charts.model.PlotOptionsOhlc) Chart(com.vaadin.addon.charts.Chart)

Example 4 with DataProviderSeries

use of com.vaadin.addon.charts.model.DataProviderSeries in project charts by vaadin.

the class GettingStarted method addColumnsSnippet1.

public void addColumnsSnippet1(List<WeatherInfo> weatherInfo) {
    Chart chart = new Chart();
    Configuration conf = chart.getConfiguration();
    ListDataProvider<WeatherInfo> dataProvider = new ListDataProvider<>(weatherInfo);
    DataProviderSeries<WeatherInfo> humidity = new DataProviderSeries<>(dataProvider);
    humidity.setName("Humidity");
    humidity.setX(WeatherInfo::getInstant);
    humidity.setY(WeatherInfo::getMeanHumidity);
    humidity.setPlotOptions(new PlotOptionsColumn());
    conf.addSeries(humidity);
}
Also used : ListDataProvider(com.vaadin.data.provider.ListDataProvider) DataProviderSeries(com.vaadin.addon.charts.model.DataProviderSeries) Configuration(com.vaadin.addon.charts.model.Configuration) PlotOptionsColumn(com.vaadin.addon.charts.model.PlotOptionsColumn) Chart(com.vaadin.addon.charts.Chart)

Example 5 with DataProviderSeries

use of com.vaadin.addon.charts.model.DataProviderSeries in project charts by vaadin.

the class BeansFromDataProvider method getChart.

@Override
protected Component getChart() {
    ListDataProvider<ClaimsReportItem> dp = DataProvider.ofCollection(getMockData());
    // Create ChartDataSeries
    DataProviderSeries<ClaimsReportItem> series = new DataProviderSeries<>(dp);
    series.setName("Claims");
    series.setPlotOptions(new PlotOptionsColumn());
    series.setY(ClaimsReportItem::getAmount);
    series.setPointName(ClaimsReportItem::getType);
    // Create chart & configuration
    Chart chart = new Chart();
    Configuration configuration = chart.getConfiguration();
    configuration.getTitle().setText("Claim Processing");
    // Create Y Axis
    YAxis y = new YAxis();
    y.setTitle("Amount");
    configuration.addyAxis(y);
    // use category names from beans on x axis instead of index
    configuration.getxAxis().setType(AxisType.CATEGORY);
    configuration.setSeries(series);
    chart.setSizeFull();
    return chart;
}
Also used : DataProviderSeries(com.vaadin.addon.charts.model.DataProviderSeries) Configuration(com.vaadin.addon.charts.model.Configuration) PlotOptionsColumn(com.vaadin.addon.charts.model.PlotOptionsColumn) Chart(com.vaadin.addon.charts.Chart) YAxis(com.vaadin.addon.charts.model.YAxis)

Aggregations

DataProviderSeries (com.vaadin.addon.charts.model.DataProviderSeries)17 Configuration (com.vaadin.addon.charts.model.Configuration)12 ListDataProvider (com.vaadin.data.provider.ListDataProvider)10 Chart (com.vaadin.addon.charts.Chart)8 Test (org.junit.Test)6 PlotOptionsColumn (com.vaadin.addon.charts.model.PlotOptionsColumn)4 YAxis (com.vaadin.addon.charts.model.YAxis)4 ArrayList (java.util.ArrayList)3 PlotOptionsSeries (com.vaadin.addon.charts.model.PlotOptionsSeries)2 XAxis (com.vaadin.addon.charts.model.XAxis)2 LocalDateTime (java.time.LocalDateTime)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ValueNode (com.fasterxml.jackson.databind.node.ValueNode)1 AxisTitle (com.vaadin.addon.charts.model.AxisTitle)1 CLOSE_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.CLOSE_PROPERTY)1 HIGH_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.HIGH_PROPERTY)1