From 81c140b4256cf9bf6f370bd6a869e54ecab23254 Mon Sep 17 00:00:00 2001 From: corpglory-dev Date: Wed, 27 Feb 2019 20:07:58 +0300 Subject: [PATCH] Initial rendering --- .../src/components/Piechart/Piechart.tsx | 51 +++++++++++++++++-- public/sass/_grafana.scss | 1 + public/sass/components/_panel_piechart.scss | 11 ++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 public/sass/components/_panel_piechart.scss diff --git a/packages/grafana-ui/src/components/Piechart/Piechart.tsx b/packages/grafana-ui/src/components/Piechart/Piechart.tsx index 809221eff22..7b5a7ce0aa6 100644 --- a/packages/grafana-ui/src/components/Piechart/Piechart.tsx +++ b/packages/grafana-ui/src/components/Piechart/Piechart.tsx @@ -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 { } 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 { top: '10px', margin: 'auto', }} - ref={element => (this.canvasElement = element)} - /> + > + (this.canvasElement = element)} /> + ); } diff --git a/public/sass/_grafana.scss b/public/sass/_grafana.scss index 8928523f2be..8897e40b655 100644 --- a/public/sass/_grafana.scss +++ b/public/sass/_grafana.scss @@ -54,6 +54,7 @@ @import 'components/panel_alertlist'; @import 'components/panel_dashlist'; @import 'components/panel_gettingstarted'; +@import 'components/panel_piechart'; @import 'components/panel_pluginlist'; @import 'components/panel_singlestat'; @import 'components/panel_table'; diff --git a/public/sass/components/_panel_piechart.scss b/public/sass/components/_panel_piechart.scss new file mode 100644 index 00000000000..e09cb31b92e --- /dev/null +++ b/public/sass/components/_panel_piechart.scss @@ -0,0 +1,11 @@ +.piechart-panel { + position: relative; + display: table; + width: 100%; + height: 100%; + + canvas { + width: 100%; + height: 100%; + } +}