1.4. Connecting Data
There are three ways how you can pass data to a chart: through the DataProvider interface, using the datasource mechanism, or the simplified data binding API, which allows adding the data directly using the addData() method and convenient MapDataItem constructors for those charts that are not bound to a datasource.
- DataProvider:
-
The
DataProviderinterface has a standard implementation:ListDataProviderclass. It contains a list ofDataIteminstances from which the data for the chart will be taken. There are several standard implementations ofDataIteminterface:-
EntityDataItemtakes an instance of any entity. -
MapDataItemis a set of key-value pairs. -
SimpleDataItemtakes an instance of anypublicclass.
An instance of
DataProvideris passed to thesetDataProvider()method of chart configuration. This approach to providing chart data is the most universal, but it requires creating instances ofDataProviderandDataItemin a screen controller. -
- Datasource:
-
A
CollectionDatasourcetype datasource can be assigned to aChartcomponent by invoking thesetDatasource()method. This approach requires an entity that will represent chart data. It may be convenient when such entity already exists in the application data model and also when chart data should be displayed as a table.Example of Working with Charts illustrates all three approaches to providing chart data.
Entity properties or the values contained in an instance of
DataProviderwhich are used for display purposes are defined in the chart attributes. The set of chart attributes may differ for different chart types. For example, for thechart:pieChartcomponent, you should define thevalueFieldandtitleFieldattributes. The following types are allowed for attribute values:Integer,Long,Double,String,Boolean,Date.Dynamic addition of data to an existing chart is supported for both the datasource and the data provider mechanisms.
- chart:data element:
-
This option is useful for quick prototyping of charts. The
chart:dataelement and its nesteditemelements enable you to set key-value pairs of data directly in the XML descriptor of the chart, for example:<chart:pieChart id="pieChart" titleField="key" valueField="value"> <chart:data> <chart:item> <chart:property name="key" value="piece of apple pie"/> <chart:property name="value" value="70" type="int"/> </chart:item> <chart:item> <chart:property name="key" value="piece of blueberry pie"/> <chart:property name="value" value="20" type="int"/> </chart:item> <chart:item> <chart:property name="key" value="piece of cherry pie"/> <chart:property name="value" value="10" type="int"/> </chart:item> </chart:data> </chart:pieChart>