当前位置:首页 > VUE

vue实现散点图源码

2026-01-16 22:55:11VUE

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

安装依赖

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

npm install echarts vue-echarts

封装可复用的散点图组件

创建 ScatterChart.vue 文件:

<template>
  <v-chart class="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: {
    chartData: {
      type: Array,
      default: () => [
        [10, 20],
        [15, 30],
        [20, 25]
      ]
    },
    xAxisName: {
      type: String,
      default: "X轴"
    },
    yAxisName: {
      type: String,
      default: "Y轴"
    }
  },
  computed: {
    option() {
      return {
        title: {
          text: "散点图示例"
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross"
          }
        },
        xAxis: {
          name: this.xAxisName,
          type: "value"
        },
        yAxis: {
          name: this.yAxisName,
          type: "value"
        },
        series: [
          {
            type: "scatter",
            symbolSize: 10,
            data: this.chartData,
            itemStyle: {
              color: "#5470c6"
            }
          }
        ]
      };
    }
  }
};
</script>

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

在父组件中使用

示例父组件 App.vue

<template>
  <div id="app">
    <scatter-chart
      :chart-data="dataPoints"
      x-axis-name="商品价格"
      y-axis-name="销量"
    />
  </div>
</template>

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

export default {
  components: { ScatterChart },
  data() {
    return {
      dataPoints: [
        [12.5, 156],
        [24.8, 98],
        [18.6, 120],
        [30.2, 65],
        [15.3, 145]
      ]
    };
  }
};
</script>

动态更新数据示例

可通过方法更新数据:

methods: {
  updateData() {
    this.dataPoints = [
      [Math.random() * 30, Math.random() * 200],
      [Math.random() * 30, Math.random() * 200],
      [Math.random() * 30, Math.random() * 200]
    ];
  }
}

自定义配置项

支持通过 props 传递完整配置:

props: {
  customOption: {
    type: Object,
    default: null
  }
},
computed: {
  option() {
    return this.customOption || {
      // 默认配置
    };
  }
}

响应式设计

组件内置了 autoresize 属性,当容器尺寸变化时会自动调整图表大小。如需手动控制,可调用:

this.$refs.chart.resize();

该实现包含完整的类型定义、响应式设计和配置灵活性,可直接集成到 Vue 2/3 项目中。

vue实现散点图源码

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

相关文章

vue实现裁剪头像

vue实现裁剪头像

Vue 实现头像裁剪功能 实现头像裁剪功能通常需要结合第三方库如 cropperjs 或 vue-cropper。以下是两种常见实现方式: 使用 vue-cropper 库 安装依赖: n…

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容…

vue实现表格多行修改

vue实现表格多行修改

实现多行编辑表格 在Vue中实现表格的多行编辑功能,可以通过以下方法完成。这里以Element UI的表格组件为例,但思路适用于其他UI库或原生实现。 数据准备 定义表格数据和编辑状态管理变量:…

vue懒加载实现难吗

vue懒加载实现难吗

vue懒加载的实现难度 Vue懒加载的实现并不复杂,核心逻辑是通过动态导入(Dynamic Imports)和路由配置或组件异步加载完成。以下是具体实现方法: 路由懒加载实现 在Vue Rout…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

vue实现功能

vue实现功能

Vue 功能实现方法 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是实现常见功能的几种方法: 数据绑定与响应式更新 在 Vue 中,数据绑定通过 v-model…