Zum Inhalt

PDFGraphs: Diagramme und Formen

Eine neuere Sammelklasse: neun Diagrammarten, sechs Farbpaletten, dazu Vielecke, Bögen, Transparenz, Drehung, Farbverläufe und abgerundete Rechtecke. Sie braucht ausser fpdf.php nichts weiter; eine ausführliche Anleitung liegt dem Paket bei.

Nummer105
OriginaltitelPDFGraphs
AutorJames
LizenzFPDF (frei, auch für kommerzielle Nutzung)
Quellefpdf.org, Skript 105 (englisch, mit Archiv zum Herunterladen)

Beispiel

Die Klasse selbst liegt im Archiv bei fpdf.org. So wird sie aufgerufen:

PHP
<?php
require('pdf_graphs.php');

$pdf = new PDFGraphs('P', 'mm', 'A4');
$pdf->SetAutoPageBreak(false);

// ---- Page 1: gradient bars, rotated labels, grouped & stacked -----
$pdf->AddPage();
$pdf->SetFont('Helvetica', 'B', 16);
$pdf->SetTextColor(17, 24, 39);
$pdf->Text(10, 15, 'PDFGraphs - Community-script showcase');

$pdf->BarChart(
    array(23, 45, 56, 78, 32, 41, 67),
    array('Monday', 'Tuesday', 'Wednesday', 'Thursday',
          'Friday', 'Saturday', 'Sunday'),
    10, 22, 95, 80,
    array('title' => 'Daily Users (rotated labels)',
          'gradient' => true, 'rotateLabels' => 30)
);

$pdf->SetPalette('ocean');
$pdf->HorizontalBarChart(
    array(120, 95, 78, 64, 50, 38),
    array('Search', 'Direct', 'Social', 'Email', 'Referral', 'Other'),
    110, 22, 90, 80,
    array('title' => 'Traffic Sources (gradient)', 'gradient' => true)
);

$pdf->SetPalette('modern');
$pdf->GroupedBarChart(
    array(
        array(30, 50, 45, 60),
        array(25, 40, 55, 48),
        array(15, 30, 35, 42),
    ),
    array('Q1', 'Q2', 'Q3'),
    array('2023', '2024', '2025', '2026'),
    10, 115, 95, 80,
    array('title' => 'Quarterly Revenue', 'gradient' => true)
);

$pdf->StackedBarChart(
    array(
        array(20, 30, 25, 35),
        array(15, 25, 30, 20),
        array(10, 15, 20, 25),
    ),
    array('Product A', 'Product B', 'Product C'),
    array('Q1', 'Q2', 'Q3', 'Q4'),
    110, 115, 90, 80,
    array('title' => 'Revenue Mix', 'gradient' => true)
);

$pdf->LineChart(
    array(
        array(12, 19, 15, 25, 22, 30, 28, 35, 32, 40),
        array( 8, 15, 18, 20, 25, 22, 28, 30, 35, 38),
    ),
    array('Revenue', 'Costs'),
    array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct'),
    10, 205, 190, 80,
    array('title' => 'Revenue vs Costs', 'smoothFill' => true,
          'fillAlpha' => 0.25)
);

// ---- Page 2: gradient pie/donut, area, scatter w/ alpha ----------
$pdf->AddPage();
$pdf->SetFont('Helvetica', 'B', 16);
$pdf->SetTextColor(17, 24, 39);
$pdf->Text(10, 15, 'Gradients, alpha & rounded shapes');

$pdf->PieChart(
    array(35, 25, 20, 12, 8),
    array('Mobile', 'Desktop', 'Tablet', 'TV', 'Other'),
    10, 22, 95, 90,
    array('title' => 'Device Share (radial gradient)',
          'gradient' => true)
);

$pdf->SetPalette('sunset');
$pdf->DonutChart(
    array(40, 30, 20, 10),
    array('Free', 'Pro', 'Team', 'Enterprise'),
    110, 22, 90, 90,
    array('title' => 'Subscription Tiers', 'gradient' => true)
);

$pdf->SetPalette('forest');
$pdf->AreaChart(
    array(
        array(10, 18, 14, 22, 28, 35, 32, 40, 45, 50, 48, 55),
        array( 5, 12, 10, 18, 22, 25, 30, 28, 33, 38, 36, 42),
    ),
    array('Active', 'New'),
    array('J','F','M','A','M','J','J','A','S','O','N','D'),
    10, 122, 95, 80,
    array('title' => 'User Growth (overlapping)', 'fillAlpha' => 0.35)
);

$pdf->SetPalette('modern');
$pdf->ScatterPlot(
    array(
        array(array(1,2), array(2,4), array(3,3), array(4,6),
              array(5,8), array(6,7), array(7,10), array(3.5,5),
              array(4.5,6.5), array(5.5,7.5)),
        array(array(1.5,1), array(2.5,3), array(3.5,5), array(4.5,4),
              array(5.5,6), array(6.5,9), array(2,2), array(3,4)),
    ),
    array('Group A', 'Group B'),
    110, 122, 90, 80,
    array('title' => 'Correlation (alpha 0.75)', 'xLabel' => 'Input',
          'alpha' => 0.75)
);

// Showcase rotated text and shape primitives
$pdf->SetFont('Helvetica', 'B', 9);
$pdf->SetTextColor(75, 85, 99);
$pdf->RotatedText(8, 165, 'Vertical axis label via Rotate()', 90);

// RoundedRect demo
$pdf->SetFillColor(59, 130, 246);
$pdf->RoundedRect(20, 215, 40, 25, 4, 'F');
$pdf->SetFillColor(239, 68, 68);
$pdf->RoundedRect(70, 215, 40, 25, 4, 'F', '13'); // diagonal corners
$pdf->SetDrawColor(34, 197, 94);
$pdf->SetLineWidth(0.8);
$pdf->RoundedRect(120, 215, 40, 25, 4, 'D', '24'); // other diagonal

$pdf->SetFont('Helvetica', '', 8);
$pdf->SetTextColor(255, 255, 255);
$pdf->Text(28, 230, 'All 4 corners');
$pdf->Text(78, 230, 'TL + BR only');
$pdf->SetTextColor(34, 197, 94);
$pdf->Text(128, 230, 'TR + BL only');

$pdf->Output();
?>

Das Ergebnis als PDF zeigt die Seite des Skripts bei fpdf.org.

Einbau

Add-ons werden nicht installiert, sondern eingebunden: Datei neben fpdf.php legen, mit require laden und die eigene Klasse von der Klasse des Add-ons ableiten. Brauchen Sie mehrere Add-ons, leiten Sie sie in einer Kette voneinander ab. Mehr dazu unter Installation und in der FAQ.