Skip to content

Commit

Permalink
支持对某个KEntity进行修改
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Nov 20, 2021
1 parent c627a46 commit f2e7f69
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
}
}

if (config.modifyKEntitiesFlag) {
config.modifyKEntitiesFlag = false
onKEntitiesChangedListeners.forEach {
it.onModifyKEntities()
}
}

if (config.appendKEntitiesFlag) {
config.appendKEntitiesFlag = false
onKEntitiesChangedListeners.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class StockChartConfig {
setKEntities(value, 0, kEntities.size - 1)
}

var setKEntitiesFlag = false
internal var setKEntitiesFlag = false

var appendKEntitiesFlag = false
internal var modifyKEntitiesFlag = false

internal var appendKEntitiesFlag = false

// 初始显示区域的起始坐标
var showStartIndex = 0
Expand Down Expand Up @@ -155,13 +157,11 @@ class StockChartConfig {
* @param showStartIndex 显示区域起点对应数据集合的下标
* @param showEndIndex 显示区域终点对应数据集合的下标
*/
@UiThread
fun setKEntities(
kEntities: List<IKEntity>,
showStartIndex: Int = 0,
showEndIndex: Int = if (kEntities.isEmpty()) 0 else kEntities.size - 1
) {
checkMainThread()
check(showStartIndex <= showEndIndex) { "The value of showStartIndex must be less than showEndIndex." }
if (kEntities.isNotEmpty()) {
check(showStartIndex in kEntities.indices && showEndIndex in kEntities.indices) { "The value of showStartIndex and showEndIndex must be in the range of kEntities indexes." }
Expand All @@ -173,12 +173,21 @@ class StockChartConfig {
setKEntitiesFlag = true
}

/**
* 修改K线数据
* @param index 对应下标
* @param kEntity 新数据
*/
fun modifyKEntity(index: Int, kEntity: IKEntity) {
check(index in 0 until kEntities.size) { "Index $index out of bounds for length ${kEntities.size}" }
kEntities[index] = kEntity
modifyKEntitiesFlag = true
}

/**
* 追加K线数据
*/
@UiThread
fun appendRightKEntities(kEntities: List<IKEntity>) {
checkMainThread()
if (this.kEntities.isEmpty()) {
setKEntities(kEntities)
} else {
Expand All @@ -190,9 +199,7 @@ class StockChartConfig {
/**
* 追加K线数据
*/
@UiThread
fun appendLeftKEntities(kEntities: List<IKEntity>) {
checkMainThread()
if (this.kEntities.isEmpty()) {
setKEntities(kEntities)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo
prepare()
}

override fun onModifyKEntities() {
onKEntitiesChanged()
prepare()
}

override fun onAppendKEntities() {
onKEntitiesChanged()
prepare()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ package com.github.wangyiqian.stockchart.entities
* @author wangyiqian E-mail: [email protected]
* @version 创建时间: 2021/2/6
*/
class Highlight(
open class Highlight(
x: Float = 0f, y: Float = 0f, valueX: Float = 0f, valueY: Float = 0f
) : GestureEvent(x, y, valueX, valueY)
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ package com.github.wangyiqian.stockchart.entities
*/
interface IKEntity {
fun getHighPrice(): Float
fun setHighPrice(price: Float)
fun getLowPrice(): Float
fun setLowPrice(price: Float)
fun getOpenPrice(): Float
fun setOpenPrice(price: Float)
fun getClosePrice(): Float
fun setClosePrice(price: Float)
fun getVolume(): Long
fun setVolume(volume: Long)
fun getTime(): Long
fun setTime(time: Long)

/**
* 分时均线价格
*/
fun getAvgPrice(): Float?
fun setAvgPrice(price: Float?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,54 @@ package com.github.wangyiqian.stockchart.entities
* @version 创建时间: 2021/1/28
*/
open class KEntity(
private val highPrice: Float,
private val lowPrice: Float,
private val openPrice: Float,
private val closePrice: Float,
private val volume: Long,
private val time: Long,
private val avgPrice: Float? = null
private var highPrice: Float,
private var lowPrice: Float,
private var openPrice: Float,
private var closePrice: Float,
private var volume: Long,
private var time: Long,
private var avgPrice: Float? = null
) : IKEntity {

override fun getHighPrice() = highPrice

override fun setHighPrice(price: Float) {
this.highPrice = price
}

override fun getLowPrice() = lowPrice

override fun setLowPrice(price: Float) {
this.lowPrice = price
}

override fun getOpenPrice() = openPrice

override fun setOpenPrice(price: Float) {
this.openPrice = price
}

override fun getClosePrice() = closePrice

override fun setClosePrice(price: Float) {
this.closePrice = price
}

override fun getVolume() = volume

override fun setVolume(volume: Long) {
this.volume = volume
}

override fun getTime() = time

override fun setTime(time: Long) {
this.time = time
}

override fun getAvgPrice() = avgPrice

override fun setAvgPrice(price: Float?) {
this.avgPrice = price
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,47 @@ package com.github.wangyiqian.stockchart.entities
* @author wangyiqian E-mail: [email protected]
* @version 创建时间: 2021/2/22
*/
class KEntityOfLineStarter(private val kEntity: IKEntity) : IKEntity {
open class KEntityOfLineStarter(private val kEntity: IKEntity) : IKEntity {

override fun getHighPrice() = kEntity.getHighPrice()

override fun setHighPrice(price: Float) {
kEntity.setHighPrice(price)
}

override fun getLowPrice() = kEntity.getLowPrice()

override fun setLowPrice(price: Float) {
kEntity.setLowPrice(price)
}

override fun getOpenPrice() = kEntity.getOpenPrice()

override fun setOpenPrice(price: Float) {
kEntity.setOpenPrice(price)
}

override fun getClosePrice() = kEntity.getClosePrice()

override fun setClosePrice(price: Float) {
kEntity.setClosePrice(price)
}

override fun getVolume() = kEntity.getVolume()

override fun setVolume(volume: Long) {
kEntity.setVolume(volume)
}

override fun getTime() = kEntity.getTime()

override fun setTime(time: Long) {
kEntity.setTime(time)
}

override fun getAvgPrice() = kEntity.getAvgPrice()

override fun setAvgPrice(price: Float?) {
kEntity.setAvgPrice(price)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ package com.github.wangyiqian.stockchart.listener
*/
interface OnKEntitiesChangedListener {
fun onSetKEntities()
fun onModifyKEntities()
fun onAppendKEntities()
}

0 comments on commit f2e7f69

Please sign in to comment.