forked from novus/nvd3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparklinePlus.html
86 lines (66 loc) · 1.92 KB
/
sparklinePlus.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
<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">
<style>
svg.sparkline {
width: 500px;
height: 70px;
font-size: 14px;
margin-top: -6px;
}
</style>
<body class='with-3d-shadow with-transitions'>
<h2>SparklinePlus:</h2>
<p><svg id="chart1" class="sparkline"></svg></p>
<br/>
<p>APPL:<svg id="chart2" class="sparkline"></svg></p>
<p>GOOG:<svg id="chart3" class="sparkline"></svg></p>
<script src="../lib/d3.v3.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/utils.js"></script>
<script src="../src/models/sparkline.js"></script>
<script src="../src/models/sparklinePlus.js"></script>
<script>
function defaultChartConfig(containerId, data) {
nv.addGraph(function() {
var chart = nv.models.sparklinePlus()
chart
.margin({left:70})
.x(function(d,i) { return i })
.xTickFormat(function(d) {
return d3.time.format('%x')(new Date(data[d].x))
})
d3.select(containerId)
.datum(data)
.transition().duration(250)
.call(chart);
return chart;
});
}
defaultChartConfig("#chart1",sine());
defaultChartConfig("#chart2", volatileChart(130.0, 0.02));
defaultChartConfig("#chart3", volatileChart(25.0, 0.09,30));
function sine() {
var sin = [];
var now =+new Date();
for (var i = 0; i < 100; i++) {
sin.push({x: now + i * 1000 * 60 * 60 * 24, y: Math.sin(i/10)});
}
return sin;
}
function volatileChart(startPrice, volatility, numPoints) {
var rval = [];
var now =+new Date();
numPoints = numPoints || 100;
for(var i = 1; i < numPoints; i++) {
rval.push({x: now + i * 1000 * 60 * 60 * 24, y: startPrice});
var rnd = Math.random();
var changePct = 2 * volatility * rnd;
if ( changePct > volatility) {
changePct -= (2*volatility);
}
startPrice = startPrice + startPrice * changePct;
}
return rval;
}
</script>