-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
280 lines (276 loc) · 12 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
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
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>殷叶航</title>
<style type="text/css">
body {
font-family: -apple-system, "Noto Sans", "Helvetica Neue", Helvetica, "Nimbus Sans L", Arial, "Liberation Sans", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Source Han Sans SC", "Source Han Sans CN", "Microsoft YaHei", "Wenquanyi Micro Hei", "WenQuanYi Zen Hei", "ST Heiti", SimHei, "WenQuanYi Zen Hei Sharp", sans-serif;
}
p {
padding: 0 20px;
line-height: 1.8em;
}
.code {
font-family: 'Consolas', 'Menlo', 'Monaco', 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif;
}
#main {
width: 80%;
max-width: 800px;
margin-left: auto;
margin-right: auto;
padding: 5% 0;
}
.stacked-label {
background: #fffffff0;
border: solid #eeeeee 1px;
border-radius: 10px;
box-shadow: 0 5px 20px #00000022;
}
.stacked-indicator {
width: 10px;
height: 10px;
left: 0;
margin: 5px;
border-radius: 5px;
position: relative;
}
.stacked-title, .stacked-value {
margin: 10px;
display: block;
white-space: nowrap;
position: relative;
}
.stacked-title {
margin-top: 5px;
font-weight: bold;
}
.stacked-polygon {
opacity: 1;
transition: all 500ms;
stroke: white;
stroke-width: 4;
user-select: none;
-moz-user-select: none;
}
.stacked-polygon:not(:hover) {
opacity: 0.8;
transition: all 500ms;
stroke: white;
stroke-width: 0;
user-select: none;
-moz-user-select: none;
}
h1, h2, h3, h4, h5, h6 {
text-align: center;
color: #0089A7;
}
h1 {
font-size: 48pt;
}
h3 {
text-align: left;
padding: 0 20px;
}
.warn {
background: #ffeeaa;
padding: 5px 15px;
border: solid 1px #ddbb88;
border-radius: 10px;
}
button:not(:active) {
outline: none;
color: #0089A7;
background: white;
border: solid 2px #0089A7;
border-radius: 10px;
padding: 10px;
margin: 5px;
transition: all 200ms;
}
button {
outline: none;
color: white;
background: #0089A7;
border: solid 2px #0089A7;
border-radius: 10px;
padding: 8px;
margin: 5px;
transition: all 200ms;
}
.toggle-buttons {
list-style-type: none;
display: inline-block;
color: #0089A7;
}
.toggle-buttons li:first-child label {
border-radius: 10px 0 0 10px;
}
.toggle-buttons li:last-child label {
border-radius: 0 10px 10px 0;
}
.toggle-buttons li {
float: left;
position: relative;
}
.toggle-buttons input[type=radio], .toggle-buttons label {
display: block;
cursor: pointer;
top: 0; left: 0; right: 0; bottom: 0;
}
.toggle-buttons input[type=radio] {
width: 100%;
height: 100%;
z-index: 100;
position: absolute;
opacity: 0;
}
.toggle-buttons label {
padding: 10px;
margin: 0 -1px;
z-index: 90;
border: solid 2px #0089A7;
transition: all 200ms;
}
.toggle-buttons input[type=radio]:checked + label {
background: #0089A7;
color: white;
transition: all 200ms;
}
</style>
</head>
<body>
<!-- plz insert your doms under the main div element -->
<div id="main">
<a href="https://github.com/celphi-misc/stacked-graph/"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
<h1>Stacked Graph</h1>
<div style="text-align:center">混合1506 殷叶航 3150104531</div>
<svg height="400px" width="100%" id="stacked"></svg>
<p>提示:在图表上悬浮鼠标指针或长按来交互。</p>
<p>基线函数:</p>
<div style="text-align:center">
<form class="toggle-buttons">
<li>
<input type="radio" name="baseline" value="zero" onchange="stackedGraph.baseline = this.value"/>
<label for="zero">Zero</label>
</li>
<li>
<input type="radio" name="baseline" value="theme" checked="checked" onchange="stackedGraph.baseline = this.value"/>
<label for="theme">Theme River</label>
</li>
<li>
<input type="radio" name="baseline" value="wiggle" onchange="stackedGraph.baseline = this.value"/>
<label for="wiggle">Wiggle</label>
</li>
<li>
<input type="radio" name="baseline" value="weighted" onchange="stackedGraph.baseline = this.value"/>
<label for="weighted">Weighted Wiggle</label>
</li>
</form>
</div>
<p>随机更新数据:</p>
<div style="text-align:center">
<button onclick="updateData(false)">更新</button>
<button onclick="updateData(true)">变换数据长度更新</button>
</div>
<p>main.js 可以当成一个独立的库来使用,下面我会简单介绍一下它的接口。</p>
<h2>API</h2>
<h3>构造器</h3>
<p class="code">let stackedGraph = new StackedGraph('element-id' | svgElement: SVGElement);</p>
<p>构造器会在指定的 <span class="code"><svg></span> 元素上构造一个默认的 <span class="code">StackedGraph</span> 对象。</p>
<h3>显示数据</h3>
<p class="code">stackedGraph.updateDrawing(animated: boolean, animationTiming: number);</p>
<p>该函数会刷新堆叠图的显示,当用户还没有指定堆叠图数据时,会采用默认数据。该函数第一个参数是一个 <span>boolean</span> 为是否启用动画,第二个参数为动画的时间,单位为毫秒。</p>
<h3>更新数据</h3>
<p class="code">stackedGraph.data = [[1, 2, 1, 3, 5, 2], [2, 3, 5, 7, 8, 3], ...]</p>
<p>每组数据都是一个数列,且要求每个数列长度均相等。数据变更会自动以动画刷新呈现。</p>
<p class="code">stackedGraph.animationTiming = 1000;</p>
<p>以毫秒数设定默认动画的时间。</p>
<h3>改变颜色</h3>
<p class="code">stackedGraph.colors = [ '#0089A7', '#3AA8C0', '#49D5EE', '#00748D' ];</p>
<p>通过改变数组来改变配色方案。数据组的颜色会在这个数组中循环。</p>
<p class="warn">注意:更改配色并不会立即刷新,你需要手动调用 <span class="code">updateDrawing</span> 来刷新。</p>
<h3>坐标映射</h3>
<p>在这一可视化工具的设计中,每组数据只允许以一位数组的方式输入。如果需要改编横坐标的映射关系(不是从0开始,或是横坐标的含义非连续的度量值),可以通过修改下面这个函数来做到:</p>
<p class="code">stackedGraph.tagMapping = index => '' + index;</p>
<p>上面是横坐标映射的默认行为,即将数组编号作为横坐标。下面一个事例改写了这个函数:</p>
<p class="code">stackedGraph.tagMapping = index => index * 2 + 2 + '';</p>
<p>它的行为是,将横坐标映射从数组索引调整为 2, 4, 6, 8</p>
<p class="warn">注意:更改坐标映射并不会立即刷新,你需要手动调用 <span class="code">updateDrawing</span> 来刷新。</p>
<h3>基线函数</h3>
<p>这个库提供了常见几种基线函数,包括零函数、Theme River、Wiggle 和加权 Wiggle 四种。你可以通过下面这种方式更改,每次更改后画面会自动刷新:</p>
<p class="code">stackedGraph.baseline = 'zero' | 'theme' | 'wiggle' | 'weighted';</p>
<h3>事件机制</h3>
<p>这个库并没有提供原生的显示数据标签和鼠标交互的功能,尽管这意味着使用者需要自己手工编程来实现这一功能,但也意味着实现可以更加灵活和自由。事件机制的策略是:画面元素接受了一个鼠标/触摸事件,计算这个事件相关的堆叠图属性,例如是哪组数据被单击、光标现在在数组的哪个下标等。然后用一个自定义事件包裹之前的事件,并加入这些属性,再从根 <span class="code"><svg></span> DOM 元素 <span class="code">stackedGraph.dom</span> 发射这一事件。你只需要从根 DOM 监听这一事件就可以了。</p>
<p>支持的事件有:<span class="code">'mousedown', 'mouseenter', 'mouseleave', 'mousemove', 'mouseout', 'mouseover', 'mouseup', 'mousewheel', 'touchstart', 'touchend', 'touchcancel', 'touchmove'</span></p>
<p>被包裹后的事件会以 <span class="code">stacked-xxx</span> 形式命名,从根 <span class="code"><svg></span> DOM 发射出去。</p>
<p>下面的实例将展示这一事件机制的使用方式,以及所支持访问的成员:</p>
<p class="code">
stackedGraph.dom.addEventListener('stacked-mousemove', event => {<br>
console.log(event.stackedIndex); // 数据组编号<br>
console.log(event.dataIndex); // 光标所在数组下标<br>
console.log(stakcedGraph.tagMapping(event.dataIndex));
console.log(event.stackedData); // 光标下数据<br>
});
</p>
<h3>使用 CSS</h3>
<p>你可以通过 DOM 的类去选取堆叠图中的元素,设置其样式。部分样式是在 JavaScript 代码中以内联方式指定的样式,如果需要更改,请在 CSS 中使用 <span class="code">!important</span> 声明。</p>
<ul style="line-height:1.8em">
<li>横坐标轴线具有类 <span class="code">stacked-h-axis</span></li>
<li>横坐标刻度具有类 <span class="code">stacked-h-grad</span></li>
<li>堆叠图多边形具有类 <span class="code">stacked-polygon</span>,同时具有组编号 <span class="code">stacked-x</span> 其中 <span class="code">x</span> 可能是 <span class="code">1, 2, 3...</span></li>
</ul>
<h3>其他</h3>
<p>这个 HTML 文档就是对 <span class="code">main.js</span> 的使用范例。下面包含有一小段初始化代码可供参考。</p>
<h2>Reference</h2>
<p>Stacked Graphs – Geometry & Aesthetics, Lee Byron & Martin Wattenberg</p>
</div>
<script type="text/javascript" src="main.js"></script>
<script>
window.addEventListener('load', event => {
let stackedGraph = window.stackedGraph = new StackedGraph('stacked');
let dataTitles = ['项目A', '项目B', '项目C', '项目D', '项目E', '项目F'];
stackedGraph.updateDrawing(true, stackedGraph.animationTiming);
let labelDOM = document.createElement('div');
labelDOM.classList.add('stacked-label');
labelDOM.style.position = 'fixed';
let indicator = document.createElement('div');
indicator.classList.add('stacked-indicator');
labelDOM.appendChild(indicator);
let titleDOM = document.createElement('div');
titleDOM.classList.add('stacked-title');
labelDOM.appendChild(titleDOM);
let valueDOM = document.createElement('div');
valueDOM.classList.add('stacked-value');
labelDOM.appendChild(valueDOM);
stackedGraph.dom.addEventListener('stacked-mousedown', event => {
document.body.appendChild(labelDOM);
indicator.style.background = stackedGraph.getColor(event.stackedIndex);
});
stackedGraph.dom.addEventListener('stacked-mousemove', event => {
labelDOM.style.left = event.clientX + 10 + 'px';
labelDOM.style.top = event.clientY + 10 + 'px';
titleDOM.innerHTML = dataTitles[event.stackedIndex];
valueDOM.innerHTML = event.stackedData.toFixed(2);
});
document.addEventListener('mouseup', event => {
try { document.body.removeChild(labelDOM); } catch(error) {}
});
// stackedGraph.dom.addEventListener('stacked-mouseout', event => {
// try { document.body.removeChild(labelDOM); } catch(error) {}
// });
});
// Update the data randomly
function updateData(changeDimension) {
let dataLength = changeDimension? Math.ceil(Math.random() * 5) + 1 : 3;
let dataDimension = changeDimension? Math.ceil(Math.random() * 100) + 10 : 50;
let data = new Array(dataLength).fill(null).map(arr => new Array(dataDimension).fill(null).map(val => Math.random() * 2 - 1));
data.forEach(arr => {
arr.forEach((val, i, arr) => arr[i] = val + (arr[i - 1] || val) + (arr[i + 1] || val));
arr.forEach((val, i, arr) => arr[i] = Math.abs(val));
});
stackedGraph.data = data;
}
</script>
</body>
</html>