Fixed legend height and margin.top issue in the ready to go charts. Also re organized code a little bit for consistency
parent
2cb05b38d0
commit
542dc094f9
@ -0,0 +1,83 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
|
||||||
|
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
||||||
|
|
||||||
|
<style>
|
||||||
|
|
||||||
|
body {
|
||||||
|
overflow-y:scroll;
|
||||||
|
}
|
||||||
|
|
||||||
|
text {
|
||||||
|
font: 12px sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
#chart1 {
|
||||||
|
height: 500px;
|
||||||
|
margin: 10px;
|
||||||
|
min-width: 100px;
|
||||||
|
min-height: 100px;
|
||||||
|
/*
|
||||||
|
Minimum height and width is a good idea to prevent negative SVG dimensions...
|
||||||
|
For example width should be =< margin.left + margin.right + 1,
|
||||||
|
of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div id="chart1">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="../lib/d3.v2.js"></script>
|
||||||
|
<script src="../nv.d3.js"></script>
|
||||||
|
<!-- including all the components so I don't have to minify every time I test in development -->
|
||||||
|
<script src="../src/tooltip.js"></script>
|
||||||
|
<script src="../src/models/legend.js"></script>
|
||||||
|
<script src="../src/models/axis.js"></script>
|
||||||
|
<script src="../src/models/line.js"></script>
|
||||||
|
<script src="../src/models/lineWithLegend.js"></script>
|
||||||
|
<script src="../src/charts/lineChart.js"></script>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
nv.charts.line()
|
||||||
|
.selector('#chart1')
|
||||||
|
.data(sinAndCos())
|
||||||
|
.y(function(d) { return d.voltage })
|
||||||
|
.yAxisLabel('Voltage (v)')
|
||||||
|
.build();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function sinAndCos() {
|
||||||
|
var sin = [],
|
||||||
|
cos = [],
|
||||||
|
r1 = Math.random(),
|
||||||
|
r2 = Math.random();
|
||||||
|
|
||||||
|
for (var i = 0; i < 100; i++) {
|
||||||
|
sin.push({x: i, voltage: r2 * Math.sin(i/10)});
|
||||||
|
cos.push({x: i, voltage: r1 * Math.cos(i/10)});
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
values: sin,
|
||||||
|
key: "Sine Wave",
|
||||||
|
color: "#ff7f0e"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
values: cos,
|
||||||
|
key: "Cosine Wave",
|
||||||
|
color: "#2ca02c"
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
</script>
|
@ -1,100 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<meta charset="utf-8">
|
|
||||||
|
|
||||||
<link href="../src/d3.css" rel="stylesheet" type="text/css">
|
|
||||||
|
|
||||||
<style>
|
|
||||||
|
|
||||||
body {
|
|
||||||
overflow-y:scroll;
|
|
||||||
}
|
|
||||||
|
|
||||||
text {
|
|
||||||
font: 12px sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
svg {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
#chart1 svg {
|
|
||||||
height: 500px;
|
|
||||||
min-width: 100px;
|
|
||||||
min-height: 100px;
|
|
||||||
/*
|
|
||||||
margin: 50px;
|
|
||||||
Minimum height and width is a good idea to prevent negative SVG dimensions...
|
|
||||||
For example width should be =< margin.left + margin.right + 1,
|
|
||||||
of course 1 pixel for the entire chart would not be very useful, BUT should not have errors
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
</style>
|
|
||||||
<body>
|
|
||||||
|
|
||||||
<div id="chart1">
|
|
||||||
<svg style="height: 500px;"></svg>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script src="../lib/d3.v2.js"></script>
|
|
||||||
<script src="../nv.d3.js"></script>
|
|
||||||
<script src="../src/tooltip.js"></script>
|
|
||||||
<script src="../src/utils.js"></script>
|
|
||||||
<script src="../src/models/legend.js"></script>
|
|
||||||
<script src="../src/models/axis.js"></script>
|
|
||||||
<script src="../src/models/scatter.js"></script>
|
|
||||||
<script src="../src/models/line.js"></script>
|
|
||||||
<script src="../src/models/lineChart.js"></script>
|
|
||||||
<script>
|
|
||||||
|
|
||||||
|
|
||||||
// Wrapping in nv.addGraph allows for '0 timeout render', stors rendered charts in nv.graphs, and may do more in the future... it's NOT required
|
|
||||||
nv.addGraph(function() {
|
|
||||||
var chart = nv.models.lineChart();
|
|
||||||
|
|
||||||
chart.xAxis // chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the partent chart, so need to chain separately
|
|
||||||
.tickFormat(d3.format(',r'));
|
|
||||||
|
|
||||||
chart.yAxis
|
|
||||||
.axisLabel('Voltage (v)')
|
|
||||||
.tickFormat(d3.format(',.2f'));
|
|
||||||
|
|
||||||
d3.select('#chart1 svg')
|
|
||||||
.datum(sinAndCos())
|
|
||||||
.transition().duration(500)
|
|
||||||
.call(chart);
|
|
||||||
|
|
||||||
//TODO: Figure out a good way to do this automatically
|
|
||||||
nv.utils.windowResize(chart.update);
|
|
||||||
//nv.utils.windowResize(function() { d3.select('#chart1 svg').call(chart) });
|
|
||||||
|
|
||||||
return chart;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function sinAndCos() {
|
|
||||||
var sin = [],
|
|
||||||
cos = [];
|
|
||||||
|
|
||||||
for (var i = 0; i < 100; i++) {
|
|
||||||
sin.push({x: i, y: Math.sin(i/10)});
|
|
||||||
cos.push({x: i, y: .5 * Math.cos(i/10)});
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
values: sin,
|
|
||||||
key: "Sine Wave",
|
|
||||||
color: "#ff7f0e"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
values: cos,
|
|
||||||
key: "Cosine Wave",
|
|
||||||
color: "#2ca02c"
|
|
||||||
}
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
</script>
|
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue