当前位置:首页 > 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实现导航栏切换的方法 使用v-for和v-bind动态渲染导航项 通过v-for循环遍历导航数据数组,结合v-bind动态绑定class或style。当点击导航项时,更新当前选中项的索引或ID。…

vue实现表格

vue实现表格

Vue 实现表格的方法 在 Vue 中实现表格可以通过多种方式完成,以下是几种常见的方法: 使用原生 HTML 表格 Vue 可以直接渲染原生 HTML 表格,通过 v-for 动态生成表格行和列…

vue实现头像剪切

vue实现头像剪切

Vue 实现头像剪切的实现方法 使用 vue-cropperjs 库 vue-cropperjs 是一个基于 cropperjs 的 Vue 组件,专门用于图片裁剪。安装方式如下: npm ins…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template>…

vue公共列表的实现

vue公共列表的实现

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

vue设计与实现下载

vue设计与实现下载

vue设计与实现电子书下载 《Vue.js设计与实现》是一本深入解析Vue.js框架原理的书籍,由霍春阳(HcySunYang)撰写。以下是获取该资源的常见途径: 正版购买渠道 京东、当当、天…