frappe-ui 1.0.2 → 1.1.0

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.
Files changed (96) hide show
  1. package/experimental/Calendar/calendarUtils.ts +33 -21
  2. package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
  3. package/experimental/MultiEmailInput/types.ts +12 -3
  4. package/experimental/Stepper/StepIndicator.vue +129 -0
  5. package/experimental/Stepper/Stepper.api.md +117 -0
  6. package/experimental/Stepper/Stepper.md +95 -0
  7. package/experimental/Stepper/Stepper.vue +82 -0
  8. package/experimental/Stepper/StepperHorizontal.vue +93 -0
  9. package/experimental/Stepper/StepperVertical.vue +291 -0
  10. package/experimental/Stepper/index.ts +11 -0
  11. package/experimental/Stepper/resolve.ts +87 -0
  12. package/experimental/Stepper/types.ts +89 -0
  13. package/experimental.ts +2 -0
  14. package/package.json +1 -1
  15. package/src/charts/CalendarHeatmapChart.vue +353 -0
  16. package/src/charts/DonutChart.vue +34 -58
  17. package/src/charts/HeatmapChart.vue +26 -64
  18. package/src/charts/NumberCard.vue +62 -7
  19. package/src/charts/PercentageBarChart.vue +243 -0
  20. package/src/charts/SankeyChart.vue +14 -11
  21. package/src/charts/ScatterChart.vue +24 -20
  22. package/src/charts/axisChartCommon.ts +182 -18
  23. package/src/charts/axisFormat.ts +8 -3
  24. package/src/charts/calendarHeatmapOptions.ts +604 -0
  25. package/src/charts/colorMath.ts +26 -0
  26. package/src/charts/components/ChartContainer.vue +63 -53
  27. package/src/charts/components/ChartLegend.vue +216 -24
  28. package/src/charts/components/ChartScale.vue +85 -0
  29. package/src/charts/core/dataLabelFit.ts +45 -6
  30. package/src/charts/core/useAxisChart.ts +36 -19
  31. package/src/charts/core/useChart.ts +6 -1
  32. package/src/charts/core/usePlotKeyboard.ts +13 -2
  33. package/src/charts/docs/BarChart.md +11 -3
  34. package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
  35. package/src/charts/docs/CalendarHeatmapChart.md +33 -0
  36. package/src/charts/docs/ChartContainer.api.md +6 -0
  37. package/src/charts/docs/ChartLegend.api.md +1 -1
  38. package/src/charts/docs/ChartLegend.md +25 -3
  39. package/src/charts/docs/DonutChart.api.md +12 -0
  40. package/src/charts/docs/DonutChart.md +17 -0
  41. package/src/charts/docs/HeatmapChart.api.md +1 -1
  42. package/src/charts/docs/LineChart.md +5 -3
  43. package/src/charts/docs/NumberCard.api.md +6 -0
  44. package/src/charts/docs/NumberCard.md +6 -0
  45. package/src/charts/docs/Overview.md +13 -6
  46. package/src/charts/docs/PercentageBarChart.api.md +148 -0
  47. package/src/charts/docs/PercentageBarChart.md +42 -0
  48. package/src/charts/docs/ScatterChart.md +2 -3
  49. package/src/charts/donutChartOptions.ts +22 -130
  50. package/src/charts/format.ts +4 -1
  51. package/src/charts/heatmapOptions.ts +17 -12
  52. package/src/charts/hiddenSeries.ts +8 -3
  53. package/src/charts/index.ts +12 -0
  54. package/src/charts/legendOverflow.ts +40 -0
  55. package/src/charts/numberCardFormat.ts +19 -13
  56. package/src/charts/referenceLines.ts +2 -1
  57. package/src/charts/scatterOptions.ts +16 -3
  58. package/src/charts/seriesData.ts +8 -3
  59. package/src/charts/slices.ts +228 -0
  60. package/src/charts/tokens.ts +99 -0
  61. package/src/charts/types.ts +318 -9
  62. package/src/components/Avatar/Avatar.md +27 -2
  63. package/src/components/Avatar/Avatar.vue +29 -12
  64. package/src/components/Combobox/Combobox.vue +9 -2
  65. package/src/components/Combobox/ComboboxResults.vue +6 -1
  66. package/src/components/Combobox/utils.ts +2 -1
  67. package/src/components/Dialog/Dialog.md +3 -1
  68. package/src/components/Duration/Duration.api.md +16 -3
  69. package/src/components/Duration/Duration.md +21 -0
  70. package/src/components/Duration/Duration.vue +29 -8
  71. package/src/components/Duration/duration.ts +105 -44
  72. package/src/components/Duration/index.ts +1 -0
  73. package/src/components/Duration/types.ts +30 -1
  74. package/src/components/Menu/utils.ts +3 -2
  75. package/src/components/MultiSelect/MultiSelect.api.md +5 -0
  76. package/src/components/MultiSelect/MultiSelect.vue +34 -0
  77. package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
  78. package/src/components/MultiSelect/types.ts +6 -0
  79. package/src/components/Select/Select.vue +4 -4
  80. package/src/components/Select/utils.ts +1 -0
  81. package/src/components/Switch/Switch.vue +3 -3
  82. package/src/components/shared/selection/utils.ts +8 -1
  83. package/src/data-fetching/useCall/useCall.ts +25 -26
  84. package/src/data-fetching/useFrappeFetch.ts +48 -2
  85. package/src/data-fetching/useList/useList.ts +11 -3
  86. package/src/data-fetching/utils.ts +15 -1
  87. package/src/molecules/editor/components/MediaNodeView.vue +10 -1
  88. package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
  89. package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
  90. package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
  91. package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +3 -1
  92. package/src/molecules/editor/useEditor.ts +12 -6
  93. package/src/utils/dialog.ts +37 -14
  94. package/tailwind/content.js +1 -0
  95. package/vite/README.md +5 -3
  96. package/vite/codeLanguages.js +41 -17
