赞
踩
项目需要先引入echarts组件
npm install echarts --save
然后封装的EchartsComponent组件文件 如下
- /* eslint-disable */
- import React, { Component } from "react";
- import PropTypes from "prop-types";
- /*引入 ECharts 主模块**/
- import echarts, { dispose } from "echarts/lib/echarts";
- import "echarts/lib/chart/pie";
- import "echarts/lib/chart/bar";
- import "echarts/lib/chart/line";
- import "echarts/lib/chart/tree";
- import "echarts/lib/component/tooltip";
- import "echarts/lib/component/legend";
- import "echarts/lib/component/title";
- import "echarts/lib/component/legendScroll";
- import 'echarts/map/js/china';
-
- class EchartsComponent extends Component {
- chart = null;
-
- componentDidMount() {
- const { chartId, option } = this.props; /**继承父级传递的值*/
- const chartIdDiv = document.getElementById(chartId);
- if (chartIdDiv) {
- this.chart = echarts.init(chartIdDiv); /**实例化echats画布*/
- if (this.chart) {
- this.renderChart(this.chart, option); /**加载数据*/
- }
- }
- window.addEventListener(
- "resize",
- this.handleResize,
- false
- ); /**监听改变画布大小*/
- }
-
- /**
- * @description: 根据父级传递的值,是否更新画布
- * @param {type}
- * @return {type}
- */
- componentDidUpdate(prevProps) {
- const { option } = this.props;
- if (prevProps.option !== option && this.chart) {
- this.renderChart(this.chart, option);
- }
- }
-
- /**
- * @description: 注销时,去除监听事件以及销毁echats
- * @param {type}
- * @return {type}
- **/
- componentWillUnmount() {
- window.removeEventListener("resize", this.handleResize, false);
- if (this.chart) {
- this.chart.dispose();
- this.chart = null;
- }
- }
-
- /**放大缩小重新布局*/
- handleResize = () => {
- if (this.chart) {
- setTimeout(() => {
- this.chart.resize();
- });
- }
- };
- /**图表配置及渲染*/
- renderChart = (chart, option) => {
- const { handleClick } = this.props;
- chart.clear();
- chart.setOption(option);
- chart.off("click"); /**要是不加上这行,事件会重复n次*/
- chart.on("click", (params) => {
- if (handleClick) {
- handleClick(params);
- }
- });
- };
-
- render() {
- const { chartId, width, height, margin } = this.props;
- return (
- <div
- id={chartId}
- style={{
- minWidth: width,
- minHeight: height,
- height: height,
- width: width,
- margin,
- }}
- />
- );
- }
- }
-
- EchartsComponent.defaultProps = {
- margin: "0 auto",
- width: "556px",
- height: "430px",
- };
-
- export default EchartsComponent;
- /* eslint-disable */
项目中引用如下文件:
提示:其中options需要单独作为参数 传入,因为每个图标配置 项可能 不一样;
- import EchartsComponent from "Components/EchartsComponent";//注意引入方式 可能需要修改
-
- //option 可以 参考 https://echarts.apache.org/examples/zh/index.html echarts地址的所有option传入即可
-
-
-
- // 下面提供一个中国地图的option实现 了解大致逻辑 因为此地图配置根据个人需要更改
- import React, { useEffect, useState } from "react";
-
- function MapTravel(props) {
- const { loading, travelInfo, geoCoordMap } = props;
- const [option, setOption] = useState("");
- const [nowTime, setTNowTime] = useState(
- moment(new Date()).format("YYYY/MM/DD HH:mm:ss")
- );
-
- let series = [];
- useEffect(() => {
- // 获取接口数据
- props.getMapTravelInfo();
- }, []);
-
-
- // 监听后段接口返回的数据 props.travelInfo 因为redux逻辑调用接口传入数据 prop方式拿数据
- useEffect(() => {
- let arr = [],
- stationNameArr = [];
- if (Object.keys(props.travelInfo).length) {
- // 把后端返回的数据 二次调整页面显示需要的数据格式
- props.travelInfo.forEach((item) => {
- if (item.stationName) {
- stationNameArr.push(item.stationName);
- arr.push({
- ...item,
- name: item.stationName,
- value: 100,
- });
- }
- });
- // 调整地图地点高亮显示样式 半圆切割色
- arr.forEach(function(item) {
- let colorObj = [{}];
- if (item.inventoryFlag === 0 && item.departureAndArrivalFlag === 0) {
- // 站点库存正常,进出港浮动正常
- colorObj = [
- {
- offset: 0.5,
- color: "#249e24",
- },
- ];
- } else if (
- item.inventoryFlag === 1 &&
- item.departureAndArrivalFlag === 0
- ) {
- // 站点库存异常,进出港浮动正常
- colorObj = [
- {
- offset: 0.5,
- color: "#ff0000",
- },
- {
- offset: 0.5,
- color: "#249e24",
- },
- ];
- } else if (
- item.inventoryFlag === 0 &&
- item.departureAndArrivalFlag === 1
- ) {
- // 站点库存正常,进出港浮动异常
- colorObj = [
- {
- offset: 0.5,
- color: "#249e24",
- },
- {
- offset: 0.5,
- color: "#ff0000",
- },
- ];
- } else if (
- item.inventoryFlag === 1 &&
- item.departureAndArrivalFlag === 1
- ) {
- // 站点库存异常,进出港浮动异常
- colorObj = [
- {
- offset: 0.5,
- color: "#ff0000",
- },
- ];
- }
-
- //以上数据二次改为自己想要的格式
- series.push({
- name: item["name"],
- type: "effectScatter",
- coordinateSystem: "geo",
- zlevel: 2,
- rippleEffect: {
- brushType: "stroke",
- },
- label: {
- normal: {
- show: true,
- position: "right",
- formatter: "{b}",
- color: "#fff",
- },
- },
- symbolSize: function(val) {
- return val[2] / 8;
- },
- itemStyle: {
- normal: {
- color: new echarts.graphic.RadialGradient(
- 0.5,
- 0.5,
- 0.6,
- colorObj
- ),
- },
- },
- data: [
- {
- ...item,
- // 匹配到对应 地理位置名称
- value: geoCoordMap[item.name]
- ? geoCoordMap[item.name].concat([item.value])
- : 0,
- },
- ],
- });
- });
-
- setOption({
- title: {
- text: "",
- // subtext: '数据纯属虚构',
- left: "center",
- top: "20px",
- textStyle: {
- color: "#fff",
- },
- },
- tooltip: {
- trigger: "item",
- backgroundColor: "#0a265f",
- formatter: function(params, ticket, callback) {
- // 后端接口返回的每一项数据. 下面的数据适用我之前的项目 可根据具体项目需求调整
- const { data, seriesType, name } = params;
- if (seriesType == "effectScatter") {
- return (
- '<div class="tipBox"> ' +
- data.name +
- '<div><div class="contentItem"><div><div>站点库存</div><span>' +
- data.inventoryNumber +
- "</span></div><div><div>开口/无源</div><span>" +
- data.openingNumber +
- "/" +
- data.passiveNumber +
- "</span></div><div><div>进港/出港总数</div><span>" +
- data.arrivalNumber +
- "/" +
- data.departureNumber +
- '</span></div></div><div class="tip">开口餐车为前天数据,无源餐车为月初1号数据</div>'
- );
- } else if (seriesType == "lines") {
- return `${data.fromName}->${data.toName}:${data.value}`;
- } else {
- return name;
- }
- },
- },
- legend: {
- orient: "vertical",
- top: "center",
- left: "right",
- data: stationNameArr,
- textStyle: {
- color: "#fff",
- },
- formatter: function(name) {
- //用来格式化图例文本,支持字符串模板和回调函数两种形式。模板变量为图例名称 {name}
- return ` ${name}`;
- },
- selectedMode: "multiple",
- },
- //地理位置配置
- geo: {
- map: "china",
- label: {
- emphasis: {
- show: true,
- color: "#fff",
- },
- },
- //中国地图放大
- zoom: 1.2,
- roam: true,
- itemStyle: {
- normal: {
- //区域省份背景色颜色设置
- areaColor: "rgba(20,41,87, .6)",
- borderColor: "#195BB9",
- borderWidth: 1,
- },
- emphasis: {
- areaColor: "#2B91B7",
- },
- },
- },
- series: series,
- });
- }
- }, [props.travelInfo]);
-
- return (
- <Spin
- spinning={loading}
- style={{ height: "100vh" }}
- className={"toggleSpin"}
- >
- <div className={style.nav}>
- {option ? (
- <EchartsComponent
- width={"90%"}
- height={"95%"}
- chartId="mapChartId"
- option={option}
- />
- ) : (
- ""
- )}
-
- </div>
- </Spin>
- );
- }
-
- MapTravel.propTypes = {
- loading: PropTypes.bool.isRequired,
- getMapTravelInfo: PropTypes.func.isRequired,
- travelInfo: PropTypes.array.isRequired,
- };
-
- function mapStateToProps(state) {
- const { loading, travelInfo } = state.mapTravel;
-
- return {
- geoCoordMap,
- loading,
- travelInfo,
- };
- }
-
- function mapDispatchToProps(dispatch) {
- return bindActionCreators(actions, dispatch);
- }
-
- export default connect(mapStateToProps, mapDispatchToProps)(MapTravel);
对应redux文件
- /* eslint-disable */
- import network from "Utils/network";
-
- const initialState = {
- loading: false,
- travelInfo: {},
- geoCoordMap: {
- // 上海: [121.4648, 31.2891],
- // 东莞: [113.8953, 22.901],
- // 东营: [118.7073, 37.5513],
- // 中山: [113.4229, 22.478],
- // 临汾: [111.4783, 36.1615],
- // 丹东: [124.541, 40.4242],
- // 丽水: [119.5642, 28.1854],
- // 佛山: [112.8955, 23.1097],
- // 保定: [115.0488, 39.0948],
- // 北海: [109.314, 21.6211],
- // 南通: [121.1023, 32.1625],
- // 台州: [121.1353, 28.6688],
- // 咸阳: [108.4131, 34.8706],
- // 唐山: [118.4766, 39.6826],
- // 嘉兴: [120.9155, 30.6354],
- // 大同: [113.7854, 39.8035],
- // 太原: [112.3352, 37.9413],
- // 宝鸡: [107.1826, 34.3433],
- // 宿迁: [118.5535, 33.7775],
- // 廊坊: [116.521, 39.0509],
- // 延安: [109.1052, 36.4252],
- // 张家口: [115.1477, 40.8527],
- // 徐州: [117.5208, 34.3268],
- // 德州: [116.6858, 37.2107],
- // 惠州: [114.6204, 23.1647],
- // 成都: [103.9526, 30.7617],
- // 扬州: [119.4653, 32.8162],
- // 承德: [117.5757, 41.4075],
- // 枣庄: [117.323, 34.8926],
- // 柳州: [109.3799, 24.9774],
- // 株洲: [113.5327, 27.0319],
- // 江门: [112.6318, 22.1484],
- // 沧州: [116.8286, 38.2104],
- // 河源: [114.917, 23.9722],
- // 泉州: [118.3228, 25.1147],
- // 泰安: [117.0264, 36.0516],
- // 泰州: [120.0586, 32.5525],
- // 济南: [117.1582, 36.8701],
- // 济宁: [116.8286, 35.3375],
- // 淄博: [118.0371, 36.6064],
- // 清远: [112.9175, 24.3292],
- // 渭南: [109.7864, 35.0299],
- // 湖州: [119.8608, 30.7782],
- // 湘潭: [112.5439, 27.7075],
- // 滨州: [117.8174, 37.4963],
- // 潍坊: [119.0918, 36.524],
- // 玉溪: [101.9312, 23.8898],
- // 盘锦: [121.9482, 41.0449],
- // 石家庄: [114.4995, 38.1006],
- // 秦皇岛: [119.2126, 40.0232],
- // 绍兴: [120.564, 29.7565],
- // 聊城: [115.9167, 36.4032],
- // 肇庆: [112.1265, 23.5822],
- // 舟山: [122.2559, 30.2234],
- // 苏州: [120.6519, 31.3989],
- // 莱芜: [117.6526, 36.2714],
- // 菏泽: [115.6201, 35.2057],
- // 营口: [122.4316, 40.4297],
- // 葫芦岛: [120.1575, 40.578],
- // 衡水: [115.8838, 37.7161],
- // 衢州: [118.6853, 28.8666],
- // 连云港: [119.1248, 34.552],
- // 邢台: [114.8071, 37.2821],
- // 邯郸: [114.4775, 36.535],
- // 金华: [120.0037, 29.1028],
- // 铜川: [109.0393, 35.1947],
- // 镇江: [119.4763, 31.9702],
- // 长治: [112.8625, 36.4746],
- // 阳泉: [113.4778, 38.0951],
- // 韶关: [113.7964, 24.7028],
-
- // 配置新增的机场名称
- '北京(大兴航食)': [116.410745, 39.510251],
- '虹桥': [121.34113, 31.19590],
- '浦东': [121.808603, 31.142363],
- '青岛': [120.4651, 36.3373],
- '齐鲁(济南)': [117.213048, 36.851644],
- '烟台': [120.7397, 37.5128],
- '武汉': [114.3896, 30.6628],
- '山西(太原)': [112.3352, 37.9413],
- '云南(昆明)': [102.9199, 25.4663],
- '甘肃(兰州)': [103.5901, 36.3043],
- '西安': [109.1162, 34.2004],
- '安徽(合肥)': [117.29, 32.0581],
- '江苏(南京)': [118.8062, 31.9208],
- '无锡': [120.3442, 31.5527],
- '江西(南昌)': [116.0046, 28.6633],
- '宁波': [121.5967, 29.6466],
- '成都(双流)': [103.951572, 30.559105],
- '成都(天府)': [104.432573, 30.278925],
- '深圳': [114.5435, 22.5439],
- '深圳(深圳航食)': [113.826548, 22.646759],
- '深圳(深圳空港)': [114.085947, 22.547],
- '重庆': [107.7539, 30.1904],
- '重庆(重庆机场)': [106.638184, 29.716311],
- '重庆(重庆中航)': [106.504962, 29.533155],
- '南宁': [108.479, 23.1152],
- '呼和浩特': [111.4124, 40.4901],
- '贵阳': [106.6992, 26.7682],
- '临沂': [118.3118, 35.2936],
- '威海': [121.9482, 37.1393],
- '郑州': [113.4668, 34.6234],
- '郑州(郑州机场)': [113.829697, 34.531751],
- '郑州(郑州南航)': [113.665412, 34.757975],
- '沈阳': [123.1238, 42.1216],
- '沈阳(沈阳空港)': [123.495322, 41.639496],
- '沈阳(沈阳南联)': [123.429096, 41.796767],
- '海拉尔': [119.7364, 49.2122],
- '包头': [110.3467, 41.4899],
- '天津': [117.4219, 39.4189],
- '绵阳': [104.741722, 31.46402],
- '齐齐哈尔': [123.953486, 47.348079],
- '张家界': [110.479921, 29.127401],
- '万州': [108.379050, 30.807500],
- '湛江': [21.191720, 110.398070],
- '长春(长春机场)': [125.694328, 43.996622],
- '长春(长春南联)': [125.212570, 43.904247],
- '长春': [125.8154, 44.2584],
- '桂林': [110.271818, 25.261428],
- '广州(广州汉莎)': [113.27307, 23.15787],
- '广州(广州南联)': [113.826548, 22.646759],
- '广州(广州南联)': [113.826548, 22.646759],
- '广州': [113.5107, 23.2196],
- '厦门': [118.1689, 24.6478],
- '哈尔滨': [127.9688, 45.368],
- '哈尔滨(哈尔滨机场)': [126.251133, 45.625854],
- '哈尔滨(哈尔滨南航)': [126.642464, 45.756967],
- '汕头(揭阳潮汕)': [117.1692, 23.3405],
- '宜昌': [30.5569, 111.48],
- '黄岩(台州路桥)': [28.5652778, 121.430278],
- '日照': [119.2786, 35.5023],
- '北京(国航航食)': [116.603039, 40.080525],
- '北京(北京空港)': [116.4551, 40.2539],
- '北京首都': [110.141357, 24.9716],
- '海口': [110.3893, 19.8516],
- '温州': [120.498, 27.8119],
- '三亚': [109.508268, 18.247872],
- '昆明(云南空港)': [102.998300, 25.121938],
- '丽江': [100.233026, 26.872108],
- '迪庆(香格里拉)': [99.70601, 27.82308],
- '大理': [100.22998, 25.59157],
- '腾冲': [98.49414, 25.02539],
- '西宁': [101.4038, 36.8207],
- '银川': [106.3586, 38.1775],
- '鄂尔多斯': [108.9734, 39.2487],
- '拉萨': [91.1865, 30.1465],
- '盐城': [120.2234, 33.5577],
- '乌鲁木齐': [87.9236, 43.5883],
- '淮安': [118.927, 33.4039],
- '常州': [119.4543, 31.5582],
- '珠海': [113.7305, 22.1155],
- '福州': [119.4543, 25.9222],
- '泸州': [105.443348, 28.889138],
- '西双版纳': [100.797941, 22.001724],
- '晋江': [118.55194, 24.78141],
- '芒市': [98.588641, 24.433735],
- '嘉峪关': [98.277304, 39.786529],
- '敦煌': [40.0922, 94.6817],
- '喀什': [76.025886, 39.540737],
- '库尔勒': [41.6992, 86.1303],
- '黄山': [118.261088, 29.730427],
- '大连(大连机场)': [121.555709, 38.957998],
- '大连(大连南联)': [121.618622, 38.91459],
- '大连': [122.2229, 39.4409],
- '榆林(榆林榆阳)': [109.599254, 38.358264],
- '大庆(目前不涉及)': [125.142936, 46.752023],
- '杭州(杭州汉莎)': [120.445531, 30.239038],
- '杭州(杭州中宇)': [119.5313, 29.8773],
- '杭州': [120.15, 30.28],
- '义乌': [120.03273, 29.34066],
- '运城': [111.038141, 35.115697],
- '延吉': [42.8828, 129.451],
- '长沙(长沙机场)': [113.226255, 28.1887],
- '长沙(长沙南联)': [113.0823, 28.2568],
- '长沙': [113.00000, 28.21667],
- },
- };
-
- export const actions = {
- getMapTravelInfo: (data) => ({
- type: 'getMapTravelInfo',
- promise: network.GET('/WBPM1-eaccart/statistical/mapData', data),
- }),
- };
-
- const reducers = {
- getMapTravelInfoRequest: (state) => ({ ...state, loading: true }),
- getMapTravelInfoSuccess: (state, action) => {
- return { ...state, loading: false, travelInfo: action.data }
- },
- getMapTravelInfoFailure: (state) => ({ ...state, loading: false, travelInfo: {} }),
-
- };
-
- export default (state = initialState, action) => {
- if (reducers[action.type]) {
- return reducers[action.type](state, action);
- }
- return state;
- };
对应 travelInfo 后端数据格式:
对应以上数据展示页面如下 :
具体option的数据data需要根据项目去灵活调整 ,整体的option是不变的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。