Handling data
Chart data in Vico is stored in ChartEntryModel
s, which you can create directly or indirectly. A FloatEntry
holds data about a single entry on a chart (for example, a column). A ChartEntryModel
contains one or more series (ChartEntry
lists).
Static data
For static data, directly create a ChartEntryModel
(for simple charts) or a ComposedChartEntryModel
(for composed charts). A ChartEntryModel
can be created via entryModelOf
. entryOf
creates a ChartEntry
, and entriesOf
creates a series. To create a ComposedChartEntryModel
, combine ChartEntryModel
s via plus
. To link a ChartEntryModel
or ComposedChartEntryModel
to a chart, use the model
parameter of the Chart
composable function, or the setModel
function of BaseChartView
.
Dynamic data
For dynamic data, use ChartEntryModelProducer
(for simple charts) or ComposedChartEntryModelProducer
(for composed charts).
ChartEntryModelProducer
has a public constructor, which optionally takes the initial chart data. Call setEntries
or setEntriesSuspending
to update a ChartEntryModelProducer
’s data.
In ComposedChartEntryModelProducer
s, data updates are handled via Transaction
s. As part of a Transaction
, an initially empty list of data sets is created, and you can update it via the class’s functions (e.g., add
or set
). Notably, you can use populate
to fill the new list of data sets with the current data. Each data set corresponds to a single nested Chart
. To create a ComposedChartEntryModelProducer
, use its build
function, which lets you run an initial transaction. To run a data update, use runTransaction
or runTransactionSuspending
.
Avoid the unnecessary recreation of ChartEntryModelProducer
s and ComposedChartEntryModelProducer
s. We recommend instantiating these classes in ViewModel
s. If you have to create an instance in a composable function, use remember
. Never run data updates by recreating your ChartEntryModelProducer
or ComposedChartEntryModelProducer
.
To link a ChartEntryModelProducer
or ComposedChartEntryModelProducer
to a chart, use the chartModelProducer
parameter of the Chart
composable function, or the entryProducer
field of BaseChartView
.
Extras
Both ChartEntryModelProducer
and ComposedChartEntryModelProducer
let you define extras (auxiliary data). These can later be read via ChartEntryModel#extraStore
. Extras are useful for properties that should change with the chart data and can’t be directly derived from it or should be precalculated. You can use them for lists of axis labels, for example. To add extras, use the updateExtras
parameters of the setEntries
and setEntriesSuspending
functions of ChartEntryModelProducer
, or, for ComposedChartEntryModelProducer
, Transaction#updateExtras
.