|
| 1 | +import React, { useState, useEffect, useRef } from 'react' |
| 2 | +import { Row, Col, Form, Spin } from 'antd' |
| 3 | +import * as Echart from 'echarts/core' |
| 4 | +import { |
| 5 | + LegendComponent, |
| 6 | + TitleComponent, |
| 7 | + TooltipComponent, |
| 8 | + TitleComponentOption, |
| 9 | + TooltipComponentOption, |
| 10 | + LegendComponentOption |
| 11 | +} from 'echarts/components' |
| 12 | +import { PieChart, PieSeriesOption } from 'echarts/charts' |
| 13 | +import { CanvasRenderer } from 'echarts/renderers'; |
| 14 | +import { LabelLayout } from 'echarts/features' |
| 15 | +import { getStockChartCount } from '../../../services/stock' |
| 16 | +import { EBlock } from 'types/stock' |
| 17 | + |
| 18 | +Echart.use([ |
| 19 | + PieChart, |
| 20 | + LegendComponent, |
| 21 | + TitleComponent, |
| 22 | + TooltipComponent, |
| 23 | + CanvasRenderer, |
| 24 | + LabelLayout |
| 25 | +]) |
| 26 | + |
| 27 | +type EchartsOption = Echart.ComposeOption< |
| 28 | + TitleComponentOption | |
| 29 | + TooltipComponentOption | |
| 30 | + LegendComponentOption | |
| 31 | + PieSeriesOption> |
| 32 | + |
| 33 | +export const StockChart: React.FC = () => { |
| 34 | + |
| 35 | + const chartRef = useRef<HTMLDivElement>() |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + const stockChart = Echart.init(chartRef.current) |
| 39 | + let options: EchartsOption; |
| 40 | + getStockChartCount().then(res => { |
| 41 | + options = { |
| 42 | + title: { |
| 43 | + text: 'Stock Blocks', |
| 44 | + subtext: 'China A-Share Market', |
| 45 | + left: 'center' |
| 46 | + }, |
| 47 | + tooltip: { |
| 48 | + trigger: 'item' |
| 49 | + }, |
| 50 | + legend: { |
| 51 | + orient: 'vertical', |
| 52 | + left: 'left' |
| 53 | + }, |
| 54 | + series: [ |
| 55 | + { |
| 56 | + name: 'Block', |
| 57 | + type: 'pie', |
| 58 | + radius: '50%', |
| 59 | + data: res.map(i => ({name: EBlock[i.block], value: i.total})) |
| 60 | + }, |
| 61 | + { |
| 62 | + emphasis: { |
| 63 | + itemStyle: { |
| 64 | + shadowBlur: 10, |
| 65 | + shadowOffsetX: 0, |
| 66 | + shadowColor: 'rgba(0,0,0,0.5)' |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + ] |
| 71 | + } |
| 72 | + stockChart.setOption(options) |
| 73 | + }) |
| 74 | + }, []) |
| 75 | + |
| 76 | + |
| 77 | + return <Row className="mgb8"> |
| 78 | + <Col span={24}> |
| 79 | + <div className='chart-w' ref={chartRef}></div> |
| 80 | + </Col> |
| 81 | + </Row> |
| 82 | +} |
0 commit comments