forked from benkeen/d3pie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_text.js
More file actions
171 lines (155 loc) · 5.41 KB
/
_text.js
File metadata and controls
171 lines (155 loc) · 5.41 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// --------- text.js -----------
var text = {
offscreenCoord: -10000,
addTitle: function(pie) {
var title = pie.svg.selectAll("." + pie.cssPrefix + "title")
.data([pie.options.header.title])
.enter()
.append("text")
.text(function(d) { return d.text; })
.attr({
id: pie.cssPrefix + "title",
class: pie.cssPrefix + "title",
x: text.offscreenCoord,
y: text.offscreenCoord
})
.attr("text-anchor", function() {
var location;
if (pie.options.header.location === "top-center" || pie.options.header.location === "pie-center") {
location = "middle";
} else {
location = "left";
}
return location;
})
.attr("fill", function(d) { return d.color; })
.style("font-size", function(d) { return d.fontSize + "px"; })
.style("font-family", function(d) { return d.font; });
},
positionTitle: function(pie) {
var textComponents = pie.textComponents;
var headerLocation = pie.options.header.location;
var canvasPadding = pie.options.misc.canvasPadding;
var canvasWidth = pie.options.size.canvasWidth;
var titleSubtitlePadding = pie.options.header.titleSubtitlePadding;
var x;
if (headerLocation === "top-left") {
x = canvasPadding.left;
} else {
x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left;
}
var y = canvasPadding.top + textComponents.title.h;
if (headerLocation === "pie-center") {
y = pie.pieCenter.y;
// still not fully correct
if (textComponents.subtitle.exists) {
var totalTitleHeight = textComponents.title.h + titleSubtitlePadding + textComponents.subtitle.h;
y = y - (totalTitleHeight / 2) + textComponents.title.h;
} else {
y += (textComponents.title.h / 4);
}
}
pie.svg.select("#" + pie.cssPrefix + "title")
.attr("x", x)
.attr("y", y);
},
addSubtitle: function(pie) {
var headerLocation = pie.options.header.location;
pie.svg.selectAll("." + pie.cssPrefix + "subtitle")
.data([pie.options.header.subtitle])
.enter()
.append("text")
.text(function(d) { return d.text; })
.attr("x", text.offscreenCoord)
.attr("y", text.offscreenCoord)
.attr("id", pie.cssPrefix + "subtitle")
.attr("class", pie.cssPrefix + "subtitle")
.attr("text-anchor", function() {
var location;
if (headerLocation === "top-center" || headerLocation === "pie-center") {
location = "middle";
} else {
location = "left";
}
return location;
})
.attr("fill", function(d) { return d.color; })
.style("font-size", function(d) { return d.fontSize + "px"; })
.style("font-family", function(d) { return d.font; });
},
positionSubtitle: function(pie) {
var canvasPadding = pie.options.misc.canvasPadding;
var canvasWidth = pie.options.size.canvasWidth;
var x;
if (pie.options.header.location === "top-left") {
x = canvasPadding.left;
} else {
x = ((canvasWidth - canvasPadding.right) / 2) + canvasPadding.left;
}
var y = text.getHeaderHeight(pie);
pie.svg.select("#" + pie.cssPrefix + "subtitle")
.attr("x", x)
.attr("y", y);
},
addFooter: function(pie) {
pie.svg.selectAll("." + pie.cssPrefix + "footer")
.data([pie.options.footer])
.enter()
.append("text")
.text(function(d) { return d.text; })
.attr("x", text.offscreenCoord)
.attr("y", text.offscreenCoord)
.attr("id", pie.cssPrefix + "footer")
.attr("class", pie.cssPrefix + "footer")
.attr("text-anchor", function() {
var location = "left";
if (pie.options.footer.location === "bottom-center") {
location = "middle";
} else if (pie.options.footer.location === "bottom-right") {
location = "left"; // on purpose. We have to change the x-coord to make it properly right-aligned
}
return location;
})
.attr("fill", function(d) { return d.color; })
.style("font-size", function(d) { return d.fontSize + "px"; })
.style("font-family", function(d) { return d.font; });
},
positionFooter: function(pie) {
var footerLocation = pie.options.footer.location;
var footerWidth = pie.textComponents.footer.w;
var canvasWidth = pie.options.size.canvasWidth;
var canvasHeight = pie.options.size.canvasHeight;
var canvasPadding = pie.options.misc.canvasPadding;
var x;
if (footerLocation === "bottom-left") {
x = canvasPadding.left;
} else if (footerLocation === "bottom-right") {
x = canvasWidth - footerWidth - canvasPadding.right;
} else {
x = canvasWidth / 2; // TODO - shouldn't this also take into account padding?
}
pie.svg.select("#" + pie.cssPrefix + "footer")
.attr("x", x)
.attr("y", canvasHeight - canvasPadding.bottom);
},
getHeaderHeight: function(pie) {
var h;
if (pie.textComponents.title.exists) {
// if the subtitle isn't defined, it'll be set to 0
var totalTitleHeight = pie.textComponents.title.h + pie.options.header.titleSubtitlePadding + pie.textComponents.subtitle.h;
if (pie.options.header.location === "pie-center") {
h = pie.pieCenter.y - (totalTitleHeight / 2) + totalTitleHeight;
} else {
h = totalTitleHeight + pie.options.misc.canvasPadding.top;
}
} else {
if (pie.options.header.location === "pie-center") {
var footerPlusPadding = pie.options.misc.canvasPadding.bottom + pie.textComponents.footer.h;
h = ((pie.options.size.canvasHeight - footerPlusPadding) / 2) + pie.options.misc.canvasPadding.top + (pie.textComponents.subtitle.h / 2);
} else {
h = pie.options.misc.canvasPadding.top + pie.textComponents.subtitle.h;
}
}
return h;
}
};