Vico Chart Library
repository·master·Indexed 25 days ago
https://github.com/patrykandpatrick/vicoA powerful and extensible chart library built for Compose Multiplatform. Vico enables developers to create rich data visualizations, including Cartesian and Pie charts, across multiple platforms. It features support for candlestick charts via CandlestickCartesianLayer, customizable axes, and advanced data management using CartesianChartModelProducer. Additionally, it provides specialized integration for Jetpack Glance widgets through CartesianChartImage and PieChartImage, and maintains a views module for legacy view-based Android projects.
What's inside Vico
- Vico is a powerful and extensible chart library designed specifically for Compose Multiplatform. It provides tools for building various types of charts within the Compose ecosystem.
Overview of Vico chart library
masterVico is a powerful and extensible chart library designed specifically for Compose Multiplatform. It allows developers to build various types of charts within Compose-based applications across multiple platforms.Understand CartesianChart ranges
masterThe chart's coordinate ranges are determined by its layers:
- x-range: This is the narrowest range that encompasses the x-ranges of all layers.
- y-range: By default, the chart uses a single shared y-range determined by all layers. However, you can also configure the chart to use two separate y-ranges.
Available CartesianLayer implementations
masterVico provides three built-in implementations of
CartesianLayerfor different chart types:CandlestickCartesianLayer: For candlestick charts.ColumnCartesianLayer: For column/bar charts.LineCartesianLayer: For line charts.
Note on Pie Chart stability
masterPie charts in Vico are currently marked as experimental. While they will remain available in the library, you should expect that breaking changes to their API or implementation are more likely than in stable components.Provide a custom VicoTheme
masterUse
ProvideVicoThemeto supply a customVicoThemeinstance to your chart components. This allows you to override default chart colors globally for a specific part of your UI tree. Functions likerememberColumnCartesianLayerandrememberLineCartesianLayerwill automatically use the colors provided by this theme if no other arguments are passed.ProvideVicoTheme(remember(/* ... */) { VicoTheme(/* ... */) }) { /* ... */ }Format axis labels using categories
masterTo display category names (e.g., "A", "B", "C") instead of numerical indices on an axis, use the
ExtraStoreto pass a list of labels into the chart model. Thexvalue in the formatter acts as the index for this list.- Define an
ExtraStore.Keyfor your list of labels. - In a
cartesianChartModelProducer.runTransaction, add your data series and store the label list inextras. - Implement a
CartesianValueFormatterthat retrieves the label from theextraStoreusing thexvalue as an index.
val labelListKey = ExtraStore.Key<List<String>>() // 1. Provide data and labels in the transaction cartesianChartModelProducer.runTransaction { columnModel { series(data.values) } extras { it[labelListKey] = data.keys.toList() } } // 2. Use the formatter to map index to label val formatter = CartesianValueFormatter { context, x, _ -> context.model.extraStore[labelListKey][x.toInt()] }- Define an
Implement Pie charts
masterVico supports Pie charts through the following components:
PieChart: The main chart structure.PieChartHost: The entry point for hosting a Pie chart in your UI.PieChartModelProducer: Manages the data models for the pie chart.
Use CartesianMarker to highlight chart points
masterUse
CartesianMarkerinstances to highlight specific points on a chart. There are two ways to implement them:- Standard markers: These appear temporarily based on user interaction (e.g., tap or hover). Add them using the
markerparameter inrememberCartesianChart. - Persistent markers: These are shown permanently at specific x-values. Add them using the
persistentMarkersparameter inrememberCartesianChart.
You can use the provided
DefaultCartesianMarkeror implement theCartesianMarkerinterface yourself.- Standard markers: These appear temporarily based on user interaction (e.g., tap or hover). Add them using the
Format axis labels using dates
masterTo display dates on an axis where dates are spaced proportionally, map the dates to their epoch day values (as
Float) for the chart data, and store the original mapping in theExtraStore.- Create an
ExtraStore.Keyfor a map ofFloat(epoch day) toLocalDate. - Convert your dates to epoch days to create the chart series.
- Store the
Map<Float, LocalDate>in the chart'sextras. - In the
CartesianValueFormatter, look up theLocalDateusing thexvalue and format it using aDateTimeFormatter.
val xToDateMapKey = ExtraStore.Key<Map<Float, LocalDate>>() val xToDates = data.keys.associateBy { it.toEpochDay().toFloat() } // 1. Provide data and date mapping in the transaction cartesianChartModelProducer.runTransaction { lineModel { series(xToDates.keys, data.values) } extras { it[xToDateMapKey] = xToDates } } // 2. Use the formatter to map epoch day to formatted date string val dateTimeFormatter = DateTimeFormatter.ofPattern("d MMM") val formatter = CartesianValueFormatter { context, x, _ -> (context.model.extraStore[xToDateMapKey][x] ?: LocalDate.ofEpochDay(x.toLong())) .format(dateTimeFormatter) }- Create an
Integrate Vico with Android platforms
masterVico provides specific integrations for Android environments:
- Jetpack Glance: For building home screen widgets.
- Views: For integrating Vico charts into traditional Android View-based layouts.
Use common Vico styling components
masterCustomize the visual appearance of charts using these common components:
Component: Base building block for chart elements.Fill: Defines how areas within a chart are filled.Shape: Defines the geometry of chart elements.Legend: Displays labels for chart data series.VicoTheme: Provides a centralized way to manage colors, typography, and other theme-related properties.