Skip to content

Commit

Permalink
支持删除一个K线数据点
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Dec 29, 2021
1 parent c505119 commit 855413b
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 240 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ StockChart是一款适用于Android的高扩展性、高性能股票图开发库
(若图片未显示,可能需要科学上网。)

# 插一条内推广告
**阿里巴巴****老虎集团(老虎证券)** 2021各岗位内推,可发简历到我邮箱:wangyiqian9891@gmail.com。
**阿里巴巴****老虎集团(老虎证券)** 2022各岗位内推,可发简历到我邮箱:wangyiqian9891@gmail.com。

# 用法
### 先明白几个概念
Expand Down
14 changes: 0 additions & 14 deletions lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,6 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
}
}

if (config.appendKEntitiesFlag) {
config.appendKEntitiesFlag = false
onKEntitiesChangedListeners.forEach {
it.onAppendKEntities()
}
}

if (config.insertKEntitiesFlag) {
config.insertKEntitiesFlag = false
onKEntitiesChangedListeners.forEach {
it.onInsertKEntities()
}
}

checkChildViews()

invalidate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
package com.github.wangyiqian.stockchart

import androidx.annotation.ColorInt
import androidx.annotation.UiThread
import com.github.wangyiqian.stockchart.childchart.base.*
import com.github.wangyiqian.stockchart.entities.IKEntity
import com.github.wangyiqian.stockchart.listener.OnGestureListener
import com.github.wangyiqian.stockchart.listener.OnLoadMoreListener
import com.github.wangyiqian.stockchart.util.checkMainThread

