2012-05-26 17:26:47 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<meta charset="utf-8">
|
2012-07-13 19:25:19 +00:00
|
|
|
<link href="../src/nv.d3.css" rel="stylesheet" type="text/css">
|
2012-05-26 17:26:47 +00:00
|
|
|
<style>
|
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
svg.sparkline {
|
2012-08-21 23:44:50 +00:00
|
|
|
width: 500px;
|
2013-08-25 17:07:54 +00:00
|
|
|
height: 70px;
|
2012-11-07 18:49:04 +00:00
|
|
|
font-size: 14px;
|
|
|
|
margin-top: -6px;
|
|
|
|
}
|
|
|
|
|
2012-05-26 17:26:47 +00:00
|
|
|
|
|
|
|
</style>
|
2013-08-14 16:00:15 +00:00
|
|
|
<body class='with-3d-shadow with-transitions'>
|
2012-05-26 17:26:47 +00:00
|
|
|
|
2012-10-19 21:58:54 +00:00
|
|
|
<h2>SparklinePlus:</h2>
|
|
|
|
<p><svg id="chart1" class="sparkline"></svg></p>
|
2013-08-25 17:07:54 +00:00
|
|
|
<br/>
|
|
|
|
<p>APPL:<svg id="chart2" class="sparkline"></svg></p>
|
|
|
|
|
|
|
|
<p>GOOG:<svg id="chart3" class="sparkline"></svg></p>
|
2012-05-26 17:26:47 +00:00
|
|
|
|
2013-04-16 22:54:46 +00:00
|
|
|
<script src="../lib/d3.v3.js"></script>
|
2012-05-26 17:26:47 +00:00
|
|
|
<script src="../nv.d3.js"></script>
|
2012-07-26 12:23:20 +00:00
|
|
|
<script src="../src/utils.js"></script>
|
2012-05-26 17:26:47 +00:00
|
|
|
<script src="../src/models/sparkline.js"></script>
|
|
|
|
<script src="../src/models/sparklinePlus.js"></script>
|
|
|
|
<script>
|
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
function defaultChartConfig(containerId, data) {
|
|
|
|
nv.addGraph(function() {
|
2012-10-20 05:39:49 +00:00
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
var chart = nv.models.sparklinePlus()
|
2012-10-20 05:39:49 +00:00
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
chart
|
|
|
|
.margin({left:70})
|
|
|
|
.x(function(d,i) { return i })
|
|
|
|
.xTickFormat(function(d) {
|
|
|
|
return d3.time.format('%x')(new Date(data[d].x))
|
|
|
|
})
|
2012-10-20 05:39:49 +00:00
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
d3.select(containerId)
|
|
|
|
.datum(data)
|
|
|
|
.transition().duration(250)
|
|
|
|
.call(chart);
|
2012-10-20 05:39:49 +00:00
|
|
|
|
2012-05-26 17:26:47 +00:00
|
|
|
|
2013-08-25 17:07:54 +00:00
|
|
|
return chart;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
defaultChartConfig("#chart1",sine());
|
|
|
|
defaultChartConfig("#chart2", volatileChart(130.0, 0.02));
|
2013-08-25 21:02:30 +00:00
|
|
|
defaultChartConfig("#chart3", volatileChart(25.0, 0.09,30));
|
2012-05-26 17:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
function sine() {
|
|
|
|
var sin = [];
|
2013-08-25 17:07:54 +00:00
|
|
|
var now =+new Date();
|
2012-10-20 05:39:49 +00:00
|
|
|
|
2012-05-26 17:26:47 +00:00
|
|
|
for (var i = 0; i < 100; i++) {
|
2012-10-20 05:39:49 +00:00
|
|
|
sin.push({x: now + i * 1000 * 60 * 60 * 24, y: Math.sin(i/10)});
|
2012-05-26 17:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sin;
|
|
|
|
}
|
|
|
|
|
2013-08-25 21:02:30 +00:00
|
|
|
function volatileChart(startPrice, volatility, numPoints) {
|
2013-08-25 17:07:54 +00:00
|
|
|
var rval = [];
|
|
|
|
var now =+new Date();
|
2013-08-25 21:02:30 +00:00
|
|
|
numPoints = numPoints || 100;
|
|
|
|
for(var i = 1; i < numPoints; i++) {
|
2013-08-25 17:07:54 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2012-05-26 17:26:47 +00:00
|
|
|
|
|
|
|
</script>
|