Skip to content

Commit

Permalink
Merge branch 'nickromano/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Oct 23, 2023
2 parents 77c49a0 + 8252e56 commit 2bd662c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 using :meth:`.Chart.add_line`, you can set the `stroke_dasharray property <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray>`_.

.. literalinclude:: ../examples/lines_dashes.py
:language: python

.. figure:: ../examples/charts/lines_dashes.svg


Chart grids
===========

Expand Down
4 changes: 4 additions & 0 deletions examples/charts/lines_dashes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions examples/lines_dashes.py
Original file line number Diff line number Diff line change
@@ -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')
4 changes: 2 additions & 2 deletions leather/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,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):
Expand Down
5 changes: 4 additions & 1 deletion leather/shapes/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,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):
"""
Expand All @@ -41,6 +42,8 @@ def _new_path(self, stroke_color):
fill='none'
)
path.set('stroke-width', str(self._width))
if self._stroke_dasharray != 'none':
path.set('stroke-dasharray', self._stroke_dasharray)

return path

Expand Down
3 changes: 3 additions & 0 deletions leather/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@

#: Default :class:`.Line` width
default_line_width = 2

#: Default stroke-dasharray property when using dashes on a line
default_stroke_dasharray = 'none'

0 comments on commit 2bd662c

Please sign in to comment.