当前位置:首页 > VUE

vue实现散点图源码

2026-03-29 15:28:59VUE

以下是一个基于 Vue 和 ECharts 实现散点图的完整源码示例,包含配置和交互逻辑:

安装依赖

确保已安装 Vue 和 ECharts:

npm install vue echarts

基础实现代码

<template>
  <div ref="scatterChart" style="width: 600px; height: 400px"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'ScatterChart',
  mounted() {
    this.initChart();
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.scatterChart;
      const myChart = echarts.init(chartDom);

      const option = {
        title: {
          text: '散点图示例'
        },
        tooltip: {},
        xAxis: {
          name: 'X轴'
        },
        yAxis: {
          name: 'Y轴'
        },
        series: [{
          symbolSize: 20,
          data: [
            [10.0, 8.04],
            [8.0, 6.95],
            [13.0, 7.58],
            [9.0, 8.81],
            [11.0, 8.33],
            [14.0, 9.96],
            [6.0, 7.24],
            [4.0, 4.26],
            [12.0, 10.84],
            [7.0, 4.82],
            [5.0, 5.68]
          ],
          type: 'scatter'
        }]
      };

      myChart.setOption(option);

      // 响应式调整
      window.addEventListener('resize', function() {
        myChart.resize();
      });
    }
  }
};
</script>

进阶功能实现

添加动态数据更新和点击事件:

methods: {
  initChart() {
    // ...基础配置同前

    // 动态更新示例
    setInterval(() => {
      const newData = this.generateRandomData();
      myChart.setOption({
        series: [{
          data: newData
        }]
      });
    }, 2000);

    // 点击事件
    myChart.on('click', (params) => {
      console.log('点击数据点:', params.data);
    });
  },
  generateRandomData() {
    return Array.from({length: 10}, () => [
      Math.random() * 20,
      Math.random() * 10
    ]);
  }
}

样式优化配置

option = {
  // ...其他配置
  series: [{
    itemStyle: {
      color: function(params) {
        const colorList = ['#c23531','#2f4554','#61a0a8'];
        return colorList[params.dataIndex % 3];
      }
    },
    label: {
      show: true,
      formatter: ({data}) => `(${data[0]}, ${data[1]})`
    }
  }]
}

注意事项

  1. 需要在 mounted 生命周期初始化图表,确保 DOM 已渲染

  2. 组件销毁时应移除事件监听和图表实例:

    beforeDestroy() {
    if (this.myChart) {
     this.myChart.dispose();
     window.removeEventListener('resize', this.resizeHandler);
    }
    }
  3. 大数据量场景建议启用渐进渲染:

    vue实现散点图源码

    series: [{
    progressive: 1000,
    progressiveThreshold: 3000
    }]

标签: 源码vue
分享给朋友:

相关文章

vue 路由实现

vue 路由实现

Vue 路由实现方法 安装 Vue Router 使用 npm 或 yarn 安装 Vue Router 依赖包: npm install vue-router # 或 yarn add vue-r…

vue实现菜单栏锚点

vue实现菜单栏锚点

实现锚点菜单的基本思路 在Vue中实现菜单栏锚点功能,主要涉及两个方面:创建可跳转的锚点位置,以及菜单项的点击滚动控制。通过监听滚动事件可以高亮当前可见区域的对应菜单项。 创建页面锚点位置 在需要跳…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现增删

vue实现增删

Vue 实现增删功能 在 Vue 中实现增删功能通常涉及数据绑定、事件处理和列表渲染。以下是基于 Vue 2 或 Vue 3 的实现方法: 数据定义 初始化一个数组用于存储列表数据,并在 Vue 实…

vue实现dialog

vue实现dialog

Vue 实现 Dialog 的方法 使用 Vue 原生组件实现 Vue 可以通过组件化的方式实现 Dialog,以下是一个简单的实现示例: <template> <div>…

vue实现流程转化

vue实现流程转化

Vue 实现流程转化 在 Vue 中实现流程转化通常涉及多个步骤,包括状态管理、组件通信和动态渲染。以下是几种常见的实现方法: 使用 Vuex 进行状态管理 Vuex 是 Vue 的官方状态管理库,…