import { useState, useRef, useEffect } from "react"; import "../index.scss"; import { Button, Select } from "antd"; // import type { EChartsOption } from "node_modules/react-echarts-core/lib/ChartCore"; // import ChartCore from "node_modules/react-echarts-core/lib/ChartCore"; import * as echarts from "echarts"; export default function EchartsEditor() { const echartRef: any = useRef(null); const [xList, setXList] = useState([]); const [yList, setYList] = useState([]); const [currentDrag, setCurrentDrag] = useState({}); const [dataList, setDataList] = useState([ { id: 1, name: "Vue", state: "Value" }, { id: 2, name: "React", state: "Value" }, { id: 3, name: "Angular", state: "Key" }, ]); const [echartOption, setEchartOption] = useState({ xAxis: { data: ["A", "B", "C", "D", "E"], }, yAxis: { type: "value", }, series: [ { data: [1, 2, 3, 4, 5], type: "line", }, ], }); const onDragStart: any = (item: any) => { console.log(item.name, "开始拖拽"); setCurrentDrag(item); }; const onDragEnd: any = (item: any) => { console.log(item.name, "结束拖拽"); }; const onYDragOver: any = (e: any) => { e.preventDefault(); }; const onXDrop: any = () => { console.log("拖拽至X轴区域"); setXList(xList.concat(currentDrag)); let newDataList = handleDelDataList(currentDrag.id); setDataList(newDataList); setEchartOption({ ...echartOption, xAxis: { data: xList.map((item) => item.name), }, }); }; const onXDragOver: any = (e: any) => { e.preventDefault(); }; const onYDrop: any = () => { console.log("拖拽至Y轴区域"); console.log("拖拽至Y轴区域"); console.log("拖拽至Y轴区域"); setYList(yList.concat(currentDrag)); let newDataList = handleDelDataList(currentDrag.id); setDataList(newDataList); }; const handleDelDataList = (id: number) => { let newDataList = dataList; const curIndex = newDataList.findIndex((item) => item.id === id); newDataList.splice(curIndex, 1); return newDataList; }; // const res = () => { // let res: any[] = []; // setXList(xList); // xList.forEach((item: any) => { // res.push(item.name); // }); // setXEchartList(res); // console.log(xEchartList); // return xEchartList; // }; useEffect(() => { const myEchart: any = echarts.init(document.querySelector(".canvas")); myEchart.setOption(echartOption); console.log(echartOption); }, [echartOption]); return ( <>