当前位置:首页 > 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 等专业图表库,通过配置项实现菱形系列。

<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中实现无限滚动列表通常结合监听滚动事件和动态加载数据。以下是几种常见方法: 使用自定义指令 创建自定义指令v-infinite-scroll监听滚动事件: Vu…

vue实现全局遮罩层

vue实现全局遮罩层

实现全局遮罩层的方法 在Vue中实现全局遮罩层可以通过多种方式完成,以下是几种常见的实现方法: 使用Vue组件创建遮罩层 创建一个遮罩层组件,通过全局注册或动态挂载的方式实现全局调用。以下是一个简单…

vue实现一个系统

vue实现一个系统

Vue 系统实现步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目: npm init vue@latest my-system 选择需要的配置(Router、Pinia 等)。…

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现图集

vue实现图集

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

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue…