Skip to content

Commit

Permalink
支持插入K线数据
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Nov 22, 2021
1 parent 2a85238 commit 57486fc
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 253 deletions.
60 changes: 35 additions & 25 deletions lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ class MatrixHelper(private val stockChart: IStockChart) {
// 计算缩放阶段真正的缩放中心
tmp2FloatArray[0] = scaleFocusXValue
tmp2FloatArray[1] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
tmpMatrix.postConcat(xScaleMatrix)
tmpMatrix.postConcat(fixXScaleMatrix)
tmpMatrix.mapPoints(tmp2FloatArray)
tmpMatrix.apply {
reset()
postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
postConcat(xScaleMatrix)
postConcat(fixXScaleMatrix)
mapPoints(tmp2FloatArray)
}
scalePx = tmp2FloatArray[0]
}

Expand Down Expand Up @@ -101,10 +103,12 @@ class MatrixHelper(private val stockChart: IStockChart) {
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = 1f
tmp4FloatArray[3] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
tmpMatrix.postConcat(xScaleMatrix)
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
postConcat(xScaleMatrix)
mapPoints(tmp4FloatArray)
}
val lengthOfOneIndex = tmp4FloatArray[2] - tmp4FloatArray[0]

val kEntitiesSize = stockChart.getConfig().getKEntitiesSize()
Expand All @@ -120,10 +124,12 @@ class MatrixHelper(private val stockChart: IStockChart) {
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = 1f
tmp4FloatArray[3] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
tmpMatrix.postConcat(xScaleMatrix)
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
postConcat(xScaleMatrix)
mapPoints(tmp4FloatArray)
}
// 重新计算"一格"长度
val lengthOfOneIndex = tmp4FloatArray[2] - tmp4FloatArray[0]
val xRange =
Expand Down Expand Up @@ -153,12 +159,14 @@ class MatrixHelper(private val stockChart: IStockChart) {
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = kEntitiesSize.toFloat() // 多一个是因为边界要在最后一个点的右侧,要多加一个宽度
tmp4FloatArray[3] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
tmpMatrix.postConcat(xScaleMatrix)
tmpMatrix.postConcat(fixXScaleMatrix)
tmpMatrix.postConcat(scrollMatrix)
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
postConcat(xScaleMatrix)
postConcat(fixXScaleMatrix)
postConcat(scrollMatrix)
mapPoints(tmp4FloatArray)
}
val limitLeft = tmp4FloatArray[0]
val limitRight = tmp4FloatArray[2]

Expand Down Expand Up @@ -266,12 +274,14 @@ class MatrixHelper(private val stockChart: IStockChart) {
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = kEntitiesSize.toFloat() // 多一个是因为边界要在最后一个点的右侧,要多加一个宽度
tmp4FloatArray[3] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
tmpMatrix.postConcat(xScaleMatrix)
tmpMatrix.postConcat(fixXScaleMatrix)
tmpMatrix.postConcat(scrollMatrix)
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(stockChart.getChildCharts()[0].getCoordinateMatrix())
postConcat(xScaleMatrix)
postConcat(fixXScaleMatrix)
postConcat(scrollMatrix)
mapPoints(tmp4FloatArray)
}
val limitLeft = tmp4FloatArray[0]
val limitRight = tmp4FloatArray[2]

Expand Down
29 changes: 18 additions & 11 deletions lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
}
}

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

checkChildViews()

invalidate()
Expand Down Expand Up @@ -188,21 +195,21 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
if (childChartConfig.setSizeFlag) {
childChartConfig.setSizeFlag = false
needRequestLayout = true
val layoutParams =
childCharts[index].view().layoutParams as LayoutParams
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
layoutParams.height = childChartConfig.height
(childCharts[index].view().layoutParams as LayoutParams).apply {
width = ViewGroup.LayoutParams.MATCH_PARENT
height = childChartConfig.height
}
}