@@ -57,77 +57,87 @@
57
57
  </span>
58
58
  </div>
59
59
 
60
- <!-- The pad stands in for the space a legend would have taken, so a
61
- single-series chart sits in the card the way a multi-series one does. -->
60
+ <!-- A centred plot keeps its legend beside it, and the two split the
61
+ height the card has to spare above and below them. -->
62
62
  <div
63
- data-slot="chart-plot"
64
- class="relative min-h-0 flex-1"
65
- :class="{ 'pb-3': !$slots.legend && !bottomAxisTitle }"
63
+ class="flex min-h-0 flex-1 flex-col gap-1.5"
64
+ :class="{ 'justify-center': centered }"
66
65
  >
67
- <!-- The plot stays mounted through every state: unmounting it would
66
+ <!-- The pad stands in for the space a legend would have taken, so a
67
+ single-series chart sits in the card the way a multi-series one does. -->
68
+ <div
69
+ data-slot="chart-plot"
70
+ class="relative min-h-0"
71
+ :class="{
72
+ 'flex-1': !centered,
73
+ 'pb-3': !$slots.legend && !bottomAxisTitle,
74
+ }"
75
+ >
76
+ <!-- The plot stays mounted through every state: unmounting it would
68
77
  dispose the echarts instance and re-init it on the way back. -->
69
- <div class="h-full w-full" :class="{ invisible: state !== 'ready' }">
70
- <slot />
71
- </div>
78
+ <div class="h-full w-full" :class="{ invisible: state !== 'ready' }">
79
+ <slot />
80
+ </div>
72
81
 
73
- <!-- The loading state gets the whole plot box rather than a row in the
82
+ <!-- The loading state gets the whole plot box rather than a row in the
74
83
  middle of it, because what it draws is the shape of the plot. A
75
84
  caller replacing it is drawing a placeholder, not a caption. -->
76
- <div
77
- v-if="state === 'loading'"
78
- data-slot="chart-loading"
79
- class="absolute inset-0"
80
- >
81
- <!-- Announced by the container, so an app's own placeholder does not
85
+ <div
86
+ v-if="state === 'loading'"
87
+ data-slot="chart-loading"
88
+ class="absolute inset-0"
89
+ >
90
+ <!-- Announced by the container, so an app's own placeholder does not
82
91
  have to carry the announcement with it. -->
83
- <span class="sr-only" role="status">Loading chart</span>
84
- <slot name="loading">
85
- <!-- A block the size of the plot, not a spinner in the middle of it.
92
+ <span class="sr-only" role="status">Loading chart</span>
93
+ <slot name="loading">
94
+ <!-- A block the size of the plot, not a spinner in the middle of it.
86
95
  A dashboard fills in a card at a time, and a placeholder holding
