Angular项目中使用echarts中国地图

首先要在Angular项目中安装echarts的依赖:

npm  install  echarts  --save
npm  install  ngx-echarts  --save

第二、在创建包含地图的Angular组件的时候引入echarts插件和包含的地图插件,并完成配置和初始化。主要有两种方式:

     第一种方式,通过JSON方式引入,步骤如下:

   (1)引入httpClient和NgxEchartsService服务,并依赖注入对应组件。

import {HttpClient} from '@angular/common/http';
import {NgxEchartsService} from 'ngx-echarts';


constructor(private http: HttpClient,
              private nes: NgxEchartsService,
              private draw: DrawService
  ) { }

另外记得在组件对应的模块配置文件中添加HttpClientModule和NgxEchartsModule模块;

(2)配置地图参数信息,具体配置信息可查询echarts官网配置项内容自行配置。该demo配置项对象代码如下:

setMapOption() {
    this.mapOption = {
      title : {
        text: '中移在线通信云项目实时监控',
        subtext: '',
        left: 'center',
        textStyle: {
          color: '#333',
          fontSize: '28',
        },
      },
      backgroundColor: '#96aec7',
      tooltip : {
        trigger: 'item',
        formatter: function (val) {
          return `项目进度:<br>${val.data.name}:${val.data.value}%<br>告警:${val.data.warn}<br>问题:${val.data.problem}`;
        }
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        data:[]
      },
      visualMap: {
        min: 0,
        max: 100,
        left: 'left',
        top: 'bottom',
        text:['高','低'],           // 文本,默认为数值文本
        calculable : true,
        inRange: {
          color: ['#feffc7', '#02cb00']
        }
      },
      series : [
        {
          name: '项目进度',
          type: 'map',
          mapType: 'china',
          roam: false,
          label: {
            normal: {
              show: true
            },
            emphasis: {
              show: true
            }
          },
          itemStyle: {
            color: 'red',
            areaColor: '#fff',
          },
          emphasis: {
            itemStyle: {
              areaColor: '#f9ba09'
            }
          },
          data:[
            {name: '北京',value: Math.round(Math.random()*100),warn: 10,problem: 12},
            {name: '天津',value: Math.round(Math.random()*100),warn: 10,problem: 12},
            {name: '上海',value: Math.round(Math.random()*100),warn: 10,problem: 12},
            {name: '重庆',value: Math.round(Math.random()*100),warn: 10,problem: 12},
            {name: '河北',value: Math.round(Math.random()*100)},
            {name: '河南',value: Math.round(Math.random()*100)},
            {name: '云南',value: Math.round(Math.random()*100)},
            {name: '辽宁',value: Math.round(Math.random()*100)},
            {name: '黑龙江',value: Math.round(Math.random()*100)},
            {name: '湖南',value: Math.round(Math.random()*100)},
            {name: '安徽',value: Math.round(Math.random()*100)},
            {name: '山东',value: Math.round(Math.random()*100)},
            {name: '新疆',value: Math.round(Math.random()*100)},
            {name: '江苏',value: Math.round(Math.random()*100)},
            {name: '浙江',value: Math.round(Math.random()*100)},
            {name: '江西',value: Math.round(Math.random()*100)},
            {name: '湖北',value: Math.round(Math.random()*100)},
            {name: '广西',value: Math.round(Math.random()*100)},
            {name: '甘肃',value: Math.round(Math.random()*100)},
            {name: '山西',value: Math.round(Math.random()*100)},
            {name: '内蒙古',value: Math.round(Math.random()*100)},
            {name: '陕西',value: Math.round(Math.random()*100)},
            {name: '吉林',value: Math.round(Math.random()*100)},
            {name: '福建',value: Math.round(Math.random()*100)},
            {name: '贵州',value: Math.round(Math.random()*100)},
            {name: '广东',value: Math.round(Math.random()*100)},
            {name: '青海',value: Math.round(Math.random()*100)},
            {name: '西藏',value: Math.round(Math.random()*100)},
            {name: '四川',value: Math.round(Math.random()*100)},
            {name: '宁夏',value: Math.round(Math.random()*100)},
            {name: '海南',value: Math.round(Math.random()*100)},
            // {name: '台湾',value: Math.round(Math.random()*100)},
            {name: '香港',value: Math.round(Math.random()*100)},
            {name: '澳门',value: Math.round(Math.random()*100)}
          ]
        }
      ]
    };
  }

