Skip to content

Commit 7c92f44

Browse files
committed
rename PopupProperties valueFormatter to contentBuilder and change its signature to (valueIndex, lineIndex, value) -> String
1 parent cb8cfb4 commit 7c92f44

File tree

8 files changed

+46
-52
lines changed

8 files changed

+46
-52
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fun RowScope.ColumnSample() {
164164
color = Color.White,
165165
fontFamily = ubuntu,
166166
),
167-
valueFormatter = { dataIndex, valueIndex, value ->
167+
contentBuilder = { dataIndex, valueIndex, value ->
168168
value.format(1) + " Million" + " - dataIdx: " + dataIndex + ", valueIdx: " + valueIndex
169169
},
170170
containerColor = Color(0xff414141),
@@ -367,8 +367,8 @@ fun RowScope.ColumnSample2() {
367367
color = Color.White,
368368
fontFamily = ubuntu,
369369
),
370-
contentBuilder = {
371-
it.format(1) + " Million"
370+
contentBuilder = { _,_,value->
371+
value.format(1) + " Million"
372372
},
373373
containerColor = Color(0xff414141),
374374
),
@@ -433,8 +433,8 @@ fun RowScope.ColumnSample3() {
433433
color = Color.White,
434434
fontFamily = ubuntu,
435435
),
436-
contentBuilder = {
437-
it.format(1) + " Million"
436+
contentBuilder = { _,_,value->
437+
value.format(1) + " Million"
438438
},
439439
containerColor = Color(0xff414141),
440440
),

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ fun RowScope.LineSample() {
141141
color = Color.White,
142142
fontFamily = ubuntu
143143
),
144-
contentBuilder = {
145-
it.format(1) + " Million"
144+
contentBuilder = { _,_,value->
145+
value.format(1) + " Million"
146146
},
147147
containerColor = Color(0xff414141)
148148
),
@@ -220,8 +220,8 @@ fun RowScope.LineSample2() {
220220
color = Color.White,
221221
fontFamily = ubuntu
222222
),
223-
contentBuilder = {
224-
it.format(1) + " °C"
223+
contentBuilder = { _,_,value->
224+
value.format(1) + " °C"
225225
},
226226
containerColor = Color(0xff414141)
227227
),
@@ -320,7 +320,7 @@ fun RowScope.LineSample3() {
320320
fontFamily = ubuntu
321321
),
322322
mode = PopupProperties.Mode.PointMode(),
323-
valueFormatter = { dataIndex, valueIndex, value ->
323+
contentBuilder = { dataIndex, valueIndex, value ->
324324
value.format(1) + " Million" + " - dataIdx: " + dataIndex + ", valueIdx: " + valueIndex
325325
},
326326
containerColor = Color(0xff414141)
@@ -403,8 +403,8 @@ fun RowScope.LineSample4() {
403403
color = Color.White,
404404
fontFamily = ubuntu,
405405
),
406-
contentBuilder = {
407-
it.format(1) + " Million"
406+
contentBuilder = { _,_,value->
407+
value.format(1) + " Million"
408408
},
409409
containerColor = Color(0xff414141)
410410
),
@@ -468,8 +468,8 @@ fun RowScope.LineSample5() {
468468
fontSize = 11.sp,
469469
fontFamily = ubuntu, color = Color.White,
470470
),
471-
contentBuilder = {
472-
it.format(1) + " Million"
471+
contentBuilder = { _,_,value->
472+
value.format(1) + " Million"
473473
},
474474
containerColor = Color(0xff414141)
475475
),
@@ -574,8 +574,8 @@ fun RowScope.LineSample6() {
574574
fontSize = 11.sp,
575575
fontFamily = ubuntu, color = Color.White
576576
),
577-
contentBuilder = {
578-
it.format(1) + " Million"
577+
contentBuilder = { _,_,value->
578+
value.format(1) + " Million"
579579
},
580580
containerColor = Color(0xff414141)
581581
),
@@ -650,8 +650,8 @@ fun RowScope.LineSample7() {
650650
color = Color.White,
651651
fontFamily = ubuntu,
652652
),
653-
contentBuilder = {
654-
it.format(1) + " Million"
653+
contentBuilder = { _,_,value->
654+
value.format(1) + " Million"
655655
},
656656
containerColor = Color(0xff414141),
657657
),
@@ -716,8 +716,8 @@ fun RowScope.LineSample8() {
716716
color = Color.White,
717717
fontFamily = ubuntu,
718718
),
719-
contentBuilder = {
720-
it.format(1) + " Million"
719+
contentBuilder = { _,_,value->
720+
value.format(1) + " Million"
721721
},
722722
containerColor = Color(0xff414141)
723723
),
@@ -771,8 +771,8 @@ fun RowScope.LineSample9() {
771771
color = Color.White,
772772
fontFamily = ubuntu,
773773
),
774-
contentBuilder = {
775-
it.format(1) + " Million"
774+
contentBuilder = { _,_,value->
775+
value.format(1) + " Million"
776776
},
777777
containerColor = Color(0xff414141)
778778
),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fun RowScope.RowSample() {
169169
color = Color.White,
170170
fontFamily = ubuntu,
171171
),
172-
valueFormatter = { dataIndex, valueIndex, value ->
172+
contentBuilder = { dataIndex, valueIndex, value ->
173173
value.format(1) + " Million" + " - dataIdx: " + dataIndex + ", valueIdx: " + valueIndex
174174
},
175175
containerColor = Color(0xff414141),
@@ -380,7 +380,7 @@ fun RowScope.RowSample2() {
380380
color = Color.White,
381381
fontFamily = ubuntu,
382382
),
383-
valueFormatter = { dataIndex, valueIndex, value ->
383+
contentBuilder = { dataIndex, valueIndex, value ->
384384
value.format(1) + " Million" + " - dataIdx: " + dataIndex + ", valueIdx: " + valueIndex
385385
},
386386
containerColor = Color(0xff414141),
@@ -452,7 +452,7 @@ fun RowScope.RowSample3() {
452452
color = Color.White,
453453
fontFamily = ubuntu,
454454
),
455-
valueFormatter = { dataIndex, valueIndex, value ->
455+
contentBuilder = { dataIndex, valueIndex, value ->
456456
value.format(1) + " Million" + " - dataIdx: " + dataIndex + ", valueIdx: " + valueIndex
457457
},
458458
containerColor = Color(0xff414141),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ private fun DrawScope.drawPopup(
395395
progress: Float,
396396
) {
397397
val measure = textMeasurer.measure(
398-
properties.valueFormatter(selectedBar.dataIndex, selectedBar.valueIndex, selectedBar.bar.value),
398+
properties.contentBuilder(selectedBar.dataIndex, selectedBar.valueIndex, selectedBar.bar.value),
399399
style = properties.textStyle.copy(
400400
color = properties.textStyle.color.copy(
401401
alpha = 1f * progress

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ private fun DrawScope.drawPopup(
562562
val offset = popup.position
563563
val popupProperties = popup.properties
564564
val measureResult = textMeasurer.measure(
565-
popupProperties.valueFormatter(popup.dataIndex, popup.valueIndex, popup.value),
565+
popupProperties.contentBuilder(popup.dataIndex, popup.valueIndex, popup.value),
566566
style = popupProperties.textStyle.copy(
567567
color = popupProperties.textStyle.color.copy(
568568
alpha = 1f * progress

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private fun DrawScope.drawPopUp(
389389
progress: Float,
390390
) {
391391
val measure = textMeasurer.measure(
392-
properties.valueFormatter(selectedBar.dataIndex, selectedBar.valueIndex, selectedBar.bar.value),
392+
properties.contentBuilder(selectedBar.dataIndex, selectedBar.valueIndex, selectedBar.bar.value),
393393
style = properties.textStyle.copy(
394394
color = properties.textStyle.color.copy(
395395
alpha = 1f * progress

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@ data class PopupProperties(
1919
val contentHorizontalPadding: Dp = 4.dp,
2020
val contentVerticalPadding: Dp = 2.dp,
2121
val mode: Mode = Mode.Normal,
22-
@Deprecated(
23-
message = "Use contentBuilderNew instead for enhanced functionality",
24-
replaceWith = ReplaceWith("valueFormatter: (dataIndex: Int, valueIndex: Int, value: Double)")
25-
)
26-
val contentBuilder: (value: Double) -> String = {
27-
it.format(1)
28-
},
29-
val valueFormatter: (dataIndex: Int, valueIndex: Int, value: Double) -> String = { dataIndex, valueIndex, value ->
30-
contentBuilder(value)
22+
val contentBuilder: (valueIndex: Int, lineIndex: Int, value: Double) -> String = { dataIndex, valueIndex, value ->
23+
value.format(1)
3124
}
3225
) {
3326
sealed class Mode {

document/docs/chart-properties/popups.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
!!! info "Usage"
44
In every chart you can set properties of popup which shown when user click or drag on chart
55

6-
| Property | Type | Default | Description |
7-
|-----------------------------------|----------------------|-------------------------|-------------------------------------------------------------------------------------------------|
8-
| `enabled` | Boolean | `true` | specifies popup visibility |
9-
| `animationSpec` | AnimationSpec<Float> | `tween(400)` | specifies popup visibility animation spec |
10-
| `duration` | Long | `1500` | in column/row charts, specifies how long the popup will be visible |
11-
| `textStyle` | TextStyle | `TextStyle.Default` | specifies popup text style |
12-
| `containerColor` | Color | `Color(0xff313131)` | specifies popup background color |
13-
| `cornerRadius` | Dp | `6.dp` | specifies popup corner radius |
14-
| `contentHorizontalPadding` | Dp | `4.dp` | specifies popup horizontal padding |
15-
| `contentVerticalPadding` | Dp | `2.dp` | specifies popup vertical padding |
16-
| `mode` | PopupProperties.Mode | `Mode.Normal` | specifies popup visibility mode ( you can pass PointMode to make popup visible just on points ) |
17-
| `contentBuilder` **(Deprecated)** | (Double)->String | `{ "%.2f".format(it) }` | specifies popup content creation template |
18-
| `valueFormatter` | (String, Double)->String | `{ "%.2f".format(it) }` | specifies popup content creation template |
6+
| Property | Type | Default | Description |
7+
|-----------------------------------|--------------------------|-------------------------|------------------------------------------------------------------------------------------------------|
8+
| `enabled` | Boolean | `true` | specifies popup visibility |
9+
| `animationSpec` | AnimationSpec<Float> | `tween(400)` | specifies popup visibility animation spec |
10+
| `duration` | Long | `1500` | in column/row charts, specifies how long the popup will be visible |
11+
| `textStyle` | TextStyle | `TextStyle.Default` | specifies popup text style |
12+
| `containerColor` | Color | `Color(0xff313131)` | specifies popup background color |
13+
| `cornerRadius` | Dp | `6.dp` | specifies popup corner radius |
14+
| `contentHorizontalPadding` | Dp | `4.dp` | specifies popup horizontal padding |
15+
| `contentVerticalPadding` | Dp | `2.dp` | specifies popup vertical padding |
16+
| `mode` | PopupProperties.Mode | `Mode.Normal` | specifies popup visibility mode ( you can pass PointMode to make popup visible just on points ) |
17+
| `contentBuilder` | (Int,Int,Double)->String | `{ "%.2f".format(it) }` | gives value index, line index and value and expect string to be returned ( specifies popup content ) |
1918

2019
!!! tip "Tip"
2120
In line charts your can set specific popup properties for each line, for example you can disable one
@@ -33,8 +32,10 @@
3332
cornerRadius = 8.dp,
3433
contentHorizontalPadding = 4.dp,
3534
contentVerticalPadding = 2.dp,
36-
valueFormatter = { label, value->
37-
label + ": %.1f".format(value)+" Million"
35+
contentBuilder = { valueIndex, lineIndex, value->
36+
// value index: which value in line?
37+
// line index: which line? (when you have more than one line in your chart)
38+
value.format(1)+" Million"
3839
}
3940
)
4041
```

0 commit comments

Comments
 (0)