87
96
  the grid's shape reads as one card arriving rather than as eight
88
97
  spinners turning out of step. NumberCard, which has no plot,
89
98
  skeletons its reading the same way. -->
90
- <Skeleton class="h-full w-full rounded-4" />
91
- </slot>
92
- </div>
93
-
94
- <div
95
- v-else-if="state !== 'ready'"
96
- class="absolute inset-0 flex flex-col items-center justify-center gap-2 text-center"
97
- >
98
- <template v-if="state === 'error'">
99
- <slot name="error" :error="error">
100
- <span class="text-sm-medium text-ink-red-7">
101
- Could not render this chart
102
- </span>
103
- <span v-if="error" class="max-w-sm text-p-sm text-ink-gray-5">
104
- {{ error }}
105
- </span>
99
+ <Skeleton class="h-full w-full rounded-4" />
106
100
  </slot>
107
- </template>
101
+ </div>
108
102
 
109
- <template v-else>
110
- <slot name="empty">
111
- <span class="text-p-sm text-ink-gray-5">No data to show</span>
112
- </slot>
113
- </template>
103
+ <div
104
+ v-else-if="state !== 'ready'"
105
+ class="absolute inset-0 flex flex-col items-center justify-center gap-2 text-center"
106
+ >
107
+ <template v-if="state === 'error'">
108
+ <slot name="error" :error="error">
109
+ <span class="text-sm-medium text-ink-red-7">
110
+ Could not render this chart
111
+ </span>
112
+ <span v-if="error" class="max-w-sm text-p-sm text-ink-gray-5">
113
+ {{ error }}
114
+ </span>
115
+ </slot>
116
+ </template>
117
+
118
+ <template v-else>
119
+ <slot name="empty">
120
+ <span class="text-p-sm text-ink-gray-5">No data to show</span>
121
+ </slot>
122
+ </template>
123
+ </div>
114
124
  </div>
115
- </div>
116
125
 
117
- <!-- The same titles under the plot, for a value axis that runs along the
126
+ <!-- The same titles under the plot, for a value axis that runs along the
118
127
  bottom. Pinned to the far end: the near end of a row chart is the
119
128
  category-label column, where a title would read as a heading for it. -->
120
- <div
121
- v-if="bottomAxisTitle"
122
- class="-mt-2 flex items-baseline justify-end gap-3 text-p-xs text-ink-gray-5"
123
- >
124
- <span class="truncate">{{ yAxisTitle }}</span>
125
- <span v-if="y2AxisTitle" class="truncate">
126
- {{ y2AxisTitle }}
127
- </span>
128
- </div>
129
+ <div
130
+ v-if="bottomAxisTitle"
131
+ class="-mt-2 flex items-baseline justify-end gap-3 text-p-xs text-ink-gray-5"
132
+ >
133
+ <span class="truncate">{{ yAxisTitle }}</span>
134
+ <span v-if="y2AxisTitle" class="truncate">
135
+ {{ y2AxisTitle }}
136
+ </span>
137
+ </div>
129
138
 
130
- <slot name="legend" />
139
+ <slot name="legend" />
140
+ </div>
131
141
  </div>
132
142
  </template>
133
143
 
@@ -1,32 +1,18 @@
1
1
  <template>
2
- <!-- No height cap: a legend that scrolls hides series names behind a gesture
3
- nobody makes. It takes the rows it needs and the plot gives up the space,
4
- which the chart's resize observer picks up. -->
5
- <div
6
- data-slot="chart-legend"
7
- class="flex shrink-0 flex-wrap items-center justify-center gap-x-1 gap-y-0.5"
8
- >
9
- <Button
10
- v-for="item in items"
11
- :key="item.name"
12
- variant="ghost"
13
- size="xs"
14
- :aria-pressed="!item.hidden"
15
- :label="toggleLabel(item)"
16
- @click="emit('change', item.name)"
17
- @mouseenter="emit('highlight', item.name)"
18
- @mouseleave="emit('highlight', null)"
19
- @focus="emit('highlight', item.name)"
20
- @blur="emit('highlight', null)"
21
- >
2
+ <!-- One row at most: past a handful of series a wrapping legend takes the
3
+ plot's height, and the plot is what the chart is for. The largest series
4
+ stay on the row and the rest wait behind "+N more", where every name is
5
+ one press away rather than behind a scroll nobody makes. -->
6
+ <div data-slot="chart-legend" class="relative min-w-0 shrink-0">
7
+ <DefineEntry v-slot="{ item }">
22
8
  <span class="flex items-center gap-1.5">
