Skip to content

Commit bb8c0e1

Browse files
provide a new "section" property to replace dataTable.group
fixes #855 `group` is still supported as a synonym likewise with `showSections` / `showGroups` use the old class name but also supply a new one adapt all examples to use the new name and/or suppress sections headers when they don't help anything
1 parent ede8166 commit bb8c0e1

10 files changed

+74
-52
lines changed

src/data-table.js

+65-34
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
* The data table is a simple widget designed to list crossfilter focused data set (rows being
33
* filtered) in a good old tabular fashion.
44
*
5-
* Note: Unlike other charts, the data table (and data grid chart) use the {@link dc.dataTable#group group} attribute as a
6-
* keying function for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data
7-
* together in groups. Do not pass in a crossfilter group as this will not work.
8-
*
9-
* Another interesting feature of the data table is that you can pass a crossfilter group to the `dimension`, as
5+
* An interesting feature of the data table is that you can pass a crossfilter group to the `dimension`, as
106
* long as you specify the {@link dc.dataTable#order order} as `d3.descending`, since the data
117
* table will use `dimension.top()` to fetch the data in that case, and the method is equally
128
* supported on the crossfilter group as the crossfilter dimension.
139
*
10+
* Note: Formerly the data table (and data grid chart) used the {@link dc.dataTable#group group} attribute as a
11+
* keying function for {@link https://github.com/d3/d3-collection/blob/master/README.md#nest nesting} the data
12+
* together in sections. This was confusing so it has been renamed to `section`, although `group` still works.
13+
*
1414
* Examples:
1515
* - {@link http://dc-js.github.com/dc.js/ Nasdaq 100 Index}
1616
* - {@link http://dc-js.github.io/dc.js/examples/table-on-aggregated-data.html dataTable on a crossfilter group}
@@ -29,7 +29,7 @@ dc.dataTable = function (parent, chartGroup) {
2929
var LABEL_CSS_CLASS = 'dc-table-label';
3030
var ROW_CSS_CLASS = 'dc-table-row';
3131
var COLUMN_CSS_CLASS = 'dc-table-column';
32-
var GROUP_CSS_CLASS = 'dc-table-group';
32+
var SECTION_CSS_CLASS = 'dc-table-section dc-table-group';
3333
var HEAD_CSS_CLASS = 'dc-table-head';
3434

3535
var _chart = dc.baseMixin({});
@@ -42,12 +42,15 @@ dc.dataTable = function (parent, chartGroup) {
4242
var _order = d3.ascending;
4343
var _beginSlice = 0;
4444
var _endSlice;
45-
var _showGroups = true;
45+
var _showSections = true;
46+
var _section = function() { return ''; }; // all in one section
47+
48+
_chart._mandatoryAttributes(['dimension']);
4649

4750
_chart._doRender = function () {
4851
_chart.selectAll('tbody').remove();
4952

50-
renderRows(renderGroups());
53+
renderRows(renderSections());
5154

5255
return _chart;
5356
};
@@ -94,7 +97,7 @@ dc.dataTable = function (parent, chartGroup) {
9497
return s;
9598
};
9699

97-
function renderGroups () {
100+
function renderSections () {
98101
// The 'original' example uses all 'functions'.
99102
// If all 'functions' are used, then don't remove/add a header, and leave
100103
// the html alone. This preserves the functionality of earlier releases.
@@ -134,19 +137,19 @@ dc.dataTable = function (parent, chartGroup) {
134137
});
135138
}
136139

137-
var groups = _chart.root().selectAll('tbody')
140+
var sections = _chart.root().selectAll('tbody')
138141
.data(nestEntries(), function (d) {
139142
return _chart.keyAccessor()(d);
140143
});
141144

142-
var rowGroup = groups
145+
var rowSection = sections
143146
.enter()
144147
.append('tbody');
145148

146-
if (_showGroups === true) {
147-
rowGroup
149+
if (_showSections === true) {
150+
rowSection
148151
.append('tr')
149-
.attr('class', GROUP_CSS_CLASS)
152+
.attr('class', SECTION_CSS_CLASS)
150153
.append('td')
151154
.attr('class', LABEL_CSS_CLASS)
152155
.attr('colspan', _columns.length)
@@ -155,9 +158,9 @@ dc.dataTable = function (parent, chartGroup) {
155158
});
156159
}
157160

158-
groups.exit().remove();
161+
sections.exit().remove();
159162

160-
return rowGroup;
163+
return rowSection;
161164
}
162165

163166
function nestEntries () {
@@ -169,15 +172,15 @@ dc.dataTable = function (parent, chartGroup) {
169172
}
170173

171174
return d3.nest()
172-
.key(_chart.group())
175+
.key(_chart.section())
173176
.sortKeys(_order)
174177
.entries(entries.sort(function (a, b) {
175178
return _order(_sortBy(a), _sortBy(b));
176179
}).slice(_beginSlice, _endSlice));
177180
}
178181

179-
function renderRows (groups) {
180-
var rows = groups.order()
182+
function renderRows (sections) {
183+
var rows = sections.order()
181184
.selectAll('tr.' + ROW_CSS_CLASS)
182185
.data(function (d) {
183186
return d.values;
@@ -205,21 +208,41 @@ dc.dataTable = function (parent, chartGroup) {
205208
};
206209

207210
/**
208-
* Get or set the group function for the data table. The group function takes a data row and
211+
* Get or set the section function for the data table. The section function takes a data row and
209212
* returns the key to specify to {@link https://github.com/d3/d3-collection/blob/master/README.md#nest d3.nest}
210-
* to split rows into groups.
213+
* to split rows into sections. By default there will be only one section with no name.
211214
*
212-
* Do not pass in a crossfilter group as this will not work.
213-
* @method group
215+
* Set {@link dc.dataTable#showSections showSections} to false to hide the section headers
216+
*
217+
* @method section
214218
* @memberof dc.dataTable
215219
* @instance
216220
* @example
217-
* // group rows by the value of their field
221+
* // section rows by the value of their field
218222
* chart
219-
* .group(function(d) { return d.field; })
223+
* .section(function(d) { return d.field; })
224+
* @param {Function} sectionFunction Function taking a row of data and returning the nest key.
225+
* @returns {Function|dc.dataTable}
226+
*/
227+
_chart.section = function (section) {
228+
if (!arguments.length) {
229+
return _section;
230+
}
231+
_section = section;
232+
return _chart;
233+
};
234+
235+
/**
236+
* Backward-compatible synonym for {@link dc.dataTable#section section}.
237+
*
238+
* @method group
239+
* @memberof dc.dataTable
240+
* @instance
220241
* @param {Function} groupFunction Function taking a row of data and returning the nest key.
221242
* @returns {Function|dc.dataTable}
222243
*/
244+
_chart.group = dc.logger.annotate(_chart.section,
245+
'consider using dataTable.section instead of dataTable.group for clarity');
223246

224247
/**
225248
* Get or set the table size which determines the number of rows displayed by the widget.
@@ -406,25 +429,33 @@ dc.dataTable = function (parent, chartGroup) {
406429
};
407430

408431
/**
409-
* Get or set if group rows will be shown. The dataTable {@link dc.dataTable#group group}
410-
* function must be specified even if groups are not shown.
411-
* @method showGroups
432+
* Get or set if section header rows will be shown.
433+
* @method showSections
412434
* @memberof dc.dataTable
413435
* @instance
414436
* @example
415437
* chart
416-
* .group([value], [name])
417-
* .showGroups(true|false);
418-
* @param {Boolean} [showGroups=true]
438+
* .section([value], [name])
439+
* .showSections(true|false);
440+
* @param {Boolean} [showSections=true]
419441
* @returns {Boolean|dc.dataTable}
420442
*/
421-
_chart.showGroups = function (showGroups) {
443+
_chart.showSections = _chart.showGroups = function (showSections) {
422444
if (!arguments.length) {
423-
return _showGroups;
445+
return _showSections;
424446
}
425-
_showGroups = showGroups;
447+
_showSections = showSections;
426448
return _chart;
427449
};
428450

451+
/**
452+
* Backward-compatible synonym for {@link dc.dataTable#showSections showSections}.
453+
* @method showGroups
454+
* @memberof dc.dataTable
455+
* @instance
456+
* @param {Boolean} [showGroups=true]
457+
* @returns {Boolean|dc.dataTable}
458+
*/
459+
429460
return _chart.anchor(parent, chartGroup);
430461
};

web/examples/cbox-menu.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149

150150
datatable
151151
.dimension(letterDimension2)
152-
.group(function (d) {
152+
.section(function (d) {
153153
return d.letter;
154154
})
155155
.columns(['color', 'state'])

web/examples/composite-brush-multi-dim.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292
dataTable
9393
.dimension(dimX)
94-
.group(function (d) { return d.x; })
94+
.section(function (d) { return d.x; })
9595
.columns(['x', 'y', 'z'])
9696
.on('renderlet', function (table) {
9797
table.selectAll('.dc-table-group').classed('info', true);

web/examples/download-table.html

-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@
110110

111111
table
112112
.dimension(spendDim)
113-
.group(function(d) {
114-
return d.value;
115-
})
116113
.sortBy(function(d) { return +d.Spent; })
117114
.showGroups(false)
118115
.columns(['Name',

web/examples/select.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109

110110
datatable
111111
.dimension(letterDimension2)
112-
.group(function(d) { return d.letter; })
112+
.section(function(d) { return d.letter; })
113113
.columns(['color', 'state'])
114114
.size(data.length);
115115

web/examples/table-on-aggregated-data.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@
5959
},
6060
function () {
6161
return {number: 0, total: 0, avg: 0}
62-
}),
63-
rank = function (p) { return "rank" };
62+
});
6463

6564
chart
6665
.width(768)
6766
.height(480)
67+
.showSections(false)
6868
.dimension(groupedDimension)
69-
.group(rank)
7069
.columns([function (d) { return d.key },
7170
function (d) { return d.value.number },
7271
function (d) { return d.value.avg }])

web/examples/table-pagination.html

-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
var fmt = d3.format('02d');
4444
var runDimension = ndx.dimension(function(d) {return [fmt(+d.Expt),fmt(+d.Run)];}),
4545
experimentDimension = ndx.dimension(function(d) {return d.Expt;}),
46-
grouping = function (d) {return d.Expt;},
4746
experimentGroup = experimentDimension.group().reduceCount();
4847

4948
piechart.width(300).height(280)
@@ -56,7 +55,6 @@
5655
.width(300)
5756
.height(480)
5857
.dimension(runDimension)
59-
.group(grouping)
6058
.size(Infinity)
6159
.showGroups(false)
6260
.columns(['Expt', 'Run', 'Speed'])

web/examples/text-filter-widget.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@
5656
var i = 0;
5757
dc.dataTable('.dc-data-grid')
5858
.dimension(dimension)
59-
.group(function (d) {
60-
return 'Jazz';
61-
})
59+
.showSections(false)
6260
.columns([
6361
function (d) {
6462
i = i + 1;

web/play-ground.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@
915915

916916
dc.dataTable(".dc-data-table")
917917
.dimension(dateDimension)
918-
.group(function (d) {
918+
.section(function (d) {
919919
return dateFormat(d3.timeDay(d.dd));
920920
})
921921
.size(3)

web/stock.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,8 @@ d3.csv('ndx.csv').then(function (data) {
522522

523523
nasdaqTable /* dc.dataTable('.dc-data-table', 'chartGroup') */
524524
.dimension(dateDimension)
525-
// Data table does not use crossfilter group but rather a closure
526-
// as a grouping function
527-
.group(function (d) {
525+
// Specify a section function to nest rows of the table
526+
.section(function (d) {
528527
var format = d3.format('02d');
529528
return d.dd.getFullYear() + '/' + format((d.dd.getMonth() + 1));
530529
})

0 commit comments

Comments
 (0)