|
|
@@ -1,8 +1,15 @@
|
|
|
import React, { PureComponent } from 'react';
|
|
|
+import * as d3 from 'd3';
|
|
|
|
|
|
import { GrafanaThemeType } from '../../types';
|
|
|
import { Themeable } from '../../index';
|
|
|
|
|
|
+
|
|
|
+export enum PiechartType {
|
|
|
+ PIE = 'pie',
|
|
|
+ DONUT = 'donut'
|
|
|
+}
|
|
|
+
|
|
|
export interface PiechartDataPoint {
|
|
|
value: number;
|
|
|
name: string;
|
|
|
@@ -15,7 +22,7 @@ export interface Props extends Themeable {
|
|
|
datapoints: PiechartDataPoint[];
|
|
|
|
|
|
unit: string;
|
|
|
- pieType: string;
|
|
|
+ pieType: PiechartType;
|
|
|
strokeWidth: number;
|
|
|
}
|
|
|
|
|
|
@@ -39,7 +46,42 @@ export class Piechart extends PureComponent<Props> {
|
|
|
}
|
|
|
|
|
|
draw() {
|
|
|
- // const { width, height, theme, value } = this.props;
|
|
|
+ const { datapoints, pieType, strokeWidth } = this.props;
|
|
|
+
|
|
|
+ const data = datapoints.map(datapoint => datapoint.value);
|
|
|
+ const colors = datapoints.map(datapoint => datapoint.color);
|
|
|
+
|
|
|
+ const width = this.canvasElement.width;
|
|
|
+ const height = this.canvasElement.height;
|
|
|
+ const radius = Math.min(width, height) / 2;
|
|
|
+
|
|
|
+ const innerRadius = pieType === PiechartType.PIE? 0: radius;
|
|
|
+
|
|
|
+ const context = this.canvasElement.getContext('2d');
|
|
|
+ context.translate(width / 2, height / 2);
|
|
|
+ context.globalAlpha = 0.5;
|
|
|
+
|
|
|
+ const pie = d3.pie();
|
|
|
+
|
|
|
+ const arcs = pie(data);
|
|
|
+ const arc = d3.arc()
|
|
|
+ .outerRadius(radius - 10)
|
|
|
+ .innerRadius(innerRadius)
|
|
|
+ .padAngle(0.03)
|
|
|
+ .context(context);
|
|
|
+
|
|
|
+ arcs.forEach((d, idx) => {
|
|
|
+ context.beginPath();
|
|
|
+ arc(d as any);
|
|
|
+ context.fillStyle = colors[idx];
|
|
|
+ context.fill();
|
|
|
+ });
|
|
|
+
|
|
|
+ context.globalAlpha = 1;
|
|
|
+ context.beginPath();
|
|
|
+ arcs.forEach(arc as any);
|
|
|
+ context.lineWidth = strokeWidth;
|
|
|
+ context.stroke();
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
@@ -54,8 +96,9 @@ export class Piechart extends PureComponent<Props> {
|
|
|
top: '10px',
|
|
|
margin: 'auto',
|
|
|
}}
|
|
|
- ref={element => (this.canvasElement = element)}
|
|
|
- />
|
|
|
+ >
|
|
|
+ <canvas ref={element => (this.canvasElement = element)} />
|
|
|
+ </div>
|
|
|
</div>
|
|
|
);
|
|
|
}
|