Skip to content

Commit

Permalink
Replace FillStyle usages with DynamicShader
Browse files Browse the repository at this point in the history
  • Loading branch information
Gowsky committed Nov 22, 2023
1 parent 4b216fa commit 09ac906
Show file tree
Hide file tree
Showing 15 changed files with 286 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ import com.patrykandpatrick.vico.compose.axis.axisLineComponent
import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.fill.solid
import com.patrykandpatrick.vico.compose.chart.fill.split
import com.patrykandpatrick.vico.compose.chart.layout.fullWidth
import com.patrykandpatrick.vico.compose.chart.line.lineChart
import com.patrykandpatrick.vico.compose.chart.line.lineSpec
import com.patrykandpatrick.vico.compose.component.shape.shader.solid
import com.patrykandpatrick.vico.compose.component.shape.shader.split
import com.patrykandpatrick.vico.compose.component.textComponent
import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.chart.fill.FillStyle
import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout
import com.patrykandpatrick.vico.core.chart.values.AxisValuesOverrider
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.entry.entryModelOf
import com.patrykandpatrick.vico.sample.showcase.rememberMarker

Expand All @@ -53,7 +53,7 @@ public fun SingleLineChartWithNegativeValues() {
chart = lineChart(
lines = listOf(
lineSpec(
lineFill = FillStyle.split(
lineShader = DynamicShaders.split(
positiveColor = Color(0xFF25BE53),
negativeColor = Color(0xFFE73B3B),
),
Expand Down Expand Up @@ -86,7 +86,9 @@ public fun SingleLineChartWithNegativeValuesAndDataLabels() {
Surface {
Chart(
chart = lineChart(
lines = listOf(lineSpec(lineFill = FillStyle.solid(Color.DarkGray), dataLabel = textComponent())),
lines = listOf(
lineSpec(lineShader = DynamicShaders.solid(Color.DarkGray), dataLabel = textComponent()),
),
),
model = model,
startAxis = rememberStartAxis(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,25 @@ import com.patrykandpatrick.vico.compose.axis.axisLabelComponent
import com.patrykandpatrick.vico.compose.axis.horizontal.rememberBottomAxis
import com.patrykandpatrick.vico.compose.axis.vertical.rememberStartAxis
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.fill.split
import com.patrykandpatrick.vico.compose.chart.fill.splitShader
import com.patrykandpatrick.vico.compose.chart.layout.fullWidth
import com.patrykandpatrick.vico.compose.chart.line.lineChart
import com.patrykandpatrick.vico.compose.chart.line.lineSpec
import com.patrykandpatrick.vico.compose.component.lineComponent
import com.patrykandpatrick.vico.compose.component.shape.dashedShape
import com.patrykandpatrick.vico.compose.component.shape.shader.fromComponent
import com.patrykandpatrick.vico.compose.component.shape.shader.split
import com.patrykandpatrick.vico.compose.component.shape.shader.verticalGradient
import com.patrykandpatrick.vico.compose.component.shapeComponent
import com.patrykandpatrick.vico.compose.dimensions.dimensionsOf
import com.patrykandpatrick.vico.compose.style.ProvideChartStyle
import com.patrykandpatrick.vico.core.axis.Axis
import com.patrykandpatrick.vico.core.axis.AxisItemPlacer
import com.patrykandpatrick.vico.core.chart.fill.FillStyle
import com.patrykandpatrick.vico.core.chart.layout.HorizontalLayout
import com.patrykandpatrick.vico.core.chart.line.LineChart
import com.patrykandpatrick.vico.core.component.shape.ShapeComponent
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.component.shape.shader.splitShader
import com.patrykandpatrick.vico.core.entry.ChartEntryModelProducer
import com.patrykandpatrick.vico.databinding.Chart9Binding
import com.patrykandpatrick.vico.sample.showcase.UISystem
Expand All @@ -73,8 +72,8 @@ private fun ComposeChart9(chartEntryModelProducer: ChartEntryModelProducer) {
chart = lineChart(
lines = listOf(
lineSpec(
lineFill = FillStyle.split(chartColors[0], chartColors[1]),
lineBackgroundFill = FillStyle.splitShader(
lineShader = DynamicShaders.split(chartColors[0], chartColors[1]),
lineBackgroundShader = DynamicShaders.splitShader(
DynamicShaders.composeShader(
DynamicShaders.fromComponent(
componentSize = 6.dp,
Expand Down Expand Up @@ -158,11 +157,8 @@ private fun ViewChart9(chartEntryModelProducer: ChartEntryModelProducer) {
with(chart as LineChart) {
lines = listOf(
LineChart.LineSpec(
lineFill = FillStyle.Split(
positiveColor = colors[0].toArgb(),
negativeColor = colors[1].toArgb(),
),
lineBackgroundFill = FillStyle.SplitShader(
lineShader = DynamicShaders.split(colors[0], colors[1]),
lineBackgroundShader = DynamicShaders.splitShader(
DynamicShaders.composeShader(
DynamicShaders.fromComponent(
componentSize = 6.dp,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.patrykandpatrick.vico.compose.chart.Chart
import com.patrykandpatrick.vico.compose.chart.fill.shader
import com.patrykandpatrick.vico.compose.component.shape.shader.fromBrush
import com.patrykandpatrick.vico.compose.style.currentChartStyle
import com.patrykandpatrick.vico.core.DefaultAlpha
Expand All @@ -37,7 +36,6 @@ import com.patrykandpatrick.vico.core.chart.DefaultPointConnector
import com.patrykandpatrick.vico.core.chart.column.ColumnChart
import com.patrykandpatrick.vico.core.chart.composed.ComposedChart
import com.patrykandpatrick.vico.core.chart.decoration.Decoration
import com.patrykandpatrick.vico.core.chart.fill.FillStyle
import com.patrykandpatrick.vico.core.chart.line.LineChart
import com.patrykandpatrick.vico.core.chart.line.LineChart.LineSpec
import com.patrykandpatrick.vico.core.chart.line.LineChartDrawingModel
Expand All @@ -46,6 +44,8 @@ import com.patrykandpatrick.vico.core.chart.values.ChartValues
import com.patrykandpatrick.vico.core.component.Component
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShader
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.component.shape.shader.HorizontalSplitShader
import com.patrykandpatrick.vico.core.component.shape.shader.SolidShader
import com.patrykandpatrick.vico.core.component.text.TextComponent
import com.patrykandpatrick.vico.core.component.text.VerticalPosition
import com.patrykandpatrick.vico.core.entry.ChartEntryModel
Expand Down Expand Up @@ -129,9 +129,9 @@ public fun lineSpec(
dataLabelRotationDegrees: Float = 0f,
pointConnector: LineSpec.PointConnector = DefaultPointConnector(),
): LineSpec = LineSpec(
lineFill = FillStyle.Solid(lineColor.toArgb()),
lineShader = SolidShader(lineColor.toArgb()),
lineThicknessDp = lineThickness.value,
lineBackgroundFill = lineBackgroundShader?.let(FillStyle::shader),
lineBackgroundShader = lineBackgroundShader,
lineCap = lineCap.paintCap,
point = point,
pointSizeDp = pointSize.value,
Expand All @@ -145,9 +145,9 @@ public fun lineSpec(
/**
* Creates a [LineChart.LineSpec] for use in [LineChart]s.
*
* @param lineFill the [FillStyle] for the line.
* @param lineShader the [DynamicShader] for the line.
* @param lineThickness the thickness of the line.
* @param lineBackgroundFill an optional [FillStyle] to use for the area below and above the line.
* @param lineBackgroundShader an optional [DynamicShader] to use for the area below and above the line.
* @param lineCap the stroke cap for the line.
* @param point an optional [Component] that can be drawn at a given point on the line.
* @param pointSize the size of the [point].
Expand All @@ -161,9 +161,9 @@ public fun lineSpec(
* @see LineChart.LineSpec
*/
public fun lineSpec(
lineFill: FillStyle,
lineShader: DynamicShader,
lineThickness: Dp = DefaultDimens.LINE_THICKNESS.dp,
lineBackgroundFill: FillStyle? = lineFill.getBackwardCompatibleBackgroundFill(),
lineBackgroundShader: DynamicShader? = lineShader.getBackwardCompatibleBackgroundShader(),
lineCap: StrokeCap = StrokeCap.Round,
point: Component? = null,
pointSize: Dp = DefaultDimens.POINT_SIZE.dp,
Expand All @@ -173,9 +173,9 @@ public fun lineSpec(
dataLabelRotationDegrees: Float = 0f,
pointConnector: LineSpec.PointConnector = DefaultPointConnector(),
): LineSpec = LineSpec(
lineFill = lineFill,
lineShader = lineShader,
lineThicknessDp = lineThickness.value,
lineBackgroundFill = lineBackgroundFill,
lineBackgroundShader = lineBackgroundShader,
lineCap = lineCap.paintCap,
point = point,
pointSizeDp = pointSize.value,
Expand All @@ -186,18 +186,18 @@ public fun lineSpec(
pointConnector = pointConnector,
)

private fun FillStyle.getBackwardCompatibleBackgroundFill(): FillStyle? =
private fun DynamicShader.getBackwardCompatibleBackgroundShader(): DynamicShader? =
when (this) {
is FillStyle.Solid -> FillStyle.SplitShader(
positiveShader = DynamicShaders.fromBrush(
is SolidShader -> HorizontalSplitShader.Double(
topShader = DynamicShaders.fromBrush(
brush = Brush.verticalGradient(
listOf(
Color(color).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_START),
Color(color).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
),
),
),
negativeShader = DynamicShaders.fromBrush(
bottomShader = DynamicShaders.fromBrush(
brush = Brush.verticalGradient(
listOf(
Color(color).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
Expand All @@ -207,20 +207,20 @@ private fun FillStyle.getBackwardCompatibleBackgroundFill(): FillStyle? =
),
)

is FillStyle.Split -> FillStyle.SplitShader(
positiveShader = DynamicShaders.fromBrush(
is HorizontalSplitShader.Solid -> HorizontalSplitShader.Double(
topShader = DynamicShaders.fromBrush(
brush = Brush.verticalGradient(
listOf(
Color(positiveColor).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_START),
Color(positiveColor).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
Color(colorTop).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_START),
Color(colorTop).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
),
),
),
negativeShader = DynamicShaders.fromBrush(
bottomShader = DynamicShaders.fromBrush(
brush = Brush.verticalGradient(
listOf(
Color(negativeColor).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
Color(negativeColor).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_START),
Color(colorBottom).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_END),
Color(colorBottom).copy(alpha = DefaultAlpha.LINE_BACKGROUND_SHADER_START),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 by Patryk Goworowski and Patrick Michalik.
* Copyright 2023 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,10 +18,15 @@ package com.patrykandpatrick.vico.compose.component.shape.shader

import android.graphics.Shader
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.Dp
import com.patrykandpatrick.vico.core.component.Component
import com.patrykandpatrick.vico.core.component.shape.shader.ComponentShader
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShader
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.component.shape.shader.HorizontalSplitShader
import com.patrykandpatrick.vico.core.component.shape.shader.SolidShader

/**
* Creates a [ComponentShader] out of the provided [component].
Expand Down Expand Up @@ -52,3 +57,15 @@ public fun DynamicShaders.fromComponent(
* @see BrushShader
*/
public fun DynamicShaders.fromBrush(brush: Brush): BrushShader = BrushShader(brush)

/**
* Creates a [DynamicShader] that fills the area with a solid color.
*/
public fun DynamicShaders.solid(color: Color): SolidShader = SolidShader(color.toArgb())

/**
* Creates a [DynamicShader] with split colors. The positive color is used for values above the zero line,
* and the negative color is used for values below the zero line.
*/
public fun DynamicShaders.split(positiveColor: Color, negativeColor: Color): HorizontalSplitShader.Solid =
HorizontalSplitShader.Solid(positiveColor.toArgb(), negativeColor.toArgb())
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.patrykandpatrick.vico.compose.chart.fill.solid
import com.patrykandpatrick.vico.compose.chart.line.lineSpec
import com.patrykandpatrick.vico.compose.component.shape.dashedShape
import com.patrykandpatrick.vico.compose.component.shape.shader.solid
import com.patrykandpatrick.vico.core.DefaultColors
import com.patrykandpatrick.vico.core.DefaultDimens
import com.patrykandpatrick.vico.core.axis.AxisPosition
import com.patrykandpatrick.vico.core.axis.formatter.AxisValueFormatter
import com.patrykandpatrick.vico.core.axis.formatter.DecimalFormatAxisValueFormatter
import com.patrykandpatrick.vico.core.chart.column.ColumnChart.MergeMode
import com.patrykandpatrick.vico.core.chart.fill.FillStyle
import com.patrykandpatrick.vico.core.chart.line.LineChart.LineSpec
import com.patrykandpatrick.vico.core.component.shape.LineComponent
import com.patrykandpatrick.vico.core.component.shape.Shape
import com.patrykandpatrick.vico.core.component.shape.ShapeComponent
import com.patrykandpatrick.vico.core.component.shape.Shapes
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShaders
import com.patrykandpatrick.vico.core.component.text.TextComponent
import com.patrykandpatrick.vico.core.component.text.VerticalPosition
import com.patrykandpatrick.vico.core.formatter.DecimalFormatValueFormatter
Expand Down Expand Up @@ -209,7 +209,7 @@ public data class ChartStyle(
lineChart = LineChart(
lines = entityColors.map { entityColor ->
lineSpec(
lineFill = FillStyle.solid(entityColor),
lineShader = DynamicShaders.solid(entityColor),
)
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.patrykandpatrick.vico.core.chart

import android.graphics.Paint
import com.patrykandpatrick.vico.core.chart.fill.FillStyle
import com.patrykandpatrick.vico.core.chart.line.LineChart
import com.patrykandpatrick.vico.core.component.Component
import com.patrykandpatrick.vico.core.component.shape.shader.DynamicShader
Expand Down Expand Up @@ -65,9 +64,9 @@ public fun LineChart.LineSpec.copy(
* Creates a new [LineChart.LineSpec] based on this one, updating select properties.
*/
@Deprecated(
message = "`lineColor` and `lineBackgroundShader` are deprecated. Use `lineFill` and `lineBackgroundFill`",
message = "`lineColor` is deprecated. Use `lineShader`",
replaceWith = ReplaceWith(
"copy(lineFill = lineFill, lineBackgroundFill = lineBackgroundFill)",
"copy(lineShader = lineShader)",
"com.patrykandpatrick.vico.core.chart.fill.FillStyle",
),
)
Expand Down Expand Up @@ -100,9 +99,9 @@ public fun LineChart.LineSpec.copy(
* Creates a new [LineChart.LineSpec] based on this one, updating select properties.
*/
public fun LineChart.LineSpec.copy(
lineFill: FillStyle = this.lineFill,
lineFill: DynamicShader = this.lineShader,
lineThicknessDp: Float = this.lineThicknessDp,
lineBackgroundFill: FillStyle? = this.lineBackgroundFill,
lineBackgroundFill: DynamicShader? = this.lineBackgroundShader,
lineCap: Paint.Cap = this.lineCap,
point: Component? = this.point,
pointSizeDp: Float = this.pointSizeDp,
Expand All @@ -112,9 +111,9 @@ public fun LineChart.LineSpec.copy(
dataLabelRotationDegrees: Float = this.dataLabelRotationDegrees,
pointConnector: LineChart.LineSpec.PointConnector = this.pointConnector,
): LineChart.LineSpec = LineChart.LineSpec(
lineFill = lineFill,
lineShader = lineFill,
lineThicknessDp = lineThicknessDp,
lineBackgroundFill = lineBackgroundFill,
lineBackgroundShader = lineBackgroundFill,
lineCap = lineCap,
point = point,
pointSizeDp = pointSizeDp,
Expand Down
Loading

0 comments on commit 09ac906

Please sign in to comment.