Skip to content

Commit

Permalink
Support onTap for childChart
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed May 13, 2021
1 parent ef754ca commit 98c9d5e
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 39 deletions.
20 changes: 18 additions & 2 deletions lib/src/main/java/com/github/wangyiqian/stockchart/StockChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.view.ViewGroup
import androidx.annotation.UiThread
import com.github.wangyiqian.stockchart.childchart.base.IChildChart
import com.github.wangyiqian.stockchart.entities.EmptyKEntity
import com.github.wangyiqian.stockchart.entities.GestureEvent
import com.github.wangyiqian.stockchart.entities.Highlight
import com.github.wangyiqian.stockchart.listener.OnKEntitiesChangedListener
import com.github.wangyiqian.stockchart.util.checkMainThread
Expand Down Expand Up @@ -123,7 +124,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
}

override fun findLastNotEmptyKEntityIdxInDisplayArea(): Int? {
if(childCharts.isEmpty()) return null
if (childCharts.isEmpty()) return null
val chartDisplayArea = childCharts[0].getChartDisplayArea()
tmp4FloatArray[0] = chartDisplayArea.left
tmp4FloatArray[1] = 0f
Expand All @@ -143,7 +144,7 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
}

override fun findFirstNotEmptyKEntityIdxInDisplayArea(): Int? {
if(childCharts.isEmpty()) return null
if (childCharts.isEmpty()) return null
val chartDisplayArea = childCharts[0].getChartDisplayArea()
tmp4FloatArray[0] = chartDisplayArea.left
tmp4FloatArray[1] = 0f
Expand Down Expand Up @@ -353,6 +354,21 @@ class StockChart @JvmOverloads constructor(context: Context, attrs: AttributeSet
notifyChanged()
matrixHelper.checkScrollBack()
}

override fun onTap(x: Float, y: Float) {
childCharts.forEach { childChart ->
val childChartX = x - childChart.view().left
val childChartY = y - childChart.view().top
tmp2FloatArray[0] = childChartX
tmp2FloatArray[1] = childChartY
childChart.mapPointsReal2Value(tmp2FloatArray)
val valueX = tmp2FloatArray[0]
val valueY = tmp2FloatArray[1]
val gestureEvent = GestureEvent(childChartX, childChartY, valueX, valueY)
childChart.onTap(gestureEvent)
}
}

}

override fun computeScroll() {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/main/java/com/github/wangyiqian/stockchart/TouchHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class TouchHelper(private val stockChart: IStockChart, private val callBack: Cal
return true
}

override fun onSingleTapConfirmed(e: MotionEvent): Boolean {
callBack.onTap(e.x, e.y)
return super.onSingleTapConfirmed(e)
}

interface CallBack {

/**
Expand Down Expand Up @@ -181,5 +186,10 @@ class TouchHelper(private val stockChart: IStockChart, private val callBack: Cal
* 手指离开屏幕
*/
fun onTouchLeave()

/**
* 点击
*/
fun onTap(x: Float, y: Float)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import android.view.View
import android.view.ViewGroup
import com.github.wangyiqian.stockchart.IStockChart
import com.github.wangyiqian.stockchart.StockChart
import com.github.wangyiqian.stockchart.entities.GestureEvent
import com.github.wangyiqian.stockchart.listener.OnKEntitiesChangedListener

/**
Expand Down Expand Up @@ -242,4 +243,5 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo

override fun getConfig() = chartConfig

override fun onTap(event: GestureEvent) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.graphics.Matrix
import android.graphics.Path
import android.graphics.RectF
import android.view.View
import com.github.wangyiqian.stockchart.entities.GestureEvent
import com.github.wangyiqian.stockchart.entities.Highlight
import com.github.wangyiqian.stockchart.entities.IKEntity

Expand Down Expand Up @@ -85,4 +86,9 @@ interface IChildChart {
* 获取对应配置
*/
fun getConfig(): BaseChildChartConfig

/**
* 点击
*/
fun onTap(event: GestureEvent)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import android.graphics.Canvas
import android.graphics.Paint
import com.github.wangyiqian.stockchart.IStockChart
import com.github.wangyiqian.stockchart.childchart.base.BaseChildChart
import kotlin.math.round

/**
* @author wangyiqian E-mail: wangyiqian9891@gmail.com
Expand Down Expand Up @@ -118,7 +117,8 @@ class KdjChart(stockChart: IStockChart, chartConfig: KdjChartConfig) :
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 WangYiqian
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
*/

package com.github.wangyiqian.stockchart.entities

/**
* @author wangyiqian E-mail: wangyiqian9891@gmail.com
* @version 创建时间: 2021/5/13
*/
open class GestureEvent(
/**
* 实际坐标x
*/
var x: Float = 0f,

/**
* 实际坐标y
*/
var y: Float = 0f,

/**
* 逻辑坐标x
*/
var valueX: Float = 0f,

/**
* 逻辑坐标y
*/
var valueY: Float = 0f
) {

/**
* 获取对应K线数据下标
*/
fun getIdx() = (valueX).toInt()

override fun equals(other: Any?): Boolean {
if (other == null || other !is Highlight) return false
return x == other.x && y == other.y && valueX == other.valueX && valueY == other.valueY
}

override fun toString(): String {
return "GestureEvent(x=$x, y=$y, valueX=$valueX, valueY=$valueY)"
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,5 @@ package com.github.wangyiqian.stockchart.entities
* @version 创建时间: 2021/2/6
*/
class Highlight(
/**
* 实际坐标x
*/
var x: Float = 0f,

/**
* 实际坐标y
*/
var y: Float = 0f,

/**
* 逻辑坐标x
*/
var valueX: Float = 0f,

/**
* 逻辑坐标y
*/
var valueY: Float = 0f
) {

/**
* 获取对应K线数据下标
*/
fun getIdx() = (valueX).toInt()

override fun equals(other: Any?): Boolean {
if (other == null || other !is Highlight) return false
return x == other.x && y == other.y && valueX == other.valueX && valueY == other.valueY
}


}
x: Float = 0f, y: Float = 0f, valueX: Float = 0f, valueY: Float = 0f
) : GestureEvent(x, y, valueX, valueY)
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.3'
implementation project(':lib')
// implementation 'com.github.wangyiqian:StockChart:1.0.3'
}

0 comments on commit 98c9d5e

Please sign in to comment.