echarts 实现各种图表

地图(china map)

// html
<div ref="fillcontainer">
	<div id="fundPosition"></div>
</div>

//js
<script>
import echarts from 'echarts'; // 引入echarts插件
import '../../../node_modules/echarts/map/js/china.js'; // 引入china map数据
import '../../../node_modules/echarts/theme/roma.js' // 引入主题颜色

export default {
   
	data () {
   
		return {
   
			chart: null
		}
	},
	mounted () {
   
		this.$nextTick(function() {
   
			this.$refs.fillcontainer.style.height = (document.body.clientHeight - 160) + 'px';
			this.drawMap('fundPosition') // 传入id
			var that = this;
			var resizeTimer = null;
			// 保证页面在放大或缩小时,也能动态显示数据
			window.onresize = function () {
   
				if (resizeTimer) clearTimeout(resizeTimer);
				resizeTimer = setTimeout(function () {
   
					that.$refs.fillcontainer.style.height = (document.body.clientHeight 160) + 'px';
					that.drawMap('fundPosition');
				}, 100)
			}
		})
	},
	methods: {
   
		randomData () {
   
			return Math.round(Math.random() * 1000000);
		}
		drawMap(id) {
   
			if (this.chart && this.chart.dispose) {
   
				this.chart.dispose(); // 释放图表
			}
			this.chart = echarts.init(document.getElementById(id), "roma") // 第一个参数是ID名,第二个参数是主题色
			this.chart.setOption({
    // 配置图表
				title: {
   
					text: '2021年全国各省份投资情况',
					subtext: '单位/万元',
					left: 'center'
				},
				tooltip: {
   
					trigger: 'item'
				},
				legend: {
   
					orient: 'vertical',
					left: 'left',
					data: ['总投资额']
				},
				visualMap: {
   
					min: 0,
					max: 1200000,
					left: 'left',
					top: 'bottom',
					text: ['高', '低'], // 文本 默认为数值
					calculable: true
				},
				toolbox: {
   
					show: true,
					orient: 'vertical',
					left: 'right',
					top: 'center',
					feature: {
    // 功能按钮
						dataView: {
   readOnly: false}, // 数据视图
						restore: {
   }, // 还原
						saveAsImage: {
   } // 保存为图片
					}
				},
				series: [
					{
   
						name: '总投资额',
						type: 'map',
						mapType: 'china',
						roam: false,
						label: {
   
							normal:{
   
								show: true
							},
							emphasis: {
   
								show: true
							},
							data:[
                            {
   name: '北京',value: this.randomData() },
                            {
   name: '天津',value: this.randomData() },
                            {
   name: '上海',value: this.randomData() },
                            {
   name: '重庆',value: this.randomData() },
                            {
   name: '河北',value: this.randomData() },
                            {
   name: '河南',value: this.randomData() },
                            {
   name: '云南',value: this.randomData() },
                            {
   name: '辽宁',value: this.randomData() },
                            {
   name: '黑龙江',value: this.randomData() },
                            {
   name: '湖南',value: this.randomData() },
                            {
   name: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts 是一款基于 JavaScript 的开源可视化图表库,支持多种类型的图表,如折线图、柱状图、饼图等。下面简单介绍一下使用 ECharts 实现可视化图表的步骤: 1. 引入 ECharts 库 在 HTML 文件中引入 ECharts 库,可以从官网下载或使用 CDN 引入。例如: ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> ``` 2. 准备数据 准备需要展示的数据,可以是数组、对象等形式。例如: ```javascript const data = [ { name: '北京', value: 100 }, { name: '上海', value: 200 }, { name: '广州', value: 150 }, { name: '深圳', value: 180 } ]; ``` 3. 创建容器 在 HTML 文件中创建一个容器,用于展示图表。例如: ```html <div id="chart" style="width: 600px; height:400px"></div> ``` 4. 初始化图表 在 JavaScript 文件中获取容器对象,并使用 ECharts 的 `init` 方法初始化图表。例如: ```javascript const chart = echarts.init(document.getElementById('chart')); ``` 5. 配置图表 使用 ECharts 的配置项配置图表,如设置图表类型、数据、颜色、样式等。例如: ```javascript const option = { title: { text: '城市销售情况' }, tooltip: {}, xAxis: { data: data.map(item => item.name) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(item => item.value), itemStyle: { color: '#3398DB' } }] }; ``` 6. 渲染图表 使用 ECharts 的 `setOption` 方法渲染图表。例如: ```javascript chart.setOption(option); ``` 完整的代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts Demo</title> <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> </head> <body> <div id="chart" style="width: 600px; height:400px"></div> <script> const data = [ { name: '北京', value: 100 }, { name: '上海', value: 200 }, { name: '广州', value: 150 }, { name: '深圳', value: 180 } ]; const chart = echarts.init(document.getElementById('chart')); const option = { title: { text: '城市销售情况' }, tooltip: {}, xAxis: { data: data.map(item => item.name) }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: data.map(item => item.value), itemStyle: { color: '#3398DB' } }] }; chart.setOption(option); </script> </body> </html> ``` 这是一个简单的柱状图示例,实际上 ECharts 还支持更多类型的图表和更多的配置项。需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值