forked from novus/nvd3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlineChart.html
126 lines (110 loc) · 3.12 KB
/
lineChart.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="../build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
<script src="../build/nv.d3.js"></script>
<style>
text {
font: 12px sans-serif;
}
svg {
display: block;
}
html,
body,
#chart1,
svg {
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
}
.dashed {
stroke-dasharray: 5, 5;
}
</style>
</head>
<body class='with-3d-shadow with-transitions'>
<div id="chart1">
</div>
<script>
// Wrapping in nv.addGraph allows for '0 timeout render', stores rendered charts in nv.graphs, and may do more in the future... it's NOT required
var chart;
var data;
var legendPosition = "top",
events = {
11: "Event 1: some loream ipsum description",
40: "Event 2: some other loream ipsum description",
75: "Event 3: some more event loream ipsum description"
};
nv.addGraph(function() {
chart = nv.models.lineChart()
.options({
duration: 300,
useInteractiveGuideline: true
});
// chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately
chart.xAxis
.axisLabel("Time (s)")
.tickFormat(d3.format(',.1f'))
.staggerLabels(true);
chart.yAxis
.axisLabel('Voltage (v)')
.tickFormat(function(d) {
if (d == null) {
return 'N/A';
}
return d3.format(',.2f')(d);
});
data = sinAndCos();
d3.select('#chart1').append('svg')
.datum(data)
.call(chart);
nv.utils.windowResize(chart.update);
// addEvents feature added
nv.utils.addEvents('#chart1', events, chart);
return chart;
});
function sinAndCos() {
var sin = [],
sin2 = [],
cos = [];
for (var i = 0; i < 100; i++) {
sin.push({
x: i,
y: i % 10 == 5 ? null : Math.sin(i / 10)
}); //the nulls are to show how defined works
sin2.push({
x: i,
y: Math.sin(i / 5) * 0.4 - 0.25
});
cos.push({
x: i,
y: .5 * Math.cos(i / 10)
});
}
return [{
area: true,
values: sin,
key: "Sine Wave",
color: "#ff7f0e",
strokeWidth: 4,
classed: 'dashed'
}, {
values: cos,
key: "Cosine Wave",
color: "#2ca02c"
}, {
area: true,
values: sin2,
key: "Fill opacity",
color: "#EF9CFB",
fillOpacity: .1
}];
}
</script>
</body>
</html>