import React, { useEffect, useRef, useState } from "react"; import * as echarts from "echarts"; import indonesiaGeo from "../../public/assets/geo/indonesia.json"; const IndonesiaMap = (props: { data: any }) => { const chartRef = useRef(null); const myChart = useRef(null); const [selectedProvince, setSelectedProvince] = useState(""); const selectedProvinceRef = useRef(null); const chartOptionRef = useRef(null); const option = { backgroundColor: "#d3d3d3", title: { text: "Sebaran Data", left: "center", textStyle: { color: "#000" }, }, visualMap: { min: 0, max: 100, show: true, seriesIndex: 0, inRange: { color: ["#ff0000", "#ffffff"], }, }, series: [ { type: "map", map: "indonesia", roam: true, zoom: 1.2, center: [118, -2], label: { show: false, color: "#000", }, itemStyle: { borderColor: "#999", }, emphasis: { label: { show: true, color: "#000", }, itemStyle: { areaColor: "#ffcccc", }, }, data: props.data, }, ], }; useEffect(() => { if (!chartRef.current) return; (window as any).echarts = echarts; const fixedGeo = { ...indonesiaGeo, features: indonesiaGeo.features.map((f) => ({ ...f, properties: { ...f.properties, name: f.properties.PROVINSI, }, })), }; echarts.registerMap("indonesia", fixedGeo as any); const chart = echarts.init(chartRef.current); myChart.current = chart; chart.on("click", (params: any) => { highlightProvince(params.name); }); chartOptionRef.current = option; chart.setOption(option); return () => { chart.dispose(); }; }, []); const highlightProvince = (name: string) => { if (!myChart.current || !chartOptionRef.current) return; myChart.current.clear(); myChart.current.setOption(chartOptionRef.current); myChart.current.dispatchAction({ type: "highlight", name, }); myChart.current.dispatchAction({ type: "showTip", name, }); selectedProvinceRef.current = name; }; return (
); }; export default IndonesiaMap;