สร้างข้อมูลลงไปใน recharts สร้างแผนภูมิ k ใช้รายชื่อของ DTO

0

คำถาม

ฉันมี DTO สิ่งที่ถูกสร้างจากที่เหลือรูปแบบ api ใช้ Typescript:

export interface BillingSummaryDTO {
    paid?: number,
    outstanding?: number,
    pastDue?: number,
    cancelled?: number,
    createdAt?: Moment | null,
}

export async function getBillingSummary(): Promise<AxiosResponse<BillingSummaryDTO[]>> {
  return await axios.get<BillingSummaryDTO[]>(
      `${baseUrl}/management/billing/summary`
  );
}

ตัวอย่างเช่นผัง:

import {
  BarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
} from "recharts";
import {Box} from "@material-ui/core";

const data = [
  {
    name: "Jan",
    Chargebacks: 4000,
    Transactions: 2400,
    USD: 2400,
  },
  {
    name: "Feb",
    Chargebacks: 3000,
    Transactions: 1398,
    USD: 2210,
  },
  {
    name: "Mar",
    Chargebacks: 2000,
    Transactions: 9800,
    USD: 2290,
  },
  {
    name: "Apr",
    Chargebacks: 2780,
    Transactions: 3908,
    USD: 2000,
  },
  {
    name: "May",
    Chargebacks: 1890,
    Transactions: 4800,
    USD: 2181,
  },
  {
    name: "Jun",
    Chargebacks: 2390,
    Transactions: 3800,
    USD: 2500,
  },
  {
    name: "Jul",
    Chargebacks: 3490,
    Transactions: 4300,
    USD: 2100,
  },
  {
    name: "Aug",
    Chargebacks: 3490,
    Transactions: 4300,
    USD: 2100,
  },
  {
    name: "Sep",
    Chargebacks: 3490,
    Transactions: 4300,
    USD: 2100,
  },
];

const useStyles = makeStyles((theme) =>
  createStyles({
    root: {
      flexGrow: 1,
    },
    paper: {
      padding: theme.spacing(2),
      textAlign: "center",
      color: theme.palette.text.secondary,
    },
  })
);

const usePaperStyles = makeStyles((theme) =>
  createStyles({
    root: {
      display: "flex",
      flexWrap: "wrap",
      "& > *": {
        margin: theme.spacing(1),
        width: theme.spacing(16),
        height: theme.spacing(16),
      },
    },
  })
);

const useTimelineStyles = makeStyles((theme) => ({
  paper: {
    padding: "6px 16px",
  },
  secondaryTail: {
    backgroundColor: theme.palette.secondary.main,
  },
}));

export default function Billing() {
  const [click, setClick] = useState(false);
  const closeMobileMenu = () => setClick(false);
  const classes = useStyles();
  const classesPaper = usePaperStyles();
  const classesTimeline = useTimelineStyles();

  return (
    <>
      <Grid container justifyContent="center" alignItems="center">
        <Grid item xs={11}>
          {/*Padding on the top*/}
          <Box m="5rem" />
        </Grid>

        <Grid item xs={12} >
          <h4 className="chart-label">Invoices Summary</h4>
          <BarChart
            width={1000}
            height={300}
            data={data}
            margin={{
              top: 5,
              right: 30,
              left: 20,
              bottom: 5,
            }}
            barSize={30}
          >
            <XAxis
              dataKey="name"
              scale="point"
              padding={{ left: 10, right: 10 }}
            />
            <YAxis />
            <Tooltip />
            <Legend />
            <CartesianGrid strokeDasharray="3 3" />
            <Bar
              dataKey="Transactions"
              fill="#8884d8"
              background={{ fill: "#eee" }}
            />
          </BarChart>
        </Grid>
      </Grid>
    </>
  );
}

ยังไงฉันสามารถใช้ข้อมูลจาก BillingSummaryDTO[] สร้างตารางราย?

1

คำตอบที่ดีที่สุด

2

