Skip to content

Commit

Permalink
Unify legends (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanialdo authored Feb 13, 2024
1 parent 1a3f2a3 commit 89e4354
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ val menuItems = listOf<MenuEntry>(
title = "Legend",
items = listOf(
MenuEntry.MenuItem(title = "Legend", screen = { LegendDemoView() }),
MenuEntry.MenuItem(title = "Legend Flow", screen = { LegendFlowDemoView() }),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import pl.krystiankaniowski.composecharts.autoColor
import pl.krystiankaniowski.composecharts.data.ChartColor
import pl.krystiankaniowski.composecharts.data.Series
import pl.krystiankaniowski.composecharts.legend.Legend
import pl.krystiankaniowski.composecharts.legend.LegendEntry
import pl.krystiankaniowski.composecharts.legend.LegendFlow

private data class LegendData(
override val label: String,
override val color: ChartColor.Solid,
) : Series

private val legendData = listOf(
LegendEntry(text = "Series A", color = autoColor(0)),
LegendEntry(text = "Series B", color = autoColor(1)),
LegendEntry(text = "Series C", color = autoColor(2)),
LegendData(label = "Series A", color = autoColor(0)),
LegendData(label = "Series B", color = autoColor(1)),
LegendData(label = "Series C", color = autoColor(2)),
)

@Suppress("MagicNumber")
Expand All @@ -25,13 +30,3 @@ fun LegendDemoView() {
)
}
}

@Suppress("MagicNumber")
@Composable
fun LegendFlowDemoView() {
Box(modifier = Modifier.padding(16.dp)) {
LegendFlow(
data = legendData,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun PointChartDemo() {
Offset(0f, 2f),
Offset(0f, 1f),
),
color = PointChart.ChartColor.Solid(autoColor(0)),
color = PointChart.ChartColor.Solid(autoColor(0).value),
),
PointChart.Series(
label = "Series B",
Expand All @@ -36,7 +36,7 @@ fun PointChartDemo() {
Offset(2f, 2f),
Offset(1f, 1f),
),
color = PointChart.ChartColor.Solid(autoColor(1)),
color = PointChart.ChartColor.Solid(autoColor(1).value),
),
PointChart.Series(
label = "Series C",
Expand All @@ -47,7 +47,7 @@ fun PointChartDemo() {
Offset(2f, 0f),
Offset(1f, 0f),
),
color = PointChart.ChartColor.Solid(autoColor(2)),
color = PointChart.ChartColor.Solid(autoColor(2).value),
),
),
),
Expand All @@ -70,7 +70,7 @@ fun PointChartCustomDemo() {
Offset(0f, 2f),
Offset(0f, 1f),
),
color = PointChart.ChartColor.Solid(autoColor(1)),
color = PointChart.ChartColor.Solid(autoColor(1).value),
pathEffect = PathEffect.dashPathEffect(floatArrayOf(5f, 10f)),
),
PointChart.Series(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import pl.krystiankaniowski.composecharts.autoColor
import pl.krystiankaniowski.composecharts.data.ChartColor
import pl.krystiankaniowski.composecharts.line.LineChart

@Suppress("MagicNumber")
Expand All @@ -15,7 +16,7 @@ fun LineChartCustomDemo() {
LineChart.Line(
label = "Custom color",
values = listOf(5f, 4f, 3f, 2f, 1f),
color = Color.Black,
color = ChartColor.Solid(Color.Black),
),
LineChart.Line(
label = "Custom line style",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pl.krystiankaniowski.composecharts

import androidx.compose.ui.graphics.Color
import pl.krystiankaniowski.composecharts.data.ChartColor

object Colors

Expand Down Expand Up @@ -35,4 +36,4 @@ object AutoColors {
fun getColor(position: Int) = Color(colors[position % colors.size])
}

fun autoColor(pos: Int) = AutoColors.getColor(pos)
fun autoColor(pos: Int) = ChartColor.Solid(AutoColors.getColor(pos))
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
package pl.krystiankaniowski.composecharts.area

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import pl.krystiankaniowski.composecharts.ChartsTheme
import pl.krystiankaniowski.composecharts.axis.XAxis
import pl.krystiankaniowski.composecharts.axis.YAxis
import pl.krystiankaniowski.composecharts.data.ChartColor
import pl.krystiankaniowski.composecharts.data.Series
import pl.krystiankaniowski.composecharts.internal.AxisScale
import pl.krystiankaniowski.composecharts.internal.ChartChoreographer
import pl.krystiankaniowski.composecharts.internal.PointMapper
import pl.krystiankaniowski.composecharts.legend.LegendEntry
import pl.krystiankaniowski.composecharts.legend.LegendFlow
import pl.krystiankaniowski.composecharts.legend.Legend
import pl.krystiankaniowski.composecharts.legend.LegendPosition

object AreaChart {
Expand All @@ -45,10 +40,10 @@ object AreaChart {
}

data class Area(
val label: String,
override val label: String,
val values: List<Float>,
val color: Color,
)
override val color: ChartColor.Solid,
) : Series

enum class Style {
OVERLAPPING,
Expand Down Expand Up @@ -106,7 +101,7 @@ fun AreaChart(
ChartChoreographer(
modifier = modifier,
title = title,
legend = { AreaLegend(data) },
legend = { Legend(data = data.areas) },
legendPosition = legendPosition,
) {

Expand Down Expand Up @@ -189,7 +184,7 @@ fun AreaChart(
}

private fun DrawScope.drawArea(
color: Color,
color: ChartColor.Solid,
values: List<Float>,
mapper: PointMapper,
) {
Expand Down Expand Up @@ -218,14 +213,14 @@ private fun DrawScope.drawArea(
path.close()

drawPath(
color = color,
color = color.value,
path = path,
style = Fill,
)
}

private fun DrawScope.drawProportionalArea(
color: Color,
color: ChartColor.Solid,
total: List<Float>,
values: List<Float>,
mapper: PointMapper,
Expand Down Expand Up @@ -255,25 +250,8 @@ private fun DrawScope.drawProportionalArea(
path.close()

drawPath(
color = color,
color = color.value,
path = path,
style = Fill,
)
}

@Composable
private fun AreaLegend(
data: AreaChart.Data,
) {
Box(modifier = Modifier.border(width = 1.dp, color = ChartsTheme.legendColor)) {
LegendFlow(
modifier = Modifier.padding(16.dp),
data = data.areas.map { item ->
LegendEntry(
item.label,
item.color,
)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package pl.krystiankaniowski.composecharts.bar

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import pl.krystiankaniowski.composecharts.ChartsTheme
import pl.krystiankaniowski.composecharts.axis.XAxis
import pl.krystiankaniowski.composecharts.axis.YAxis
import pl.krystiankaniowski.composecharts.data.ChartColor
import pl.krystiankaniowski.composecharts.data.Series
import pl.krystiankaniowski.composecharts.internal.AxisScale
import pl.krystiankaniowski.composecharts.internal.ChartChoreographer
import pl.krystiankaniowski.composecharts.internal.PointMapper
import pl.krystiankaniowski.composecharts.legend.LegendEntry
import pl.krystiankaniowski.composecharts.legend.LegendFlow
import pl.krystiankaniowski.composecharts.legend.Legend
import pl.krystiankaniowski.composecharts.legend.LegendPosition

object BarChart {
Expand All @@ -44,10 +39,10 @@ object BarChart {
}

data class Bar(
val label: String,
val color: Color,
override val label: String,
override val color: ChartColor.Solid,
val values: List<Float>,
)
) : Series

enum class Style {
GROUPED,
Expand Down Expand Up @@ -103,7 +98,7 @@ fun BarChart(
ChartChoreographer(
modifier = modifier,
title = title,
legend = { BarLegend(data) },
legend = { Legend(data = data.bars) },
legendPosition = legendPosition,
) {

Expand Down Expand Up @@ -154,7 +149,7 @@ fun BarChart(
data.bars.forEachIndexed { series, value ->
value.values.forEachIndexed { pos, v ->
drawRect(
color = value.color,
color = value.color.value,
topLeft = Offset(
x = mapper.x(0),
y = mapper.y(pos + w) + series * barHeight,
Expand All @@ -180,7 +175,7 @@ fun BarChart(
var counter = maxValues[i]
for (j in (series - 1) downTo 0) {
drawRect(
color = data.bars[j].color,
color = data.bars[j].color.value,
topLeft = Offset(
x = mapper.x(0),
y = mapper.y(i + w),
Expand All @@ -207,7 +202,7 @@ fun BarChart(
var counter = maxValues[i]
for (j in (series - 1) downTo 0) {
drawRect(
color = data.bars[j].color,
color = data.bars[j].color.value,
topLeft = Offset(
x = mapper.x(0),
y = mapper.y(i + w),
Expand All @@ -225,20 +220,3 @@ fun BarChart(
}
}
}

@Composable
private fun BarLegend(
data: BarChart.Data,
) {
Box(modifier = Modifier.border(width = 1.dp, color = ChartsTheme.legendColor)) {
LegendFlow(
modifier = Modifier.padding(16.dp),
data = data.bars.map { item ->
LegendEntry(
item.label,
item.color,
)
},
)
}
}
Loading

0 comments on commit 89e4354

Please sign in to comment.