当前位置:首页 > JavaScript

js实现图例

2026-04-04 13:03:31JavaScript

使用Canvas绘制图例

Canvas是HTML5提供的绘图API,适合动态生成图例。通过创建画布上下文,可以自由绘制图形和文字。

const canvas = document.getElementById('legendCanvas');
const ctx = canvas.getContext('2d');

// 绘制矩形图例
ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 20, 20);
ctx.fillStyle = 'black';
ctx.font = '12px Arial';
ctx.fillText('数据系列1', 35, 25);

// 绘制圆形图例
ctx.beginPath();
ctx.arc(10, 50, 10, 0, Math.PI * 2);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.fillStyle = 'black';
ctx.fillText('数据系列2', 35, 55);

使用SVG创建图例

SVG是矢量图形标准,适合创建可缩放的图例。通过DOM操作可以动态添加图例元素。

const svg = document.getElementById('legendSvg');

// 添加矩形图例
const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('x', '10');
rect.setAttribute('y', '10');
rect.setAttribute('width', '20');
rect.setAttribute('height', '20');
rect.setAttribute('fill', 'green');
svg.appendChild(rect);

const text1 = document.createElementNS('http://www.w3.org/2000/svg', 'text');
text1.setAttribute('x', '35');
text1.setAttribute('y', '25');
text1.textContent = '数据系列3';
svg.appendChild(text1);

使用CSS和HTML构建图例

纯前端方案,利用CSS样式和HTML结构快速创建图例。

<div class="legend">
  <div class="legend-item">
    <div class="legend-color" style="background-color: orange;"></div>
    <div class="legend-label">数据系列4</div>
  </div>
  <div class="legend-item">
    <div class="legend-color" style="background-color: purple;"></div>
    <div class="legend-label">数据系列5</div>
  </div>
</div>

<style>
.legend {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
}
.legend-color {
  width: 20px;
  height: 20px;
}
.legend-label {
  font-family: Arial;
  font-size: 14px;
}
</style>

使用第三方库生成图例

借助Chart.js等可视化库可以快速生成专业图例。

import { Chart } from 'chart.js';

const chart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ['一月', '二月', '三月'],
    datasets: [{
      label: '销量',
      data: [12, 19, 3],
      backgroundColor: 'rgba(75, 192, 192, 0.2)',
      borderColor: 'rgba(75, 192, 192, 1)',
    }]
  },
  options: {
    plugins: {
      legend: {
        position: 'top',
        labels: {
          font: {
            size: 14
          }
        }
      }
    }
  }
});

响应式图例实现

通过监听窗口大小变化,动态调整图例布局。

function updateLegendLayout() {
  const width = window.innerWidth;
  const legend = document.querySelector('.legend');

  if (width < 600) {
    legend.style.flexDirection = 'column';
  } else {
    legend.style.flexDirection = 'row';
  }
}

window.addEventListener('resize', updateLegendLayout);
updateLegendLayout();

动态数据绑定图例

将图例与数据源绑定,实现数据变化时自动更新。

js实现图例

const dataSeries = [
  { color: '#FF6384', label: '产品A' },
  { color: '#36A2EB', label: '产品B' },
  { color: '#FFCE56', label: '产品C' }
];

function renderLegend(data) {
  const container = document.getElementById('legendContainer');
  container.innerHTML = '';

  data.forEach(item => {
    const div = document.createElement('div');
    div.className = 'legend-item';
    div.innerHTML = `
      <span style="display:inline-block;width:20px;height:20px;background:${item.color}"></span>
      <span>${item.label}</span>
    `;
    container.appendChild(div);
  });
}

renderLegend(dataSeries);

标签: 图例js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时功能可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是几种常见的实现方式: 使用 setInterva…

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js节流实现

js节流实现

节流的概念 节流(Throttle)是一种限制函数执行频率的技术,确保函数在一定时间间隔内只执行一次。常用于滚动事件、窗口调整等高频触发的场景。 基础实现方法 使用时间戳判断是否执行函数: fun…

js 实现图片 放大

js 实现图片 放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性可以实现图片的平滑放大效果。结合 JavaScript 监听鼠标事件控制放大状态: con…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…

js实现删除

js实现删除

使用 splice 方法删除数组元素 splice 方法可以删除数组中的元素,并返回被删除的元素。它接受两个参数:起始索引和要删除的元素数量。 const array = [1, 2, 3, 4…