fluid

# Charts with a *swappable engine*

Bar, line, area, pie and donut – described in a plain format, drawn by an engine the project chooses. The default is Apache ECharts, self-hosted.

 
 
 

 

Values by hand

## Enquiries per month

Two series, one label per line, values separated by semicolons.

 

Als Tabelle anzeigen

|  | Website | Telefon |
| --- | --- | --- |
| Jan | 42 | 18 |
| Feb | 51 | 22 |
| Mar | 47 | 19 |
| Apr | 63 | 17 |
| May | 78 | 21 |
| Jun | 71 | 16 |

 
 

 

Live from the database

## What this website is made of

A data source of the kit counts the blocks of this website – fresh on every request. AddOns or the project register their own sources with #[AsChartSource].

 

Als Tabelle anzeigen

|  | Blocks |
| --- | --- |
| Body text / article | 63 |
| Code | 55 |
| Text + image | 40 |
| Call to action | 31 |
| Intro (hero) | 24 |
| Features / services | 17 |
| Key figures | 6 |
| Quote / testimonials | 5 |

 
 

 

 

## Visits, stacked

 

Als Tabelle anzeigen

|  | Neu | Wiederkehrend |
| --- | --- | --- |
| Mon | 120 | 220 |
| Tue | 132 | 182 |
| Wed | 101 | 191 |
| Thu | 134 | 234 |
| Fri | 90 | 290 |
| Sat | 230 | 330 |
| Sun | 210 | 310 |

 
 
 

 

## Share of devices

 

Als Tabelle anzeigen

|  | Anteil |
| --- | --- |
| Smartphone | 58 % |
| Desktop | 36 % |
| Tablet | 6 % |

 
 
 

 

 

## How the charts work

 

### Own format instead of a library

Blocks and AddOns describe a chart with `type`, `labels` and `series` – never in the format of a library. Swapping the engine changes no caller.

### Loaded when it becomes visible

The loader (`charts.mjs`, a few KB) fetches the engine only when a chart comes into view. Pages without a chart load nothing.

### Accessible

Every chart has a sentence for screen readers and the values as a table (“Show as table”). Without JavaScript the table remains.

### Colours from the kit

Series colours come from CSS variables (`--chart-1` … `--chart-8`), text and lines from the surroundings – light and dark, also in dark sections.

### Editing on the website

The “Diagramm” block has ordinary core fields: labels, series as a collection, type, number format. Values change in the sidebar and appear at once.

 
 

 

## Rendering a chart

From any template, module or AddOn – the same output as the block.

 
 
 
PHP
 

```
use Klxm\Fluid\Chart\Chart;

echo Chart::render([
    'type' => 'line',                              // line | area | bar | hbar | pie | donut
    'labels' => ['Jan', 'Feb', 'Mär', 'Apr'],
    'series' => [['name' => 'Anfragen', 'data' => [12, 19, 15, 24]]],
    'description' => 'Anfragen je Monat, höchster Wert im April',
    'format' => 'number', 'unit' => '', 'height' => 320,
], ['table' => false]);   // true = Tabelle aufgeklappt, 'none' = ohne
```

 
 
 

Pie and donut show one series; stacking applies to bar and area.

 
 

 

## Registering a data source

A static method with #[AsChartSource] appears in the block under “Daten”. This source feeds the chart above.

 
 
 
kits/fluid/src/ChartSources.php PHP
 

```
<?php

namespace Klxm\Fluid\Kit\Fluid;

use Klxm\Fluid\Chart\Attribute\AsChartSource;
use Redaxo\Core\Content\AsModule;
use Redaxo\Core\Content\Module;
use Redaxo\Core\Database\Sql;
use Redaxo\Core\Language\Language;
use ReflectionClass;
use Throwable;

/**
 * Datenquellen des Kits für den Block „Diagramm“ – zugleich Beispiel für #[AsChartSource]:
 * eine statische Methode, die labels und series liefert. Die Werte kommen bei jedem Aufruf frisch.
 */
final class ChartSources
{
    /** @return array{labels: list<string>, series: list<array{name: string, data: list<int>}>, description: string} */
    #[AsChartSource('fluid.blocks', 'Inhalte der Website: Blöcke je Art (live)')]
    public static function blocks(): array
    {
        $rows = Sql::factory()->getArray(
            'SELECT module, COUNT(*) AS n FROM rex_article_slice WHERE language_id = ? AND revision = 0 AND status = 1 GROUP BY module ORDER BY n DESC',
            [Language::getCurrentId()],
        );
        $labels = [];
        $data = [];
        foreach ($rows as $row) {
            $key = (string) $row['module'];
            if ('fluid.layout' === $key) {
                continue;   // Raster, kein Inhalt
            }
            $labels[] = self::label($key);
            $data[] = (int) $row['n'];
            if (8 === \count($labels)) {
                break;
            }
        }

        return ['labels' => $labels, 'series' => [['name' => lt('Blöcke'), 'data' => $data]],
            'description' => lt('Die häufigsten Blöcke auf dieser Website, gezählt aus der Datenbank')];
    }

    private static function label(string $key): string
    {
        try {
            $class = Module::getClass($key);
            $attr = null !== $class ? (new ReflectionClass($class))->getAttributes(AsModule::class)[0] ?? null : null;
            $name = $attr?->newInstance()->name;

            return null !== $name ? (\is_string($name) ? $name : $name->translate()) : $key;
        } catch (Throwable) {
            return $key;
        }
    }
}
```

 
 
 
 
 

 

## Swapping the engine

A class with #[AsChartEngine] names the ES module, the configuration picks it – like the policies of REDAXO 6.

 
 
 
src/Chart/ApexEngine.php PHP
 

```
use Klxm\Fluid\Chart\Attribute\AsChartEngine;
use Klxm\Fluid\Chart\ChartEngine;

#[AsChartEngine('apex')]
final class ApexEngine implements ChartEngine
{
    public function url(): string
    {
        return '/assets/project/charts-apex.mjs';
    }
}

// src/Project.php – in configure()
Chart::$config = new ChartConfig(engine: 'apex');
```

 
 
 
charts-apex.mjs javascript
 

```
// assets/project/charts-apex.mjs – die einzige Datei, die die Bibliothek kennt
export function render(el, spec, theme) {
  // spec: type, labels, series[{name, data}], format, unit, stacked, height, locale
  // theme: colors[], ink, muted, grid, bg, font, dark (aus den CSS-Variablen der Seite)
  const chart = draw(el, spec, theme);
  return {
    update: (t) => chart.restyle(t),   // Hell/Dunkel gewechselt
    resize: () => chart.resize(),
    dispose: () => chart.destroy(),
    image: () => chart.png(),          // optional: Bild für den Druck
  };
}
```

 
 
 

Template for an engine of your own: resources/js/charts-echarts.mjs in fluid.

 
 

 
 
 

Try it

 

## This site is built with REDAXO 6 and fluid.

 

Sign in, tap “Edit” – and change every text, every image and the whole design right here.

 
 
[How editing works](https://fluid.klxm.de/en/fluid/editing/)[See the technology](https://fluid.klxm.de/en/redaxo-6/technology/)


---

Source: https://fluid.klxm.de/en/plus/charts/
<style nonce="693aef5901a3ef8355268053ef6b055d">.fs-455c15fee1{--ncss-grid-min:13rem !important}</style>