forked from danvk/dygraphs
/
independent-series.js
165 lines (159 loc) · 4.82 KB
/
independent-series.js
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
// Use this as a template for new Gallery entries.
Gallery.register(
'independent-series',
{
name: 'Independent Series',
title: 'Independent Series',
setup: function(parent) {
parent.innerHTML = [
"<p>By using the <i>connectSeparated</i> attribute, it's possible to display a chart of several time series with completely independent x-values.</p> ",
"",
"<p>The trick is to specify values for the series at the union of the x-values of all series. For one series' x values, specify <code>null</code> for each of the other series.</p> ",
"",
"<div id='graph' style='float: right; margin-right: 50px; width: 400px; height: 300px;'></div> ",
"<p>For example, say you had two series:</p> ",
"<table><tr> ",
"<td valign=top> ",
"<table> ",
" <table class='thinborder'> ",
" <tr><th>x</th><th>A</th></tr> ",
" <tr><td>2</td><td>2</td></tr> ",
" <tr><td>4</td><td>6</td></tr> ",
" <tr><td>6</td><td>4</td></tr> ",
" </table> ",
"</td> ",
"<td valign=top style='padding-left:25px;'> ",
" <table class='thinborder'> ",
" <tr><th>x</th><th>B</th></tr> ",
" <tr><td>1</td><td>3</td></tr> ",
" <tr><td>3</td><td>7</td></tr> ",
" <tr><td>5</td><td>5</td></tr> ",
" </table> ",
"</td> ",
"</tr></table> ",
"",
"<p>Then you would specify the following CSV or native data:</p> ",
"<table><tr> ",
"<td valign=top> ",
"(CSV) ",
"<pre id='csv1'></pre> ",
"</td> ",
"<td valign=top style='padding-left: 25px;'>",
"(native) ",
"<pre id='native1'></pre> ",
"</td> ",
"</tr></table> ",
"",
"<h3>Encoding a gap</h3>",
"<p>There's one extra wrinkle. What if one of the series has a missing ",
"value, i.e. what if your series are something like </p> ",
"",
"<table><tr> ",
"<td valign=top> ",
"<table> ",
" <table class='thinborder'> ",
" <tr><th>x</th><th>A</th></tr> ",
" <tr><td>2</td><td>2</td></tr> ",
" <tr><td>4</td><td>4</td></tr> ",
" <tr><td>6</td><td>(gap)</td></tr> ",
" <tr><td>8</td><td>8</td></tr> ",
" <tr><td>10</td><td>10</td></tr> ",
" </table> ",
"</td> ",
"<td valign=top style='padding-left:25px;'> ",
" <table class='thinborder'> ",
" <tr><th>x</th><th>B</th></tr> ",
" <tr><td>1</td><td>3</td></tr> ",
" <tr><td>3</td><td>5</td></tr> ",
" <tr><td>5</td><td>7</td></tr> ",
" </table> ",
"</td> ",
"</tr></table> ",
"",
"<div id='graph2' style='float: right; margin-right: 50px; width: 400px; height: 300px;'></div> ",
"<p>The gap would normally be encoded as a null, or missing value. But when you use <code>connectSeparatedPoints</code>, that has a special meaning. Instead, you have to use <code>NaN</code>. This is a bit of a hack, but it gets the job done.</p> ",
"",
"<table><tr> ",
"<td valign=top> ",
"(CSV) ",
"<pre id='csv2'></pre> ",
"</td> ",
"<td valign=top style='padding-left: 25px;'> ",
"(native) ",
"<pre id='native2'></pre> ",
"</td> ",
"</tr></table>"].join("\n");
},
run: function() {
document.getElementById("csv1").textContent =
"X,A,B\n" +
"1,,3\n" +
"2,2,\n" +
"3,,7\n" +
"4,6,\n" +
"5,,5\n" +
"6,4,";
document.getElementById("native1").textContent =
"[\n" +
" [1, null, 3],\n" +
" [2, 2, null],\n" +
" [3, null, 7],\n" +
" [4, 6, null],\n" +
" [5, null, 5],\n" +
" [6, 4, null]\n" +
"]";
document.getElementById("csv2").textContent =
"X,A,B\n" +
"1,,3\n" +
"2,2,\n" +
"3,,5\n" +
"4,4,\n" +
"6,Nan,\n" +
"8,8,\n" +
"10,10,";
document.getElementById("native2").textContent =
"[\n" +
" [1, null, 3],\n" +
" [2, 2, null],\n" +
" [3, null, 5],\n" +
" [4, 4, null],\n" +
" [5, null, 7],\n" +
" [6, NaN, null],\n" +
" [8, 8, null]\n" +
" [10, 10, null]\n" +
"]";
var g1 = new Dygraph(
document.getElementById('graph'),
[
[1, null, 3],
[2, 2, null],
[3, null, 7],
[4, 5, null],
[5, null, 5],
[6, 3, null]
],
{
labels: ['x', 'A', 'B' ],
connectSeparatedPoints: true,
drawPoints: true
}
);
g2 = new Dygraph(
document.getElementById('graph2'),
'x,A,B \n' +
'1,,3 \n' +
'2,2, \n' +
'3,,5 \n' +
'4,4, \n' +
'5,,7 \n' +
'6,NaN, \n' +
'8,8, \n' +
'10,10, \n'
, {
labels: ['x', 'A', 'B' ],
connectSeparatedPoints: true,
drawPoints: true
}
);
}
});