博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
d3实现中国地图实时刷新地图颜色.md
阅读量:7187 次
发布时间:2019-06-29

本文共 3725 字,大约阅读时间需要 12 分钟。

import React from 'react';import ReactCharts from 'echarts-for-react';import $ from 'jquery'import './style.less';export default class ChinaMap extends React.Component {   constructor() {      super();      this.state = {         colors: ["#1AC977", " #00D2AA", "#00D7E1", "#00AAE1", "#0082D2", "#0070B5"],         features:[]      }   }   componentDidMount() {      this.paintChinaMap();   }   componentDidUpdate() {      const colorArray = this.props.patientData.healthRate;      let dataArray = [];      let page = this;      this.state.features.map((key,index)=>{         let item = {};         item.geometry = key.geometry;         item.properties = key.properties;         item.type = key.type;         item.colorindex = colorArray[index+1];         dataArray.push(item);      });      this.state.svg.selectAll("path").data(dataArray).attr("fill", function (d, i) {         const proId = d.properties.id;         const colorIndex = colorArray[proId];         return page.state.colors[colorIndex];      }).on("mouseout", function (d, i) {         d3.select(this)            .attr("fill", function (d, i) {               const proId = d.properties.id;               const colorIndex = colorArray[proId];               return page.state.colors[colorIndex];            })            .attr("stroke-width", 0)      });   }   paintChinaMap() {      const colorArray = this.props.patientData.healthRate;      let conWidth = $(".ChinaMap_chart").width();      let width = conWidth;      let height = conWidth / 1.3;      let page = this;      let svg = d3.select(this.refs.china_chart).append("svg")         .attr("width", width)         .attr("height", height)         .append("g")         .data(colorArray)         .attr("transform", "translate(0,0)");      let projection = d3.geo.mercator()         .center([107, 31])         .scale(750)         .translate([width / 2, height / 2]);      let path = d3.geo.path()         .projection(projection);      let color = d3.scale.category20();      this.setState({         svg:svg      });      d3.json("./js/json/china.json", function (error, root) {         if (error)            return console.error(error);         let dataArray = [];         root.features.map((key,index)=>{            let item = {};               item.geometry = key.geometry;               item.properties = key.properties;               item.type = key.type;               item.colorindex = colorArray[index+1];            dataArray.push(item);         });         page.setState({            features: dataArray         });         svg.selectAll("path")            .data(dataArray)            .enter()            .append("path")            .attr("fill", function (d, i) {               const proId = d.properties.id;               const colorIndex = colorArray[proId];               return page.state.colors[colorIndex];            })            .text(function (d) {               return d.properties.name;            })            .attr("text", function (d, i) {               return d.properties.name;            })            .attr("d", path)            .on("mouseover", function (d, i) {               d3.select(this)                  .attr("stroke", "#fff")                  .attr("stroke-width", 1)            })            .on("mouseout", function (d, i) {               d3.select(this)                  .attr("fill", function (d, i) {                     const proId = d.properties.id;                     const colorIndex = colorArray[proId];                     return page.state.colors[colorIndex];                  })                  .attr("stroke-width", 0)            });      });   }   render() {      return (         
) }}复制代码

转载于:https://juejin.im/post/5c91fa10f265da60cd2b556e

你可能感兴趣的文章
leetcode-cn 删除排序数组中的重复项
查看>>
计算机网络之前端必备
查看>>
持久化、序列化、反序列化、编码、解码的概念
查看>>
javascript函数
查看>>
国内高速Maven仓库
查看>>
文本文件、二进制文件
查看>>
Prometheus学习系列(八)之数据模型
查看>>
源码分析RocketMQ同一个消费组设置不同tag,消息订阅失败问题
查看>>
反转数字、字符串
查看>>
Attrib命令,可以让文件夹彻底的隐藏起来
查看>>
使Windows服务以控制台方式调试
查看>>
3.IntelliJ IDEA 使用详解
查看>>
1.mybatis实战教程mybatis in action之一开发环境搭建
查看>>
2019CCSU第二次校赛部分题解(A,B,E,G)
查看>>
HDU 1394 Minimum Inversion Number(线段树求逆序对)
查看>>
好听轻音乐
查看>>
Microsoft Security Essentials
查看>>
软媒时间---任务栏滚动工具
查看>>
第10条:始终要覆盖toString
查看>>
树的 起步*------二叉树
查看>>