werewolf-ui 0.1.0-beta.2 → 0.1.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -5
- package/data/manifest.json +16077 -6044
- package/data/native/android/WerewolfAccordion.kt +256 -0
- package/data/native/android/WerewolfAlert.kt +4 -4
- package/data/native/android/WerewolfAlertDialog.kt +156 -0
- package/data/native/android/WerewolfAttachmentPreview.kt +273 -0
- package/data/native/android/WerewolfAvatar.kt +393 -0
- package/data/native/android/WerewolfAvatarStack.kt +178 -0
- package/data/native/android/WerewolfBackdropBlur.kt +89 -0
- package/data/native/android/WerewolfBadge.kt +71 -3
- package/data/native/android/WerewolfBottomNav.kt +354 -0
- package/data/native/android/WerewolfBreadcrumb.kt +22 -0
- package/data/native/android/WerewolfBreadcrumbRuntime.kt +177 -0
- package/data/native/android/WerewolfButton.kt +12 -3
- package/data/native/android/WerewolfButtonGroup.kt +54 -0
- package/data/native/android/WerewolfCalendar.kt +85 -0
- package/data/native/android/WerewolfCalendarGrid.kt +412 -0
- package/data/native/android/WerewolfCalendarMath.kt +252 -0
- package/data/native/android/WerewolfCard.kt +3 -3
- package/data/native/android/WerewolfCarousel.kt +177 -0
- package/data/native/android/WerewolfChart.kt +200 -0
- package/data/native/android/WerewolfChartAccessibility.kt +69 -0
- package/data/native/android/WerewolfChartFormat.kt +18 -0
- package/data/native/android/WerewolfChartGeometry.kt +397 -0
- package/data/native/android/WerewolfChartInteraction.kt +106 -0
- package/data/native/android/WerewolfChartModel.kt +238 -0
- package/data/native/android/WerewolfChartPalette.kt +34 -0
- package/data/native/android/WerewolfChatRuntime.kt +242 -0
- package/data/native/android/WerewolfCheckbox.kt +387 -0
- package/data/native/android/WerewolfChecklist.kt +249 -0
- package/data/native/android/WerewolfCodeBlock.kt +353 -0
- package/data/native/android/WerewolfColorPicker.kt +84 -0
- package/data/native/android/WerewolfColorPickerRuntime.kt +592 -0
- package/data/native/android/WerewolfCombobox.kt +371 -0
- package/data/native/android/WerewolfComposer.kt +300 -0
- package/data/native/android/WerewolfContextMenu.kt +26 -0
- package/data/native/android/WerewolfContextMenuRuntime.kt +116 -0
- package/data/native/android/WerewolfConversationList.kt +182 -0
- package/data/native/android/WerewolfCounter.kt +640 -0
- package/data/native/android/WerewolfDataTrend.kt +245 -0
- package/data/native/android/WerewolfDatePicker.kt +334 -0
- package/data/native/android/WerewolfDialog.kt +131 -0
- package/data/native/android/WerewolfDropdownMenu.kt +30 -0
- package/data/native/android/WerewolfEmojiData.kt +594 -0
- package/data/native/android/WerewolfEmojiPicker.kt +67 -0
- package/data/native/android/WerewolfEmojiPickerRuntime.kt +627 -0
- package/data/native/android/WerewolfEmptyState.kt +1 -1
- package/data/native/android/WerewolfField.kt +66 -0
- package/data/native/android/WerewolfGroupAvatar.kt +126 -0
- package/data/native/android/WerewolfIcons.kt +448 -1
- package/data/native/android/WerewolfImage.kt +82 -0
- package/data/native/android/WerewolfInput.kt +3 -3
- package/data/native/android/WerewolfKbd.kt +3 -3
- package/data/native/android/WerewolfLabel.kt +2 -2
- package/data/native/android/WerewolfLayers.kt +1173 -0
- package/data/native/android/WerewolfLink.kt +163 -0
- package/data/native/android/WerewolfList.kt +504 -0
- package/data/native/android/WerewolfListRuntime.kt +383 -0
- package/data/native/android/WerewolfMessageActions.kt +86 -0
- package/data/native/android/WerewolfMessageActionsRuntime.kt +568 -0
- package/data/native/android/WerewolfMessageBubble.kt +402 -0
- package/data/native/android/WerewolfMessageList.kt +133 -0
- package/data/native/android/WerewolfMetadata.kt +180 -0
- package/data/native/android/WerewolfMeter.kt +189 -0
- package/data/native/android/WerewolfModifiers.kt +309 -4
- package/data/native/android/WerewolfNumberInput.kt +665 -0
- package/data/native/android/WerewolfOtpInput.kt +246 -0
- package/data/native/android/WerewolfPageRuntime.kt +83 -0
- package/data/native/android/WerewolfPagination.kt +29 -0
- package/data/native/android/WerewolfPaginationRuntime.kt +245 -0
- package/data/native/android/WerewolfPlot.kt +1129 -0
- package/data/native/android/WerewolfPopover.kt +92 -0
- package/data/native/android/WerewolfProgress.kt +281 -0
- package/data/native/android/WerewolfRadioGroup.kt +382 -0
- package/data/native/android/WerewolfRangeMath.kt +80 -0
- package/data/native/android/WerewolfRating.kt +44 -0
- package/data/native/android/WerewolfRatingRuntime.kt +113 -0
- package/data/native/android/WerewolfSelect.kt +330 -0
- package/data/native/android/WerewolfSeparator.kt +1 -1
- package/data/native/android/WerewolfSheet.kt +204 -0
- package/data/native/android/WerewolfSkeleton.kt +4 -4
- package/data/native/android/WerewolfSlider.kt +214 -0
- package/data/native/android/WerewolfSliderRuntime.kt +307 -0
- package/data/native/android/WerewolfSortableList.kt +34 -0
- package/data/native/android/WerewolfSortableListRuntime.kt +232 -0
- package/data/native/android/WerewolfSpinner.kt +1 -1
- package/data/native/android/WerewolfStepper.kt +690 -0
- package/data/native/android/WerewolfSwitch.kt +9 -57
- package/data/native/android/WerewolfTable.kt +134 -0
- package/data/native/android/WerewolfTableRuntime.kt +55 -0
- package/data/native/android/WerewolfTabs.kt +591 -0
- package/data/native/android/WerewolfTextarea.kt +329 -0
- package/data/native/android/WerewolfTheme.kt +17 -3
- package/data/native/android/WerewolfTimeColumns.kt +333 -0
- package/data/native/android/WerewolfTimePicker.kt +319 -0
- package/data/native/android/WerewolfTimeline.kt +382 -0
- package/data/native/android/WerewolfToast.kt +226 -0
- package/data/native/android/WerewolfToaster.kt +281 -0
- package/data/native/android/WerewolfToggle.kt +395 -0
- package/data/native/android/WerewolfToggleGroup.kt +427 -0
- package/data/native/android/WerewolfToken.kt +1 -1
- package/data/native/android/WerewolfTokenizer.kt +54 -0
- package/data/native/android/WerewolfTokenizerRuntime.kt +221 -0
- package/data/native/android/WerewolfTooltip.kt +78 -0
- package/data/native/ios/WerewolfAccordion.swift +340 -0
- package/data/native/ios/WerewolfAlert.swift +3 -3
- package/data/native/ios/WerewolfAlertDialog.swift +187 -0
- package/data/native/ios/WerewolfAttachmentPreview.swift +323 -0
- package/data/native/ios/WerewolfAvatar.swift +552 -0
- package/data/native/ios/WerewolfAvatarStack.swift +214 -0
- package/data/native/ios/WerewolfBadge.swift +74 -22
- package/data/native/ios/WerewolfBottomNav.swift +463 -0
- package/data/native/ios/WerewolfBreadcrumb.swift +22 -0
- package/data/native/ios/WerewolfBreadcrumbRuntime.swift +96 -0
- package/data/native/ios/WerewolfButton.swift +22 -5
- package/data/native/ios/WerewolfButtonGroup.swift +62 -0
- package/data/native/ios/WerewolfCalendar.swift +98 -0
- package/data/native/ios/WerewolfCalendarGrid.swift +453 -0
- package/data/native/ios/WerewolfCalendarMath.swift +321 -0
- package/data/native/ios/WerewolfCard.swift +2 -2
- package/data/native/ios/WerewolfCarousel.swift +168 -0
- package/data/native/ios/WerewolfChart.swift +206 -0
- package/data/native/ios/WerewolfChartAccessibility.swift +59 -0
- package/data/native/ios/WerewolfChartFormat.swift +14 -0
- package/data/native/ios/WerewolfChartGeometry.swift +479 -0
- package/data/native/ios/WerewolfChartInteraction.swift +113 -0
- package/data/native/ios/WerewolfChartModel.swift +337 -0
- package/data/native/ios/WerewolfChartPalette.swift +37 -0
- package/data/native/ios/WerewolfChatRuntime.swift +216 -0
- package/data/native/ios/WerewolfCheckbox.swift +623 -0
- package/data/native/ios/WerewolfChecklist.swift +276 -0
- package/data/native/ios/WerewolfCodeBlock.swift +405 -0
- package/data/native/ios/WerewolfColorPicker.swift +136 -0
- package/data/native/ios/WerewolfColorPickerRuntime.swift +469 -0
- package/data/native/ios/WerewolfCombobox.swift +469 -0
- package/data/native/ios/WerewolfComposer.swift +269 -0
- package/data/native/ios/WerewolfContextMenu.swift +21 -0
- package/data/native/ios/WerewolfConversationList.swift +141 -0
- package/data/native/ios/WerewolfCounter.swift +770 -0
- package/data/native/ios/WerewolfDataTrend.swift +292 -0
- package/data/native/ios/WerewolfDatePicker.swift +388 -0
- package/data/native/ios/WerewolfDialog.swift +172 -0
- package/data/native/ios/WerewolfDropdownMenu.swift +35 -0
- package/data/native/ios/WerewolfEmojiData.swift +600 -0
- package/data/native/ios/WerewolfEmojiPicker.swift +74 -0
- package/data/native/ios/WerewolfEmojiPickerRuntime.swift +361 -0
- package/data/native/ios/WerewolfEmptyState.swift +1 -1
- package/data/native/ios/WerewolfField.swift +103 -0
- package/data/native/ios/WerewolfGroupAvatar.swift +165 -0
- package/data/native/ios/WerewolfIcons.swift +85 -1
- package/data/native/ios/WerewolfImage.swift +118 -0
- package/data/native/ios/WerewolfInput.swift +5 -3
- package/data/native/ios/WerewolfKbd.swift +2 -2
- package/data/native/ios/WerewolfLabel.swift +2 -2
- package/data/native/ios/WerewolfLayers.swift +929 -0
- package/data/native/ios/WerewolfLink.swift +188 -0
- package/data/native/ios/WerewolfList.swift +603 -0
- package/data/native/ios/WerewolfListRuntime.swift +414 -0
- package/data/native/ios/WerewolfMessageActions.swift +99 -0
- package/data/native/ios/WerewolfMessageActionsRuntime.swift +218 -0
- package/data/native/ios/WerewolfMessageBubble.swift +474 -0
- package/data/native/ios/WerewolfMessageList.swift +123 -0
- package/data/native/ios/WerewolfMetadata.swift +205 -0
- package/data/native/ios/WerewolfMeter.swift +186 -0
- package/data/native/ios/WerewolfModifiers.swift +205 -1
- package/data/native/ios/WerewolfNumberInput.swift +808 -0
- package/data/native/ios/WerewolfOtpInput.swift +326 -0
- package/data/native/ios/WerewolfPageRuntime.swift +98 -0
- package/data/native/ios/WerewolfPagination.swift +34 -0
- package/data/native/ios/WerewolfPaginationRuntime.swift +165 -0
- package/data/native/ios/WerewolfPlot.swift +1249 -0
- package/data/native/ios/WerewolfPopover.swift +132 -0
- package/data/native/ios/WerewolfProgress.swift +338 -0
- package/data/native/ios/WerewolfRadioGroup.swift +485 -0
- package/data/native/ios/WerewolfRangeMath.swift +87 -0
- package/data/native/ios/WerewolfRating.swift +52 -0
- package/data/native/ios/WerewolfRatingRuntime.swift +100 -0
- package/data/native/ios/WerewolfSelect.swift +420 -0
- package/data/native/ios/WerewolfSeparator.swift +1 -1
- package/data/native/ios/WerewolfSheet.swift +208 -0
- package/data/native/ios/WerewolfSkeleton.swift +3 -3
- package/data/native/ios/WerewolfSlider.swift +330 -0
- package/data/native/ios/WerewolfSliderRuntime.swift +260 -0
- package/data/native/ios/WerewolfSortableList.swift +40 -0
- package/data/native/ios/WerewolfSortableListRuntime.swift +192 -0
- package/data/native/ios/WerewolfSpinner.swift +1 -1
- package/data/native/ios/WerewolfStepper.swift +802 -0
- package/data/native/ios/WerewolfSwitch.swift +21 -78
- package/data/native/ios/WerewolfTable.swift +185 -0
- package/data/native/ios/WerewolfTableRuntime.swift +47 -0
- package/data/native/ios/WerewolfTabs.swift +758 -0
- package/data/native/ios/WerewolfTextarea.swift +405 -0
- package/data/native/ios/WerewolfTheme.swift +17 -2
- package/data/native/ios/WerewolfTimeColumns.swift +134 -0
- package/data/native/ios/WerewolfTimePicker.swift +365 -0
- package/data/native/ios/WerewolfTimeline.swift +526 -0
- package/data/native/ios/WerewolfToast.swift +280 -0
- package/data/native/ios/WerewolfToaster.swift +269 -0
- package/data/native/ios/WerewolfToggle.swift +647 -0
- package/data/native/ios/WerewolfToggleGroup.swift +511 -0
- package/data/native/ios/WerewolfToken.swift +1 -1
- package/data/native/ios/WerewolfTokenizer.swift +77 -0
- package/data/native/ios/WerewolfTokenizerRuntime.swift +214 -0
- package/data/native/ios/WerewolfTooltip.swift +106 -0
- package/dist/cli.mjs +8573 -672
- package/dist/core.mjs +8279 -566
- package/dist/lint.mjs +205 -21
- package/dist/paths.mjs +2 -1
- package/dist/project.mjs +56 -10
- package/package.json +1 -1
|
@@ -0,0 +1,1129 @@
|
|
|
1
|
+
// Werewolf native runtime. Hand-written, independent of the seed.
|
|
2
|
+
package dev.werewolfui.runtime
|
|
3
|
+
|
|
4
|
+
import androidx.compose.foundation.Canvas
|
|
5
|
+
import androidx.compose.foundation.gestures.detectTapGestures
|
|
6
|
+
import androidx.compose.foundation.interaction.collectIsPressedAsState
|
|
7
|
+
import androidx.compose.foundation.layout.BoxWithConstraints
|
|
8
|
+
import androidx.compose.foundation.layout.fillMaxWidth
|
|
9
|
+
import androidx.compose.foundation.layout.height
|
|
10
|
+
import androidx.compose.foundation.layout.heightIn
|
|
11
|
+
import androidx.compose.foundation.layout.size
|
|
12
|
+
import androidx.compose.foundation.selection.toggleable
|
|
13
|
+
import androidx.compose.runtime.Composable
|
|
14
|
+
import androidx.compose.runtime.getValue
|
|
15
|
+
import androidx.compose.runtime.remember
|
|
16
|
+
import androidx.compose.ui.Modifier
|
|
17
|
+
import androidx.compose.ui.geometry.Offset
|
|
18
|
+
import androidx.compose.ui.graphics.Color
|
|
19
|
+
import androidx.compose.ui.graphics.Path
|
|
20
|
+
import androidx.compose.ui.graphics.PathEffect
|
|
21
|
+
import androidx.compose.ui.graphics.StrokeCap
|
|
22
|
+
import androidx.compose.ui.graphics.StrokeJoin
|
|
23
|
+
import androidx.compose.ui.graphics.drawscope.DrawScope
|
|
24
|
+
import androidx.compose.ui.graphics.drawscope.Stroke
|
|
25
|
+
import androidx.compose.ui.graphics.drawscope.clipPath
|
|
26
|
+
import androidx.compose.ui.graphics.drawscope.clipRect
|
|
27
|
+
import androidx.compose.ui.input.pointer.pointerInput
|
|
28
|
+
import androidx.compose.ui.platform.LocalDensity
|
|
29
|
+
import androidx.compose.ui.platform.LocalLayoutDirection
|
|
30
|
+
import androidx.compose.ui.semantics.contentDescription
|
|
31
|
+
import androidx.compose.ui.semantics.hideFromAccessibility
|
|
32
|
+
import androidx.compose.ui.semantics.semantics
|
|
33
|
+
import androidx.compose.ui.text.AnnotatedString
|
|
34
|
+
import androidx.compose.ui.text.TextStyle
|
|
35
|
+
import androidx.compose.ui.text.drawText
|
|
36
|
+
import androidx.compose.ui.text.rememberTextMeasurer
|
|
37
|
+
import androidx.compose.ui.unit.Dp
|
|
38
|
+
import androidx.compose.ui.unit.LayoutDirection
|
|
39
|
+
import androidx.compose.ui.unit.dp
|
|
40
|
+
|
|
41
|
+
/** Draw timing of a plot, in milliseconds: the time to issue the drawing commands of one pass, not the GPU time. */
|
|
42
|
+
class WerewolfPlotStats {
|
|
43
|
+
var passes = 0
|
|
44
|
+
private set
|
|
45
|
+
var lastMs = 0.0
|
|
46
|
+
private set
|
|
47
|
+
var maxMs = 0.0
|
|
48
|
+
private set
|
|
49
|
+
private var totalMs = 0.0
|
|
50
|
+
|
|
51
|
+
fun record(ms: Double) {
|
|
52
|
+
passes += 1
|
|
53
|
+
lastMs = ms
|
|
54
|
+
if (ms > maxMs) maxMs = ms
|
|
55
|
+
totalMs += ms
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fun reset() {
|
|
59
|
+
passes = 0
|
|
60
|
+
lastMs = 0.0
|
|
61
|
+
maxMs = 0.0
|
|
62
|
+
totalMs = 0.0
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
fun summary(): String {
|
|
66
|
+
val average = if (passes == 0) 0.0 else totalMs / passes
|
|
67
|
+
return "draw ${"%.2f".format(lastMs)} ms last, ${"%.2f".format(average)} avg, ${"%.2f".format(maxMs)} max, $passes passes"
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
internal class WerewolfLineLayout(
|
|
72
|
+
val frame: WerewolfChartFrame,
|
|
73
|
+
val axes: Boolean,
|
|
74
|
+
val domain: WerewolfChartSpan,
|
|
75
|
+
val ticks: List<Double>,
|
|
76
|
+
val tickLabels: List<String>,
|
|
77
|
+
val positions: List<Double>,
|
|
78
|
+
val visible: List<Int>,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
/** The line form's layout in dp, from the same functions as the web: derived domain, nice ticks, gutter, frame, row positions. */
|
|
82
|
+
internal fun werewolfLineLayout(
|
|
83
|
+
width: Double,
|
|
84
|
+
height: Double,
|
|
85
|
+
rows: List<WerewolfChartRow>,
|
|
86
|
+
series: List<WerewolfChartSeries>,
|
|
87
|
+
hidden: Set<String>,
|
|
88
|
+
format: (Double) -> String,
|
|
89
|
+
): WerewolfLineLayout {
|
|
90
|
+
val geometry = WerewolfChartGeometry
|
|
91
|
+
val visible = series.indices.filter { !hidden.contains(series[it].id) }
|
|
92
|
+
val axes = geometry.axesVisible(width, height, false, true)
|
|
93
|
+
val bare = geometry.plotFrame(width, height, axes, 0.0)
|
|
94
|
+
val intervals = geometry.tickIntervals(bare.bottom - bare.top, 48.0)
|
|
95
|
+
val values = rows.flatMap { row -> visible.map { row.values[series[it].id] } }
|
|
96
|
+
val derived = geometry.valueDomain(values, false)
|
|
97
|
+
val domain = if (axes) geometry.niceDomain(derived, intervals) else derived
|
|
98
|
+
val ticks = geometry.chartTicks(domain, intervals)
|
|
99
|
+
val tickLabels = ticks.map(format)
|
|
100
|
+
val frame = geometry.plotFrame(width, height, axes, geometry.valueGutter(tickLabels, width))
|
|
101
|
+
val count = rows.size
|
|
102
|
+
val positions = rows.indices.map { index ->
|
|
103
|
+
if (count <= 1) (frame.left + frame.right) / 2 else geometry.scaleLinear(index.toDouble(), WerewolfChartSpan(0.0, (count - 1).toDouble()), WerewolfChartSpan(frame.left, frame.right))
|
|
104
|
+
}
|
|
105
|
+
return WerewolfLineLayout(frame, axes, domain, ticks, tickLabels, positions, visible)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The line plot: one canvas, one gesture surface, no animation on the geometry. `patterned` fills under each line with the
|
|
110
|
+
* series' clipped stripes, which is the draw cost of a patterned area, and is for measuring.
|
|
111
|
+
*/
|
|
112
|
+
@Composable
|
|
113
|
+
fun WerewolfPlot(
|
|
114
|
+
label: String,
|
|
115
|
+
rows: List<WerewolfChartRow>,
|
|
116
|
+
series: List<WerewolfChartSeries>,
|
|
117
|
+
modifier: Modifier = Modifier,
|
|
118
|
+
inspected: Int? = null,
|
|
119
|
+
onInspect: (Int?) -> Unit = {},
|
|
120
|
+
hidden: Set<String> = emptySet(),
|
|
121
|
+
height: Dp = 256.dp,
|
|
122
|
+
interactive: Boolean = true,
|
|
123
|
+
haptics: Boolean = true,
|
|
124
|
+
patterned: Boolean = false,
|
|
125
|
+
stats: WerewolfPlotStats? = null,
|
|
126
|
+
) {
|
|
127
|
+
val tokens = Werewolf.tokens
|
|
128
|
+
val fonts = LocalWerewolfFonts.current
|
|
129
|
+
val measurer = rememberTextMeasurer()
|
|
130
|
+
val scale = LocalDensity.current.density
|
|
131
|
+
val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl
|
|
132
|
+
val summary = WerewolfChartModel.summary(label, WerewolfChartType.Line, series.size - hidden.size, rows.size)
|
|
133
|
+
val readout = inspected?.let { rows.getOrNull(it) }?.let { row ->
|
|
134
|
+
WerewolfChartModel.readout(row, series, hidden, WerewolfChartLabels()) { value, _ -> WerewolfChartFormat.value(value) }.spoken()
|
|
135
|
+
}
|
|
136
|
+
BoxWithConstraints(modifier.fillMaxWidth().height(height)) {
|
|
137
|
+
val widthDp = maxWidth.value.toDouble()
|
|
138
|
+
val heightDp = height.value.toDouble()
|
|
139
|
+
val layout = remember(widthDp, heightDp, rows, series, hidden) {
|
|
140
|
+
werewolfLineLayout(widthDp, heightDp, rows, series, hidden) { WerewolfChartFormat.compact(it) }
|
|
141
|
+
}
|
|
142
|
+
val positionsPx = layout.positions.map { (it * scale).toFloat() }
|
|
143
|
+
val tickStyle = TextStyle(
|
|
144
|
+
color = tokens.colorTextSecondary,
|
|
145
|
+
fontSize = tokens.fontSizeXs,
|
|
146
|
+
fontFamily = fonts.resolve(WerewolfFontRole.Sans),
|
|
147
|
+
fontFeatureSettings = "tnum",
|
|
148
|
+
)
|
|
149
|
+
Canvas(
|
|
150
|
+
Modifier
|
|
151
|
+
.fillMaxWidth()
|
|
152
|
+
.height(height)
|
|
153
|
+
.werewolfScrub(positionsPx, interactive, haptics, inspected, onInspect)
|
|
154
|
+
.werewolfChartSemantics(label, summary, readout, rows.size, inspected, onInspect),
|
|
155
|
+
) {
|
|
156
|
+
val started = System.nanoTime()
|
|
157
|
+
fun px(value: Double): Float = (value * scale).toFloat()
|
|
158
|
+
fun mirrored(x: Double): Float = px(if (rtl) widthDp - x else x)
|
|
159
|
+
val frame = layout.frame
|
|
160
|
+
val domainRange = WerewolfChartSpan(frame.bottom, frame.top)
|
|
161
|
+
fun yOf(value: Double): Float = px(WerewolfChartGeometry.scaleLinear(value, layout.domain, domainRange))
|
|
162
|
+
|
|
163
|
+
layout.ticks.forEach { tick ->
|
|
164
|
+
val y = yOf(tick)
|
|
165
|
+
drawLine(
|
|
166
|
+
color = tokens.colorBorder,
|
|
167
|
+
start = Offset(mirrored(frame.left), y),
|
|
168
|
+
end = Offset(mirrored(frame.right), y),
|
|
169
|
+
strokeWidth = px(1.0),
|
|
170
|
+
pathEffect = if (tick == 0.0) null else PathEffect.dashPathEffect(floatArrayOf(px(2.0), px(4.0)), 0f),
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
if (layout.axes) {
|
|
174
|
+
layout.ticks.forEachIndexed { index, tick ->
|
|
175
|
+
val result = measurer.measure(AnnotatedString(layout.tickLabels[index]), tickStyle)
|
|
176
|
+
val x = if (rtl) mirrored(frame.left - 6) else mirrored(frame.left - 6) - result.size.width
|
|
177
|
+
drawText(result, topLeft = Offset(x, yOf(tick) - result.size.height / 2f))
|
|
178
|
+
}
|
|
179
|
+
val capacity = WerewolfChartGeometry.labelCapacity(frame.right - frame.left, false)
|
|
180
|
+
WerewolfChartGeometry.shownLabels(rows.size, capacity).forEach { index ->
|
|
181
|
+
val result = measurer.measure(AnnotatedString(rows[index].label), tickStyle)
|
|
182
|
+
val center = mirrored(layout.positions[index])
|
|
183
|
+
val x = if (index == 0) {
|
|
184
|
+
if (rtl) center - result.size.width else center
|
|
185
|
+
} else if (index == rows.size - 1) {
|
|
186
|
+
if (rtl) center else center - result.size.width
|
|
187
|
+
} else {
|
|
188
|
+
center - result.size.width / 2f
|
|
189
|
+
}
|
|
190
|
+
drawText(result, topLeft = Offset(x, px(frame.bottom + 8)))
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
val clipLeft = minOf(mirrored(frame.left), mirrored(frame.right)) - px(4.0)
|
|
195
|
+
val clipRight = maxOf(mirrored(frame.left), mirrored(frame.right)) + px(4.0)
|
|
196
|
+
clipRect(left = clipLeft, top = px(frame.top - 4), right = clipRight, bottom = px(frame.bottom + 4)) {
|
|
197
|
+
layout.visible.forEach { seriesIndex ->
|
|
198
|
+
val item = series[seriesIndex]
|
|
199
|
+
val color = WerewolfChartPalette.fill(tokens, seriesIndex)
|
|
200
|
+
val points = rows.mapIndexed { row, data ->
|
|
201
|
+
data.values[item.id]?.takeIf { it.isFinite() }?.let { WerewolfChartPoint(layout.positions[row], WerewolfChartGeometry.scaleLinear(it, layout.domain, domainRange)) }
|
|
202
|
+
}
|
|
203
|
+
val segments = WerewolfChartGeometry.lineSegments(points)
|
|
204
|
+
val dash = WerewolfChartGeometry.seriesDash(seriesIndex, item.comparison, series.size > 4)
|
|
205
|
+
val effect = dash?.let { PathEffect.dashPathEffect(it.map { length -> px(length) }.toFloatArray(), 0f) }
|
|
206
|
+
segments.forEach { segment ->
|
|
207
|
+
if (patterned && segment.size > 1) drawPattern(segment, frame, seriesIndex, color, scale, rtl, widthDp)
|
|
208
|
+
val path = pathOf(WerewolfChartGeometry.smoothPath(segment), scale, rtl, widthDp)
|
|
209
|
+
drawPath(path, color, style = Stroke(width = px(2.0), cap = StrokeCap.Round, join = StrokeJoin.Round, pathEffect = effect))
|
|
210
|
+
if (segment.size == 1) drawMarker(mirrored(segment[0].x), px(segment[0].y), color, tokens.colorBorderChartMarker, scale)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (inspected != null && inspected in rows.indices) {
|
|
216
|
+
val x = mirrored(layout.positions[inspected])
|
|
217
|
+
drawLine(color = tokens.colorBorderStrong, start = Offset(x, px(frame.top)), end = Offset(x, px(frame.bottom)), strokeWidth = px(1.0))
|
|
218
|
+
layout.visible.forEach { seriesIndex ->
|
|
219
|
+
val value = rows[inspected].values[series[seriesIndex].id]
|
|
220
|
+
if (value != null && value.isFinite()) {
|
|
221
|
+
drawMarker(x, yOf(value), WerewolfChartPalette.fill(tokens, seriesIndex), tokens.colorBorderChartMarker, scale)
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
stats?.record((System.nanoTime() - started) / 1_000_000.0)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private fun pathOf(commands: List<WerewolfChartPathCommand>, scale: Float, rtl: Boolean, width: Double): Path {
|
|
231
|
+
val path = Path()
|
|
232
|
+
fun x(value: Double): Float = ((if (rtl) width - value else value) * scale).toFloat()
|
|
233
|
+
fun y(value: Double): Float = (value * scale).toFloat()
|
|
234
|
+
commands.forEach { command ->
|
|
235
|
+
val v = command.values
|
|
236
|
+
when (command.kind) {
|
|
237
|
+
'M' -> path.moveTo(x(v[0]), y(v[1]))
|
|
238
|
+
'L' -> path.lineTo(x(v[0]), y(v[1]))
|
|
239
|
+
'C' -> path.cubicTo(x(v[0]), y(v[1]), x(v[2]), y(v[3]), x(v[4]), y(v[5]))
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return path
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private fun DrawScope.drawMarker(x: Float, y: Float, fill: Color, ring: Color, scale: Float) {
|
|
246
|
+
val radius = ((WerewolfChartGeometry.MARKER_RADIUS + 1) * scale).toFloat()
|
|
247
|
+
drawCircle(color = fill, radius = radius, center = Offset(x, y))
|
|
248
|
+
drawCircle(color = ring, radius = radius, center = Offset(x, y), style = Stroke(width = 2 * scale))
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private fun DrawScope.drawPattern(
|
|
252
|
+
segment: List<WerewolfChartPoint>,
|
|
253
|
+
frame: WerewolfChartFrame,
|
|
254
|
+
seriesIndex: Int,
|
|
255
|
+
color: Color,
|
|
256
|
+
scale: Float,
|
|
257
|
+
rtl: Boolean,
|
|
258
|
+
width: Double,
|
|
259
|
+
) {
|
|
260
|
+
val outline = pathOf(WerewolfChartGeometry.smoothPath(segment), scale, rtl, width)
|
|
261
|
+
fun x(value: Double): Float = ((if (rtl) width - value else value) * scale).toFloat()
|
|
262
|
+
outline.lineTo(x(segment.last().x), (frame.bottom * scale).toFloat())
|
|
263
|
+
outline.lineTo(x(segment.first().x), (frame.bottom * scale).toFloat())
|
|
264
|
+
outline.close()
|
|
265
|
+
clipPath(outline) {
|
|
266
|
+
WerewolfChartGeometry.patternLines(0.0, frame.top, width, frame.bottom, seriesIndex).forEach { line ->
|
|
267
|
+
drawLine(
|
|
268
|
+
color = color,
|
|
269
|
+
start = Offset(x(line.x1), (line.y1 * scale).toFloat()),
|
|
270
|
+
end = Offset(x(line.x2), (line.y2 * scale).toFloat()),
|
|
271
|
+
strokeWidth = (line.width * scale).toFloat(),
|
|
272
|
+
)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** App-level Differentiate Without Color setting for charts on Android. */
|
|
278
|
+
val LocalWerewolfDifferentiateWithoutColor = androidx.compose.runtime.compositionLocalOf { false }
|
|
279
|
+
|
|
280
|
+
/** Token-derived visual style passed from the generated `WerewolfChart` into the runtime plot. */
|
|
281
|
+
data class WerewolfChartStyle(
|
|
282
|
+
val rootFontSize: androidx.compose.ui.unit.TextUnit,
|
|
283
|
+
val rootColor: Color,
|
|
284
|
+
val rootGap: Dp,
|
|
285
|
+
val readoutLabelFontSize: androidx.compose.ui.unit.TextUnit,
|
|
286
|
+
val readoutLabelColor: Color,
|
|
287
|
+
val readoutValueFontSize: androidx.compose.ui.unit.TextUnit,
|
|
288
|
+
val readoutValueColor: Color,
|
|
289
|
+
val plotAxisColor: Color,
|
|
290
|
+
val plotAxisFontSize: androidx.compose.ui.unit.TextUnit,
|
|
291
|
+
val plotGridColor: Color,
|
|
292
|
+
val plotPendingOpacity: Float,
|
|
293
|
+
val legendGap: Dp,
|
|
294
|
+
val legendItemMinHeight: Dp,
|
|
295
|
+
val legendItemGap: Dp,
|
|
296
|
+
val legendItemRadius: Dp,
|
|
297
|
+
val legendLabelFontSize: androidx.compose.ui.unit.TextUnit,
|
|
298
|
+
val legendLabelColor: Color,
|
|
299
|
+
val legendValueFontSize: androidx.compose.ui.unit.TextUnit,
|
|
300
|
+
val legendValueColor: Color,
|
|
301
|
+
val readoutDurationMs: Int,
|
|
302
|
+
val readoutEasing: dev.werewolfui.tokens.WerewolfEasing,
|
|
303
|
+
val pendingDurationMs: Int,
|
|
304
|
+
val pendingEasing: dev.werewolfui.tokens.WerewolfEasing,
|
|
305
|
+
) {
|
|
306
|
+
companion object {
|
|
307
|
+
fun resolve(
|
|
308
|
+
tokens: dev.werewolfui.tokens.WerewolfTokens,
|
|
309
|
+
rootFontSize: androidx.compose.ui.unit.TextUnit,
|
|
310
|
+
rootColor: Color,
|
|
311
|
+
rootGap: Dp,
|
|
312
|
+
plotPendingOpacity: Float,
|
|
313
|
+
): WerewolfChartStyle = WerewolfChartStyle(
|
|
314
|
+
rootFontSize = rootFontSize,
|
|
315
|
+
rootColor = rootColor,
|
|
316
|
+
rootGap = rootGap,
|
|
317
|
+
readoutLabelFontSize = tokens.fontSizeXs,
|
|
318
|
+
readoutLabelColor = tokens.colorTextSecondary,
|
|
319
|
+
readoutValueFontSize = tokens.fontSizeMd,
|
|
320
|
+
readoutValueColor = tokens.colorTextPrimary,
|
|
321
|
+
plotAxisColor = tokens.colorTextSecondary,
|
|
322
|
+
plotAxisFontSize = tokens.fontSizeXs,
|
|
323
|
+
plotGridColor = tokens.colorBorder,
|
|
324
|
+
plotPendingOpacity = plotPendingOpacity,
|
|
325
|
+
legendGap = tokens.spacingSm,
|
|
326
|
+
legendItemMinHeight = tokens.sizeTargetMin,
|
|
327
|
+
legendItemGap = tokens.spacingXs,
|
|
328
|
+
legendItemRadius = tokens.radiusElement,
|
|
329
|
+
legendLabelFontSize = tokens.fontSizeSm,
|
|
330
|
+
legendLabelColor = tokens.colorTextPrimary,
|
|
331
|
+
legendValueFontSize = tokens.fontSizeSm,
|
|
332
|
+
legendValueColor = tokens.colorTextSecondary,
|
|
333
|
+
readoutDurationMs = tokens.durationFastMs,
|
|
334
|
+
readoutEasing = tokens.easeEnter,
|
|
335
|
+
pendingDurationMs = tokens.durationMediumMs,
|
|
336
|
+
pendingEasing = tokens.easeStandard,
|
|
337
|
+
)
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** The key glyph for one series or part: stroke for lines, swatch for area/bar, dot for composition/donut; hollow when hidden. */
|
|
342
|
+
@Composable
|
|
343
|
+
fun WerewolfChartKey(
|
|
344
|
+
type: WerewolfChartType,
|
|
345
|
+
paletteIndex: Int,
|
|
346
|
+
comparison: Boolean = false,
|
|
347
|
+
distinguish: Boolean = false,
|
|
348
|
+
hidden: Boolean = false,
|
|
349
|
+
isOther: Boolean = false,
|
|
350
|
+
) {
|
|
351
|
+
val tokens = Werewolf.tokens
|
|
352
|
+
val color = if (isOther) tokens.colorBackgroundGray else WerewolfChartPalette.fill(tokens, paletteIndex)
|
|
353
|
+
val muted = tokens.colorTextTertiary
|
|
354
|
+
val scale = LocalDensity.current.density
|
|
355
|
+
if (type == WerewolfChartType.Line || type == WerewolfChartType.Sparkline) {
|
|
356
|
+
Canvas(
|
|
357
|
+
modifier = Modifier.size(16.dp, 10.dp).semantics { hideFromAccessibility() },
|
|
358
|
+
) {
|
|
359
|
+
val y = size.height / 2f
|
|
360
|
+
val dash = WerewolfChartGeometry.seriesDash(paletteIndex, comparison, distinguish)
|
|
361
|
+
val effect = if (hidden) {
|
|
362
|
+
PathEffect.dashPathEffect(floatArrayOf(2f * scale, 2f * scale), 0f)
|
|
363
|
+
} else {
|
|
364
|
+
dash?.let { PathEffect.dashPathEffect(it.map { d -> (d * scale).toFloat() }.toFloatArray(), 0f) }
|
|
365
|
+
}
|
|
366
|
+
drawLine(
|
|
367
|
+
color = if (hidden) muted else color,
|
|
368
|
+
start = Offset(0f, y),
|
|
369
|
+
end = Offset(size.width, y),
|
|
370
|
+
strokeWidth = 2f * scale,
|
|
371
|
+
cap = StrokeCap.Round,
|
|
372
|
+
pathEffect = effect,
|
|
373
|
+
)
|
|
374
|
+
}
|
|
375
|
+
} else if (type == WerewolfChartType.Composition || type == WerewolfChartType.Donut) {
|
|
376
|
+
Canvas(
|
|
377
|
+
modifier = Modifier.size(8.dp).semantics { hideFromAccessibility() },
|
|
378
|
+
) {
|
|
379
|
+
if (hidden) {
|
|
380
|
+
drawCircle(color = muted, style = Stroke(width = 1.5f * scale))
|
|
381
|
+
} else {
|
|
382
|
+
drawCircle(color = color)
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
Canvas(
|
|
387
|
+
modifier = Modifier.size(10.dp).semantics { hideFromAccessibility() },
|
|
388
|
+
) {
|
|
389
|
+
val radius = minOf(tokens.radiusInner.value, 3f) * scale
|
|
390
|
+
val corner = androidx.compose.ui.geometry.CornerRadius(radius, radius)
|
|
391
|
+
if (hidden) {
|
|
392
|
+
drawRoundRect(color = muted, cornerRadius = corner, style = Stroke(width = 1.5f * scale))
|
|
393
|
+
} else {
|
|
394
|
+
drawRoundRect(color = color, cornerRadius = corner)
|
|
395
|
+
if (distinguish) {
|
|
396
|
+
val box = Path().apply {
|
|
397
|
+
addRoundRect(
|
|
398
|
+
androidx.compose.ui.geometry.RoundRect(
|
|
399
|
+
left = 0f,
|
|
400
|
+
top = 0f,
|
|
401
|
+
right = size.width,
|
|
402
|
+
bottom = size.height,
|
|
403
|
+
cornerRadius = corner,
|
|
404
|
+
),
|
|
405
|
+
)
|
|
406
|
+
}
|
|
407
|
+
clipPath(box) {
|
|
408
|
+
WerewolfChartGeometry.patternLines(0.0, 0.0, (size.width / scale).toDouble(), (size.height / scale).toDouble(), paletteIndex).forEach { line ->
|
|
409
|
+
drawLine(
|
|
410
|
+
color = tokens.colorBackgroundSurface,
|
|
411
|
+
start = Offset((line.x1 * scale).toFloat(), (line.y1 * scale).toFloat()),
|
|
412
|
+
end = Offset((line.x2 * scale).toFloat(), (line.y2 * scale).toFloat()),
|
|
413
|
+
strokeWidth = (line.width * scale).toFloat(),
|
|
414
|
+
)
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
object WerewolfChartFormatting {
|
|
424
|
+
fun formatNumber(value: Double, id: String, formatValue: ((Double, String) -> String)?, locale: java.util.Locale): String {
|
|
425
|
+
if (formatValue != null) return formatValue(value, id)
|
|
426
|
+
return WerewolfChartFormat.value(value, locale)
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
fun formatPercent(share: Double, locale: java.util.Locale): String {
|
|
430
|
+
val fmt = java.text.NumberFormat.getPercentInstance(locale)
|
|
431
|
+
fmt.maximumFractionDigits = 1
|
|
432
|
+
return fmt.format(share)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
fun formatPercentAt(shares: List<Double?>, index: Int, locale: java.util.Locale): String =
|
|
436
|
+
formatPercent(shares.getOrNull(index) ?: 0.0, locale)
|
|
437
|
+
|
|
438
|
+
fun formatRowX(row: WerewolfChartRow, xScale: WerewolfChartXScale, formatX: ((Double) -> String)?, locale: java.util.Locale): String {
|
|
439
|
+
val x = row.x
|
|
440
|
+
if (xScale == WerewolfChartXScale.Category || x == null) return row.label
|
|
441
|
+
if (formatX != null) return formatX(x)
|
|
442
|
+
if (xScale == WerewolfChartXScale.Time) {
|
|
443
|
+
val fmt = java.text.SimpleDateFormat("MMM d", locale)
|
|
444
|
+
fmt.timeZone = java.util.TimeZone.getTimeZone("UTC")
|
|
445
|
+
return fmt.format(java.util.Date(x.toLong()))
|
|
446
|
+
}
|
|
447
|
+
return WerewolfChartFormat.compact(x, locale)
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
fun partReadout(
|
|
451
|
+
items: List<WerewolfChartItem>,
|
|
452
|
+
inspected: Int?,
|
|
453
|
+
labels: WerewolfChartLabels,
|
|
454
|
+
locale: java.util.Locale,
|
|
455
|
+
formatValue: ((Double, String) -> String)?,
|
|
456
|
+
): WerewolfChartReadout {
|
|
457
|
+
val total = items.sumOf { it.value }
|
|
458
|
+
val shares = WerewolfChartGeometry.normalizedShares(items.map { it.value })
|
|
459
|
+
if (inspected != null && inspected in items.indices) {
|
|
460
|
+
val item = items[inspected]
|
|
461
|
+
val share = shares[inspected] ?: 0.0
|
|
462
|
+
return WerewolfChartReadout(
|
|
463
|
+
title = item.label,
|
|
464
|
+
entries = listOf(
|
|
465
|
+
WerewolfChartReadoutEntry(item.id, labels.value, formatNumber(item.value, item.id, formatValue, locale)),
|
|
466
|
+
WerewolfChartReadoutEntry("${item.id}-share", labels.share, formatPercent(share, locale)),
|
|
467
|
+
),
|
|
468
|
+
)
|
|
469
|
+
}
|
|
470
|
+
return WerewolfChartReadout(
|
|
471
|
+
title = labels.total,
|
|
472
|
+
entries = listOf(WerewolfChartReadoutEntry("total", labels.total, formatNumber(total, "total", formatValue, locale))),
|
|
473
|
+
)
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
private class WerewolfCartesianPlan(
|
|
478
|
+
val frame: WerewolfChartFrame,
|
|
479
|
+
val axes: Boolean,
|
|
480
|
+
val grid: Boolean,
|
|
481
|
+
val horizontal: Boolean,
|
|
482
|
+
val domain: WerewolfChartSpan,
|
|
483
|
+
val ticks: List<Double>,
|
|
484
|
+
val tickLabels: List<String>,
|
|
485
|
+
val positions: List<Double>,
|
|
486
|
+
val visibleIndices: List<Int>,
|
|
487
|
+
val stacks: List<List<WerewolfChartStackValue?>>,
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
/** Full v1 plot renderer supporting `line`, `area`, `bar`, `sparkline`, `composition`, and `donut`. */
|
|
491
|
+
@Composable
|
|
492
|
+
fun WerewolfChartPlotView(
|
|
493
|
+
type: WerewolfChartType,
|
|
494
|
+
label: String,
|
|
495
|
+
data: WerewolfChartData,
|
|
496
|
+
hidden: Set<String>,
|
|
497
|
+
height: Dp,
|
|
498
|
+
interactive: Boolean,
|
|
499
|
+
haptics: Boolean,
|
|
500
|
+
pending: Boolean,
|
|
501
|
+
locale: java.util.Locale,
|
|
502
|
+
labels: WerewolfChartLabels,
|
|
503
|
+
formatValue: ((Double, String) -> String)?,
|
|
504
|
+
xScale: WerewolfChartXScale,
|
|
505
|
+
formatX: ((Double) -> String)?,
|
|
506
|
+
domain: WerewolfChartSpan?,
|
|
507
|
+
axes: Boolean,
|
|
508
|
+
grid: Boolean,
|
|
509
|
+
stack: WerewolfChartStack,
|
|
510
|
+
orientation: WerewolfChartOrientation,
|
|
511
|
+
layout: WerewolfChartLayout,
|
|
512
|
+
style: WerewolfChartStyle,
|
|
513
|
+
inspected: Int?,
|
|
514
|
+
onInspect: (Int?) -> Unit,
|
|
515
|
+
modifier: Modifier = Modifier,
|
|
516
|
+
) {
|
|
517
|
+
val plotHeight = if (type == WerewolfChartType.Composition) (12 + WerewolfChartGeometry.ACTIVE_GROWTH * 2).dp else height
|
|
518
|
+
when (data) {
|
|
519
|
+
is WerewolfChartData.Rows -> CartesianPlotCanvas(
|
|
520
|
+
type = type,
|
|
521
|
+
label = label,
|
|
522
|
+
rows = data.rows,
|
|
523
|
+
series = data.series,
|
|
524
|
+
hidden = hidden,
|
|
525
|
+
height = plotHeight,
|
|
526
|
+
interactive = interactive,
|
|
527
|
+
haptics = haptics,
|
|
528
|
+
pending = pending,
|
|
529
|
+
locale = locale,
|
|
530
|
+
labels = labels,
|
|
531
|
+
formatValue = formatValue,
|
|
532
|
+
xScale = xScale,
|
|
533
|
+
formatX = formatX,
|
|
534
|
+
domain = domain,
|
|
535
|
+
axes = axes,
|
|
536
|
+
grid = grid,
|
|
537
|
+
stack = stack,
|
|
538
|
+
orientation = orientation,
|
|
539
|
+
layout = layout,
|
|
540
|
+
style = style,
|
|
541
|
+
inspected = inspected,
|
|
542
|
+
onInspect = onInspect,
|
|
543
|
+
modifier = modifier,
|
|
544
|
+
)
|
|
545
|
+
is WerewolfChartData.Parts -> PartsPlotCanvas(
|
|
546
|
+
type = type,
|
|
547
|
+
items = data.items,
|
|
548
|
+
height = plotHeight,
|
|
549
|
+
interactive = interactive,
|
|
550
|
+
locale = locale,
|
|
551
|
+
labels = labels,
|
|
552
|
+
formatValue = formatValue,
|
|
553
|
+
style = style,
|
|
554
|
+
inspected = inspected,
|
|
555
|
+
onInspect = onInspect,
|
|
556
|
+
modifier = modifier,
|
|
557
|
+
)
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
@Composable
|
|
562
|
+
private fun CartesianPlotCanvas(
|
|
563
|
+
type: WerewolfChartType,
|
|
564
|
+
label: String,
|
|
565
|
+
rows: List<WerewolfChartRow>,
|
|
566
|
+
series: List<WerewolfChartSeries>,
|
|
567
|
+
hidden: Set<String>,
|
|
568
|
+
height: Dp,
|
|
569
|
+
interactive: Boolean,
|
|
570
|
+
haptics: Boolean,
|
|
571
|
+
pending: Boolean,
|
|
572
|
+
locale: java.util.Locale,
|
|
573
|
+
labels: WerewolfChartLabels,
|
|
574
|
+
formatValue: ((Double, String) -> String)?,
|
|
575
|
+
xScale: WerewolfChartXScale,
|
|
576
|
+
formatX: ((Double) -> String)?,
|
|
577
|
+
domain: WerewolfChartSpan?,
|
|
578
|
+
axes: Boolean,
|
|
579
|
+
grid: Boolean,
|
|
580
|
+
stack: WerewolfChartStack,
|
|
581
|
+
orientation: WerewolfChartOrientation,
|
|
582
|
+
layout: WerewolfChartLayout,
|
|
583
|
+
style: WerewolfChartStyle,
|
|
584
|
+
inspected: Int?,
|
|
585
|
+
onInspect: (Int?) -> Unit,
|
|
586
|
+
modifier: Modifier,
|
|
587
|
+
) {
|
|
588
|
+
val tokens = Werewolf.tokens
|
|
589
|
+
val fonts = LocalWerewolfFonts.current
|
|
590
|
+
val measurer = rememberTextMeasurer()
|
|
591
|
+
val scale = LocalDensity.current.density
|
|
592
|
+
val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl
|
|
593
|
+
val largeText = rememberLargeText()
|
|
594
|
+
val differentiate = LocalWerewolfDifferentiateWithoutColor.current
|
|
595
|
+
val isSparkline = type == WerewolfChartType.Sparkline
|
|
596
|
+
val showAxes = !isSparkline && axes
|
|
597
|
+
val showGrid = !isSparkline && grid
|
|
598
|
+
val effectiveInteractive = !isSparkline && interactive
|
|
599
|
+
val horizontal = type == WerewolfChartType.Bar && orientation == WerewolfChartOrientation.Horizontal
|
|
600
|
+
|
|
601
|
+
BoxWithConstraints(modifier.fillMaxWidth().height(height)) {
|
|
602
|
+
val widthDp = maxWidth.value.toDouble()
|
|
603
|
+
val heightDp = height.value.toDouble()
|
|
604
|
+
val plan = remember(widthDp, heightDp, type, rows, series, hidden, showAxes, showGrid, horizontal, xScale, domain, stack, layout, locale) {
|
|
605
|
+
val g = WerewolfChartGeometry
|
|
606
|
+
val visible = series.indices.filter { !hidden.contains(series[it].id) }
|
|
607
|
+
val axesOn = g.axesVisible(widthDp, heightDp, isSparkline, showAxes)
|
|
608
|
+
val bare = g.plotFrame(widthDp, heightDp, axesOn, 0.0)
|
|
609
|
+
val percentage = type == WerewolfChartType.Bar && layout == WerewolfChartLayout.Percentage
|
|
610
|
+
val stacked = (type == WerewolfChartType.Area && stack == WerewolfChartStack.Normal) || (type == WerewolfChartType.Bar && layout != WerewolfChartLayout.Grouped)
|
|
611
|
+
val stacks = rows.map { row ->
|
|
612
|
+
val raw = visible.map { row.values[series[it].id] }
|
|
613
|
+
if (type == WerewolfChartType.Area && stack == WerewolfChartStack.Normal && raw.contains(null)) {
|
|
614
|
+
raw.map { null }
|
|
615
|
+
} else {
|
|
616
|
+
val normalized = if (percentage) g.normalizedShares(raw) else raw
|
|
617
|
+
if (stacked) g.stackValues(normalized) else normalized.map { v -> v?.let { WerewolfChartStackValue(0.0, it) } }
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
val flatValues = stacks.flatMap { rowStacks -> rowStacks.mapNotNull { it?.end } }
|
|
621
|
+
val categoryBand = g.categoryGutter(widthDp)
|
|
622
|
+
val intervals = if (horizontal) {
|
|
623
|
+
g.tickIntervals(widthDp - (if (axesOn) 12.0 else 5.0) - (if (axesOn) categoryBand else 5.0), 80.0)
|
|
624
|
+
} else {
|
|
625
|
+
g.tickIntervals(bare.bottom - bare.top, 48.0)
|
|
626
|
+
}
|
|
627
|
+
val derived = g.valueDomain(flatValues, type == WerewolfChartType.Bar || type == WerewolfChartType.Area)
|
|
628
|
+
val resolvedDomain = domain ?: if (percentage) WerewolfChartSpan(0.0, 100.0) else if (axesOn) g.niceDomain(derived, intervals) else derived
|
|
629
|
+
val ticks = g.chartTicks(resolvedDomain, intervals)
|
|
630
|
+
val tickLabels = ticks.map { tick ->
|
|
631
|
+
if (percentage) WerewolfChartFormatting.formatPercent(tick / 100.0, locale) else (formatValue?.invoke(tick, series.firstOrNull()?.id ?: "") ?: WerewolfChartFormat.compact(tick, locale))
|
|
632
|
+
}
|
|
633
|
+
val gutter = if (horizontal) categoryBand else g.valueGutter(tickLabels, widthDp)
|
|
634
|
+
val frame = g.plotFrame(widthDp, heightDp, axesOn, gutter)
|
|
635
|
+
val start = if (horizontal) frame.top else frame.left
|
|
636
|
+
val end = if (horizontal) frame.bottom else frame.right
|
|
637
|
+
val count = rows.size
|
|
638
|
+
val positions = if (xScale != WerewolfChartXScale.Category && count > 0) {
|
|
639
|
+
val first = rows[0].x ?: 0.0
|
|
640
|
+
val last = rows[count - 1].x ?: first
|
|
641
|
+
val inset = if (type == WerewolfChartType.Bar) (end - start) / maxOf(1, count) / 2.0 else 0.0
|
|
642
|
+
rows.map { g.scaleLinear(it.x ?: first, WerewolfChartSpan(first, last), WerewolfChartSpan(start + inset, end - inset)) }
|
|
643
|
+
} else if (type == WerewolfChartType.Bar) {
|
|
644
|
+
rows.indices.map { index -> start + ((index + 0.5) * (end - start)) / maxOf(1, count) }
|
|
645
|
+
} else {
|
|
646
|
+
rows.indices.map { index ->
|
|
647
|
+
if (count <= 1) (start + end) / 2 else g.scaleLinear(index.toDouble(), WerewolfChartSpan(0.0, (count - 1).toDouble()), WerewolfChartSpan(start, end))
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
WerewolfCartesianPlan(frame, axesOn, !isSparkline && showGrid, horizontal, resolvedDomain, ticks, tickLabels, positions, visible, stacks)
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
val positionsPx = plan.positions.map { (it * scale).toFloat() }
|
|
654
|
+
val summary = WerewolfChartModel.summary(label, type, plan.visibleIndices.size, rows.size)
|
|
655
|
+
val readout = inspected?.let { rows.getOrNull(it) }?.let { row ->
|
|
656
|
+
WerewolfChartModel.readout(row, series, hidden, labels) { value, id ->
|
|
657
|
+
WerewolfChartFormatting.formatNumber(value, id, formatValue, locale)
|
|
658
|
+
}.spoken()
|
|
659
|
+
}
|
|
660
|
+
val tickStyle = TextStyle(
|
|
661
|
+
color = style.plotAxisColor,
|
|
662
|
+
fontSize = style.plotAxisFontSize,
|
|
663
|
+
fontFamily = fonts.resolve(WerewolfFontRole.Sans),
|
|
664
|
+
fontFeatureSettings = "tnum",
|
|
665
|
+
)
|
|
666
|
+
val tapModifier = if (effectiveInteractive && horizontal) {
|
|
667
|
+
Modifier.pointerInput(plan.positions, scale) {
|
|
668
|
+
detectTapGestures { offset ->
|
|
669
|
+
val yDp = (offset.y / scale).toDouble()
|
|
670
|
+
val row = WerewolfChartGeometry.nearestIndex(plan.positions, yDp)
|
|
671
|
+
onInspect(if (row == inspected) null else row)
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
} else {
|
|
675
|
+
Modifier
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
Canvas(
|
|
679
|
+
Modifier
|
|
680
|
+
.fillMaxWidth()
|
|
681
|
+
.height(height)
|
|
682
|
+
.werewolfScrub(if (horizontal) emptyList() else positionsPx, effectiveInteractive && !horizontal, haptics, inspected, onInspect)
|
|
683
|
+
.then(tapModifier)
|
|
684
|
+
.werewolfChartSemantics(label, summary, if (pending) labels.loading else readout, rows.size, inspected, onInspect),
|
|
685
|
+
) {
|
|
686
|
+
fun px(value: Double): Float = (value * scale).toFloat()
|
|
687
|
+
fun mirrored(x: Double): Float = px(if (rtl) widthDp - x else x)
|
|
688
|
+
val frame = plan.frame
|
|
689
|
+
val valueRange = if (plan.horizontal) WerewolfChartSpan(frame.left, frame.right) else WerewolfChartSpan(frame.bottom, frame.top)
|
|
690
|
+
fun vPos(value: Double): Double = WerewolfChartGeometry.scaleLinear(value, plan.domain, valueRange)
|
|
691
|
+
val distinguish = series.size > 4 || differentiate
|
|
692
|
+
|
|
693
|
+
plan.ticks.forEachIndexed { index, tick ->
|
|
694
|
+
val pos = vPos(tick)
|
|
695
|
+
if (plan.grid) {
|
|
696
|
+
drawLine(
|
|
697
|
+
color = style.plotGridColor,
|
|
698
|
+
start = if (plan.horizontal) Offset(mirrored(pos), px(frame.top)) else Offset(mirrored(frame.left), px(pos)),
|
|
699
|
+
end = if (plan.horizontal) Offset(mirrored(pos), px(frame.bottom)) else Offset(mirrored(frame.right), px(pos)),
|
|
700
|
+
strokeWidth = px(1.0),
|
|
701
|
+
pathEffect = if (tick == 0.0) null else PathEffect.dashPathEffect(floatArrayOf(px(2.0), px(4.0)), 0f),
|
|
702
|
+
)
|
|
703
|
+
}
|
|
704
|
+
if (plan.axes) {
|
|
705
|
+
val result = measurer.measure(AnnotatedString(plan.tickLabels[index]), tickStyle)
|
|
706
|
+
if (plan.horizontal) {
|
|
707
|
+
val cx = mirrored(pos)
|
|
708
|
+
val x = when {
|
|
709
|
+
index == 0 -> if (rtl) cx - result.size.width else cx
|
|
710
|
+
index == plan.ticks.lastIndex -> if (rtl) cx else cx - result.size.width
|
|
711
|
+
else -> cx - result.size.width / 2f
|
|
712
|
+
}
|
|
713
|
+
drawText(result, topLeft = Offset(x, px(frame.bottom + 8)))
|
|
714
|
+
} else {
|
|
715
|
+
val x = if (rtl) mirrored(frame.left - 6) else mirrored(frame.left - 6) - result.size.width
|
|
716
|
+
drawText(result, topLeft = Offset(x, px(pos) - result.size.height / 2f))
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
if (plan.axes) {
|
|
721
|
+
val axisLength = if (plan.horizontal) frame.bottom - frame.top else frame.right - frame.left
|
|
722
|
+
val capacity = if (largeText) 2 else WerewolfChartGeometry.labelCapacity(axisLength, plan.horizontal)
|
|
723
|
+
WerewolfChartGeometry.shownLabels(rows.size, capacity).forEach { index ->
|
|
724
|
+
val labelText = WerewolfChartFormatting.formatRowX(rows[index], xScale, formatX, locale)
|
|
725
|
+
val result = measurer.measure(AnnotatedString(labelText), tickStyle)
|
|
726
|
+
if (plan.horizontal) {
|
|
727
|
+
val x = if (rtl) mirrored(frame.left - 6) else mirrored(frame.left - 6) - result.size.width
|
|
728
|
+
drawText(result, topLeft = Offset(x, px(plan.positions[index]) - result.size.height / 2f))
|
|
729
|
+
} else {
|
|
730
|
+
val center = mirrored(plan.positions[index])
|
|
731
|
+
val x = when {
|
|
732
|
+
type == WerewolfChartType.Bar -> center - result.size.width / 2f
|
|
733
|
+
index == 0 -> if (rtl) center - result.size.width else center
|
|
734
|
+
index == rows.lastIndex -> if (rtl) center else center - result.size.width
|
|
735
|
+
else -> center - result.size.width / 2f
|
|
736
|
+
}
|
|
737
|
+
drawText(result, topLeft = Offset(x, px(frame.bottom + 8)))
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (plan.domain.from < 0.0 && plan.domain.to > 0.0) {
|
|
741
|
+
val zero = vPos(0.0)
|
|
742
|
+
drawLine(
|
|
743
|
+
color = tokens.colorBorderStrong,
|
|
744
|
+
start = if (plan.horizontal) Offset(mirrored(zero), px(frame.top)) else Offset(mirrored(frame.left), px(zero)),
|
|
745
|
+
end = if (plan.horizontal) Offset(mirrored(zero), px(frame.bottom)) else Offset(mirrored(frame.right), px(zero)),
|
|
746
|
+
strokeWidth = px(1.0),
|
|
747
|
+
)
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
val clipLeft = minOf(mirrored(frame.left), mirrored(frame.right)) - px(4.0)
|
|
752
|
+
val clipRight = maxOf(mirrored(frame.left), mirrored(frame.right)) + px(4.0)
|
|
753
|
+
clipRect(left = clipLeft, top = px(frame.top - 4), right = clipRight, bottom = px(frame.bottom + 4)) {
|
|
754
|
+
if (type == WerewolfChartType.Bar) {
|
|
755
|
+
val grouped = layout == WerewolfChartLayout.Grouped
|
|
756
|
+
val step = (if (plan.horizontal) frame.bottom - frame.top else frame.right - frame.left) / maxOf(1, rows.size)
|
|
757
|
+
val band = WerewolfChartGeometry.barBand(step, plan.visibleIndices.size, grouped)
|
|
758
|
+
val breadthInfo = WerewolfChartGeometry.barBreadth(band, plan.visibleIndices.size, grouped)
|
|
759
|
+
plan.visibleIndices.forEachIndexed { slot, seriesIndex ->
|
|
760
|
+
val color = WerewolfChartPalette.fill(tokens, seriesIndex)
|
|
761
|
+
rows.indices.forEach { rowIndex ->
|
|
762
|
+
val seg = plan.stacks[rowIndex][slot]
|
|
763
|
+
if (seg != null && seg.start != seg.end) {
|
|
764
|
+
val startPos = vPos(seg.start)
|
|
765
|
+
val endPos = vPos(seg.end)
|
|
766
|
+
val cross = plan.positions[rowIndex] - band / 2.0 + (if (grouped) slot * breadthInfo.breadth + breadthInfo.gutter / 2.0 else 0.0)
|
|
767
|
+
val barThickness = maxOf(0.0, breadthInfo.breadth - breadthInfo.gutter)
|
|
768
|
+
val minV = minOf(startPos, endPos)
|
|
769
|
+
val lenV = kotlin.math.abs(endPos - startPos)
|
|
770
|
+
val leftPx = if (plan.horizontal) px(if (rtl) widthDp - (minV + lenV) else minV) else minOf(mirrored(cross), mirrored(cross + barThickness))
|
|
771
|
+
val topPx = if (plan.horizontal) px(cross) else px(minV)
|
|
772
|
+
val widthPx = if (plan.horizontal) px(lenV) else px(barThickness)
|
|
773
|
+
val heightPx = if (plan.horizontal) px(barThickness) else px(lenV)
|
|
774
|
+
val radiusPx = minOf(tokens.radiusInner.value * scale, minOf(widthPx, heightPx) / 2f)
|
|
775
|
+
val corner = androidx.compose.ui.geometry.CornerRadius(radiusPx, radiusPx)
|
|
776
|
+
drawRoundRect(color = color, topLeft = Offset(leftPx, topPx), size = androidx.compose.ui.geometry.Size(widthPx, heightPx), cornerRadius = corner)
|
|
777
|
+
if (distinguish) {
|
|
778
|
+
val barPath = Path().apply {
|
|
779
|
+
addRoundRect(androidx.compose.ui.geometry.RoundRect(leftPx, topPx, leftPx + widthPx, topPx + heightPx, corner))
|
|
780
|
+
}
|
|
781
|
+
clipPath(barPath) {
|
|
782
|
+
WerewolfChartGeometry.patternLines((leftPx / scale).toDouble(), (topPx / scale).toDouble(), ((leftPx + widthPx) / scale).toDouble(), ((topPx + heightPx) / scale).toDouble(), seriesIndex).forEach { stripe ->
|
|
783
|
+
drawLine(
|
|
784
|
+
color = tokens.colorBackgroundSurface,
|
|
785
|
+
start = Offset(px(stripe.x1), px(stripe.y1)),
|
|
786
|
+
end = Offset(px(stripe.x2), px(stripe.y2)),
|
|
787
|
+
strokeWidth = px(stripe.width),
|
|
788
|
+
)
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
} else {
|
|
796
|
+
plan.visibleIndices.forEachIndexed { slot, seriesIndex ->
|
|
797
|
+
val item = series[seriesIndex]
|
|
798
|
+
val color = WerewolfChartPalette.fill(tokens, seriesIndex)
|
|
799
|
+
val soft = WerewolfChartPalette.soft(tokens, seriesIndex)
|
|
800
|
+
val topPoints = rows.indices.map { row ->
|
|
801
|
+
plan.stacks[row][slot]?.let { WerewolfChartPoint(plan.positions[row], vPos(it.end)) }
|
|
802
|
+
}
|
|
803
|
+
val bottomPoints = rows.indices.map { row ->
|
|
804
|
+
plan.stacks[row][slot]?.let { WerewolfChartPoint(plan.positions[row], vPos(it.start)) }
|
|
805
|
+
}
|
|
806
|
+
if (type == WerewolfChartType.Area) {
|
|
807
|
+
val run = mutableListOf<Int>()
|
|
808
|
+
fun flushArea() {
|
|
809
|
+
if (run.isEmpty()) return
|
|
810
|
+
val upper = run.mapNotNull { topPoints[it] }
|
|
811
|
+
val lower = run.reversed().mapNotNull { bottomPoints[it] }
|
|
812
|
+
if (upper.size > 1) {
|
|
813
|
+
val areaPath = pathOf(WerewolfChartGeometry.smoothPath(upper), scale, rtl, widthDp)
|
|
814
|
+
WerewolfChartGeometry.smoothPath(lower).forEach { cmd ->
|
|
815
|
+
val v = cmd.values
|
|
816
|
+
when (cmd.kind) {
|
|
817
|
+
'M', 'L' -> areaPath.lineTo(mirrored(v[0]), px(v[1]))
|
|
818
|
+
'C' -> areaPath.cubicTo(mirrored(v[0]), px(v[1]), mirrored(v[2]), px(v[3]), mirrored(v[4]), px(v[5]))
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
areaPath.close()
|
|
822
|
+
if (distinguish) {
|
|
823
|
+
drawPath(areaPath, soft)
|
|
824
|
+
clipPath(areaPath) {
|
|
825
|
+
WerewolfChartGeometry.patternLines(0.0, frame.top, widthDp, frame.bottom, seriesIndex).forEach { stripe ->
|
|
826
|
+
drawLine(
|
|
827
|
+
color = color,
|
|
828
|
+
start = Offset(mirrored(stripe.x1), px(stripe.y1)),
|
|
829
|
+
end = Offset(mirrored(stripe.x2), px(stripe.y2)),
|
|
830
|
+
strokeWidth = px(stripe.width),
|
|
831
|
+
)
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
} else {
|
|
835
|
+
drawPath(
|
|
836
|
+
areaPath,
|
|
837
|
+
brush = androidx.compose.ui.graphics.Brush.verticalGradient(
|
|
838
|
+
colors = listOf(soft, soft.copy(alpha = 0f)),
|
|
839
|
+
startY = px(frame.top),
|
|
840
|
+
endY = px(frame.bottom),
|
|
841
|
+
),
|
|
842
|
+
)
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
run.clear()
|
|
846
|
+
}
|
|
847
|
+
rows.indices.forEach { row ->
|
|
848
|
+
if (topPoints[row] != null) run.add(row) else flushArea()
|
|
849
|
+
}
|
|
850
|
+
flushArea()
|
|
851
|
+
}
|
|
852
|
+
val dash = WerewolfChartGeometry.seriesDash(seriesIndex, item.comparison, distinguish)
|
|
853
|
+
val effect = dash?.let { PathEffect.dashPathEffect(it.map { d -> px(d) }.toFloatArray(), 0f) }
|
|
854
|
+
WerewolfChartGeometry.lineSegments(topPoints).forEach { segment ->
|
|
855
|
+
val outline = pathOf(WerewolfChartGeometry.smoothPath(segment), scale, rtl, widthDp)
|
|
856
|
+
drawPath(outline, color, style = Stroke(width = px(2.0), cap = StrokeCap.Round, join = StrokeJoin.Round, pathEffect = effect))
|
|
857
|
+
if (segment.size == 1) drawMarker(mirrored(segment[0].x), px(segment[0].y), color, tokens.colorBorderChartMarker, scale)
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (inspected != null && inspected in rows.indices && !isSparkline) {
|
|
864
|
+
if (plan.horizontal) {
|
|
865
|
+
val y = px(plan.positions[inspected])
|
|
866
|
+
drawLine(color = tokens.colorBorderStrong, start = Offset(mirrored(frame.left), y), end = Offset(mirrored(frame.right), y), strokeWidth = px(1.0))
|
|
867
|
+
} else {
|
|
868
|
+
val x = mirrored(plan.positions[inspected])
|
|
869
|
+
drawLine(color = tokens.colorBorderStrong, start = Offset(x, px(frame.top)), end = Offset(x, px(frame.bottom)), strokeWidth = px(1.0))
|
|
870
|
+
if (type != WerewolfChartType.Bar) {
|
|
871
|
+
plan.visibleIndices.forEachIndexed { slot, seriesIndex ->
|
|
872
|
+
plan.stacks[inspected][slot]?.let { seg ->
|
|
873
|
+
drawMarker(x, px(vPos(seg.end)), WerewolfChartPalette.fill(tokens, seriesIndex), tokens.colorBorderChartMarker, scale)
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
@Composable
|
|
884
|
+
private fun PartsPlotCanvas(
|
|
885
|
+
type: WerewolfChartType,
|
|
886
|
+
items: List<WerewolfChartItem>,
|
|
887
|
+
height: Dp,
|
|
888
|
+
interactive: Boolean,
|
|
889
|
+
locale: java.util.Locale,
|
|
890
|
+
labels: WerewolfChartLabels,
|
|
891
|
+
formatValue: ((Double, String) -> String)?,
|
|
892
|
+
style: WerewolfChartStyle,
|
|
893
|
+
inspected: Int?,
|
|
894
|
+
onInspect: (Int?) -> Unit,
|
|
895
|
+
modifier: Modifier,
|
|
896
|
+
) {
|
|
897
|
+
val tokens = Werewolf.tokens
|
|
898
|
+
val fonts = LocalWerewolfFonts.current
|
|
899
|
+
val measurer = rememberTextMeasurer()
|
|
900
|
+
val scale = LocalDensity.current.density
|
|
901
|
+
val rtl = LocalLayoutDirection.current == LayoutDirection.Rtl
|
|
902
|
+
val differentiate = LocalWerewolfDifferentiateWithoutColor.current
|
|
903
|
+
val shares = remember(items) { WerewolfChartGeometry.normalizedShares(items.map { it.value }).map { it ?: 0.0 } }
|
|
904
|
+
val coloredCount = remember(items) { items.count { it.role != WerewolfChartItemRole.Other } }
|
|
905
|
+
val paletteIndices = remember(items) {
|
|
906
|
+
var slot = 0
|
|
907
|
+
items.map { item -> if (item.role == WerewolfChartItemRole.Other) -1 else slot++ }
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
BoxWithConstraints(modifier.fillMaxWidth().height(height)) {
|
|
911
|
+
val widthDp = maxWidth.value.toDouble()
|
|
912
|
+
val heightDp = height.value.toDouble()
|
|
913
|
+
val tapModifier = if (interactive) {
|
|
914
|
+
Modifier.pointerInput(type, shares, widthDp, heightDp, rtl, inspected) {
|
|
915
|
+
detectTapGestures { offset ->
|
|
916
|
+
val xDp = (offset.x / scale).toDouble()
|
|
917
|
+
val yDp = (offset.y / scale).toDouble()
|
|
918
|
+
if (type == WerewolfChartType.Composition) {
|
|
919
|
+
val lx = if (rtl) widthDp - xDp else xDp
|
|
920
|
+
val segments = WerewolfChartGeometry.compositionSegments(shares, widthDp, WerewolfChartGeometry.BAR_GAP)
|
|
921
|
+
val hit = segments.firstOrNull { lx >= it.x && lx <= it.x + it.width }
|
|
922
|
+
onInspect(if (hit == null || hit.row == inspected) null else hit.row)
|
|
923
|
+
} else {
|
|
924
|
+
val dl = WerewolfChartGeometry.donutLayout(widthDp, heightDp)
|
|
925
|
+
val dx = xDp - widthDp / 2.0
|
|
926
|
+
val dy = yDp - heightDp / 2.0
|
|
927
|
+
val dist = kotlin.math.hypot(dx, dy)
|
|
928
|
+
if (dist >= dl.inner && dist <= dl.radius + WerewolfChartGeometry.ACTIVE_GROWTH) {
|
|
929
|
+
var angle = kotlin.math.atan2(dy, dx) + kotlin.math.PI / 2.0
|
|
930
|
+
if (angle < 0.0) angle += kotlin.math.PI * 2.0
|
|
931
|
+
val angles = WerewolfChartGeometry.donutAngles(shares, dl.radius)
|
|
932
|
+
val hit = angles.firstOrNull { angle >= it.start && angle <= it.end }
|
|
933
|
+
onInspect(if (hit == null || hit.row == inspected) null else hit.row)
|
|
934
|
+
} else {
|
|
935
|
+
onInspect(null)
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
} else {
|
|
941
|
+
Modifier
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
Canvas(
|
|
945
|
+
Modifier
|
|
946
|
+
.fillMaxWidth()
|
|
947
|
+
.height(height)
|
|
948
|
+
.then(tapModifier)
|
|
949
|
+
.semantics { hideFromAccessibility() },
|
|
950
|
+
) {
|
|
951
|
+
fun px(value: Double): Float = (value * scale).toFloat()
|
|
952
|
+
val distinguish = coloredCount > 4 || differentiate
|
|
953
|
+
if (type == WerewolfChartType.Composition) {
|
|
954
|
+
val segments = WerewolfChartGeometry.compositionSegments(shares, widthDp, WerewolfChartGeometry.BAR_GAP)
|
|
955
|
+
segments.forEach { seg ->
|
|
956
|
+
val active = inspected == seg.row
|
|
957
|
+
val top = if (active) 0.0 else WerewolfChartGeometry.ACTIVE_GROWTH
|
|
958
|
+
val barH = if (active) 12.0 + WerewolfChartGeometry.ACTIVE_GROWTH * 2 else 12.0
|
|
959
|
+
val rx = if (rtl) widthDp - (seg.x + seg.width) else seg.x
|
|
960
|
+
val pIdx = paletteIndices[seg.row]
|
|
961
|
+
val fillColor = if (pIdx == -1) tokens.colorBackgroundGray else WerewolfChartPalette.fill(tokens, pIdx)
|
|
962
|
+
val radiusPx = minOf(tokens.radiusInner.value * scale, minOf(px(seg.width), px(barH)) / 2f)
|
|
963
|
+
val corner = androidx.compose.ui.geometry.CornerRadius(radiusPx, radiusPx)
|
|
964
|
+
drawRoundRect(color = fillColor, topLeft = Offset(px(rx), px(top)), size = androidx.compose.ui.geometry.Size(px(seg.width), px(barH)), cornerRadius = corner)
|
|
965
|
+
if (distinguish && pIdx >= 0) {
|
|
966
|
+
val box = Path().apply {
|
|
967
|
+
addRoundRect(androidx.compose.ui.geometry.RoundRect(px(rx), px(top), px(rx + seg.width), px(top + barH), corner))
|
|
968
|
+
}
|
|
969
|
+
clipPath(box) {
|
|
970
|
+
WerewolfChartGeometry.patternLines(rx, top, rx + seg.width, top + barH, pIdx).forEach { stripe ->
|
|
971
|
+
drawLine(color = tokens.colorBackgroundSurface, start = Offset(px(stripe.x1), px(stripe.y1)), end = Offset(px(stripe.x2), px(stripe.y2)), strokeWidth = px(stripe.width))
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
} else {
|
|
977
|
+
val dl = WerewolfChartGeometry.donutLayout(widthDp, heightDp)
|
|
978
|
+
val cx = widthDp / 2.0
|
|
979
|
+
val cy = heightDp / 2.0
|
|
980
|
+
val angles = WerewolfChartGeometry.donutAngles(shares, dl.radius)
|
|
981
|
+
val ordered = angles.filter { inspected != it.row } + angles.filter { inspected == it.row }
|
|
982
|
+
ordered.forEach { sector ->
|
|
983
|
+
val active = inspected == sector.row
|
|
984
|
+
val outer = if (active) dl.radius + WerewolfChartGeometry.ACTIVE_GROWTH else dl.radius
|
|
985
|
+
val cmds = WerewolfChartGeometry.donutSector(WerewolfChartDonutSector(cx, cy, dl.inner, outer, sector.drawStart, sector.drawEnd))
|
|
986
|
+
val p = Path()
|
|
987
|
+
var curX = cx
|
|
988
|
+
var curY = cy
|
|
989
|
+
cmds.forEach { cmd ->
|
|
990
|
+
val v = cmd.values
|
|
991
|
+
when (cmd.kind) {
|
|
992
|
+
'M' -> {
|
|
993
|
+
curX = v[0]
|
|
994
|
+
curY = v[1]
|
|
995
|
+
p.moveTo(px(curX), px(curY))
|
|
996
|
+
}
|
|
997
|
+
'L' -> {
|
|
998
|
+
curX = v[0]
|
|
999
|
+
curY = v[1]
|
|
1000
|
+
p.lineTo(px(curX), px(curY))
|
|
1001
|
+
}
|
|
1002
|
+
'A' -> {
|
|
1003
|
+
val rx = v[0]
|
|
1004
|
+
val sweep = v[4] > 0.5
|
|
1005
|
+
val ex = v[5]
|
|
1006
|
+
val ey = v[6]
|
|
1007
|
+
val a0 = Math.toDegrees(kotlin.math.atan2(curY - cy, curX - cx)).toFloat()
|
|
1008
|
+
var a1 = Math.toDegrees(kotlin.math.atan2(ey - cy, ex - cx)).toFloat()
|
|
1009
|
+
var sweepDeg = a1 - a0
|
|
1010
|
+
if (sweep && sweepDeg < 0f) sweepDeg += 360f
|
|
1011
|
+
if (!sweep && sweepDeg > 0f) sweepDeg -= 360f
|
|
1012
|
+
p.arcTo(
|
|
1013
|
+
rect = androidx.compose.ui.geometry.Rect(px(cx - rx), px(cy - rx), px(cx + rx), px(cy + rx)),
|
|
1014
|
+
startAngleDegrees = a0,
|
|
1015
|
+
sweepAngleDegrees = sweepDeg,
|
|
1016
|
+
forceMoveTo = false,
|
|
1017
|
+
)
|
|
1018
|
+
curX = ex
|
|
1019
|
+
curY = ey
|
|
1020
|
+
}
|
|
1021
|
+
'Z' -> p.close()
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
val pIdx = paletteIndices[sector.row]
|
|
1025
|
+
val fillColor = if (pIdx == -1) tokens.colorBackgroundGray else WerewolfChartPalette.fill(tokens, pIdx)
|
|
1026
|
+
drawPath(p, fillColor)
|
|
1027
|
+
if (distinguish && pIdx >= 0) {
|
|
1028
|
+
clipPath(p) {
|
|
1029
|
+
WerewolfChartGeometry.patternLines(cx - outer, cy - outer, cx + outer, cy + outer, pIdx).forEach { stripe ->
|
|
1030
|
+
drawLine(color = tokens.colorBackgroundSurface, start = Offset(px(stripe.x1), px(stripe.y1)), end = Offset(px(stripe.x2), px(stripe.y2)), strokeWidth = px(stripe.width))
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
val total = items.sumOf { it.value }
|
|
1036
|
+
val formattedTotal = WerewolfChartFormatting.formatNumber(total, "total", formatValue, locale)
|
|
1037
|
+
val compactTotal = WerewolfChartFormat.compact(total, locale)
|
|
1038
|
+
val shownTotal = if (formattedTotal.length * 12.0 <= dl.inner * 1.6) formattedTotal else if (compactTotal.length * 12.0 <= dl.inner * 1.6) compactTotal else ""
|
|
1039
|
+
if (shownTotal.isNotEmpty()) {
|
|
1040
|
+
val totalStyle = TextStyle(
|
|
1041
|
+
color = style.rootColor,
|
|
1042
|
+
fontSize = tokens.fontSizeLg,
|
|
1043
|
+
fontWeight = androidx.compose.ui.text.font.FontWeight.SemiBold,
|
|
1044
|
+
fontFamily = fonts.resolve(WerewolfFontRole.Sans),
|
|
1045
|
+
fontFeatureSettings = "tnum",
|
|
1046
|
+
)
|
|
1047
|
+
val subStyle = TextStyle(
|
|
1048
|
+
color = style.plotAxisColor,
|
|
1049
|
+
fontSize = style.plotAxisFontSize,
|
|
1050
|
+
fontFamily = fonts.resolve(WerewolfFontRole.Sans),
|
|
1051
|
+
)
|
|
1052
|
+
val totalResult = measurer.measure(AnnotatedString(shownTotal), totalStyle)
|
|
1053
|
+
val subResult = measurer.measure(AnnotatedString(labels.total), subStyle)
|
|
1054
|
+
drawText(totalResult, topLeft = Offset(px(cx) - totalResult.size.width / 2f, px(cy) - totalResult.size.height + 2f))
|
|
1055
|
+
drawText(subResult, topLeft = Offset(px(cx) - subResult.size.width / 2f, px(cy) + 4f))
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
@Composable
|
|
1063
|
+
fun WerewolfChartLegendItem(
|
|
1064
|
+
type: WerewolfChartType,
|
|
1065
|
+
label: String,
|
|
1066
|
+
paletteIndex: Int,
|
|
1067
|
+
style: WerewolfChartStyle,
|
|
1068
|
+
modifier: Modifier = Modifier,
|
|
1069
|
+
valueText: String? = null,
|
|
1070
|
+
shareText: String? = null,
|
|
1071
|
+
comparison: Boolean = false,
|
|
1072
|
+
distinguish: Boolean = false,
|
|
1073
|
+
hidden: Boolean = false,
|
|
1074
|
+
isOther: Boolean = false,
|
|
1075
|
+
onToggle: (() -> Unit)? = null,
|
|
1076
|
+
) {
|
|
1077
|
+
val t = Werewolf.tokens
|
|
1078
|
+
val fonts = LocalWerewolfFonts.current
|
|
1079
|
+
val largeText = rememberLargeText()
|
|
1080
|
+
val labelColor = if (hidden) t.colorTextTertiary else style.legendLabelColor
|
|
1081
|
+
val interactionSource = remember { androidx.compose.foundation.interaction.MutableInteractionSource() }
|
|
1082
|
+
val pressed by interactionSource.collectIsPressedAsState()
|
|
1083
|
+
val rowModifier = if (onToggle != null) {
|
|
1084
|
+
modifier
|
|
1085
|
+
.then(if (largeText) Modifier.fillMaxWidth() else Modifier)
|
|
1086
|
+
.heightIn(min = style.legendItemMinHeight)
|
|
1087
|
+
.werewolfHitArea()
|
|
1088
|
+
.werewolfOverlay(t.colorOverlayPressed, androidx.compose.foundation.shape.RoundedCornerShape(style.legendItemRadius), pressed)
|
|
1089
|
+
.werewolfPressScale(pressed)
|
|
1090
|
+
.toggleable(
|
|
1091
|
+
value = !hidden,
|
|
1092
|
+
interactionSource = interactionSource,
|
|
1093
|
+
indication = null,
|
|
1094
|
+
role = androidx.compose.ui.semantics.Role.Switch,
|
|
1095
|
+
onValueChange = { onToggle() },
|
|
1096
|
+
)
|
|
1097
|
+
.semantics(mergeDescendants = true) {
|
|
1098
|
+
contentDescription = label
|
|
1099
|
+
}
|
|
1100
|
+
} else {
|
|
1101
|
+
modifier.fillMaxWidth().then(if (largeText) Modifier.heightIn(min = style.legendItemMinHeight) else Modifier)
|
|
1102
|
+
}
|
|
1103
|
+
androidx.compose.foundation.layout.Row(
|
|
1104
|
+
modifier = rowModifier,
|
|
1105
|
+
verticalAlignment = androidx.compose.ui.Alignment.CenterVertically,
|
|
1106
|
+
horizontalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(style.legendItemGap),
|
|
1107
|
+
) {
|
|
1108
|
+
WerewolfChartKey(type = type, paletteIndex = paletteIndex, comparison = comparison, distinguish = distinguish, hidden = hidden, isOther = isOther)
|
|
1109
|
+
androidx.compose.foundation.text.BasicText(
|
|
1110
|
+
text = label,
|
|
1111
|
+
style = TextStyle(color = labelColor, fontSize = style.legendLabelFontSize, fontFamily = fonts.resolve(WerewolfFontRole.Sans)),
|
|
1112
|
+
maxLines = if (largeText) Int.MAX_VALUE else 1,
|
|
1113
|
+
overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis,
|
|
1114
|
+
modifier = if (valueText != null) Modifier.weight(1f) else Modifier,
|
|
1115
|
+
)
|
|
1116
|
+
if (valueText != null) {
|
|
1117
|
+
androidx.compose.foundation.text.BasicText(
|
|
1118
|
+
text = valueText,
|
|
1119
|
+
style = TextStyle(color = style.legendValueColor, fontSize = style.legendValueFontSize, fontFamily = fonts.resolve(WerewolfFontRole.Sans), fontFeatureSettings = "tnum"),
|
|
1120
|
+
)
|
|
1121
|
+
}
|
|
1122
|
+
if (shareText != null) {
|
|
1123
|
+
androidx.compose.foundation.text.BasicText(
|
|
1124
|
+
text = shareText,
|
|
1125
|
+
style = TextStyle(color = style.legendValueColor, fontSize = style.legendValueFontSize, fontFamily = fonts.resolve(WerewolfFontRole.Sans), fontFeatureSettings = "tnum"),
|
|
1126
|
+
)
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
}
|