Search in sources :

Example 1 with OPEN_PROPERTY

use of com.vaadin.addon.charts.model.DataProviderSeries.OPEN_PROPERTY 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)

Aggregations

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 DataProviderSeries (com.vaadin.addon.charts.model.DataProviderSeries)1 CLOSE_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.CLOSE_PROPERTY)1 HIGH_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.HIGH_PROPERTY)1 LOW_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.LOW_PROPERTY)1 OPEN_PROPERTY (com.vaadin.addon.charts.model.DataProviderSeries.OPEN_PROPERTY)1 PlotOptionsSeries (com.vaadin.addon.charts.model.PlotOptionsSeries)1 IOException (java.io.IOException)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1