Skip to content

Commit 31a2c94

Browse files
committed
Change chart. Add chart with lagand and delete lines with label's
1 parent 5e3edcd commit 31a2c94

File tree

2 files changed

+21
-67
lines changed

2 files changed

+21
-67
lines changed

scripts/chart/chart-core.js

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function donutChart() {
4949
.attr('height', height + margin.top + margin.bottom)
5050
.attr('class', 'backColorChart')
5151
.append('g')
52-
.attr('transform', 'translate(' + (width / 2 + 20) + ',' + (height / 2 + 10) + ')');
52+
.attr('transform', 'translate(' + (width / 2 - 100) + ',' + (height / 2) + ')');
5353
// ===========================================================================================
5454

5555
// ===========================================================================================
@@ -70,74 +70,28 @@ function donutChart() {
7070
.attr('id', function (d) { return d.data[category]; });
7171
// ===========================================================================================
7272

73-
// ===========================================================================================
74-
// add text labels
75-
var label = svg.select('.labelName').selectAll('text')
76-
.data(pie)
77-
.enter().append('text')
78-
.attr('dy', '.35em')
79-
.html(function (d) {
80-
// add "key: value" for given category. Number inside tspan is bolded in stylesheet.
81-
return ' <tspan class="siteName">' + d.data[category] + ' </tspan>';
73+
var legendG = svg.selectAll(".legend") // note appending it to mySvg and not svg to make positioning easier
74+
.data(pie(data))
75+
.enter().append("g")
76+
.attr("transform", function (d, i) {
77+
return "translate(" + (120) + "," + (i * 20 - 30) + ")"; // place each legend on the right and bump each one down 15 pixels
8278
})
83-
.attr('transform', function (d) {
84-
85-
// effectively computes the centre of the slice.
86-
// see https://github.com/d3/d3-shape/blob/master/README.md#arc_centroid
87-
var pos = outerArc.centroid(d);
88-
89-
// changes the point to be on left or right depending on where label is.
90-
pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
91-
92-
var currentAngle = midAngle(d);
93-
if (angleIsInRangeDifference(tempAngle, currentAngle, 0.5)){
94-
tempOffset.x -= 0.07;
95-
tempOffset.y += 0.07;
96-
tempAngle = 0;
79+
.attr("class", "legend");
9780

98-
pos[0] = pos[0] * tempOffset.x;
99-
pos[1] = pos[1] * tempOffset.y;
100-
}
101-
102-
tempAngle = midAngle(d);
103-
104-
return 'translate(' + pos + ')';
105-
})
106-
.style('text-anchor', function (d) {
107-
// if slice centre is on the left, anchor text to start, otherwise anchor to end
108-
return (midAngle(d)) < Math.PI ? 'start' : 'end';
81+
legendG.append("rect") // make a matching color rect
82+
.attr("width", 10)
83+
.attr("height", 10)
84+
.attr("fill", function (d, i) {
85+
return colour(d.data[category]);
10986
});
110-
// ===========================================================================================
11187

112-
tempOffset = {
113-
x: 1,
114-
y: 0.95
115-
};
116-
// ===========================================================================================
117-
// add lines connecting labels to slice. A polyline creates straight lines connecting several points
118-
var polyline = svg.select('.lines')
119-
.selectAll('polyline')
120-
.data(pie)
121-
.enter().append('polyline')
122-
.attr('points', function (d) {
123-
// see label transform function for explanations of these three lines.
124-
var pos = outerArc.centroid(d);
125-
pos[0] = radius * 0.95 * (midAngle(d) < Math.PI ? 1 : -1);
126-
127-
var currentAngle = midAngle(d);
128-
if (angleIsInRangeDifference(tempAngle, currentAngle, 0.5)){
129-
tempOffset.x -= 0.07;
130-
tempOffset.y += 0.07;
131-
132-
pos[0] = pos[0] * tempOffset.x;
133-
pos[1] = pos[1] * tempOffset.y;
134-
tempAngle = 0;
135-
}
136-
137-
tempAngle = midAngle(d);
138-
return [arc.centroid(d), outerArc.centroid(d), pos]
139-
});
140-
// ===========================================================================================
88+
legendG.append("text") // add the text
89+
.text(function (d) {
90+
return d.data.url;
91+
})
92+
.style("font-size", 13)
93+
.attr("y", 10)
94+
.attr("x", 11);
14195

14296
// ===========================================================================================
14397
// add tooltip to mouse events on slices and labels
@@ -204,7 +158,7 @@ function donutChart() {
204158
return tip;
205159
}
206160

207-
function angleIsInRangeDifference(tempAngle, currentAngle, difference){
161+
function angleIsInRangeDifference(tempAngle, currentAngle, difference) {
208162
return currentAngle < (tempAngle + difference) && currentAngle > (tempAngle - difference);
209163
}
210164
// ===========================================================================================

scripts/ui.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class UI {
8181
drawChart(tabs) {
8282
var donut = donutChart()
8383
.width(480)
84-
.height(280)
84+
.height(240)
8585
.cornerRadius(5) // sets how rounded the corners are on each slice
8686
.padAngle(0.020) // effectively dictates the gap between slices
8787
.variable('percentage')

0 commit comments

Comments
 (0)