-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathfocus-ordinal-bar.html
185 lines (168 loc) · 6.47 KB
/
focus-ordinal-bar.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>dc.js - Range / Focus Ordinal Bar Chart</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/dc.css"/>
</head>
<body>
<div class="container">
<script type="text/javascript" src="header.js"></script>
<p>This example demonstrates using range and focus charts to scroll an ordinal bar chart
with too many items. Instead of filtering, the brush only changes the view; bars are
clickable as in an ordinary ordinal bar chart.</p>
<p>Since ordinal-scale charts are not ordinarily brushable or zoomable, the example maps
the ordinal values to integers, and then maps the integers back to the values for the
axis ticks.</p>
<div id="range"></div>
<div id="focus"></div>
<div id="first-letters"></div>
<script type="text/javascript" src="../js/promise-polyfill.js"></script>
<script type="text/javascript" src="../js/fetch.umd.js"></script>
<script type="text/javascript" src="../js/d3.js"></script>
<script type="text/javascript" src="../js/crossfilter.js"></script>
<script type="text/javascript" src="../js/dc.js"></script>
<style>
#focus .axis.x .tick text {
text-anchor: end;
transform: rotate(-60deg) translate(-10px,-12px);
}
#range.dc-chart .axis {
display: none;
}
#range svg {
display: block; /* default inline causes padding? */
}
</style>
<script type="text/javascript">
// we need a slight delay to enable debounce, but a large delay is annoying for brushing
dc.constants.EVENT_DELAY = 10;
// index a group key -> i and i -> key
function ordinal_to_linear_group(group, sort) {
var _ord2int, _int2ord;
return {
all: function() {
var ret = group.all();
_ord2int = {};
_int2ord = [];
ret.forEach(function(d, i) {
_ord2int[d.key] = i;
_int2ord[i] = d.key;
});
return ret;
},
ord2int: function(o) {
if(!_ord2int)
this.all();
return _ord2int[o];
},
int2ord: function(i) {
if(!_int2ord)
this.all();
return _int2ord[i];
}
};
}
// phrases generated with https://www.fourmilab.ch/javascrypt/pass_phrase.html
var focus, range;
d3.json("wide-ordinal.json").then(function(wide) {
// dummy crossfilter taking group data and regrouping it to the same thing
var cf = crossfilter(wide),
dimension = cf.dimension(function(d) {return d.key;}),
group = dimension.group().reduceSum(function(d) {return d.value;});
group = ordinal_to_linear_group(group);
focus = dc.barChart('#focus');
var linear_domain = [-0.5, wide.length - 0.5];
focus
.width(1000).height(330)
.margins({left: 60, top: 0, right: 10, bottom: 145})
.x(d3.scaleLinear().domain(linear_domain))
.xUnits(dc.units.integers)
.keyAccessor(kv => group.ord2int(kv.key))
.centerBar(true)
.yAxisLabel('counts')
.elasticY(true)
.brushOn(false)
.dimension(dimension)
.mouseZoomable(true)
.zoomScale([4,8])
.group(group)
.title(kv => kv.key)
.transitionDuration(0);
focus.xAxis()
.tickFormat(function(d) { return group.int2ord(d); });
// unfortunately we have to recreate click-selection, since a focus chart
// ordinarily filters to the visible area (and we don't want a brush either)
var focusFilter = [];
focus.filterHandler(function() {}); // disable built-in filtering
focus.fadeDeselectedArea = function (brushSelection) {
var _chart = this;
var bars = _chart.chartBodyG().selectAll('rect.bar');
if (focusFilter.length) {
bars.classed(dc.constants.SELECTED_CLASS, function (d) {
return focusFilter.includes(d.data.key);
});
bars.classed(dc.constants.DESELECTED_CLASS, function (d) {
return !focusFilter.includes(d.data.key);
});
} else {
bars.classed(dc.constants.SELECTED_CLASS, false);
bars.classed(dc.constants.DESELECTED_CLASS, false);
}
};
focus.on('pretransition', function(chart) {
chart.selectAll('rect.bar').on('click.ordinal-select', function(d) {
var i = focusFilter.indexOf(d.data.key);
if(i >= 0)
focusFilter.splice(i, 1);
else
focusFilter.push(d.data.key);
if(focusFilter.length)
chart.dimension().filterFunction(function(k) {
return focusFilter.includes(k);
});
else chart.dimension().filter(null);
chart.redrawGroup();
});
});
focus.on('preRedraw', function(chart) {
var domain = chart.x().domain(),
min = Math.ceil(domain[0]), max = Math.floor(domain[1]);
chart.xAxis().tickValues(d3.range(min, max+1));
});
range = dc.barChart('#range');
range.filterHandler(function() {}); // disable built-in filtering
range
.margins({left: 74, top: 0, right: 20, bottom: 2})
.width(1000).height(20)
.x(d3.scaleLinear().domain(linear_domain))
.xUnits(dc.units.integers)
.keyAccessor(kv => group.ord2int(kv.key))
.elasticY(true)
.brushOn(true)
.dimension(dimension)
.group(group)
.transitionDuration(0);
focus
.rangeChart(range)
// display a row chart of first letters, to test filtering
var letterDimension = cf.dimension(function(d) {
return d.key.split(' ').map(function(s) { return s[0]; });
}, true);
var letterGroup = letterDimension.group();
var row = dc.rowChart('#first-letters');
row
.width(1000)
.height(350)
.gap(1)
.dimension(letterDimension)
.group(letterGroup);
dc.renderAll();
focus
.focus([-0.5,60])
});
</script>
</div>
</body>
</html>