Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lpdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,12 @@ class FieldType(StrEnum):


class ChartType(StrEnum):
"""The values of the `type` attribute of a chart: the kind of chart. bar draws a bar for each value."""
"""The values of the `type` attribute of a chart: the kind of chart. bar draws a bar for each value, line a line through the values of each series, area a line filled down to zero, or stacked, and sparkline a small line with no axis, labels or legend."""

BAR = "bar"
LINE = "line"
AREA = "area"
SPARKLINE = "sparkline"


class ChartDirection(StrEnum):
Expand Down
5 changes: 3 additions & 2 deletions src/lpdf/l_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class LChart:

@staticmethod
def series(attrs: ChartSeriesAttr | None = None) -> ChartSeriesNode:
"""Build a series of a chart: its entry in the legend and the color of its bars."""
"""Build a series of a chart: its entry in the legend and the color of its bars, line or area."""
return ChartSeriesNode(options_to_attrs(attrs))

@staticmethod
Expand All @@ -61,7 +61,8 @@ def category(
def value(attrs: None = None, nodes: list[int | float | Decimal | str] | None = None) -> ChartValueNode:
"""Build a value of a category: one number, written in full with no exponent and no locale, a ``Decimal``
with its decimals, or a string that is a number, such as ``"0.250"``, taken as written so its label keeps its
decimals. No value, ``[]``, draws no bar. It has no attributes: pass ``NoAttr``.
decimals. No value, ``[]``, draws no bar and breaks a line, an area or a sparkline; a stacked area counts it
as zero. It has no attributes: pass ``NoAttr``.

:raises TypeError: when a value is not an int, a float, a Decimal or a string.
:raises ValueError: when a float or a Decimal is not finite.
Expand Down
55 changes: 37 additions & 18 deletions src/lpdf/layout/chart_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@ class ChartAttr:
a row and each value a cell, a value for each series in the series' order. A chart with
one series needs no series. It takes its box in the flow as an image does, never splits
across pages, and moves whole to the next page when it doesn't fit. The engine draws its
axis, steps, gridlines, labels and legend by fixed rules: the value axis starts at zero
and runs in round steps, each 1, 2 or 5 times a power of ten, written in full; a
negative value takes the axis below zero, where a zero line is drawn; a label wider than
its room wraps at spaces onto three lines at most, and what still doesn't fit ends in an
ellipsis, so a chart always renders. A chart is a leaf, with no padding, background or
border: a column around it gives them, and a text above it is its title.
axis, steps, gridlines, labels and legend by fixed rules: the value axis of bars and
areas starts at zero, and a line's fits its values, from a round step under the lowest
to one over the highest; the axis runs in round steps, each 1, 2 or 5 times a power of
ten, written in full; a negative value takes the axis below zero, where a zero line is
drawn; a label wider than its room wraps at spaces onto three lines at most, and what
still doesn't fit ends in an ellipsis, so a chart always renders. A line or an area
writes every 2nd, 3rd or 5th label where they don't all fit, and every point keeps its
place. A missing value breaks a line, an area and a sparkline, and counts as zero in a
stacked area. A sparkline draws no axis, labels or legend. A chart is a leaf, with no
padding, background or border: a column around it gives them, and a text above it is its
title.
"""

# The kind of chart. bar draws a bar for each value, side by side within a category, or
# stacked with stacked="true".
# stacked with stacked="true". line draws a line through the values of each series, on an
# axis that fits them. area draws a line filled down to zero, or with stacked="true" each
# series filled down to the one under it. sparkline draws one small line through the whole
# box, with no axis, labels or legend, for a table cell.
type: str
# Height of the whole chart, a length or a name from the height row of the tokens. The axis,
# the labels and the legend take room inside it and the width, and the bars get the rest.
# the labels and the legend take room inside it and the width, and the plot gets the rest.
# The width is fill unless it is set.
height: str
# Which way the bars run. vertical, the default, puts the categories along the bottom and
# the value axis on the left; horizontal lists the categories down the left side, which
# suits long labels, with the value axis along the bottom.
# suits long labels, with the value axis along the bottom. Bars only: refused on any other
# kind.
direction: str | None = None
# Stacks the bars of a category on each other, in the order of the series, so the axis runs
# to the largest total. A stacked value can't be negative. Default false: side by side.
# Stacks the bars of a category on each other, in the order of the series, or the areas of
# the series, each filled down to the one under it, so the axis runs to the largest total. A
# stacked value can't be negative, and a missing one counts as zero. Default false: side by
# side, or areas filled down to zero. Bars and areas only: refused on any other kind.
stacked: bool | None = None
# Width of the element. A length fixes it. fill, in a row, takes the width its line leaves
# after the other children, shared equally if several on the line use fill, and never moves
Expand All @@ -48,11 +59,12 @@ class ChartAttr:
# it has. The steps of the axis take the decimals their size needs.
decimals: int | None = None
# A number the value axis reaches down to at least, which takes it below zero: min="-50". It
# only widens the axis: a value below it still fits.
# only widens the axis: a value below it still fits. On a line, whose axis fits its values,
# min="0" brings the axis down to zero. A sparkline reaches it too, though it draws no axis.
min: float | None = None
# A number the value axis reaches at least: max="100" for percentages, or the same max on
# charts meant to be compared side by side. It only widens the axis: a value above it still
# fits.
# charts meant to be compared side by side, such as the sparklines of a table. It only
# widens the axis: a value above it still fits.
max: float | None = None
# The character between the groups of three digits of a number, or empty for none. Default
# ,: with decimal-mark="," and thousands-mark=".", 1234.5 is written 1.234,5; with a space,
Expand All @@ -61,7 +73,7 @@ class ChartAttr:
# The character before the decimals of a number. Default ., and it can't be the
# thousands-mark.
decimal_mark: str | None = None
# The space between the bars and the axis labels, and between them and the legend. Default
# The space between the plot and the axis labels, and between them and the legend. Default
# s, from the space row of the tokens.
gap: str | None = None
# The space between the bars of one category, side by side. Default a tenth of a bar.
Expand All @@ -72,13 +84,20 @@ class ChartAttr:
# Where the legend goes, which lists each series with its label and its color: bottom, top,
# right, or none. Default bottom with two series or more, and none with one.
legend: str | None = None
# Writes each value at the end of its bar, and the total of a stack at the end of the stack.
# Default false.
# Writes each value at the end of its bar or over its point. A point lower than the points
# beside it has its value under it, where its line doesn't run. The total of a stack goes at
# the end of the stack or over its top. Default false. A sparkline writes none.
value_labels: bool | None = None
# The width of every line of a line, an area or a sparkline chart, a length such as 2pt:
# 1.5pt by default, and 1pt for a sparkline. 0 draws no line, so an area is its fill alone.
# Every line takes the same width: which line is the current one and which the comparison is
# told by the series' color.
stroke_width: str | None = None
font: str | None = None
font_size: str | None = None
# The color of the chart's text, its labels, steps and legend, and of its zero line. Default
# the color of the text around it.
# the color of the text around it. A sparkline, which has no text, draws its line in it, and
# without it in the first color of the palette.
color: str | None = None
# What the chart shows, which a screen reader reads in a tagged document, one with pdfua or
# a pdfa of level a: "Revenue per month, highest in March". alt="" marks it as decoration. A
Expand Down
15 changes: 8 additions & 7 deletions src/lpdf/layout/chart_category_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
@dataclass(frozen=True, kw_only=True)
class ChartCategoryAttr:
"""
A category of a chart: a bar for each series, or a stack of them, with its label beside
the axis. It holds a value for each series of the chart, after the data is bound.
data-source repeats it for each item of a list. A chart holds 10,000 categories at most,
counted after the data is bound.
A category of a chart: a bar for each series, or a stack of them, or a point on each
line, with its label beside the axis. It holds a value for each series of the chart,
after the data is bound. data-source repeats it for each item of a list. A chart holds
10,000 categories at most, counted after the data is bound.
"""

# The category's label, under its bars, or beside them with direction="horizontal".
# The category's label, under its bars or its point, or beside the bars with
# direction="horizontal".
label: str | None = None
# The color of the category's bar, in a chart of one series, where the series' color is the
# default.
# The color of the category's bar, in a bar chart of one series, where the series' color is
# the default.
color: str | None = None
7 changes: 4 additions & 3 deletions src/lpdf/layout/chart_series_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class ChartSeriesAttr:
A series of a chart: one value in each category, in the order of the series. Left out,
the chart has one series. data-source repeats it for each item of a list, so the data
can set how many series there are. A chart holds 100 series at most, counted after the
data is bound.
data is bound. A sparkline has one series and takes none.
"""

# The series' entry in the legend.
label: str | None = None
# The color of the series' bars and its swatch in the legend. Without it, the series takes
# the next color of a fixed palette of eight, which starts over past the eighth.
# The color of the series' bars, line or area, and its swatch in the legend. Without it, the
# series takes the next color of a fixed palette of eight, which starts over past the
# eighth.
color: str | None = None