FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix(Donut Chart): Add fixed point notation into percentage donut charts by amirfefer · Pull Request #2375 · patternfly/patternfly-react · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
    • DonutChart.js
      • DonutChartStory.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import { getComposer } from './ChartConstants';
const { pfSetDonutChartTitle } = patternfly;
const colIndexOfMaxValue = columns => columns.reduce((iMax, x, i, arr) => (x[1] > arr[iMax][1] ? i : iMax), 0);

const truncateNum = (num, precision) => {
const pointNotation = num.toString().split('.');
if (pointNotation.length === 1) return pointNotation[0];
return `${pointNotation[0]}.${pointNotation[1].slice(0, precision)}`;
};

const setDonutTitle = obj => {
let primary;
let secondary;

const { props } = obj;
const { data, title = {} } = props;
const { type, precision = 0 } = title;
const { columns } = data;
const sum = columns.reduce((acc, x) => acc + x[1], 0);
const iMax = colIndexOfMaxValue(columns);
const percentage = (100 * columns[iMax][1]) / sum;

switch (title.type) {
switch (type) {
case 'percent':
primary = `${Math.round((100 * columns[iMax][1]) / sum).toString()}%`;
[secondary] = columns[iMax];
primary = precision ? `${truncateNum(percentage, precision)}%` : `${Math.round(percentage)}%`;
break;
case 'max':
primary = Math.round(columns[iMax][1]).toString();
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ const donutData = {
columns: [['Dogs', 2], ['Cats', 2], ['Fish', 3], ['Hamsters', 1]]
};

const donutPercentageData = {
columns: [['Out of sync hosts', 1], ['Hosts with no reports', 1000]],
colors: {
'Out of sync hosts': patternfly.pfPaletteColors.blue,
'Hosts with no reports': patternfly.pfPaletteColors.grey
}
};

const donutRightConfigData = donutData;
const donutRightConfigLegend = {
show: true,
Expand Down Expand Up @@ -67,6 +75,20 @@ const donutChart = stories => {
/>
</div>

<h2>Donut Chart - Percentage</h2>
<div>
<DonutChart
id="donunt-chart-3"
size={{
width: 210,
height: 210
}}
data={donutPercentageData}
tooltip={donutConfigTooltip}
title={{ type: 'percent', precision: 1 }}
/>
</div>

<h2>Donut Chart - Relationship to a Whole</h2>
<div>
<DonutChart
Expand Down

Back | FazBrowse Home | New Git URL