Bar Chart

A bar chart is a chart that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. It is a very common chart type used in data visualization.

TypeResource
DesignFigma
WebWeb Storybook
CodeGitlab

Bar Chart Preview

Usage

import id.co.biofarma.binduiandroid.components.barchart.BarChart
val xAxisLabels = listOf("Jan", "Feb", "Mar", "Apr", "Mei")
 
val sampleData = listOf(
    BarChartDataset(
        label = "Data 1",
        values = listOf(50f, 55f, 30f, 15f, 44f),
        color = colors.random().hashCode()
    ),
    BarChartDataset(
        label = "Data 2",
        values = listOf(35f, 66f, 50f, 22f, 23f),
        color = colors.random().hashCode()
    ),
    BarChartDataset(
        label = "Data 3",
        values = listOf(24f, 15f, 42f, 34f, 30f),
        color = colors.random().hashCode()
    ),
    BarChartDataset(
        label = "Data 4",
        values = listOf(55f, 90f, 85f, 75f, 98f),
        color = colors.random().hashCode()
    ),
    BarChartDataset(
        label = "Data 5",
        values = listOf(10f, 15f, 13f, 11f, 15f),
        color = colors.random().hashCode()
    ),
)
 
val data = when (dataset) {
    1 -> sampleData.take(1)
    2 -> sampleData.take(2)
    3 -> sampleData.take(3)
    4 -> sampleData.take(4)
    else -> sampleData.take(5)
}
 
BarChart(
    xAxisLabels = xAxisLabels,
    dataset = data,
    onBarClick = {x, y ->
        Toast.makeText(context, "Data: $y in $x", Toast.LENGTH_SHORT).show()
    },
    legendPosition = legendPosition
)

Examples

Dataset

The number of datasets can affect the shape of the bar chart

Bar ChartDouble Preview

Legend Position

Use legendPosition parameter to set the position of the legend. There is two options: top and bottom.

Doughnut Chart Legend Position Preview


Props

PropsRequiredDefaultTypedescription
idfalse-StringA unique identifier for the pie chart.
legendPositionfalse-top bottomLocations of the legend.
xAxisLabelsfalse-List<String>The labels corresponding to the dataset.
datasetfalse-List<BarChartDataset>The values corresponding to the dataset.
BIND © Biofarma Innovate Next-level Design .