YCharts
YCharts is a light and extensible chart library for Jetpack Compose system. It comprises two main modules:
YChartslib
(Chart components for Jetpack Compose)app
(sample app to showcase chart components)
Adding YCharts to your project:
You can add the library via Maven:
Gradle:
implementation 'co.yml:ycharts:2.0.0'
Modules
The following table outlines the modules included in this library:
Artifact | Description |
---|---|
axis |
Includes the horizontal and vertical axis components along with the math engines. |
charts |
Includes the all the chart components i.e: Line, Bar, Combined, Pie & Donut etc. also the math engines |
chartcontainer |
Provides the base container to draw any chart inside it with scroll tap feature etc out of the box. |
piechart |
Includes all the 360' chart components i.e Pie, Donut charts etc. |
Sample app
Included in this repository is a sample app with multiple charts with different styling in each section. Studying the source code of the app will give you a deep understanding of how to use YCharts, including customizing and styling the charts. All of the charts i.e line, bar, groupedBar, pie & donut are implemented in the sample app.
Examples
Let's see how we can use the chart components and style them with available customization options.
1. Line Chart:
-
Create list of points with x & y co-ordinates using
Point
data class.val pointsData: List<Point> = listOf(Point(0f, 40f), Point(1f, 90f), Point(2f, 0f), Point(3f, 60f), Point(4f, 10f))
-
Initialize X and Y Axis builders using the
AxisData
data class.val xAxisData = AxisData.Builder() .axisStepSize(100.dp) .backgroundColor(Color.Blue) .steps(pointsData.size - 1) .labelData { i -> i.toString() } .labelAndAxisLinePadding(15.dp) .build() val yAxisData = AxisData.Builder() .steps(steps) .backgroundColor(Color.Red) .labelAndAxisLinePadding(20.dp) .labelData { i -> val yScale = 100 / steps (i * yScale).formatToSinglePrecision() }.build()
-
Initialize the Line chart data with axis and other line related styling using
LineChartData
data class.val lineChartData = LineChartData( linePlotData = LinePlotData( lines = listOf( Line( dataPoints = pointsData, LineStyle(), IntersectionPoint(), SelectionHighlightPoint(), ShadowUnderLine(), SelectionHighlightPopUp() ) ), ), xAxisData = xAxisData, yAxisData = yAxisData, gridLines = GridLines(), backgroundColor = Color.White )
-
Finally use the
LineChart
Component to render the line chart with the above input params.LineChart( modifier = Modifier .fillMaxWidth() .height(300.dp), lineChartData = lineChartData )
2. Bar Chart:
-
Create list of barChartData using the random generator extension and
BarData
data class.val barChartData = DataUtils.getBarChartData(barChartListSize, maxRange)
-
Initialize X and Y Axis builders using the
AxisData
data class.val xAxisData = AxisData.Builder() .axisStepSize(30.dp) .steps(barChartData.size - 1) .bottomPadding(40.dp) .axisLabelAngle(20f) .labelData { index -> barData[index].label } .build() val yAxisData = AxisData.Builder() .steps(yStepSize) .labelAndAxisLinePadding(20.dp) .axisOffset(20.dp) .labelData { index -> (index * (maxRange / yStepSize)).toString() } .build()
-
Initialize the Bar chart data with axis and other line related styling using
BarChartData
data class.val barChartData = BarChartData( chartData = barChartData, xAxisData = xAxisData, yAxisData = yAxisData, paddingBetweenBars = 20.dp, barWidth = 25.dp )
-
Last, use the
BarChart
Component to render the bar chart with the above input params.BarChart(modifier = Modifier.height(350.dp), barChartData = barChartData)
3. Grouped Bar Chart:
-
Create list of grouped combinations of bar chart data using the random generator extension and
BarPlotData
data class.val groupBarPlotData = BarPlotData( groupBarList = DataUtils.getGroupBarChartData( barChartListSize, maxRange, eachGroupBarSize ), barColorPaletteList = getColorPaletteList(barSize) )
-
Initialize X and Y Axis builders using the
AxisData
data class.val xAxisData = AxisData.Builder() .axisStepSize(30.dp) .steps(groupBarData.size - 1) .bottomPadding(40.dp) .labelData { index -> groupBarData[index].label } .build() val yAxisData = AxisData.Builder() .steps(yStepSize) .labelAndAxisLinePadding(20.dp) .axisOffset(20.dp) .labelData { index -> (index * (maxRange / yStepSize)).toString() } .build()
-
Initialize the group bar chart data with axis and other line related styling using
GroupBarChartData
data class.val groupBarChartData = GroupBarChartData( barPlotData = groupBarPlotData, xAxisData = xAxisData, yAxisData = yAxisData )
-
Use the
GroupBarChart
Component to render the bar chart with the above input params.GroupBarChart(modifier = Modifier.height(300.dp), groupBarChartData = groupBarChartData)
4. Pie Chart:
-
Create list of slices using the
PieChartData
data class.val pieChartData = PieChartData( slices = listOf( PieChartData.Slice("SciFi", 65f, Color(0xFF333333)), PieChartData.Slice("Comedy", 35f, Color(0xFF666a86)), PieChartData.Slice("Drama", 10f, Color(0xFF95B8D1)), PieChartData.Slice("Romance", 40f, Color(0xFFF53844)) ) )
-
Initialize the pie chart config with
PieChartConfig
data class in order to achieve styling and configurations related to pie chart.val pieChartConfig = PieChartConfig( percentVisible = true, isAnimationEnable = true, showSliceLabels = false, animationDuration = 1500 )
-
Finally, use the
PieChart
component to render the chart.PieChart( modifier = Modifier .width(400.dp) .height(400.dp), pieChartData, pieChartConfig )
5. Donut Chart:
-
Similar to pie chart here we need create list of slices using the
PieChartData
data class.val donutChartData = PieChartData( slices = listOf( PieChartData.Slice("HP", 15f, Color(0xFF5F0A87)), PieChartData.Slice("Dell", 30f, Color(0xFF20BF55)), PieChartData.Slice("Lenovo", 40f, Color(0xFFEC9F05)), PieChartData.Slice("Asus", 10f, Color(0xFFF53844)) ) )
-
Initialize the pie chart config with
PieChartConfig
data class in order to achieve styling and configurations related to pie chart.val donutChartConfig = PieChartConfig( percentVisible = true, percentageFontSize = 42.sp, strokeWidth = 120f, percentColor = Color.Black, activeSliceAlpha = .9f, isAnimationEnable = true )
-
Finally, use the
DonutPieChart
component to render the chart.DonutPieChart( modifier = Modifier .fillMaxWidth() .height(500.dp), donutChartData, donutChartConfig )
6. Combined Chart:
-
Similar to line and bar chart we can combine both entities in one chart, just need to initialize the line and bar plot data using the random generator extension and add styling related to individual component.
val linePlotData = LinePlotData( lines = listOf( Line( DataUtils.getLineChartData(listSize, maxRange = 100), lineStyle = LineStyle(color = Color.Blue), intersectionPoint = IntersectionPoint(), selectionHighlightPoint = SelectionHighlightPoint(), selectionHighlightPopUp = SelectionHighlightPopUp() ), Line( DataUtils.getLineChartData(listSize, maxRange), lineStyle = LineStyle(color = Color.Black), intersectionPoint = IntersectionPoint(), selectionHighlightPoint = SelectionHighlightPoint(), selectionHighlightPopUp = SelectionHighlightPopUp() ) ) ) val barPlotData = BarPlotData( groupBarList = DataUtils.getGroupBarChartData(listSize, maxValueRange, barSize), barStyle = BarStyle(barWidth = 35.dp), barColorPaletteList = colorPaletteList )
-
Initialize X and Y Axis builders using the
AxisData
data class.val xAxisData = AxisData.Builder() .axisStepSize(30.dp) .steps(maxOf(barChartData.size - 1, lineChartData.size - 1)) .bottomPadding(40.dp) .labelData { index -> index.toString() } .build() val yAxisData = AxisData.Builder() .steps(yStepSize) .labelAndAxisLinePadding(20.dp) .axisOffset(20.dp) .labelData { index -> (index * (maxRange / yStepSize)).toString() } .build()
-
Initialize the combined chart config data with
CombinedChartData
data class in order to achieve styling and configurations related to same.val combinedChartData = CombinedChartData( combinedPlotDataList = listOf(barPlotData, linePlotData), xAxisData = xAxisData, yAxisData = yAxisData )
-
Finally, use the
CombinedChart
component to render the chart.CombinedChart( modifier = Modifier.height(400.dp), combinedCharthData = combinedChartData )
Note : To show legends infomartion related to bar,
Legends
component can be used.
Accessibility Support
To interact with your device with touch and spoken feedback, you can turn on the TalkBack screen reader. TalkBack describes the graphs/charts when tapped on the graph container. Compose views by deafult supports accessibility services, but for views drawn using canvas has no straight forward approach as of now, hence all our graph components supports an accessibility popup out of the box that will be shown over the graph with tapped on the container, with all the values described in an order to support accessibility services. User will be able to scroll the popup and find all the points, bars, slices or combined values in a descriptive manner, where user can tap and talkback would read it out loud.
Here fig(a) represents the line graph with the container being highlighted & fig(b) represents the accessibility sheet with all values laid out in detail so that talkback can describe the graph values.
Note: All the descriptions that are visible in the accessibility popup can be customized to the required string.
KMM Support.
- For the ongoing effort to provide multiplatform solution, Ycharts now being written in Compose Multiplatform, adopting KMM has been a game-changer, we hope to achieve good results in this effort with community contribution.
Currently KMM compatible code can be found at
kmm-main
branch
Changes in version 2.0
- Fix for multiple line graph/chart has been added, now it can be used to plot multiple line graphs/line charts on a single canvas or a single line with multiple colors.
- Background color for Pie chart/Donut chart now can be set via
backgroundColor
parameter through PieChartConfig model, this is done to keep the behaviour consistent with current syntax pattern. - Kotlin-library updates:
Kotlin version
has been upgraded to '1.8.10' along with agp, compose-compiler, core-ktx, target sdk, and other compatible library updates. - Sample App has been updated to showcase more use-cases for each chart type.
License
Copyright 2022 YCharts
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.