forked from BigFatDog/cubism-es
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrandom.html
200 lines (170 loc) · 5.75 KB
/
random.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
margin: 30px auto;
width: 1280px;
position: relative;
}
header {
padding: 6px 0;
}
.group {
margin-bottom: 1em;
}
.axis {
font: 10px sans-serif;
position: fixed;
pointer-events: none;
z-index: 2;
}
.axis text {
-webkit-transition: fill-opacity 250ms linear;
}
.axis path {
display: none;
}
.axis line {
stroke: #000;
shape-rendering: crispEdges;
}
.axis.top {
background-image: linear-gradient(top, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -o-linear-gradient(top, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -moz-linear-gradient(top, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -webkit-linear-gradient(top, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -ms-linear-gradient(top, #fff 0%, rgba(255,255,255,0) 100%);
top: 0px;
padding: 0 0 24px 0;
}
.axis.bottom {
background-image: linear-gradient(bottom, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -o-linear-gradient(bottom, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -moz-linear-gradient(bottom, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -webkit-linear-gradient(bottom, #fff 0%, rgba(255,255,255,0) 100%);
background-image: -ms-linear-gradient(bottom, #fff 0%, rgba(255,255,255,0) 100%);
bottom: 0px;
padding: 24px 0 0 0;
}
.horizon {
border-bottom: solid 1px #000;
overflow: hidden;
position: relative;
}
.horizon {
border-top: solid 1px #000;
border-bottom: solid 1px #000;
}
.horizon + .horizon {
border-top: none;
}
.horizon canvas {
display: block;
}
.horizon .title,
.horizon .value {
bottom: 0;
line-height: 30px;
margin: 0 6px;
position: absolute;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
white-space: nowrap;
}
.horizon .title {
left: 0;
}
.horizon .value {
right: 0;
}
.zoom{
z-index: 2;
}
.line {
background: #000;
z-index: 2;
}
</style>
<script src="lib/d3.v7.min.js" charset="utf-8"></script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script>-->
<script src="./cubism-es.standalone.js"></script>
<body id="demo">
<script>
var context = cubism.context()
.step(1e4)
.size(1280);
d3.select("#demo").selectAll(".axis")
.data(["top", "bottom"])
.enter().append("div")
.attr("class", function(d) { return d + " axis"; })
.each(function(d) { context.axis().ticks(12).orient(d).render(d3.select(this)); });
const r = d3.select("body").append("div")
.attr("class", "rule")
.attr('id', 'rule');
context.rule().render(r);
const z = d3.select("body").append("div")
.attr("class", "zoom");
context.zoom(function(start, end) {
console.log(`Doing a zoom from point ${start} to point ${end}`);
context.zoom().zoomTime(start, end);
}).render(z);
const h = d3.select("body").selectAll(".horizon")
.data(d3.range(1, 50).map(random))
.enter().insert("div", ".bottom")
.attr("class", "horizon");
context.horizon()
.extent([-10, 10])
.render(h);
context.on("focus", function(i) {
d3.selectAll(".value").style("right", i == null ? null : context.size() - i + "px");
});
context.setCSSClass('primary', 'abc');
var newName = context.getCSSClass('primary');
console.log("After setting className for 'primary'", newName);
// Replace this with context.graphite and graphite.metric!
function random(x) {
var value = 0,
values = [],
i = 0,
last, last_width = 0,
last_stop = null, last_step =0;
var metric_name = x;
return context.metric(function(start, stop, step, callback) {
start = +start, stop = +stop;
if (isNaN(last)) last = start;
while (last < stop) {
last += step;
value = Math.max(-10, Math.min(10, value + .8 * Math.random() - .4 + .2 * Math.cos(i += x * .02)));
values.push(value);
}
if (last_step == 0) {
last_width = (stop - start) / step;
}
if (last_step == 0 || last_step == step) {
// first argument is the error, null indicates no error
callback(null, values.slice((start - stop) / step));
} else {
// calculate last_start relative to last stop, store witdth
last_start = last_stop - (last_step * last_width);
// get the last width / nb_points elements
var tmp = values.slice(-last_width);
var offset_start = (start - last_start) / last_step
var cur = start - step;
var offset = offset_start;
var old_cur = start;
var new_values = []
while (cur < stop) {
while((cur = cur + step ) < old_cur && cur <= stop) new_values.push(tmp[offset - 1])
if (cur < stop) {
new_values.push(tmp[offset])
old_cur = old_cur + last_step;
offset++;
}
}
callback(null, new_values);
}
last_stop = stop;
last_step = step;
}, metric_name);
}
</script>