Skip to content

Commit 59d9dca

Browse files
committed
chart
1 parent b99443a commit 59d9dca

File tree

8 files changed

+182
-0
lines changed

8 files changed

+182
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"antd": "^3.10.4",
1515
"axios": "^0.18.0",
16+
"echarts": "^4.2.0-rc.2",
1617
"mobx": "^5.6.0",
1718
"mobx-react": "^5.3.6",
1819
"quill": "^1.3.6",
@@ -28,6 +29,7 @@
2829
"@babel/preset-env": "^7.2.0",
2930
"@babel/preset-react": "^7.0.0",
3031
"@babel/preset-typescript": "^7.1.0",
32+
"@types/echarts": "^4.1.3",
3133
"@types/quill": "^2.0.1",
3234
"@types/react": "^16.7.7",
3335
"@types/react-dom": "^16.0.10",

src/assets/scss/app.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,10 @@
157157
.blue{
158158
background-color: #3399ff
159159
}
160+
}
161+
162+
/* chart-w */
163+
.chart-w{
164+
height: 400px;
165+
border: 1px solid #eee;
160166
}

src/components/shared/sider.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const MenuList = [{
4141
key: '5-0', title: '双色球', path: '/home/lottery-balls', icon: 'dollar'
4242
},{
4343
key: '5-1', title: '趋势图', path: '/home/lottery-trend', icon: 'fund'
44+
},{
45+
key: '5-2', title: '统计图', path: '/home/lottery-chart', icon: 'bar-chart'
4446
}]
4547
},{
4648
key:'99', title:'Demo', path: '/home/demos', icon:'bulb'

src/pages/lottery/ballChart.tsx

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import * as React from 'react'
2+
import {inject, observer} from 'mobx-react'
3+
import { Row, Col, Form, Button } from 'antd';
4+
import { IBall } from '@models/ball'
5+
import * as Echart from 'echarts'
6+
// const Echart = require('echarts/lib/echarts')
7+
// 引入柱状图
8+
require('echarts/lib/chart/bar');
9+
// 引入提示框和标题组件
10+
require('echarts/lib/component/tooltip');
11+
require('echarts/lib/component/title');
12+
13+
@inject('ballChartStore')
14+
@observer
15+
export default class BallChart extends React.Component<IProps> {
16+
17+
chartRef: any = React.createRef()
18+
ballCountChart: Echart.ECharts
19+
20+
state = {
21+
visible: false
22+
}
23+
24+
redBalls: number[] = [
25+
1,2,3,4,5,6,7,8,9,10,
26+
11,12,13,14,15,16,17,18,19,20,
27+
21,22,23,24,25,26,27,28,29,30,
28+
31,32,33
29+
]
30+
blueBalls: number[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
31+
32+
setBall = (data: IBall, num: number, type: 'red'|'blue') => {
33+
const color = type === 'red' ? '#f54646' : '#3399ff'
34+
}
35+
36+
setColor = (num: number, type: string) => {
37+
return <div style={{textAlign: 'center', color: type}}>{num}</div>
38+
}
39+
40+
showRule = () => {
41+
this.setState({visible: !this.state.visible})
42+
}
43+
44+
componentDidMount() {
45+
this.ballCountChart = Echart.init(this.chartRef.current)
46+
const { fetchChartData } = this.props.ballChartStore
47+
48+
fetchChartData((reds:[any], blues:[any]) => {
49+
let list = [...reds, ...blues]
50+
let xData = list.map((v:any, i:number) => {
51+
return v.name
52+
})
53+
let yData = list.map((v:any, i:number) => {
54+
return v.value
55+
})
56+
this.ballCountChart.setOption({
57+
title: {
58+
text: '历史出现次数'
59+
},
60+
tooltip: {},
61+
xAxis: {
62+
data: xData
63+
},
64+
yAxis: {},
65+
series: [{
66+
name: '次数',
67+
type: 'bar',
68+
data: yData
69+
}]
70+
})
71+
})
72+
}
73+
74+
render(){
75+
return <React.Fragment>
76+
<div className="search-form">
77+
<h3>红蓝球历史出现次数</h3>
78+
<Row gutter={24}>
79+
<Col span={24}>
80+
<div className='chart-w' ref={this.chartRef}>
81+
</div>
82+
</Col>
83+
</Row>
84+
</div>
85+
</React.Fragment>
86+
87+
}
88+
}

src/pages/lottery/ballChartStore.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { observable, action, autorun, runInAction, computed } from 'mobx';
2+
import { GRAPHQL_API } from '@constants/index';
3+
4+
const queryChart = `
5+
fragment common on ballChartType { name value }
6+
query ball{
7+
ballCount{reds{...common},blues{...common}}
8+
}`
9+
10+
11+
class ballChartStore {
12+
@observable loading: boolean = false
13+
redBalls: number[] = [
14+
1,2,3,4,5,6,7,8,9,10,
15+
11,12,13,14,15,16,17,18,19,20,
16+
21,22,23,24,25,26,27,28,29,30,
17+
31,32,33
18+
]
19+
blueBalls: number[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
20+
@observable reds: number[] = []
21+
@observable blues: number[] = []
22+
23+
chart: HTMLElement = null
24+
25+
setChart = (ref: HTMLElement) => {
26+
this.chart = ref
27+
}
28+
29+
@action fetchChartData = (cb: Function) => {
30+
this.loading = true
31+
$http.post(GRAPHQL_API, {
32+
query: queryChart,
33+
variables: {}
34+
}).then((res: any) => {
35+
const {ballCount} = res.data
36+
runInAction(() => {
37+
this.loading = false
38+
// this.reds = ballCount.reds
39+
// this.blues = ballCount.blues
40+
console.log(ballCount)
41+
cb && cb(ballCount.reds, ballCount.blues)
42+
})
43+
}, err => {
44+
runInAction(() => {
45+
this.loading = false
46+
})
47+
})
48+
}
49+
50+
}
51+
52+
export default new ballChartStore()

src/routes/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const BallList = lazy(() => import( /* webpackChunkName:"ballList" */ '@pages/lo
2929
const BallCreate = lazy(() => import( /* webpackChunkName:"ballCreate" */ '@pages/lottery/ballCreate'))
3030
const BallEdit = lazy(() => import( /* webpackChunkName:"ballEdit" */ '@pages/lottery/ballEdit'))
3131
const BallTrend = lazy(() => import( /* webpackChunkName:"ballTrend" */ '@pages/lottery/ballTrend'))
32+
const BallChart = lazy(() => import( /* webpackChunkName:"ballChart" */ '@pages/lottery/ballChart'))
3233

3334
const Demo = lazy(() => import( /* webpackChunkName:"demo" */ '@pages/demo/demo'))
3435

@@ -154,6 +155,11 @@ export const routes: RouteProps[] = [
154155
exact: true,
155156
component: BallTrend
156157
},
158+
{
159+
path: '/home/lottery-chart',
160+
exact: true,
161+
component: BallChart
162+
},
157163
{
158164
path: '/home/demos',
159165
exact: true,

src/stores/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import ballListStore from '@pages/lottery/ballListStore';
2222
import ballCreateStore from '@pages/lottery/ballCreateStore';
2323
import ballEditStore from '@pages/lottery/ballEditStore';
2424
import ballTrendStore from '@pages/lottery/ballTrendStore';
25+
import ballChartStore from '@pages/lottery/ballChartStore';
2526

2627
import demoStore from '../pages/demo/store';
2728

@@ -52,6 +53,7 @@ export default {
5253
ballCreateStore,
5354
ballEditStore,
5455
ballTrendStore,
56+
ballChartStore,
5557

5658
demoStore
5759
}

yarn.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,13 @@
720720
lodash "^4.17.10"
721721
to-fast-properties "^2.0.0"
722722

723+
"@types/echarts@^4.1.3":
724+
version "4.1.3"
725+
resolved "https://registry.yarnpkg.com/@types/echarts/-/echarts-4.1.3.tgz#dd8be2f197ac9778175cddbdc2228ada8f304b66"
726+
integrity sha512-1w4s4MisDNz8RlznvxHHiJ1eAE5sLQrbf+D5A27rECku4VH/LqDzMFk9Svd16VJI6yag5Zz2ROy12e6R3kC18g==
727+
dependencies:
728+
"@types/zrender" "*"
729+
723730
"@types/history@*":
724731
version "4.7.2"
725732
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.2.tgz#0e670ea254d559241b6eeb3894f8754991e73220"
@@ -776,6 +783,11 @@
776783
"@types/prop-types" "*"
777784
csstype "^2.2.0"
778785

786+
"@types/zrender@*":
787+
version "4.0.0"
788+
resolved "https://registry.yarnpkg.com/@types/zrender/-/zrender-4.0.0.tgz#a6806f12ec4eccaaebd9b0d816f049aca6188fbd"
789+
integrity sha512-s89GOIeKFiod2KSqHkfd2rzx+T2DVu7ihZCBEBnhFrzvQPUmzvDSBot9Fi1DfMQm9Odg+rTqoMGC38RvrwJK2w==
790+
779791
"@webassemblyjs/[email protected]":
780792
version "1.7.11"
781793
resolved "https://registry.yarnpkg.com/@webassemblyjs/ast/-/ast-1.7.11.tgz#b988582cafbb2b095e8b556526f30c90d057cace"
@@ -2648,6 +2660,13 @@ ecc-jsbn@~0.1.1:
26482660
jsbn "~0.1.0"
26492661
safer-buffer "^2.1.0"
26502662

2663+
echarts@^4.2.0-rc.2:
2664+
version "4.2.0-rc.2"
2665+
resolved "https://registry.yarnpkg.com/echarts/-/echarts-4.2.0-rc.2.tgz#6a98397aafa81b65cbf0bc15d9afdbfb244df91e"
2666+
integrity sha512-5Y4Kyi4eNsRM9Cnl7Q8C6PFVjznBJv1VIiMm/VSQ9zyqeo+ce1695GqUd9v4zfVx+Ow1gnwMJX67h0FNvarScw==
2667+
dependencies:
2668+
zrender "4.0.5"
2669+
26512670
26522671
version "1.1.1"
26532672
resolved "https://registry.yarnpkg.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -8053,3 +8072,8 @@ yargs@^7.0.0:
80538072
which-module "^1.0.0"
80548073
y18n "^3.2.1"
80558074
yargs-parser "^5.0.0"
8075+
8076+
8077+
version "4.0.5"
8078+
resolved "https://registry.yarnpkg.com/zrender/-/zrender-4.0.5.tgz#6e8f738971ce2cd624aac82b2156729b1c0e5a82"
8079+
integrity sha512-SintgipGEJPT9Sz2ABRoE4ZD7Yzy7oR7j7KP6H+C9FlbHWnLUfGVK7E8UV27pGwlxAMB0EsnrqhXx5XjAfv/KA==

0 commit comments

Comments
 (0)