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

Code improvement #67

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions app/charts/src/main/java/com/hd/charts/BarChartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,30 @@ import com.hd.charts.internal.barchart.BarChart
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.internal.style.BarChartDefaults
import com.hd.charts.internal.style.BarChartStyleInternal
import com.hd.charts.style.BarChartViewStyle

@Composable
fun BarChartView(
chartData: ChartDataSet,
dataSet: ChartDataSet,
style: BarChartViewStyle
) {
var currentTitle by remember { mutableStateOf(chartData.data.label) }
val chartViewsStyle: ChartViewStyleInternal =
ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val chartStyle: BarChartStyleInternal =
BarChartDefaults.barChartStyle(style = style)
var title by remember { mutableStateOf(dataSet.data.label) }
val chartViewsStyle = ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val barChartStyle = BarChartDefaults.barChartStyle(style = style)

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

BarChart(chartData = chartData.data.item, style = chartStyle) {
currentTitle = when (it) {
NO_SELECTION -> currentTitle
else -> chartData.data.item.labels[it]
}
BarChart(chartData = dataSet.data.item, style = barChartStyle) {
title = when (it) {
NO_SELECTION -> title
else -> dataSet.data.item.labels[it]
}
}
}
Expand All @@ -67,15 +61,16 @@ private fun BarChartViewPreview() {

val chartData = ChartDataSet(
items = listOf(-300f, 50f, 20f, 1f, 15f, -30f, 50f, 35f, 25f, 40f, 500f),
title = stringResource(id = R.string.bar_chart))
title = stringResource(id = R.string.bar_chart)
)

Row(
modifier = Modifier
.size(400.dp)
.wrapContentHeight(),
) {
BarChartView(
chartData = chartData,
dataSet = chartData,
style = style
)
}
Expand Down
71 changes: 36 additions & 35 deletions app/charts/src/main/java/com/hd/charts/BarStackedChartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,57 +27,57 @@ import com.hd.charts.style.StackedBarChartViewStyle

@Composable
fun StackedBarChartView(
data: MultiChartDataSet,
dataSet: MultiChartDataSet,
style: StackedBarChartViewStyle
) {
val chartViewStyle = ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val chartStyle = StackedBarChartDefaults.barChartStyle(style = style)
val barChartStyle = StackedBarChartDefaults.barChartStyle(style = style)

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

val colors by remember {
mutableStateOf(
chartStyle.colors.ifEmpty {
generateColorShades(chartStyle.barColor, data.data.getFirstPointsSize())
barChartStyle.colors.ifEmpty {
generateColorShades(barChartStyle.barColor, dataSet.data.getFirstPointsSize())
}
)
}

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

StackedBarChart(data = data.data, style = chartStyle, colors = colors) {selectedIndex ->
currentTitle = when (selectedIndex) {
NO_SELECTION -> currentTitle
else -> {
data.data.items[selectedIndex].label
}
StackedBarChart(
data = dataSet.data,
style = barChartStyle,
colors = colors
) { selectedIndex ->
title = when (selectedIndex) {
NO_SELECTION -> title
else -> {
dataSet.data.items[selectedIndex].label
}
}



if (data.data.hasCategories()) {
labels = when (selectedIndex) {
NO_SELECTION -> emptyList()
else -> data.data.items[selectedIndex].item.labels
}
if (dataSet.data.hasCategories()) {
labels = when (selectedIndex) {
NO_SELECTION -> emptyList()
else -> dataSet.data.items[selectedIndex].item.labels
}
}
}

if (data.data.hasCategories()) {
LegendItem(
chartViewsStyle = chartViewStyle,
colors = colors,
legend = data.data.categories,
labels = labels
)
}
if (dataSet.data.hasCategories()) {
LegendItem(
chartViewsStyle = chartViewStyle,
colors = colors,
legend = dataSet.data.categories,
labels = labels
)
}
}
}
Expand Down Expand Up @@ -108,15 +108,16 @@ private fun StackedBarChartViewPreview() {
val data = MultiChartDataSet(
items = items,
categories = categories,
title = stringResource(id = R.string.bar_stacked_chart))
title = stringResource(id = R.string.bar_stacked_chart)
)

Row(
modifier = Modifier
.width(400.dp)
.wrapContentHeight(),
) {
StackedBarChartView(
data = data,
dataSet = data,
style = style
)
}
Expand Down
12 changes: 6 additions & 6 deletions app/charts/src/main/java/com/hd/charts/LineChartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ import com.hd.charts.style.LineChartViewStyle

@Composable
fun LineChartView(
data: ChartDataSet,
dataSet: ChartDataSet,
style: LineChartViewStyle
) {
LineChartViewImpl(
data = MultiChartData(
items = listOf(data.data),
title = data.data.label
items = listOf(dataSet.data),
title = dataSet.data.label
),
style = style
)
}

@Composable
fun LineChartView(
data: MultiChartDataSet,
dataSet: MultiChartDataSet,
style: LineChartViewStyle
) {
LineChartViewImpl(
data = data.data,
data = dataSet.data,
style = style
)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ private fun LineChartViewPreview() {
.wrapContentHeight(),
) {
LineChartView(
data = data,
dataSet = data,
style = style.build()
)
}
Expand Down
44 changes: 18 additions & 26 deletions app/charts/src/main/java/com/hd/charts/PieChartView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,33 @@ import com.hd.charts.common.model.ChartDataSet
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.internal.piechart.PieChart
import com.hd.charts.internal.style.PieChartDefaults
import com.hd.charts.internal.style.PieChartStyleInternal
import com.hd.charts.style.PieChartViewStyle

@Composable
fun PieChartView(
chartData: ChartDataSet,
dataSet: ChartDataSet,
style: PieChartViewStyle,
) {
val chartViewStyle: ChartViewStyleInternal = ChartViewDefaults.chartViewStyle(
style = style.chartViewStyle
)
val pieChartStyle: PieChartStyleInternal = PieChartDefaults.pieChartStyle(
style = style
)
val chartViewStyle = ChartViewDefaults.chartViewStyle(style = style.chartViewStyle)
val pieChartStyle = PieChartDefaults.pieChartStyle(style = style)

var currentTitle by remember { mutableStateOf(chartData.data.label) }
ChartsDefaultTheme {
ChartView(chartViewsStyle = chartViewStyle) {
Text(
modifier = chartViewStyle.modifierTopTitle,
text = currentTitle,
style = chartViewStyle.styleTitle
)
PieChart(
chartData = chartData.data.item,
style = pieChartStyle
) {
currentTitle = when (it) {
NO_SELECTION -> chartData.data.label
else -> chartData.data.item.labels[it]
}
var title by remember { mutableStateOf(dataSet.data.label) }
ChartView(chartViewsStyle = chartViewStyle) {
Text(
modifier = chartViewStyle.modifierTopTitle,
text = title,
style = chartViewStyle.styleTitle
)
PieChart(
chartData = dataSet.data.item,
style = pieChartStyle
) {
title = when (it) {
NO_SELECTION -> dataSet.data.label
else -> dataSet.data.item.labels[it]
}
}
}
Expand Down Expand Up @@ -89,7 +81,7 @@ private fun PieChartViewPreview() {
.wrapContentHeight(),
) {
PieChartView(
chartData = data,
dataSet = data,
style = style
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ class MultiChartDataSet(
categories = categories
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ internal fun BarChart(
}
}

val maxValue = chartData.points.max()
val minValue = chartData.points.min()

val maxValue = remember { chartData.points.max() }
val minValue = remember { chartData.points.min() }
var selectedIndex by remember { mutableIntStateOf(-1) }

Canvas(modifier = style.modifier.pointerInput(Unit) {
Expand Down Expand Up @@ -108,24 +107,12 @@ internal fun BarChartPreview() {
}
}.build()


Column(
modifier = Modifier.wrapContentSize()
) {
BarChart(
chartData = listOf(
100f,
-50f,
-5f,
-60f,
-1f,
-30f,
-50f,
-35f,
-25f,
-40f,
100f
).toChartData(),
chartData = listOf(100f, -50f, -5f, -60f, -1f, -30f, -50f, -35f, -25f, -40f, 100f)
.toChartData(),
style = BarChartDefaults.barChartStyle(style)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.hd.charts.internal.common.style.ChartViewStyleInternal
import com.hd.charts.internal.common.theme.ChartsDefaultTheme

@Composable
internal fun ChartView(chartViewsStyle: ChartViewStyleInternal, content: @Composable () -> Unit) {
Box(
modifier = chartViewsStyle.modifierMain
) {
Column(
modifier = Modifier
.wrapContentSize()
ChartsDefaultTheme {
Box(
modifier = chartViewsStyle.modifierMain
) {
content()
Column(
modifier = Modifier
.wrapContentSize()
) {
content()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.hd.charts.internal.common.model
internal data class ChartDataItem(
val label: String,
val item: ChartData
)
)
Loading