-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
134 lines (113 loc) · 4.07 KB
/
index.ts
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
import "./style.css";
import { DataImportError, InputDataValidationErrors } from "./Errors.js";
import { Example } from "./DataSource/Example.js";
import { RadarConfig, RadarContainer } from "./RadarPie/RadarContainer.js";
import { nestedAssign, RecursivePartial } from "./utils.js";
import { RadarDataSource } from "./DataSource/RadarDataSource.js";
import { SingleDsvDataSource } from "./DataSource/SingleDsvDataSource.js";
type RadarUrlParams = {
radarDebugMode: boolean;
exampleId: number;
singleCsvUri: string;
};
const DEFAULT_EXAMPLE_ID = 2;
const CONFIG: RecursivePartial<RadarConfig> = {
// most formats can be adjusted in style.css
// There are sensible defaults for the rest but you can customize it to your needs:
container: {
/////// for more padding within the SVG container
// padding: 100,
},
pie: {
/////// marker size and symbol
// itemMarker: { size: 40, symbolType: d3.symbolCross },
/////// Empty middle
// innerRadius: 10,
/////// space between rings
// ringPadding: 5,
/////// the smallest angle (even for empty sub-slices). Tip: use 360 for equal size sub slices
// minSubSliceAngle: 12,
/////// padding around slice and sub-slice labels
// sliceLabelPadding: 8,
// subSliceLabelPadding: 4,
},
itemLegend: {
// position legend manually (center of svg: 0,0)
// pos: { x: 400, y: 0 },
/////// Increase spacing between legend items
// itemSpacing: 25,
/////// Increase padding around legend
// bBoxPadding: 20,
},
ringLegend: {
/////// position legend manually (center of svg: 0,0)
// pos: { x: 400, y: 0 },
/////// start and end angle of ring legend circles. 0 => noon, 90 = 3 o'clock
// startAngle: 0,
// endAngle: 90,
/////// size of ring legend (1 = same as main pie)
// scale: 0.3,
},
};
const urlParams = parseUrlParams();
window.RADAR_DEBUG_MODE = urlParams.radarDebugMode;
const svgDiv = document.getElementById("myRadar-div");
//////////////////////////////////////////////////////////////////////////
// Fetch data and create radar
let config: RecursivePartial<RadarConfig>;
let radarDs: RadarDataSource;
if (urlParams.singleCsvUri) {
console.log(urlParams.singleCsvUri);
radarDs = new SingleDsvDataSource(urlParams.singleCsvUri);
} else {
const example = new Example(urlParams.exampleId);
radarDs = example.getDataSource();
config = nestedAssign(example.radarConfig, CONFIG);
}
// Just as an example to listen to event which indicates that label arrangements and zooming is done.
document.addEventListener("scaleToFitEnd", () => {
console.log("scaleToFit finished. RadarChart rendering finished");
});
const radarContainer = new RadarContainer(config);
radarContainer
.fetchData(radarDs)
.then(() => {
return radarContainer.appendTo(svgDiv);
})
.catch((error) => {
let errorText: string;
if (error instanceof InputDataValidationErrors) {
// TODO: have some naming and generic source link in RadarDataSource so we can have more meaningful msgs
errorText = `${error.errors.length} input data validation errors while fetching data\n ${
error.message
}\n${error.errors.join("\n")}`;
} else if (error instanceof DataImportError) {
errorText = "Radar content import failed.\n" + error;
} else {
errorText = error;
}
console.error(error);
console.error(errorText);
const errorDiv = document.getElementById("myError-div");
errorDiv.innerHTML += `<p><pre>${errorText}</pre></p> </p>`;
});
function parseUrlParams(): RadarUrlParams {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
let exampleId = parseInt(urlParams.get("ex"));
if (isNaN(exampleId)) exampleId = DEFAULT_EXAMPLE_ID;
const debugUrlParam = urlParams.get("debug");
let radarDebugMode = false;
switch (debugUrlParam) {
case "true":
case "1":
case "on":
case "yes":
radarDebugMode = true;
break;
default:
radarDebugMode = false;
}
const singleCsvUri = urlParams.get("csv");
return { radarDebugMode, exampleId, singleCsvUri };
}