fix🛠️: 日期排序bug & pie样式更改

This commit is contained in:
clz 2022-09-13 15:29:37 +08:00
parent 7afb38b831
commit 8460b89711
5 changed files with 65 additions and 35 deletions

View File

@ -1,6 +1,6 @@
#root { #root {
height: 100%; height: 100%;
max-width: 1280px; /* max-width: 1280px; */
margin: 0 auto; margin: 0 auto;
/* padding: 2rem; */ /* padding: 2rem; */
/* text-align: center; */ /* text-align: center; */

View File

@ -1,10 +1,12 @@
import { import {
useRef, useRef,
useMemo, useMemo,
useEffect useEffect,
useCallback
} from 'react'; } from 'react';
import echarts, { ECOption } from '../config'; import echarts, { ECOption } from '../config';
import styles from "./indx.module.scss" import styles from "./indx.module.scss"
import dayjs from "dayjs"
type BarData = { type BarData = {
x: string | number x: string | number
@ -21,6 +23,13 @@ export const Bar = (props: IProps) => {
const { data, title, subTitle } = props; const { data, title, subTitle } = props;
// 获取数据 // 获取数据
const chartRef = useRef<HTMLDivElement>(null); const chartRef = useRef<HTMLDivElement>(null);
const colors = useCallback((value: number) => {
if (value < 50) { return "#008000" }
else if (value < 100) { return "#FFA500" }
else { return "#8B0000" }
}, [])
const option: ECOption = useMemo(() => { const option: ECOption = useMemo(() => {
return { return {
title: { title: {
@ -30,20 +39,29 @@ export const Bar = (props: IProps) => {
}, },
xAxis: { xAxis: {
type: 'category', type: 'category',
data: data.map(item => item.x), data: data.map(item => {
return dayjs(item.x).format("MM-DD")
}),
}, },
yAxis: { yAxis: {
type: "value" type: "value"
}, },
series: [ series: [
{ {
data: data.map(item => item.y), data: data.map(item => {
return {
value: item.y,
itemStyle: {
color: colors(item.y)
}
}
}),
type: "bar", type: "bar",
label: { label: {
show: true, show: true,
position: "top", position: "top",
margin: 8 margin: 8
} },
}, },
], ],
tooltip: { tooltip: {

View File

@ -3,7 +3,7 @@ import {
useMemo, useMemo,
useEffect useEffect
} from "react" } from "react"
import echarts, {ECOption} from '../config'; import echarts, { ECOption } from '../config';
import styles from "./index.module.scss" import styles from "./index.module.scss"
type PieData = { type PieData = {
@ -20,7 +20,7 @@ interface IProps {
export default function Pie(props: IProps) { export default function Pie(props: IProps) {
// 获取数据 // 获取数据
const {data, title, subTitle} = props const { data, title, subTitle } = props
const chartRef = useRef<HTMLDivElement>(null) const chartRef = useRef<HTMLDivElement>(null)
// 设置图标配置 // 设置图标配置
@ -43,16 +43,13 @@ export default function Pie(props: IProps) {
{ {
// name: `${year}-${month}`, // name: `${year}-${month}`,
type: "pie", type: "pie",
data: data.map(item => { radius: ['40%', '70%'],
return { // roseType: "radius",
name: item.x,
value: item.y
}
}),
radius: [20, 100],
roseType: "radius",
itemStyle: { itemStyle: {
borderRadius: 5 borderRadius: 10,
borderColor: '#fff',
borderWidth: 2,
}, },
label: { label: {
show: true, show: true,
@ -65,6 +62,12 @@ export default function Pie(props: IProps) {
show: true show: true
} }
}, },
data: data.map(item => {
return {
name: item.x,
value: item.y
}
}),
}, },
], ],
} }
@ -72,7 +75,7 @@ export default function Pie(props: IProps) {
// 挂载echarts // 挂载echarts
const pie = useMemo(() => { const pie = useMemo(() => {
return chartRef.current ? echarts.init(chartRef.current, undefined, {renderer: "svg"}) : null return chartRef.current ? echarts.init(chartRef.current, undefined, { renderer: "svg" }) : null
}, [chartRef.current]) }, [chartRef.current])
// 设置pie选项 // 设置pie选项
useEffect(() => { useEffect(() => {

View File

@ -1,26 +1,35 @@
import styles from "./Home.module.scss" import styles from "./Home.module.scss"
import * as R from 'ramda' import * as R from 'ramda'
import {BillType, IBill} from "../../model" import { BillType, IBill } from "../../model"
import Bar from "../../components/charts/bar" import Bar from "../../components/charts/bar"
import {useContext, useEffect, useState} from "react"; import { useContext, useEffect, useState } from "react";
import {BillContext} from "../../store"; import { BillContext } from "../../store";
import {observer} from "mobx-react-lite"; import { observer } from "mobx-react-lite";
import Pie from "../../components/charts/pie"; import Pie from "../../components/charts/pie";
import {Card, DatePicker, Radio, Space} from "antd"; import { Card, DatePicker, Radio, Space } from "antd";
import moment from 'moment'; import moment from 'moment';
import 'moment/locale/zh-cn'; import 'moment/locale/zh-cn';
import dayjs from 'dayjs'
const Home = () => { const Home = () => {
const billStore = useContext(BillContext) const billStore = useContext(BillContext)
const transformer = (record: Record<string, IBill[]>) => { const transformer = (record: Record<string, IBill[]>) => {
return R.map((key: string) => { const funcs = R.compose(
R.sort((a: { x: string, y: number }, b) => {
const date1 = dayjs(a.x).toDate().getTime()
const date2 = dayjs(b.x).toDate().getTime()
return date1 - date2
}),
R.map((key: string) => {
const moneys = record[key].map(bill => bill.money) const moneys = record[key].map(bill => bill.money)
return { return {
x: key, x: key,
y: Number(R.sum(moneys).toFixed(2)), y: Number(R.sum(moneys).toFixed(2)),
} }
})(R.keys(record)) }))
return funcs(R.keys(record))
} }
const now = new Date(); const now = new Date();
@ -38,8 +47,8 @@ const Home = () => {
} }
const typeOpt = [ const typeOpt = [
{label: '支出', value: BillType.consume}, { label: '支出', value: BillType.consume },
{label: '收入', value: BillType.income}, { label: '收入', value: BillType.income },
]; ];
const [billType, setBillType] = useState(BillType.consume) const [billType, setBillType] = useState(BillType.consume)
@ -65,8 +74,8 @@ const Home = () => {
</Card> </Card>
</Space> </Space>
</div> </div>
<Bar data={transformer(billStore.groupByDate(billType))}/> <Bar data={transformer(billStore.groupByDate(billType))} />
<Pie data={transformer(billStore.groupByClass(billType))}/> <Pie data={transformer(billStore.groupByClass(billType))} />
</div> </div>
) )
} }

View File

@ -1,6 +1,6 @@
import {Button, DatePicker, Input, InputNumber, message, Radio, Select, Space, Table, Tag} from "antd"; import {Button, DatePicker, Input, InputNumber, message, Radio, Select, Space, Table, Tag} from "antd";
import {ArrowDownOutlined, CloudUploadOutlined, DeleteOutlined,} from "@ant-design/icons"; import {ArrowDownOutlined, CloudUploadOutlined, DeleteOutlined,} from "@ant-design/icons";
import {useCallback, useContext, useEffect, useMemo, useRef, useState} from "react"; import {useContext, useEffect, useRef, useState} from "react";
import {BillType, EmptyBill, IBill} from "../../model"; import {BillType, EmptyBill, IBill} from "../../model";
import moment from "moment/moment"; import moment from "moment/moment";
import {BillContext} from "../../store"; import {BillContext} from "../../store";