• Stars
    star
    583
  • Rank 76,663 (Top 2 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

YCharts is a graph library for Android.

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
    )

    Line chart looks like this!

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)

    Bar chart looks like this!

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)

    Grouped Bar chart looks like this!

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
    )

    Pie chart looks like this!

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
    )

    Donut chart looks like this!

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.

    Combined bar with line chart looks like this!


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.

fig (a)

fig (b)

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.

More Repositories

1

ychat

A Kotlin Multiplatform library that provides access to ChatGPT and Open AI APIs
Objective-C
141
star
2

YMatterType

An opinionated take on Design System Typography for iOS and tvOS.
Swift
22
star
3

ytemplate-android

Y—Template aims to build your initial setup for your Android project quickly.
Kotlin
22
star
4

ycalendarpicker-ios

An easy-to-use and highly customizable month calendar.
Swift
12
star
5

YCoreUI

Core components for iOS and tvOS to accelerate building user interfaces in code.
Swift
9
star
6

YTooltip

Y-Tooltip is a light and extensible tooltip library for Jetpack Compose system
Kotlin
7
star
7

kmm-ycharts

Cross platform repository
Kotlin
5
star
8

ytags-ios

Accessible and customizable tag user interface elements for iOS.
Swift
5
star
9

ynetwork-android

Y-Network is a networking library for Android.
Kotlin
5
star
10

ybottomsheet-ios

An easy-to-use bottom sheet controller for iOS.
Swift
5
star
11

YComponentBrowser

Easily extend any project to include an intelligent design component browser.
Swift
4
star
12

ystepper-ios

Accessible and customizable shopping cart-style stepper for iOS.
Swift
4
star
13

yanalytics-ios

Y—Analytics: a generic analytics wrapper you can put around most any analytics framework
Swift
4
star
14

ycarousel-ios

An easy-to-use carousel that comes in both view and view controller flavors.
Swift
4
star
15

ysnackbar-ios

An easy-to-use UI component to display brief, transient messages to the user.
Swift
4
star
16

compose-testing-samples

This is a collection of all Android testing samples developed by YML engineering team.
Kotlin
4
star
17

lighthouse-slack-reports

Run lighthouse on a URL(s), send the report to a slack channel
TypeScript
3
star
18

yanalytics-firebase-ios

Y—Analytics implementation for Firebase SDK
Swift
3
star
19

ynetwork-ios

A networking layer for iOS and tvOS.
Swift
3
star
20

fe-component-library

fe-component-library
TypeScript
2
star
21

YTag-android

Y Tag is a UI element in Android (some times referred to as chips) which displays a piece of information. It consist of a leading icon(optional), Text and a trailing icon (optional).
Kotlin
2
star
22

ypersistence-ios

A Core Data wrapper that leverages the power of generics to allow you to work with custom model objects.
Swift
1
star
23

.github

All the templates. All of them.
1
star
24

ysidemenu-ios

Accessible and customizable side menu for iOS.
Swift
1
star
25

yanalytics-pendo-ios

Y—Analytics implementation for Pendo SDK
Swift
1
star
26

yanalytics-adobe-ios

Y—Analytics implementation for Adobe SDK
Swift
1
star