23
9
  <span
24
10
  class="size-2 shrink-0 rounded-full"
25
11
  :class="{ 'opacity-30': item.hidden }"
26
12
  :style="{ backgroundColor: item.color }"
27
13
  />
28
- <!-- A step back from the axis titles: the legend names the marks, it is
29
- not a label of the chart's own. -->
14
+ <!-- A step back from the axis titles: the legend names the marks, it
15
+ is not a label of the chart's own. -->
30
16
  <span :class="item.hidden ? 'text-ink-gray-4' : 'text-ink-gray-6'">
31
17
  {{ item.label }}
32
18
  </span>
@@ -34,22 +20,228 @@
34
20
  {{ item.hint }}
35
21
  </span>
36
22
  </span>
37
- </Button>
23
+ </DefineEntry>
24
+
25
+ <div ref="rowEl" class="flex min-w-0 items-center justify-center gap-x-1">
26
+ <Button
27
+ v-for="item in shown"
28
+ :key="item.name"
29
+ variant="ghost"
30
+ size="xs"
31
+ :aria-pressed="!item.hidden"
32
+ :label="toggleLabel(item)"
33
+ @click="emit('change', item.name)"
34
+ @mouseenter="emit('highlight', item.name)"
35
+ @mouseleave="emit('highlight', null)"
36
+ @focus="emit('highlight', item.name)"
37
+ @blur="emit('highlight', null)"
38
+ >
39
+ <ReuseEntry :item="item" />
40
+ </Button>
41
+
42
+ <!-- The model is the visible series, so a checked row is a drawn one.
43
+ Ranked rather than matched while searching: the list reads largest
44
+ first whatever is typed. -->
45
+ <MultiSelect
46
+ v-if="shown.length < items.length"
47
+ v-model:open="open"
48
+ v-model:query="query"
49
+ :model-value="visible"
50
+ :options="options"
51
+ :filterable="false"
52
+ :hide-search="items.length <= SEARCH_AFTER"
53
+ placeholder="Search series"
54
+ empty-text="No series match"
55
+ side="top"
56
+ align="end"
57
+ :portal-to="portalEl"
58
+ @update:model-value="showOnly"
59
+ >
60
+ <template #trigger="{ open: isOpen }">
61
+ <Button
62
+ variant="ghost"
63
+ size="xs"
64
+ aria-haspopup="listbox"
65
+ :aria-expanded="isOpen"
66
+ :label="`Show ${items.length - shown.length} more series`"
67
+ >
68
+ <span class="tabular-nums text-ink-gray-6">
69
+ {{ moreText(items.length - shown.length) }}
70
+ </span>
71
+ </Button>
72
+ </template>
73
+ <template #item-prefix="{ item }">
74
+ <span
75
+ class="size-2 shrink-0 rounded-full"
76
+ :class="{ 'opacity-30': item.hidden }"
77
+ :style="{ backgroundColor: item.color }"
78
+ />
79
+ </template>
80
+ <template #item-suffix="{ item }">
81
+ <span v-if="item.formattedValue" class="tabular-nums text-ink-gray-5">
82
+ {{ item.formattedValue }}
83
+ </span>
84
+ </template>
85
+ <!-- Clear All would hide every series, which the chart refuses, and
86
+ Select All is one more control than a legend has ever had. -->
87
+ <template #footer />
88
+ </MultiSelect>
89
+ </div>
90
+
91
+ <!-- The list is portalled out of the chart, so it would lay out in the
92
+ page's direction. This holder carries the row's instead. -->
93
+ <Teleport v-if="shown.length < items.length" :to="portalTarget ?? 'body'">
94
+ <div ref="portalEl" :dir="dir" />
95
+ </Teleport>
96
+
97
+ <!-- Every entry and the trigger at their natural widths, so the fit is
98
+ measured rather than guessed. After the row, so a search by text finds
99
+ the real entry first. Clipped to nothing: left at full width it
100
+ overflows the card, and the browser scrolls the card sideways to reach
101
+ whatever takes focus. -->
102
+ <div
103
+ aria-hidden="true"
104
+ inert
105
+ class="invisible absolute h-0 w-0 overflow-hidden"
106
+ >
107
+ <div ref="rulerEl" class="flex w-max gap-x-1">
108
+ <Button
109
+ v-for="item in ranked"
110
+ :key="item.name"
111
+ variant="ghost"
112
+ size="xs"
113
+ >
114
+ <ReuseEntry :item="item" />
115
+ </Button>
116
+ <!-- One trigger per digit count the hidden count can have: the
117
+ digits are tabular, so the count's length is all that moves the
118
+ width. -->
119
+ <Button
120
+ v-for="digits in String(items.length).length"
121
+ :key="`more-${digits}`"
122
+ variant="ghost"
123
+ size="xs"
124
+ >
125
+ <span class="tabular-nums text-ink-gray-6">
126
+ {{ moreText(Number('8'.repeat(digits))) }}
127
+ </span>
128
+ </Button>
129
+ </div>
130
+ </div>
38
131
  </div>
