Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
Add horizontal line and text chart layers
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jun 9, 2023
1 parent 20272af commit 38e9358
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ buildscript {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0'
}
ext.groupId = 'com.kylecorry.ceres'
ext.versionName = '0.3.4'
ext.andromedaVersion = '5.0.0-beta26'
ext.versionName = '0.3.5'
ext.andromedaVersion = '5.6.0'
ext.targetVersion = 33
ext.compileVersion = 33
}
Expand Down
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
}
}
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
}

}

0 comments on commit 38e9358

Please sign in to comment.