Skip to content

Commit

Permalink
Update CartesianLayerModels to sort entries
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickmichalik committed Dec 15, 2023
1 parent fc21bb5 commit e7b9317
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 32 deletions.
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 @@ -16,12 +16,8 @@

package com.patrykandpatrick.vico.core.extension

/**
* Calls the [selector] function for each value in the collection and returns the range of the produced values.
*/
public inline fun <T> Iterable<T>.rangeOfOrNull(selector: (T) -> Float): ClosedFloatingPointRange<Float>? {
internal inline fun <T> Iterable<T>.rangeOf(selector: (T) -> Float): ClosedFloatingPointRange<Float> {
val iterator = iterator()
if (!iterator.hasNext()) return null
var minValue = selector(iterator.next())
var maxValue = minValue
while (iterator.hasNext()) {
Expand All @@ -32,14 +28,8 @@ public inline fun <T> Iterable<T>.rangeOfOrNull(selector: (T) -> Float): ClosedF
return minValue..maxValue
}

/**
* Calls the [selector] function for each value in the collection and returns the range of the produced values.
*/
public inline fun <T> Iterable<T>.rangeOfPairOrNull(
selector: (T) -> Pair<Float, Float>,
): ClosedFloatingPointRange<Float>? {
internal inline fun <T> Iterable<T>.rangeOfPair(selector: (T) -> Pair<Float, Float>): ClosedFloatingPointRange<Float> {
val iterator = iterator()
if (!iterator.hasNext()) return null
var (minValue, maxValue) = selector(iterator.next())
while (iterator.hasNext()) {
val (negValue, posValue) = selector(iterator.next())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ internal fun <T : Comparable<T>> T.isBoundOf(range: ClosedFloatingPointRange<T>)

internal fun ClosedFloatingPointRange<Float>.random(): Float = start + (endInclusive - start) * Random.nextFloat()

internal val ClosedFloatingPointRange<Float>?.orZero: ClosedFloatingPointRange<Float>
get() = this ?: 0f..0f

/**
* Half of this value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
package com.patrykandpatrick.vico.core.model

import com.patrykandpatrick.vico.core.chart.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.core.extension.orZero
import com.patrykandpatrick.vico.core.extension.rangeOfOrNull
import com.patrykandpatrick.vico.core.extension.rangeOfPairOrNull
import com.patrykandpatrick.vico.core.extension.rangeOf
import com.patrykandpatrick.vico.core.extension.rangeOfPair

/**
* Stores a [ColumnCartesianLayer]’s data.
Expand Down Expand Up @@ -57,12 +56,17 @@ public class ColumnCartesianLayerModel : CartesianLayerModel {
public constructor(series: List<List<Entry>>) : this(series, ExtraStore.empty)

private constructor(series: List<List<Entry>>, extraStore: ExtraStore) {
val entries = series.flatten()
val xRange = entries.rangeOfOrNull { it.x }.orZero
val yRange = entries.rangeOfOrNull { it.y }.orZero
require(series.isNotEmpty()) { "At least one series should be added." }
this.series =
series.map { entries ->
require(entries.isNotEmpty()) { "Series can’t be empty." }
entries.sortedBy { entry -> entry.x }
}
val entries = this.series.flatten()
val xRange = this.series.rangeOfPair { it.first().x to it.last().x }
val yRange = entries.rangeOf { it.y }
val aggregateYRange = entries.getAggregateYRange()
this.series = series
this.id = series.hashCode()
this.id = this.series.hashCode()
this.minX = xRange.start
this.maxX = xRange.endInclusive
this.minY = yRange.start
Expand Down Expand Up @@ -186,4 +190,4 @@ internal fun Iterable<ColumnCartesianLayerModel.Entry>.getAggregateYRange() =
val (negativeY, positiveY) = map.getOrElse(entry.x) { 0f to 0f }
map[entry.x] = if (entry.y < 0f) negativeY + entry.y to positiveY else negativeY to positiveY + entry.y
map
}.values.rangeOfPairOrNull { it }.orZero
}.values.rangeOfPair { it }
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.patrykandpatrick.vico.core.model

import com.patrykandpatrick.vico.core.chart.layer.LineCartesianLayer
import com.patrykandpatrick.vico.core.extension.orZero
import com.patrykandpatrick.vico.core.extension.rangeOfOrNull
import com.patrykandpatrick.vico.core.extension.rangeOf
import com.patrykandpatrick.vico.core.extension.rangeOfPair

/**
* Stores a [LineCartesianLayer]’s data.
Expand Down Expand Up @@ -46,11 +46,16 @@ public class LineCartesianLayerModel : CartesianLayerModel {
public constructor(series: List<List<Entry>>) : this(series, ExtraStore.empty)

private constructor(series: List<List<Entry>>, extraStore: ExtraStore) {
val entries = series.flatten()
val xRange = entries.rangeOfOrNull { it.x }.orZero
val yRange = entries.rangeOfOrNull { it.y }.orZero
this.series = series
this.id = series.hashCode()
require(series.isNotEmpty()) { "At least one series should be added." }
this.series =
series.map { entries ->
require(entries.isNotEmpty()) { "Series can’t be empty." }
entries.sortedBy { entry -> entry.x }
}
val entries = this.series.flatten()
val xRange = this.series.rangeOfPair { it.first().x to it.last().x }
val yRange = entries.rangeOf { it.y }
this.id = this.series.hashCode()
this.minX = xRange.start
this.maxX = xRange.endInclusive
this.minY = yRange.start
Expand Down

0 comments on commit e7b9317

Please sign in to comment.