-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
index.html
179 lines (152 loc) · 5.19 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Highcharts-scatter-csv</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<!-- load jQuery -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<!-- load Highcharts, Highcharts-more, and modules -->
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
</head>
<body>
<div id="container" style="width:100%; height:400px;"></div>
<script>
creditURL = 'https://www.datavizforall.org/highcharts';
$(function() {
$.get('data-scatter.csv', function(csv) { // EDIT to match your CSV data file name
Highcharts.chart('container', {
chart: {
type: 'scatter',
zoomType: 'xy',
events: {
load: function() {
this.credits.element.onclick = function() {
window.open(creditURL, '_blank');
}
}
}
},
title: {
text: 'Highcharts scatter chart from CSV' // EDIT to insert your chart title
},
subtitle: {
text: 'View <a href="data-scatter.csv"> source data</a>', // EDIT link and text
useHTML: true
},
data: {
csv: csv,
// data column headers (x, y, name...) must match column numbers (0, 1, 2...). Optional: category: 3
seriesMapping: [{x: 0, y: 1, name: 2}],
complete: function(data) {
categoriesIntoSeries(data);
changeSeriesColors(data);
changeSeriesMarkers(data)
}
},
legend: {
enabled: false // EDIT to true if using more than one category, color, shape
},
xAxis: {
title: {
text: 'Insert x-axis label' // EDIT to insert horizontal x-axis label
}
},
yAxis: {
title: {
text: 'Insert y-axis label' // EDIT to insert vertical y-axis label
}
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
}
}
},
stickyTracking: false,
}
},
tooltip: {
useHTML: true,
formatter: function() {
point = this.point;
// EDIT tooltip text labels below to match your data
html = '<table>';
html += '<tr><th colspan="2"><h3>District: ' + point.name + '</h3></th></tr>';
html += '<tr><th>Median household income:</th><td>$' + comma(point.x) + '</td></tr>';
html += '<tr><th>Grades above/below average:</th><td>' + point.y + '</td></tr>';
html += '</table>';
return html;
},
followPointer: true,
hideDelay: 50
},
credits: {
enabled: true,
text: 'View Highcharts.com template by DataVizForAll.org',
href: creditURL,
},
});
});
});
// Split data based on 'category' property; needed for correct legend display
function categoriesIntoSeries(data) {
rows = data.series[0].data;
data.series = [];
for (i = 0; i < rows.length; i++) {
cat = rows[i].category;
catExists = false;
for (j = 0; j < data.series.length; j++) {
if (data.series[j].name == cat) {
// Add a data point to existing series
data.series[j].data.push(rows[i]);
catExists = true;
}
}
if (!catExists) {
// When category is encountered for the first time, create a series
data.series.push({name: cat, data: [rows[i]]})
}
}
}
// Customize colors of series
function changeSeriesColors(data) {
colors = {
'SampleCategory1': 'rgba(100, 0, 0, 0.3)', // to add transparency, define colors in rgba() model, where the last number is between 0 and 1 (opacity)
'SampleCategory2': 'red'
}
for (i = 0; i < data.series.length; i++) {
data.series[i].color = colors[data.series[i].name] || 'rgba(51, 102, 255, 0.5)';
}
}
// Customize markers of scatter series. Possible options are:
// 'circle', 'square', 'diamond', 'triangle', 'triangle-down'
function changeSeriesMarkers(data) {
markers = {
'SampleCategory1': 'circle',
'SampleCategory2': 'triangle'
}
for (i = 0; i < data.series.length; i++) {
data.series[i].marker = {symbol: markers[data.series[i].name] || 'circle'};
}
}
// Returns a string that contains digits of val split by comma evey 3 positions
// Example: 12345678 -> "12,345,678"
function comma(val) {
while (/(\d+)(\d{3})/.test(val.toString())) {
val = val.toString().replace(/(\d+)(\d{3})/, '$1' + ',' + '$2');
}
return val;
}
</script>
</body>
</html>