当前位置:首页 > VUE

vue菱形图实现

2026-01-18 15:29:38VUE

Vue 实现菱形图的方法

使用 CSS transform 旋转正方形

通过 CSS 的 transform 属性将正方形旋转 45 度即可生成菱形。在 Vue 中可以通过动态绑定样式实现。

<template>
  <div class="diamond-container">
    <div 
      v-for="(item, index) in items" 
      :key="index" 
      class="diamond"
      :style="{ backgroundColor: item.color }"
    >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { label: 'A', color: '#ff7675' },
        { label: 'B', color: '#74b9ff' },
        { label: 'C', color: '#55efc4' }
      ]
    }
  }
}
</script>

<style>
.diamond-container {
  display: flex;
  gap: 20px;
}

.diamond {
  width: 100px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: rotate(45deg);
  color: white;
  font-weight: bold;
}

.diamond span {
  transform: rotate(-45deg);
}
</style>

使用 SVG 绘制菱形

SVG 的 polygon 元素可以精确绘制菱形,通过 Vue 动态生成 points 属性。

<template>
  <svg width="300" height="200">
    <polygon 
      v-for="(item, index) in items"
      :key="index"
      :points="getDiamondPoints(index)"
      :fill="item.color"
    />
    <text 
      v-for="(item, index) in items"
      :key="'text-' + index"
      :x="50 + index * 100"
      :y="100"
      fill="white"
      text-anchor="middle"
    >
      {{ item.label }}
    </text>
  </svg>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { label: 'X', color: '#e84393' },
        { label: 'Y', color: '#0984e3' },
        { label: 'Z', color: '#00b894' }
      ]
    }
  },
  methods: {
    getDiamondPoints(index) {
      const x = 50 + index * 100
      return `${x},50 ${x+40},100 ${x},150 ${x-40},100`
    }
  }
}
</script>

使用 Canvas 绘制

通过 Canvas 的路径绘制功能可以创建菱形图形,适合需要动态交互的场景。

<template>
  <canvas ref="canvas" width="400" height="200"></canvas>
</template>

<script>
export default {
  mounted() {
    this.drawDiamonds()
  },
  data() {
    return {
      diamonds: [
        { x: 50, y: 100, size: 40, color: '#fdcb6e' },
        { x: 150, y: 100, size: 40, color: '#e17055' },
        { x: 250, y: 100, size: 40, color: '#6c5ce7' }
      ]
    }
  },
  methods: {
    drawDiamonds() {
      const ctx = this.$refs.canvas.getContext('2d')
      this.diamonds.forEach(d => {
        ctx.beginPath()
        ctx.moveTo(d.x, d.y - d.size)
        ctx.lineTo(d.x + d.size, d.y)
        ctx.lineTo(d.x, d.y + d.size)
        ctx.lineTo(d.x - d.size, d.y)
        ctx.closePath()
        ctx.fillStyle = d.color
        ctx.fill()
      })
    }
  }
}
</script>

使用第三方图表库

对于复杂的菱形图表,可以使用 ECharts 等专业图表库,通过配置项实现菱形系列。

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 chart = echarts.init(this.$refs.chart)
      const option = {
        series: [{
          type: 'scatter',
          symbol: 'diamond',
          symbolSize: 30,
          data: [
            [10, 20],
            [30, 40],
            [50, 60]
          ],
          itemStyle: {
            color: function(params) {
              const colors = ['#a29bfe', '#fd79a8', '#ffeaa7']
              return colors[params.dataIndex]
            }
          }
        }]
      }
      chart.setOption(option)
    }
  }
}
</script>

这些方法涵盖了从基础 CSS 实现到专业图表库的应用,可以根据项目需求选择适合的方案。CSS 方案适合简单展示,SVG/Canvas 适合需要自定义交互的场景,而图表库则适合数据可视化需求。

标签: 菱形vue
分享给朋友:

相关文章

vue实现getapp

vue实现getapp

Vue 中实现全局获取应用实例的方法 在 Vue 3 中,可以通过 getCurrentInstance 方法获取当前组件实例,但需要明确其使用场景和限制。以下是具体实现方式: 方法一:通过 get…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…

vue实现 hover

vue实现 hover

实现 Vue 中的 hover 效果 在 Vue 中实现 hover 效果可以通过多种方式完成,以下是常见的几种方法: 使用 CSS 伪类 最简单的方式是直接使用 CSS 的 :hover 伪类,无…

vue 实现长按

vue 实现长按

实现长按功能的基本思路 在Vue中实现长按功能,通常需要监听元素的mousedown或touchstart事件,启动一个计时器。当用户按住元素超过设定的时间阈值时,触发长按回调函数。如果在时间阈值内触…

vue 实现音乐

vue 实现音乐

Vue 实现音乐播放功能 在 Vue 中实现音乐播放功能,可以通过 HTML5 的 <audio> 元素或第三方库如 howler.js 来实现。以下是两种方法的详细步骤: 使用 HTM…

vue实现登录拦截

vue实现登录拦截

实现登录拦截的方法 在Vue项目中,可以通过路由守卫(Navigation Guards)实现登录拦截,确保未登录用户无法访问受保护的路由。以下是具体实现方式: 使用全局前置守卫 在路由配置文件中(…