Skip to content

Commit

Permalink
Add kchart period of DAY_TIME
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Apr 9, 2021
1 parent 07cc23f commit a389dfa
Show file tree
Hide file tree
Showing 14 changed files with 1,808 additions and 15 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ Data.loadDayData(this, 0) { kEntities: List<IKEntity> ->
|indexColors|指标线的颜色|
|leftLabelConfig|左侧标签配置|
|rightLabelConfig|右侧标签配置|
|showAvgLine|是否显示分时均线。若需要显示,K线数据需带有分时均线价格。|
|avgLineColor|分时均线颜色|
|avgLineStrokeWidth|分时均线宽度|

##### 时间条图配置`TimeBarConfig`
|字段|描述|
Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/java/com/github/wangyiqian/stockchart/Default.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ val DEFAULT_K_CHART_LEFT_LABEL_CONFIG =
15f
)
val DEFAULT_K_CHART_HIGHLIGHT_LABEL_LEFT = HighlightLabelConfig()
const val DEFAULT_AVG_LINE_WIDTH = 2f
val DEFAULT_AVG_LINE_COLOR = Color.parseColor("#FF7B11")


// Volume图
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ open class KChart(
private val indexTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val highestAndLowestLabelPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val labelPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val avgPriceLinePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
strokeCap = Paint.Cap.ROUND
}

private var indexList: List<List<Float?>>? = null
private var lastCalculateIndexType: Index? = null
Expand Down Expand Up @@ -119,8 +122,21 @@ open class KChart(
yMax = maxBy { it.getHighPrice() }?.getHighPrice() ?: 0f
}
else -> {
yMin = minBy { it.getClosePrice() }?.getClosePrice() ?: 0f
yMax = maxBy { it.getClosePrice() }?.getClosePrice() ?: 0f
forEachIndexed { index, kEntity ->
if (index == 0) {
yMin = kEntity.getClosePrice()
yMax = kEntity.getClosePrice()
} else {
yMin = min(yMin, kEntity.getClosePrice())
yMax = max(yMax, kEntity.getClosePrice())
}
kEntity.getAvgPrice()?.let { avgPrice ->
if (chartConfig.showAvgLine) {
yMin = min(yMin, avgPrice)
yMax = max(yMax, avgPrice)
}
}
}
}
}
}
Expand Down Expand Up @@ -528,7 +544,8 @@ open class KChart(
chartConfig.index?.let { index ->
indexList?.let { indexList ->
val highlight = getHighlight()
var indexIdx = highlight?.getIdx() ?: stockChart.findLastNotEmptyKEntityIdxInDisplayArea()
var indexIdx =
highlight?.getIdx() ?: stockChart.findLastNotEmptyKEntityIdxInDisplayArea()
indexTextPaint.textSize = index.textSize
var left = index.textMarginLeft
val top = index.textMarginTop
Expand Down Expand Up @@ -786,6 +803,36 @@ open class KChart(
)
preIdx = idx
}
if (chartConfig.showAvgLine) {
avgPriceLinePaint.strokeWidth = chartConfig.avgLineStrokeWidth
avgPriceLinePaint.color = chartConfig.avgLineColor
var preAvgIdx = -1
for (idx in getKEntities().indices) {
if (getKEntities()[idx] is EmptyKEntity || getKEntities()[idx].getAvgPrice() == null) {
preAvgIdx = -1
continue
}

if (preAvgIdx == -1 || getKEntities()[idx] is KEntityOfLineStarter) {
preAvgIdx = idx
continue
}

tmp4FloatArray[0] = preAvgIdx + 0.5f
tmp4FloatArray[1] = getKEntities()[preAvgIdx].getAvgPrice()!!
tmp4FloatArray[2] = idx + 0.5f
tmp4FloatArray[3] = getKEntities()[idx].getAvgPrice()!!
mapPointsValue2Real(tmp4FloatArray)
canvas.drawLine(
tmp4FloatArray[0],
tmp4FloatArray[1],
tmp4FloatArray[2],
tmp4FloatArray[3],
avgPriceLinePaint
)
preAvgIdx = idx
}
}
}

private fun drawLabels(canvas: Canvas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package com.github.wangyiqian.stockchart.childchart.kchart

import androidx.annotation.ColorInt
import com.github.wangyiqian.stockchart.*
import com.github.wangyiqian.stockchart.childchart.base.*
import com.github.wangyiqian.stockchart.index.Index
Expand Down Expand Up @@ -74,7 +75,13 @@ open class KChartConfig(
// 左侧标签配置
var leftLabelConfig: LabelConfig? = DEFAULT_K_CHART_LEFT_LABEL_CONFIG,
// 右侧标签配置
var rightLabelConfig: LabelConfig? = null
var rightLabelConfig: LabelConfig? = null,
// 是否显示分时均线。若需要显示,K线数据需带有分时均线价格。
var showAvgLine: Boolean = false,
// 分时均线颜色
var avgLineColor: Int = DEFAULT_AVG_LINE_COLOR,
// 分时均线宽度
var avgLineStrokeWidth: Float = DEFAULT_AVG_LINE_WIDTH
) : BaseChildChartConfig(
height,
marginTop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TimeBar(stockChart: IStockChart, chartConfig: TimeBarConfig) :
is TimeBarConfig.Type.OneMinute -> drawLabelOfOneMinuteType(canvas)
is TimeBarConfig.Type.FiveMinutes -> drawLabelOfFiveMinutesType(canvas)
is TimeBarConfig.Type.SixtyMinutes -> drawLabelOfSixtyMinutesType(canvas)
is TimeBarConfig.Type.DayTime -> drawLabelOfDayTimeType(canvas)
}
}

Expand Down Expand Up @@ -565,6 +566,42 @@ class TimeBar(stockChart: IStockChart, chartConfig: TimeBarConfig) :
}
}