39
132
  </template>
40
133
 
41
134
  <script setup lang="ts">
135
+ import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
136
+ import { createReusableTemplate } from '@vueuse/core'
42
137
  import Button from '#components/Button/Button.vue'
138
+ import MultiSelect from '#components/MultiSelect/MultiSelect.vue'
139
+ import { usePortalTarget } from '#composables/usePortalTarget'
140
+ import { legendFit, rankLegendItems } from '../legendOverflow'
43
141
  import type {
44
142
  ChartLegendEmits,
45
143
  ChartLegendItem,
46
144
  ChartLegendProps,
47
145
  } from '../types'
48
146
 
49
- defineProps<ChartLegendProps>()
147
+ // Past this many entries, finding one by reading the list is slower than typing.
148
+ const SEARCH_AFTER = 15
149
+
150
+ const props = defineProps<ChartLegendProps>()
50
151
 
51
152
  const emit = defineEmits<ChartLegendEmits>()
52
153
 
154
+ const [DefineEntry, ReuseEntry] = createReusableTemplate<{
155
+ item: ChartLegendItem
156
+ }>()
157
+
158
+ const ranked = computed(() => rankLegendItems(props.items))
159
+
160
+ const rowEl = ref<HTMLElement>()
161
+ const rulerEl = ref<HTMLElement>()
162
+ const fit = ref(props.items.length)
163
+
164
+ // A row that fits keeps the order the series are drawn in. Only an overflowing
165
+ // one trades that order for size.
166
+ const shown = computed(() =>
167
+ fit.value < props.items.length
168
+ ? ranked.value.slice(0, fit.value)
169
+ : props.items,
170
+ )
171
+
172
+ function measure() {
173
+ const row = rowEl.value
174
+ const ruler = rulerEl.value
175
+ if (!row || !ruler) return
176
+ const widths = Array.from(
177
+ ruler.children,
178
+ (child) => child.getBoundingClientRect().width,
179
+ )
180
+ // The triggers follow the entries, one per digit count.
181
+ const triggers = widths.splice(ranked.value.length)
182
+ const gap = parseFloat(getComputedStyle(ruler).columnGap) || 0
183
+ fit.value = legendFit(
184
+ widths,
185
+ row.getBoundingClientRect().width,
186
+ (hidden) => triggers[String(hidden).length - 1] ?? 0,
187
+ gap,
188
+ )
189
+ // The panel goes with its trigger. Left open, it would come back on its own
190
+ // the next time the row overflows.
191
+ if (fit.value >= props.items.length) open.value = false
192
+ }
193
+
194
+ // The row follows the card's width and the ruler follows the labels and the
195
+ // font, so either one changing can change the fit.
196
+ let observer: ResizeObserver | undefined
197
+ onMounted(() => {
198
+ observer = new ResizeObserver(() => measure())
199
+ if (rowEl.value) observer.observe(rowEl.value)
200
+ if (rulerEl.value) observer.observe(rulerEl.value)
201
+ measure()
202
+ })
203
+ onBeforeUnmount(() => observer?.disconnect())
204
+ // A new ranking can leave the ruler's width alone and still reorder the widths
205
+ // within it.
206
+ watch(ranked, measure, { flush: 'post' })
207
+
208
+ const open = ref(false)
209
+ const query = ref('')
210
+ const dir = ref<string>()
211
+ const portalTarget = usePortalTarget()
212
+ const portalEl = ref<HTMLElement>()
213
+
214
+ const visible = computed(() =>
215
+ props.items.filter((item) => !item.hidden).map((item) => item.name),
216
+ )
217
+
218
+ const options = computed(() => {
219
+ const text = query.value.trim().toLowerCase()
220
+ return ranked.value
221
+ .filter((item) => !text || item.label.toLowerCase().includes(text))
222
+ .map((item) => ({ ...item, value: item.name }))
223
+ })
224
+
225
+ // Each series whose checkbox moved is one `change`, the same press an entry on
226
+ // the row makes. The chart may refuse it, as it refuses hiding the last visible
227
+ // series, and then the model it hands back leaves the row checked.
228
+ function showOnly(names: Array<string | number>) {
229
+ const next = new Set(names)
230
+ for (const item of props.items) {
231
+ if (next.has(item.name) === item.hidden) emit('change', item.name)
232
+ }
233
+ }
234
+
235
+ // The query is bound, so the list never clears it on its own.
236
+ watch(open, (value) => {
237
+ query.value = ''
238
+ if (value && rowEl.value) dir.value = getComputedStyle(rowEl.value).direction
239
+ })
240
+
241
+ function moreText(count: number) {
242
+ return `+${count} more`
243
+ }
244
+
53
245
  // The visible text is the series label; the accessible name has to say what the
