当前位置:首页 > 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 依赖:

vue实现散点图

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 依赖:

vue实现散点图

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);
}

数据动态更新

实现数据动态更新功能:

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

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

相关文章

vue实现sql操作

vue实现sql操作

在Vue中实现SQL操作通常需要结合后端服务或本地数据库技术,因为前端JavaScript无法直接连接SQL数据库。以下是几种常见的实现方式: 通过API与后端交互 Vue前端通过HTTP请求(如a…

vue实现网页切换

vue实现网页切换

Vue 实现网页切换的方法 在 Vue 中实现网页切换通常可以通过以下几种方式完成,具体取决于项目需求和路由管理方式。 使用 Vue Router Vue Router 是 Vue.js 官方提供的…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDi…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…