Skip to content

Commit 53f4664

Browse files
committed
stock chart
1 parent 9d27e68 commit 53f4664

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

src/pages/stocks/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { stockPageList, StockQuery } from '../../services/stock'
55
import { data2PageData, pageData2Params } from '../../utils/tools'
66
import { Stock, EMarket, EBlock } from '../../types/stock'
77
import { object2Options } from '@utils/tools'
8+
import { StockChart } from './components/stockChart'
89

910
const StockList: React.FC<ICommonProps> = ({history}) => {
1011

@@ -34,6 +35,7 @@ const StockList: React.FC<ICommonProps> = ({history}) => {
3435

3536

3637
return <Spin spinning={loading}>
38+
<StockChart/>
3739
<Form className="mgb16" form={form}>
3840
<Row gutter={16}>
3941
<Col span={6}>

src/services/stock.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import $http from '@utils/http'
22
import { pageData2Params } from '@utils/tools'
3-
import { Stock } from 'types/stock'
3+
import { EBlock, Stock } from 'types/stock'
44

55

66
export type StockQuery = Omit<Stock & IPageParams, 'id' | 'amount'> & { noPage: boolean }
@@ -11,4 +11,9 @@ export const stockPageList = (params: Partial<StockQuery> = pageData2Params()) =
1111

1212
export const getStockDetail = (id: number) => {
1313
return $http.get<any, IResponseData<Stock>>(`/api/stocks/${id}`).then(res => res.data)
14+
}
15+
16+
17+
export const getStockChartCount = () => {
18+
return $http.get<{total: number, block: EBlock}[]>(`/api/stock/chartCount`).then(res => res.data)
1419
}

0 commit comments

Comments
 (0)