54
246
  // press does, since the toggle state alone doesn't read as an action.
55
247
  function toggleLabel(item: ChartLegendItem) {
@@ -0,0 +1,85 @@
1
+ <template>
2
+ <!-- `text-2xs` is `AXIS_LABEL_FONT_SIZE` as a class: 11px and tight, because
3
+ these read as axis labels rather than as captions. -->
4
+ <div
5
+ v-if="vertical"
6
+ data-slot="chart-scale"
7
+ class="flex flex-col items-center justify-end"
8
+ >
9
+ <span class="text-2xs tabular-nums text-ink-gray-5">{{ high }}</span>
10
+ <!-- Grows into the room it has and stops: a ramp as tall as the card reads
11
+ as a second column of data rather than as a key to one. -->
12
+ <span
13
+ class="mt-2 min-h-8 max-h-20 w-2 flex-1 rounded-1"
14
+ :style="{ backgroundImage: gradient }"
15
+ />
16
+ <!-- Holds the whole label row, so `min` starts where the labels beside it
17
+ start. Its own height until that row is known, or the first frame
18
+ would draw a collapsed box. -->
19
+ <span
20
+ data-slot="chart-scale-min"
21
+ class="text-2xs tabular-nums text-ink-gray-5"
22
+ :style="{
23
+ marginTop: `${AXIS_LABEL_MARGIN}px`,
24
+ height: labelRowHeight
25
+ ? `${Math.max(labelRowHeight - AXIS_LABEL_MARGIN, 0)}px`
26
+ : undefined,
27
+ }"
28
+ >
29
+ {{ low }}
30
+ </span>
31
+ </div>
32
+ <div
33
+ v-else
34
+ data-slot="chart-scale"
35
+ class="flex items-center gap-2 whitespace-nowrap text-2xs tabular-nums text-ink-gray-5"
36
+ >
37
+ <span>{{ low }}</span>
38
+ <span class="h-2 w-20 rounded-1" :style="{ backgroundImage: gradient }" />
39
+ <span>{{ high }}</span>
40
+ </div>
41
+ </template>
42
+
43
+ <script setup lang="ts">
44
+ import { computed } from 'vue'
45
+ import { AXIS_LABEL_MARGIN } from '../axisChartCommon'
46
+ import { formatValue } from '../format'
47
+ import { sampleRamp } from '../heatmapOptions'
48
+ import type { ChartDir, ChartValueFormatter } from '../types'
49
+
50
+ const props = defineProps<{
51
+ min: number
52
+ max: number
53
+ /** The ramp the scale runs along, low end first. */
54
+ stops: string[]
55
+ /** Prints the ends. They sit in a narrow strip, so they default to short. */
56
+ format?: ChartValueFormatter
57
+ /** Some value lies under `min`, so its end reads `≤`. */
58
+ belowMin?: boolean
59
+ /** Some value lies over `max`, so its end reads `≥`. */
60
+ aboveMax?: boolean
61
+ /** Upright beside the plot, low end at the foot. Else lying under it. */
62
+ vertical?: boolean
63
+ /** Upright, the height of the label row `min` lines up with. */
64
+ labelRowHeight?: number
65
+ dir?: ChartDir
66
+ }>()
67
+
68
+ function print(value: number) {
69
+ return props.format ? props.format(value) : formatValue(value, 1, true)
70
+ }
71
+
72
+ const low = computed(() => `${props.belowMin ? '≤ ' : ''}${print(props.min)}`)
73
+ const high = computed(() => `${props.aboveMax ? '≥ ' : ''}${print(props.max)}`)
74
+
75
+ // Upright, the labels sit above and below the ramp rather than either side of
76
+ // it, so it reads the same in both directions and RTL has nothing to flip.
77
+ const gradient = computed(() => {
78
+ const towards = props.vertical
79
+ ? 'top'
80
+ : props.dir === 'rtl'
81
+ ? 'left'
82
+ : 'right'
83
+ return `linear-gradient(to ${towards}, ${sampleRamp(props.stops).join(', ')})`
84
+ })
85
+ </script>
@@ -7,19 +7,21 @@ export type DataLabelSide = 'top' | 'right' | 'left'
7
7
  const DEFAULT_LABEL_DISTANCE = 5
