OrderDualAxes.tsx 3.93 KB
// import { DualAxes } from '@ant-design/charts';

// const OrderDualAxes = ({ data, statisticMethod }) => {
//   console.log(data);
//   let yFiledString = 'curTime';
//   if (
//     statisticMethod === 'MONTH_STATISTICS' ||
//     statisticMethod === 'LAST_MONTH_STATISTICS'
//   ) {
//     yFiledString = 'curTime';
//   } else {
//     yFiledString = 'curMonth';
//   }
//   const config = {
//     data: [
//       data.targetAndTotalPaymentDtoList === undefined ||
//         data.targetAndTotalPaymentDtoList === null
//         ? []
//         : data.targetAndTotalPaymentDtoList,
//       data.orderNumberDtoList === undefined || data.orderNumberDtoList === null
//         ? []
//         : data.orderNumberDtoList,
//     ],
//     xField: yFiledString,
//     yField: ['curDayTotalPayment', 'curDayTotalOrderNumber', 'lastTime'],
//     geometryOptions: [
//       {
//         geometry: 'line',
//         seriesField: 'type',
//         lineStyle: {
//           lineWidth: 3,
//           lineDash: [5, 5],
//         },
//         smooth: true,
//       },
//       {
//         geometry: 'line',
//         seriesField: 'name',
//         point: {},
//       },
//     ],
//   };
//   const handleClick = (event) => {
//     console.log('点击事件event:', event);
//     console.log('点击事件:', event?.data);
//     alert(`你点击了数据: ${JSON.stringify(event?.data)}`);
//     console.log('点击事件:', JSON.stringify(event?.data));

//   };

//   const handleReady = (plot) => {
//     // 绑定多个事件以确保响应
//     plot.on('plot:click', handleClick);
//     plot.on('element:click', handleClick);
//   };
//   return <DualAxes {...config} onReady={handleReady} />;
// };

// export default OrderDualAxes;

import { DualAxes } from '@ant-design/charts';

const OrderDualAxes = ({ data, statisticMethod }) => {
  console.log('传入的数据:', data);

  let yFiledString = 'curTime';

  if (
    statisticMethod === 'MONTH_STATISTICS' ||
    statisticMethod === 'LAST_MONTH_STATISTICS'
  ) {
    yFiledString = 'curTime';
  } else {
    yFiledString = 'curMonth';
  }

  const targetData = data.targetAndTotalPaymentDtoList || [];
  const orderData = data.orderNumberDtoList || [];
  console.log(orderData, '传入的数据:', targetData);

  const config = {
    data: [targetData, orderData],
    xField: yFiledString,
    yField: ['curDayTotalPayment', 'curDayTotalOrderNumber', 'lastTime'],
    geometryOptions: [
      {
        geometry: 'line',
        seriesField: 'type',
        lineStyle: {
          lineWidth: 3,
          lineDash: [5, 5],
        },
        smooth: true,
        point: {
          shape: 'circle',
        },
      },
      {
        geometry: 'line',
        seriesField: 'name',
        point: {
          shape: 'circle',
        },
      },
    ],
  };

  const handleClick = (event) => {
    // console.log(targetData, '5656')
    // 获取当前点击点的数据
    const pointData = event?.data?.data;

    if (pointData) {
      if (pointData.type === '上年营业额' || pointData.type === '订单量') {
        const xValue = pointData[yFiledString]; // 获取当前列的 xField 值

        // 合并两个数据源
        const allData = [...targetData, ...orderData];
        console.log(orderData, '5656传入的数据:', targetData);
        // 筛选出当前列的所有数据
        const columnData = allData.filter(
          (item) => item[yFiledString] === xValue,
        );

        console.log(`当前列中 ${yFiledString} = ${xValue} 的数据:`, columnData);

        // 可选择将筛选出的数据展示到页面或以其他方式处理
        alert(`当前列数据: ${JSON.stringify(columnData, null, 2)}`);
      }
    } else {
      console.warn('点击事件未携带有效数据');
    }
  };

  const handleReady = (plot) => {
    // 绑定点击事件
    plot.on('element:click', handleClick);
  };

  return <DualAxes {...config} onReady={handleReady} />;
};

export default OrderDualAxes;