Skip to content

Commit 5908117

Browse files
authored
Update node-pie.js
1 parent e4ac2ac commit 5908117

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

node-pie.js

+56-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
21
var DEFAULT_OPTIONS = {
32
radius: 8,
43
outerStrokeWidth: 1,
54
parentNodeColor: 'blue',
65
showPieChartBorder: true,
76
pieChartBorderColor: 'white',
87
pieChartBorderWidth: '1',
9-
showLabelText: false,
8+
showLabelText: true,
109
labelText: 'text',
10+
labelWeight: 'bold',
1111
labelColor: 'blue'
1212
};
1313

@@ -48,7 +48,7 @@ function drawPieChartBorder(nodeElement, options) {
4848
.attr("stroke-width", pieChartBorderWidth);
4949
}
5050

51-
function drawPieChart(nodeElement, percentages, options) {
51+
function drawPieChart(nodeElement, percentages, options, colorScale) {
5252
var radius = getOptionOrDefault('radius', options);
5353
var halfRadius = radius / 2;
5454
var halfCircumference = 2 * Math.PI * halfRadius;
@@ -60,7 +60,7 @@ function drawPieChart(nodeElement, percentages, options) {
6060
nodeElement.insert('circle', '#parent-pie + *')
6161
.attr("r", halfRadius)
6262
.attr("fill", 'transparent')
63-
.style('stroke', color(percentages[p].color))
63+
.style('stroke', colorScale(percentages[p].color))
6464
.style('stroke-width', radius)
6565
.style('stroke-dasharray',
6666
halfCircumference * percentToDraw / 100
@@ -73,28 +73,70 @@ function drawTitleText(nodeElement, options) {
7373
var radius = getOptionOrDefault('radius', options);
7474
var text = getOptionOrDefault('labelText', options);
7575
var color = getOptionOrDefault('labelColor', options);
76+
var labelWeight = getOptionOrDefault('labelWeight', options);
77+
78+
var textElement = nodeElement.select(".text-label");
79+
80+
var fontSize = 12;
81+
82+
if (textElement.empty()) {
83+
textElement = nodeElement.append("text")
84+
.attr("class", "text-label")
85+
.attr("fill", color)
86+
.attr("dy", radius * 3)
87+
.style("font-size", fontSize + "px")
88+
.style("font-weight", labelWeight)
89+
.style("pointer-events", "none");
90+
}
91+
92+
// Set text content
93+
textElement.text(String(text));
7694

77-
nodeElement.append("text")
78-
.text(String(text))
79-
.attr("fill", color)
80-
.attr("dy", radius * 3);
95+
// Set initial visibility based on the group and showLabelText option
96+
textElement.style("display", options.group === 1 ? "inline" : "none");
97+
98+
// Add mouseover and mouseout events directly to the nodeElement
99+
nodeElement.on("mouseover", function () {
100+
if (options.group === 0 && options.showLabelText) {
101+
textElement.style("visibility", "visible");
102+
}
103+
}).on("mouseout", function () {
104+
if (options.group === 0) {
105+
textElement.style("visibility", "hidden");
106+
}
107+
});
81108
}
82109

110+
111+
112+
83113
var NodePieBuilder = {
84-
drawNodePie: function (nodeElement, percentages, options) {
114+
drawNodePie: function (nodeElement, percentages, options, colorScale) {
85115
drawParentCircle(nodeElement, options);
86116

87117
if (!percentages) return;
88-
drawPieChart(nodeElement, percentages, options);
118+
drawPieChart(nodeElement, percentages, options, colorScale);
89119

90120
var showPieChartBorder = getOptionOrDefault('showPieChartBorder', options);
91121
if (showPieChartBorder) {
92122
drawPieChartBorder(nodeElement, options);
93123
}
94124

95-
var showLabelText = getOptionOrDefault('showLabelText', options);
96-
if (showLabelText) {
97-
drawTitleText(nodeElement, options);
98-
}
125+
drawTitleText(nodeElement, options);
126+
127+
// Add mouseover and mouseout events directly to the nodeElement
128+
nodeElement.on("mouseover", function () {
129+
if (options.group === 0 && options.showLabelText) {
130+
nodeElement.select(".text-label").style("display", "inline");
131+
}
132+
}).on("mouseout", function () {
133+
if (options.group === 0) {
134+
nodeElement.select(".text-label").style("display", "none");
135+
}
136+
});
99137
}
100138
};
139+
140+
141+
142+

0 commit comments

Comments
 (0)