(3)使用http服务获取china.json,在对应的回调函数中初始化地图,代码如下:

@ViewChild('chartMap') chartMap: ElementRef; // 获取DOM节点的对象
initEcharts() {
     this.http.get('assets/json/china.json').subscribe(res => {
      const echart = this.echarts.init(this.chartMap.nativeElement); // 获取视图的echarts的DOM节点,并初始化对象
      this.echarts.registerMap('china', res); // 注册china.json的数据到初始化的echarts对象
      echart.setOption(this.mapOption); // 绑定地图的配置参数对象,参考第二步
      echart.on('click', function(params) { // 绑定地图点击事件
        // console.log(params);
      }.bind(this));
    });   
}

(4)在组件初始化方法中调用echarts初始化方法,这个地方有个设置地图dom固定宽高的方法,可根据页面宽度自适应地图的高度,如果不设置高度,echarts图表无法显示

ngOnInit() {
    this.setChartStyle(); // 设置地图固定宽高比的方法
    this.setMapOption();  // 配置map地图参数信息方法
    this.initEcharts();  // 初始化地图
}

setChartStyle() { 
    const width = this.chartMap.nativeElement.offsetWidth;
    this.chartMap.nativeElement.style.height = width * 0.4 + 'px';
}

第二种方式,通过china.js引入的方式,该方式可不引入NgxEchartsService,步骤跟第一种类似,如下:

(1)引入china.js和echarts组件,此处不需要注入其他服务,代码如下:

import 'echarts/map/js/china.js';
import * as echarts from 'echarts';

同样在组件对应的模块配置文件中添加HttpClientModule和NgxEchartsModule模块;

(2)配置地图信息同第一种方法。

(3)初始化地图方法,代码如下:

const echart = echarts.init(this.chartMap.nativeElement); // 获取视图的echarts的DOM节点,并初始化对象
    echart.setOption(this.mapOption); // 绑定地图的配置参数对象
    echart.on('click', function(params) {
      console.log(params);
    }.bind(this));

(4)在组件的初始化方法中初始化地图,代码同第一种方法(4)。

效果图如下所示:

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Angular实现Echarts的3D饼图,需要先安装Echarts库和它的类型定义: ``` npm install echarts --save npm install @types/echarts --save-dev ``` 然后在组件引入echarts: ```typescript import * as echarts from 'echarts'; ``` 在模板添加一个`div`元素作为Echarts的容器: ```html <div #chart style="width: 600px; height: 400px;"></div> ``` 在组件获取这个`div`元素: ```typescript @ViewChild('chart', { static: true }) chartElement: ElementRef; ``` 然后在`ngOnInit`生命周期钩子函数使用Echarts的API来绘制3D饼图: ```typescript ngOnInit() { const chart = echarts.init(this.chartElement.nativeElement); chart.setOption({ tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 335, name: '直接访问' }, { value: 310, name: '邮件营销' }, { value: 234, name: '联盟广告' }, { value: 135, name: '视频广告' }, { value: 1548, name: '搜索引擎' } ], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' }, normal: { color: function(params) { // build a color map as your need. const colorList = [ '#FF6666', '#FFCC66', '#99CC66', '#66CCCC', '#6699CC', '#CC99CC' ]; return colorList[params.dataIndex]; } } } } ] }); } ``` 这个例子的3D饼图是一个有五个扇形的图,每个扇形的颜色、名称和数值都是自定义的。你可以根据自己的需求修改这个例子或者参考Echarts官方文档的其他示例来自定义你自己的3D饼图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值