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:chartHorizontalScrollingEnabled="..."
app:chartZoomEnabled="..."
... />
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
(Axis
): the style of all chart axes*AxisStyle
(Axis
): the style of a specific chart axiscandlestickLayerStyle
(CandlestickLayerStyle
): theCandlestickCartesianLayer
stylecolumnLayerStyle
(ColumnLayerStyle
): the style of column chartslineLayerStyle
(LineLayerStyle
): the style of line chartsshow*Axis
(boolean): whether to show a specific axischartHorizontalScrollingEnabled
(boolean): whether to enable horizontal scrollingchartZoomEnabled
(boolean): whether to enable zoomingfadingEdgeWidth
(dimension): the width of both fading edges*fadingEdgeWidth
(dimension): the width of a specific fading edgefadingEdgeVisibilityThreshold
(dimension): the visibility threshold for the fading edgesfadingEdgeVisibilityInteropolator
(path toTimeInterpolator
implementation): the visibility interpolator for fading edgeshorizontalLayout
(segmented
orfullWidth
): the chart’sHorizontalLayout
layers
(|
-separated list ofcandlestick
,column
, andline
): theCartesianLayer
sscalableStartContentPadding
(dimension): equivalent toHorizontalLayout.FullWidth#scalableStartPaddingDp
scalableEndContentPadding
(dimension): equivalent toHorizontalLayout.FullWidth#scalableEndPaddingDp
unscalableStartContentPadding
(dimension): equivalent toHorizontalLayout.FullWidth#unscalableStartPaddingDp
unscalableEndContentPadding
(dimension): equivalent toHorizontalLayout.FullWidth#unscalableEndPaddingDp
Attribute sets
Axis
axisLineStyle
(LineComponentStyle
): the style of the axis lineaxisGuidelineStyle
(LineComponentStyle
): the style of axis guidelinesaxisTickStyle
(LineComponentStyle
): the style of axis ticksaxisLabelStyle
(TextComponentStyle
): the style of axis labelsaxisTickLength
(dimension): the length of axis ticksaxisLabelBackground
(color orShape
): the background for axis labelsverticalAxisHorizontalLabelPosition
(outside
orinside
): the horizontal position of the labels on this axis if it is verticalverticalAxisVerticalLabelPosition
(top
,center
, orbottom
): the vertical position of the labels on this axis is if it verticallabelRotationDegrees
(floating-point number): the rotation of axis labels in degreestitle
(string or reference): the axis titleshowTitle
(boolean or reference): whether to display the axis titletitleStyle
(TextComponentStyle
): the style of the axis titlehorizontalAxisLabelSpacing
(integer): the label spacing for this axis if it is horizontalhorizontalAxisLabelOffset
(integer): the label offset for this axis if it is horizontalshowAxisLine
(boolean): whether to display the axis lineshowGuideline
(boolean): whether to display guidelinesshowTick
(boolean): whether to display ticksshiftExtremeHorizontalAxisTicks
(boolean): equivalent to theshiftExtremeTicks
parameter ofAxisItemPlacer.Horizontal.default
addExtremeHorizontalAxisLabelPadding
(boolean): equivalent to theaddExtremeLabel
parameter ofAxisItemPlacer.Horizontal.default
shiftTopVerticalAxisLines
(boolean): equivalent to theshiftTopLines
parameter ofAxisItemPlacer.Vertical.count
maxVerticalAxisItemCount
(integer): equivalent to themaxItemCount
parameter ofAxisItemPlacer.Vertical.count
CandleStyle
bodyStyle
(LineComponentStyle
): used for the bodytopWickStyle
(LineComponentStyle
): used for the top wickbottomWickStyle
(LineComponentStyle
): used for the bottom wick
ComponentStyle
color
(color): the background colorshapeStyle
(Shape
): the shape stylestrokeColor
(color): the stroke colorstrokeWidth
(dimension): the stroke widthoverlayingComponentStyle
(ComponentStyle
): a component with which to overlay this componentoverlayingComponentPadding
(dimension): the padding between this component and the component with which it is overlaid
CandlestickLayerStyle
candleStyle
(absolute
orabsoluteRelative
): whether to useCandlestickCartesianLayer.CandleProvider.absolute
orCandlestickCartesianLayer.CandleProvider.absoluteRelative
bullishCandleStyle
(CandleStyle
): equivalent to thebullish
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
neutralCandleStyle
(CandleStyle
): equivalent to theneutral
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
bearishCandleStyle
(CandleStyle
): equivalent to thebearish
parameter ofCandlestickCartesianLayer.CandleProvider.absolute
absolutelyBullishRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBullishRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBullishRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBullishRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyNeutralRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyNeutralRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBearishRelativelyBullishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyBullish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBearishRelativelyNeutralCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyNeutral
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
absolutelyBearishRelativelyBearishCandleStyle
(CandleStyle
): equivalent to theabsolutelyBearishRelativelyBearish
parameter ofCandlestickCartesianLayer.CandleProvider.absoluteRelative
minCandleBodyHeight
(dimension): equivalent toCandlestickCartesianLayer#minCandleBodyHeightDp
candleSpacing
(dimension): equivalent toCandlestickCartesianLayer#candleSpacingDp
scaleCandleWicks
(boolean): equivalent toCandlestickCartesianLayer#scaleCandleWicks
ColumnLayerStyle
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)columnOuterSpacing
(dimension): the distance between neighboring column collectionscolumnInnerSpacing
(dimension): the distance between neighboring grouped columnsshowDataLabels
(boolean): whether to show data labelsdataLabelStyle
(TextComponentStyle
): the style of data labelsdataLabelVerticalPosition
(top
,center
, orbottom
): the vertical position of each data label relative to the top edge of its respective columndataLabelRotationDegrees
(floating-point number): the rotation of data labels in degrees
TextComponentStyle
labelColor
(color): the text colorbackgroundStyle
(ComponentStyle
): the background for the labelandroid:padding
(dimension): the padding for each edge of the labelandroid:padding*
(dimension): the padding for a specific edge or edge pair of the labelandroid:fontFamily
(sans-serif
,sans-serif-*
, or@font
reference): the font familyandroid:textFontWeight
(integer between 100 and 900): the font weightandroid:fontStyle
(normal
oritalic
): the font styleandroid:textSize
(dimension): the text sizeandroid:ellipsize
(start
,end
,middle
,none
, ormarquee
): the text truncation behaviortypeface
(normal
,sans
,serif
, ormonospace
): the typefacemargin
(dimension): the size of all four marginsmargin*
(dimension): the size of a specific margin or margin pairtextAlignment
(normal
,center
, oropposite
): the text alignment
LineComponentStyle
color
(color): the background colorthickness
(dimension): the line thicknessshapeStyle
(Shape
): the shape of the linestrokeColor
(color): the stroke colorstrokeWidth
(dimension): the stroke width
LineLayerStyle
line1Spec
(LineSpec
): the style for lines whose index in the list of lines in a line chart is 3k (k ∈ N)line2Spec
(LineSpec
): the style for lines whose index in the list of lines in a line chart is 1 + 3k (k ∈ N)line3Spec
(LineSpec
): the style for lines whose index in the list of lines in a line chart is 2 + 3k (k ∈ N)spacing
(dimension): the point spacing
LineSpec
color
(color): the line colorgradientTopColor
(color): the top color of the vertical background gradientgradientBottomColor
(color): the bottom color of the vertical background gradientpointSize
(dimension): the point sizelineThickness
(dimension): the thickness of the linepointStyle
(ComponentStyle
): the style of pointscubicStrength
(fraction): the strength of the cubic bezier curve between each key point on the lineshowDataLabels
(boolean): whether to show data labelsdataLabelStyle
(TextComponentStyle
): the style of data labelsdataLabelVerticalPosition
(top
,center
, orbottom
): the vertical position of each data label relative to its respective point on the linedataLabelRotationDegrees
(floating-point number): the rotation of data labels in degrees
Shape
cornerSize
(dimension or fraction): the corner sizecornerTreatment
(rounded
orcut
): the corner styletopStartCornerSize
(dimension or fraction): the size of the top-start cornertopEndCornerSize
(dimension or fraction): the size of the top-end cornerbottomStartCornerSize
(dimension or fraction): the size of the bottom-start cornerbottomEndCornerSize
(dimension or faction): the size of the bottom-end cornerdashLength
(dimension): the dash length (0 means no dashes)dashGapLength
(dimension): the dash gap length (0 means no dashes)