Hosts
To display a CartesianChart
, use a host. The hosts act as entry points. Either one accepts a CartesianChart
, a CartesianChartModelProducer
, and extra elements and configuration options not handled directly by CartesianChart
s. The hosts are responsible for showing placeholders when no data is available.
- Compose
- Views
The host for Jetpack Compose is the CartesianChartHost
composable function. It’s interacted with via its parameters. A CartesianChart
is created via rememberCartesianChart
and passed in via the chart
parameter:
CartesianChartHost(chart = rememberCartesianChart(...), ...)
The host for the view system is CartesianChartView
, which can be used both programatically and via XML. The API reference doesn’t include information on XML attributes, so all available attributes are listed below. CartesianChartView
automatically creates a CartesianChart
when the layers
attribute is used:
<com.patrykandpatrick.vico.views.chart.CartesianChartView
android:id="@+id/cartesian_chart_view"
app:layers="..."
... />
You can use the chart
property to get a reference to this CartesianChart
or to apply a programmatically instantiated CartesianChart
:
cartesianChartView.chart = CartesianChart(...)
Scroll and zoom
One of the key responsibilities of the hosts is handling scroll and zoom, which you can customize.
- Compose
- Views
CartesianChartHost
accepts instances of VicoScrollState
and VicoZoomState
. Use rememberVicoScrollState
and rememberVicoZoomState
to instantiate these classes:
val scrollState = rememberVicoScrollState(...)
val zoomState = rememberVicoZoomState(...)
CartesianChartHost(scrollState = scrollState, zoomState = zoomState, ...)
CartesianChartView
has XML attributes for toggling scroll and zoom:
<com.patrykandpatrick.vico.views.chart.CartesianChartView
android:id="@+id/cartesian_chart_view"
app:scrollEnabled="..."
app:zoomEnabled="..."
... />
More advanced customization is performed by creating and applying ScrollHandler
and ZoomHandler
instances:
val scrollHandler = ScrollHandler(...)
val zoomHandler = ZoomHandler(...)
cartesianChartView.scrollHandler = scrollHandler
cartesianChartView.zoomHandler = zoomHandler
You can toggle user scroll, set the initial scroll, add and customize automatic scrolling (which occurs on data updates), read the scroll position, and scroll programmatically (with or without an animation). Scroll values and deltas are represented by Scroll
, which is split into Scroll.Absolute
and Scroll.Relative
. Scroll.Absolute.Companion
and Scroll.Relative.Companion
house singletons and factory functions, which let you perform the following scrolls:
- to the start
- to the end
- to a specific position in pixels
- to a specific x value (for which you can specify an alignment)
- by a specific number of pixels
- by a specific number of x units
You can toggle user zoom, read the zoom factor, and set the initial, minimum, and maximum zooms. Zoom factors are represented by Zoom
. Zoom.Companion
houses singletons and factory functions, which let you do the following:
- Make the content fill the viewport.
- Use a static zoom factor.
- Ensure that a specific number of x units is visible.
- Use the minimum of two
Zoom
s. - Use the maximum of two
Zoom
s.
XML attributes
If you’re using Jetpack Compose, skip this section.
CartesianChartView
has the following XML attributes:
*AxisStyle
(AxisStyle
): the style of a specific chart axis*fadingEdgeWidth
(dimension): the width of a specific fading edgeaxisStyle
(AxisStyle
): the style of all chart axescandlestickLayerStyle
(CandlestickCartesianLayerStyle
): theCandlestickCartesianLayer
stylecolumnLayerStyle
(ColumnCartesianLayerStyle
): the style of column chartsfadingEdgeVisibilityInteropolator
(path toTimeInterpolator
implementation): the visibility interpolator for fading edgesfadingEdgeVisibilityThreshold
(dimension): the visibility threshold for the fading edgesfadingEdgeWidth
(dimension): the width of both fading edgeslayers
(|
-separated list ofcandlestick
,column
, andline
): theCartesianLayer
slineLayerStyle
(LineCartesianLayerStyle
): the style of line chartsscalableEndLayerPadding
(dimension): equivalent to thescalableEndPaddingDp
parameter of theCartesianLayerPadding
constructorscalableStartLayerPadding
(dimension): equivalent to thescalableStartPaddingDp
parameter of theCartesianLayerPadding
constructorscrollEnabled
(boolean): whether to enable horizontal scrollingshow*Axis
(boolean): whether to show a specific axisunscalableEndLayerPadding
(dimension): equivalent to theunscalableEndPaddingDp
parameter of theCartesianLayerPadding
constructorunscalableStartLayerPadding
(dimension): equivalent to theunscalableStartPaddingDp
parameter of theCartesianLayerPadding
constructorzoomEnabled
(boolean): whether to enable zooming
Attribute sets
AxisStyle
addExtremeHorizontalAxisLabelPadding
(boolean): equivalent to theaddExtremeLabel
parameter ofAxisItemPlacer.Horizontal.default
guidelineStyle
(LineComponentStyle
): the style of axis guidelineshorizontalAxisItemPlacer
(aligned
orsegmented
): theHorizontalAxis.ItemPlacer
—seeAxisItemPlacer.Horizontal.aligned
andAxisItemPlacer.Horizontal.segmented
horizontalAxisLabelOffset
(integer): the label offset for this axis if it is horizontalhorizontalAxisLabelSpacing
(integer): the label spacing for this axis if it is horizontallabelBackground
(color orShapeStyle
): the background for axis labelslabelRotationDegrees
(floating-point number): the rotation of axis labels in degreeslabelStyle
(TextComponentStyle
): the style of axis labelslineStyle
(LineComponentStyle
): the style of the axis lineshiftExtremeHorizontalAxisLines
(boolean): equivalent to theshiftExtremeLines
parameters ofAxisItemPlacer.Horizontal.aligned
andAxisItemPlacer.Horizontal.segmented
shiftTopVerticalAxisLines
(boolean): equivalent to theshiftTopLines
parameter ofAxisItemPlacer.Vertical.count
showGuidelines
(boolean): whether to display guidelinesshowLine
(boolean): whether to display the axis lineshowTick
(boolean): whether to display ticksshowTitle
(boolean or reference): whether to display the axis titletickLength
(dimension): the length of axis tickstickStyle
(LineComponentStyle
): the style of axis tickstitleStyle
(TextComponentStyle
): the style of the axis titletitle
(string or reference): the axis titleverticalAxisHorizontalLabelPosition
(outside
orinside
): the horizontal position of the labels on this axis if it is verticalverticalAxisItemCount
(integer): equivalent to theitemCount
parameter ofAxisItemPlacer.Vertical.count
verticalAxisVerticalLabelPosition
(top
,center
, orbottom
): the vertical position of the labels on this axis is if it vertical
CandlestickCartesianLayerStyle
absolutelyBearishRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBearishRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBearishRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBullishRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBullishRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBullishRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
bearishCandleStyle
(CandleStyle
): equivalent to thebearish
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
bullishCandleStyle
(CandleStyle
): equivalent to thebullish
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
candleSpacing
(dimension): equivalent toCandlestickCartesianLayer#candleSpacingDp
candleStyle
(absolute
orabsoluteRelative
): whether to useCandlestickCartesianLayer.CandleProvider.absolute
orCandlestickCartesianLayer.CandleProvider.absoluteRelative
minCandleBodyHeight
(dimension): equivalent toCandlestickCartesianLayer#minCandleBodyHeightDp
neutralCandleStyle
(CandleStyle
): equivalent to theneutral
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
scaleCandleWicks
(boolean): equivalent toCandlestickCartesianLayer#scaleCandleWicks
CandleStyle
bodyStyle
(LineComponentStyle
): used for the bodybottomWickStyle
(LineComponentStyle
): used for the bottom wicktopWickStyle
(LineComponentStyle
): used for the top wick
ColumnCartesianLayerStyle
column1Style
(ComponentStyle
): the style for columns whose index in a column collection is 3k (k ∈ N)column2Style
(ComponentStyle
): the style for columns whose index in a column collection is 1 + 3k (k ∈ N)column3Style
(ComponentStyle
): the style for columns whose index in a column collection is 2 + 3k (k ∈ N)columnCollectionSpacing
(dimension): the distance between neighboring column collectionsdataLabelRotationDegrees
(floating-point number): the rotation of data labels in degreesdataLabelStyle
(TextComponentStyle
): the style of data labelsdataLabelVerticalPosition
(top
,center
, orbottom
): the vertical position of each data label relative to the top edge of its respective columngroupedColumnSpacing
(dimension): the distance between neighboring grouped columnsshowDataLabels
(boolean): whether to show data labels
ComponentStyle
color
(color): the background coloroverlayingComponentPadding
(dimension): the padding between this component and the component with which it is overlaidoverlayingComponentStyle
(ComponentStyle
): a component with which to overlay this componentshapeStyle
(ShapeStyle
): the shape stylestrokeColor
(color): the stroke colorstrokeThickness
(dimension): the stroke thickness
TextComponentStyle
android:color
(color): the text colorandroid:ellipsize
(start
,end
,middle
,none
, ormarquee
): the text truncation behaviorandroid:fontFamily
(sans-serif
,sans-serif-*
, or@font
reference): the font familyandroid:fontStyle
(normal
oritalic
): the font styleandroid:padding*
(dimension): the padding for a specific edge or edge pair of the labelandroid:padding
(dimension): the padding for each edge of the labelandroid:textFontWeight
(integer between 100 and 900): the font weightandroid:textSize
(dimension): the text sizebackgroundStyle
(ComponentStyle
): the background for the labelmargin*
(dimension): the size of a specific margin or margin pairmargin
(dimension): the size of all four marginstextAlignment
(normal
,center
, oropposite
): the text alignmenttypeface
(normal
,sans
,serif
, ormonospace
): the typeface
LineCartesianLayerStyle
line1Style
(LineStyle
): the style for lines whose index in the list of lines in a line chart is 3k (k ∈ N)line2Style
(LineStyle
): the style for lines whose index in the list of lines in a line chart is 1 + 3k (k ∈ N)line3Style
(LineStyle
): the style for lines whose index in the list of lines in a line chart is 2 + 3k (k ∈ N)pointSpacing
(dimension): the point spacing
LineComponentStyle
color
(color): the background colorshapeStyle
(ShapeStyle
): the shape of the linestrokeColor
(color): the stroke colorstrokeThickness
(dimension): the stroke thicknessthickness
(dimension): the line thickness
LineStyle
color
(color): the line colorcurvature
(fraction): the strength of the cubic Bézier curve between each key point on the linedataLabelRotationDegrees
(floating-point number): the rotation of data labels in degreesdataLabelStyle
(TextComponentStyle
): the style of data labelsdataLabelVerticalPosition
(top
,center
, orbottom
): the vertical position of each data label relative to its respective point on the linegradientBottomColor
(color): the bottom color of the vertical background gradientgradientTopColor
(color): the top color of the vertical background gradientpointSize
(dimension): the point sizepointStyle
(ComponentStyle
): the style of pointsshowDataLabels
(boolean): whether to show data labelsthickness
(dimension): the thickness of the line
ShapeStyle
bottomEndCornerSize
(dimension or faction): the size of the bottom-end cornerbottomStartCornerSize
(dimension or fraction): the size of the bottom-start cornercornerSize
(dimension or fraction): the corner sizecornerTreatment
(rounded
orcut
): the corner styledashLength
(dimension): the dash length (0 means no dashes)gapLength
(dimension): the dash gap length (0 means no dashes)topEndCornerSize
(dimension or fraction): the size of the top-end cornertopStartCornerSize
(dimension or fraction): the size of the top-start corner