if (childChartConfig.setMarginFlag) {
childChartConfig.setMarginFlag = false
needRequestLayout = true
val layoutParams =
childCharts[index].view().layoutParams as LayoutParams
layoutParams.leftMargin = 0
layoutParams.topMargin = childChartConfig.marginTop
layoutParams.rightMargin = 0
layoutParams.bottomMargin = childChartConfig.marginBottom
(childCharts[index].view().layoutParams as LayoutParams).apply {
leftMargin = 0
topMargin = childChartConfig.marginTop
rightMargin = 0
bottomMargin = childChartConfig.marginBottom
}
}
}
}
Expand All @@ -213,7 +220,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
removeAllViews()
config.childChartFactories.forEach {
val childChart = it.createChart()
childCharts.add(childChart)
childCharts += childChart
addView(childChart.view())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class StockChartConfig {

internal var appendKEntitiesFlag = false

internal var insertKEntitiesFlag = false

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

Expand Down Expand Up @@ -179,7 +181,7 @@ class StockChartConfig {
* @param kEntity 新数据
*/
fun modifyKEntity(index: Int, kEntity: IKEntity) {
check(index in 0 until kEntities.size) { "Index $index out of bounds for length ${kEntities.size}" }
check(index in this.kEntities.indices) { "Index $index out of bounds for length ${kEntities.size}" }
kEntities[index] = kEntity
modifyKEntitiesFlag = true
}
Expand Down Expand Up @@ -211,6 +213,23 @@ class StockChartConfig {
}
}

/**
* 插入K线数据
*/
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
}
}

