BarChart.svelte
5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<script>
import { scaleLinear, scaleBand } from 'd3-scale';
let {
data = [],
hoveredYear = $bindable(null),
height = 220,
fill = false,
marginTop = 20,
marginRight = 10,
marginBottom = 30,
marginLeft = 50
} = $props();
// Medir altura real del contenedor cuando fill=true
let wrapperEl = $state(null);
let measuredHeight = $state(height);
$effect(() => {
if (fill && wrapperEl) {
const updateHeight = () => {
const h = wrapperEl.clientHeight;
if (h > 0) measuredHeight = h;
};
updateHeight();
const observer = new ResizeObserver(updateHeight);
observer.observe(wrapperEl);
return () => observer.disconnect();
} else {
measuredHeight = height;
}
});
// Dimensiones del área del gráfico
let effectiveHeight = $derived(fill ? measuredHeight : height);
let chartHeight = $derived(Math.max(effectiveHeight - marginTop - marginBottom, 50));
// Máximo del eje Y (con guard para data vacía o valores undefined)
let maxPerCapita = $derived(
data.length > 0 ? Math.max(...data.map(d => d.perCapita || 0), 1) : 1
);
// Escalas D3
let xScale = $derived(
scaleBand()
.domain(data.map(d => d.año))
.range([0, 100]) // porcentaje
.padding(0.2)
);
// Para CSS bottom positioning: 0 → 0%, max → 100%
let yScale = $derived(
scaleLinear()
.domain([0, maxPerCapita])
.range([0, chartHeight])
.nice()
);
// Ticks para el eje Y
let yTicks = $derived(yScale.ticks(3));
// Índices para mostrar en eje X
let midIndex = $derived(Math.floor(data.length / 2));
// Formato para valores del eje Y
function formatValue(v) {
if (v >= 1e6) return (v / 1e6).toLocaleString('es-BO', { maximumFractionDigits: 1 }) + ' M';
if (v >= 1e3) return Math.round(v / 1e3).toLocaleString('es-BO') + ' K';
return v.toLocaleString('es-BO');
}
</script>
<div class="chart-wrapper" bind:this={wrapperEl} style="height: {fill ? '100%' : height + 'px'}; min-height: {height}px">
{#if data.length > 0 && chartHeight > 0}
<!-- Contenedor principal con márgenes -->
<div class="chart-inner" style="top: {marginTop}px; bottom: {marginBottom}px; left: {marginLeft}px; right: {marginRight}px">
<!-- Y-axis labels (fuera del área de barras, a la izquierda) -->
{#each yTicks as tick}
{@const pct = (yScale(tick) / chartHeight) * 100}
<span class="y-label" style="bottom: {pct}%; left: -{marginLeft}px">
Bs {formatValue(tick)}
</span>
{/each}
<!-- Grid lines -->
{#each yTicks as tick}
{@const pct = (yScale(tick) / chartHeight) * 100}
<div class="grid-line" style="bottom: {pct}%"></div>
{/each}
<!-- Barras -->
<div class="bars" onmouseleave={() => hoveredYear = null} role="group">
{#each data as d}
{@const barPct = (yScale(d.perCapita) / chartHeight) * 100}
<div
class="bar-container"
onmouseenter={() => hoveredYear = d.año}
role="button"
tabindex="0"
>
<div
class="bar"
class:hovered={hoveredYear === d.año}
style="height: {barPct}%"
></div>
</div>
{/each}
</div>
</div>
<!-- Eje X -->
<div class="x-axis" style="height: {marginBottom}px; left: {marginLeft}px; right: {marginRight}px">
{#each data as d, i}
{@const isFirst = i === 0}
{@const isLast = i === data.length - 1}
{@const isMid = i === midIndex && data.length > 5}
{@const showLabel = isFirst || isLast || isMid || hoveredYear === d.año}
<span
class="x-label"
class:visible={showLabel}
>
{d.año}
</span>
{/each}
</div>
{:else}
<div class="chart-empty">
<span>Sin datos</span>
</div>
{/if}
</div>
<style>
.chart-wrapper {
position: relative;
width: 100%;
transition: height 0.3s ease;
}
.chart-inner {
position: absolute;
}
.y-label {
position: absolute;
width: 45px;
text-align: right;
transform: translateY(50%);
font-family: 'Qanelas', var(--font-sans);
font-size: 0.6875rem;
color: var(--theme-texto);
white-space: nowrap;
}
.grid-line {
position: absolute;
left: 0;
right: 0;
border-top: 0.5px dotted var(--theme-texto);
opacity: 0.15;
pointer-events: none;
}
.bars {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
align-items: flex-end;
gap: 2px;
}
.bar-container {
flex: 1;
height: 100%;
display: flex;
align-items: flex-end;
cursor: default;
}
.bar {
width: 100%;
background: #c4897d;
border-radius: 4px 4px 0 0;
transition: opacity 0.1s ease;
min-height: 2px;
}
:global(html.dark) .bar {
background: rgba(107, 159, 212, 0.85);
}
.bars:hover .bar {
opacity: 0.35;
}
.bars:hover .bar.hovered {
opacity: 1;
}
.x-axis {
position: absolute;
bottom: 0;
display: flex;
align-items: center;
}
.x-label {
flex: 1;
text-align: center;
font-family: 'Qanelas', var(--font-sans);
font-size: 0.6875rem;
color: var(--theme-texto);
opacity: 0;
transition: opacity 0.2s ease;
}
.x-label.visible {
opacity: 1;
}
.chart-empty {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
color: var(--theme-texto);
opacity: 0.5;
font-size: 0.875rem;
}
</style>