From 391885c2a378efbbc43680ff9d2414e7675024e3 Mon Sep 17 00:00:00 2001 From: Nick Romano Date: Sat, 30 Sep 2017 17:50:48 -0700 Subject: [PATCH 1/2] Add lines with dashes to chart --- docs/examples.rst | 11 +++++++++++ examples/charts/lines_dashes.svg | 4 ++++ examples/lines_dashes.py | 12 ++++++++++++ leather/chart.py | 4 ++-- leather/shapes/line.py | 5 ++++- leather/theme.py | 4 ++++ 6 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 examples/charts/lines_dashes.svg create mode 100644 examples/lines_dashes.py diff --git a/docs/examples.rst b/docs/examples.rst index cdbe358..10f4924 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -192,6 +192,17 @@ Style attributes of individual data points can be set by value using a :func:`.s .. figure:: ../examples/charts/colorized_dots.svg +Creating dashed lines +---------------------------- + +When adding a line using :meth:`.Chart.add_line` you can set the `stroke_dasharray` property. More details on the property `here `_. + +.. literalinclude:: ../examples/lines_dashes.py + :language: python + +.. figure:: ../examples/charts/lines_dashes.svg + + Chart grids =========== diff --git a/examples/charts/lines_dashes.svg b/examples/charts/lines_dashes.svg new file mode 100644 index 0000000..c7e0841 --- /dev/null +++ b/examples/charts/lines_dashes.svg @@ -0,0 +1,4 @@ + + +Line Dashes246802.557.510 \ No newline at end of file diff --git a/examples/lines_dashes.py b/examples/lines_dashes.py new file mode 100644 index 0000000..376e65c --- /dev/null +++ b/examples/lines_dashes.py @@ -0,0 +1,12 @@ +import leather + +data = [ + (0, 3), + (4, 5), + (7, 9), + (8, 4) +] + +chart = leather.Chart('Line Dashes') +chart.add_line(data, stroke_dasharray='2%') +chart.to_svg('examples/charts/lines_dashes.svg') diff --git a/leather/chart.py b/leather/chart.py index e272183..fa88335 100644 --- a/leather/chart.py +++ b/leather/chart.py @@ -166,13 +166,13 @@ def add_dots(self, data, x=None, y=None, name=None, fill_color=None, radius=None Dots(fill_color, radius) ) - def add_line(self, data, x=None, y=None, name=None, stroke_color=None, width=None): + def add_line(self, data, x=None, y=None, name=None, stroke_color=None, width=None, stroke_dasharray=None): """ Create and add a :class:`.Series` rendered with :class:`.Line`. """ self.add_series( Series(data, x=x, y=y, name=name), - Line(stroke_color, width) + Line(stroke_color, width, stroke_dasharray) ) def _validate_dimension(self, dimension): diff --git a/leather/shapes/line.py b/leather/shapes/line.py index c013f7b..a1d0b9b 100644 --- a/leather/shapes/line.py +++ b/leather/shapes/line.py @@ -21,9 +21,10 @@ class Line(Shape): :param width: The width of the lines. Defaults to :data:`.theme.default_line_width`. """ - def __init__(self, stroke_color=None, width=None): + def __init__(self, stroke_color=None, width=None, stroke_dasharray=None): self._stroke_color = stroke_color self._width = width or theme.default_line_width + self._stroke_dasharray = stroke_dasharray or theme.default_stroke_dasharray def validate_series(self, series): """ @@ -44,6 +45,8 @@ def _new_path(self, stroke_color): fill='none' ) path.set('stroke-width', six.text_type(self._width)) + if self._stroke_dasharray != theme.STROKE_DASHARRAY_NONE: + path.set('stroke-dasharray', self._stroke_dasharray) return path diff --git a/leather/theme.py b/leather/theme.py index f678e03..4ece2e5 100644 --- a/leather/theme.py +++ b/leather/theme.py @@ -126,3 +126,7 @@ #: Default :class:`.Line` width default_line_width = 2 + +#: Default stroke-dasharray property when using dashes on a line +STROKE_DASHARRAY_NONE = 'none' +default_stroke_dasharray = STROKE_DASHARRAY_NONE From 8252e56a893ad46da73215ec242eb731f6783f46 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:54:16 -0400 Subject: [PATCH 2/2] chore: Don't set constants in theme.py --- docs/examples.rst | 4 ++-- leather/shapes/line.py | 2 +- leather/theme.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/examples.rst b/docs/examples.rst index 4c2fc85..8e421c9 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -193,9 +193,9 @@ Style attributes of individual data points can be set by value using a :func:`.s .. figure:: ../examples/charts/colorized_dots.svg Creating dashed lines ----------------------------- +--------------------- -When adding a line using :meth:`.Chart.add_line` you can set the `stroke_dasharray` property. More details on the property `here `_. +When using :meth:`.Chart.add_line`, you can set the `stroke_dasharray property `_. .. literalinclude:: ../examples/lines_dashes.py :language: python diff --git a/leather/shapes/line.py b/leather/shapes/line.py index d2fe4b9..73d51f7 100644 --- a/leather/shapes/line.py +++ b/leather/shapes/line.py @@ -42,7 +42,7 @@ def _new_path(self, stroke_color): fill='none' ) path.set('stroke-width', str(self._width)) - if self._stroke_dasharray != theme.STROKE_DASHARRAY_NONE: + if self._stroke_dasharray != 'none': path.set('stroke-dasharray', self._stroke_dasharray) return path diff --git a/leather/theme.py b/leather/theme.py index cf2ad95..b770d21 100644 --- a/leather/theme.py +++ b/leather/theme.py @@ -126,5 +126,4 @@ default_line_width = 2 #: Default stroke-dasharray property when using dashes on a line -STROKE_DASHARRAY_NONE = 'none' -default_stroke_dasharray = STROKE_DASHARRAY_NONE +default_stroke_dasharray = 'none'