Skip to content

Commit

Permalink
支持配置左右边距
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Nov 27, 2022
1 parent 94c2883 commit 5d84b42
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 117 deletions.
54 changes: 27 additions & 27 deletions lib/src/main/java/com/github/wangyiqian/stockchart/MatrixHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
val limitRight = tmp4FloatArray[2]

// 显示区域
val displayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()
val mainDisplayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()

// 当前拖动的距离
val dragDistanceX = -distanceX
Expand All @@ -184,25 +184,25 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
var dx = 0f
if (dragDistanceX > 0) { // 手指往右拖

if (limitLeft + dragDistanceX > displayArea.left) {
if (limitLeft + dragDistanceX > mainDisplayArea.left) {
var dragDistanceXLeft = dragDistanceX // 需要拖动的距离

if (limitLeft < displayArea.left) { // 处理未超出边界部分
dx += displayArea.left - limitLeft
if (limitLeft < mainDisplayArea.left) { // 处理未超出边界部分
dx += mainDisplayArea.left - limitLeft
dragDistanceXLeft -= dx
}

if (stockChart.getConfig().overScrollAble && dragDistanceXLeft > 0) { // 超出边界部分,处理回弹区
if (limitLeft - displayArea.left >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (limitLeft - mainDisplayArea.left >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnLeftLoadMore()
}
}
dragDistanceXLeft *= stockChart.getConfig().frictionScrollExceedLimit // 加阻力
if (limitLeft + dragDistanceXLeft > displayArea.left + stockChart.getConfig().overScrollDistance) { // 超出极限
if (limitLeft + dragDistanceXLeft > mainDisplayArea.left + stockChart.getConfig().overScrollDistance) { // 超出极限
dragDistanceXLeft =
displayArea.left + stockChart.getConfig().overScrollDistance - limitLeft
mainDisplayArea.left + stockChart.getConfig().overScrollDistance - limitLeft
}
dx += dragDistanceXLeft
} else {
Expand All @@ -218,25 +218,25 @@ internal class MatrixHelper(private val stockChart: IStockChart) {

} else { // 手指往左拖

if (limitRight + dragDistanceX < displayArea.right) {
if (limitRight + dragDistanceX < mainDisplayArea.right) {
var dragDistanceXLeft = dragDistanceX // 需要拖动的距离

if (limitRight > displayArea.right) { // 未超出边界部分
dx += displayArea.right - limitRight
if (limitRight > mainDisplayArea.right) { // 未超出边界部分
dx += mainDisplayArea.right - limitRight
dragDistanceXLeft -= dx
}

if (stockChart.getConfig().overScrollAble && dragDistanceXLeft < 0) { // 超出边界部分,处理回弹区
if (displayArea.right - limitRight >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (mainDisplayArea.right - limitRight >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnRightLoadMore()
}
}
dragDistanceXLeft *= stockChart.getConfig().frictionScrollExceedLimit // 加阻力
if (limitRight + dragDistanceXLeft < displayArea.right - stockChart.getConfig().overScrollDistance) { // 超出极限
if (limitRight + dragDistanceXLeft < mainDisplayArea.right - stockChart.getConfig().overScrollDistance) { // 超出极限
dragDistanceXLeft =
displayArea.right - stockChart.getConfig().overScrollDistance - limitRight
mainDisplayArea.right - stockChart.getConfig().overScrollDistance - limitRight
}
dx += dragDistanceXLeft
} else {
Expand Down Expand Up @@ -290,18 +290,18 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
val limitRight = tmp4FloatArray[2]

// 显示区域
val displayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()
val mainDisplayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()

if (limitRight < displayArea.right || limitLeft > displayArea.left) {
if (limitRight < mainDisplayArea.right || limitLeft > mainDisplayArea.left) {
// 边界外不能开始fling
return
}

computeScrollCurrX = 0

// 计算出x能惯性滑动的限制位置
val minX = -abs(limitRight - displayArea.right)
val maxX = abs(limitLeft - displayArea.left)
val minX = -abs(limitRight - mainDisplayArea.right)
val maxX = abs(limitLeft - mainDisplayArea.left)


if (velocityX < 0 && maxX != 0f || velocityX > 0 && minX != 0f) {
Expand Down Expand Up @@ -340,14 +340,14 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
stockChart.getChildCharts()[0].mapPointsValue2Real(tmp4FloatArray)
val limitLeft = tmp4FloatArray[0]
val limitRight = tmp4FloatArray[2]
val displayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()
val mainDisplayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()
if (stockChart.getConfig().overScrollAble) {
if (limitLeft - displayArea.left >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (limitLeft - mainDisplayArea.left >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnLeftLoadMore()
}
} else if (displayArea.right - limitRight >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
} else if (mainDisplayArea.right - limitRight >= stockChart.getConfig().overScrollOnLoadMoreDistance) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnRightLoadMore()
Expand All @@ -356,12 +356,12 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
enableTriggerOnLoadMoreNextTime = true
}
} else {
if (limitLeft.toInt() >= displayArea.left.toInt()) {
if (limitLeft.toInt() >= mainDisplayArea.left.toInt()) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnLeftLoadMore()
}
} else if (limitRight.toInt() <= displayArea.right.toInt()) {
} else if (limitRight.toInt() <= mainDisplayArea.right.toInt()) {
if (enableTriggerOnLoadMoreNextTime) {
enableTriggerOnLoadMoreNextTime = false
stockChart.dispatchOnRightLoadMore()
Expand Down Expand Up @@ -394,13 +394,13 @@ internal class MatrixHelper(private val stockChart: IStockChart) {
val limitRight = tmp4FloatArray[2]

// 显示区域
val displayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()
val mainDisplayArea = stockChart.getChildCharts()[0].getChartMainDisplayArea()

var dx = 0
if (limitLeft > displayArea.left) { // 左边界滑过头
dx = (displayArea.left - limitLeft).toInt()
} else if (limitRight < displayArea.right) { // 右边界滑过头
dx = (displayArea.right - limitRight).toInt()
if (limitLeft > mainDisplayArea.left) { // 左边界滑过头
dx = (mainDisplayArea.left - limitLeft).toInt()
} else if (limitRight < mainDisplayArea.right) { // 右边界滑过头
dx = (mainDisplayArea.right - limitRight).toInt()
}

if (dx != 0) { // 需要回弹
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ class StockChartConfig {

var valueTendToZero = DEFAULT_VALUE_TEND_TO_ZERO

/**
* 主数据显示区域的左内间距
*/
var chartMainDisplayAreaPaddingLeft: Float = 0f

/**
* 主数据显示区域的右内间距
*/
var chartMainDisplayAreaPaddingRight: Float = 0f

/**
* 加载更多监听
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ abstract class BaseChildChart<C : BaseChildChartConfig> @JvmOverloads constructo
}

chartMainDisplayArea.apply {
left = chartDisplayArea.left
right = chartDisplayArea.right
left = chartDisplayArea.left + stockChart.getConfig().chartMainDisplayAreaPaddingLeft
right = chartDisplayArea.right - stockChart.getConfig().chartMainDisplayAreaPaddingRight
top = chartDisplayArea.top + chartConfig.chartMainDisplayAreaPaddingTop
bottom = chartDisplayArea.bottom - chartConfig.chartMainDisplayAreaPaddingBottom
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(
private fun prepareCoordinateMatrix() {
coordinateMatrix.reset()

val chartDisplayArea = chart.getChartMainDisplayArea()
val chartMainDisplayArea = chart.getChartMainDisplayArea()

chart.getXValueRange(
stockChart.getConfig().showStartIndex,
Expand All @@ -89,36 +89,36 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(
if (yValueRangeLen == 0f) {
// 非正常情况,y轴逻辑区间无法算出(所有值相等),之前处于原始逻辑坐标,将需要显示的逻辑区域移动到显示区域左边垂直居中位置
coordinateMatrix.postTranslate(
chartDisplayArea.left - xValueRangeFrom,
(chartDisplayArea.bottom - chartDisplayArea.top) / 2
chartMainDisplayArea.left - xValueRangeFrom,
(chartMainDisplayArea.bottom - chartMainDisplayArea.top) / 2
)
} else {
// 正常情况,之前处于原始逻辑坐标,将需要显示的逻辑区域移动到显示区域左上角
coordinateMatrix.postTranslate(
chartDisplayArea.left - xValueRangeFrom,
chartDisplayArea.top - yValueRangeFrom
chartMainDisplayArea.left - xValueRangeFrom,
chartMainDisplayArea.top - yValueRangeFrom
)
}

val sx = (chartDisplayArea.right - chartDisplayArea.left) / xValueRangeLen
val sx = (chartMainDisplayArea.right - chartMainDisplayArea.left) / xValueRangeLen
val sy = if (yValueRangeLen == 0f) {
// 非正常情况,y轴逻辑区间无法算出(所有值相等),直接保持原状不缩放
1f
} else {
// 正常情况,y按照区间比缩放即可
(chartDisplayArea.bottom - chartDisplayArea.top) / yValueRangeLen
(chartMainDisplayArea.bottom - chartMainDisplayArea.top) / yValueRangeLen
}

// 缩放使得需要显示的内容刚好撑满显示区域,再向上翻转,使得y内容翻转在显示区域上方
coordinateMatrix.postScale(
sx,
-sy,
chartDisplayArea.left,
chartDisplayArea.top
chartMainDisplayArea.left,
chartMainDisplayArea.top
)

// 正常情况,下移一个显示区域
coordinateMatrix.postTranslate(0f, chartDisplayArea.bottom - chartDisplayArea.top)
coordinateMatrix.postTranslate(0f, chartMainDisplayArea.bottom - chartMainDisplayArea.top)
}

/**
Expand All @@ -131,9 +131,9 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(

// "一格一格"地滑

val chartDisplayArea = chart.getChartMainDisplayArea()
val mainChartDisplayArea = chart.getChartMainDisplayArea()

tmp2FloatArray[0] = chartDisplayArea.left
tmp2FloatArray[0] = mainChartDisplayArea.left
tmp2FloatArray[1] = 0f
// 反算出会被移动到显示区域的第一个逻辑坐标值(数据下标)
tmpMatrix.apply {
Expand Down Expand Up @@ -168,7 +168,7 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(
val lengthOfOneIndex = second - first

if (lengthOfOneIndex != 0f) {
val unalignedDis = (first - chartDisplayArea.left) % lengthOfOneIndex
val unalignedDis = (first - mainChartDisplayArea.left) % lengthOfOneIndex

val dx = when {
// 右移
Expand Down Expand Up @@ -203,11 +203,11 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(
private fun setFixYMatrix() {
fixYMatrix.reset()

val chartDisplayArea = chart.getChartMainDisplayArea()
val mainChartDisplayArea = chart.getChartMainDisplayArea()

tmp4FloatArray[0] = chartDisplayArea.left
tmp4FloatArray[0] = mainChartDisplayArea.left
tmp4FloatArray[1] = 0f
tmp4FloatArray[2] = chartDisplayArea.right
tmp4FloatArray[2] = mainChartDisplayArea.right
tmp4FloatArray[3] = 0f
// 反算出哪个下标(逻辑坐标)范围会被移动到显示区域
tmpMatrix.apply {
Expand Down Expand Up @@ -258,10 +258,10 @@ internal class ChildChartMatrixHelper<O : BaseChildChartConfig>(

if (yMin != yMax) {
// 先贴顶
fixYMatrix.postTranslate(0f, chartDisplayArea.top - yMin)
val sy = (chartDisplayArea.bottom - chartDisplayArea.top) / (yMax - yMin)
fixYMatrix.postTranslate(0f, mainChartDisplayArea.top - yMin)
val sy = (mainChartDisplayArea.bottom - mainChartDisplayArea.top) / (yMax - yMin)
// 再缩放
fixYMatrix.postScale(1f, sy, 0f, chartDisplayArea.top)
fixYMatrix.postScale(1f, sy, 0f, mainChartDisplayArea.top)
}
}

Expand Down
Loading

0 comments on commit 5d84b42

Please sign in to comment.