-
Notifications
You must be signed in to change notification settings - Fork 556
/
advanced_bar_labels.html
56 lines (56 loc) · 2.37 KB
/
advanced_bar_labels.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
<!----------------------------------------------------------------->
<!-- AUTOMATICALLY GENERATED CODE - PLEASE EDIT TEMPLATE INSTEAD -->
<!----------------------------------------------------------------->
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<div id="chartContainer">
<script src="/lib/d3.v4.3.0.js"></script>
<script src="/dist/dimple.v2.3.0.js"></script>
<script type="text/javascript">
// This is the simple vertical grouped stacked 100% bar example
var svg = dimple.newSvg("#chartContainer", 590, 400);
d3.tsv("/data/example_data.tsv", function (data) {
var myChart = new dimple.chart(svg, data);
myChart.setBounds(65, 45, 505, 315)
var x = myChart.addCategoryAxis("x", ["Price Tier", "Channel"]);
var y = myChart.addPctAxis("y", "Unit Sales");
var s = myChart.addSeries("Owner", dimple.plot.bar);
// Using the afterDraw callback means this code still works with animated
// draws (e.g. myChart.draw(1000)) or storyboards (though an onTick callback should
// also be used to clear the text from the previous frame)
s.afterDraw = function (shape, data) {
// Get the shape as a d3 selection
var s = d3.select(shape),
rect = {
x: parseFloat(s.attr("x")),
y: parseFloat(s.attr("y")),
width: parseFloat(s.attr("width")),
height: parseFloat(s.attr("height"))
};
// Only label bars where the text can fit
if (rect.height >= 8) {
// Add a text label for the value
svg.append("text")
// Position in the centre of the shape (vertical position is
// manually set due to cross-browser problems with baseline)
.attr("x", rect.x + rect.width / 2)
.attr("y", rect.y + rect.height / 2 + 3.5)
// Centre align
.style("text-anchor", "middle")
.style("font-size", "10px")
.style("font-family", "sans-serif")
// Make it a little transparent to tone down the black
.style("opacity", 0.6)
// Prevent text cursor on hover and allow tooltips
.style("pointer-events", "none")
// Format the number
.text(d3.format(",.1f")(data.yValue / 1000) + "k");
}
};
myChart.addLegend(200, 10, 380, 20, "right");
myChart.draw();
});
</script>
</div>
</html>