Skip to content

Commit

Permalink
Added support for setting Text/Label rotation (#2814)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed Jun 20, 2018
1 parent f4c7477 commit 38dde13
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 5 additions & 2 deletions holoviews/plotting/bokeh/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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,))
Expand Down
34 changes: 33 additions & 1 deletion tests/plotting/bokeh/testannotationplot.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.)

0 comments on commit 38dde13

Please sign in to comment.