From 15a0b78e1db77586e2ebc146c1714622d3d15022 Mon Sep 17 00:00:00 2001 From: Lucian Pacurar Date: Wed, 25 Nov 2026 15:32:06 -0600 Subject: [PATCH 1/5] Add the line, area and sparkline chart types and stroke-width Work: 694 --- src/lpdf/constants.py | 5 ++- src/lpdf/layout/chart_attr.py | 53 +++++++++++++++++--------- src/lpdf/layout/chart_category_attr.py | 15 ++++---- src/lpdf/layout/chart_series_attr.py | 7 ++-- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/lpdf/constants.py b/src/lpdf/constants.py index 8f8f6c7..5b55fff 100644 --- a/src/lpdf/constants.py +++ b/src/lpdf/constants.py @@ -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): diff --git a/src/lpdf/layout/chart_attr.py b/src/lpdf/layout/chart_attr.py index 98e0539..3d7363a 100644 --- a/src/lpdf/layout/chart_attr.py +++ b/src/lpdf/layout/chart_attr.py @@ -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 @@ -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, @@ -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. @@ -72,13 +84,18 @@ 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, and the total of a stack 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. + # 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 diff --git a/src/lpdf/layout/chart_category_attr.py b/src/lpdf/layout/chart_category_attr.py index aa50b36..787af15 100644 --- a/src/lpdf/layout/chart_category_attr.py +++ b/src/lpdf/layout/chart_category_attr.py @@ -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 diff --git a/src/lpdf/layout/chart_series_attr.py b/src/lpdf/layout/chart_series_attr.py index e6b219c..6d4433e 100644 --- a/src/lpdf/layout/chart_series_attr.py +++ b/src/lpdf/layout/chart_series_attr.py @@ -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 From 4bfb7d4ca858d2e666f46fdd45a13503690dff2b Mon Sep 17 00:00:00 2001 From: Lucian Pacurar Date: Wed, 25 Nov 2026 16:09:26 -0600 Subject: [PATCH 2/5] Regenerate the chart's value-labels and stroke-width docs Work: 694 --- src/lpdf/layout/chart_attr.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lpdf/layout/chart_attr.py b/src/lpdf/layout/chart_attr.py index 3d7363a..8fe009d 100644 --- a/src/lpdf/layout/chart_attr.py +++ b/src/lpdf/layout/chart_attr.py @@ -84,12 +84,14 @@ 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 or over its point, and the total of a stack at the - # end of the stack or over its top. Default false. A sparkline writes none. + # Writes each value at the end of its bar, or over its point and under a point lower than + # the points beside it, where its line doesn't run, and the total of a stack 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. - # Which line is the current one and which the comparison is told by the series' color. + # 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 From 324a929a4277ffadd1d22809451156103fa11393 Mon Sep 17 00:00:00 2001 From: Lucian Pacurar Date: Wed, 25 Nov 2026 16:09:26 -0600 Subject: [PATCH 3/5] Say that a series colors a line or an area, and no value breaks a line Work: 694 --- src/lpdf/l_chart.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lpdf/l_chart.py b/src/lpdf/l_chart.py index 7947b9f..eddd8e0 100644 --- a/src/lpdf/l_chart.py +++ b/src/lpdf/l_chart.py @@ -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 @@ -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; 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. From fe3d553d68260a0a2506931e9e04c0721e3ee1b4 Mon Sep 17 00:00:00 2001 From: Lucian Pacurar Date: Wed, 25 Nov 2026 16:36:15 -0600 Subject: [PATCH 4/5] Regenerate the chart's value-labels doc Work: 694 --- src/lpdf/layout/chart_attr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lpdf/layout/chart_attr.py b/src/lpdf/layout/chart_attr.py index 8fe009d..64984d1 100644 --- a/src/lpdf/layout/chart_attr.py +++ b/src/lpdf/layout/chart_attr.py @@ -84,9 +84,9 @@ 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, or over its point and under a point lower than - # the points beside it, where its line doesn't run, and the total of a stack at the end of - # the stack or over its top. Default false. A sparkline writes none. + # 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. From faae8b9da2603e6fcef04d911f3c02d4b2520e5a Mon Sep 17 00:00:00 2001 From: Lucian Pacurar Date: Wed, 25 Nov 2026 16:36:15 -0600 Subject: [PATCH 5/5] Say that no value breaks an area or a sparkline too Work: 694 --- src/lpdf/l_chart.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lpdf/l_chart.py b/src/lpdf/l_chart.py index eddd8e0..bd45fc4 100644 --- a/src/lpdf/l_chart.py +++ b/src/lpdf/l_chart.py @@ -61,8 +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 and breaks a line; a stacked area counts it as zero. 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.