Skip to content

Commit 8841f08

Browse files
add test for ordinal bar ordering with stacks
1 parent 159fcdd commit 8841f08

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

spec/bar-chart-spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,71 @@ describe('dc.barChart', function() {
10371037
}
10381038
});
10391039

1040+
describe('ordering with stacks', function() {
1041+
var group2;
1042+
beforeEach(function() {
1043+
var rows = [
1044+
{x: 'a', y: 1, z: 10},
1045+
{x: 'b', y: 3, z: 20},
1046+
{x: 'd', y: 4, z: 30},
1047+
{x: 'c', y: 2, z: 40}
1048+
];
1049+
1050+
id = 'bar-chart';
1051+
appendChartID(id);
1052+
data = crossfilter(rows);
1053+
dimension = data.dimension(function(d) {
1054+
return d.x;
1055+
});
1056+
group = dimension.group().reduceSum(function(d) {
1057+
return d.y;
1058+
});
1059+
var group2 = dimension.group().reduceSum(function(d) {
1060+
return d.z;
1061+
});
1062+
1063+
chart = dc.barChart('#' + id);
1064+
chart.width(500).transitionDuration(0)
1065+
.x(d3.scale.ordinal())
1066+
.xUnits(dc.units.ordinal)
1067+
.elasticY(true).elasticX(true)
1068+
.dimension(dimension)
1069+
.group(group)
1070+
.stack(group2);
1071+
chart.render();
1072+
});
1073+
1074+
it('should be ordered by default alphabetical order', function() {
1075+
var data = chart.data()["0"].values;
1076+
var expectedData = ["a", "b", "c", "d"];
1077+
expect(data.map(function(d) { return d.x; })).toEqual(expectedData);
1078+
});
1079+
1080+
// note: semantics are kind of screwy here: which stack do you want to sort
1081+
// by when you order by value? right now it's all of them together.
1082+
it('should be ordered by value increasing', function() {
1083+
chart.ordering(function(d) { return d.data.value; });
1084+
chart.redraw();
1085+
expect(xAxisText()).toEqual(["a", "c", "b", "d"]);
1086+
});
1087+
1088+
it('should be ordered by value decreasing', function() {
1089+
chart.ordering(function(d) { return -d.data.value; });
1090+
chart.redraw();
1091+
expect(xAxisText()).toEqual(["c", "d", "b", "a"]);
1092+
});
1093+
1094+
it('should be ordered by alphabetical order', function() {
1095+
chart.ordering(function(d) { return d.data.key; });
1096+
chart.redraw();
1097+
expect(xAxisText()).toEqual(["a", "b", "c", "d"]);
1098+
});
1099+
1100+
function xAxisText() {
1101+
return chart.selectAll("g.x text")[0].map(function(x) { return d3.select(x).text(); });
1102+
}
1103+
});
1104+
10401105
function nthStack(n) {
10411106
var stack = d3.select(chart.selectAll(".stack")[0][n]);
10421107

0 commit comments

Comments
 (0)