This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add horizontal line and text chart layers
- Loading branch information
1 parent
20272af
commit 38e9358
Showing
3 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
chart/src/main/java/com/kylecorry/ceres/chart/data/HorizontalLineChartLayer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.kylecorry.ceres.chart.data | ||
|
||
import androidx.annotation.ColorInt | ||
import com.kylecorry.andromeda.canvas.ICanvasDrawer | ||
import com.kylecorry.andromeda.core.units.PixelCoordinate | ||
import com.kylecorry.ceres.chart.IChart | ||
import com.kylecorry.ceres.chart.data.ChartLayer | ||
import com.kylecorry.sol.math.Vector2 | ||
|
||
class HorizontalLineChartLayer( | ||
initialValue: Float, | ||
@ColorInt initialColor: Int, | ||
private val thickness: Float = 2f, | ||
) : ChartLayer { | ||
|
||
@ColorInt | ||
var color: Int = initialColor | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
var value: Float = initialValue | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
override var data: List<Vector2> = emptyList() | ||
|
||
override var hasChanges: Boolean = false | ||
private set | ||
|
||
override fun draw(drawer: ICanvasDrawer, chart: IChart) { | ||
val left = chart.toPixel(Vector2(chart.xRange.start, value)) | ||
val right = chart.toPixel(Vector2(chart.xRange.end, value)) | ||
|
||
drawer.strokeWeight(thickness) | ||
drawer.stroke(color) | ||
drawer.line(left.x, left.y, right.x, right.y) | ||
|
||
// Reset the opacity | ||
drawer.opacity(255) | ||
|
||
hasChanges = false | ||
} | ||
|
||
override fun onClick(drawer: ICanvasDrawer, chart: IChart, pixel: PixelCoordinate): Boolean { | ||
return false | ||
} | ||
|
||
override fun invalidate() { | ||
hasChanges = true | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
chart/src/main/java/com/kylecorry/ceres/chart/data/TextChartLayer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.kylecorry.ceres.chart.data | ||
|
||
import androidx.annotation.ColorInt | ||
import com.kylecorry.andromeda.canvas.ICanvasDrawer | ||
import com.kylecorry.andromeda.canvas.TextMode | ||
import com.kylecorry.andromeda.core.units.PixelCoordinate | ||
import com.kylecorry.ceres.chart.IChart | ||
import com.kylecorry.ceres.chart.data.ChartLayer | ||
import com.kylecorry.sol.math.Vector2 | ||
|
||
class TextChartLayer( | ||
initialText: String, | ||
initialPosition: Vector2, | ||
@ColorInt initialColor: Int, | ||
initialSize: Float = 12f, | ||
private val verticalAlign: TextVerticalPosition = TextVerticalPosition.Center, | ||
private val horizontalAlign: TextHorizontalPosition = TextHorizontalPosition.Center | ||
) : ChartLayer { | ||
|
||
@ColorInt | ||
var color: Int = initialColor | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
var position: Vector2 = initialPosition | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
var text: String = initialText | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
var size: Float = initialSize | ||
set(value) { | ||
field = value | ||
invalidate() | ||
} | ||
|
||
override var data: List<Vector2> = emptyList() | ||
|
||
override var hasChanges: Boolean = false | ||
private set | ||
|
||
override fun draw(drawer: ICanvasDrawer, chart: IChart) { | ||
val chartPosition = chart.toPixel(position) | ||
|
||
drawer.textSize(drawer.sp(size)) | ||
drawer.noStroke() | ||
drawer.fill(color) | ||
drawer.textMode(TextMode.Corner) | ||
val tWidth = drawer.textWidth(text) | ||
val tHeight = drawer.textHeight(text) | ||
|
||
val x = when (horizontalAlign) { | ||
TextHorizontalPosition.Left -> chartPosition.x | ||
TextHorizontalPosition.Center -> chartPosition.x - tWidth / 2 | ||
TextHorizontalPosition.Right -> chartPosition.x - tWidth | ||
} | ||
|
||
val y = when (verticalAlign) { | ||
TextVerticalPosition.Top -> chartPosition.y + tHeight | ||
TextVerticalPosition.Center -> chartPosition.y + tHeight / 2 | ||
TextVerticalPosition.Bottom -> chartPosition.y | ||
} | ||
|
||
drawer.text(text, x, y) | ||
|
||
// Reset the opacity | ||
drawer.opacity(255) | ||
|
||
hasChanges = false | ||
} | ||
|
||
override fun onClick(drawer: ICanvasDrawer, chart: IChart, pixel: PixelCoordinate): Boolean { | ||
return false | ||
} | ||
|
||
override fun invalidate() { | ||
hasChanges = true | ||
} | ||
|
||
enum class TextVerticalPosition { | ||
Top, Center, Bottom | ||
} | ||
|
||
enum class TextHorizontalPosition { | ||
Left, Center, Right | ||
} | ||
|
||
} |