Vue+ECharts 实现散点图

本文将指导你如何使用Vue.js框架结合ECharts库,详细步骤解析创建散点图的过程,包括安装ECharts,引入图表组件,配置散点图选项,以及在Vue实例中动态更新数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
​

<template>

<!--为echarts准备一个具备大小的容器dom-->

<div id="main01" style="width: 700px;height: 400px;"></div>

</template>

<script>

import * as echarts from 'echarts'

export default {

name: 'chartFirst',

props:{

//上级传入

list:{

type:Array,

default:[]

}

},

data () {

return {

// list:null,//数据

xMax:0,//X轴最大值

xInterval:10,//X轴刻度间隔

yMax:0,//y轴最大值

yInterval:10,//y轴刻度间隔

sdata:[],//显示数据

}

},

methods:{

drawPie(id){

this.charts = echarts.init(document.getElementById(id))

this.charts.setOption({

xAxis: {

name:"风向(°)",

max:100,//x轴最大值

interval:10,//x轴刻度大小

splitLine: { //坐标轴内竖线

lineStyle: {

color:"\tGainsboro", //没有颜色即不显示

}

},

axisLine: { //直角坐标系x轴

show:true,

lineStyle: { //为了不让轴上的数字显现

color:"black",

}

}

},

yAxis: {

name:"风速(m/s)",

max:18,//y轴最大值

interval:2,//y轴刻度大小

splitLine: { //坐标轴内横线

lineStyle: {

color:"\tGainsboro", //没有颜色即不显示

}

},

axisLine: { //直角坐标系y轴

show:true,

lineStyle: { //为了不让轴上的数字显现

color:"black",

}

}

},

title: {

text: '风速函数图',

left: 'center',

tap:2,

top: 20

},

/* tooltip: { //触发提示框

trigger: 'item', //触发器

// formatter: '{a}: ({c})', //触发器触发时显示的内容,'({c})'为坐标

formatter: function (obj) {

var value = obj.value;

return schema[0].text + ':' + value[2] + '<br>'

+schema[1].text + ':' + value[3] + '<br>'

+schema[2].text + ':' + value[4] + '<br>'

}

},*/

series: [{

symbol:'', // 点的图形:长方形,若不写默认为圆

symbolSize: 4, //点的大小

color:'red', //点的颜色

itemStyle: {

normal: {

// opacity: 0.001, //图元以及其附属物(如文字标签)的透明度

}

},

/* data: [ //点的位置(坐标轴范围最大与最小根据数据本身的范围自动定)

[0,0],

[5,80],

[4,20],

[2,40],

[8,4],

[10,60],

[8,60],

[10,18],

],*/

data:this.sdata,

type: 'scatter' //鼠标放到点上时鼠标的样式(此处为小手)。同 css 的鼠标样式,默认 'default'。

}]

})

},

getList(){

// listFshst().then(res=>{

// this.list = res.data;

console.log(this.list);

if(this.list){

this.list.forEach(rv=>{

this.tdata = [];

// 最大值

this.tdata.push(rv.fx);

this.tdata.push(rv.fs);

this.sdata.push(this.tdata);

if(this.xMax<rv.fs){

this.xMax=rv.fs;

}

if(this.yMax<rv.qy){

this.yMax=rv.qy;

}

this.xInterval=this.xMax/10;

this.yInterval = this.yMax/10;

})

}

this.$nextTick(function() {

console.log("main====01")

this.drawPie('main01')

})

// })

}

},

//调用

mounted(){

this.getList()

}

}

</script>

<style scoped>

* {

margin: 0;

padding: 0;

list-style: none;

}

</style>

​

Vue.jsECharts结合使用时,绘制3D散点图可以创建动态交互的数据可视化效果。首先,你需要安装Vue-ECharts插件,它提供了将ECharts集成到Vue组件中的便利方式。以下是基本步骤: 1. **引入依赖**: - 在`main.js`文件或其他入口文件中引入VueECharts及其Vue封装库: ```javascript import Vue from 'vue'; import ECharts from 'echarts-for-vue'; Vue.use(ECharts); ``` 2. **创建组件**: 创建一个新的Vue组件,例如`ThreeDSpotChart.vue`,并初始化ECharts实例: ```html <template> <div ref="chart" style="width: 800px;height:600px;"></div> </template> <script> export default { data() { return { chartInstance: null, }; }, mounted() { this.initChart(); }, methods: { initChart() { this.chartInstance = this.$echarts.init(this.$refs.chart); // ...后续配置3D散点图选项 } }, }; </script> ``` 3. **配置图表**: 使用ECharts提供的API设置3D散点图的配置项,包括数据、坐标系、视觉样式等: ```javascript methods: { ..., initChart() { const options = { tooltip: {}, visualMap: {}, // 可能需要视觉映射区域 xAxis3D: { type: 'value' }, // 设置x轴 yAxis3D: { type: 'value' }, // 设置y轴 zAxis3D: { type: 'value' }, // 设置z轴 series: [ { type: 'scatter3D', // 选择3D散点图类型 data: [], // 数据源 coordinateSystem: 'cartesian3d', symbolSize: function(data) { return Math.sqrt(data[2] / 10); // 根据第三个值调整大小 }, }, ], }; this.chartInstance.setOption(options); }, }, ``` 4. **使用组件**: 在其他Vue组件中引用并使用这个3D散点图组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值