@@ -10,7 +10,7 @@ const props = withDefaults(defineProps<{
10
10
data: KV []
11
11
colors? : ChartColor []
12
12
margins? : Partial <{ top: number ; right: number ; bottom: number ; left: number }>
13
- mode ? : ' horizontal' | ' vertical'
13
+ type ? : ' horizontal' | ' vertical'
14
14
debug? : boolean
15
15
ticks? : number
16
16
animate? : boolean
@@ -20,14 +20,16 @@ const props = withDefaults(defineProps<{
20
20
yLabel? : string
21
21
xLabelOffset? : number
22
22
yLabelOffset? : number
23
+ maxBandwidth? : number
23
24
}>(), {
24
25
colors : () => [' blue' ],
25
- mode : ' vertical' ,
26
+ type : ' vertical' ,
26
27
debug: false ,
27
28
ticks: 5 ,
28
29
animate: true ,
29
30
tooltip: true ,
30
- tooltipFormat : (d : KV ) => ` ${d .key } – ${d .value } `
31
+ tooltipFormat : (d : KV ) => ` ${d .key } – ${d .value } ` ,
32
+ maxBandwidth: 100
31
33
})
32
34
33
35
const chartRef = useTemplateRef (' chart' )
@@ -57,7 +59,7 @@ function renderChart({
57
59
.remove ()
58
60
59
61
// Set dimensions and margins
60
- const vertical = props .mode === ' vertical'
62
+ const vertical = props .type === ' vertical'
61
63
62
64
const margin = {
63
65
top: props .margins ?.top ?? 30 ,
@@ -98,6 +100,8 @@ function renderChart({
98
100
// Compute a constant offset to center the colored bar inside its full band.
99
101
const groupOffset = (paddedScale .step () - paddedScale .bandwidth ()) / 2
100
102
const heightPadding = 24
103
+ const bandwidth = Math .min (paddedScale .bandwidth (), props .maxBandwidth )
104
+ const innerOffset = (paddedScale .bandwidth () - bandwidth ) / 2
101
105
102
106
// For the axes, use the paddedScale so ticks remain centered on the bars.
103
107
svg
@@ -106,7 +110,7 @@ function renderChart({
106
110
.call (vertical ? d3 .axisBottom (paddedScale ) : d3 .axisBottom (y ).ticks (props .ticks ))
107
111
.selectAll (' text' )
108
112
.attr (' fill' , c .text2 )
109
- .style (' font-size' , ' 14px ' )
113
+ .style (' font-size' , ' var(--chart-tick-font-size) ' )
110
114
.style (' text-anchor' , ' middle' )
111
115
112
116
// Remove X axis line
@@ -120,7 +124,7 @@ function renderChart({
120
124
.call (vertical ? d3 .axisLeft (y ).ticks (props .ticks ) : d3 .axisLeft (paddedScale ))
121
125
.selectAll (' text' )
122
126
.attr (' fill' , c .text2 )
123
- .style (' font-size' , ' 14px ' )
127
+ .style (' font-size' , ' var(--chart-tick-font-size) ' )
124
128
125
129
// Remove Y axis line
126
130
svg
@@ -157,7 +161,7 @@ function renderChart({
157
161
.attr (' x' , width / 2 )
158
162
.attr (' y' , height + xLabelOffset )
159
163
.attr (' fill' , c .text2 )
160
- .style (' font-size' , ' 14px ' )
164
+ .style (' font-size' , ' var(--chart-label-font-size) ' )
161
165
.style (' text-anchor' , ' middle' )
162
166
.html (props .xLabel )
163
167
}
@@ -168,7 +172,7 @@ function renderChart({
168
172
.attr (' y' , - yLabelOffset )
169
173
.attr (' transform' , ' rotate(-90)' )
170
174
.attr (' fill' , c .text2 )
171
- .style (' font-size' , ' 14px ' )
175
+ .style (' font-size' , ' var(--chart-label-font-size) ' )
172
176
.style (' text-anchor' , ' middle' )
173
177
.html (props .yLabel )
174
178
}
@@ -207,9 +211,9 @@ function renderChart({
207
211
.attr (' width' , paddedScale .step ())
208
212
.attr (' height' , (d ) => height - Math .max (0 , y (d .value ) - heightPadding ))
209
213
bars
210
- .attr (' x' , groupOffset )
214
+ .attr (' x' , groupOffset + innerOffset )
211
215
.attr (' y' , (d ) => y (d .value ))
212
- .attr (' width' , paddedScale . bandwidth () )
216
+ .attr (' width' , bandwidth )
213
217
.attr (' height' , (d ) => height - y (d .value ))
214
218
} else {
215
219
outerBars
@@ -219,9 +223,9 @@ function renderChart({
219
223
.attr (' height' , paddedScale .step ())
220
224
bars
221
225
.attr (' x' , 0 )
222
- .attr (' y' , groupOffset )
226
+ .attr (' y' , groupOffset + innerOffset )
223
227
.attr (' width' , (d ) => y (d .value ))
224
- .attr (' height' , paddedScale . bandwidth () )
228
+ .attr (' height' , bandwidth )
225
229
}
226
230
} else {
227
231
// Animate the bars
@@ -237,9 +241,9 @@ function renderChart({
237
241
.attr (' y' , (d ) => Math .max (0 , y (d .value ) - heightPadding ))
238
242
.attr (' height' , (d ) => height - Math .max (0 , y (d .value ) - heightPadding ))
239
243
bars
240
- .attr (' x' , groupOffset )
244
+ .attr (' x' , groupOffset + innerOffset )
241
245
.attr (' y' , height )
242
- .attr (' width' , paddedScale . bandwidth () )
246
+ .attr (' width' , bandwidth )
243
247
.attr (' height' , 0 )
244
248
.transition ()
245
249
.duration (800 )
@@ -258,9 +262,9 @@ function renderChart({
258
262
.attr (' width' , (d ) => Math .min (width , y (d .value ) + heightPadding ))
259
263
bars
260
264
.attr (' x' , 0 )
261
- .attr (' y' , groupOffset )
265
+ .attr (' y' , groupOffset + innerOffset )
262
266
.attr (' width' , 0 )
263
- .attr (' height' , paddedScale . bandwidth () )
267
+ .attr (' height' , bandwidth )
264
268
.transition ()
265
269
.duration (800 )
266
270
.delay ((_ , i ) => i * 100 )
@@ -355,7 +359,7 @@ watch(
355
359
background-color : var (--c-bg-elv-2 );
356
360
border : 1 px solid var (--c-divider );
357
361
border-radius : 6 px ;
358
- font-size : 12 px ;
362
+ font-size : var ( --chart-tooltip-font-size ) ;
359
363
}
360
364
361
365
:deep (.tick line) {
0 commit comments