/**
* @author wangyiqian E-mail: [email protected]
Expand All @@ -33,14 +31,6 @@ class StockChartConfig {
setKEntities(value, 0, kEntities.size - 1)
}

internal var setKEntitiesFlag = false

internal var modifyKEntitiesFlag = false

internal var appendKEntitiesFlag = false

internal var insertKEntitiesFlag = false

// 初始显示区域的起始坐标
var showStartIndex = 0

Expand Down Expand Up @@ -214,7 +204,7 @@ class StockChartConfig {
setKEntities(kEntities)
} else {
this.kEntities.addAll(kEntities)
appendKEntitiesFlag = true
modifyKEntitiesFlag = true
}
}

Expand All @@ -228,7 +218,7 @@ class StockChartConfig {
showStartIndex += kEntities.size
showEndIndex += kEntities.size
this.kEntities.addAll(0, kEntities)
appendKEntitiesFlag = true
modifyKEntitiesFlag = true

}
}
Expand All @@ -238,16 +228,25 @@ class StockChartConfig {
*/
fun insertKEntities(index: Int, kEntities: List<IKEntity>) {
check(index in this.kEntities.indices) { "Index $index out of bounds for length ${kEntities.size}" }
if (this.kEntities.isEmpty()) {
setKEntities(kEntities)
} else {
if(index < showEndIndex) {
showEndIndex += kEntities.size
showStartIndex += kEntities.size
}
this.kEntities.addAll(index, kEntities)
insertKEntitiesFlag = true
if(index <= showEndIndex) {
showEndIndex += kEntities.size
showStartIndex += kEntities.size
}
this.kEntities.addAll(index, kEntities)
modifyKEntitiesFlag = true
}

/**
* 删除一个K线数据点
*/
fun removeKEntity(index: Int){
check(index in this.kEntities.indices) { "Index $index out of bounds for length ${kEntities.size}" }
if(index <= showEndIndex){
showEndIndex --
showStartIndex --
}
this.kEntities.removeAt(index)
modifyKEntitiesFlag = true
}

/**
Expand Down Expand Up @@ -285,6 +284,8 @@ class StockChartConfig {
onGestureListeners.remove(listener)
}

fun getOnGestureListeners() = onGestureListeners
internal fun getOnGestureListeners() = onGestureListeners
internal var setKEntitiesFlag = false
internal var modifyKEntitiesFlag = false

}
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,6 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo
prepare()
}

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

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

private fun prepare() {
if (stockChart.getConfig().getKEntitiesSize() <= 0) return
childChartMatrixHelper?.prepare()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ open class KChart(
var yMin = 0f
var yMax = 0f

getKEntities().filterIndexed { index, kEntity -> index in startIndex..endIndex && !kEntity.containFlag(FLAG_EMPTY) }
getKEntities().filterIndexed { index, kEntity ->
index in startIndex..endIndex && !kEntity.containFlag(
FLAG_EMPTY
)
}
.apply {
when (chartConfig.kChartType) {
is KChartConfig.KChartType.CANDLE, is KChartConfig.KChartType.HOLLOW, is KChartConfig.KChartType.BAR -> {
Expand Down Expand Up @@ -451,7 +455,11 @@ open class KChart(
mapPointsReal2Value(tmp4FloatArray)
val leftIdx = (tmp4FloatArray[0] + 0.5f).toInt()
val rightIdx = (tmp4FloatArray[2] + 0.5f).toInt() - 1
getKEntities().filterIndexed { kEntityIdx, kEntity -> kEntityIdx in leftIdx..rightIdx && !kEntity.containFlag(FLAG_EMPTY) }
getKEntities().filterIndexed { kEntityIdx, kEntity ->
kEntityIdx in leftIdx..rightIdx && !kEntity.containFlag(
FLAG_EMPTY
)
}
.map { it.getHighPrice() }.max()

var maxIdx: Int? = null
Expand Down Expand Up @@ -479,41 +487,46 @@ open class KChart(
}
}

fun doDraw(idx: Int, price: Float) {
tmp2FloatArray[0] = idx + 0.5f
tmp2FloatArray[1] = price
mapPointsValue2Real(tmp2FloatArray)
val isLeft =
tmp2FloatArray[0] - getChartDisplayArea().left > (getChartDisplayArea().right - getChartDisplayArea().left) / 2
val lineLength = config.lineLength
val lineEndX =
if (isLeft) tmp2FloatArray[0] - lineLength else tmp2FloatArray[0] + lineLength
canvas.drawLine(
tmp2FloatArray[0],
tmp2FloatArray[1],
lineEndX,
tmp2FloatArray[1],
highestAndLowestLabelPaint
)
val text = "${config.formatter.invoke(price)}"
val textWidth = highestAndLowestLabelPaint.measureText(text)
val textStartX = if (isLeft) lineEndX - textWidth else lineEndX
highestAndLowestLabelPaint.getFontMetrics(tmpFontMetrics)
val baseLine =
tmp2FloatArray[1] + (tmpFontMetrics.bottom - tmpFontMetrics.top) / 2 - tmpFontMetrics.bottom
canvas.drawText(text, textStartX, baseLine, highestAndLowestLabelPaint)
}

maxIdx?.let {
doDraw(it, maxPrice)
doDrawHighestAndLowestLabel(canvas, config, it, maxPrice)
}

minIdx?.let {
doDraw(it, minPrice)
doDrawHighestAndLowestLabel(canvas, config, it, minPrice)
}
}
}

private fun doDrawHighestAndLowestLabel(
canvas: Canvas,
config: KChartConfig.HighestAndLowestLabelConfig,
idx: Int,
price: Float
) {
tmp2FloatArray[0] = idx + 0.5f
tmp2FloatArray[1] = price
mapPointsValue2Real(tmp2FloatArray)
val isLeft =
tmp2FloatArray[0] - getChartDisplayArea().left > (getChartDisplayArea().right - getChartDisplayArea().left) / 2
val lineLength = config.lineLength
val lineEndX =
if (isLeft) tmp2FloatArray[0] - lineLength else tmp2FloatArray[0] + lineLength
canvas.drawLine(
tmp2FloatArray[0],
tmp2FloatArray[1],
lineEndX,
tmp2FloatArray[1],
highestAndLowestLabelPaint
)
val text = "${config.formatter.invoke(price)}"
val textWidth = highestAndLowestLabelPaint.measureText(text)
val textStartX = if (isLeft) lineEndX - textWidth else lineEndX
highestAndLowestLabelPaint.getFontMetrics(tmpFontMetrics)
val baseLine =
tmp2FloatArray[1] + (tmpFontMetrics.bottom - tmpFontMetrics.top) / 2 - tmpFontMetrics.bottom
canvas.drawText(text, textStartX, baseLine, highestAndLowestLabelPaint)
}

private fun drawIndex(canvas: Canvas) {
drawnIndexTextHeight = 0f
if (chartConfig.index == null) {
Expand Down Expand Up @@ -564,29 +577,34 @@ open class KChart(
var left = index.textMarginLeft
val top = index.textMarginTop
indexTextPaint.getFontMetrics(tmpFontMetrics)
fun drawIndexText(text: String) {
if (!index.startText.isNullOrEmpty()) {
indexTextPaint.color = index.startTextColor
canvas.drawText(
text,
index.startText,
left,
-tmpFontMetrics.top + top,
indexTextPaint
)
left += indexTextPaint.measureText(text) + index.textSpace
left += indexTextPaint.measureText(index.startText) + index.textSpace
drawnIndexTextHeight =
tmpFontMetrics.bottom - tmpFontMetrics.top
}
if (!index.startText.isNullOrEmpty()) {
indexTextPaint.color = index.startTextColor
drawIndexText(index.startText)
}
indexList.forEachIndexed { lineIdx, pointList ->
chartConfig.indexColors?.let { indexColors ->
if (lineIdx < indexColors.size) {
indexTextPaint.color = indexColors[lineIdx]
val value =
if (indexIdx != null && indexIdx in pointList.indices && pointList[indexIdx] != null) pointList[indexIdx] else null
val text = index.textFormatter.invoke(lineIdx, value)
drawIndexText(text)
canvas.drawText(
text,
left,
-tmpFontMetrics.top + top,
indexTextPaint
)
left += indexTextPaint.measureText(text) + index.textSpace
drawnIndexTextHeight =
tmpFontMetrics.bottom - tmpFontMetrics.top
}
}
}
Expand All @@ -611,7 +629,10 @@ open class KChart(

var preIdx = -1
for (idx in getKEntities().indices) {
if (getKEntities()[idx].containFlag(FLAG_EMPTY) || getKEntities()[idx].containFlag(FLAG_LINE_STARTER)) {
if (getKEntities()[idx].containFlag(FLAG_EMPTY) || getKEntities()[idx].containFlag(
FLAG_LINE_STARTER
)
) {
if (preIdx != -1) {
tmpPath.lineTo(preIdx + 1f, getKEntities()[preIdx].getClosePrice())
tmpPath.lineTo(preIdx + 1f, yMinValue)
Expand Down Expand Up @@ -865,46 +886,45 @@ open class KChart(
}

private fun drawLabels(canvas: Canvas) {

fun doDraw(isLeft: Boolean, config: KChartConfig.LabelConfig) {
if (config.count > 0) {
labelPaint.textSize = config.textSize
labelPaint.color = config.textColor
labelPaint.getFontMetrics(tmpFontMetrics)
val labelHeight = tmpFontMetrics.bottom - tmpFontMetrics.top
val areaTop =
getChartDisplayArea().top + drawnIndexTextHeight + config.marginTop
val areaBottom = getChartDisplayArea().bottom - config.marginBottom
var verticalSpace = 0f
if (config.count > 1) {
verticalSpace =
(areaBottom - areaTop - config.count * labelHeight) / (config.count - 1)
}
var pos = areaTop
for (i in 1..config.count) {
tmp2FloatArray[0] = 0f
tmp2FloatArray[1] = pos + labelHeight / 2
mapPointsReal2Value(tmp2FloatArray)
val text = config.formatter.invoke(tmp2FloatArray[1])
val startX = if (isLeft) {
config.horizontalMargin
} else {
getChartDisplayArea().right - config.horizontalMargin - labelPaint.measureText(
text
)
}
canvas.drawText(text, startX, pos - tmpFontMetrics.top, labelPaint)
pos += verticalSpace + labelHeight
}
}
}

chartConfig.leftLabelConfig?.let { config ->
doDraw(true, config)
doDrawLabel(canvas, true, config)
}

chartConfig.rightLabelConfig?.let { config ->
doDraw(false, config)
doDrawLabel(canvas, false, config)
}
}

private fun doDrawLabel(canvas: Canvas, isLeft: Boolean, config: KChartConfig.LabelConfig) {
if (config.count > 0) {
labelPaint.textSize = config.textSize
labelPaint.color = config.textColor
labelPaint.getFontMetrics(tmpFontMetrics)
val labelHeight = tmpFontMetrics.bottom - tmpFontMetrics.top
val areaTop =
getChartDisplayArea().top + drawnIndexTextHeight + config.marginTop
val areaBottom = getChartDisplayArea().bottom - config.marginBottom
var verticalSpace = 0f
if (config.count > 1) {
verticalSpace =
(areaBottom - areaTop - config.count * labelHeight) / (config.count - 1)
}
var pos = areaTop
for (i in 1..config.count) {
tmp2FloatArray[0] = 0f
tmp2FloatArray[1] = pos + labelHeight / 2
mapPointsReal2Value(tmp2FloatArray)
val text = config.formatter.invoke(tmp2FloatArray[1])
val startX = if (isLeft) {
config.horizontalMargin
} else {
getChartDisplayArea().right - config.horizontalMargin - labelPaint.measureText(
text
)
}
canvas.drawText(text, startX, pos - tmpFontMetrics.top, labelPaint)
pos += verticalSpace + labelHeight
}
}
}
}
Loading

0 comments on commit 855413b

Please sign in to comment.