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;
}
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);
}
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);
}
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);
}
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;
}
Aggregations