乐闻世界logo
搜索文章和话题

How to create charts using SVG

2月21日 15:21

SVG charts are important tools for data visualization and can create various types of charts. Here are methods for creating charts using SVG:

1. Bar Chart Use <rect> elements to create bar charts.

svg
<svg width="400" height="300" viewBox="0 0 400 300"> <!-- Axes --> <line x1="50" y1="250" x2="380" y2="250" stroke="black" stroke-width="2" /> <line x1="50" y1="50" x2="50" y2="250" stroke="black" stroke-width="2" /> <!-- Bars --> <rect x="70" y="100" width="40" height="150" fill="#3498db" /> <rect x="130" y="80" width="40" height="170" fill="#e74c3c" /> <rect x="190" y="120" width="40" height="130" fill="#2ecc71" /> <rect x="250" y="60" width="40" height="190" fill="#f39c12" /> <rect x="310" y="90" width="40" height="160" fill="#9b59b6" /> <!-- Labels --> <text x="90" y="270" text-anchor="middle" font-size="12">Q1</text> <text x="150" y="270" text-anchor="middle" font-size="12">Q2</text> <text x="210" y="270" text-anchor="middle" font-size="12">Q3</text> <text x="270" y="270" text-anchor="middle" font-size="12">Q4</text> <text x="330" y="270" text-anchor="middle" font-size="12">Q5</text> </svg>

2. Line Chart Use <polyline> or <path> elements to create line charts.

svg
<svg width="400" height="300" viewBox="0 0 400 300"> <!-- Axes --> <line x1="50" y1="250" x2="380" y2="250" stroke="black" stroke-width="2" /> <line x1="50" y1="50" x2="50" y2="250" stroke="black" stroke-width="2" /> <!-- Grid lines --> <line x1="50" y1="200" x2="380" y2="200" stroke="gray" stroke-dasharray="5,5" /> <line x1="50" y1="150" x2="380" y2="150" stroke="gray" stroke-dasharray="5,5" /> <line x1="50" y1="100" x2="380" y2="100" stroke="gray" stroke-dasharray="5,5" /> <!-- Line --> <polyline points="70,200 130,150 190,180 250,100 310,120" fill="none" stroke="#3498db" stroke-width="3" /> <!-- Data points --> <circle cx="70" cy="200" r="5" fill="#3498db" /> <circle cx="130" cy="150" r="5" fill="#3498db" /> <circle cx="190" cy="180" r="5" fill="#3498db" /> <circle cx="250" cy="100" r="5" fill="#3498db" /> <circle cx="310" cy="120" r="5" fill="#3498db" /> </svg>

3. Pie Chart Use <path> elements to create pie charts.

svg
<svg width="300" height="300" viewBox="0 0 300 300"> <g transform="translate(150, 150)"> <!-- Slice 1: 30% --> <path d="M 0 0 L 0 -100 A 100 100 0 0 1 95.1 -30.9 Z" fill="#3498db" /> <!-- Slice 2: 25% --> <path d="M 0 0 L 95.1 -30.9 A 100 100 0 0 1 58.8 80.9 Z" fill="#e74c3c" /> <!-- Slice 3: 20% --> <path d="M 0 0 L 58.8 80.9 A 100 100 0 0 1 -58.8 80.9 Z" fill="#2ecc71" /> <!-- Slice 4: 15% --> <path d="M 0 0 L -58.8 80.9 A 100 100 0 0 1 -95.1 -30.9 Z" fill="#f39c12" /> <!-- Slice 5: 10% --> <path d="M 0 0 L -95.1 -30.9 A 100 100 0 0 1 0 -100 Z" fill="#9b59b6" /> </g> </svg>

4. Scatter Plot Use <circle> elements to create scatter plots.

svg
<svg width="400" height="300" viewBox="0 0 400 300"> <!-- Axes --> <line x1="50" y1="250" x2="380" y2="250" stroke="black" stroke-width="2" /> <line x1="50" y1="50" x2="50" y2="250" stroke="black" stroke-width="2" /> <!-- Data points --> <circle cx="80" cy="200" r="5" fill="#3498db" /> <circle cx="120" cy="150" r="5" fill="#3498db" /> <circle cx="160" cy="180" r="5" fill="#3498db" /> <circle cx="200" cy="100" r="5" fill="#3498db" /> <circle cx="240" cy="120" r="5" fill="#3498db" /> <circle cx="280" cy="80" r="5" fill="#3498db" /> <circle cx="320" cy="140" r="5" fill="#3498db" /> <circle cx="360" cy="60" r="5" fill="#3498db" /> </svg>

5. Area Chart Use <path> elements to create area charts.

svg
<svg width="400" height="300" viewBox="0 0 400 300"> <!-- Axes --> <line x1="50" y1="250" x2="380" y2="250" stroke="black" stroke-width="2" /> <line x1="50" y1="50" x2="50" y2="250" stroke="black" stroke-width="2" /> <!-- Area --> <path d="M 70 200 L 130 150 L 190 180 L 250 100 L 310 120 L 310 250 L 70 250 Z" fill="#3498db" opacity="0.5" /> <!-- Line --> <polyline points="70,200 130,150 190,180 250,100 310,120" fill="none" stroke="#3498db" stroke-width="3" /> </svg>

6. Dynamic Charts Use JavaScript to create dynamic charts.

javascript
function createBarChart(data, containerId) { const svgNS = 'http://www.w3.org/2000/svg'; const container = document.getElementById(containerId); const svg = document.createElementNS(svgNS, 'svg'); svg.setAttribute('width', '400'); svg.setAttribute('height', '300'); svg.setAttribute('viewBox', '0 0 400 300'); const maxValue = Math.max(...data); const barWidth = 40; const gap = 20; const startX = 50; const startY = 250; data.forEach((value, index) => { const height = (value / maxValue) * 200; const x = startX + index * (barWidth + gap); const y = startY - height; const rect = document.createElementNS(svgNS, 'rect'); rect.setAttribute('x', x); rect.setAttribute('y', y); rect.setAttribute('width', barWidth); rect.setAttribute('height', height); rect.setAttribute('fill', `hsl(${index * 60}, 70%, 50%)`); svg.appendChild(rect); }); container.appendChild(svg); } // Usage example const data = [100, 150, 120, 180, 90]; createBarChart(data, 'chart-container');

Best Practices:

  • Use viewBox for responsive charts
  • Add appropriate labels and legends
  • Use colors to distinguish different data series
  • Consider adding interactive effects (hover, tooltip)
  • Optimize performance, avoid too many elements
  • Add accessibility support for charts
  • Use CSS animations to enhance visual effects
标签:SVG