Skip to content
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
18 changes: 17 additions & 1 deletion playground/src/jsMain/kotlin/codegen/bar/BarStyleProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import model.BarStyleState
import model.StylePropertiesSnapshot

@Composable
fun barStylePropertiesSnapshot(styleState: BarStyleState): StylePropertiesSnapshot {
fun barStylePropertiesSnapshot(
styleState: BarStyleState,
seriesCount: Int,
): StylePropertiesSnapshot {
val defaultStyle = BarChartDefaults.style()
val normalizedBarColors =
styleState.barColors?.let { colors ->
normalizeColorCount(colors = colors, targetCount = seriesCount)
}
val currentStyle =
BarChartDefaults.style(
barColor = styleState.barColor ?: defaultStyle.barColor,
barColors = normalizedBarColors ?: defaultStyle.barColors,
barAlpha = styleState.barAlpha ?: defaultStyle.barAlpha,
gridVisible = styleState.gridVisible ?: defaultStyle.gridVisible,
axisVisible = styleState.axisVisible ?: defaultStyle.axisVisible,
Expand All @@ -23,3 +31,11 @@ fun barStylePropertiesSnapshot(styleState: BarStyleState): StylePropertiesSnapsh
defaults = defaultStyle.getProperties(),
)
}

private fun normalizeColorCount(
colors: List<androidx.compose.ui.graphics.Color>,
targetCount: Int,
): List<androidx.compose.ui.graphics.Color> {
if (targetCount <= 0 || colors.isEmpty()) return emptyList()
return List(targetCount) { index -> colors[index % colors.size] }
}
1 change: 1 addition & 0 deletions playground/src/jsMain/kotlin/model/PlaygroundModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ data class MultiLineStyleState(

data class BarStyleState(
val barColor: Color? = null,
val barColors: List<Color>? = null,
val barAlpha: Float? = null,
val gridVisible: Boolean? = null,
val axisVisible: Boolean? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ internal object BarChartDefinition : PlaygroundChartDefinition {
read = { style -> (style as BarStyleState).barColor },
write = { style, value -> (style as BarStyleState).copy(barColor = value) },
),
SettingDescriptor.ColorPalette(
id = "barColors",
title = "Bar Colors",
itemCount = {
val data = it.appliedData as PlaygroundDataModel.SimpleSeries
data.values.size
},
read = { style -> (style as BarStyleState).barColors },
write = { style, value -> (style as BarStyleState).copy(barColors = value) },
),
SettingDescriptor.Divider,
SettingDescriptor.Section("Visibility"),
SettingDescriptor.Toggle(
Expand Down Expand Up @@ -140,9 +150,14 @@ internal object BarChartDefinition : PlaygroundChartDefinition {
labels = data.labels,
)
val defaultStyle = BarChartDefaults.style()
val normalizedBarColors =
styleState.barColors?.let { colors ->
normalizeColorCount(colors = colors, targetCount = data.values.size)
}
val style =
BarChartDefaults.style(
barColor = styleState.barColor ?: defaultStyle.barColor,
barColors = normalizedBarColors ?: defaultStyle.barColors,
barAlpha = styleState.barAlpha ?: defaultStyle.barAlpha,
gridVisible = styleState.gridVisible ?: defaultStyle.gridVisible,
axisVisible = styleState.axisVisible ?: defaultStyle.axisVisible,
Expand Down Expand Up @@ -170,7 +185,7 @@ internal object BarChartDefinition : PlaygroundChartDefinition {
points = points,
title = session.title,
style = style,
styleProperties = barStylePropertiesSnapshot(style),
styleProperties = barStylePropertiesSnapshot(style, data.values.size),
codegenMode = session.codegenMode,
functionName = deriveFunctionName(session.title, type),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codegen.bar

import androidx.compose.ui.graphics.Color
import model.BarCodegenConfig
import model.CodegenMode
import model.PieSliceInput
Expand Down Expand Up @@ -75,4 +76,27 @@ class BarChartCodeGeneratorTest {
assertTrue(snippet.code.contains("axisVisible = true,"))
assertTrue(snippet.code.contains("selectionLineWidth = 1.5f,"))
}

@Test
fun style_with_bar_colors_emits_palette_and_color_import() {
val snippet =
generator.generate(
BarCodegenConfig(
points =
listOf(
PieSliceInput(label = "A", valueText = "1"),
PieSliceInput(label = "B", valueText = "2"),
),
styleProperties =
StylePropertiesSnapshot(
current = listOf("barColors" to listOf(Color.Red, Color.Green)),
defaults = listOf("barColors" to emptyList<Color>()),
),
codegenMode = CodegenMode.MINIMAL,
),
)

assertTrue(snippet.code.contains("import androidx.compose.ui.graphics.Color"))
assertTrue(snippet.code.contains("barColors = listOf(Color("))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codegen

import androidx.compose.ui.graphics.Color
import codegen.area.AreaChartCodeGenerator
import codegen.bar.BarChartCodeGenerator
import codegen.line.LineChartCodeGenerator
Expand All @@ -17,6 +18,7 @@ import model.PieCodegenConfig
import model.PieSliceInput
import model.RadarCodegenConfig
import model.StackedBarCodegenConfig
import model.StylePropertiesSnapshot
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -59,6 +61,17 @@ class GeneratedSnippetCompilationTest {
PieSliceInput(label = "Mon", valueText = "12"),
PieSliceInput(label = "Tue", valueText = "18"),
),
styleProperties =
StylePropertiesSnapshot(
current =
listOf(
"barColors" to listOf(Color.Red, Color.Green),
),
defaults =
listOf(
"barColors" to emptyList<Color>(),
),
),
),
).code,
MultiLineChartCodeGenerator()
Expand Down
Loading