Browse Source

Tooltip: show percent instead of value

corpglory-dev 6 years ago
parent
commit
dbec66b3d6
1 changed files with 5 additions and 2 deletions
  1. 5 2
      packages/grafana-ui/src/components/PieChart/PieChart.tsx

+ 5 - 2
packages/grafana-ui/src/components/PieChart/PieChart.tsx

@@ -1,5 +1,6 @@
 import React, { PureComponent } from 'react';
 import React, { PureComponent } from 'react';
 import { select, pie, arc, event } from 'd3';
 import { select, pie, arc, event } from 'd3';
+import { sum } from 'lodash';
 
 
 import { GrafanaThemeType } from '../../types';
 import { GrafanaThemeType } from '../../types';
 import { Themeable } from '../../index';
 import { Themeable } from '../../index';
@@ -58,6 +59,9 @@ export class PieChart extends PureComponent<Props> {
     const names = datapoints.map(datapoint => datapoint.name);
     const names = datapoints.map(datapoint => datapoint.name);
     const colors = datapoints.map(datapoint => datapoint.color);
     const colors = datapoints.map(datapoint => datapoint.color);
 
 
+    const total = sum(data) || 1;
+    const percents = data.map((item: number) => (item / total) * 100);
+
     const width = this.containerElement.offsetWidth;
     const width = this.containerElement.offsetWidth;
     const height = this.containerElement.offsetHeight;
     const height = this.containerElement.offsetHeight;
     const radius = Math.min(width, height) / 2;
     const radius = Math.min(width, height) / 2;
@@ -91,8 +95,7 @@ export class PieChart extends PureComponent<Props> {
       .style('stroke-width', `${strokeWidth}px`)
       .style('stroke-width', `${strokeWidth}px`)
       .on('mouseover', (d: any, idx: any) => {
       .on('mouseover', (d: any, idx: any) => {
         select(this.tooltipElement).style('opacity', 1);
         select(this.tooltipElement).style('opacity', 1);
-        // TODO: show percents
-        select(this.tooltipValueElement).text(`${names[idx]} (${data[idx]})`);
+        select(this.tooltipValueElement).text(`${names[idx]} (${percents[idx].toFixed(2)}%)`);
       })
       })
       .on('mousemove', () => {
       .on('mousemove', () => {
         select(this.tooltipElement)
         select(this.tooltipElement)