grafana使用d3开发panel

2020-10-18  本文已影响0人  Nick_4438

使用d3创建 panel插件

简介

本文讲解如何使用d3创建一个panel插件

import React from 'react';
import { PanelProps } from '@grafana/data';
import { SimpleOptions } from 'types';
import {  useTheme } from '@grafana/ui';

interface Props extends PanelProps<SimpleOptions> {}

export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
  const theme = useTheme();
  const values = [4, 8, 15, 16, 23, 42];
  const barHeight = height / values.length;
  return (
    <svg width={width} height={height}>
    <g>
      {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={value} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))}
    </g>
  </svg>
  );
};

操作步骤

yarn add --dev @types/d3
import * as d3 from 'd3';

const scale = d3
  .scaleLinear()
  .domain([0, d3.max(values) || 0.0])
  .range([0, width]);
  return (
    <svg width={width} height={height}>
    <g>
      {/* {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={value} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))} */}
       {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={scale(value)} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))}
    </g>
  </svg>
  );
import React from 'react';
import { PanelProps } from '@grafana/data';
import { SimpleOptions } from 'types';
import {  useTheme } from '@grafana/ui';
import * as d3 from 'd3';


interface Props extends PanelProps<SimpleOptions> {}

export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
  const theme = useTheme();
  const values = [4, 8, 15, 16, 23, 42];
  const barHeight = height / values.length;

  const scale = d3
  .scaleLinear()
  .domain([0, d3.max(values) || 0.0])
  .range([0, width]);
  
  return (
    <svg width={width} height={height}>
    <g>
      {/* {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={value} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))} */}
       {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={scale(value)} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))}
    </g>
  </svg>
  );
};


yarn dev
export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) => {
  const theme = useTheme();
  const values = [4, 8, 15, 16, 23, 42];
  const padding = 20;
  const chartHeight = height - padding;
  const barHeight = chartHeight / values.length;

  const scale = d3
  .scaleLinear()
  .domain([0, d3.max(values) || 0.0])
  .range([0, width]);
  // 声明一个坐标
  const axis = d3.axisBottom(scale);
  
  return (
    <svg width={width} height={height}>
      {/* 坐标渲染 */}
    <g
      transform={`translate(0, ${chartHeight})`}
      ref={node => {
        d3.select(node).call(axis as any);
      }}
    />
    <g>
       {values.map((value, i) => (
        <rect x={0} y={i * barHeight} width={scale(value)} height={barHeight - 1} fill={theme.palette.greenBase} />
      ))}
    </g>
  </svg>
  );
};
image.png
上一篇下一篇

猜你喜欢

热点阅读