From 38dde1357cfe39ccb3e5e22d63340eb59ae2e9fd Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Wed, 20 Jun 2018 16:29:28 +0100 Subject: [PATCH] Added support for setting Text/Label rotation (#2814) --- holoviews/plotting/bokeh/annotation.py | 7 +++-- tests/plotting/bokeh/testannotationplot.py | 34 +++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/holoviews/plotting/bokeh/annotation.py b/holoviews/plotting/bokeh/annotation.py index a6dea97277..e7be56b704 100644 --- a/holoviews/plotting/bokeh/annotation.py +++ b/holoviews/plotting/bokeh/annotation.py @@ -25,7 +25,7 @@ class TextPlot(ElementPlot): - style_opts = text_properties+['color'] + style_opts = text_properties+['color', 'angle'] _plot_methods = dict(single='text', batched='text') def get_data(self, element, ranges, style): @@ -47,6 +47,7 @@ def get_data(self, element, ranges, style): style['text_font_size'] = '%dPt' % element.fontsize if 'color' in style: style['text_color'] = style.pop('color') + style['angle'] = np.deg2rad(style.get('angle', element.rotation)) return (data, mapping, style) def get_batched_data(self, element, ranges=None): @@ -80,13 +81,15 @@ class LabelsPlot(ColorbarPlot): yoffset = param.Number(default=None, doc=""" Amount of offset to apply to labels along x-axis.""") - style_opts = text_properties + ['cmap'] + style_opts = text_properties + ['cmap', 'angle'] _plot_methods = dict(single='text', batched='text') _batched_style_opts = text_properties def get_data(self, element, ranges, style): style = self.style[self.cyclic_index] + style['angle'] = np.deg2rad(style.get('angle', 0)) + dims = element.dimensions() coords = (1, 0) if self.invert_axes else (0, 1) xdim, ydim, tdim = (dimension_sanitizer(dims[i].name) for i in coords+(2,)) diff --git a/tests/plotting/bokeh/testannotationplot.py b/tests/plotting/bokeh/testannotationplot.py index a87fc4bf16..620adf7d66 100644 --- a/tests/plotting/bokeh/testannotationplot.py +++ b/tests/plotting/bokeh/testannotationplot.py @@ -1,4 +1,6 @@ -from holoviews.element import HLine, VLine, Text +import numpy as np + +from holoviews.element import HLine, VLine, Text, Labels from .testplot import TestBokehPlot, bokeh_renderer @@ -47,3 +49,33 @@ def test_text_plot_fontsize(self): plot = bokeh_renderer.get_plot(text) glyph = plot.handles['glyph'] self.assertEqual(glyph.text_font_size, '18Pt') + + def test_text_plot_rotation(self): + text = Text(0, 0, 'Test', rotation=90) + plot = bokeh_renderer.get_plot(text) + glyph = plot.handles['glyph'] + self.assertEqual(glyph.angle, np.pi/2.) + + def test_text_plot_rotation_style(self): + text = Text(0, 0, 'Test').options(angle=90) + plot = bokeh_renderer.get_plot(text) + glyph = plot.handles['glyph'] + self.assertEqual(glyph.angle, np.pi/2.) + + + +class TestLabelsPlot(TestBokehPlot): + + def test_labels_plot(self): + text = Labels([(0, 0, 'Test')]) + plot = bokeh_renderer.get_plot(text) + source = plot.handles['source'] + data = {'x': np.array([0]), 'y': np.array([0]), 'Label': ['Test']} + for c, col in source.data.items(): + self.assertEqual(col, data[c]) + + def test_labels_plot_rotation_style(self): + text = Labels([(0, 0, 'Test')]).options(angle=90) + plot = bokeh_renderer.get_plot(text) + glyph = plot.handles['glyph'] + self.assertEqual(glyph.angle, np.pi/2.)