当前位置:首页 > VUE

vue实现散点图

2026-02-10 14:49:23VUE

Vue 实现散点图的方法

使用 ECharts 库

安装 ECharts 依赖:

npm install echarts --save

在 Vue 组件中引入并使用:

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

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

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

      const option = {
        xAxis: {},
        yAxis: {},
        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);
    }
  }
};
</script>

使用 Chart.js 库

安装 Chart.js 依赖:

npm install chart.js --save

创建散点图组件:

<template>
  <canvas ref="chart"></canvas>
</template>

<script>
import { Chart, registerables } from 'chart.js';
Chart.register(...registerables);

export default {
  mounted() {
    this.renderChart();
  },
  methods: {
    renderChart() {
      const ctx = this.$refs.chart.getContext('2d');
      new Chart(ctx, {
        type: 'scatter',
        data: {
          datasets: [{
            label: 'Scatter Dataset',
            data: [{
              x: -10,
              y: 0
            }, {
              x: 0,
              y: 10
            }, {
              x: 10,
              y: 5
            }],
            backgroundColor: 'rgb(255, 99, 132)'
          }]
        },
        options: {
          scales: {
            x: {
              type: 'linear',
              position: 'bottom'
            }
          }
        }
      });
    }
  }
};
</script>

使用 D3.js 实现更高级定制

安装 D3.js 依赖:

npm install d3 --save

创建自定义散点图组件:

<template>
  <svg ref="svg" width="500" height="300"></svg>
</template>

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

export default {
  mounted() {
    this.drawScatterPlot();
  },
  methods: {
    drawScatterPlot() {
      const data = [
        { x: 10, y: 20 },
        { x: 20, y: 50 },
        { x: 40, y: 10 },
        { x: 60, y: 40 },
        { x: 80, y: 20 }
      ];

      const svg = d3.select(this.$refs.svg);
      const margin = { top: 20, right: 20, bottom: 30, left: 40 };
      const width = +svg.attr('width') - margin.left - margin.right;
      const height = +svg.attr('height') - margin.top - margin.bottom;

      const x = d3.scaleLinear()
        .domain([0, 100])
        .range([0, width]);

      const y = d3.scaleLinear()
        .domain([0, 60])
        .range([height, 0]);

      const g = svg.append('g')
        .attr('transform', `translate(${margin.left},${margin.top})`);

      g.append('g')
        .attr('transform', `translate(0,${height})`)
        .call(d3.axisBottom(x));

      g.append('g')
        .call(d3.axisLeft(y));

      g.selectAll('circle')
        .data(data)
        .enter().append('circle')
        .attr('cx', d => x(d.x))
        .attr('cy', d => y(d.y))
        .attr('r', 5)
        .attr('fill', 'steelblue');
    }
  }
};
</script>

响应式处理

添加窗口大小变化时的图表重绘:

methods: {
  initChart() {
    // ...之前的图表初始化代码

    window.addEventListener('resize', () => {
      myChart.resize();
    });
  }
},
beforeDestroy() {
  window.removeEventListener('resize', this.resizeHandler);
}

数据动态更新

实现数据动态更新功能:

vue实现散点图

methods: {
  updateData(newData) {
    this.chart.setOption({
      series: [{
        data: newData
      }]
    });
  }
}

标签: vue散点图
分享给朋友:

相关文章

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue 实现分屏

vue 实现分屏

Vue 实现分屏的方法 使用 CSS Flexbox 或 Grid 布局 通过 CSS Flexbox 或 Grid 布局可以轻松实现分屏效果。在 Vue 组件中定义两个或多个区域,并使用 CSS 控…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue实现hexo

vue实现hexo

Vue 集成 Hexo 的实现方法 Hexo 是一个静态博客框架,而 Vue 是一个前端框架。将 Vue 集成到 Hexo 中可以通过以下方式实现: 在 Hexo 中使用 Vue 组件 通过 Hex…

vue实现梯形

vue实现梯形

在Vue中实现梯形效果,可以通过CSS的transform属性或clip-path属性来实现。以下是几种常见的实现方法: 使用CSS transform实现梯形 通过CSS的transform: s…