Charts ====== ```` draws a data visualization -- scatter, spline, line, histogram, or pie -- directly on the page, without shelling out to an external plotting library. Internally it's built on top of ```` (see :doc:`shapes`): each chart style is a small "plugin" that fills a canvas with ordinary primitive nodes (lines, circles, rectangles, text), so nothing renderer-side needs to know about charts at all. A chart always needs explicit ``width``/``height`` and a ``style``, and contains one or more ```` children supplying its data: .. code-block:: xml ```` Attributes ---------------------- In addition to the common node attributes (see :doc:`structure`), ```` accepts: .. list-table:: :header-rows: 1 :widths: 20 15 65 * - Attribute - Type - Description * - ``style`` - string - **Required.** One of ``scatter``, ``spline``, ``line``, ``histogram``, ``pie``. An unrecognized value is a parse error. * - ``width``, ``height`` - float - **Required.** Pixel size of the chart's canvas. * - ``title`` - string - Optional chart title, centered above the plot. * - ``x_label``, ``y_label`` - string - Optional axis labels. Ignored by ``pie`` (no axes). * - ``axis_position`` - string - Where the axis lines cross: ``left`` (default), ``right``, ``top-left``, ``top-right``, ``bottom-left``, ``bottom-right``, or ``center``. Ignored by ``pie``. * - ``show_percentage`` - bool - ``pie`` only: whether each slice draws its share of the total as a centered percentage label (default ``true``). ```` Attributes ------------------------ Each ```` contains one or more ```` elements supplying its data: .. list-table:: :header-rows: 1 :widths: 20 15 65 * - Attribute - Type - Description * - ``name`` - string - Series label, shown in the legend (and, for ``pie``, used as a slice's label when the point itself doesn't carry one). * - ``color`` - color - Series color. When omitted, a color is picked from a fixed palette, cycled by series index. * - ``model`` - JSON - **Required.** The series' data points, in one of two shapes (see below). A ``model`` is a JSON array whose entries are either: - **Coordinate pairs** -- ``[x, y]`` or ``{"x": ..., "y": ...}`` -- for continuous data (scatter, spline, line), or - **Labeled values** -- ``{"label": value}`` -- for categorical data (histogram, pie). Each entry becomes one point at ``x`` = its ordinal position in the array, ``y`` = the value, with the key kept as that point's category label (used for the X-axis tick in a histogram, or the slice label in a pie chart). Data can also come from ``${...}`` template substitution, the same as any other ``model`` attribute (see :doc:`templating`). Chart Styles ------------ Scatter ~~~~~~~ ``style="scatter"`` plots one dot per data point, unconnected -- for showing the relationship (or lack of one) between two continuous variables across one or more series. .. code-block:: xml .. image:: ../_static/charts/scatter.png :alt: Scatter chart example with three series :width: 550px Spline ~~~~~~ ``style="spline"`` draws a smooth, interpolated curve through each series' points (plus a marker dot at every point) -- for a trend that should read as continuous rather than piecewise-linear. Two series with crossing trends (below) show the interpolation staying smooth right through the crossover. .. code-block:: xml .. image:: ../_static/charts/spline.png :alt: Spline chart example with two crossing series :width: 550px Line ~~~~ ``style="line"`` connects each series' points with straight segments (plus a marker dot at every point) -- the same point model as ``spline``, differing only in how points are connected. .. code-block:: xml .. image:: ../_static/charts/line.png :alt: Line chart example with three series :width: 550px Histogram ~~~~~~~~~ ``style="histogram"`` draws grouped bars, one per data point, grouped side-by-side by series at each shared category -- for comparing values across categories and, when there's more than one series, across groups within each category. Use the ``{"label": value}`` model shape to get named categories on the X axis: .. code-block:: xml .. image:: ../_static/charts/histogram.png :alt: Histogram chart example with three series across five categories :width: 600px .. note:: The Y axis auto-zooms to the actual data range rather than always starting at zero (visible above: the axis starts at 20, not 0) -- so small differences between bars stay readable. A bar never sits flush against a non-zero baseline; a small gap always separates it from the axis line. Pie ~~~ ``style="pie"`` draws one slice per data point across every ````, sized by its share of the total. Unlike the other styles, a pie chart has no axes at all. Use the ``{"label": value}`` model shape for a labeled category breakdown: .. code-block:: xml .. image:: ../_static/charts/pie.png :alt: Pie chart example with six slices :width: 550px A series with a single point (one ```` per slice) keeps that series' own ``color``; a series with multiple points (one ```` holding a whole category breakdown, as above) cycles each of its slices through the default palette instead, since a single series color can't distinguish them. Points with a non-positive value are skipped. Combining Charts with Canvas ----------------------------- Because ```` is just another node, it can be nested as a child of ```` (see :doc:`shapes`) alongside ordinary shapes and text -- each positioned by its own ``x``/``y``, exactly like any other canvas child. This is how a multi-chart dashboard or a chart annotated with custom callouts is built: there's no dedicated "dashboard" or "annotation" node, just composition. Multiple Charts on One Canvas ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A ```` sized for a full widget grid, with three independent ```` children (a pie, a histogram, and a line chart) plus a heading and divider lines drawn as ordinary shapes: .. code-block:: xml Q1-Q3 Business Overview .. image:: ../_static/charts/dashboard.png :alt: Dashboard combining a pie chart, a histogram, and a line chart on one canvas :width: 650px Each ```` lays out its own chrome (title, axes, legend) independently within the bounding box its own ``width``/``height`` give it -- the parent ```` doesn't know or care that its children happen to be charts. Annotating a Chart with Custom Callouts ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ A ```` has no built-in support for callouts or stat badges -- but wrapping it in a ```` and adding ordinary ````/```` siblings below it gets the same effect: .. code-block:: xml Peak: 50k App A, month 12 Growth: +317% App A, year over year .. image:: ../_static/charts/annotated.png :alt: Spline chart with custom stat-badge callouts below it :width: 550px .. note:: A ````'s own internal plot coordinates (where a given data point actually lands in pixels) aren't exposed back to the ``.craft`` file -- so a callout can be positioned relative to the chart's outer bounding box (as above), but not pixel-aligned to one specific data point without already knowing the chart's own axis-scaling.