当前位置:首页 > VUE

vue实现散点图源码

2026-02-17 15:28:57VUE

以下是一个基于 Vue 和 ECharts 实现散点图的完整源码示例,分为组件封装和基础使用两部分:

安装依赖

确保项目中已安装 ECharts 和 Vue-ECharts:

npm install echarts vue-echarts

封装可复用的散点图组件

创建 ScatterChart.vue 文件:

<template>
  <v-chart 
    class="scatter-chart" 
    :option="option" 
    :autoresize="true"
  />
</template>

<script>
import { use } from 'echarts/core';
import { ScatterChart } from 'echarts/charts';
import {
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
} from 'echarts/components';
import { CanvasRenderer } from 'echarts/renderers';
import VChart from 'vue-echarts';

use([
  ScatterChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent,
  CanvasRenderer
]);

export default {
  components: { VChart },
  props: {
    data: {
      type: Array,
      required: true
    },
    xAxisName: {
      type: String,
      default: 'X轴'
    },
    yAxisName: {
      type: String,
      default: 'Y轴'
    }
  },
  computed: {
    option() {
      return {
        title: {
          text: '散点图示例'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {
          data: ['数据集1']
        },
        xAxis: {
          name: this.xAxisName,
          type: 'value'
        },
        yAxis: {
          name: this.yAxisName,
          type: 'value'
        },
        series: [{
          name: '数据集1',
          type: 'scatter',
          symbolSize: 10,
          data: this.data,
          itemStyle: {
            color: '#5470c6'
          }
        }]
      };
    }
  }
};
</script>

<style scoped>
.scatter-chart {
  width: 100%;
  height: 400px;
}
</style>

在父组件中使用

在需要展示散点图的页面中引入组件:

<template>
  <div>
    <scatter-chart 
      :data="chartData" 
      x-axis-name="温度(℃)" 
      y-axis-name="销量(件)"
    />
  </div>
</template>

<script>
import ScatterChart from './components/ScatterChart.vue';

export default {
  components: { ScatterChart },
  data() {
    return {
      chartData: [
        [10, 35],
        [15, 48],
        [20, 60],
        [25, 72],
        [30, 85],
        [35, 92],
        [40, 78]
      ]
    };
  }
};
</script>

高级配置选项(可选)

若需要更复杂的散点图,可修改 option 计算属性:

option() {
  return {
    // ...其他配置
    series: [{
      type: 'scatter',
      symbolSize: function (data) {
        return Math.sqrt(data[1]) * 2;
      },
      label: {
        show: true,
        formatter: function (param) {
          return param.data[0] + ',' + param.data[1];
        }
      },
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }]
  };
}

动态更新数据

通过 Vue 的响应式特性,当数据变化时图表会自动更新:

vue实现散点图源码

// 在父组件中
methods: {
  updateData() {
    this.chartData = [
      ...this.chartData,
      [Math.random() * 50, Math.random() * 100]
    ];
  }
}

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

相关文章

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue实现摘要

vue实现摘要

Vue 实现摘要的方法 在 Vue 中实现文本摘要功能通常涉及截取文本的前部分内容并添加省略号。可以通过计算属性、过滤器或自定义指令来实现。 计算属性实现 在 Vue 组件中定义一个计算属性,用于截…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert 生成…

vue实现网站

vue实现网站

Vue 实现网站的基本步骤 Vue.js 是一个渐进式 JavaScript 框架,适合构建现代化的单页应用(SPA)或动态网站。以下是使用 Vue 实现网站的基本方法。 初始化 Vue 项目 使用…

vue实现画板

vue实现画板

Vue 实现画板功能 在 Vue 中实现画板功能可以通过 HTML5 的 Canvas API 实现。以下是一个完整的实现方案: 创建 Vue 组件 新建一个 Vue 单文件组件(SFC),命名为…

vue 实现table

vue 实现table

Vue 实现 Table 的方法 使用原生 HTML 表格 通过 Vue 的 v-for 指令动态渲染表格数据,适合简单表格场景。 <template> <table>…