This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.js
1225 lines (1081 loc) · 30.3 KB
/
render.js
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
/*!
* ddionrails-visualization - render.js
* Copyright 2015-2018
* Licensed under AGPL (https://github.com/ddionrails/ddionrails-visualization/blob/master/LICENSE)
*/
// Global vars
// Set margin, width, padding for charts and menu
// Height of chart later set by number of data elements
var margin = { top: 30, right: 40, bottom: 40, left: 200 };
var w = 750 - margin.left - margin.right;
var h_menu = 40;
var barPadding = 1;
function render(rawData) {
if (rawData.scale != "cat") {
$("#vis_menu").hide();
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Menu
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Create Array of available options from data modell
var menu2_data = [];
for (i in rawData.bi) {
menu2_data.push(i);
}
// Append dropdown menu if bivariate data available
if (menu2_data.length > 0) {
var opt_bi = "";
menu2_data.forEach(function(i, d) {
opt_bi += "<li><a class='opt_bi chart_nav' href='#'>" + i + "</a></li>";
});
d3.select("#chart_nav_dropdown").html(
"<button type='button' class='btn btn-default dropdown-toggle chart_nav' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>More Options " +
"<span class='caret '></span>" +
"</button>" +
"<ul class='dropdown-menu chart_nav'>" +
"<li><a class='opt_bi chart_nav' href='#'>Univariate (default)</a></li>" +
"<li role='separator' class='divider'></li>" +
"<li class='dropdown-header'>Bivariate Options</li>" +
opt_bi +
"</ul>"
);
}
d3.selectAll(".opt_bi").on("click", function() {
menu2_active = this.innerHTML;
if (menu2_active == "Univariate (default)") {
menu2_active = "uni";
}
try {
if (menu2_active == "uni") {
if (rawData.scale == "cat") {
cat_uni(options);
} else if (rawData.scale == "num") {
density(options);
} else {
console.log("Error. Not defined.");
}
} else {
if (rawData.scale == "cat") {
draw_biCatChart(options, menu2_active);
}
if (rawData.scale == "num") {
density_bi(options, menu2_active);
}
}
} catch (error) {
d3.selectAll(".chart").remove();
d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", 300)
.attr("class", "chart")
.append("text")
.text("Not available.")
.attr("x", 300)
.attr("y", 100);
}
});
// Control active option in menu 2, default is 'Univariate'
var menu2_active = "uni";
// Add option 'weighted' to menu 3 if available in data modell
if (!("weighted" in rawData.uni)) {
d3.select("#weighted").attr("disabled", "disabled");
}
// Control active options in menu 3
var options = {
missings: false, // hide missings
percent: false, // show in percentages
weighted: false // use weighted data
};
// Choose diagram type by scale
if (rawData.scale == "cat") {
cat_uni(options);
} else if (rawData.scale == "num") {
density(options);
} else {
console.log("Error. Not defined.");
}
d3.selectAll(".opt").on("click", function() {
if (d3.select(this).classed("active")) {
d3.select(this).classed("active", false);
} else {
d3.select(this).classed("active", true);
}
if (options[this.id] == false) {
options[this.id] = true;
if (menu2_active == "uni") {
if (rawData.scale == "cat") {
cat_uni(options);
}
if (rawData.scale == "num") {
density(options);
}
} else {
if (rawData.scale == "cat") {
draw_biCatChart(options, menu2_active);
}
if (rawData.scale == "num") {
density_bi(options, menu2_active);
}
}
} else {
options[this.id] = false;
if (menu2_active == "uni") {
if (rawData.scale == "cat") {
cat_uni(options);
}
if (rawData.scale == "num") {
density(options);
}
} else {
if (rawData.scale == "cat") {
draw_biCatChart(options, menu2_active);
}
if (rawData.scale == "num") {
density_bi(options, menu2_active);
}
}
}
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Univariate CATEGORY CHART
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Render Univariate category chart
function cat_uni(options) {
d3.selectAll(".chart").remove();
var rData = rawData;
var hideMissings;
var dataType;
// Flags for options to modify data or not
if (options.missings == true) {
hideMissings = true;
} else {
hideMissings = false;
}
if (options.weighted == true) {
dataType = "weighted";
} else {
dataType = "frequencies";
}
// Build data modell for chart
var data = [];
for (var i = 0; i < rData.uni[dataType].length; i++) {
if (hideMissings == true && rData.uni.missings[i]) {
continue;
}
var tmp = [
rData.uni.values[i],
rData.uni.labels[i],
rData.uni[dataType][i]
];
data.push(tmp);
}
// Compute height of chart by number of data elements
var h = 100 + 20 * data.length - margin.top - margin.bottom;
// Create SVG ELement and append to #chart
var svg = d3
.select("#chart")
.append("svg")
.attr(
"viewBox",
"0 0 " +
(w + margin.left + margin.right) +
" " +
(h + margin.top + margin.bottom)
)
.attr("perserveAspectRatio", "xMinYMid")
.attr("class", "chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Color Scale
var colors = d3.scale.category20c();
colors.domain(rData.uni.labels);
// Append rect elements and map with data
var rects = svg
.selectAll("rect")
.data(data)
.enter()
.append("rect")
.style("fill", function(d) {
return colors(d[1]);
})
.attr("class", "rects");
var text = svg
.selectAll("text")
.data(data)
.enter()
.append("text")
.attr("class", "text");
// Define text labels
var format;
if (options.percent == true) {
var sum = d3.sum(
data.map(function(d) {
return d[2];
})
);
format = d3.format("0.1%");
text.text(function(d) {
return format(d[2] / sum);
});
} else {
format = d3.format("");
text.text(function(d) {
return d[2];
});
}
// X-Scale
var xScale = d3.scale
.linear()
.domain([
0,
d3.max(data, function(d) {
return d3.max(
d.filter(function(value) {
return typeof value === "number";
})
);
})
])
.range([0, w]);
// Y-Scale
var yScale = d3.scale
.ordinal()
.domain(
data.map(function(d) {
return d[1];
})
)
.rangeRoundBands([h, 0]);
// X-Axis
var xAxis = d3.svg
.axis()
.scale(xScale)
.orient("bottom");
svg
.append("g")
.call(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")");
// Y-Axis
var str;
var yAxis = d3.svg
.axis()
.scale(yScale)
.orient("left")
.tickFormat(function(d) {
if (d.length > 30) {
str = d.substr(0, 30) + "...";
} else {
str = d;
}
return str;
});
svg
.append("g")
.call(yAxis)
.attr("class", "axis");
// Tooltip: on mouseover show label and values
var tip = d3
.select("body")
.append("tip")
.attr("class", "tooltip")
.style("opacity", 0);
// Draw bars
rects
.attr("x", 0)
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("width", function(d) {
return xScale(d[2]);
})
.attr("height", h / data.length - barPadding)
.on("mouseover", function(d) {
tip.transition().style("opacity", 1);
tip.html(function() {
if (options.percent == true) {
return "<strong>" + d[1] + ": </strong>" + format(d[2] / sum);
} else {
return "<strong>" + d[1] + ": </strong>" + format(d[2]);
}
});
tip
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY + "px");
})
.on("mouseout", function(d) {
tip.transition().style("opacity", 0);
});
//Append Labels
var barHeight = h / data.length - barPadding;
text
.attr("x", function(d) {
return xScale(d[2]) + 3;
})
.attr("y", function(d) {
return yScale(d[1]) + barHeight / 2 + 2;
});
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BIVARIATE CATEGORY CHART
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function draw_biCatChart(options, menu2_active) {
var stacked;
var hideMissings;
var colors;
var format;
var format_axis;
var rData = JSON.parse(JSON.stringify(rawData));
// Color Scale
var colors = d3.scale.category20c().domain(rData.bi[menu2_active].labels);
// Show missings / hide missings
if (options.missings == true) {
hideMissings = true;
} else {
hideMissings = false;
}
// Show percentages or not
if (options.percent == true) {
offset = "expand";
format = d3.format("0.1%");
format_axis = d3.format("%");
} else {
offset = "";
format = d3.format("");
format_axis = d3.format("");
}
// unweighted or weighted data
if (options.weighted == true) {
dataType = "weighted";
} else {
dataType = "frequencies";
}
var data = [];
var indices = [];
for (i = 0; i < rData.bi[menu2_active].missings.length; i++) {
if (rData.bi[menu2_active].missings[i] == true) {
indices.unshift(i);
}
}
// Build data model for chart
for (i in rData.bi[menu2_active].categories) {
id = rData.bi[menu2_active].categories[i].label;
var freqs = rData.bi[menu2_active].categories[i][dataType];
if (hideMissings == true) {
for (i in indices) {
freqs.splice(indices[i], 1);
}
}
freqs.unshift(id);
data.push(freqs);
}
// Get Value codes and labels
var labels = rData.bi[menu2_active].labels;
var values = rData.bi[menu2_active].values;
if (hideMissings == true) {
for (i in indices) {
labels.splice(indices[i], 1);
}
}
// Map labels with data
var mapped = labels.map(function(dat, i) {
return data.map(function(d) {
return { x: d[0], y: d[i + 1], label: dat, code: values[i] };
});
});
// Stack data (normalized or not)
stacked = d3.layout.stack().offset(offset)(mapped);
// Remove current chart
d3.selectAll(".chart").remove();
// Set height for chart
var h = 300 - margin.top - margin.bottom;
barPadding = 0.2;
barOutPadding = 0.1;
// Create SVG ELement and append to #chart
var svg = d3
.select("#chart")
.append("svg")
.attr(
"viewBox",
"0 0 " +
(w + margin.left + margin.right) +
" " +
(h + margin.top + margin.bottom)
)
.attr("perserveAspectRatio", "xMinYMid")
.attr("class", "chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// X-Scale
var xScale = d3.scale
.ordinal()
.domain(
stacked[0].map(function(d) {
return d.x;
})
)
.rangeRoundBands([0, w], barPadding, barOutPadding);
// Y-Scale
var yScale = d3.scale
.linear()
.domain([
0,
d3.max(stacked[stacked.length - 1], function(d) {
return d.y0 + d.y;
})
])
.range([h, 0]);
// X-Axis
var xAxis = d3.svg
.axis()
.scale(xScale)
.orient("bottom");
svg
.append("g")
.call(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")");
// Y-Axis
var yAxis = d3.svg
.axis()
.scale(yScale)
.tickFormat(format_axis)
.orient("left");
svg
.append("g")
.call(yAxis)
.attr("class", "axis");
// Draw Bars
var layer = svg
.selectAll("layer")
.data(stacked)
.enter()
.append("g")
.attr("class", "layer")
.style("fill", function(d) {
for (i in d) {
return colors(d[i].label);
}
});
// Tooltip: on mouseover show label and values
var tip = d3
.select("body")
.append("tip")
.attr("class", "tooltip")
.style("opacity", 0);
var rect = layer
.selectAll("rect")
.data(function(d) {
return d;
})
.enter()
.append("rect")
.attr("x", function(d) {
return xScale(d.x);
})
.attr("y", function(d) {
return yScale(d.y + d.y0);
})
.attr("height", function(d) {
return yScale(d.y0) - yScale(d.y + d.y0);
})
.attr("width", xScale.rangeBand())
.attr("class", "rect")
.on("mouseover", function(d, i) {
tip.transition().style("opacity", 1);
tip
.html("<strong>" + d.label + ":</strong> " + format(d.y))
.style("left", d3.event.pageX + "px")
.style("top", d3.event.pageY + "px");
})
.on("mouseout", function(d) {
tip.transition().style("opacity", 0);
});
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Univariate DENSITY CHART
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Density draws to charts: density chart + missings chart
function density(options) {
// Remove current charts
d3.selectAll(".chart").remove();
d3.select(".chart_missings").remove();
var rData = JSON.parse(JSON.stringify(rawData));
if (options.weighted == true) {
dataType = "weighted";
} else {
dataType = "density";
}
if (options.weighted == true) {
dataType_missings = "weighted";
} else {
dataType_missings = "frequencies";
}
// Build data model for density chart
var data = [];
var range = d3.range(rData.uni.min, rData.uni.max + 1, rData.uni.by);
range.map(function(d, i) {
var tmp = [range[i], rData.uni[dataType][i]];
data.push(tmp);
});
// Set height for chart
var h = 300 - margin.top - margin.bottom;
// Create SVG ELement and append to #chart
var svg = d3
.select("#chart")
.append("svg")
.attr(
"viewBox",
"0 0 " +
(w + margin.left + margin.right) +
" " +
(h + margin.top + margin.bottom)
)
.attr("perserveAspectRatio", "xMinYMid")
.attr("class", "chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// X-Scale
var xScale = d3.scale
.ordinal()
.domain(range)
.rangePoints([0, w], 0.5);
// Y-Scale
var yScale = d3.scale
.linear()
.domain([0, d3.max(rData.uni[dataType])])
.range([h, 0]);
// X-Axis
var xAxis = d3.svg
.axis()
.scale(xScale)
.orient("bottom");
svg
.append("g")
.call(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")");
// Apend path
var path = d3.svg
.line()
.x(function(d) {
return xScale(d[0]);
})
.y(function(d) {
return yScale(d[1]);
})
.interpolate("linear");
svg
.append("path")
.attr("class", "line")
.attr("d", path(data));
// Missings Chart //
if (options.missings == false) {
var rData = rawData;
// Prepare data
var dataMissings = [];
for (var i = 0; i < rData.uni.missings[dataType_missings].length; i++) {
var tmp = [
rData.uni.missings.values[i],
rData.uni.missings.labels[i],
rData.uni.missings[dataType_missings][i]
];
dataMissings.push(tmp);
}
// Add category for vaild cases
sumValidData = d3.sum(
data.map(function(d) {
return d[1];
})
);
dataMissings.push([" ", "valid cases", sumValidData]);
// Set height for chart
var h = 80 + 10 * dataMissings.length - margin.top - margin.bottom;
// Append SVG-Element for missings chart
var svg2 = d3
.select("#chart_missings")
.append("svg")
.attr(
"viewBox",
"0 0 " +
(w + margin.left + margin.right) +
" " +
(h + margin.top + margin.bottom)
)
.attr("perserveAspectRatio", "xMinYMid")
.attr("class", "chart_missings")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Color Scale (grey)
var colors = [
"#d9d9d9",
"#969696",
"#000000",
"#ffffff",
"#f0f0f0",
"#bdbdbd",
"#525252",
"#737373",
"#252525",
"#d9d9d9",
"#969696",
"#000000",
"#ffffff",
"#f0f0f0",
"#bdbdbd",
"#525252",
"#737373",
"#252525"
];
// Append rect elements
var rects = svg2
.selectAll("rect")
.data(dataMissings)
.enter()
.append("rect")
.style("fill", function(d, i) {
if (d[1] == "valid cases") {
return "steelblue";
} else {
return colors[i];
}
})
.attr("class", "rects");
// Append labels
var text = svg2
.selectAll("text")
.data(dataMissings)
.enter()
.append("text")
.attr("class", "text");
// Show missings in % or not
sumAllData = sumValidData + d3.sum(rData.uni.missings[dataType_missings]);
if (options.percent == true) {
format = d3.format("0.1%");
text.text(function(d) {
return format(d[2] / sumAllData);
});
} else {
format = d3.format("");
text.text(function(d) {
return d[2];
});
}
// X-Scale missings
var xScale = d3.scale
.linear()
.domain([
0,
d3.max(dataMissings, function(d) {
return d3.max(
d.filter(function(value) {
return typeof value === "number";
})
);
})
])
.range([0, w]);
// Y-Scale missings
var yScale = d3.scale
.ordinal()
.domain(
dataMissings.map(function(d) {
return d[1];
})
)
.rangeRoundBands([h, 0]);
// X-Axis missings
var xAxis = d3.svg
.axis()
.scale(xScale)
.orient("bottom");
svg2
.append("g")
.call(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")");
// Y-Axis missings
var str;
var yAxis = d3.svg
.axis()
.scale(yScale)
.orient("left")
.tickFormat(function(d) {
if (d.length > 30) {
str = d.substr(0, 30) + "...";
} else {
str = d;
}
return str;
});
svg2
.append("g")
.call(yAxis)
.attr("class", "axis");
// Tooltip: on mouseover show label and values
var tip = d3
.select("body")
.append("tip")
.attr("class", "tooltip")
.style("opacity", 0);
// Draw bars and append labels
rects
.attr("x", 0)
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("width", function(d) {
return xScale(d[2]);
})
.attr("height", h / dataMissings.length - 1)
.on("mouseover", function(d) {
tip.transition().style("opacity", 1);
tip.html(function() {
if (options.percent == true) {
return (
"<strong>" + d[1] + ": </strong>" + format(d[2] / sumAllData)
);
} else {
return "<strong>" + d[1] + ": </strong>" + format(d[2]);
}
});
tip.style("left", d3.event.pageX + "px");
tip.style("top", d3.event.pageY + "px");
})
.on("mouseout", function(d) {
tip.transition().style("opacity", 0);
});
var barHeight = h / dataMissings.length - 1;
text
.attr("x", function(d) {
return xScale(d[2]) + 3;
})
.attr("y", function(d) {
return yScale(d[1]) + barHeight / 2 + 2;
});
}
if (options.missings == true) {
d3.select(".chart_missings").remove();
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BIVARIATE DENSITY CHART
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
function density_bi(options, menu2_active) {
// Remove current chart
d3.selectAll(".chart").remove();
d3.select(".chart_missings").remove();
var rData = JSON.parse(JSON.stringify(rawData));
var data = [];
var labels = [];
// Unweighted / weighted
if (options.weighted == true) {
dataType = "weighted";
} else {
dataType = "density";
}
// Prepare Data
var range = d3.range(rData.uni.min, rData.uni.max + 1, rData.uni.by);
for (i in rData.bi[menu2_active].categories) {
id = rData.bi[menu2_active].categories[i].label;
freqs = rData.bi[menu2_active].categories[i][dataType];
//freqs.unshift(id);
data.push(freqs);
labels.push(id);
}
// Set height for chart
var h = 300 - margin.top - margin.bottom;
// Append SVG-Element to #chart
var svg = d3
.select("#chart")
.append("svg")
.attr(
"viewBox",
"0 0 " +
(w + margin.left + margin.right) +
" " +
(h + margin.top + margin.bottom)
)
.attr("perserveAspectRatio", "xMinYMid")
.attr("class", "chart")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// X-Scale
var xScale = d3.scale
.ordinal()
.domain(labels)
.rangeRoundBands([0, w], 0, 0);
// Y-Scale
var yScale = d3.scale
.ordinal()
.domain(range)
.rangeRoundBands([h, 0], 0, 0.5);
// X-Axis
var xAxis = d3.svg
.axis()
.scale(xScale)
.orient("bottom");
svg
.append("g")
.call(xAxis)
.attr("class", "axis")
.attr("transform", "translate(0," + h + ")");
// Y-Axis
var yAxis = d3.svg
.axis()
.scale(yScale)
.orient("left");
svg
.append("g")
.call(yAxis)
.attr("class", "axis");
var gAxis = svg
.append("g")
.attr("class", "axis")
.call(yAxis);
// Calculate maximum length of axis ticks in order to avoid an overlap with axis label
var maxLabelWidth = 0;
gAxis.selectAll("text").each(function() {
var width = this.getBBox().width;
if (width > maxLabelWidth) {
maxLabelWidth = width;
}
});
// Append y-Axis Label
var yAxisLabel = svg
.append("text")
.attr(
"transform",
"translate(" + (-maxLabelWidth - 15) + "," + h / 2 + ")rotate(-90)"
)
.attr("class", "labels")
.attr("text-anchor", "middle")
.text(rData.label);
// Add a minified, mirrored path diagram for each category
for (i = 0; i < data.length; i++) {
var xScale2 = d3.scale
.ordinal()
.domain(d3.range(rData.uni.min, rData.uni.max + 1, rData.uni.by))
.rangeRoundBands([0, h], 0, 0.5);
var yScale2 = d3.scale
.linear()
.domain([
0,
d3.max(data, function(d) {
return d3.max(d);
})
])
.range([
xScale.rangeBand() * i + xScale.rangeBand() / 2,
xScale.rangeBand() * (i + 1) - 5
]);
var path = d3.svg
.area()
.x(function(d, i) {
return xScale2(range[i]);
})
.y(function(d) {
return yScale2(d);
})
.y0(function(d) {
return yScale2(-d);