uniapp 使用echarts 在一个页面上用组件形式展示多个图表

uniapp 使用echarts 在一个页面上用组件形式展示多个图表,可是只显示了一个要么直接不显示了
解决方法:用组件形式引用可解决
下面是代码片段

<barchart ref="barcharts"></barchart>
<linechart ref="linecharts"></linechart>```
```javascript
import barchart from "./barchart.vue"
import linechart from "./linechart.vue"
export default {
		components: {
			barchart,
			linechart
		},
		data(){
			return{
				maxIndex:0,
				BarServeData:{},
				LineServeData:{}
			}
		},
		methods: {
			this.$nextTick(() => {
			    this.$refs.barcharts.IntervalChart(this.maxIndex,this.BarServeData);
			    this.$refs.barcharts.IntervalChart(this.BarServeData);
			})
		}

barchart 代码

<template>
	<view style="width: 100%;height: 100%;">
		<uni-ec-canvas
			class="uni-ec-canvas"
			id="bar-chart"
			ref="BarchartDom"
			canvas-id="multi-charts-bar"
			:ec="BarOption"
		/>
	</view>
</template>

<script>
	import uniEcCanvas from "@/components/uni-ec-canvas/uni-ec-canvas.vue";
	export default{
		components: {
			uniEcCanvas
		},
		data(){
			return{
				BarOption: {
					option:{}
				},
			}
		},
		methods:{
			IntervalChart(maxIndex,data){
				this.$refs['BarchartDom'].$curChart.clear()
				let option = {
					//配置项是写在option属性下  和echarts配置一样 参考其官网就行
					color: ["#1890FF"],
					// color: ["#FAC858","#91CB74","#EE6666","#1890FF","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
					title:{
						text:'单位(KW)',
						textStyle: {
							fontSize: 12,
							fontWeight: 400,
							color: "#333",
							fontFamily: "Microsoft YaHei",
						},
					},
					legend:{
						top:0,
						data: []
					},
					grid:{
						top:"25px",
						left: 0,
						right: 0,
						bottom: 0,
						containLabel: true
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: JSON.parse(JSON.stringify(data.dataAxis))
					},
					yAxis:{
						type: 'value',
						axisLine: {
							show: true,
						},
						axisLabel: {
							formatter: "{value} kW",
						},
					},
					tooltip:{
						trigger: "axis",//item 单项,axis 横坐标轴上的所有项
						// 相对位置
						position: ['30%', '20%'],
						backgroundColor:"rgba(32, 33, 36,.7)",
						borderColor: "rgba(32, 33, 36,0.20)",
						borderWidth: 1,
						textStyle: {
							// 文字提示样式
							color: "#fff",
							fontSize: "12",
						},
						axisPointer: {
							// 坐标轴虚线
							type: "cross",
							label: {
								backgroundColor: "#6a7985",
							}
						},
					},
					series:[{
						type: "bar",
						showBackground: false,
						itemStyle: {
							color: function(params) {
								console.log(params)
								// 根据索引判断柱子颜色
								if (params.value == maxIndex) {
									return '#e04e4e'; // 最大值柱子颜色为红色
								}  else {
									return "#2378f7" // 其他柱子使用蓝色
								}
							}
						},
						emphasis: {
							itemStyle: {
								color: "#EE6666" ,
							},
						},
						data: JSON.parse(JSON.stringify(data.data)),
					}],
					dataZoom: [
					{
						borderRadius: 3,
						borderColor: "rgba(241,224,90,0.5)",
						dataBackgroundColor: "#f2f2f2",
						backgroundColor: "#f2f2f2",
						handleIcon:
						  "M512 512m-208 0a6.5 6.5 0 1 0 416 0 6.5 6.5 0 1 0-416 0Z M512 192C335.264 192 192 335.264 192 512c0 176.736 143.264 320 320 320s320-143.264 320-320C832 335.264 688.736 192 512 192zM512 800c-159.072 0-288-128.928-288-288 0-159.072 128.928-288 288-288s288 128.928 288 288C800 671.072 671.072 800 512 800z",
						handleColor: "#e6edd4",
						fillerColor: "rgba(241,224,90,0.18)", //选中范围的填充颜色
						filterMode: "filter",
						handleSize: 15,
						type: "inside", inside 鼠标手指缩放 slider图表下方的伸缩条
						show: true, //是否显示
						realtime: true, //拖动时,是否实时更新系列的视图
						// start: 0, //伸缩条开始位置(1-100),可以随时更改
						// end: 100, //伸缩条结束位置(1-100),可以随时更改
					  },
					],
				};
				this.BarOption.option = JSON.parse(JSON.stringify(option))
			},
		}
	}
</script>

<style>
	.uni-ec-canvas{
		display: block;
		width: 100%;
		height: 100%;
	}
</style>

linechart 代码

<template>
	<view style="width: 100%;height: 100%;">
		<uni-ec-canvas
			class="uni-ec-canvas"
			id="line-chart"
			ref="LinechartDom"
			canvas-id="multi-charts-line"
			:ec="LineOption"
		/>
	</view>
</template>
<script>
	import uniEcCanvas from "@/components/uni-ec-canvas/uni-ec-canvas.vue";
	export default{
		components: {
			uniEcCanvas
		},
		data(){
			return{
				LineOption: {
					option:{}
				},
			}
		},
		methods:{
			TimeChart(data){
				console.log(data)
				this.$refs['LinechartDom'].$curChart.clear()
				let option = {
					//配置项是写在option属性下  和echarts配置一样 参考其官网就行
					color: ["#FAC858"],
					// color: ["#FAC858","#91CB74","#EE6666","#1890FF","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
					title:{
						text:'单位(KW)',
						textStyle: {
							fontSize: 12,
							fontWeight: 400,
							color: "#333",
							fontFamily: "Microsoft YaHei",
						},
					},
					legend:{
						top:0,
						data:[]
					},
					grid:{
						top:"25px",
						left: 0,
						right: 0,
						bottom: 0,
						containLabel: true
					},
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: JSON.parse(JSON.stringify(data.dataAxis))
					},
					yAxis:{
						type: 'value',
						axisLine: {
							show: true,
						},
						axisLabel: {
							formatter: "{value} kW",
						},
					},
					tooltip:{
						trigger: "axis",//item 单项,axis 横坐标轴上的所有项
						// 相对位置
						position: ['30%', '20%'],
						backgroundColor:"rgba(32, 33, 36,.7)",
						borderColor: "rgba(32, 33, 36,0.20)",
						borderWidth: 1,
						textStyle: {
							// 文字提示样式
							color: "#fff",
							fontSize: "12",
						},
						axisPointer: {
							// 坐标轴虚线
							type: "cross",
							label: {
								backgroundColor: "#6a7985",
							}
						},
					},
					series:[{
						type: "line",
						showBackground: false,
						itemStyle: {
							color: function(params) {
								console.log(params)
								// 根据索引判断柱子颜色
								if (params.value == this.maxIndex) {
									return '#e04e4e'; // 最大值柱子颜色为红色
								}  else {
									return "#2378f7" // 其他柱子使用蓝色
								}
							}
						},
						emphasis: {
							itemStyle: {
								color: "#EE6666" ,
							},
						},
						data: JSON.parse(JSON.stringify(data.data)),
					}],
					dataZoom: [
					{
						borderRadius: 3,
						borderColor: "rgba(241,224,90,0.5)",
						dataBackgroundColor: "#f2f2f2",
						backgroundColor: "#f2f2f2",
						handleIcon:
						  "M512 512m-208 0a6.5 6.5 0 1 0 416 0 6.5 6.5 0 1 0-416 0Z M512 192C335.264 192 192 335.264 192 512c0 176.736 143.264 320 320 320s320-143.264 320-320C832 335.264 688.736 192 512 192zM512 800c-159.072 0-288-128.928-288-288 0-159.072 128.928-288 288-288s288 128.928 288 288C800 671.072 671.072 800 512 800z",
						handleColor: "#e6edd4",
						fillerColor: "rgba(241,224,90,0.18)", //选中范围的填充颜色
						filterMode: "filter",
						handleSize: 15,
						type: "inside", inside 鼠标手指缩放 slider图表下方的伸缩条
						show: true, //是否显示
						realtime: true, //拖动时,是否实时更新系列的视图
						// start: 0, //伸缩条开始位置(1-100),可以随时更改
						// end: 100, //伸缩条结束位置(1-100),可以随时更改
					  },
					],
				};
				
				this.LineOption.option = JSON.parse(JSON.stringify(option))
			}
		}
	}
