Skip to content

Commit

Permalink
Improve mock data (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
dautovicharis authored Jun 17, 2024
1 parent 2ea76e2 commit 9765d39
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.dautovicharis.charts.common.model.ChartDataSet
import io.github.dautovicharis.charts.internal.common.theme.ChartsDefaultTheme
import io.github.dautovicharis.charts.mock.Mock
import io.github.dautovicharis.charts.style.BarChartDefaults

@Composable
private fun BarChartViewPreview() {
val chartData = ChartDataSet(
items = listOf(-300f, 50f, 20f, 1f, 15f, -30f, 50f, 35f, 25f, 40f, 500f),
title = "Bar Chart"
)

Column(
modifier = Modifier
.size(400.dp)
.wrapContentHeight(),
) {
BarChartView(
dataSet = chartData,
dataSet = Mock.barChart(),
style = BarChartDefaults.style()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.dautovicharis.charts.common.model.MultiChartDataSet
import io.github.dautovicharis.charts.internal.common.theme.ChartsDefaultTheme
import io.github.dautovicharis.charts.mock.Mock
import io.github.dautovicharis.charts.style.StackedBarChartDefaults

@Composable
private fun StackedBarChartViewPreview() {
val items = listOf(
"Cherry St." to listOf(8261.68f, 8810.34f, 30000.57f),
"Cherry St." to listOf(8261.68f, 8810.34f, 30000.57f),
"Test1" to listOf(1500.87f, 2765.58f, 33245.81f),
"Test2" to listOf(5444.87f, 233.58f, 67544.81f)
)

val data = MultiChartDataSet(
items = items,
categories = listOf("Jan", "Feb", "Mar"),
title = "Stacked Bar Chart"
)

StackedBarChartView(
dataSet = data,
dataSet = Mock.stackedBarChart(),
style = StackedBarChartDefaults.style()
)
}
Expand Down Expand Up @@ -61,21 +48,8 @@ private fun StackedBarChartViewInvalidData() {
space = 8.dp
)

val items = listOf(
"Cherry St." to listOf(8261.68f, 8810.34f, 30000.57f),
"Cherry St." to listOf(8261.68f, 8810.34f),
"Test1" to listOf(1500.87f, 2765.58f, 33245.81f),
"Test2" to listOf(5444.87f, 233.58f)
)

val data = MultiChartDataSet(
items = items,
categories = listOf("Jan", "Feb"),
title = "Stacked Bar Chart"
)

StackedBarChartView(
dataSet = data,
dataSet = Mock.stackedBarChartInvalid(),
style = style
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.dautovicharis.charts.common.model.MultiChartDataSet
import io.github.dautovicharis.charts.internal.common.theme.ChartsDefaultTheme
import io.github.dautovicharis.charts.mock.Mock
import io.github.dautovicharis.charts.style.ChartViewDefaults
import io.github.dautovicharis.charts.style.LineChartDefaults

Expand All @@ -27,22 +27,8 @@ private fun LineChartViewPreview() {
chartViewStyle = ChartViewDefaults.style(width = 300.dp)
)

val items = listOf(
"Cherry St." to listOf(8261.68f, 8810.34f, 30000.57f),
"Strawberry Mall" to listOf(8261.68f, 8810.34f, 30000.57f),
"Peach St." to listOf(1500.87f, 2765.58f, 33245.81f),
"Lime Av." to listOf(5444.87f, 233.58f, 67544.81f)
)

val data = MultiChartDataSet(
items = items,
categories = listOf("Jan", "Feb", "Mar"),
title = "Line Chart",
prefix = "$"
)

LineChartView(
dataSet = data,
dataSet = Mock.lineChart(),
style = style
)
}
Expand Down Expand Up @@ -88,22 +74,8 @@ private fun LineChartViewInvalidData() {
chartViewStyle = ChartViewDefaults.style(width = 300.dp)
)

val items = listOf(
"Cherry St." to listOf(8261.68f, 8810.34f),
"Strawberry Mall" to listOf(8261.68f, 8810.34f, 30000.57f),
"Peach St." to listOf(1500.87f, 2765.58f),
"Lime Av." to listOf(5444.87f, 233.58f, 67544.81f)
)

val data = MultiChartDataSet(
items = items,
categories = listOf("Jan", "Feb"),
title = "Line Chart",
prefix = "$"
)

LineChartView(
dataSet = data,
dataSet = Mock.lineChartInvalid(),
style = style
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.github.dautovicharis.charts.common.model.ChartDataSet
import io.github.dautovicharis.charts.internal.common.theme.ChartsDefaultTheme
import io.github.dautovicharis.charts.mock.Mock
import io.github.dautovicharis.charts.style.ChartViewDefaults
import io.github.dautovicharis.charts.style.PieChartDefaults
import io.github.dautovicharis.charts.style.PieChartStyle
Expand All @@ -31,18 +31,13 @@ private fun PieChartViewPreview() {
)
)

val data = ChartDataSet(
items = listOf(8.0f, 23.0f, 54.0f, 32.0f, 12.0f, 37.0f, 7.0f, 23.0f, 43.0f),
title = "Pie Chart"
)

Row(
modifier = Modifier
.width(300.dp)
.wrapContentHeight(),
) {
PieChartView(
dataSet = data,
dataSet = Mock.pieChart(),
style = style
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.github.dautovicharis.charts.mock

import io.github.dautovicharis.charts.common.model.ChartDataSet
import io.github.dautovicharis.charts.common.model.MultiChartDataSet
import kotlin.random.Random

internal object Mock {
private const val LINE_CHART_TITLE = "Line Chart"
private const val BAR_CHART_TITLE = "Bar Chart"
private const val STACKED_BAR_CHART_TITLE = "Stacked Bar Chart"
private const val PIE_CHART_TITLE = "Pie Chart"

private val CATEGORIES = listOf("Jan", "Feb", "Mar")

private val FIRST_ITEM = listOf(8261.68f, 8810.34f, 30000.57f)
private val SECOND_ITEM = listOf(8261.68f, 8810.34f, 30000.57f)
private val THIRD_ITEM = listOf(1500.87f, 2765.58f, 33245.81f)
private val FOURTH_ITEM = listOf(5444.87f, 233.58f, 67544.81f)

private const val FIRST_ITEM_NAME = "Item 1"
private const val SECOND_ITEM_NAME = "Item 2"
private const val THIRD_ITEM_NAME = "Item 3"
private const val FOURTH_ITEM_NAME = "Item 4"

private const val PREFIX = "$"

private val ITEMS = listOf(
FIRST_ITEM_NAME to FIRST_ITEM,
SECOND_ITEM_NAME to SECOND_ITEM,
THIRD_ITEM_NAME to THIRD_ITEM,
FOURTH_ITEM_NAME to FOURTH_ITEM
)

private fun mockList(
size: Int, minFloat: Float = -300f,
maxFloat: Float = 500f
): List<Float> {
val mockList = mutableListOf<Float>()
repeat(size) {
val randomFloat = Random.nextDouble(minFloat.toDouble(), maxFloat.toDouble()).toFloat()
mockList.add(randomFloat)
}
return mockList
}

fun barChart(): ChartDataSet {
return ChartDataSet(
items = mockList(size = 10),
title = BAR_CHART_TITLE
)
}

fun stackedBarChart(): MultiChartDataSet {
return MultiChartDataSet(
items = ITEMS,
categories = CATEGORIES,
title = STACKED_BAR_CHART_TITLE
)
}

fun stackedBarChartInvalid(): MultiChartDataSet {
val items = listOf(
FIRST_ITEM_NAME to FIRST_ITEM,
SECOND_ITEM_NAME to SECOND_ITEM.dropLast(1),
THIRD_ITEM_NAME to THIRD_ITEM,
FOURTH_ITEM_NAME to FOURTH_ITEM.dropLast(1)
)

return MultiChartDataSet(
items = items,
categories = CATEGORIES.dropLast(1),
title = STACKED_BAR_CHART_TITLE
)
}

fun lineChart(): MultiChartDataSet {
return MultiChartDataSet(
items = ITEMS,
categories = CATEGORIES,
title = LINE_CHART_TITLE,
prefix = PREFIX
)
}

fun lineChartInvalid(): MultiChartDataSet {
val items = listOf(
FIRST_ITEM_NAME to FIRST_ITEM.dropLast(1),
SECOND_ITEM_NAME to SECOND_ITEM,
THIRD_ITEM_NAME to THIRD_ITEM.dropLast(1),
FOURTH_ITEM_NAME to FOURTH_ITEM
)

return MultiChartDataSet(
items = items,
categories = CATEGORIES.dropLast(1),
title = LINE_CHART_TITLE,
prefix = PREFIX
)
}

fun pieChart(): ChartDataSet {
return ChartDataSet(
items = mockList(size = 9, minFloat = 7f, maxFloat = 54f),
title = PIE_CHART_TITLE
)
}
}

0 comments on commit 9765d39

Please sign in to comment.