Series: Provide Data
- 4 minutes to read
Series use Data interfaces to interact with their data sources. This allows you to provide data point parameters from your data sets.
Note that different series types use different data interfaces. A Pie series uses the PieSeriesData interface and a Cartesian series uses interfaces that extend the XYSeriesData interface. Besides, different Cartesian series should use different base interface descendants, because series visualize different value numbers. Additionally, a Cartesian series should use a data interface compatible with an X-axis type; otherwise the chart does not plot the series.
Below is a table of interfaces’ use cases.
Data Interface | Compatible X-Axis | Used in Series | Argument/Label Type | Values |
---|---|---|---|---|
PieSeriesData | None | Pie series | java.lang.String |
Value |
QualitativeSeriesData | QualitativeAxisX | Common Cartesian series | java.lang.String |
Value |
WeightedQualitativeSeriesData | QualitativeAxisX | The Bubble series | java.lang.String |
Value, Weight |
NumericSeriesData | NumericAxisX | Common Cartesian series | double |
Value |
WeightedNumericSeriesData | NumericAxisX | The Bubble series | double |
Value, Weight |
DateTimeSeriesData | DateTimeAxisX | Common Cartesian series | java.util.Date |
Value |
WeightedDateTimeSeriesData | DateTimeAxisX | The Bubble series | java.util.Date |
Value, Weight |
FinancialSeriesData | DateTimeAxisX | Financial series | java.util.Date |
Open, High, Low, Close |
The following sections explain how to perform typical data providing scenarios.
How to: Provide series data
The following code snippet demonstrates how to implement a NumericSeriesData interface:
class GdpData implements NumericSeriesData {
private List<Gdp> gdps = new ArrayList<>();
public GdpData(Gdp... points) { for (Gdp point : points) gdps.add(point); }
@Override
public int getDataCount() { return gdps.size(); }
@Override
public double getArgument(int i) { return gdps.get(i).getYear(); }
@Override
public double getValue(int i) { return gdps.get(i).getProduct(); }
}
class Gdp {
int mYear;
double mProduct;
public Gdp(int year, double product) { mYear = year; mProduct = product; }
public int getYear() { return mYear; }
public double getProduct() { return mProduct; }
}
// ...
Series line = new LineSeries();
line.setData(new GdpData(gdps));
mChart.addSeries(line);
// ...
Symbols required to specify data.
Symbol | Description |
---|---|
PieSeries.getData() | Returns the Pie series‘s data. |
PieSeries.setData(PieSeriesData) | Specifies the Pie series‘s data. |
Series.getData() | Returns the Cartesian series‘s data. |
Series.setData(XYSeriesData) | Specifies the Cartesian series‘s data. |
How to: Update series data on a data source change
Your data class should implement the optional ChangeableSeriesData interface that allows you to send redraw requests to a chart when data changes:
class GdpData implements NumericSeriesData, ChangeableSeriesData {
private List<Gdp> mGdps = new ArrayList<>();
private List<SeriesDataChangedListener> mListeners = new ArrayList<>();
public GdpData(Gdp... points) {
for (Gdp point : points) mGdps.add(point);
}
public void addGdp(Gdp point) {
mGdps.add(point);
// Notify all listeners about data changes.
for (SeriesDataChangedListener listener : mListeners) {
listener.onItemAdded();
}
}
@Override
public int getDataCount() { return mGdps.size(); }
@Override
public double getArgument(int i) { return mGdps.get(i).getYear(); }
@Override
public double getValue(int i) { return mGdps.get(i).getProduct(); }
// The add and remove methods below allow to configure the notification listener set.
@Override
public void addChangedListener(SeriesDataChangedListener dataChangedListener) {
mListeners.add(dataChangedListener);
}
@Override
public void removeChangedListener(SeriesDataChangedListener dataChangedListener) {
mListeners.remove(dataChangedListener);
}
}
// ...
// The chart starts to listen to notifications automatically.
Series line = new LineSeries();
line.setData(new GdpData(gdps));
mChart.addSeries(line);
// ...
The code above uses the following symbols:
Symbol | Description |
---|---|
ChangeableSeriesData | The interface that should be implemented by the data adapter notifying a chart about data changes. |