diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt index 7889948..9323b18 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt @@ -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] } @@ -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() @@ -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 = @@ -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] @@ -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] diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt index ef3b6f6..49fc614 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt @@ -110,6 +110,13 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet } } + if(config.insertKEntitiesFlag){ + config.insertKEntitiesFlag = false + onKEntitiesChangedListeners.forEach { + it.onInsertKEntities() + } + } + checkChildViews() invalidate() @@ -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 + } } } } @@ -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()) } } diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChartConfig.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChartConfig.kt index 365b8a2..44541b0 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/StockChartConfig.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/StockChartConfig.kt @@ -38,6 +38,8 @@ class StockChartConfig { internal var appendKEntitiesFlag = false + internal var insertKEntitiesFlag = false + // 初始显示区域的起始坐标 var showStartIndex = 0 @@ -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 } @@ -211,6 +213,23 @@ class StockChartConfig { } } + /** + * 插入K线数据 + */ + fun insertKEntities(index: Int, kEntities: List) { + 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线数据总数 */ diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt index 5d21099..94cbe0d 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/BaseChildChart.kt @@ -98,18 +98,20 @@ abstract class BaseChildChart @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 } /** @@ -139,6 +141,11 @@ abstract class BaseChildChart @JvmOverloads constructo prepare() } + override fun onInsertKEntities() { + onKEntitiesChanged() + prepare() + } + private fun prepare() { if (stockChart.getConfig().getKEntitiesSize() <= 0) return childChartMatrixHelper?.prepare() diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/ChildChartMatrixHelper.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/ChildChartMatrixHelper.kt index 2edf796..afe2f47 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/ChildChartMatrixHelper.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/base/ChildChartMatrixHelper.kt @@ -133,16 +133,19 @@ class ChildChartMatrixHelper( 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 @@ -153,12 +156,14 @@ class ChildChartMatrixHelper( 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 @@ -201,19 +206,21 @@ class ChildChartMatrixHelper( 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 @@ -227,17 +234,19 @@ class ChildChartMatrixHelper( 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]) { @@ -258,13 +267,15 @@ class ChildChartMatrixHelper( } 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) { diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnKEntitiesChangedListener.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnKEntitiesChangedListener.kt index bf5045d..631dc6d 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnKEntitiesChangedListener.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/listener/OnKEntitiesChangedListener.kt @@ -21,4 +21,5 @@ interface OnKEntitiesChangedListener { fun onSetKEntities() fun onModifyKEntities() fun onAppendKEntities() + fun onInsertKEntities() } \ No newline at end of file diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/util/NumberFormatUtil.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/util/NumberFormatUtil.kt index 176f86a..d687d22 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/util/NumberFormatUtil.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/util/NumberFormatUtil.kt @@ -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) } } \ No newline at end of file diff --git a/samples/build.gradle b/samples/build.gradle index a0f43ef..0ecc46e 100644 --- a/samples/build.gradle +++ b/samples/build.gradle @@ -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.7' + implementation project(':lib') +// implementation 'com.github.wangyiqian:StockChart:1.0.7' } \ No newline at end of file diff --git a/samples/src/main/java/com/github/wangyiqian/stockchart/sample/Util.kt b/samples/src/main/java/com/github/wangyiqian/stockchart/sample/Util.kt index 5e26c8c..2449fe0 100644 --- a/samples/src/main/java/com/github/wangyiqian/stockchart/sample/Util.kt +++ b/samples/src/main/java/com/github/wangyiqian/stockchart/sample/Util.kt @@ -30,7 +30,7 @@ object Util { * 格式化成交量 */ fun formatVolume(volume: Long): String { - var magnitude = 1f + var magnitude: Float var unit = "" when { volume > 1_0000_0000_0000f -> { diff --git a/samples/src/main/java/com/github/wangyiqian/stockchart/sample/sample2/Sample2Activity.kt b/samples/src/main/java/com/github/wangyiqian/stockchart/sample/sample2/Sample2Activity.kt index 0d789a9..f5ff5b4 100644 --- a/samples/src/main/java/com/github/wangyiqian/stockchart/sample/sample2/Sample2Activity.kt +++ b/samples/src/main/java/com/github/wangyiqian/stockchart/sample/sample2/Sample2Activity.kt @@ -135,43 +135,48 @@ class Sample2Activity : AppCompatActivity() { initKdjChart() initCustomChart() - // 将需要显示的子图的工厂添加进StockChart配置 - stockChartConfig.addChildCharts( - kChartFactory!!, - volumeChartFactory!!, - timeBarFactory!!, - macdChartFactory!!, - kdjChartFactory!!, - customChartFactory!! - ) + stockChartConfig.apply { + // 将需要显示的子图的工厂添加进StockChart配置 + addChildCharts( + kChartFactory!!, + volumeChartFactory!!, + timeBarFactory!!, + macdChartFactory!!, + kdjChartFactory!!, + customChartFactory!! + ) - // 最大缩放比例 - stockChartConfig.scaleFactorMax = 2f - - // 最小缩放比例 - stockChartConfig.scaleFactorMin = 0.5f - - // 网格线设置 - stockChartConfig.gridVerticalLineCount = 3 - stockChartConfig.gridHorizontalLineCount = 4 - - // 设置滑动到左边界加载更多 - stockChartConfig.addOnLoadMoreListener(object : OnLoadMoreListener { - override fun onLeftLoadMore() { - if (!isLoading) { - if (period != Period.FIVE_DAYS - && period != Period.QUARTER - && period != Period.YEAR - && period != Period.FIVE_YEARS - && period != Period.YTD - ) { - loadData(page = currentPage + 1, period = period) + // 最大缩放比例 + scaleFactorMax = 2f + + scrollSmoothly = false + + // 最小缩放比例 + scaleFactorMin = 0.5f + + // 网格线设置 + gridVerticalLineCount = 3 + gridHorizontalLineCount = 4 + + // 设置滑动到左边界加载更多 + addOnLoadMoreListener(object : OnLoadMoreListener { + override fun onLeftLoadMore() { + if (!isLoading) { + if (period != Period.FIVE_DAYS + && period != Period.QUARTER + && period != Period.YEAR + && period != Period.FIVE_YEARS + && period != Period.YTD + ) { + loadData(page = currentPage + 1, period = period) + } } } - } - override fun onRightLoadMore() {} - }) + override fun onRightLoadMore() {} + }) + } + // 绑定配置 stock_chart.setConfig(stockChartConfig) @@ -183,87 +188,94 @@ class Sample2Activity : AppCompatActivity() { private fun initKChart() { kChartFactory = KChartFactory(stock_chart, kChartConfig) - // 指标线宽度 - kChartConfig.indexStrokeWidth = DimensionUtil.dp2px(this, 0.5f).toFloat() + kChartConfig.apply { - // 监听长按信息 - kChartConfig.onHighlightListener = object : OnHighlightListener { - override fun onHighlightBegin() {} + // 指标线宽度 + indexStrokeWidth = DimensionUtil.dp2px(this@Sample2Activity, 0.5f).toFloat() - override fun onHighlightEnd() { - tv_highlight_info.text = "" - } + // 监听长按信息 + onHighlightListener = object : OnHighlightListener { + override fun onHighlightBegin() {} + + override fun onHighlightEnd() { + tv_highlight_info.text = "" + } - override fun onHighlight(highlight: Highlight) { - val idx = highlight.getIdx() - val kEntities = stockChartConfig.kEntities - var showContent = "" - - if (idx in kEntities.indices) { - val kEntity = kEntities[idx] - if (kEntity is EmptyKEntity) { - showContent = "" - } else if (kChartConfig.kChartType is KChartConfig.KChartType.LINE - || kChartConfig.kChartType is KChartConfig.KChartType.MOUNTAIN - ) { - val firstIdx = stock_chart.findFirstNotEmptyKEntityIdxInDisplayArea() - val price = "最新价:${NumberFormatUtil.formatPrice(kEntity.getClosePrice())}" - var changeRatio = "涨跌幅:——" - firstIdx?.let { - changeRatio = "涨跌幅:${Util.formatChangeRatio( - kEntity.getClosePrice(), - kEntities[it].getClosePrice() - )}" + override fun onHighlight(highlight: Highlight) { + val idx = highlight.getIdx() + val kEntities = stockChartConfig.kEntities + var showContent = "" + + if (idx in kEntities.indices) { + val kEntity = kEntities[idx] + if (kEntity is EmptyKEntity) { + showContent = "" + } else if (kChartType is KChartConfig.KChartType.LINE || kChartType is KChartConfig.KChartType.MOUNTAIN) { + val firstIdx = stock_chart.findFirstNotEmptyKEntityIdxInDisplayArea() + val price = + "最新价:${NumberFormatUtil.formatPrice(kEntity.getClosePrice())}" + var changeRatio = "涨跌幅:——" + firstIdx?.let { + changeRatio = "涨跌幅:${ + Util.formatChangeRatio( + kEntity.getClosePrice(), + kEntities[it].getClosePrice() + ) + }" + } + val volume = "成交量:${Util.formatVolume(kEntity.getVolume())}" + + showContent = "$price,$changeRatio,$volume" + } else { + val open = "开盘价:${NumberFormatUtil.formatPrice(kEntity.getOpenPrice())}" + val close = + "收盘价:${NumberFormatUtil.formatPrice(kEntity.getClosePrice())}" + val high = "最高价:${NumberFormatUtil.formatPrice(kEntity.getHighPrice())}" + val low = "最低价${NumberFormatUtil.formatPrice(kEntity.getLowPrice())}" + val changeRatio = + "涨跌幅:${ + Util.formatChangeRatio( + kEntity.getClosePrice(), + kEntity.getOpenPrice() + ) + }" + val volume = "成交量:${Util.formatVolume(kEntity.getVolume())}" + + showContent = "$open,$close,$high,$low,$changeRatio,$volume" } - val volume = "成交量:${Util.formatVolume(kEntity.getVolume())}" - showContent = "$price,$changeRatio,$volume" - } else { - val open = "开盘价:${NumberFormatUtil.formatPrice(kEntity.getOpenPrice())}" - val close = "收盘价:${NumberFormatUtil.formatPrice(kEntity.getClosePrice())}" - val high = "最高价:${NumberFormatUtil.formatPrice(kEntity.getHighPrice())}" - val low = "最低价${NumberFormatUtil.formatPrice(kEntity.getLowPrice())}" - val changeRatio = - "涨跌幅:${Util.formatChangeRatio( - kEntity.getClosePrice(), - kEntity.getOpenPrice() - )}" - val volume = "成交量:${Util.formatVolume(kEntity.getVolume())}" - - showContent = "$open,$close,$high,$low,$changeRatio,$volume" } + // 长按信息显示到界面 + tv_highlight_info.text = showContent } - - // 长按信息显示到界面 - tv_highlight_info.text = showContent } - } - // 图高度 - kChartConfig.height = DimensionUtil.dp2px(this, 250f) - - // 左侧标签设置 - kChartConfig.leftLabelConfig = KChartConfig.LabelConfig( - 5, - { "${NumberFormatUtil.formatPrice(it)}" }, - DimensionUtil.sp2px(this, 8f).toFloat(), - Color.parseColor("#E4E4E4"), - DimensionUtil.dp2px(this, 10f).toFloat(), - DimensionUtil.dp2px(this, 30f).toFloat(), - DimensionUtil.dp2px(this, 30f).toFloat() - ) - - // 长按左侧标签配置 - kChartConfig.highlightLabelLeft = - HighlightLabelConfig( - textSize = DimensionUtil.sp2px(this, 10f).toFloat(), - bgColor = Color.parseColor("#A3A3A3"), - padding = DimensionUtil.dp2px(this, 5f).toFloat() + // 图高度 + height = DimensionUtil.dp2px(this@Sample2Activity, 250f) + + // 左侧标签设置 + leftLabelConfig = KChartConfig.LabelConfig( + 5, + { "${NumberFormatUtil.formatPrice(it)}" }, + DimensionUtil.sp2px(this@Sample2Activity, 8f).toFloat(), + Color.parseColor("#E4E4E4"), + DimensionUtil.dp2px(this@Sample2Activity, 10f).toFloat(), + DimensionUtil.dp2px(this@Sample2Activity, 30f).toFloat(), + DimensionUtil.dp2px(this@Sample2Activity, 30f).toFloat() ) - // 空心蜡烛边框宽度 - kChartConfig.hollowChartLineStrokeWidth = DimensionUtil.dp2px(this, 1f).toFloat() + // 长按左侧标签配置 + highlightLabelLeft = + HighlightLabelConfig( + textSize = DimensionUtil.sp2px(this@Sample2Activity, 10f).toFloat(), + bgColor = Color.parseColor("#A3A3A3"), + padding = DimensionUtil.dp2px(this@Sample2Activity, 5f).toFloat() + ) + + // 空心蜡烛边框宽度 + hollowChartLineStrokeWidth = DimensionUtil.dp2px(this@Sample2Activity, 1f).toFloat() + } } /** @@ -272,19 +284,22 @@ class Sample2Activity : AppCompatActivity() { private fun initVolumeChart() { volumeChartFactory = VolumeChartFactory(stock_chart, volumeChartConfig) - // 图高度 - volumeChartConfig.height = DimensionUtil.dp2px(this, 60f) + volumeChartConfig.apply { + // 图高度 + height = DimensionUtil.dp2px(this@Sample2Activity, 60f) - // 长按左侧标签配置 - volumeChartConfig.highlightLabelLeft = HighlightLabelConfig( - textSize = DimensionUtil.sp2px(this, 10f).toFloat(), - bgColor = Color.parseColor("#A3A3A3"), - padding = DimensionUtil.dp2px(this, 5f).toFloat(), - textFormat = { volume -> - Util.formatVolume(volume = volume.toLong()) - } - ) + // 长按左侧标签配置 + highlightLabelLeft = HighlightLabelConfig( + textSize = DimensionUtil.sp2px(this@Sample2Activity, 10f).toFloat(), + bgColor = Color.parseColor("#A3A3A3"), + padding = DimensionUtil.dp2px(this@Sample2Activity, 5f).toFloat(), + textFormat = { volume -> + Util.formatVolume(volume = volume.toLong()) + } + ) + } + } /** @@ -293,11 +308,14 @@ class Sample2Activity : AppCompatActivity() { private fun initTimeBar() { timeBarFactory = TimeBarFactory(stock_chart, timeBarConfig) - // 背景色(时间条这里不像显示网格线,加个背景色覆盖掉) - timeBarConfig.backGroundColor = stockChartConfig.backgroundColor + timeBarConfig.apply { + // 背景色(时间条这里不像显示网格线,加个背景色覆盖掉) + backGroundColor = stockChartConfig.backgroundColor + + // 长按标签背景色 + highlightLabelBgColor = Color.parseColor("#A3A3A3") + } - // 长按标签背景色 - timeBarConfig.highlightLabelBgColor = Color.parseColor("#A3A3A3") } /** @@ -306,15 +324,17 @@ class Sample2Activity : AppCompatActivity() { private fun initMacdChart() { macdChartFactory = MacdChartFactory(stock_chart, macdChartConfig) - // 图高度 - macdChartConfig.height = DimensionUtil.dp2px(this, 90f) + macdChartConfig.apply { + // 图高度 + height = DimensionUtil.dp2px(this@Sample2Activity, 90f) - // 长按左侧标签配置 - macdChartConfig.highlightLabelLeft = HighlightLabelConfig( - textSize = DimensionUtil.sp2px(this, 10f).toFloat(), - bgColor = Color.parseColor("#A3A3A3"), - padding = DimensionUtil.dp2px(this, 5f).toFloat() - ) + // 长按左侧标签配置 + highlightLabelLeft = HighlightLabelConfig( + textSize = DimensionUtil.sp2px(this@Sample2Activity, 10f).toFloat(), + bgColor = Color.parseColor("#A3A3A3"), + padding = DimensionUtil.dp2px(this@Sample2Activity, 5f).toFloat() + ) + } } /** @@ -323,15 +343,18 @@ class Sample2Activity : AppCompatActivity() { private fun initKdjChart() { kdjChartFactory = KdjChartFactory(stock_chart, kdjChartConfig) - // 图高度 - kdjChartConfig.height = DimensionUtil.dp2px(this, 90f) + kdjChartConfig.apply { + // 图高度 + height = DimensionUtil.dp2px(this@Sample2Activity, 90f) + + // 长按左侧标签配置 + highlightLabelLeft = HighlightLabelConfig( + textSize = DimensionUtil.sp2px(this@Sample2Activity, 10f).toFloat(), + bgColor = Color.parseColor("#A3A3A3"), + padding = DimensionUtil.dp2px(this@Sample2Activity, 5f).toFloat() + ) + } - // 长按左侧标签配置 - kdjChartConfig.highlightLabelLeft = HighlightLabelConfig( - textSize = DimensionUtil.sp2px(this, 10f).toFloat(), - bgColor = Color.parseColor("#A3A3A3"), - padding = DimensionUtil.dp2px(this, 5f).toFloat() - ) } /** @@ -339,8 +362,10 @@ class Sample2Activity : AppCompatActivity() { */ private fun initCustomChart() { customChartFactory = CustomChartFactory(stock_chart, customChartConfig) - customChartConfig.height = DimensionUtil.dp2px(this, 70f) - customChartConfig.bigLabel = "这是自定义子图示例" + customChartConfig.apply { + height = DimensionUtil.dp2px(this@Sample2Activity, 70f) + bigLabel = "这是自定义子图示例" + } } // 加载模拟数据 @@ -448,37 +473,52 @@ class Sample2Activity : AppCompatActivity() { private fun changePeriod(period: Period) { when (period) { Period.DAY_TIME, Period.FIVE_DAYS -> { - kChartConfig.showAvgLine = true // 显示分时均线 - stockChartConfig.scaleAble = false - stockChartConfig.scrollAble = false - stockChartConfig.overScrollAble = false - kChartConfig.index = null - kChartConfig.kChartType = KChartConfig.KChartType.LINE() + stockChartConfig.apply { + scaleAble = false + scrollAble = false + overScrollAble = false + } + kChartConfig.apply { + showAvgLine = true // 显示分时均线 + index = null + kChartType = KChartConfig.KChartType.LINE() + } } Period.YEAR, Period.QUARTER, Period.FIVE_YEARS -> { - kChartConfig.showAvgLine = false - stockChartConfig.scaleAble = true - stockChartConfig.scrollAble = true - stockChartConfig.overScrollAble = false - kChartConfig.index = kChartIndex - kChartConfig.kChartType = kChartType + stockChartConfig.apply { + scaleAble = true + scrollAble = true + overScrollAble = false + } + kChartConfig.apply { + showAvgLine = false + index = kChartIndex + kChartType = kChartType + } } Period.YTD -> { - kChartConfig.showAvgLine = false - stockChartConfig.scaleAble = false - stockChartConfig.scrollAble = false - stockChartConfig.overScrollAble = false - kChartConfig.index = kChartIndex - kChartConfig.kChartType = kChartType - + stockChartConfig.apply { + scaleAble = false + scrollAble = false + overScrollAble = false + } + kChartConfig.apply { + showAvgLine = false + index = kChartIndex + kChartType = kChartType + } } else -> { - kChartConfig.showAvgLine = false - stockChartConfig.scaleAble = true - stockChartConfig.scrollAble = true - stockChartConfig.overScrollAble = true - kChartConfig.index = kChartIndex - kChartConfig.kChartType = kChartType + stockChartConfig.apply { + scaleAble = true + scrollAble = true + overScrollAble = true + } + kChartConfig.apply { + showAvgLine = false + index = kChartIndex + kChartType = kChartType + } } } this.period = period