-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraficas.js
More file actions
1710 lines (1463 loc) · 63.2 KB
/
Copy pathgraficas.js
File metadata and controls
1710 lines (1463 loc) · 63.2 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* ScientificCharts - Librería de Visualización Científica
* Arquitectura: Patrón Builder con Clases ES6
* Estilo: Publicación Científica (APA/IEEE)
* Autor: Ingeniero de Software Principal
* Versión: 1.0.0
*/
class ScientificCharts {
/**
* Constructor principal de la clase ScientificCharts
* @param {string} containerId - ID del contenedor DOM
* @param {object} config - Configuración del gráfico
*/
constructor(containerId, config = {}) {
// Validación básica
if (!containerId || typeof containerId !== 'string') {
throw new Error('El ID del contenedor es requerido y debe ser un string');
}
this.container = document.getElementById(containerId);
if (!this.container) {
throw new Error(`No se encontró el contenedor con ID: ${containerId}`);
}
// Configuración por defecto con estilo APA/IEEE
this.config = {
width: 800,
height: 600,
margin: { top: 40, right: 40, bottom: 60, left: 60 },
fontFamily: 'Arial, Helvetica, sans-serif',
fontSize: 12,
titleFontSize: 16,
axisColor: '#333333',
gridColor: '#E5E5E5',
primaryColor: '#2E5BBA',
secondaryColor: '#D32F2F',
backgroundColor: '#FFFFFF',
...config
};
// Estado interno
this.data = null;
this.svg = null;
this.scales = {};
this.elements = {};
// Inicializar SVG base
this._initializeSVG();
}
/**
* Inicializa el elemento SVG base
* @private
*/
_initializeSVG() {
this.svg = d3.select(this.container)
.append('svg')
.attr('width', this.config.width)
.attr('height', this.config.height)
.attr('font-family', this.config.fontFamily)
// role="img" + aria-label (con el título) para lectores de pantalla.
.attr('role', 'img')
.style('background-color', this.config.backgroundColor);
// Añadir definiciones para gradientes y patrones
this.defs = this.svg.append('defs');
this._createGradients();
}
/**
* Crea gradientes y patrones comunes
* @private
*/
_createGradients() {
// Gradiente para áreas sombreadas
const gradient = this.defs.append('linearGradient')
.attr('id', 'areaGradient')
.attr('x1', '0%')
.attr('y1', '0%')
.attr('x2', '0%')
.attr('y2', '100%');
gradient.append('stop')
.attr('offset', '0%')
.attr('stop-color', this.config.primaryColor)
.attr('stop-opacity', 0.3);
gradient.append('stop')
.attr('offset', '100%')
.attr('stop-color', this.config.primaryColor)
.attr('stop-opacity', 0.1);
}
/**
* Valida que los arrays tengan la misma longitud
* @param {...Array} arrays - Arrays a validar
* @private
*/
_validateArrays(...arrays) {
if (arrays.length < 2) return true;
const length = arrays[0].length;
return arrays.every(arr => Array.isArray(arr) && arr.length === length);
}
/**
* Calcula estadísticas básicas
* @param {Array} data - Array de números
* @returns {object} - Objeto con estadísticas
* @private
*/
_calculateStats(data) {
const sorted = [...data].sort((a, b) => a - b);
const n = data.length;
const mean = d3.mean(data);
const median = d3.median(data);
const std = d3.deviation(data) || 0;
const min = d3.min(data);
const max = d3.max(data);
const q1 = d3.quantile(sorted, 0.25);
const q3 = d3.quantile(sorted, 0.75);
const iqr = q3 - q1;
return { mean, median, std, min, max, q1, q3, iqr, n };
}
/**
* Crea la estructura base del gráfico (ejes, títulos)
* @param {string} title - Título del gráfico
* @param {string} xLabel - Etiqueta del eje X
* @param {string} yLabel - Etiqueta del eje Y
* @private
*/
_createChartBase(title, xLabel, yLabel) {
// Limpiar contenido previo
this.svg.selectAll('.chart-content').remove();
const g = this.svg.append('g')
.attr('class', 'chart-content')
.attr('transform', `translate(${this.config.margin.left},${this.config.margin.top})`);
// Título
if (title) {
this.svg.attr('aria-label', title);
this.svg.append('text')
.attr('x', this.config.width / 2)
.attr('y', 25)
.attr('text-anchor', 'middle')
.attr('font-size', this.config.titleFontSize)
.attr('font-weight', 'bold')
.text(title);
}
// Dimensiones del área de contenido (dentro de los márgenes)
const anchoContenido = this.config.width - this.config.margin.left - this.config.margin.right;
const altoContenido = this.config.height - this.config.margin.top - this.config.margin.bottom;
// Etiquetas de ejes
if (yLabel) {
g.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 0 - this.config.margin.left)
.attr('x', 0 - altoContenido / 2)
.attr('dy', '1em')
.style('text-anchor', 'middle')
.attr('font-size', this.config.fontSize)
.text(yLabel);
}
if (xLabel) {
// Posición relativa al área de contenido, con holgura suficiente bajo
// las etiquetas de los ticks del eje X (que quedan en altoContenido).
g.append('text')
.attr('transform', `translate(${anchoContenido / 2}, ${altoContenido + 42})`)
.style('text-anchor', 'middle')
.attr('font-size', this.config.fontSize)
.text(xLabel);
}
return g;
}
/**
* Crea una distribución gaussiana con área sombreada
* @param {Array} data - Datos numéricos
* @param {number} mean - Media (opcional)
* @param {number} std - Desviación estándar (opcional)
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
createGaussianDistribution(data, mean = null, std = null, options = {}) {
if (!Array.isArray(data) || data.length === 0) {
throw new Error('Los datos deben ser un array no vacío');
}
const stats = this._calculateStats(data);
const mu = mean !== null ? mean : stats.mean;
const sigma = std !== null ? std : stats.std;
// Generar puntos para la curva gaussiana
const xMin = mu - 4 * sigma;
const xMax = mu + 4 * sigma;
const xValues = d3.range(xMin, xMax, (xMax - xMin) / 100);
const gaussianData = xValues.map(x => ({
x: x,
y: (1 / (sigma * Math.sqrt(2 * Math.PI))) * Math.exp(-0.5 * Math.pow((x - mu) / sigma, 2))
}));
// Configurar escalas
const xScale = d3.scaleLinear()
.domain([xMin, xMax])
.range([0, this.config.width - this.config.margin.left - this.config.margin.right]);
const yScale = d3.scaleLinear()
.domain([0, d3.max(gaussianData, d => d.y)])
.range([this.config.height - this.config.margin.top - this.config.margin.bottom, 0]);
// Crear base del gráfico
const g = this._createChartBase(
options.title || 'Distribución Gaussiana',
options.xLabel || 'Valor',
options.yLabel || 'Densidad de Probabilidad'
);
// Área sombreada
const area = d3.area()
.x(d => xScale(d.x))
.y0(this.config.height - this.config.margin.top - this.config.margin.bottom)
.y1(d => yScale(d.y))
.curve(d3.curveBasis);
g.append('path')
.datum(gaussianData)
.attr('fill', 'url(#areaGradient)')
.attr('d', area);
// Línea de la curva
const line = d3.line()
.x(d => xScale(d.x))
.y(d => yScale(d.y))
.curve(d3.curveBasis);
g.append('path')
.datum(gaussianData)
.attr('fill', 'none')
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 2)
.attr('d', line);
// Líneas verticales para desviaciones estándar
[-2, -1, 0, 1, 2].forEach(sd => {
const x = mu + sd * sigma;
g.append('line')
.attr('x1', xScale(x))
.attr('x2', xScale(x))
.attr('y1', this.config.height - this.config.margin.top - this.config.margin.bottom)
.attr('y2', 0)
.attr('stroke', this.config.secondaryColor)
.attr('stroke-width', 1)
.attr('stroke-dasharray', '3,3')
.attr('opacity', 0.5);
});
// Ejes
g.append('g')
.attr('transform', `translate(0,${this.config.height - this.config.margin.top - this.config.margin.bottom})`)
.call(d3.axisBottom(xScale));
g.append('g')
.call(d3.axisLeft(yScale));
// Leyenda con estadísticas, anclada a la esquina superior derecha
// (zona libre: en las colas la densidad es baja y la curva queda abajo).
const legend = g.append('g')
.attr('transform', `translate(${this.config.width - this.config.margin.left - this.config.margin.right}, 0)`);
const legendData = [
`μ = ${mu.toFixed(3)}`,
`σ = ${sigma.toFixed(3)}`,
`N = ${stats.n}`
];
legendData.forEach((text, i) => {
legend.append('text')
.attr('y', i * 18 + 12)
.attr('text-anchor', 'end')
.attr('font-size', this.config.fontSize)
.text(text);
});
return this;
}
/**
* Crea una matriz de correlación (heatmap)
* @param {Array} data - Matriz de correlaciones
* @param {Array} labels - Etiquetas de variables
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
createCorrelationMatrix(data, labels, options = {}) {
if (!Array.isArray(data) || !Array.isArray(labels)) {
throw new Error('Los datos y etiquetas deben ser arrays');
}
const n = labels.length;
if (!this._validateArrays(...data)) {
throw new Error('Todas las filas deben tener la misma longitud');
}
// Reservar espacio para las etiquetas: un margen a la izquierda para las
// etiquetas de fila y otro arriba para las de columna (que se rotan para
// no solaparse cuando hay varias columnas o nombres largos). El tamaño de
// celda se calcula con el espacio RESTANTE, de modo que ni las celdas ni
// las etiquetas se salgan del área del gráfico.
const anchoContenido = this.config.width - this.config.margin.left - this.config.margin.right;
const altoContenido = this.config.height - this.config.margin.top - this.config.margin.bottom;
const margenEtiquetasFila = 72;
const margenEtiquetasColumna = 56;
const espacioLeyenda = 44;
const cellSize = Math.min(
(anchoContenido - margenEtiquetasFila) / n,
(altoContenido - margenEtiquetasColumna - espacioLeyenda) / n
);
const width = cellSize * n;
const height = cellSize * n;
// Acorta etiquetas muy largas (el nombre completo queda en el tooltip).
const acortarEtiqueta = t => (typeof t === 'string' && t.length > 12) ? t.slice(0, 11) + '…' : t;
// Escala de color para correlaciones
const colorScale = d3.scaleSequential()
.domain([-1, 1])
.interpolator(d3.interpolateRdBu);
// Crear base del gráfico
const g = this._createChartBase(
options.title || 'Matriz de Correlación',
options.xLabel || '',
options.yLabel || ''
);
// Subtítulo con el coeficiente empleado (coherente con el análisis).
if (options.subtitle) {
g.append('text')
.attr('x', anchoContenido / 2)
.attr('y', -8)
.attr('text-anchor', 'middle')
.attr('font-size', 11)
.attr('fill', '#555')
.text(options.subtitle);
}
// Subgrupo de la matriz, desplazado para dejar sitio a las etiquetas.
const gMatriz = g.append('g')
.attr('transform', `translate(${margenEtiquetasFila}, ${margenEtiquetasColumna})`);
// Crear celdas del heatmap
const cells = gMatriz.selectAll('.cell')
.data(data.flatMap((row, i) =>
row.map((value, j) => ({ value, i, j }))
))
.enter().append('g')
.attr('class', 'cell')
.attr('transform', d => `translate(${d.j * cellSize}, ${d.i * cellSize})`);
// Rectángulos de las celdas
cells.append('rect')
.attr('width', cellSize)
.attr('height', cellSize)
.attr('fill', d => colorScale(d.value))
.attr('stroke', '#FFFFFF')
.attr('stroke-width', 1);
// Texto con valores de correlación
cells.append('text')
.attr('x', cellSize / 2)
.attr('y', cellSize / 2)
.attr('text-anchor', 'middle')
.attr('dominant-baseline', 'middle')
.attr('font-size', Math.min(cellSize * 0.3, 14))
.attr('fill', d => Math.abs(d.value) > 0.5 ? '#FFFFFF' : '#000000')
.text(d => d.value.toFixed(2));
// Etiquetas de filas (ancladas a la derecha, dentro del margen izquierdo)
const rowLabels = gMatriz.selectAll('.row-label')
.data(labels)
.enter().append('text')
.attr('class', 'row-label')
.attr('x', -10)
.attr('y', (d, i) => i * cellSize + cellSize / 2)
.attr('text-anchor', 'end')
.attr('dominant-baseline', 'middle')
.attr('font-size', this.config.fontSize)
.text(d => acortarEtiqueta(d));
rowLabels.append('title').text(d => d);
// Etiquetas de columnas (rotadas -45° para no solaparse entre sí)
const colLabels = gMatriz.selectAll('.col-label')
.data(labels)
.enter().append('text')
.attr('class', 'col-label')
.attr('transform', (d, i) => `translate(${i * cellSize + cellSize / 2}, -8) rotate(-45)`)
.attr('text-anchor', 'start')
.attr('dominant-baseline', 'middle')
.attr('font-size', this.config.fontSize)
.text(d => acortarEtiqueta(d));
colLabels.append('title').text(d => d);
// Leyenda de color
const legendWidth = 200;
const legendHeight = 20;
const legendX = width - legendWidth;
const legendY = height + 40;
const legendScale = d3.scaleLinear()
.domain([-1, 1])
.range([0, legendWidth]);
const legendAxis = d3.axisBottom(legendScale)
.ticks(5)
.tickFormat(d3.format('.1f'));
const legend = gMatriz.append('g')
.attr('transform', `translate(${legendX}, ${legendY})`);
// Gradientes para la leyenda
const legendGradient = this.defs.append('linearGradient')
.attr('id', 'legend-gradient')
.attr('x1', '0%')
.attr('x2', '100%')
.attr('y1', '0%')
.attr('y2', '0%');
[-1, -0.5, 0, 0.5, 1].forEach((value, i) => {
legendGradient.append('stop')
.attr('offset', `${i * 25}%`)
.attr('stop-color', colorScale(value));
});
legend.append('rect')
.attr('width', legendWidth)
.attr('height', legendHeight)
.style('fill', 'url(#legend-gradient)');
legend.append('g')
.attr('transform', `translate(0, ${legendHeight})`)
.call(legendAxis);
return this;
}
/**
* Crea un diagrama de caja y bigotes (boxplot)
* @param {Array} data - Datos numéricos o array de arrays
* @param {Array} labels - Etiquetas para múltiples boxplots
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
createBoxPlot(data, labels = null, options = {}) {
let datasets;
if (Array.isArray(data[0])) {
datasets = data.map((d, i) => ({
data: d,
label: labels ? labels[i] : `Grupo ${i + 1}`
}));
} else {
datasets = [{
data: data,
label: labels ? labels[0] : 'Datos'
}];
}
// Calcular estadísticas para cada dataset
const boxData = datasets.map(dataset => {
const sorted = [...dataset.data].sort((a, b) => a - b);
const stats = this._calculateStats(dataset.data);
return {
...stats,
label: dataset.label,
outliers: dataset.data.filter(d =>
d < stats.q1 - 1.5 * stats.iqr || d > stats.q3 + 1.5 * stats.iqr
)
};
});
// Configurar escalas
const xScale = d3.scaleBand()
.domain(boxData.map(d => d.label))
.range([0, this.config.width - this.config.margin.left - this.config.margin.right])
.padding(0.3);
// Los valores se toman de `datasets` (que conserva `.data`); `boxData`
// solo guarda estadísticos resumidos, no las observaciones originales.
const allValues = datasets.flatMap(d => d.data);
const yScale = d3.scaleLinear()
.domain([d3.min(allValues) * 0.9, d3.max(allValues) * 1.1])
.range([this.config.height - this.config.margin.top - this.config.margin.bottom, 0]);
// Crear base del gráfico
const g = this._createChartBase(
options.title || 'Diagrama de Caja y Bigotes',
options.xLabel || '',
options.yLabel || 'Valor'
);
// Crear boxplots
boxData.forEach((d, i) => {
const x = xScale(d.label);
const width = xScale.bandwidth();
// Caja principal
g.append('rect')
.attr('x', x)
.attr('y', yScale(d.q3))
.attr('width', width)
.attr('height', yScale(d.q1) - yScale(d.q3))
.attr('fill', this.config.primaryColor)
.attr('opacity', 0.3)
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
// Línea media
g.append('line')
.attr('x1', x)
.attr('x2', x + width)
.attr('y1', yScale(d.median))
.attr('y2', yScale(d.median))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 2);
// Bigotes
const whiskerTop = Math.min(d.max, d.q3 + 1.5 * d.iqr);
const whiskerBottom = Math.max(d.min, d.q1 - 1.5 * d.iqr);
// Líneas de bigotes
g.append('line')
.attr('x1', x + width / 2)
.attr('x2', x + width / 2)
.attr('y1', yScale(whiskerBottom))
.attr('y2', yScale(d.q1))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
g.append('line')
.attr('x1', x + width / 2)
.attr('x2', x + width / 2)
.attr('y1', yScale(d.q3))
.attr('y2', yScale(whiskerTop))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
// Límites de bigotes
g.append('line')
.attr('x1', x + width * 0.3)
.attr('x2', x + width * 0.7)
.attr('y1', yScale(whiskerBottom))
.attr('y2', yScale(whiskerBottom))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
g.append('line')
.attr('x1', x + width * 0.3)
.attr('x2', x + width * 0.7)
.attr('y1', yScale(whiskerTop))
.attr('y2', yScale(whiskerTop))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
// Outliers
if (d.outliers.length > 0) {
g.selectAll(`.outlier-${i}`)
.data(d.outliers)
.enter().append('circle')
.attr('class', `outlier-${i}`)
.attr('cx', x + width / 2)
.attr('cy', d => yScale(d))
.attr('r', 3)
.attr('fill', this.config.secondaryColor);
}
});
// Ejes
g.append('g')
.attr('transform', `translate(0,${this.config.height - this.config.margin.top - this.config.margin.bottom})`)
.call(d3.axisBottom(xScale));
g.append('g')
.call(d3.axisLeft(yScale));
return this;
}
/**
* Crea un diagrama de violín
* @param {Array} data - Datos numéricos o array de arrays
* @param {Array} labels - Etiquetas para múltiples violines
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
createViolinPlot(data, labels = null, options = {}) {
let datasets;
if (Array.isArray(data[0])) {
datasets = data.map((d, i) => ({
data: d,
label: labels ? labels[i] : `Grupo ${i + 1}`
}));
} else {
datasets = [{
data: data,
label: labels ? labels[0] : 'Datos'
}];
}
// Configurar escalas
const xScale = d3.scaleBand()
.domain(datasets.map(d => d.label))
.range([0, this.config.width - this.config.margin.left - this.config.margin.right])
.padding(0.3);
const allValues = datasets.flatMap(d => d.data);
const yScale = d3.scaleLinear()
.domain([d3.min(allValues) * 0.9, d3.max(allValues) * 1.1])
.range([this.config.height - this.config.margin.top - this.config.margin.bottom, 0]);
// Crear base del gráfico
const g = this._createChartBase(
options.title || 'Diagrama de Violín',
options.xLabel || '',
options.yLabel || 'Valor'
);
// Función para generar curva de densidad
const kde = (data, bandwidth = 1) => {
const n = data.length;
const min = d3.min(data);
const max = d3.max(data);
const x = d3.range(min, max, (max - min) / 100);
return x.map(xi => {
let sum = 0;
data.forEach(d => {
sum += Math.exp(-0.5 * Math.pow((xi - d) / bandwidth, 2));
});
return {
x: xi,
y: sum / (n * bandwidth * Math.sqrt(2 * Math.PI))
};
});
};
// Crear violines
datasets.forEach((dataset, i) => {
const x = xScale(dataset.label);
const width = xScale.bandwidth();
// Calcular densidad
// Regla de Silverman; si la desviación es 0 (datos constantes) el
// ancho de banda sería 0 y la densidad daría NaN, así que se usa un
// mínimo positivo.
const desviacion = d3.deviation(dataset.data) || 0;
const bandwidth = (1.06 * desviacion * Math.pow(dataset.data.length, -0.2)) || 1;
const density = kde(dataset.data, bandwidth);
const maxDensity = d3.max(density, d => d.y);
// Escalar el ancho del violín
const xViolin = d3.scaleLinear()
.domain([0, maxDensity])
.range([width / 2, 0]);
// Generar paths para ambos lados del violín
const areaLeft = d3.area()
.x0(d => x + width / 2 - xViolin(d.y))
.x1(d => x + width / 2)
.y(d => yScale(d.x))
.curve(d3.curveBasis);
const areaRight = d3.area()
.x0(d => x + width / 2)
.x1(d => x + width / 2 + xViolin(d.y))
.y(d => yScale(d.x))
.curve(d3.curveBasis);
// Área del violín
g.append('path')
.datum(density)
.attr('d', areaLeft)
.attr('fill', this.config.primaryColor)
.attr('opacity', 0.3);
g.append('path')
.datum(density)
.attr('d', areaRight)
.attr('fill', this.config.primaryColor)
.attr('opacity', 0.3);
// Línea central
const stats = this._calculateStats(dataset.data);
g.append('line')
.attr('x1', x)
.attr('x2', x + width)
.attr('y1', yScale(stats.median))
.attr('y2', yScale(stats.median))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 2);
// Cuartiles
g.append('line')
.attr('x1', x + width * 0.25)
.attr('x2', x + width * 0.75)
.attr('y1', yScale(stats.q1))
.attr('y2', yScale(stats.q1))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
g.append('line')
.attr('x1', x + width * 0.25)
.attr('x2', x + width * 0.75)
.attr('y1', yScale(stats.q3))
.attr('y2', yScale(stats.q3))
.attr('stroke', this.config.primaryColor)
.attr('stroke-width', 1);
});
// Ejes
g.append('g')
.attr('transform', `translate(0,${this.config.height - this.config.margin.top - this.config.margin.bottom})`)
.call(d3.axisBottom(xScale));
g.append('g')
.call(d3.axisLeft(yScale));
return this;
}
/**
* Crea un scatter plot con línea de regresión
* @param {Array} xData - Datos del eje X
* @param {Array} yData - Datos del eje Y
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
createScatterPlot(xData, yData, options = {}) {
if (!this._validateArrays(xData, yData)) {
throw new Error('Los arrays deben tener la misma longitud');
}
// Calcular regresión lineal
const n = xData.length;
const sumX = d3.sum(xData);
const sumY = d3.sum(yData);
const sumXY = d3.sum(xData.map((x, i) => x * yData[i]));
const sumXX = d3.sum(xData.map(x => x * x));
const sumYY = d3.sum(yData.map(y => y * y));
const slope = (n * sumXY - sumX * sumY) / (n * sumXX - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
// Calcular R²
const meanY = sumY / n;
const ssTotal = d3.sum(yData.map(y => Math.pow(y - meanY, 2)));
const ssResidual = d3.sum(yData.map((y, i) =>
Math.pow(y - (slope * xData[i] + intercept), 2)
));
const rSquared = 1 - (ssResidual / ssTotal);
// Configurar escalas
const xScale = d3.scaleLinear()
.domain(d3.extent(xData))
.range([0, this.config.width - this.config.margin.left - this.config.margin.right]);
const yScale = d3.scaleLinear()
.domain(d3.extent(yData))
.range([this.config.height - this.config.margin.top - this.config.margin.bottom, 0]);
// Crear base del gráfico
const g = this._createChartBase(
options.title || 'Scatter Plot con Regresión',
options.xLabel || 'X',
options.yLabel || 'Y'
);
// Línea de regresión
const xMin = d3.min(xData);
const xMax = d3.max(xData);
const yMinPred = slope * xMin + intercept;
const yMaxPred = slope * xMax + intercept;
g.append('line')
.attr('x1', xScale(xMin))
.attr('x2', xScale(xMax))
.attr('y1', yScale(yMinPred))
.attr('y2', yScale(yMaxPred))
.attr('stroke', this.config.secondaryColor)
.attr('stroke-width', 2)
.attr('stroke-dasharray', '5,5');
// Puntos de datos
g.selectAll('.dot')
.data(xData.map((x, i) => ({ x, y: yData[i] })))
.enter().append('circle')
.attr('class', 'dot')
.attr('cx', d => xScale(d.x))
.attr('cy', d => yScale(d.y))
.attr('r', 4)
.attr('fill', this.config.primaryColor)
.attr('opacity', 0.7);
// Ejes
g.append('g')
.attr('transform', `translate(0,${this.config.height - this.config.margin.top - this.config.margin.bottom})`)
.call(d3.axisBottom(xScale));
g.append('g')
.call(d3.axisLeft(yScale));
// Leyenda con ecuación y R²
const legend = g.append('g')
.attr('transform', `translate(${this.config.width - this.config.margin.left - this.config.margin.right - 150}, 20)`);
legend.append('rect')
.attr('width', 140)
.attr('height', 60)
.attr('fill', 'white')
.attr('stroke', '#ccc')
.attr('opacity', 0.8);
legend.append('text')
.attr('x', 10)
.attr('y', 20)
.attr('font-size', this.config.fontSize)
.text(`y = ${slope.toFixed(3)}x + ${intercept.toFixed(3)}`);
legend.append('text')
.attr('x', 10)
.attr('y', 40)
.attr('font-size', this.config.fontSize)
.text(`R² = ${rSquared.toFixed(3)}`);
return this;
}
/**
* Crea un gráfico Q-Q (cuantil-cuantil) para evaluar visualmente la
* normalidad: enfrenta los cuantiles observados (estandarizados) con los
* cuantiles teóricos de la normal estándar. Si los puntos se alinean con la
* recta de referencia y = x, la distribución es aproximadamente normal.
* @param {Array} data - Datos numéricos
* @param {object} options - Opciones adicionales
* @returns {ScientificCharts} - Instancia para chaining
*/
// ----------------------------------------------------------------
// DISPERSIÓN PROFESIONAL: puntos translúcidos, recta de mínimos
// cuadrados, BANDA DE CONFIANZA 95% de la recta y caja de anotaciones
// estadísticas (método, coeficiente, p, R², n). Nivel publicación.
// ----------------------------------------------------------------
createScatterPlotPro(xData, yData, options = {}) {
this._validateArrays(xData, yData);
const n = xData.length;
const W = this.config.width - this.config.margin.left - this.config.margin.right;
const H = this.config.height - this.config.margin.top - this.config.margin.bottom;
// Regresión por mínimos cuadrados
const xm = d3.mean(xData), ym = d3.mean(yData);
let Sxx = 0, Sxy = 0;
for (let i = 0; i < n; i++) { const dx = xData[i] - xm; Sxx += dx * dx; Sxy += dx * (yData[i] - ym); }
const b = Sxx === 0 ? 0 : Sxy / Sxx;
const a = ym - b * xm;
let SSE = 0;
for (let i = 0; i < n; i++) { const e = yData[i] - (a + b * xData[i]); SSE += e * e; }
const gl = Math.max(n - 2, 1);
const s = Math.sqrt(SSE / gl);
const t = 1.96 + 2.4 / gl; // aprox. t(0.975, gl), suficiente para visualización
// Escalas con 5% de respiro
const padX = (d3.max(xData) - d3.min(xData)) * 0.05 || 1;
const padY = (d3.max(yData) - d3.min(yData)) * 0.05 || 1;
const xScale = d3.scaleLinear().domain([d3.min(xData) - padX, d3.max(xData) + padX]).range([0, W]);
const yScale = d3.scaleLinear().domain([d3.min(yData) - padY, d3.max(yData) + padY]).range([H, 0]);
const g = this._createChartBase(options.title || 'Diagrama de dispersión',
options.xLabel || 'X', options.yLabel || 'Y');
// Rejilla sutil
g.append('g').attr('class', 'grid')
.selectAll('line.h').data(yScale.ticks(6)).enter().append('line')
.attr('x1', 0).attr('x2', W)
.attr('y1', d => yScale(d)).attr('y2', d => yScale(d))
.attr('stroke', '#94a3b8').attr('stroke-opacity', 0.18);
g.append('g').attr('class', 'grid')
.selectAll('line.v').data(xScale.ticks(6)).enter().append('line')
.attr('y1', 0).attr('y2', H)
.attr('x1', d => xScale(d)).attr('x2', d => xScale(d))
.attr('stroke', '#94a3b8').attr('stroke-opacity', 0.12);
// Banda de confianza 95% de la recta (media condicional)
const dominio = xScale.domain();
const malla = d3.range(60).map(i => dominio[0] + (i / 59) * (dominio[1] - dominio[0]));
const banda = malla.map(x => {
const half = t * s * Math.sqrt(1 / n + (Sxx === 0 ? 0 : ((x - xm) ** 2) / Sxx));
const yc = a + b * x;
return { x, lo: yc - half, hi: yc + half };
});
g.append('path').datum(banda)
.attr('fill', this.config.primaryColor).attr('fill-opacity', 0.13).attr('stroke', 'none')
.attr('d', d3.area().x(d => xScale(d.x)).y0(d => yScale(d.lo)).y1(d => yScale(d.hi)));
// Puntos
g.selectAll('.punto').data(xData).enter().append('circle')
.attr('cx', (d, i) => xScale(xData[i])).attr('cy', (d, i) => yScale(yData[i]))
.attr('r', 4).attr('fill', this.config.primaryColor).attr('fill-opacity', 0.55)
.attr('stroke', '#ffffff').attr('stroke-width', 0.8);
// Recta de regresión sobre la banda
g.append('line')
.attr('x1', xScale(dominio[0])).attr('y1', yScale(a + b * dominio[0]))
.attr('x2', xScale(dominio[1])).attr('y2', yScale(a + b * dominio[1]))
.attr('stroke', '#b91c1c').attr('stroke-width', 2.2);
// Caja de anotaciones (lado opuesto a la pendiente para no tapar puntos)
const lineas = options.annotationLines || [];
if (lineas.length) {
const anchoCaja = 12 + 7.2 * d3.max(lineas, l => l.length);
const altoCaja = 14 + lineas.length * 17;
const cx = b >= 0 ? 10 : W - anchoCaja - 10;
const caja = g.append('g').attr('transform', `translate(${cx},8)`);
caja.append('rect').attr('width', anchoCaja).attr('height', altoCaja)
.attr('rx', 6).attr('fill', '#ffffff').attr('fill-opacity', 0.92)
.attr('stroke', '#cbd5e1');
lineas.forEach((l, i) => {
caja.append('text').attr('x', 8).attr('y', 20 + i * 17)
.attr('font-size', 12).attr('fill', '#1e293b').text(l);
});
}
return this;
}
// ----------------------------------------------------------------
// HISTOGRAMA CON CURVA NORMAL TEÓRICA N(μ, σ) superpuesta, escalada a
// frecuencias esperadas. Acompaña al Q-Q en el panel de normalidad.
// ----------------------------------------------------------------
createHistogramNormal(data, options = {}) {
if (!Array.isArray(data) || data.length < 3) {
throw new Error('Se necesitan al menos 3 datos');
}
const n = data.length;
const media = d3.mean(data), de = d3.deviation(data) || 1;
const binCount = Math.ceil(Math.log2(n)) + 1;
const histo = d3.histogram().domain(d3.extent(data)).thresholds(binCount);
const bins = histo(data);
const anchoBin = bins.length ? (bins[0].x1 - bins[0].x0) : 1;
const W = this.config.width - this.config.margin.left - this.config.margin.right;
const H = this.config.height - this.config.margin.top - this.config.margin.bottom;
const xScale = d3.scaleLinear().domain(d3.extent(data)).range([0, W]).nice();
const pdf = x => Math.exp(-((x - media) ** 2) / (2 * de * de)) / (de * Math.sqrt(2 * Math.PI));
const maxY = Math.max(d3.max(bins, d => d.length), n * anchoBin * pdf(media)) * 1.08;
const yScale = d3.scaleLinear().domain([0, maxY]).range([H, 0]);
const g = this._createChartBase(options.title || 'Histograma con curva normal',
options.xLabel || 'Valor', options.yLabel || 'Frecuencia');
// Ejes con marcas numéricas: el eje X muestra los valores de los
// intervalos de las barras y el eje Y las frecuencias.
const ejeX = g.append('g').attr('transform', `translate(0,${H})`)
.call(d3.axisBottom(xScale).ticks(7).tickSizeOuter(0));
const ejeY = g.append('g')
.call(d3.axisLeft(yScale).ticks(5).tickFormat(d3.format('d')).tickSizeOuter(0));
[ejeX, ejeY].forEach(e => e.selectAll('text').attr('font-size', 10).attr('fill', '#333'));
g.selectAll('.barra').data(bins).enter().append('rect')
.attr('x', d => xScale(d.x0) + 0.5)
.attr('width', d => Math.max(xScale(d.x1) - xScale(d.x0) - 1, 1))
.attr('y', d => yScale(d.length))
.attr('height', d => H - yScale(d.length))
.attr('fill', this.config.primaryColor).attr('fill-opacity', 0.55)
.attr('stroke', '#ffffff');
// Curva normal teórica escalada a frecuencias: f(x) = n·Δbin·pdf(x)
const dom = xScale.domain();
const curva = d3.range(80).map(i => {
const x = dom[0] + (i / 79) * (dom[1] - dom[0]);
return { x, y: n * anchoBin * pdf(x) };
});
g.append('path').datum(curva)
.attr('fill', 'none').attr('stroke', '#b91c1c').attr('stroke-width', 2.2)
.attr('d', d3.line().x(d => xScale(d.x)).y(d => yScale(d.y)).curve(d3.curveBasis));
g.append('text').attr('x', W - 6).attr('y', 14).attr('text-anchor', 'end')
.attr('font-size', 11).attr('fill', '#b91c1c')
.text(`— Normal teórica N(${media.toFixed(1)}, ${de.toFixed(1)})`);
return this;
}