diff --git a/docs/examples.rst b/docs/examples.rst index 6ee85cd..8e421c9 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 using :meth:`.Chart.add_line`, you can set the `stroke_dasharray property `_. + +.. 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 8d682f5..cdc1b2e 100644 --- a/leather/chart.py +++ b/leather/chart.py @@ -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): diff --git a/leather/shapes/line.py b/leather/shapes/line.py index 4749fcb..73d51f7 100644 --- a/leather/shapes/line.py +++ b/leather/shapes/line.py @@ -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): """ @@ -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 diff --git a/leather/theme.py b/leather/theme.py index 5f99c09..b770d21 100644 --- a/leather/theme.py +++ b/leather/theme.py @@ -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'