Skip to content

Commit

Permalink
统一K线图与成交量图的涨跌色规则
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Dec 20, 2021
1 parent e04e6ae commit 83181c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ open class KChart(
}
}

private fun needDrawAvgPriceLine() = chartConfig.showAvgLine && (chartConfig.kChartType is KChartConfig.KChartType.LINE || chartConfig.kChartType is KChartConfig.KChartType.MOUNTAIN)
private fun needDrawAvgPriceLine() =
chartConfig.showAvgLine && (chartConfig.kChartType is KChartConfig.KChartType.LINE || chartConfig.kChartType is KChartConfig.KChartType.MOUNTAIN)

private fun drawAvgPriceLine(canvas: Canvas) {
if (needDrawAvgPriceLine()) {
Expand Down Expand Up @@ -716,10 +717,10 @@ open class KChart(
val barWidth = 1 * (1 - chartConfig.barSpaceRatio)
val spaceWidth = 1 * chartConfig.barSpaceRatio
var left = spaceWidth / 2f
getKEntities().forEach { kEntity ->
getKEntities().forEachIndexed { idx, kEntity ->
if (kEntity !is EmptyKEntity) {
barKChartPaint.color =
if (kEntity.getClosePrice() >= kEntity.getOpenPrice()) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
if (isRise(idx)) stockChart.getConfig().riseColor else stockChart.getConfig().downColor

tmp12FloatArray[0] = left + barWidth / 2
tmp12FloatArray[1] = kEntity.getHighPrice()
Expand Down Expand Up @@ -749,10 +750,10 @@ open class KChart(
val barWidth = 1 * (1 - chartConfig.barSpaceRatio)
val spaceWidth = 1 * chartConfig.barSpaceRatio
var left = spaceWidth / 2f
getKEntities().forEach { kEntity ->
getKEntities().forEachIndexed { idx, kEntity ->
if (kEntity !is EmptyKEntity) {
hollowKChartPaint.color =
if (kEntity.getClosePrice() >= kEntity.getOpenPrice()) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
if (isRise(idx)) stockChart.getConfig().riseColor else stockChart.getConfig().downColor

tmp4FloatArray[0] = left + barWidth / 2
tmp4FloatArray[1] = kEntity.getHighPrice()
Expand Down Expand Up @@ -791,10 +792,10 @@ open class KChart(
val barWidth = 1 * (1 - chartConfig.barSpaceRatio)
val spaceWidth = 1 * chartConfig.barSpaceRatio
var left = spaceWidth / 2f
getKEntities().forEach { kEntity ->
getKEntities().forEachIndexed { idx, kEntity ->
if (kEntity !is EmptyKEntity) {
candleKChartPaint.color =
if (kEntity.getClosePrice() >= kEntity.getOpenPrice()) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
if (isRise(idx)) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
candleKChartPaint.color = candleKChartPaint.color
tmp4FloatArray[0] = left + barWidth / 2
tmp4FloatArray[1] = kEntity.getHighPrice()
Expand All @@ -815,6 +816,22 @@ open class KChart(
}
}

private fun isRise(idx: Int) =
if (getKEntities()[idx].getClosePrice() == getKEntities()[idx].getOpenPrice()) {
if (idx - 1 in getKEntities().indices) {
val preKEntity = getKEntities()[idx - 1]
if (preKEntity !is EmptyKEntity) {
getKEntities()[idx].getClosePrice() >= preKEntity.getClosePrice()
} else {
true
}
} else {
true
}
} else {
getKEntities()[idx].getClosePrice() > getKEntities()[idx].getOpenPrice()
}

private fun drawLineKChart(canvas: Canvas) {
lineKChartLinePaint.strokeWidth = chartConfig.lineChartStrokeWidth
lineKChartLinePaint.color = chartConfig.lineChartColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,10 @@ class VolumeChart(
getKEntities().forEachIndexed { idx, kEntity ->

if (kEntity !is EmptyKEntity) {
val isRise = if (kEntity.getClosePrice() == kEntity.getOpenPrice()) {
if (idx - 1 in getKEntities().indices) {
val preKEntity = getKEntities()[idx - 1]
if (preKEntity !is EmptyKEntity) {
kEntity.getClosePrice() >= preKEntity.getClosePrice()
} else {
true
}
} else {
true
}
} else {
kEntity.getClosePrice() > kEntity.getOpenPrice()
}
val isRise = isRise(idx)
volumePaint.color =
if (isRise) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
if (kEntity.getClosePrice() >= kEntity.getOpenPrice() && isHollow) { // 空心
if (isRise && isHollow) { // 空心
volumePaint.style = Paint.Style.STROKE
} else {
volumePaint.style = Paint.Style.FILL
Expand All @@ -113,6 +100,22 @@ class VolumeChart(
}
}

private fun isRise(idx: Int) =
if (getKEntities()[idx].getClosePrice() == getKEntities()[idx].getOpenPrice()) {
if (idx - 1 in getKEntities().indices) {
val preKEntity = getKEntities()[idx - 1]
if (preKEntity !is EmptyKEntity) {
getKEntities()[idx].getClosePrice() >= preKEntity.getClosePrice()
} else {
true
}
} else {
true
}
} else {
getKEntities()[idx].getClosePrice() > getKEntities()[idx].getOpenPrice()
}

override fun preDrawHighlight(canvas: Canvas) {}

override fun drawHighlight(canvas: Canvas) {
Expand Down
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.15'
implementation project(':lib')
// implementation 'com.github.wangyiqian:StockChart:1.0.15'
}

0 comments on commit 83181c9

Please sign in to comment.