-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
144 lines (127 loc) · 3.56 KB
/
index.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
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<div id="bar_chart">
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// d3.json("data_bar_chart.json", function(error, data) {
// if (error) throw error;
// var parseTime = d3.timeParse("%M:%S");
// var timeformat = d3.timeFormat("%M:%S")
var data = [{
"name": "A",
"val1": 6,
"val2": 2
}, {
"name": "B",
"val1": 5,
"val2": 4
}]
data.forEach(function(d) {
// d.atime = parseTime(d.atime);
d.value = +d.value;
});
var margin = {
top: 50,
right: 50,
bottom: 100,
left: 80
},
width = 1500 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var x = d3.scaleBand()
.domain(data.map(function(d) {
return d.name
}))
.range([0, width / 4]);
var y1 = d3.scaleLinear()
.domain([0, d3.max(data, function(d) {
return d.val1
})])
.range([height, 0]);
var y2 = d3.scaleLinear()
.domain([0, d3.max(data, function(d) {
return d.val2
})])
.range([height, 0]);
var svg = d3.select("#bar_chart")
.data(data)
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Add the X Axis
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Add the Y Axis
svg.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y1)
.ticks(7));
var tooltip = d3.select("#info")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden");
var rects = svg.selectAll('rect')
.data(data);
//********* Bar Chart 1 ****************
var newRects1 = rects.enter();
newRects1.append('rect')
.attr('x', function(d, i) {
return x(d.name);
})
.attr('y', function(d, i) {
return y1(d.val1);
})
.attr('height', function(d, i) {
return height - y1(d.val1)
})
.attr('width', 40)
.attr("transform", "translate(" + 65 + ",0)")
.style('fill', 'blue')
.style('stroke', 'gray')
.attr('class', function(d, i) {
'class-' + i
})
.on("mouseover", function(d) {
// return tooltip.style("fill", "red")
d3.select(this).style("fill", "lightsteelblue")
})
.on("mouseout", function(d) {
// return tooltip.style("fill", "red")
d3.select(this).style("fill", "blue")
});
//********* Bar Chart 2 ****************
var newRects2 = rects.enter();
newRects2.append('rect')
.attr('x', function(d, i) {
return x(d.name);
})
.attr('y', function(d, i) {
return y1(d.val2);
})
.attr('height', function(d, i) {
return height - y1(d.val2)
})
.attr('width', 40)
.attr("transform", "translate(" + 65 + ",0)")
.style('fill', 'green')
.style('stroke', 'gray')
.attr('class', function(d, i) {
'class-' + i
})
.on("mouseover", function(d) {
// return tooltip.style("fill", "red")
d3.select(this).style("fill", "lightgreen")
})
.on("mouseout", function(d) {
// return tooltip.style("fill", "red")
d3.select(this).style("fill", "green")
});
// });
</script>