diff --git a/src/lpdf/constants.py b/src/lpdf/constants.py index 5b55fff..235218d 100644 --- a/src/lpdf/constants.py +++ b/src/lpdf/constants.py @@ -391,11 +391,13 @@ 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, 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.""" + """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, pie a slice for each category, donut a pie with a hole, and sparkline a small line with no axis, labels or legend.""" BAR = "bar" LINE = "line" AREA = "area" + PIE = "pie" + DONUT = "donut" SPARKLINE = "sparkline" diff --git a/src/lpdf/l_chart.py b/src/lpdf/l_chart.py index bd45fc4..33ef8e9 100644 --- a/src/lpdf/l_chart.py +++ b/src/lpdf/l_chart.py @@ -47,7 +47,8 @@ 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, line or area.""" + """Build a series of a chart: its entry in the legend and the color of its bars, line or area. A pie, a donut + and a sparkline take none.""" return ChartSeriesNode(options_to_attrs(attrs)) @staticmethod @@ -61,8 +62,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, an area or a sparkline; a stacked area counts it - as zero. It has no attributes: pass ``NoAttr``. + decimals. No value, ``[]``, draws no bar or slice 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. diff --git a/src/lpdf/layout/chart_attr.py b/src/lpdf/layout/chart_attr.py index 64984d1..636f300 100644 --- a/src/lpdf/layout/chart_attr.py +++ b/src/lpdf/layout/chart_attr.py @@ -20,16 +20,22 @@ class ChartAttr: 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. + stacked area. A sparkline draws no axis, labels or legend. A pie or a donut draws no + axis either: a slice for each category, clockwise from the top, as wide as its share of + the whole, in the largest circle the plot holds, and a legend at the right that lists + each slice's label, color and share; a donut has a hole, with a text of the author's in + it, and a negative value, or values that add up to zero, are refused. 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". 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. + # series filled down to the one under it. pie draws a slice for each category, as wide as + # its share of the whole, with no axis. donut is a pie with a hole, which hole sizes and + # hole-text writes in. 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 plot gets the rest. @@ -55,8 +61,9 @@ class ChartAttr: # Written after every number of the chart: % writes 12%, and " ms" writes 12 ms. With values # in thousands, suffix="k" writes $412k. suffix: str | None = None - # The decimals of the value labels, 0 to 6. Left out, a value is written with the decimals - # it has. The steps of the axis take the decimals their size needs. + # The decimals of the value labels, 0 to 6, and of the shares in the legend of a pie or a + # donut, which are whole percents unless it says otherwise. Left out, a value is written + # with the decimals 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. On a line, whose axis fits its values, @@ -81,8 +88,10 @@ class ChartAttr: # The space between categories. Default a quarter of the room of a category. The bars take # the room the two gaps leave. category_gap: str | None = None - # 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. + # Where the legend goes, which lists each series with its label and its color, or each slice + # of a pie or a donut with its label, its color and its share of the whole, 22%: bottom, + # top, right, or none. Default bottom with two series or more, and none with one; right for + # a pie or a donut. legend: str | None = 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 @@ -93,6 +102,13 @@ class ChartAttr: # 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 + # The hole of a donut as a share of its size, from 0.1 to 0.9: 0.8 is a thin ring and 0.5 a + # thick one. Default 0.6. Donuts only: refused on any other kind. + hole: float | None = None + # A text in the middle of a donut's hole, as it should read: a total, $11,100, or a share, + # 72%. The engine adds nothing up. Too wide for the hole, it wraps at spaces, and what still + # doesn't fit ends in an ellipsis. Donuts only: refused on any other kind. + hole_text: 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 diff --git a/src/lpdf/layout/chart_category_attr.py b/src/lpdf/layout/chart_category_attr.py index 787af15..050c1c7 100644 --- a/src/lpdf/layout/chart_category_attr.py +++ b/src/lpdf/layout/chart_category_attr.py @@ -9,14 +9,16 @@ class ChartCategoryAttr: """ 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. + line, or a slice of a pie or a donut, with its label beside the axis, or in the legend + of a pie or a donut. 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 its point, or beside the bars with - # direction="horizontal". + # direction="horizontal", or in the legend of a pie or a donut, before the slice's share. label: str | None = None # The color of the category's bar, in a bar chart of one series, where the series' color is - # the default. + # the default, or of its slice in a pie or a donut, where the default is the next color of + # the palette. color: str | None = None diff --git a/src/lpdf/layout/chart_series_attr.py b/src/lpdf/layout/chart_series_attr.py index 6d4433e..1cd37d8 100644 --- a/src/lpdf/layout/chart_series_attr.py +++ b/src/lpdf/layout/chart_series_attr.py @@ -11,7 +11,7 @@ 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. A sparkline has one series and takes none. + data is bound. A pie, a donut and a sparkline have one series and take none. """ # The series' entry in the legend.