Skip to content

Commit

Permalink
Make CandlestickCartesianLayerModel-related improvements (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalik committed Mar 21, 2024
1 parent 92d6557 commit 513e69b
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import com.patrykandpatrick.vico.compose.cartesian.axis.rememberStartAxis
import com.patrykandpatrick.vico.compose.cartesian.layer.rememberCandlestickCartesianLayer
import com.patrykandpatrick.vico.compose.cartesian.layer.rememberHollow
import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.core.cartesian.SampleCandlestickEntryProvider
import com.patrykandpatrick.vico.core.cartesian.layer.CandlestickCartesianLayer
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel
import com.patrykandpatrick.vico.core.cartesian.model.CartesianChartModel

@Preview(widthDp = 350)
Expand All @@ -41,7 +41,17 @@ fun CandlestickLinePreview() {
startAxis = rememberStartAxis(),
bottomAxis = rememberBottomAxis(),
),
model = remember { CartesianChartModel(SampleCandlestickEntryProvider.sampleModel) },
model =
remember {
CartesianChartModel(
CandlestickCartesianLayerModel.build(
low = listOf(2, 6, 10, 8, 6, 10),
high = listOf(10, 14, 16, 14, 18, 18),
open = listOf(4, 8, 12, 14, 10, 16),
close = listOf(8, 12, 14, 10, 16, 12),
),
)
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,39 +52,42 @@ public object RandomCartesianModelGenerator {
openCloseRange: ClosedFloatingPointRange<Float> = defaultOpenCloseRange,
lowHighRange: ClosedFloatingPointRange<Float> = defaultLowHighRange,
): CandlestickCartesianLayerModel.Partial {
var previousClose: Float? = null
var previousOpen: Float? = null
val entries = mutableListOf<CandlestickCartesianLayerModel.Entry>()
var previousCloseValue: Float? = null
var previousOpenValue: Float? = null
val low = mutableListOf<Float>()
val high = mutableListOf<Float>()
val open = mutableListOf<Float>()
val close = mutableListOf<Float>()
for (i in x) {
val isIncreasing = Random.nextBoolean()
val open: Float
val close: Float
val openValue: Float
val closeValue: Float
if (isIncreasing) {
open =
if (previousOpen != null && previousClose != null) {
floatArrayOf(previousOpen, previousClose).random().coerceIn(openCloseRange)
openValue =
if (previousOpenValue != null && previousCloseValue != null) {
floatArrayOf(previousOpenValue, previousCloseValue).random().coerceIn(openCloseRange)
} else {
y.random().coerceIn(openCloseRange)
}
close = (open + lowHighRange.random())
closeValue = (openValue + lowHighRange.random())
} else {
close =
if (previousOpen != null && previousClose != null) {
floatArrayOf(previousOpen, previousClose).random().coerceIn(openCloseRange)
closeValue =
if (previousOpenValue != null && previousCloseValue != null) {
floatArrayOf(previousOpenValue, previousCloseValue).random().coerceIn(openCloseRange)
} else {
y.random().coerceIn(openCloseRange)
}
open = (close + lowHighRange.random())
openValue = (closeValue + lowHighRange.random())
}
val low = minOf(open, close) - lowHighRange.random()
val high = maxOf(open, close) + lowHighRange.random()
low += minOf(openValue, closeValue) - lowHighRange.random()
high += maxOf(openValue, closeValue) + lowHighRange.random()
open += openValue
close += closeValue

entries.add(CandlestickCartesianLayerModel.Entry(i.toFloat(), low, high, open, close))

previousOpen = open
previousClose = close
previousOpenValue = openValue
previousCloseValue = closeValue
}
return CandlestickCartesianLayerModel.partial(entries)
return CandlestickCartesianLayerModel.partial(x.toList(), low, high, open, close)
}

public fun getRandomModel(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import com.patrykandpatrick.vico.core.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.core.cartesian.marker.put
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerDrawingModel
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel.TypedEntry.Type
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel.TypedEntry.Type.Change
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel.Entry.Type
import com.patrykandpatrick.vico.core.cartesian.model.CandlestickCartesianLayerModel.Entry.Type.Change
import com.patrykandpatrick.vico.core.cartesian.model.forEachInIndexed
import com.patrykandpatrick.vico.core.cartesian.values.ChartValues
import com.patrykandpatrick.vico.core.cartesian.values.MutableChartValues
Expand Down Expand Up @@ -198,7 +198,7 @@ public open class CandlestickCartesianLayer(
}

private fun updateMarkerLocationMap(
entry: CandlestickCartesianLayerModel.TypedEntry,
entry: CandlestickCartesianLayerModel.Entry,
entryX: Float,
entryY: Float,
entryIndex: Int,
Expand Down
Loading

0 comments on commit 513e69b

Please sign in to comment.