/**
* 获取所有K线数据总数
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,20 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo
private fun setDisplayArea() {

// x轴显示区域固定死 子类不可各自实现
chartDisplayArea.left = 0f
chartDisplayArea.right = width.toFloat()
chartDisplayArea.apply {
left = 0f
right = width.toFloat()
top = getDisplayAreaYRangeMin()
bottom = getDisplayAreaYRangeMax()
}

chartMainDisplayArea.apply {
left = chartDisplayArea.left
right = chartDisplayArea.right
top = chartDisplayArea.top + chartConfig.chartMainDisplayAreaPaddingTop
bottom = chartDisplayArea.bottom - chartConfig.chartMainDisplayAreaPaddingBottom
}

chartDisplayArea.top = getDisplayAreaYRangeMin()
chartDisplayArea.bottom = getDisplayAreaYRangeMax()

chartMainDisplayArea.left = chartDisplayArea.left
chartMainDisplayArea.right = chartDisplayArea.right
chartMainDisplayArea.top =
chartDisplayArea.top + chartConfig.chartMainDisplayAreaPaddingTop
chartMainDisplayArea.bottom =
chartDisplayArea.bottom - chartConfig.chartMainDisplayAreaPaddingBottom
}

/**
Expand Down Expand Up @@ -139,6 +141,11 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo
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 @@ -133,16 +133,19 @@ class ChildChartMatrixHelper<O : BaseChildChartConfig>(

val chartDisplayArea = chart.getChartMainDisplayArea()

// 反算出会被移动到显示区域的第一个逻辑坐标值(数据下标)
tmpMatrix.reset()
tmpMatrix.postConcat(coordinateMatrix)
tmpMatrix.postConcat(stockChart.getXScaleMatrix())
tmpMatrix.postConcat(stockChart.getFixXScaleMatrix())
tmpMatrix.postConcat(stockChart.getScrollMatrix())
tmpMatrix.invert(tmpMatrix)

tmp2FloatArray[0] = chartDisplayArea.left
tmp2FloatArray[1] = 0f
tmpMatrix.mapPoints(tmp2FloatArray)
// 反算出会被移动到显示区域的第一个逻辑坐标值(数据下标)
tmpMatrix.apply {
reset()
postConcat(coordinateMatrix)
postConcat(stockChart.getXScaleMatrix())
postConcat(stockChart.getFixXScaleMatrix())
postConcat(stockChart.getScrollMatrix())
invert(tmpMatrix)
mapPoints(tmp2FloatArray)
}
var indexFrom = (round(tmp2FloatArray[0])).toInt()
if (indexFrom !in 0 until stockChart.getConfig().getKEntitiesSize()) {
indexFrom = 0
Expand All @@ -153,12 +156,14 @@ class ChildChartMatrixHelper<O : BaseChildChartConfig>(
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = (indexFrom + 1).toFloat()
tmp4FloatArray[3] = 0f
tmpMatrix.reset()
tmpMatrix.postConcat(coordinateMatrix)
tmpMatrix.postConcat(stockChart.getXScaleMatrix())
tmpMatrix.postConcat(stockChart.getFixXScaleMatrix())
tmpMatrix.postConcat(stockChart.getScrollMatrix())
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(coordinateMatrix)
postConcat(stockChart.getXScaleMatrix())
postConcat(stockChart.getFixXScaleMatrix())
postConcat(stockChart.getScrollMatrix())
mapPoints(tmp4FloatArray)
}
val first = tmp4FloatArray[0]
val second = tmp4FloatArray[2]
val lengthOfOneIndex = second - first
Expand Down Expand Up @@ -201,19 +206,21 @@ class ChildChartMatrixHelper<O : BaseChildChartConfig>(

val chartDisplayArea = chart.getChartMainDisplayArea()

// 反算出哪个下标(逻辑坐标)范围会被移动到显示区域
tmpMatrix.reset()
tmpMatrix.postConcat(coordinateMatrix)
tmpMatrix.postConcat(stockChart.getXScaleMatrix())
tmpMatrix.postConcat(stockChart.getFixXScaleMatrix())
tmpMatrix.postConcat(stockChart.getScrollMatrix())
tmpMatrix.postConcat(fixXMatrix)
tmpMatrix.invert(tmpMatrix)
tmp4FloatArray[0] = chartDisplayArea.left
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = chartDisplayArea.right
tmp4FloatArray[3] = 0f
tmpMatrix.mapPoints(tmp4FloatArray)
// 反算出哪个下标(逻辑坐标)范围会被移动到显示区域
tmpMatrix.apply {
reset()
postConcat(coordinateMatrix)
postConcat(stockChart.getXScaleMatrix())
postConcat(stockChart.getFixXScaleMatrix())
postConcat(stockChart.getScrollMatrix())
postConcat(fixXMatrix)
invert(tmpMatrix)
mapPoints(tmp4FloatArray)
}
var indexFrom = (round(tmp4FloatArray[0])).toInt()
if (indexFrom !in 0 until stockChart.getConfig().getKEntitiesSize()) {
indexFrom = 0
Expand All @@ -227,17 +234,19 @@ class ChildChartMatrixHelper<O : BaseChildChartConfig>(
chart.getYValueRange(indexFrom, indexEnd, tmp2FloatArray)
val yValueRangeFrom = tmp2FloatArray[0]
val yValueRangeEnd = tmp2FloatArray[1]
tmpMatrix.reset()
tmpMatrix.postConcat(coordinateMatrix)
tmpMatrix.postConcat(stockChart.getXScaleMatrix())
concatMatrix.postConcat(stockChart.getFixXScaleMatrix())
tmpMatrix.postConcat(stockChart.getScrollMatrix())
tmpMatrix.postConcat(fixXMatrix)
tmp4FloatArray[0] = 0f
tmp4FloatArray[1] = yValueRangeFrom
tmp4FloatArray[2] = 0f
tmp4FloatArray[3] = yValueRangeEnd
tmpMatrix.mapPoints(tmp4FloatArray)
tmpMatrix.apply {
reset()
postConcat(coordinateMatrix)
postConcat(stockChart.getXScaleMatrix())
postConcat(stockChart.getFixXScaleMatrix())
postConcat(stockChart.getScrollMatrix())
postConcat(fixXMatrix)
mapPoints(tmp4FloatArray)
}
val yMin: Float
val yMax: Float
if (tmp4FloatArray[3] > tmp4FloatArray[1]) {
Expand All @@ -258,13 +267,15 @@ class ChildChartMatrixHelper<O : BaseChildChartConfig>(
}

private fun setConcatMatrix() {
concatMatrix.reset()
concatMatrix.postConcat(coordinateMatrix)
concatMatrix.postConcat(stockChart.getXScaleMatrix())
concatMatrix.postConcat(stockChart.getFixXScaleMatrix())
concatMatrix.postConcat(stockChart.getScrollMatrix())
concatMatrix.postConcat(fixXMatrix)
concatMatrix.postConcat(fixYMatrix)
concatMatrix.apply {
reset()
postConcat(coordinateMatrix)
postConcat(stockChart.getXScaleMatrix())
postConcat(stockChart.getFixXScaleMatrix())
postConcat(stockChart.getScrollMatrix())
postConcat(fixXMatrix)
postConcat(fixYMatrix)
}
}

fun mapPointsValue2Real(pts: FloatArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ interface OnKEntitiesChangedListener {
fun onSetKEntities()
fun onModifyKEntities()
fun onAppendKEntities()
fun onInsertKEntities()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ object NumberFormatUtil {
private val decimalFormat = DecimalFormat()

@Synchronized
fun formatPrice(price: Float): String {
decimalFormat.maximumFractionDigits = 2
decimalFormat.minimumFractionDigits = 2
decimalFormat.groupingSize = 3
return decimalFormat.format(price)
fun formatPrice(price: Float): String = decimalFormat.run {
maximumFractionDigits = 2
minimumFractionDigits = 2
groupingSize = 3
format(price)
}

}
Loading

0 comments on commit 57486fc

Please sign in to comment.