Skip to content

Utilization Donut Chart Component #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/charts/donut/donut-pct-chart-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ angular.module('patternfly.charts').component('pfDonutPctChart', {
onThresholdChange: '&'
},
templateUrl: 'charts/donut/donut-pct-chart.html',
controller: function (pfUtils, $element, $timeout) {
controller: function (pfUtils, $scope) {
'use strict';
var ctrl = this, prevData;
ctrl.$id = $scope.$id;

ctrl.$onInit = function () {
ctrl.donutChartId = 'donutPctChart';
ctrl.donutChartId = 'donutPctChart' + ctrl.$id;

if (ctrl.config.chartId) {
ctrl.donutChartId = ctrl.config.chartId + ctrl.donutChartId;
}
Expand All @@ -24,6 +26,10 @@ angular.module('patternfly.charts').component('pfDonutPctChart', {
ctrl.data.available = ctrl.data.total - ctrl.data.used;
};

ctrl.updatePercentage = function () {
ctrl.data.percent = Math.round(ctrl.data.used / ctrl.data.total * 100.0);
};

ctrl.getStatusColor = function (used, thresholds) {
var threshold = "none";
var color = pfUtils.colorPalette.blue;
Expand Down Expand Up @@ -127,6 +133,7 @@ angular.module('patternfly.charts').component('pfDonutPctChart', {

ctrl.config = pfUtils.merge(patternfly.c3ChartDefaults().getDefaultDonutConfig(), ctrl.config);
ctrl.updateAvailable();
ctrl.updatePercentage();
ctrl.config.data = pfUtils.merge(ctrl.config.data, ctrl.getDonutData());
ctrl.config.color = ctrl.statusDonutColor(ctrl);
ctrl.config.tooltip = ctrl.donutTooltip();
Expand Down
25 changes: 21 additions & 4 deletions src/charts/donut/donut-pct-chart.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
<span>
<pf-c3-chart ng-if="$ctrl.data.dataAvailable !== false" id="{{$ctrl.donutChartId}}" config="$ctrl.config"
get-chart-callback="$ctrl.setChart"></pf-c3-chart>
<pf-empty-chart ng-if="$ctrl.data.dataAvailable === false" chart-height="$ctrl.chartHeight"></pf-empty-chart>
<span class="pct-donut-chart-pf">
<span ng-class="{'pct-donut-chart-pf-left': $ctrl.config.labelConfig.orientation === 'left', 'pct-donut-chart-pf-right': $ctrl.config.labelConfig.orientation === 'right'}">
<span class="pct-donut-chart-pf-chart">
<pf-c3-chart ng-if="$ctrl.data.dataAvailable !== false" id="{{$ctrl.donutChartId}}" config="$ctrl.config"
get-chart-callback="$ctrl.setChart"></pf-c3-chart>
<pf-empty-chart ng-if="$ctrl.data.dataAvailable === false" chart-height="$ctrl.chartHeight"></pf-empty-chart>
</span>
<span ng-if="$ctrl.data.dataAvailable !== false && $ctrl.config.labelConfig && !$ctrl.config.labelConfig.labelFn()" class="pct-donut-chart-pf-label">
{{$ctrl.config.labelConfig.title}}
<span ng-if="$ctrl.data" ng-switch="$ctrl.config.labelConfig.label">
<span ng-switch-when="none"></span>
<span ng-switch-when="available">{{$ctrl.data.available}} {{$ctrl.config.labelConfig.units}} available</span>
<span ng-switch-when="percent">{{$ctrl.data.percent}}&#37; used</span>
<span ng-switch-default="">{{$ctrl.data.used}} {{$ctrl.config.labelConfig.units}} of {{$ctrl.data.total}} {{$ctrl.config.labelConfig.units}} used</span>
</span>
</span>
<span ng-if="$ctrl.data.dataAvailable !== false && $ctrl.config.labelConfig && $ctrl.config.labelConfig.labelFn()" class="pct-donut-chart-pf-label"
ng-bind-html="$ctrl.config.labelConfig.labelFn()">
</span>
</span>
</span>

138 changes: 125 additions & 13 deletions src/charts/donut/examples/donut-pct-chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@
* <li>.tooltipFn(d) - user defined function to customize the tool tip (optional)
* <li>.centerLabelFn - user defined function to customize the text of the center label (optional)
* <li>.onClickFn(d,i) - user defined function to handle when donut arc is clicked upon.
* <li>.labelConfig - object containing properties for external label (optional) - default: undefined
* <ul>
* <li>.orientation - string with possible values: 'left', 'right' (optional) - default: 'center'
* <li>.title - string representing a prefix or title (optional) - default: empty string
* <li>.label - the wording format to display, possible values: 'used', 'available', 'percent', 'none' (optional) - default: 'used'
* <li>.units - unit label for values, ex: 'MHz','GB', etc.. (optional) - default: empty string
* <li>.labelFn - function to customize the text of the external label. This callback returns no data. Updated display data can be accessed through the passed and updated parameter 'data'. (optional) - default: undefined
* </ul>
* </li>
* </ul>
*
* @param {object} data the Total and Used values for the donut chart. Available is calculated as Total - Used.<br/>
* <ul style='list-style-type: none'>
* <li>.used - number representing the amount used
* <li>.percent - number representing the percentage used
* <li>.total - number representing the total amount
* <li>.dataAvailable - Flag if there is data available - default: true
* </ul>
Expand Down Expand Up @@ -62,19 +72,23 @@
<div class="row">
<div class="col-md-3 text-center">
<label>Error Threshold</label>
<pf-donut-pct-chart config="configErr" data="dataErr" chart="chartErr"></pf-donut-pct-chart>
<p class="text-right">
<pf-donut-pct-chart config="configErr" data="dataErr" chart="chartErr"></pf-donut-pct-chart>
</p>
</div>
<div class="col-md-3 text-center"">
<div class="col-md-3 text-center">
<label>Warning Threshold</label>
<pf-donut-pct-chart config="configWarn" data="dataWarn"></pf-donut-pct-chart>
</div>
<div class="col-md-3 text-center"">
<div class="col-md-3 text-center">
<label class="camelcase">{{threshLabel}} Threshold</label>
<pf-donut-pct-chart config="configDynamic" data="dataDynamic" center-label="labelDynamic"
on-threshold-change="thresholdChanged(threshold)">
</pf-donut-pct-chart>
<p class="text-left">
<pf-donut-pct-chart config="configDynamic" data="dataDynamic" center-label="labelDynamic"
on-threshold-change="thresholdChanged(threshold)">
</pf-donut-pct-chart>
</p>
</div>
<div class="col-md-3 text-center"">
<div class="col-md-3 text-center">
<label>No Threshold</label>
<pf-donut-pct-chart config="configNoThresh" data="dataNoThresh"></pf-donut-pct-chart>
</div>
Expand All @@ -88,20 +102,45 @@

<div class="row">
<div class="col-md-3 text-center">
<pf-donut-pct-chart config="usedConfig" data="usedData" center-label="usedLabel"></pf-donut-pct-chart>
<label>center-label = 'used'</label>
<pf-donut-pct-chart config="usedConfig" data="usedData" center-label="usedLabel"></pf-donut-pct-chart>
</div>
<div class="col-md-3 text-center">
<pf-donut-pct-chart config="availConfig" data="availData" center-label="availLabel"></pf-donut-pct-chart>
<label>center-label = 'available'</label>
<pf-donut-pct-chart config="availConfig" data="availData" center-label="availLabel"></pf-donut-pct-chart>
</div>
<div class="col-md-3 text-center">
<pf-donut-pct-chart config="pctConfig" data="pctData" center-label="pctLabel"></pf-donut-pct-chart>
<label>center-label = 'percent'</label>
<pf-donut-pct-chart config="pctConfig" data="pctData" center-label="pctLabel"></pf-donut-pct-chart>
</div>
<div class="col-md-3 text-center">
<label>center-label = 'none'</label>
<pf-donut-pct-chart config="noneConfig" data="noneData" center-label="noLabel"></pf-donut-pct-chart>
<label>center-label = ' none'</label>
</div>
</div>

<div class="row">
<div class="col-md-12">
<hr>
</div>
</div>

<div class="row">
<div class="col-md-4 text-center">
<label>Sized with orientation left 'configLabel'</label>
<p class="text-right">
<pf-donut-pct-chart config="configOrientationLeft" data="dataOrientationLeft"></pf-donut-pct-chart>
</p>
</div>
<div class="col-md-4 text-center">
<label>Sized with 'configLabel'</label>
<pf-donut-pct-chart config="configOrientationCenter" data="dataOrientationCenter"></pf-donut-pct-chart>
</div>
<div class="col-md-4 text-center">
<label>Sized with orientation right 'configLabel'</label>
<p class="text-left">
<pf-donut-pct-chart config="configOrientationRight" data="dataOrientationRight"></pf-donut-pct-chart>
</p>
</div>
</div>

Expand All @@ -120,7 +159,7 @@
</div>
<div class="row">
<div class="col-md-3">
<form role="form"">
<form role="form">
<div class="form-group">
<label class="checkbox-inline">
<input type="checkbox" ng-model="custData.dataAvailable">Data Available</input>
Expand All @@ -144,6 +183,7 @@

<file name="script.js">
angular.module( 'patternfly.charts' ).controller( 'ChartCtrl', function( $scope, $interval ) {
// start demo 1st row
$scope.configErr = {
'chartId': 'chartErr',
'units': 'GB',
Expand Down Expand Up @@ -200,14 +240,15 @@

$scope.configNoThresh = {
'chartId': 'chartNoThresh',
'units': 'GB',
'units': 'GB'
};

$scope.dataNoThresh = {
'used': '750',
'total': '1000'
};

//start demo 2nd row
$scope.usedConfig = {
'chartId': 'usedChart',
'units': 'GB',
Expand Down Expand Up @@ -260,6 +301,76 @@

$scope.noLabel = "none";

//start demo 3rd row
$scope.configOrientationLeft = {
'units': 'GB',
'thresholds':{'warning':'60','error':'90'},
'labelConfig': {
'orientation': 'left',
'labelFn': function () {
return "<strong>Lorem ipsum</strong><br/>" + $scope.dataOrientationLeft.used + " GB used";
}
},
'size': {
'height': 85,
'width': 85
},
'centerLabelFn': function () {
return $scope.dataOrientationLeft.used + "GB";
}
};

$scope.dataOrientationLeft = {
'used': '350',
'total': '1000'
};

$scope.configOrientationCenter = {
'units': 'GB',
'thresholds':{'warning':'60','error':'90'},
'labelConfig': {
'label': 'available',
'units': 'GB',
'title': 'Lorem ipsum,'
},
'size': {
'height': 115,
'width': 115
},
'centerLabelFn': function () {
return $scope.dataOrientationCenter.used + "GB";
}
};

$scope.dataOrientationCenter = {
'used': '350',
'total': '1000'
};

$scope.configOrientationRight = {
'units': 'GB',
'thresholds':{'warning':'60','error':'90'},
'labelConfig': {
'orientation': 'right',
'labelFn': function () {
return "<strong>Lorem ipsum</strong><br/>" + $scope.dataOrientationRight.percent + "% used";
}
},
'size': {
'height': 85,
'width': 85
},
'centerLabelFn': function () {
return $scope.dataOrientationRight.percent + "%";
}
};

$scope.dataOrientationRight = {
'used': '350',
'total': '1000'
};

//start demo 4th row
$scope.custConfig = {
'chartId': 'custChart',
'units': 'MHz',
Expand Down Expand Up @@ -289,3 +400,4 @@
</file>
</example>
*/

16 changes: 15 additions & 1 deletion test/charts/donut/donut-pct.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ describe('Directive: pfDonutPctChart', function() {
beforeEach(function() {
$scope.config = {
'units': 'MHz',
'thresholds':{'warning':'75.0','error':'90.00'}
'thresholds':{'warning':'75.0','error':'90.00'},
'labelConfig': {
'orientation': 'right',
'title': 'Lorem Ipsum',
'label': 'available',
'units': 'MHz'
}
};

$scope.data = {
Expand All @@ -41,6 +47,13 @@ describe('Directive: pfDonutPctChart', function() {
element = compileDonut('<pf-donut-pct-chart config="config" data="data" center-label="cntrLabel"></pf-donut-pct-chart>');
};

it("should have an external label", function() {
compileSimpleDonut();
expect(element.find('.pct-donut-chart-pf-right').length).toEqual(1);
expect(element.find('.pct-donut-chart-pf-label')[0].innerText).toContain('Lorem Ipsum');
expect(element.find('.pct-donut-chart-pf-label > span')[0].innerText).toContain('50 MHz available');
});

it("should trigger error threshold", function() {
compileSimpleDonut();
expect(ctrl.statusDonutColor().pattern[0]).toBe('#cc0000'); //red
Expand Down Expand Up @@ -133,3 +146,4 @@ describe('Directive: pfDonutPctChart', function() {
});

});