From 7f6661cca40e3530111d628222fa25462022ec78 Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期四, 04 九月 2025 18:25:36 +0800 Subject: [PATCH] 2025.9.4 --- src/utils/chart/chart-map.js | 200 ++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 163 insertions(+), 37 deletions(-) diff --git a/src/utils/chart/chart-map.js b/src/utils/chart/chart-map.js index cb40c75..fb72feb 100644 --- a/src/utils/chart/chart-map.js +++ b/src/utils/chart/chart-map.js @@ -2,6 +2,12 @@ // import * as shanghaiMap from 'echarts-china-cities-js/echarts-china-cities-js/shanghai.js'; // 淇鍦板浘鏁版嵁鏂囦欢璺緞 // import 'echarts-china-cities-js/echarts-china-cities-js/shanghai.js'; // 淇鍦板浘鏁版嵁鏂囦欢璺緞 import { shanghai, jingan } from '@/utils/chart/shanghai.js'; // 纭繚璺緞姝g‘ +import calculate from '@/utils/map/calculate.js'; + +// 鍋忕Щ绾害锛屽搴旂害100绫� +const OFFSET_LAT = 0.00082; +// 鍋忕Щ绾害锛屽搴旂害100绫� +const OFFSET_LNG = 0.0009; /** * 璁$畻鍦板浘涓績鐐瑰拰缂╂斁姣斾緥 @@ -9,40 +15,47 @@ * @returns */ function calCenterPointAndZoom(gridData) { - let centerLng = 0; - let centerLat = 0; + const coordinates = []; let maxLng = -180; let minLng = 180; let maxLat = -90; let minLat = 90; - let zoom = 100; + let zoom = 1; gridData.forEach((g) => { - centerLng += g.centerLng; - centerLat += g.centerLat; - maxLng = Math.max(maxLng, g.centerLng); - minLng = Math.min(minLng, g.centerLng); - maxLat = Math.max(maxLat, g.centerLat); - minLat = Math.min(minLat, g.centerLat); + g.coordinates.forEach((coordArr) => { + coordinates.push({ + lng: coordArr[0], + lat: coordArr[1] + }); + maxLng = Math.max(maxLng, coordArr[0]); + minLng = Math.min(minLng, coordArr[0]); + maxLat = Math.max(maxLat, coordArr[1]); + minLat = Math.min(minLat, coordArr[1]); + }); }); - if (gridData.length > 0) { - centerLng /= gridData.length; - centerLat /= gridData.length; - } - console.log((maxLng - centerLng) / zoom); + console.log('zoom', zoom); return { - centerLng, - centerLat, - zoom + centerLng: (maxLng + minLng) / 2, + centerLat: (maxLat + minLat) / 2, + zoom, + bounds: [ + minLng - OFFSET_LNG, + minLat - OFFSET_LAT, + maxLng + OFFSET_LNG, + maxLat + OFFSET_LAT + ] }; } function generateGridMap(gridData) { + const width = 800; + const height = 400; // 1. 鍒涘缓涓存椂DOM鍏冪礌 const div = document.createElement('div'); - div.style.width = '800px'; - div.style.height = '400px'; + div.style.width = `${width}px`; + div.style.height = `${height}px`; document.body.appendChild(div); // 娉ㄥ唽涓婃捣甯傚湴鍥炬暟鎹� // console.log(shanghaiMap); @@ -57,7 +70,8 @@ value: [grid.centerLng, grid.centerLat, grid.value], // 涓績鐐圭粡绾害鍜屾暟鍊� coords: grid.coordinates // 缃戞牸鍥涜缁忕含搴﹀潗鏍囨暟缁� [[lng1,lat1], [lng2,lat2], ...] })); - const { centerLng, centerLat, zoom } = calCenterPointAndZoom(gridData); + const { centerLng, centerLat, zoom, bounds } = + calCenterPointAndZoom(gridData); // 2. 閰嶇疆椤� const option = { @@ -72,18 +86,6 @@ label: { show: true }, // 鏄剧ず鍦板悕鏍囩 itemStyle: { areaColor: '#ddddddff', borderColor: '#999' } }, - // series: [ - // { - // type: 'scatter', - // coordinateSystem: 'geo', - // data: [ - // { - // name: '榛勬郸鍖�', - // value: [121.490317, 31.222771, 100] // 涓績鐐瑰潗鏍�+鏁板�� - // } - // ] - // } - // ] series: [ { type: 'custom', @@ -112,12 +114,136 @@ }; chart.setOption(option); - // 3. 瀵煎嚭涓哄浘鐗囷紙杩斿洖base64锛� - return chart.getDataURL({ - type: 'png', - pixelRatio: 1, - backgroundColor: '#ddddddff' + + // 灏嗗儚绱犲潗鏍囪浆鎹负缁忕含搴� + // const convert = (x, y) => { + // return chart.convertFromPixel( + // { + // geoIndex: 0 + // }, + // [x, y] + // ); + // }; + + // 灏嗙粡绾害杞崲涓哄儚绱犲潗鏍� + const convert = (lng, lat) => { + return chart.convertToPixel( + { + geoIndex: 0 + }, + [lng, lat] + ); + }; + + // 璁$畻鐢诲竷鐨勫乏涓婅鍜屽彸涓嬭瀵瑰簲鐨勭粡绾害 + // 璁$畻缃戞牸鍖哄煙鐨勫乏涓婅鍜屽彸涓嬭缁忕含搴﹀搴旂殑鍍忕礌鍧愭爣 + const topLeft = convert(bounds[0], bounds[3]); + const bottomRight = convert(bounds[2], bounds[1]); + // 璁$畻鍚堥�傜殑缂╂斁鍊嶆暟 + const scale = Math.min( + Math.abs(width / (bottomRight[0] - topLeft[0])), + Math.abs(height / (bottomRight[1] - topLeft[1])) + ); + console.log('scale', scale); + + // 鍦板浘缂╂斁姣斾緥 + chart.setOption({ + geo: { + zoom: scale + } }); + + // 3. 瀵煎嚭涓哄浘鐗囷紙杩斿洖base64锛� + return new Promise((resolve, reject) => { + // 寤惰繜鎵ц纭繚缁樺埗瀹屾垚 + setTimeout(() => { + const url = chart.getDataURL({ + type: 'png', + pixelRatio: 2, + backgroundColor: '#fff' + }); + 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>} 鎴彇鍖哄煙鐨刡ase64鍥剧墖 + */ +// 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 // 鐩爣鍥惧儚缁樺埗灏哄 +// ); + +// // 杞崲涓篵ase64鍥剧墖 +// return new Promise((resolve) => { +// // 寤惰繜鎵ц纭繚缁樺埗瀹屾垚 +// setTimeout(() => { +// const base64 = canvas.toDataURL('image/png', 1.0); +// resolve(base64); +// }, 100); +// }); +// } + export default { generateGridMap }; -- Gitblit v1.9.3