</script>

<style >
	.uni-ec-canvas{
		display: block;
		width: 100%;
		height: 100%;
	}
</style>
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UniApp一个基于Vue.js的跨平台开发框架,可以用于开发微信小程序、H5、App多个平台的应用程序。而ECharts一个由百度开发的数据可视库,可以用于绘制各种图表和图形。 在UniApp使用ECharts来绘制图表需要以下几个步骤: 1. 安装ECharts插件:在UniApp项目中,可以通过npm安装ECharts插件,命令如下: ``` npm install echarts --save ``` 2. 引入ECharts组件:在需要使用ECharts页面组件中,引入ECharts组件,例如: ```vue <template> <view> <ec-canvas id="mychart" canvas-id="mychart" :ec="ec"></ec-canvas> </view> </template> <script> import * as echarts from 'echarts'; export default { data() { return { ec: { lazyLoad: true // 延迟加载 } }; }, onLoad() { this.initChart(); }, methods: { initChart() { this.$nextTick(() => { this.ecComponent = this.selectComponent('#mychart'); this.ecComponent.init((canvas, width, height) => { const chart = echarts.init(canvas, null, { width: width, height: height }); // 绘制图表 chart.setOption({ // 配置项 }); // 将图表实例绑定到this上,方便其他方法调用 this.chart = chart; return chart; }); }); } } }; </script> ``` 3. 配置ECharts图表:在`initChart`方法中,可以通过`chart.setOption`方法来配置ECharts图表的各种属性和数据,具体的配置项可以参考ECharts官方文档。 以上就是在UniApp使用ECharts绘制图表的基本步骤。通过这种方式,你可以在微信小程序使用ECharts展示各种图表和数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值