-
Notifications
You must be signed in to change notification settings - Fork 2
/
barchart.html
155 lines (128 loc) · 3.43 KB
/
barchart.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
<!DOCTYPE html>
<html>
<head>
<title>Bar Chart</title>
<script type="text/javascript" src="d3.v2.js"></script>
<script type="text/javascript" src="data.js"></script>
<script type="text/javascript" src="icicle.js"></script>
<style type="text/css">
body {
font: 11px sans-serif;
}
.bar text.value {
fill: white;
}
.axis {
shape-rendering: crispEdges;
}
.axis path {
fill: none;
}
.x.axis line {
stroke: #fff;
stroke-opacity: .8;
}
.y.axis path {
stroke: black;
}
</style>
</head>
<body>
<script type="text/javascript">
var m = [30, 10, 10, 30],
w = 960 - m[1] - m[3],
h = 930 - m[0] - m[2];
var format = d3.format(",.0f");
var x = d3.scale.linear().range([0, w]),
y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h),
yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
var svg = d3.select("body").append("svg")
.attr("width", w + m[1] + m[3])
.attr("height", h + m[0] + m[2])
.append("g")
.attr("transform", "translate(" + m[3] + "," + m[0] + ")");
/*
data1 = [
{name:'pippo', value:10},
{name:'AK', value:20},
{name:'AZ', value:30},
{name:'AR', value:40},
{name:'CA', value:50},
];
*/
/* Program state */
var AvgData; /* raw data for the average transaction, filled in by wrapper code */
var MaxData; /* raw data for the worst transaction, filled in by wrapper code */
var MinData; /* raw data for the best transaction, filled in by wrapper code */
var bcTree = {}; /* intermediate data representation */
var bcData = []; /* the converted data ready to be digested by the barchart */
function convertIntermediate(tree)
{
var res = [];
var key, rem;
for (key in tree) {
convertIntermediate(tree[key].ch);
var akey = key;
var adata = {name:akey, value:tree[key].t, cont:tree[key].cont};
if (bcTree[akey]) {
bcTree[akey].value += adata.value;
} else {
bcTree[akey] = adata;
}
}
}
function convertData(tree)
{
convertIntermediate(tree);
for (key in bcTree) {
bcData.push(bcTree[key]);
}
}
function barChart(data) {
// Parse numbers, and sort by value.
data.forEach(function(d) { d.value = +d.value; });
data.sort(function(a, b) { return b.value - a.value; });
// Set the scale domain.
x.domain([0, d3.max(data, function(d) { return d.value; })]);
y.domain(data.map(function(d) { return d.name; }));
var bar = svg.selectAll("g.bar")
.data(data)
.enter().append("g")
.attr("class", "bar")
.attr("transform", function(d) {
return "translate(0," + y(d.name) + ")";
});
bar.append("rect")
.attr("width", function(d) { return x(d.value); })
.attr("height", y.rangeBand())
// .attr('fill', function(d) { return svColorMono(d.name); })
.attr('fill', "steelblue")
;
bar.append("text")
.attr("class", "value")
/*
.attr("x", function(d) {
return x(d.value);
})
*/
.attr("x", 6)
.attr("y", y.rangeBand() / 2)
.attr("dx", -3)
.attr("dy", ".35em")
// .attr("text-anchor", "end")
.text(function(d) { return d.name; });
svg.append("g")
.attr("class", "x axis")
.call(xAxis);
/*
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
*/
}
convertData(AvgData);
barChart(bcData);
</script>
</body>
</html>