Skip to content

Commit 6e7de99

Browse files
Merge pull request #705 from TheRealJon/donut-pct-tooltip
fix(donut-pct-chart): Add tooltip attribute
2 parents cc7e9f8 + 500ce75 commit 6e7de99

File tree

3 files changed

+110
-14
lines changed

3 files changed

+110
-14
lines changed

src/charts/donut/donut-pct-chart-component.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ angular.module('patternfly.charts').component('pfDonutPctChart', {
22
bindings: {
33
config: '<',
44
data: '<',
5+
tooltip: '<',
56
chartHeight: '<?',
67
centerLabel: '<?',
78
onThresholdChange: '&'
@@ -67,19 +68,31 @@ angular.module('patternfly.charts').component('pfDonutPctChart', {
6768
ctrl.donutTooltip = function () {
6869
return {
6970
contents: function (d) {
70-
var tooltipHtml;
71-
71+
// Default to percent format
72+
var tooltipContent =
73+
'<span class="donut-tooltip-pf" style="white-space: nowrap;">' +
74+
Math.round(d[0].ratio * 100) + '% ' + d[0].name +
75+
'</span>';
7276
if (ctrl.config.tooltipFn) {
73-
tooltipHtml = '<span class="donut-tooltip-pf" style="white-space: nowrap;">' +
74-
ctrl.config.tooltipFn(d) +
75-
'</span>';
76-
} else {
77-
tooltipHtml = '<span class="donut-tooltip-pf" style="white-space: nowrap;">' +
78-
Math.round(d[0].ratio * 100) + '%' + ' ' + ctrl.config.units + ' ' + d[0].name +
79-
'</span>';
77+
tooltipContent =
78+
'<span class="donut-tooltip-pf" style="white-space: nowrap;">' +
79+
ctrl.config.tooltipFn(d) +
80+
'</span>';
81+
} else if (ctrl.tooltip === "amount") {
82+
tooltipContent =
83+
'<span class="donut-tooltip-pf" style="white-space: nowrap;">' +
84+
d[0].value + ' ' + ctrl.config.units + ' ' + d[0].name +
85+
'</span>';
86+
} else if (ctrl.tooltip === "both") {
87+
tooltipContent =
88+
'<table class="c3-tooltip"><tbody><tr>' +
89+
'<td>' +
90+
d[0].value + ' ' + ctrl.config.units + ' ' + d[0].name +
91+
'</td><td>' +
92+
Math.round(d[0].ratio * 100) + '%' +
93+
'</td></tr></tbody></table>';
8094
}
81-
82-
return tooltipHtml;
95+
return tooltipContent;
8396
}
8497
};
8598
};

src/charts/donut/examples/donut-pct-chart.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
* <li> 'none' - does not display the center label
5555
* </ul>
5656
*
57+
* @param {string=} tooltip specifies the value to show in the tooltip when hovering Used or Available chart segments
58+
* <strong>Values:</strong>
59+
* <ul style='list-style-type: none'>
60+
* <li> 'percent' - displays the Used or Available percentage of the Total in the tooltop (default)
61+
* <li> 'amount' - displays the Used or Available amount and units in the tooltip
62+
* <li> 'both' - displays both the percentage and amount in the tooltip
63+
* </ul>
64+
*
5765
* @param {int=} chartHeight height of the donut chart
5866
* @param {function (threshold)} on-threshold-change user defined function to handle when thresolds change <br/>
5967
* <strong>'threshold' Values:</strong>
@@ -114,9 +122,30 @@
114122
<pf-donut-pct-chart config="pctConfig" data="pctData" center-label="pctLabel"></pf-donut-pct-chart>
115123
</div>
116124
<div class="col-md-3 text-center">
117-
<label>center-label = 'none'</label>
125+
<label>center-label = 'none'</label>
126+
<pf-donut-pct-chart config="noneConfig" data="noneData" center-label="noLabel"></pf-donut-pct-chart>
127+
</div>
128+
</div>
129+
130+
<div class="row">
131+
<div class="col-md-12">
132+
<hr>
133+
</div>
134+
</div>
135+
136+
<div class="row">
137+
<div class="col-md-4 text-center">
138+
<label>tooltip = 'percent'</label>
118139
<pf-donut-pct-chart config="noneConfig" data="noneData" center-label="noLabel"></pf-donut-pct-chart>
119140
</div>
141+
<div class="col-md-4 text-center">
142+
<label>tooltip = 'amount'</label>
143+
<pf-donut-pct-chart config="noneConfig" data="noneData" center-label="noLabel" tooltip="tooltipAmount"></pf-donut-pct-chart>
144+
</div>
145+
<div class="col-md-4 text-center">
146+
<label>tooltip = 'both'</label>
147+
<pf-donut-pct-chart config="noneConfig" data="noneData" center-label="noLabel" tooltip="tooltipBoth"></pf-donut-pct-chart>
148+
</div>
120149
</div>
121150
122151
<div class="row">
@@ -302,6 +331,10 @@
302331
$scope.noLabel = "none";
303332
304333
//start demo 3rd row
334+
$scope.tooltipAmount = "amount";
335+
$scope.tooltipBoth = "both";
336+
337+
//start demo 4th row
305338
$scope.configOrientationLeft = {
306339
'units': 'GB',
307340
'thresholds':{'warning':'60','error':'90'},
@@ -370,7 +403,7 @@
370403
'total': '1000'
371404
};
372405
373-
//start demo 4th row
406+
//start demo 5th row
374407
$scope.custConfig = {
375408
'chartId': 'custChart',
376409
'units': 'MHz',

test/charts/donut/donut-pct.spec.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ describe('Directive: pfDonutPctChart', function () {
5151
element = compileDonut('<pf-donut-pct-chart config="config" data="data" center-label="cntrLabel"></pf-donut-pct-chart>');
5252
};
5353

54+
var compileTooltipDonut = function () {
55+
element = compileDonut('<pf-donut-pct-chart config="config" data="data" tooltip="tooltip"></pf-donut-pct-chart>');
56+
};
57+
5458
it("should have an external label", function () {
5559
compileSimpleDonut();
5660
expect(element.find('.pct-donut-chart-pf-right').length).toEqual(1);
@@ -119,7 +123,6 @@ describe('Directive: pfDonutPctChart', function () {
119123
expect(ctrl.getCenterLabelText().smText).toContain('Used');
120124
});
121125

122-
123126
it("should use center label funtion", function () {
124127
compileDonutCenterLabel();
125128

@@ -149,5 +152,52 @@ describe('Directive: pfDonutPctChart', function () {
149152
expect(emptyChart.length).toBe(1);
150153
});
151154

155+
it('should have percentage in the tooltip content', function() {
156+
var d = [{
157+
name: "Used",
158+
ratio: .35,
159+
value: 350
160+
}];
161+
compileSimpleDonut();
162+
expect(ctrl.donutTooltip().contents(d)).toContain('35% Used');
163+
});
164+
165+
it('should have value and units in tooltip content', function() {
166+
var d = [{
167+
name: "Used",
168+
ratio: .35,
169+
value: 350
170+
}];
171+
compileTooltipDonut();
172+
$scope.tooltip = "amount";
173+
$scope.$digest();
174+
expect(ctrl.donutTooltip().contents(d)).toContain('350 MHz Used');
175+
});
176+
177+
it('should have value, units, and percentage in tooltip content', function() {
178+
var d = [{
179+
name: "Used",
180+
ratio: .35,
181+
value: 350
182+
}];
183+
compileTooltipDonut();
184+
$scope.tooltip = "both";
185+
$scope.$digest();
186+
expect(ctrl.donutTooltip().contents(d)).toContain('350 MHz Used');
187+
expect(ctrl.donutTooltip().contents(d)).toContain('35%');
188+
});
189+
190+
it('should have use config.tooltipFn for tooltip content', function() {
191+
var d = [{
192+
name: "Used",
193+
ratio: .35,
194+
value: 350
195+
}];
196+
$scope.config.tooltipFn = function(d) {
197+
return "This is the tooltip content.";
198+
};
199+
compileSimpleDonut();
200+
expect(ctrl.donutTooltip().contents(d)).toContain('This is the tooltip content.');
201+
});
152202
});
153203

0 commit comments

Comments
 (0)