-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdata-table.js
341 lines (291 loc) · 10.6 KB
/
data-table.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
## Data Table Widget
Includes: [Base Mixin](#base-mixin)
The data table is a simple widget designed to list crossfilter focused data set (rows being
filtered) in a good old tabular fashion.
Examples:
* [Nasdaq 100 Index](http://dc-js.github.com/dc.js/)
#### dc.dataTable(parent[, chartGroup])
Create a data table widget instance and attach it to the given parent element.
Parameters:
* parent : string | node | selection - any valid
[d3 single selector](https://github.com/mbostock/d3/wiki/Selections#selecting-elements) specifying
a dom block element such as a div; or a dom element or d3 selection.
* chartGroup : string (optional) - name of the chart group this chart instance should be placed in.
Interaction with a chart will only trigger events and redraws within the chart's group.
Returns:
A newly created data table widget instance
**/
dc.dataTable = function (parent, chartGroup) {
var LABEL_CSS_CLASS = 'dc-table-label';
var ROW_CSS_CLASS = 'dc-table-row';
var COLUMN_CSS_CLASS = 'dc-table-column';
var GROUP_CSS_CLASS = 'dc-table-group';
var HEAD_CSS_CLASS = 'dc-table-head';
var _chart = dc.baseMixin({});
var _size = 25;
var _columns = [];
var _sortBy = function (d) {
return d;
};
var _order = d3.ascending;
_chart._doRender = function () {
_chart.selectAll('tbody').remove();
renderRows(renderGroups());
return _chart;
};
_chart._doColumnValueFormat = function (v, d) {
return ((typeof v === 'function') ?
v(d) : // v as function
((typeof v === 'string') ?
d[v] : // v is field name string
v.format(d) // v is Object, use fn (element 2)
)
);
};
_chart._doColumnHeaderFormat = function (d) {
// if 'function', convert to string representation
// show a string capitalized
// if an object then display it's label string as-is.
return (typeof d === 'function') ?
_chart._doColumnHeaderFnToString(d) :
((typeof d === 'string') ?
_chart._doColumnHeaderCapitalize(d) : String(d.label));
};
_chart._doColumnHeaderCapitalize = function (s) {
// capitalize
return s.charAt(0).toUpperCase() + s.slice(1);
};
_chart._doColumnHeaderFnToString = function (f) {
// columnString(f) {
var s = String(f);
var i1 = s.indexOf('return ');
if (i1 >= 0) {
var i2 = s.lastIndexOf(';');
if (i2 >= 0) {
s = s.substring(i1 + 7, i2);
var i3 = s.indexOf('numberFormat');
if (i3 >= 0) {
s = s.replace('numberFormat', '');
}
}
}
return s;
};
function renderGroups() {
// The 'original' example uses all 'functions'.
// If all 'functions' are used, then don't remove/add a header, and leave
// the html alone. This preserves the functionality of earlier releases.
// A 2nd option is a string representing a field in the data.
// A third option is to supply an Object such as an array of 'information', and
// supply your own _doColumnHeaderFormat and _doColumnValueFormat functions to
// create what you need.
var bAllFunctions = true;
_columns.forEach(function (f) {
bAllFunctions = bAllFunctions & (typeof f === 'function');
});
if (!bAllFunctions) {
_chart.selectAll('th').remove();
var headcols = _chart.root().selectAll('th')
.data(_columns);
var headGroup = headcols
.enter()
.append('th');
headGroup
.attr('class', HEAD_CSS_CLASS)
.html(function (d) {
return (_chart._doColumnHeaderFormat(d));
});
}
var groups = _chart.root().selectAll('tbody')
.data(nestEntries(), function (d) {
return _chart.keyAccessor()(d);
});
var rowGroup = groups
.enter()
.append('tbody');
rowGroup
.append('tr')
.attr('class', GROUP_CSS_CLASS)
.append('td')
.attr('class', LABEL_CSS_CLASS)
.attr('colspan', _columns.length)
.html(function (d) {
return _chart.keyAccessor()(d);
});
groups.exit().remove();
return rowGroup;
}
function nestEntries() {
var entries = _chart.dimension().top(_size);
return d3.nest()
.key(_chart.group())
.sortKeys(_order)
.entries(entries.sort(function (a, b) {
return _order(_sortBy(a), _sortBy(b));
}));
}
function renderRows(groups) {
var rows = groups.order()
.selectAll('tr.' + ROW_CSS_CLASS)
.data(function (d) {
return d.values;
});
var rowEnter = rows.enter()
.append('tr')
.attr("class", function(d) {
var baseClasses = ROW_CSS_CLASS;
if (isSelected(d)) baseClasses += " selected";
if (isDeselected(d)) baseClasses += " deselected";
return baseClasses;
});
_columns.forEach(function (v, i) {
rowEnter.append('td')
.attr('class', COLUMN_CSS_CLASS + ' _' + i)
.html(function (d) {
return _chart._doColumnValueFormat(v, d);
});
});
rows.exit().remove();
return rows;
}
_chart._doRedraw = function () {
return _chart._doRender();
};
function isSelected(d) {
return _chart.hasFilter() && _chart.hasFilter(_chart.keyAccessor()(d));
}
function isDeselected(d) {
return _chart.hasFilter() && !_chart.hasFilter(_chart.keyAccessor()(d));
}
/**
#### .size([size])
Get or set the table size which determines the number of rows displayed by the widget.
**/
_chart.size = function (s) {
if (!arguments.length) {
return _size;
}
_size = s;
return _chart;
};
/**
#### .columns([columnFunctionArray])
Get or set column functions. The data table widget now supports several methods of specifying
the columns to display. The original method, first shown below, uses an array of functions to
generate dynamic columns. Column functions are simple javascript functions with only one input
argument `d` which represents a row in the data set. The return value of these functions will be
used directly to generate table content for each cell. However, this method requires the .html
table entry to have a fixed set of column headers.
```js
chart.columns([
function(d) {
return d.date;
},
function(d) {
return d.open;
},
function(d) {
return d.close;
},
function(d) {
return numberFormat(d.close - d.open);
},
function(d) {
return d.volume;
}
]);
```
The next example shows you can simply list the data (d) content directly without
specifying it as a function, except where necessary (ie, computed columns). Note
the data element accessor name is capitalized when displayed in the table. You can
also mix in functions as desired or necessary, but you must use the
Object = [Label, Fn] method as shown below.
You may wish to override the following two functions, which are internally used to
translate the column information or function into a displayed header. The first one
is used on the simple "string" column specifier, the second is used to transform the
String(fn) into something displayable. For the Stock example, the function for Change
becomes a header of 'd.close - d.open'.
_chart._doColumnHeaderCapitalize _chart._doColumnHeaderFnToString
You may use your own Object definition, however you must then override
_chart._doColumnHeaderFormat , _chart._doColumnValueFormat
Be aware that fields without numberFormat specification will be displayed just as
they are stored in the data, unformatted.
```js
chart.columns([
"date", // d["date"], ie, a field accessor; capitalized automatically
"open", // ...
"close", // ...
["Change", // Specify an Object = [Label, Fn]
function (d) {
return numberFormat(d.close - d.open);
}],
"volume" // d["volume"], ie, a field accessor; capitalized automatically
]);
```
A third example, where all fields are specified using the Object = [Label, Fn] method.
```js
chart.columns([
["Date", // Specify an Object = [Label, Fn]
function (d) {
return d.date;
}],
["Open",
function (d) {
return numberFormat(d.open);
}],
["Close",
function (d) {
return numberFormat(d.close);
}],
["Change",
function (d) {
return numberFormat(d.close - d.open);
}],
["Volume",
function (d) {
return d.volume;
}]
]);
```
**/
_chart.columns = function (_) {
if (!arguments.length) {
return _columns;
}
_columns = _;
return _chart;
};
/**
#### .sortBy([sortByFunction])
Get or set sort-by function. This function works as a value accessor at row level and returns a
particular field to be sorted by. Default value: identity function
```js
chart.sortBy(function(d) {
return d.date;
});
```
**/
_chart.sortBy = function (_) {
if (!arguments.length) {
return _sortBy;
}
_sortBy = _;
return _chart;
};
/**
#### .order([order])
Get or set sort order. Default value: ``` d3.ascending ```
```js
chart.order(d3.descending);
```
**/
_chart.order = function (_) {
if (!arguments.length) {
return _order;
}
_order = _;
return _chart;
};
return _chart.anchor(parent, chartGroup);
};