Skip to content

Commit 31b7064

Browse files
committed
fix enable/disable popups per line issue
1 parent c7a3c4d commit 31b7064

File tree

2 files changed

+71
-66
lines changed
  • app/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/ui
  • compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts

2 files changed

+71
-66
lines changed

app/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/ui/LineSample.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ val dividerProperties = DividerProperties(
6363
)
6464
@Composable
6565
fun RowScope.LineSample() {
66+
val popupProperties = PopupProperties(
67+
textStyle = TextStyle(
68+
fontSize = 11.sp,
69+
color = Color.White,
70+
fontFamily = ubuntu
71+
),
72+
contentBuilder = { dataIndex,valueIndex,value->
73+
value.format(1) + " Million, lineIndex: $dataIndex, valueIndex: $valueIndex"
74+
},
75+
containerColor = Color(0xff414141)
76+
)
6677
val data = remember {
6778
listOf(
6879
Line(
@@ -80,7 +91,8 @@ fun RowScope.LineSample() {
8091
strokeAnimationSpec = tween(2000, easing = EaseInOutCubic),
8192
gradientAnimationDelay = 1000,
8293
drawStyle = DrawStyle.Stroke(.5.dp),
83-
curvedEdges = true
94+
curvedEdges = true,
95+
popupProperties = popupProperties
8496
),
8597
Line(
8698
label = "Linux",
@@ -96,7 +108,8 @@ fun RowScope.LineSample() {
96108
secondGradientFillColor = Color.Transparent,
97109
strokeAnimationSpec = tween(2000, easing = EaseInOutCubic),
98110
gradientAnimationDelay = 1000,
99-
drawStyle = DrawStyle.Stroke(.5.dp)
111+
drawStyle = DrawStyle.Stroke(.5.dp),
112+
popupProperties = popupProperties.copy(enabled = false)
100113
),
101114
Line(
102115
label = "MacOS",
@@ -114,6 +127,7 @@ fun RowScope.LineSample() {
114127
gradientAnimationDelay = 1000,
115128
drawStyle = DrawStyle.Stroke(.5.dp),
116129
curvedEdges = true,
130+
popupProperties = popupProperties
117131
),
118132
)
119133
}
@@ -135,17 +149,6 @@ fun RowScope.LineSample() {
135149
}),
136150
gridProperties = gridProperties,
137151
dividerProperties = dividerProperties,
138-
popupProperties = PopupProperties(
139-
textStyle = TextStyle(
140-
fontSize = 11.sp,
141-
color = Color.White,
142-
fontFamily = ubuntu
143-
),
144-
contentBuilder = { dataIndex,valueIndex,value->
145-
value.format(1) + " Million, lineIndex: $dataIndex, valueIndex: $valueIndex"
146-
},
147-
containerColor = Color(0xff414141)
148-
),
149152
indicatorProperties = HorizontalIndicatorProperties(
150153
textStyle = TextStyle(
151154
fontSize = 11.sp,

compose-charts/src/commonMain/kotlin/ir/ehsannarmani/compose_charts/LineChart.kt

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fun LineChart(
265265
.weight(1f)
266266
.fillMaxSize()
267267
.pointerInput(data, minValue, maxValue, linesPathData) {
268-
if (!popupProperties.enabled) return@pointerInput
268+
if (!popupProperties.enabled || data.all { it.popupProperties?.enabled == false }) return@pointerInput
269269
detectHorizontalDragGestures(
270270
onDragEnd = {
271271
scope.launch {
@@ -280,66 +280,68 @@ fun LineChart(
280280
popups.clear()
281281
data.forEachIndexed { dataIndex, line ->
282282
val properties = line.popupProperties ?: popupProperties
283-
284-
val positionX =
285-
(change.position.x).coerceIn(
286-
0f,
287-
size.width.toFloat()
288-
)
289-
val pathData = linesPathData[dataIndex]
290-
291-
if (positionX >= pathData.xPositions[pathData.startIndex] && positionX <= pathData.xPositions[pathData.endIndex]) {
292-
val showOnPointsThreshold =
293-
((properties.mode as? PopupProperties.Mode.PointMode)?.threshold
294-
?: 0.dp).toPx()
295-
val pointX =
296-
pathData.xPositions.find { it in positionX - showOnPointsThreshold..positionX + showOnPointsThreshold }
297-
298-
if (properties.mode !is PopupProperties.Mode.PointMode || pointX != null) {
299-
val fraction =
300-
((if (properties.mode is PopupProperties.Mode.PointMode) (pointX?.toFloat()
301-
?: 0f) else positionX) / size.width)
302-
303-
//Calculate the data index
304-
val valueIndex = calculateValueIndex(
305-
fraction = fraction.toDouble(),
306-
values = line.values,
307-
pathData = pathData
283+
if(properties.enabled){
284+
val positionX =
285+
(change.position.x).coerceIn(
286+
0f,
287+
size.width.toFloat()
308288
)
289+
val pathData = linesPathData[dataIndex]
290+
291+
if (positionX >= pathData.xPositions[pathData.startIndex] && positionX <= pathData.xPositions[pathData.endIndex]) {
292+
val showOnPointsThreshold =
293+
((properties.mode as? PopupProperties.Mode.PointMode)?.threshold
294+
?: 0.dp).toPx()
295+
val pointX =
296+
pathData.xPositions.find { it in positionX - showOnPointsThreshold..positionX + showOnPointsThreshold }
297+
298+
if (properties.mode !is PopupProperties.Mode.PointMode || pointX != null) {
299+
val fraction =
300+
((if (properties.mode is PopupProperties.Mode.PointMode) (pointX?.toFloat()
301+
?: 0f) else positionX) / size.width)
302+
303+
//Calculate the data index
304+
val valueIndex = calculateValueIndex(
305+
fraction = fraction.toDouble(),
306+
values = line.values,
307+
pathData = pathData
308+
)
309309

310-
val popupValue = getPopupValue(
311-
points = line.values,
312-
fraction = fraction.toDouble(),
313-
rounded = line.curvedEdges ?: curvedEdges,
314-
size = _size,
315-
minValue = minValue,
316-
maxValue = maxValue
317-
)
318-
popups.add(
319-
Popup(
320-
position = popupValue.offset,
321-
value = popupValue.calculatedValue,
322-
properties = properties,
323-
dataIndex = dataIndex,
324-
valueIndex = valueIndex
310+
val popupValue = getPopupValue(
311+
points = line.values,
312+
fraction = fraction.toDouble(),
313+
rounded = line.curvedEdges ?: curvedEdges,
314+
size = _size,
315+
minValue = minValue,
316+
maxValue = maxValue
325317
)
326-
)
327-
if (popupsOffsetAnimators.count() < popups.count()) {
328-
repeat(popups.count() - popupsOffsetAnimators.count()) {
329-
popupsOffsetAnimators.add(
330-
// add fixed position for popup when mode is point mode
331-
if (properties.mode is PopupProperties.Mode.PointMode) {
332-
Animatable(popupValue.offset.x) to Animatable(
333-
popupValue.offset.y
334-
)
335-
} else {
336-
Animatable(0f) to Animatable(0f)
337-
}
318+
popups.add(
319+
Popup(
320+
position = popupValue.offset,
321+
value = popupValue.calculatedValue,
322+
properties = properties,
323+
dataIndex = dataIndex,
324+
valueIndex = valueIndex
338325
)
326+
)
327+
if (popupsOffsetAnimators.count() < popups.count()) {
328+
repeat(popups.count() - popupsOffsetAnimators.count()) {
329+
popupsOffsetAnimators.add(
330+
// add fixed position for popup when mode is point mode
331+
if (properties.mode is PopupProperties.Mode.PointMode) {
332+
Animatable(popupValue.offset.x) to Animatable(
333+
popupValue.offset.y
334+
)
335+
} else {
336+
Animatable(0f) to Animatable(0f)
337+
}
338+
)
339+
}
339340
}
340341
}
341342
}
342343
}
344+
343345
}
344346
scope.launch {
345347
// animate popup (alpha)

0 commit comments

Comments
 (0)