Skip to content

Commit

Permalink
library restructured to render based arch
Browse files Browse the repository at this point in the history
  • Loading branch information
AndroidPoet committed Jan 8, 2025
1 parent 29578fa commit c9b1759
Show file tree
Hide file tree
Showing 59 changed files with 718 additions and 591 deletions.
7 changes: 6 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ root = true
[*]
# Most of the standard properties are supported
indent_size=2
max_line_length=100
max_line_length=100

ktlint_standard_function-naming = disabled
ktlint_standard_property-naming = disabled
ktlint_standard_filename = disabled
ktlint_standard_no-empty-file = disabled
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ fun ChartContainer(
) {
Box(
modifier =
modifier
.fillMaxWidth()
.height(200.dp)
.padding(horizontal = 16.dp),
modifier
.fillMaxWidth()
.height(200.dp)
.padding(horizontal = 16.dp),
contentAlignment = Alignment.Center,
) {
content()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ import io.androidpoet.drafter.bars.model.GroupedBarChartData
import io.androidpoet.drafter.bars.renderer.GroupedBarChartRenderer
import io.androidpoet.drafterdemo.ChartTitle

private fun getBarChartRenderer() = GroupedBarChartRenderer(
GroupedBarChartData(
labelsList = listOf("2020", "2021", "2022"),
itemNames = listOf("Product A", "Product B", "Product C"),
groupedValues = listOf(
listOf(10f, 20f, 15f), // 2020
listOf(25f, 5f, 30f), // 2021
listOf(12f, 28f, 10f), // 2022
private fun getBarChartRenderer() =
GroupedBarChartRenderer(
GroupedBarChartData(
labelsList = listOf("2020", "2021", "2022"),
itemNames = listOf("Product A", "Product B", "Product C"),
groupedValues =
listOf(
listOf(10f, 20f, 15f), // 2020
listOf(25f, 5f, 30f), // 2021
listOf(12f, 28f, 10f), // 2022
),
colors = listOf(Color.Red, Color.Green, Color.Blue),
),
colors = listOf(Color.Red, Color.Green, Color.Blue),
),
)
)

@Composable
fun GroupedBarChartExample() {
ChartTitle(text = "Grouped Bar Chart")
BarChart(
renderer = getBarChartRenderer(),
modifier = Modifier
.height(300.dp)
.fillMaxWidth(),
modifier =
Modifier
.height(300.dp)
.fillMaxWidth(),
animate = true,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import io.androidpoet.drafter.bars.renderer.HistogramRenderer

private fun getHistogramData() = listOf(0.3f, 1.1f, 2.7f, 1.9f)

private fun getHistogramRenderer() = HistogramRenderer(
dataPoints = getHistogramData(),
binCount = 5,
color = Color.Blue,
)
private fun getHistogramRenderer() =
HistogramRenderer(
dataPoints = getHistogramData(),
binCount = 5,
color = Color.Blue,
)

@Composable
public fun HistogramChartExample(
fun HistogramChartExample(
modifier: Modifier = Modifier,
animate: Boolean = true,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import io.androidpoet.drafter.bars.model.SimpleBarChartData
import io.androidpoet.drafter.bars.renderer.BarChartRenderer
import io.androidpoet.drafterdemo.ChartTitle

private fun getBarChartData() = SimpleBarChartData(
labelsList = listOf("Jan", "Feb", "Mar", "Apr"),
values = listOf(10f, 30f, 15f, 45f),
colors = listOf(Color.Red, Color.Green, Color.Blue, Color.Magenta),
)
private fun getBarChartData() =
SimpleBarChartData(
labelsList = listOf("Jan", "Feb", "Mar", "Apr"),
values = listOf(10f, 30f, 15f, 45f),
colors = listOf(Color.Red, Color.Green, Color.Blue, Color.Magenta),
)

private fun getSimpleBarChartRenderer() = BarChartRenderer(getBarChartData())

Expand All @@ -40,9 +41,10 @@ fun SimpleBarChartExample() {

BarChart(
renderer = getSimpleBarChartRenderer(),
modifier = Modifier
.height(300.dp)
.fillMaxWidth(),
modifier =
Modifier
.height(300.dp)
.fillMaxWidth(),
animate = true,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ import io.androidpoet.drafter.bars.model.StackedBarChartData
import io.androidpoet.drafter.bars.renderer.StackedBarChartRenderer
import io.androidpoet.drafterdemo.ChartTitle

private fun getStackedBarChartData() = StackedBarChartData(
labelsList = listOf("Q1", "Q2", "Q3"),
stacks = listOf(
listOf(10f, 15f, 5f), // Q1
listOf(8f, 12f, 20f), // Q2
listOf(18f, 10f, 15f), // Q3
),
colors = listOf(Color.Red, Color.Green, Color.Blue),
)
private fun getStackedBarChartData() =
StackedBarChartData(
labelsList = listOf("Q1", "Q2", "Q3"),
stacks =
listOf(
listOf(10f, 15f, 5f), // Q1
listOf(8f, 12f, 20f), // Q2
listOf(18f, 10f, 15f), // Q3
),
colors = listOf(Color.Red, Color.Green, Color.Blue),
)

private fun getStackedBarChartRenderer() = StackedBarChartRenderer(getStackedBarChartData())

Expand All @@ -44,9 +46,10 @@ fun StackedBarChartExample(modifier: Modifier = Modifier) {

BarChart(
renderer = getStackedBarChartRenderer(),
modifier = Modifier
.height(300.dp)
.fillMaxWidth(),
modifier =
modifier
.height(300.dp)
.fillMaxWidth(),
animate = true,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@ import io.androidpoet.drafter.bars.model.WaterfallChartData
import io.androidpoet.drafter.bars.renderer.WaterfallChartRenderer
import io.androidpoet.drafterdemo.ChartTitle

private fun getWaterfallChartRenderer() = WaterfallChartRenderer(
WaterfallChartData(
labelsList = listOf("Start", "Revenue", "Cost", "Profit"),
values = listOf(+50f, -20f, +30f), // Changes from 'Start'
colors = listOf(Color.Green, Color.Red, Color.Green),
initialValue = 100f, // Start from 100
),
)
private fun getWaterfallChartRenderer() =
WaterfallChartRenderer(
WaterfallChartData(
labelsList = listOf("Start", "Revenue", "Cost", "Profit"),
values = listOf(+50f, -20f, +30f), // Changes from 'Start'
colors = listOf(Color.Green, Color.Red, Color.Green),
initialValue = 100f, // Start from 100
),
)

@Composable
fun WaterfallChartExample(modifier: Modifier = Modifier) {
ChartTitle(text = "Waterfall Chart")

BarChart(
renderer = getWaterfallChartRenderer(),
modifier = Modifier
.height(300.dp)
.fillMaxWidth(),
modifier =
modifier
.height(300.dp)
.fillMaxWidth(),
animate = true,
)
}
50 changes: 26 additions & 24 deletions app/src/main/kotlin/io/androidpoet/drafterdemo/buble/BubbleChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,32 @@ import io.androidpoet.drafter.buble.BubbleChartData
import io.androidpoet.drafter.buble.SimpleBubbleChartDataRenderer
import io.androidpoet.drafterdemo.ChartTitle

private fun getBubbleChartData() = BubbleChartData(
series = listOf(
listOf(
BubbleChartData.BubbleData(10f, 26f, 30f, Color.Blue),
BubbleChartData.BubbleData(26f, 30f, 60f, Color.Blue),
BubbleChartData.BubbleData(26f, 46f, 45f, Color.Blue),
),
listOf(
BubbleChartData.BubbleData(14f, 15f, 30f, Color.Green),
BubbleChartData.BubbleData(22f, 36f, 45f, Color.Green),
BubbleChartData.BubbleData(90f, 57f, 75f, Color.Green),
),
listOf(
BubbleChartData.BubbleData(8f, 9f, 90f, Color.Yellow),
BubbleChartData.BubbleData(20f, 57f, 45f, Color.Yellow),
BubbleChartData.BubbleData(40f, 50f, 60f, Color.Yellow),
),
listOf(
BubbleChartData.BubbleData(8f, 20f, 22.5f, Color.Red),
BubbleChartData.BubbleData(12f, 30f, 30f, Color.Red),
BubbleChartData.BubbleData(30f, 40f, 45f, Color.Red),
),
),
)
private fun getBubbleChartData() =
BubbleChartData(
series =
listOf(
listOf(
BubbleChartData.BubbleData(10f, 26f, 30f, Color.Blue),
BubbleChartData.BubbleData(26f, 30f, 60f, Color.Blue),
BubbleChartData.BubbleData(26f, 46f, 45f, Color.Blue),
),
listOf(
BubbleChartData.BubbleData(14f, 15f, 30f, Color.Green),
BubbleChartData.BubbleData(22f, 36f, 45f, Color.Green),
BubbleChartData.BubbleData(90f, 57f, 75f, Color.Green),
),
listOf(
BubbleChartData.BubbleData(8f, 9f, 90f, Color.Yellow),
BubbleChartData.BubbleData(20f, 57f, 45f, Color.Yellow),
BubbleChartData.BubbleData(40f, 50f, 60f, Color.Yellow),
),
listOf(
BubbleChartData.BubbleData(8f, 20f, 22.5f, Color.Red),
BubbleChartData.BubbleData(12f, 30f, 30f, Color.Red),
BubbleChartData.BubbleData(30f, 40f, 45f, Color.Red),
),
),
)

private fun getBubbleChartRenderer() = SimpleBubbleChartDataRenderer(getBubbleChartData())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import io.androidpoet.drafter.gant.GanttChartRenderer
import io.androidpoet.drafter.gant.GanttTask
import io.androidpoet.drafterdemo.ChartTitle

private fun getGanttChartRenderer() = GanttChartRenderer(
GanttChartData(
listOf(
GanttTask("Planning", 0f, 2f),
GanttTask("Design", 2f, 2f),
GanttTask("Development", 4f, 3f),
GanttTask("Testing", 7f, 2f),
GanttTask("Deployment", 9f, 1f),
private fun getGanttChartRenderer() =
GanttChartRenderer(
GanttChartData(
listOf(
GanttTask("Planning", 0f, 2f),
GanttTask("Design", 2f, 2f),
GanttTask("Development", 4f, 3f),
GanttTask("Testing", 7f, 2f),
GanttTask("Deployment", 9f, 1f),
),
),
),
)
)

@Composable
fun GanttChartExample() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ import kotlinx.datetime.Clock
import kotlin.random.Random
import kotlin.time.Duration.Companion.days

private fun getHeatmapRenderer() = DefaultHeatmapRenderer(
ContributionHeatmapData(
buildList {
val now = Clock.System.now()
repeat(365) { day ->
val date = now.minus(day.days)
val count = if (Random.nextFloat() > 0.6f) Random.nextInt(1, 15) else 0
add(ContributionData(date, count))
}
},
),
)
private fun getHeatmapRenderer() =
DefaultHeatmapRenderer(
ContributionHeatmapData(
buildList {
val now = Clock.System.now()
repeat(365) { day ->
val date = now.minus(day.days)
val count = if (Random.nextFloat() > 0.6f) Random.nextInt(1, 15) else 0
add(ContributionData(date, count))
}
},
),
)

@Composable
fun GithubGraph() {
ChartTitle(text = "Github Graph")

ContributionHeatmap(
renderer = getHeatmapRenderer(),
modifier = Modifier
.fillMaxWidth()
.height(112.dp),
modifier =
Modifier
.fillMaxWidth()
.height(112.dp),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ import io.androidpoet.drafter.lines.renderer.GroupedLineChartRenderer
import io.androidpoet.drafterdemo.ChartContainer
import io.androidpoet.drafterdemo.ChartTitle

private fun getGroupedLineChartData() = GroupedLineChartData(
labels = listOf("Q1", "Q2", "Q3", "Q4"),
itemNames = listOf("Product A", "Product B"),
groupedValues = listOf(
listOf(10f, 15f),
listOf(20f, 25f),
listOf(15f, 10f),
listOf(25f, 20f),
),
colors = listOf(Color.Cyan, Color.Magenta),
)
private fun getGroupedLineChartData() =
GroupedLineChartData(
labels = listOf("Q1", "Q2", "Q3", "Q4"),
itemNames = listOf("Product A", "Product B"),
groupedValues =
listOf(
listOf(10f, 15f),
listOf(20f, 25f),
listOf(15f, 10f),
listOf(25f, 20f),
),
colors = listOf(Color.Cyan, Color.Magenta),
)

private fun getGroupedLineChartRenderer() = GroupedLineChartRenderer(getGroupedLineChartData())

Expand Down
Loading

0 comments on commit c9b1759

Please sign in to comment.