8
8
 
9
9
  /**
10
- * The axis end one tick past the current one, or `null` when echarts has drawn
11
- * fewer than two ticks to read a step from. Read off the ticks rather than the
12
- * scale's extent, because the ticks are what the reader sees and what a label
13
- * has to clear.
10
+ * The axis end one tick past the current one and the step that reaches it, or
11
+ * `null` when echarts has drawn fewer than two ticks to read a step from. Read
12
+ * off the ticks rather than the scale's extent, because the ticks are what the
13
+ * reader sees and what a label has to clear. The step is pinned too: left to
14
+ * echarts, the longer span can get a coarser step the new end is not on.
14
15
  */
15
- export function nextAxisMax(chart: ECharts, axis: 'xAxis' | 'yAxis') {
16
+ export function nextAxisTick(chart: ECharts, axis: 'xAxis' | 'yAxis') {
16
17
  const ticks = (chart as any)
17
18
  .getModel()
18
19
  .getComponent(axis, 0)
19
20
  ?.axis?.scale?.getTicks?.()
20
21
  if (!ticks || ticks.length < 2) return null
21
22
  const last = ticks[ticks.length - 1].value
22
- return last + (last - ticks[ticks.length - 2].value)
23
+ const interval = last - ticks[ticks.length - 2].value
24
+ return { max: last + interval, interval }
23
25
  }
24
26
 
25
27
  /**
@@ -76,6 +78,43 @@ export function dataLabelOverflow(
76
78
  return overflow
77
79
  }
78
80
 
81
+ /**
82
+ * How far the furthest reference-line label reaches past `bound`, read the way
83
+ * `dataLabelOverflow` reads a data label. A rule at the end of the scale, such
84
+ * as a goal that stretched the axis, puts a label placed beyond it outside the
85
+ * plot. echarts only positions that label when it paints, so it is placed here
86
+ * first to be measured.
87
+ */
88
+ export function referenceLabelOverflow(
89
+ chart: ECharts,
90
+ side: DataLabelSide,
91
+ bound: number,
92
+ ) {
93
+ const width = chart.getWidth()
94
+ let overflow: number | null = null
95
+
96
+ ;(chart as any).getModel().eachComponent('markLine', (markLine: any) => {
97
+ const view = (chart as any).getViewOfComponentModel(markLine)
98
+ view?.group.traverse((rule: any) => {
99
+ const label = rule.getTextContent?.()
100
+ if (!label || label.ignore || !rule.beforeUpdate) return
101
+ rule.beforeUpdate()
102
+ const box = label.getBoundingRect().clone()
103
+ box.applyTransform(label.getLocalTransform())
104
+
105
+ const reach =
106
+ side === 'top'
107
+ ? bound - box.y
108
+ : side === 'right'
109
+ ? box.x + box.width - (width - bound)
110
+ : bound - box.x
111
+ overflow = Math.max(overflow ?? -Infinity, reach)
112
+ })
113
+ })
114
+
115
+ return overflow
116
+ }
117
+
79
118
  /** The pixel a mark ends at on the side its label sits past. */
80
119
  function markTip(layout: any, side: DataLabelSide): number | null {
81
120
  if (!layout) return null