当前位置:首页 > VUE

vue实现散点图源码

2026-02-17 15:28:57VUE

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

安装依赖

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

vue实现散点图源码

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>

在父组件中使用

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

vue实现散点图源码

<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 的响应式特性,当数据变化时图表会自动更新:

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

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

相关文章

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身: &…

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue实现双向

vue实现双向

Vue 实现双向绑定的方法 Vue 的双向绑定主要通过 v-model 指令实现,它结合了数据绑定和事件监听,适用于表单元素(如 input、select、textarea 等)。以下是几种常见的实现…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…