Skip to content

Commit 2e5baa5

Browse files
controlsUseVisibility
fixes #888 changing pie chart spec to use the new functionality; bar chart spec explicitly uses the old functionality
1 parent ae7e5fa commit 2e5baa5

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

spec/bar-chart-spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('dc.barChart', function() {
1414
chart.dimension(dimension).group(group)
1515
.width(1100).height(200)
1616
.x(d3.time.scale.utc().domain([makeDate(2012, 0, 1), makeDate(2012, 11, 31)]))
17-
.transitionDuration(0);
17+
.transitionDuration(0)
18+
.controlsUseVisibility(false);
1819
});
1920

2021
describe('rendering', function () {

spec/pie-chart-spec.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('dc.pieChart', function() {
5151

5252
function buildChart(id) {
5353
var div = appendChartID(id);
54-
div.append("a").attr("class", "reset").style("display", "none");
55-
div.append("span").attr("class", "filter").style("display", "none");
54+
div.append("a").attr("class", "reset").style("visibility", "hidden");
55+
div.append("span").attr("class", "filter").style("visibility", "hidden");
5656
var chart = dc.pieChart("#" + id);
5757
chart.dimension(valueDimension).group(valueGroup)
5858
.width(width)
@@ -163,13 +163,13 @@ describe('dc.pieChart', function() {
163163
});
164164
});
165165
it('reset link hidden after init rendering', function() {
166-
expect(chart.select("a.reset").style("display")).toEqual("none");
166+
expect(chart.select("a.reset").style("visibility")).toEqual("hidden");
167167
});
168168
it('filter printer should be set', function() {
169169
expect(chart.filterPrinter()).not.toBeNull();
170170
});
171171
it('filter info should be hidden after init rendering', function() {
172-
expect(chart.select("span.filter").style("display")).toEqual("none");
172+
expect(chart.select("span.filter").style("visibility")).toEqual("hidden");
173173
});
174174
describe('center positioning', function() {
175175
beforeEach(function() {
@@ -287,14 +287,14 @@ describe('dc.pieChart', function() {
287287
});
288288
chart.filterAll();
289289
});
290-
it('reset link generated after slice selection', function() {
290+
it('reset link shown after slice selection', function() {
291291
chart.filter("66");
292-
expect(chart.select("a.reset").style("display")).not.toEqual('none');
292+
expect(chart.select("a.reset").style("visibility")).not.toEqual('hidden');
293293
});
294-
it('filter info generated after slice selection', function() {
294+
it('filter info shown after slice selection', function() {
295295
chart.filter(null);
296296
chart.filter("66");
297-
expect(chart.select("span.filter").style("display")).not.toEqual('none');
297+
expect(chart.select("span.filter").style("visibility")).not.toEqual('hidden');
298298
expect(chart.select("span.filter").text()).toEqual("66");
299299
});
300300
it('should remove highlight if no slice selected', function() {

src/base-mixin.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dc.baseMixin = function (_chart) {
4242
return _chart.keyAccessor()(d) + ': ' + _chart.valueAccessor()(d);
4343
};
4444
var _renderTitle = true;
45+
var _controlsUseVisibility = true;
4546

4647
var _transitionDuration = 750;
4748

@@ -399,6 +400,19 @@ dc.baseMixin = function (_chart) {
399400
return _chart;
400401
};
401402

403+
/**
404+
#### .controlsUseVisibility
405+
If set, use the `visibility` attribute instead of the `display` attribute, for less disruption
406+
to layout. Default: true.
407+
**/
408+
_chart.controlsUseVisibility = function (_) {
409+
if (!arguments.length) {
410+
return _controlsUseVisibility;
411+
}
412+
_controlsUseVisibility = _;
413+
return _chart;
414+
};
415+
402416
/**
403417
#### .turnOnControls() & .turnOffControls()
404418
Turn on/off optional control elements within the root element. dc currently supports the
@@ -411,19 +425,25 @@ dc.baseMixin = function (_chart) {
411425
content of this element is then replaced with the current filter value using the filter printer
412426
function. This type of element will be turned off automatically if the filter is cleared.
413427
428+
The method (display or visibility) for turning on/off the controls depends on the
429+
`controlsUseVisibility` flag.
430+
414431
**/
415432
_chart.turnOnControls = function () {
416433
if (_root) {
417-
_chart.selectAll('.reset').style('display', null);
418-
_chart.selectAll('.filter').text(_filterPrinter(_chart.filters())).style('display', null);
434+
var attribute = _chart.controlsUseVisibility() ? 'visibility' : 'display';
435+
_chart.selectAll('.reset').style(attribute, null);
436+
_chart.selectAll('.filter').text(_filterPrinter(_chart.filters())).style(attribute, null);
419437
}
420438
return _chart;
421439
};
422440

423441
_chart.turnOffControls = function () {
424442
if (_root) {
425-
_chart.selectAll('.reset').style('display', 'none');
426-
_chart.selectAll('.filter').style('display', 'none').text(_chart.filter());
443+
var attribute = _chart.controlsUseVisibility() ? 'visibility' : 'display';
444+
var value = _chart.controlsUseVisibility() ? 'hidden' : 'none';
445+
_chart.selectAll('.reset').style(attribute, value);
446+
_chart.selectAll('.filter').style(attribute, value).text(_chart.filter());
427447
}
428448
return _chart;
429449
};

web/stock.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,19 @@ var nasdaqTable = dc.dataTable('.dc-data-table');
3131
// will automatically hide/show it based on whether there is a filter
3232
// set on the chart (e.g. slice selection for pie chart and brush
3333
// selection for bar chart). Enable this with `chart.turnOnControls(true)`
34+
35+
// dc.js >=2.1 uses `visibility: hidden` to hide/show controls without
36+
// disrupting the layout. To return the old `display: none` behavior,
37+
// set `chart.controlsUseVisibility(false)` and use that style instead.
3438
<div id='chart'>
3539
<a class='reset'
3640
href='javascript:myChart.filterAll();dc.redrawAll();'
37-
style='display: none;'>reset</a>
41+
style='visibility: hidden;'>reset</a>
3842
</div>
3943
// dc.js will also automatically inject the current filter value into
4044
// any html element with its css class set to `filter`
4145
<div id='chart'>
42-
<span class='reset' style='display: none;'>
46+
<span class='reset' style='visibility: hidden;'>
4347
Current filter: <span class='filter'></span>
4448
</span>
4549
</div>

0 commit comments

Comments
 (0)