Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #37

Merged
merged 33 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ca2352a
Improve bar chart code
dautovicharis Jan 31, 2024
a6d7a5a
Merge pull request #27 from dautovicharis/improvement/bar-chart
dautovicharis Jan 31, 2024
5e0fff7
Update dependencies
dautovicharis Jan 31, 2024
efaa497
Merge pull request #28 from dautovicharis/update/dependencies
dautovicharis Jan 31, 2024
af2238c
Implement donut chart
dautovicharis Jan 31, 2024
dda674d
Simplify pie chart style
dautovicharis Jan 31, 2024
6b0dd66
Update app demo - add donut chart
dautovicharis Jan 31, 2024
b8e6b88
Merge pull request #29 from dautovicharis/feature/pie-chart-donut
dautovicharis Jan 31, 2024
254b020
Update README.md
dautovicharis Jan 31, 2024
505d076
Update README.md
dautovicharis Feb 1, 2024
c59d086
Add stacked bar chart
dautovicharis Feb 1, 2024
87b505b
Improve bar chart helper function
dautovicharis Feb 1, 2024
65646f4
Merge pull request #31 from dautovicharis/feature/stacked-bar-chart
dautovicharis Feb 1, 2024
c5238aa
Improve app demo
dautovicharis Feb 1, 2024
96bfdac
Merge pull request #32 from dautovicharis/improve/app-demo
dautovicharis Feb 1, 2024
8b82428
Update README.md
dautovicharis Feb 1, 2024
c030faa
Update README.md
dautovicharis Feb 2, 2024
664812f
Simplify styles public interface - pie chart
dautovicharis Feb 2, 2024
039c977
Simplify styles public interface - line chart
dautovicharis Feb 2, 2024
8e21b44
Simplify styles public interface - stacked bar chart
dautovicharis Feb 2, 2024
2ba442b
Simplify styles public interface - bar chart
dautovicharis Feb 2, 2024
6e0d87d
Merge pull request #33 from dautovicharis/improvement/styles
dautovicharis Feb 2, 2024
ea4a4f9
Improve app demo - use new styles
dautovicharis Feb 2, 2024
8fa4c66
Merge pull request #34 from dautovicharis/improve/app-demo
dautovicharis Feb 2, 2024
9d0a095
Update README.md
dautovicharis Feb 2, 2024
baea1ff
Generate documentation
dautovicharis Feb 3, 2024
78b9939
Add documentation plugin
dautovicharis Feb 3, 2024
efbefff
Refactor lib structure
dautovicharis Feb 3, 2024
030ff9e
Update documentation
dautovicharis Feb 3, 2024
0ac94ec
Merge pull request #35 from dautovicharis/feature/documentation
dautovicharis Feb 3, 2024
e460715
Update README.md
dautovicharis Feb 3, 2024
9146e32
Update README.md
dautovicharis Feb 3, 2024
a164779
Merge pull request #36 from dautovicharis/update/readme
dautovicharis Feb 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
68 changes: 16 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ This is a simple chart library built with Jetpack Compose.

Inspired by: https://github.com/AppPear/ChartView

