com.vaadin.flow.component.charts.model.DataProviderSeries Maven / Gradle / Ivy
/**
* Copyright 2000-2024 Vaadin Ltd.
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See {@literal } for the full
* license.
*/
package com.vaadin.flow.component.charts.model;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vaadin.flow.data.provider.DataProvider;
import com.vaadin.flow.data.provider.DataProviderListener;
import com.vaadin.flow.data.provider.Query;
import com.vaadin.flow.function.SerializableFunction;
import com.vaadin.flow.shared.Registration;
/**
* A series which is based on data from a DataProvider.
*
* You must use {@link #setY(SerializableFunction)} to define which part of the
* data bean to use as y
values.
*
* Note that even if you use a lazy loading {@link DataProvider}, this series
* will work in an eager fashion and load all the data from the provider at
* once.
*/
public class DataProviderSeries extends AbstractSeries {
@JsonIgnore
private final DataProvider dataProvider;
public static final String X_ATTRIBUTE = "x";
public static final String Y_ATTRIBUTE = "y";
private static final String NAME_ATTRIBUTE = "name";
public static final String HIGH_PROPERTY = "high";
public static final String LOW_PROPERTY = "low";
public static final String OPEN_PROPERTY = "open";
public static final String CLOSE_PROPERTY = "close";
@JsonIgnore
private final Map> chartAttributeToCallback;
@JsonIgnore
private boolean automaticChartUpdateEnabled = true;
@JsonIgnore
private Registration dataProviderRegistration;
@JsonIgnore
private DataProviderListener listener = (DataProviderListener) event -> {
updateSeries();
};
/**
* Creates a new series using data from the given data provider.
*
* Many chart types such as {@link ChartType#BAR}, {@link ChartType#LINE},
* {@link ChartType#AREA} etc use {@code y} values to define the data points
* to show in the chart. For these chart types you should use either
* {@link #DataProviderSeries(DataProvider, SerializableFunction)} or
* {@link #setY(SerializableFunction)} to define the function (lambda) which
* extracts the values from the bean in the provider.
*
* Other chart types such as {@link ChartType#ERRORBAR} do not require
* {@code y} values but instead {@code high} and {@code low} values.
* Functions for extracting these are set using
* {@link #setHigh(SerializableFunction)} and
* {@link #setLow(SerializableFunction)} respectively.
*
* @param dataProvider
* the data provider which contains the data
*/
public DataProviderSeries(DataProvider dataProvider) {
this.dataProvider = dataProvider;
chartAttributeToCallback = new HashMap<>();
dataProviderRegistration = dataProvider
.addDataProviderListener(listener);
}
/**
* Creates a new series using data from the given data provider and y
* values.
*
* @param dataProvider
* the data provider which contains the data
* @param callBack
* the function which retrieves the y values
*/
public DataProviderSeries(DataProvider dataProvider,
SerializableFunction callBack) {
this(dataProvider);
setY(callBack);
}
/**
* Sets the function used for retrieving the value for the given property
* name from the given data provider.
*
* @param propertyName
* the property name
* @param callBack
* the function which retrieves the value for the property
*/
public void setProperty(String propertyName,
SerializableFunction callBack) {
chartAttributeToCallback.put(propertyName, callBack);
}
/**
* Sets the function used for retrieving x
values from the bean
* provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setX(SerializableFunction callBack) {
setProperty(X_ATTRIBUTE, callBack);
}
/**
* Sets the function used for retrieving y
values from the bean
* provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setY(SerializableFunction callBack) {
setProperty(Y_ATTRIBUTE, callBack);
}
/**
* Sets the function used for retrieving name
values from the
* bean provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setPointName(SerializableFunction callBack) {
setProperty(NAME_ATTRIBUTE, callBack);
}
/**
* Sets the function used for retrieving low
values from the
* bean provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setLow(SerializableFunction callBack) {
setProperty(LOW_PROPERTY, callBack);
}
/**
* Sets the function used for retrieving high
values from the
* bean provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setHigh(SerializableFunction callBack) {
setProperty(HIGH_PROPERTY, callBack);
}
/**
* Sets the function used for retrieving open
values from the
* bean provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setOpen(SerializableFunction callBack) {
setProperty(OPEN_PROPERTY, callBack);
}
/**
* Sets the function used for retrieving close
values from the
* bean provided by the data provider.
*
* How exactly the values are used depends on the used chart type.
*
* @param callBack
* the function which retrieves the values
*/
public void setClose(SerializableFunction callBack) {
setProperty(CLOSE_PROPERTY, callBack);
}
/**
* Returns the underlying data provider.
*
* @return the underlying data provider.
*/
public DataProvider getDataProvider() {
return dataProvider;
}
/**
* Returns a list mappings between chart attributes(keys) and values. For
* example: x->1, x->2, y->2, y->3 for linear chart
*
* @return
*/
public List