Skip to content

Commit

Permalink
成交量图支持配置实心或空心的样式
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Dec 15, 2021
1 parent bad48e0 commit 0b4603f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ Data.loadDayData(this, 0) { kEntities: List<IKEntity> ->
|highlightLabelLeft|长按时高亮线左侧标签配置|
|highlightLabelRight|长按时高亮线右侧标签配置|
|barSpaceRatio|柱子之间的空间占比柱子宽度|
|volumeChartType|柱子样式|
|hollowChartLineStrokeWidth|柱子空心时的线条宽度|

##### MACD指标图配置`MacdChartConfig`
|字段|描述|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ val DEFAULT_AVG_LINE_COLOR = Color.parseColor("#FF7B11")

// Volume图
const val DEFAULT_VOLUME_BAR_SPACE_RATIO = 0.5f
const val DEFAULT_VOLUME_CHART_HOLLOW_CHART_LINE_STROKE_WIDTH = 1.5f

// HighlightLabel
const val DEFAULT_HIGHLIGHT_LABEL_BG_COLOR = Color.LTGRAY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,21 @@ class VolumeChart(
override fun drawBackground(canvas: Canvas) {
}

override fun preDrawData(canvas: Canvas) {}
override fun preDrawData(canvas: Canvas) {
}

override fun drawData(canvas: Canvas) {
when (chartConfig.volumeChartType) {
is VolumeChartConfig.VolumeChartType.CANDLE -> {
drawVolumeChart(canvas, false)
}
is VolumeChartConfig.VolumeChartType.HOLLOW -> {
drawVolumeChart(canvas, true)
}
}
}

private fun drawVolumeChart(canvas: Canvas, isHollow: Boolean) {
val barWidth = 1 * (1 - chartConfig.barSpaceRatio)
val spaceWidth = 1 * chartConfig.barSpaceRatio
var left = spaceWidth / 2f
Expand All @@ -80,6 +92,12 @@ class VolumeChart(
}
volumePaint.color =
if (isRise) stockChart.getConfig().riseColor else stockChart.getConfig().downColor
volumePaint.strokeWidth = chartConfig.hollowChartLineStrokeWidth
if (kEntity.getClosePrice() >= kEntity.getOpenPrice() && isHollow) { // 空心
volumePaint.style = Paint.Style.STROKE
} else {
volumePaint.style = Paint.Style.FILL
}

tmpRectF.left = left
tmpRectF.top = kEntity.getVolume().toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,30 @@ class VolumeChartConfig(
onHighlightListener: OnHighlightListener? = null,
chartMainDisplayAreaPaddingTop: Float = 0f,
chartMainDisplayAreaPaddingBottom: Float = 0f,
// 柱子样式
var volumeChartType: VolumeChartType = VolumeChartType.CANDLE(),
// 长按时高亮线左侧标签配置
var highlightLabelLeft: HighlightLabelConfig? = null,
// 长按时高亮线右侧标签配置
var highlightLabelRight: HighlightLabelConfig? = null,
// 柱子之间的空间占比柱子宽度
var barSpaceRatio: Float = DEFAULT_VOLUME_BAR_SPACE_RATIO
var barSpaceRatio: Float = DEFAULT_VOLUME_BAR_SPACE_RATIO,
// 柱子空心时的线条宽度
var hollowChartLineStrokeWidth: Float = DEFAULT_VOLUME_CHART_HOLLOW_CHART_LINE_STROKE_WIDTH
) : BaseChildChartConfig(
height,
marginTop,
marginBottom,
onHighlightListener,
chartMainDisplayAreaPaddingTop,
chartMainDisplayAreaPaddingBottom
)
) {

sealed class VolumeChartType {
// 实心
class CANDLE : VolumeChartType()
// 空心
class HOLLOW : VolumeChartType()
}

}
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.12'
implementation project(':lib')
// implementation 'com.github.wangyiqian:StockChart:1.0.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ class Sample2Activity : AppCompatActivity() {

this.kChartType = kChartType
kChartConfig.kChartType = this.kChartType
// 成交量图根据K线图类型决定是空心还是实现
volumeChartConfig.volumeChartType = if(this.kChartType is KChartConfig.KChartType.HOLLOW) VolumeChartConfig.VolumeChartType.HOLLOW() else VolumeChartConfig.VolumeChartType.CANDLE()
stock_chart.notifyChanged()
refreshOptionButtonsState()
}
Expand Down

0 comments on commit 0b4603f

Please sign in to comment.