![Screenshot 2024-02-03 at 21 21 37](https://github.com/dautovicharis/Charts/assets/7049715/9ae209dd-5334-4436-9a80-febb6439abab)

# Documentation
Public API: https://dautovicharis.github.io/Charts/

# Features
- Animations
- M3 theme support
Expand Down Expand Up @@ -40,64 +45,23 @@ Add it in your *settings.gradle.kts* at the end of repositories:

# Supported charts
### Pie
```kotlin
@Composable
private fun AddPieChart () {
PieChartView(
chartData = ChartData.fromDoubleList(listOf(
8.0, 23.0, 54.0, 32.0, 12.0, 37.0
), postfix = " °C"),
title = stringResource(id = R.string.pie_chart),
chartStyle = PieChartDefaults.pieChartStyle()
)
}
```
![Untitled design](https://github.com/dautovicharis/Charts/assets/7049715/7147b4ba-d16a-42bb-ac63-6a868f76813d)

![ezgif com-optimize (1)](https://github.com/dautovicharis/Charts/assets/7049715/09e988bc-f0d8-4026-bce7-192fbbccbecc)

### Line
```kotlin
@Composable
private fun AddLineChart (bezier: Boolean = false, showPoints: Boolean = false) {
LineChartView(
data = ChartData.fromIntList(listOf(
8, 23, 54, 32, 12, 37, 7, 23, 43
)),
chartStyle = lineChartStyle(
bezier = bezier,
showPoints = showPoints
),
title = stringResource(id = R.string.line_chart)
)
}
```

![Untitled design-2](https://github.com/dautovicharis/Charts/assets/7049715/f87d7e38-b097-43c5-87cb-8a9102f57e07)


![line-curve-merged](https://github.com/dautovicharis/Charts/assets/7049715/f48a96be-955c-4c3a-bf3e-ca6ec7357c03)
![line-merged](https://github.com/dautovicharis/Charts/assets/7049715/07fc5cce-1f3d-43b0-a12a-f9ef20b36878)

### Bar
```kotlin
@Composable
private fun AddBarChart() {
BarChartView(
chartData = ChartData.fromFloatList(listOf(
-8.0f, 23.0f, 54.0f, 12.0f, 37.0f, -100f
)),
title = stringResource(id = R.string.bar_chart),
chartStyle = BarChartDefaults.barChartStyle()
)
}
```
![Untitled design-5](https://github.com/dautovicharis/Charts/assets/7049715/ad4a597c-ebe0-42df-9168-a08cbf3fb994)

# App demo
Code: https://github.com/dautovicharis/Charts/blob/main/app/src/main/java/com/hd/charts/app/MainActivity.kt

![bar-chart](https://github.com/dautovicharis/Charts/assets/7049715/86a490f5-613f-4fba-b62a-b3106c31cf17)

https://github.com/dautovicharis/Charts/assets/7049715/a5d5f337-6c83-4853-8dd1-caf7c498bc13

https://github.com/dautovicharis/Charts/assets/7049715/1677bbfe-77ff-4af7-ae8e-bf8ea8e836c3
### Stacked Bar
![stacked-bar-merged](https://github.com/dautovicharis/Charts/assets/7049715/09161564-88de-479f-9be8-aa7c134faffa)

# Examples
[Code Examples](https://github.com/dautovicharis/Charts/tree/main/app/src/main/java/com/hd/charts/app/demo)


# Contributions
Feel free to create an issue for any suggestions, improvement ideas, feature requests, or bug fixes.
If you have any questions, please DM me: https://www.linkedin.com/in/dautovicharis/
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ dependencies {
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.navigation.compose)

androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
Expand Down
16 changes: 16 additions & 0 deletions app/charts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.jetbrainsKotlinAndroid)
`maven-publish`
alias(libs.plugins.dokka)
}

android {
Expand Down Expand Up @@ -58,8 +59,23 @@ dependencies {
testImplementation(libs.google.tuth)
testImplementation(libs.mockk)

// Documentation
dokkaPlugin(libs.dokkaAndroid)
}

// https://kotlin.github.io/dokka/1.6.0/user_guide/gradle/usage/
tasks.dokkaHtml.configure {
outputDirectory.set(project.rootDir.resolve("docs"))
dokkaSourceSets {
configureEach {
includeNonPublic.set(false)
skipEmptyPackages.set(true)
skipDeprecated.set(false)
}
}
}


publishing {
publications {
register<MavenPublication>("release") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hd.charts.barchart
package com.hd.charts

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
Expand All @@ -14,26 +14,31 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hd.charts.R
import com.hd.charts.common.NO_SELECTION
import com.hd.charts.internal.common.NO_SELECTION
import com.hd.charts.internal.common.composable.ChartView
import com.hd.charts.common.model.ChartData
import com.hd.charts.common.style.ChartViewDefaults
import com.hd.charts.common.style.ChartViewStyle
import com.hd.charts.common.theme.ChartsDefaultTheme
import com.hd.charts.internal.common.style.ChartViewDefaults
import com.hd.charts.internal.common.style.ChartViewStyleInternal
import com.hd.charts.internal.common.theme.ChartsDefaultTheme
import com.hd.charts.internal.barchart.BarChart
import com.hd.charts.internal.barchart.BarChartDefaults
import com.hd.charts.internal.barchart.BarChartStyleInternal
import com.hd.charts.style.BarChartViewStyle

@Composable
fun BarChartView(
chartData: ChartData,
title: String,
chartViewsStyle: ChartViewStyle = ChartViewDefaults.chartViewStyle(),
chartStyle: BarChartStyle = BarChartDefaults.barChartStyle()
style: BarChartViewStyle
) {
var currentTitle by remember { mutableStateOf(title) }
val chartViewsStyle: ChartViewStyleInternal =
ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val chartStyle: BarChartStyleInternal =
BarChartDefaults.barChartStyle(style = style)

ChartsDefaultTheme {
Column(
modifier = chartViewsStyle.modifierMain
) {
ChartView(chartViewsStyle = chartViewsStyle) {
Text(
modifier = chartViewsStyle.modifierTopTitle,
text = currentTitle,
Expand All @@ -52,14 +57,26 @@ fun BarChartView(

@Composable
private fun BarChartViewPreview() {
val barColor = MaterialTheme.colorScheme.primary

val style = BarChartViewStyle.BarChartStyleBuilder().apply {
barChartStyle {
this.barColor = barColor
this.space = 8.dp
}
}.build()

Row(
modifier = Modifier
.size(400.dp)
.wrapContentHeight(),
) {
BarChartView(
chartData = ChartData.fromFloatList(listOf(-300f, 50f, 20f, 1f, 15f, -30f, 50f, 35f, 25f, 40f, 500f)),
title = stringResource(id = R.string.bar_chart)
chartData = ChartData.fromFloatList(
listOf(-300f, 50f, 20f, 1f, 15f, -30f, 50f, 35f, 25f, 40f, 500f)
),
title = stringResource(id = R.string.bar_chart),
style = style
)
}
}
Expand Down
166 changes: 166 additions & 0 deletions app/charts/src/main/java/com/hd/charts/BarStackedChartView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.hd.charts

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.hd.charts.internal.barstackedchart.LegendItem
import com.hd.charts.internal.barstackedchart.StackedBarChart
import com.hd.charts.internal.barstackedchart.StackedBarChartDefaults
import com.hd.charts.internal.barstackedchart.StackedBarChartStyleInternal
import com.hd.charts.internal.barstackedchart.generateColorShades
import com.hd.charts.internal.common.NO_SELECTION
import com.hd.charts.internal.common.composable.ChartView
import com.hd.charts.internal.common.style.ChartViewDefaults
import com.hd.charts.internal.common.style.ChartViewStyleInternal
import com.hd.charts.internal.common.theme.ChartsDefaultTheme
import com.hd.charts.common.model.ChartData
import com.hd.charts.style.StackedBarChartViewStyle

data class StackedChartData(
val label: String,
val data: ChartData
)

@Composable
fun StackedBarChartView(
chartData: List<StackedChartData>,
title: String,
legend: List<String>,
style: StackedBarChartViewStyle
) {

val chartViewStyle: ChartViewStyleInternal =
ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val chartStyle: StackedBarChartStyleInternal =
StackedBarChartDefaults.barChartStyle(style = style)

var currentTitle by remember { mutableStateOf(title) }
var labels by remember { mutableStateOf(listOf<String>()) }

val colors = when (chartStyle.colors.isEmpty()) {
true -> {
generateColorShades(chartStyle.barColor, chartData.first().data.points.size)
}

else -> {
chartStyle.colors
}
}

ChartsDefaultTheme {
ChartView(chartViewsStyle = chartViewStyle) {
Text(
modifier = chartViewStyle.modifierTopTitle,
text = currentTitle,
style = chartViewStyle.styleTitle
)

StackedBarChart(chartData = chartData, style = chartStyle, colors = colors) {
currentTitle = when (it) {
NO_SELECTION -> title
else -> chartData[it].label
}

labels = when (it) {
NO_SELECTION -> emptyList()
else -> chartData[it].data.labels
}
}

LegendItem(
chartViewsStyle = chartViewStyle,
colors = colors,
legend = legend,
labels = labels
)
}
}
}

@Composable
private fun StackedBarChartViewPreview() {
val barColor = MaterialTheme.colorScheme.primary

val style = StackedBarChartViewStyle.StackedBarChartStyleBuilder().apply {
stackedBarChartStyle {
this.barColor = barColor
this.space = 8.dp
}
}.build()

val legendLabels = listOf(
"Jan", "Feb", "Mar"
)

val chartData = listOf(
StackedChartData(
label = "Cherry St.", data = ChartData.fromFloatList(
listOf(8261.68f, 4810.34f, 1536.57f)
)
), StackedChartData(
label = "Strawberry Mall", data = ChartData.fromFloatList(
listOf(7875.87f, 3126.58f, 2019.81f)
)
), StackedChartData(
label = "Peach St.", data = ChartData.fromFloatList(
listOf(4990.23f, 4923.48f, 1472.59f)
)
), StackedChartData(
label = "Lime Av.", data = ChartData.fromFloatList(
listOf(4658.42f, 2955.55f, 1390.55f)
)
), StackedChartData(
label = "Apple Rd.", data = ChartData.fromFloatList(
listOf(3952f, 1858.46f, 917.9f)
)
)
)

Row(
modifier = Modifier
.width(400.dp)
.wrapContentHeight(),
) {
StackedBarChartView(
chartData = chartData,
legend = legendLabels,
title = stringResource(id = R.string.bar_stacked_chart),
style = style
)
}
}

@Preview
@Composable
private fun StackedBarChartViewDefault() {
ChartsDefaultTheme(darkTheme = false, dynamicColor = false) {
StackedBarChartViewPreview()
}
}

@Preview
@Composable
private fun StackedBarChartViewDark() {
ChartsDefaultTheme(darkTheme = true, dynamicColor = false) {
StackedBarChartViewPreview()
}
}

@Preview
@Composable
private fun StackedBarChartViewDynamic() {
ChartsDefaultTheme(darkTheme = false, dynamicColor = true) {
StackedBarChartViewPreview()
}
}
Loading