-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn-chart.js
144 lines (121 loc) · 4.29 KB
/
column-chart.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
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
function columnChart() {
var margin = {top: 0, right: 0, bottom: 0, left: 0},
width = 420,
height = 500,
xRoundBands = 0.1,
xValue = function(d) { return d[0]; },
yValue = function(d) { return d[1]; },
zValue = function(d) { return d[2]; },
xScale = d3.scale.ordinal(),
yScale = d3.scale.linear(),
xAxis = d3.svg.axis().scale(xScale);
function chart(selection) {
selection.each(function(data) {
// Convert data to standard representation greedily;
// this is needed for nondeterministic accessors.
data = data.map(function(d, i) {
return [xValue.call(data, d, i), yValue.call(data, d, i), zValue.call(data, d, i)];
});
console.log(data)
// Update the x-scale.
xScale
.domain(data.map(function(d) { return d[0];} ))
.rangeRoundBands([0, width - margin.left - margin.right], xRoundBands);
// Update the y-scale.
yScale
.domain(d3.extent(data.map(function(d) { return d[1];} )))
.range([height - margin.top - margin.bottom, 0])
.nice();
// Select the svg element, if it exists.
var svg = d3.select(this).selectAll("svg").data([data]);
// Otherwise, create the skeletal chart.
var gEnter = svg.enter().append("svg").append("g");
gEnter.append("g").attr("class", "scrubber");
gEnter.append("g").attr("class", "bars");
gEnter.append("g").attr("class", "hotspots");
gEnter.append("g").attr("class", "x axis zero");
// Update the outer dimensions.
svg .attr("width", width)
.attr("height", height);
// Update the inner dimensions.
var g = svg.select("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var scrub = svg.select(".scrubber").append("rect")
.attr("height", height - margin.top - margin.bottom)
.attr("width", xScale.rangeBand())
.attr("y", 0)
.attr("x", 0)
.attr("class", "scrub")
.style("opacity", 0);
// Update the bars.
var bar = svg.select(".bars").selectAll(".bar").data(data);
bar.enter().append("rect");
bar.exit().remove();
bar .attr("class", function(d, i) { return d[1] < 0 ? "bar negative" : "bar positive"; })
.attr("x", function(d) { return X(d); })
.attr("y", function(d, i) { return d[1] < 0 ? Y0() : Y(d); })
.attr("width", xScale.rangeBand())
.attr("height", function(d, i) { return Math.max(Math.abs( Y(d) - Y0() ), 5); })
.style("opacity", function(d, i) { return d[2] == 0 ? 0.1 : 1 });
// Update the scrubbers.
var hotspot = svg.selectAll(".hotspot").data(data, function (d,i) {
return d[0];
});
hotspot.enter().append("rect");
hotspot.exit().remove();
hotspot.attr("class", "hotspot")
.attr("x", function(d) { return X(d); })
.attr("y", 0)
.attr("width", xScale.rangeBand())
.attr("height", height - margin.top - margin.bottom)
.on("mouseover", function(d) {
scrub.attr("transform", "translate(" + d3.select(this).attr("x") + ", 0)")
.style("opacity", .9);
})
.on("mouseout", function(d) {
scrub.style("opacity", 0);
});
// zero line
g.select(".x.axis.zero")
.attr("transform", "translate(0," + Y0() + ")")
.call(xAxis.tickFormat("").tickSize(0));
});
}
// The x-accessor for the path generator; xScale ∘ xValue.
function X(d) {
return xScale(d[0]);
}
function Y0() {
return yScale(0);
}
// The x-accessor for the path generator; yScale ∘ yValue.
function Y(d) {
return yScale(d[1]);
}
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
return chart;
};
chart.height = function(_) {
if (!arguments.length) return height;
height = _;
return chart;
};
chart.x = function(_) {
if (!arguments.length) return xValue;
xValue = _;
return chart;
};
chart.y = function(_) {
if (!arguments.length) return yValue;
yValue = _;
return chart;
};
return chart;
}