private fun drawLabelOfDayTimeType(canvas: Canvas) {
val labelMinSpace = DimensionUtil.dp2px(context, 5f)

fun drawLabel(idx: Int) {
val kEntity = getKEntities()[idx]
val time = kEntity.getTime()
tmpDate.time = time
val label = chartConfig.type.labelDateFormat.format(tmpDate)

val labelWidth = labelPaint.measureText(label)
val labelHalfWidth = labelWidth / 2

tmp2FloatArray[0] = idx + 0.5f
tmp2FloatArray[1] = 0f
mapPointsValue2Real(tmp2FloatArray)
val centerRealX = tmp2FloatArray[0]

var x = centerRealX - labelHalfWidth
if (x + labelWidth > getChartDisplayArea().right - labelMinSpace) x =
getChartDisplayArea().right - labelMinSpace - labelWidth
if (x < getChartDisplayArea().left + labelMinSpace) x =
getChartDisplayArea().left + labelMinSpace
val y =
getChartDisplayArea().top + getChartDisplayArea().height() / 2 + (tmpFontMetrics.bottom - tmpFontMetrics.top) / 2 - tmpFontMetrics.bottom
canvas.drawText(label, x, y, labelPaint)
}

stockChart.findFirstNotEmptyKEntityIdxInDisplayArea()?.let { idx ->
drawLabel(idx)
}

stockChart.findLastNotEmptyKEntityIdxInDisplayArea()?.let { idx ->
drawLabel(idx)
}
}

private fun drawHighlightLabel(canvas: Canvas) {
getHighlight()?.let { highlight ->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class TimeBarConfig(
labelDateFormat: DateFormat = SimpleDateFormat("HH:mm"),
highlightLabelDateFormat: DateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm")
) : Type(labelDateFormat, highlightLabelDateFormat)

class DayTime(
labelDateFormat: DateFormat = SimpleDateFormat("HH:mm"),
highlightLabelDateFormat: DateFormat = SimpleDateFormat("HH:mm")
) : Type(labelDateFormat, highlightLabelDateFormat)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ interface IKEntity {
fun getClosePrice(): Float
fun getVolume(): Long
fun getTime(): Long

/**
* 分时均线价格
*/
fun getAvgPrice(): Float?
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ open class KEntity(
private val openPrice: Float,
private val closePrice: Float,
private val volume: Long,
private val time: Long
private val time: Long,
private val avgPrice: Float? = null
) : IKEntity {

override fun getHighPrice() = highPrice
Expand All @@ -39,4 +40,6 @@ open class KEntity(
override fun getVolume() = volume

override fun getTime() = time

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

override fun getHighPrice() = kEntity.getHighPrice()

Expand All @@ -32,4 +32,6 @@ class KEntityOfLineStarter(val kEntity: IKEntity) : IKEntity {
override fun getVolume() = kEntity.getVolume()

override fun getTime() = kEntity.getTime()

override fun getAvgPrice() = kEntity.getAvgPrice()
}
4 changes: 2 additions & 2 deletions samples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android:flexbox:2.0.1'

// implementation project(':lib')
implementation 'com.github.wangyiqian:StockChart:1.0.1'
implementation project(':lib')
// implementation 'com.github.wangyiqian:StockChart:1.0.1'
}
Loading

0 comments on commit a389dfa

Please sign in to comment.