ทำงานที่แผนที่ตร BillingSummaryDTO[และ converts นมันเป็นข้อมูลรุ่น(เป็นที่มองเห็นในตัวแปร data)ที่คุณต้องกำหนดสำหรับ consumption ย <BarChart/> คือที่ใช้งานได้ใกล้เข้ามาแล้ว

export interface BarChartDataModel {
  name: string,
  Chargebacks: number,
  Transactions: number,
  USD: number,
}

const data : BarChartDataModel []

นี้ข้อมูลจะถูกใช้อยู่ <BarChart/> เป็


<BarChart
            data={data}
            // ....
          >
         // ...
 </BarChart>

เช่นฟังก์ชันจะตาม map reduce เข้าใกล้กับความช่วยเหลือเล็กน้อยจาก JS วันที่การจัดการห้องสมุดชอบ momentjs

4 ขั้นตอทางออก

  1. จัดการให้ทั้งค่า chronologically(เก่าแก่บิลแรก)สำหรับแผนที่ง่ายกว่า-ลดทีหลัง
  2. ถัค่าโดยปีในเก่ากันทุกคนเรียก'yearwise-เก่ากันทุก'
  3. ถังที่มีค่าอยู่ในแต่ละ yearwise-ถังเข้าเก่ากันทุกคนเรียก'monthwise-ถั'
  4. ลงทุนเงินในแต่ละ monthwise-ถังที่จะเป็นวัตถุต่อไปนี้ที่ BarChartDataModel ส่วนเชื่อมต่อ. คลังภาพของวัตถุต่อไปนี้ที่ BarChartDataModel ส่วนติดต่อเป็น data เราผ่านของเรา <BarChart/> ส่วนประกอบ
  5. กินนี้สิ่งแรมคลังภาพของเรา <BarChart/> ส่วนประกอบ

ขอรหัสสำหรับเดียวกันจะเป็น:

export interface BillingSummaryDTO {
    paid?: number,
    outstanding?: number,
    pastDue?: number,
    cancelled?: number,
    createdAt?: Moment | null,
}

export interface BarChartDataModel {
  name: string,
  Chargebacks: number,
  Transactions: number,
  USD: number,
}

export async function getBillingSummary(): Promise<AxiosResponse<BillingSummaryDTO[]>> {
  const response = await axios.get<BillingSummaryDTO[]>(
      `${baseUrl}/management/billing/summary`
  );
  // STEP 1 : Chronological ordering. Oldest bills will show first
  const chronologicallyOrdered = response.sort((a:BillingSummaryDTO,b:BillingSummaryDTO)=> a.createdAt - b.createdAt )

  // STEP 2 : Bucket by year
  const groupByYear = chronologicallyOrdered.reduce((yearwiseBills : any, bill:BillingSummaryDTO, currIdx:number)) => 
     {
        const year = moment(bill.createdAt).year().toString()
        if(!yearwiseBills[year]){ 
          yearwiseBills[year] = []
        } 
        yearwiseBills[year].push(bill)
        return yearwiseBills
     }
  ,{})

  // STEP 3 : In each yearwise bucket -> bucket by month
  const groupByMonth = Object.keys(groupByYear).map((year, yearwiseBills) => yearwiseBills.reduce((monthwiseBills: any, bill:BillingSummaryDTO, currIdx:number)) => 
     {
        const moment = moment(bill.createdAt).month().toString()
        if(!yearAcc[month]){ 
          monthwiseBills[month] = []
        } 
        monthwiseBills[month].push(bill)
        return monthwiseBills
     }
  ,{}) );

 // STEP 4 : Reduce all bills in a monthwise bucket into a monthlyReport object and store all monthlyReport objects in an monthlyReportArray
 const monthlyReportArray:BarChartDataModel[] = Object.keys(groupByMonth).map((year, yearwiseBills) => 
   Object.keys(bills).map((month, monthwiseBills) => monthwiseBills.reduce((monthlyReport:BarChartDataModel,bill:BillingSummaryDTO) => {
    if(bill.cancelled){
      monthlyReport.Chargebacks++
    }else{
      monthlyReport.Transactions++,
      monthlyReport.USD += bill.paid
    }
    return monthlyReport
  },{
    name : moment(month, 'M').format('MMM')
    Transactions : 0,
    USD : 0,
    Chargebacks:0
   } )
 )


 // STEP 5 : Consume this as the "data" for the "<BarChart/>" component
 return monthlyReportArray
}
 

ฉัน bucketed ยนปีแรกและจากนั้น bucketed โดยเดือนแทนโดยตรง bucketing โดยเดือนเพราะเราไม่อยากรวมรายเดือรายงานขอพูดว่า"อาจจะของ 1997"และ"ของขอ 1998"ไปแค่"อาจจะ"สำหรับของเราเป็นลายลักษณ์อักษร เราคงต้องการให้พวกเขาจะแยกออก

2021-11-24 05:49:15

ฉันได้หลายข้อผิดพลาด. คุณแน่ใจหรือว่านี่จะเป็นทางออกที่เหมาะสม?
Peter Penzov

คุณช่วยแบ่งปันของคุณเกิดข้อผิดพลาด? จากวิธีการสำหรับแผนที่-ลดจุดรบสมบูรณ์แบบงั้นคนตรรกะรีบทำเวลาให้ทัน ไวยากรณ์ปัญหาเรื่องอาจจะเกิดขึ้น(ส่วนใหญ่เป็นชนิด-เช็คข้อผิดพลาด).
Ankit Sanghvi

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่