echarts项目积累

按js文件或cdn导入

javascript 复制代码
 <script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script>

在main.js中注册到全局的对象原型上

javascript 复制代码
Vue.prototype.$echarts = window.echarts

搭建echart模版

分拆option

分为数据,初始化,自适应三个方向

javascript 复制代码
<script>
export default {
  data () {
    return {
      chartInstance: null,
      allData: null
   }
 },
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
 },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
 },
  methods: {
  // 初始化chart
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref)
      const initOption = {}
      this.chartInstance.setOption(initOption)
   },
    async getData () {
  // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      this.updateChart()
   },
    updateChart () {
      // 处理图表需要的数据
      const dataOption = {}
      this.chartInstance.setOption(dataOption)
   },
   // 自适应chart
    screenAdapter () {
      const adapterOption = {}
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
   }
 }
}
</script>

使用import命令来导入echarts

完全导入

javascript 复制代码
import * as echarts from 'echarts'

后面操作一样

按需导入

官网说明

引入核心包

javascript 复制代码
import * as echarts from 'echarts/core'

引入图标类型

javascript 复制代码
import { LineChart } from 'echarts/charts'

引入需要使用的组件

javascript 复制代码
import { GridComponent } from 'echarts/components' // 引入组件

引入渲染器

javascript 复制代码
import { CanvasRenderer } from 'echarts/renderers'

注册所有引入

javascript 复制代码
echarts.use([
  LineChart,
  GridComponent,
  CanvasRenderer
])

地图的使用步骤

首先获取json数据

init图表后,使用

javascript 复制代码
this.$echarts.registerMap('china', res)

创建地图实例

javascript 复制代码
const initOption = {
        geo: {
          type: 'map',
          map: 'china'
        }
      }

在option中用geo绑定map的实例

javascript 复制代码
 this.chartInstance.setOption(initOption)

创建图表

在地图中加入散点图

首先配置series

javascript 复制代码
updataChart () {
      const seriesArr = this.mapData.map(item => {
        return {
          type: 'effectScatter',
          rippleEffect: {
            // 涟漪大小
            scale: 5,
            brushType: 'stroke' // 涟漪效果
          },
          name: item.name,
          data: item.children,
          //   散点使用地图坐标
          coordinateSystem: 'geo'
        }
      })
      const dataOption = {
        legend: {},
        series: seriesArr
      }
      this.chartInstance.setOption(dataOption)
    },

定义type为effectScatter

data为{name: xx, value: \[维度,维度},{ }, { }]格式 或 \[12, 14, 34, 50]直接这样的格式(具体看官网)

使用coordinateSystem: 'geo'绑定地图,使用地图坐标

legend会自动匹配给定的name

javascript 复制代码
backChina () {
      const backOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(backOption)
    },

对已有实例的图表只需要切换map就可以实现切换

相关推荐
小兔子10 分钟前
Python 的 GIL 与 free-threading:3.13 之后「去 GIL」走到哪一步了
前端
IT_陈寒16 分钟前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
guslegend1 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
小宋10211 小时前
A2UI从零实战:Agent安全生成交互界面与事件回传
javascript·人工智能·安全·交互
大包子2 小时前
structuredClone vs lodash.cloneDeep:核心差异与选型指南
javascript·vue.js
海码事务所2 小时前
Google Play 新个人开发者账号上架指南:12 人连续 14 天封闭测试怎么做?
前端
田威AI2 小时前
图片内文字翻译的规格:输入输出、保真、自动化、时间与费用
前端·计算机视觉
不可能片场2 小时前
命令行中文变问号 我用环境变量救了场
前端·electron
lerhxx2 小时前
AI 应用中的上下文管理 —— 从"上下文窗口"到"分层压缩"的工程实践
前端·javascript
骑着蜗牛撵大象3272 小时前
多 Agent 分治协作:MapReduce 模式下的并行扇出与结果归并
前端