riku
2025-09-11 307b17ef15c73a071912a262834f2a5f68e1fa87
src/utils/chart/chart-map.js
@@ -115,16 +115,6 @@
  chart.setOption(option);
  // 将像素坐标转换为经纬度
  // const convert = (x, y) => {
  //   return chart.convertFromPixel(
  //     {
  //       geoIndex: 0
  //     },
  //     [x, y]
  //   );
  // };
  // 将经纬度转换为像素坐标
  const convert = (lng, lat) => {
    return chart.convertToPixel(
@@ -162,88 +152,12 @@
        pixelRatio: 2,
        backgroundColor: '#fff'
      });
      // 销毁实例并移除临时DOM
      chart.dispose();
      document.body.removeChild(div);
      resolve(url);
    }, 1000);
  });
  // return captureMapByBounds({
  //   chart: chart,
  //   bounds: bounds
  // }).catch((err) => {
  //   console.error('截图失败:', err);
  // });
}
/**
 * 根据经纬度范围截取地图区域
 * @param {Object} params - 截取参数
 * @param {Array} params.bounds - 经纬度范围 [minLng, minLat, maxLng, maxLat]
 * @param {Object} params.chart - ECharts实例
 * @returns {Promise<string>} 截取区域的base64图片
 */
// function captureMapByBounds(params) {
//   const { bounds, chart } = params;
//   const [minLng, minLat, maxLng, maxLat] = bounds;
//   // 获取地图坐标系
//   // const geo = chart.getModel().getComponent('geo');
//   // if (!geo) return Promise.reject('未找到地图组件');
//   // 将经纬度转换为像素坐标
//   const convert = (lng, lat) => {
//     return chart.convertToPixel(
//       {
//         geoIndex: 0
//       },
//       [lng, lat]
//     );
//   };
//   // 计算四个角的像素坐标
//   const topLeft = convert(minLng, maxLat);
//   const bottomRight = convert(maxLng, minLat);
//   // 创建临时Canvas
//   const canvas = document.createElement('canvas');
//   const ctx = canvas.getContext('2d');
//   // 获取原始图表Canvas
//   const originalCanvas = chart.getDom().querySelector('canvas');
//   // 设置Canvas尺寸为截取区域大小
//   topLeft[0] -= 10;
//   topLeft[1] -= 10;
//   bottomRight[0] += 10;
//   bottomRight[1] += 10;
//   topLeft[0] = Math.max(topLeft[0], 0);
//   topLeft[1] = Math.max(topLeft[1], 0);
//   bottomRight[0] = Math.min(bottomRight[0], originalCanvas.width);
//   bottomRight[1] = Math.min(bottomRight[1], originalCanvas.height);
//   const width = bottomRight[0] - topLeft[0];
//   const height = bottomRight[1] - topLeft[1];
//   canvas.width = width;
//   canvas.height = height;
//   // 裁剪指定区域
//   ctx.drawImage(
//     originalCanvas,
//     topLeft[0],
//     topLeft[1], // 源图像裁剪起点
//     width,
//     height, // 源图像裁剪尺寸
//     0,
//     0, // 目标图像绘制起点
//     width,
//     height // 目标图像绘制尺寸
//   );
//   // 转换为base64图片
//   return new Promise((resolve) => {
//     // 延迟执行确保绘制完成
//     setTimeout(() => {
//       const base64 = canvas.toDataURL('image/png', 1.0);
//       resolve(base64);
//     }, 100);
//   });
// }
export default { generateGridMap };