forked from dc-js/dc.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-addition-test.js
112 lines (97 loc) · 3.51 KB
/
data-addition-test.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
require("./env");
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('Dynamic data addition in crossfilter');
var width = 200;
var height = 200;
var radius = 100;
var innerRadius = 30;
var baseData = crossfilter(json);
var valueDimension = baseData.dimension(function(d) {
return d.value;
});
var valueGroup = valueDimension.group();
function buildPieChart(id) {
var div = d3.select("body").append("div").attr("id", id);
div.append("a").attr("class", "reset").style("display", "none");
div.append("span").attr("class", "filter").style("display", "none");
var chart = dc.pieChart("#" + id);
chart.dimension(valueDimension).group(valueGroup)
.width(width)
.height(height)
.radius(radius)
.transitionDuration(0);
chart.render();
baseData.add(json2);
return chart;
}
var baseData2 = crossfilter(json);
var timeDimension = baseData2.dimension(function(d) {
return d.dd;
});
var timeGroup = timeDimension.group();
function buildLineChart(id) {
d3.select("body").append("div").attr("id", id);
var chart = dc.lineChart("#" + id);
chart.dimension(timeDimension).group(timeGroup)
.width(width).height(height)
.x(d3.time.scale().domain([new Date(2012, 4, 20), new Date(2012, 7, 15)]))
.transitionDuration(0)
.xUnits(d3.time.days)
.renderArea(true)
.renderTitle(true);
chart.render();
baseData2.add(json2);
return chart;
}
function occurrences(str, value) {
return (str.split(value)).length - 1;
}
suite.addBatch({
'pie chart slice addition': {
topic: function() {
var chart = buildPieChart("pie-chart");
chart.redraw();
return chart;
},
'slice g should be created with class': function(pieChart) {
assert.equal(pieChart.selectAll("svg g g.pie-slice").data().length, 7);
},
'slice path should be created': function(pieChart) {
assert.equal(pieChart.selectAll("svg g g.pie-slice path").data().length, 7);
},
'default function should be used to dynamically generate label': function(chart) {
assert.equal(d3.select(chart.selectAll("text.pie-slice")[0][0]).text(), "66");
},
'default function should be used to dynamically generate title': function(chart) {
assert.equal(d3.select(chart.selectAll("g.pie-slice title")[0][0]).text(), "66: 1");
},
teardown:function(chart) {
resetAllFilters();
resetBody();
}
},
'line chart segment addition': {
topic: function() {
var chart = buildLineChart("line-chart");
chart.redraw();
return chart;
},
'number of dots should equal the size of the group': function(lineChart) {
assert.equal(lineChart.selectAll("circle.dot")[0].length, timeGroup.size());
},
'number of line segments should equal the size of the group': function(lineChart) {
var path = lineChart.selectAll("path.line").attr("d");
assert.equal(occurrences(path, 'L') + 1, timeGroup.size());
},
'number of area segments should equal twice the size of the group': function(lineChart) {
var path = lineChart.selectAll("path.area").attr("d");
assert.equal(occurrences(path, 'L') + 1, timeGroup.size() * 2);
},
teardown:function(chart) {
resetAllFilters();
resetBody();
}
}
});
suite.export(module);