当前位置:首页 > 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实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue 实现工具

vue 实现工具

Vue 实现工具的方法 使用 Vue 实现工具功能可以通过多种方式完成,具体取决于工具的类型和需求。以下是几种常见的方法: 组件化实现 创建一个独立的 Vue 组件来封装工具功能。例如,实现一个计…

vue实现treeselect

vue实现treeselect

Vue TreeSelect 实现方法 使用 Vue 实现 TreeSelect 组件可以通过以下几种方式完成,包括使用现成的第三方库或自行封装组件。 使用第三方库 推荐使用成熟的第三方 TreeS…

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…