| 1 |
import warnings |
| 2 |
import json |
| 3 |
import random |
| 4 |
from .base import Renderer |
| 5 |
from ..exporter import Exporter |
| 6 |
|
| 7 |
|
| 8 |
class VegaRenderer(Renderer): |
| 9 |
def open_figure(self, fig, props): |
| 10 |
self.props = props |
| 11 |
self.figwidth = int(props['figwidth'] * props['dpi']) |
| 12 |
self.figheight = int(props['figheight'] * props['dpi']) |
| 13 |
self.data = [] |
| 14 |
self.scales = [] |
| 15 |
self.axes = [] |
| 16 |
self.marks = [] |
| 17 |
|
| 18 |
def open_axes(self, ax, props): |
| 19 |
if len(self.axes) > 0: |
| 20 |
warnings.warn("multiple axes not yet supported") |
| 21 |
self.axes = [dict(type="x", scale="x", ticks=10), |
| 22 |
dict(type="y", scale="y", ticks=10)] |
| 23 |
self.scales = [dict(name="x", |
| 24 |
domain=props['xlim'], |
| 25 |
type="linear", |
| 26 |
range="width", |
| 27 |
), |
| 28 |
dict(name="y", |
| 29 |
domain=props['ylim'], |
| 30 |
type="linear", |
| 31 |
range="height", |
| 32 |
),] |
| 33 |
|
| 34 |
def draw_line(self, data, coordinates, style, label, mplobj=None): |
| 35 |
if coordinates != 'data': |
| 36 |
warnings.warn("Only data coordinates supported. Skipping this") |
| 37 |
dataname = "table{0:03d}".format(len(self.data) + 1) |
| 38 |
|
| 39 |
|
| 40 |
self.data.append({'name': dataname, |
| 41 |
'values': [dict(x=d[0], y=d[1]) for d in data]}) |
| 42 |
self.marks.append({'type': 'line', |
| 43 |
'from': {'data': dataname}, |
| 44 |
'properties': { |
| 45 |
"enter": { |
| 46 |
"interpolate": {"value": "monotone"}, |
| 47 |
"x": {"scale": "x", "field": "data.x"}, |
| 48 |
"y": {"scale": "y", "field": "data.y"}, |
| 49 |
"stroke": {"value": style['color']}, |
| 50 |
"strokeOpacity": {"value": style['alpha']}, |
| 51 |
"strokeWidth": {"value": style['linewidth']}, |
| 52 |
} |
| 53 |
} |
| 54 |
}) |
| 55 |
|
| 56 |
def draw_markers(self, data, coordinates, style, label, mplobj=None): |
| 57 |
if coordinates != 'data': |
| 58 |
warnings.warn("Only data coordinates supported. Skipping this") |
| 59 |
dataname = "table{0:03d}".format(len(self.data) + 1) |
| 60 |
|
| 61 |
|
| 62 |
self.data.append({'name': dataname, |
| 63 |
'values': [dict(x=d[0], y=d[1]) for d in data]}) |
| 64 |
self.marks.append({'type': 'symbol', |
| 65 |
'from': {'data': dataname}, |
| 66 |
'properties': { |
| 67 |
"enter": { |
| 68 |
"interpolate": {"value": "monotone"}, |
| 69 |
"x": {"scale": "x", "field": "data.x"}, |
| 70 |
"y": {"scale": "y", "field": "data.y"}, |
| 71 |
"fill": {"value": style['facecolor']}, |
| 72 |
"fillOpacity": {"value": style['alpha']}, |
| 73 |
"stroke": {"value": style['edgecolor']}, |
| 74 |
"strokeOpacity": {"value": style['alpha']}, |
| 75 |
"strokeWidth": {"value": style['edgewidth']}, |
| 76 |
} |
| 77 |
} |
| 78 |
}) |
| 79 |
|
| 80 |
def draw_text(self, text, position, coordinates, style, |
| 81 |
text_type=None, mplobj=None): |
| 82 |
if text_type == 'xlabel': |
| 83 |
self.axes[0]['title'] = text |
| 84 |
elif text_type == 'ylabel': |
| 85 |
self.axes[1]['title'] = text |
| 86 |
|
| 87 |
|
| 88 |
class VegaHTML(object): |
| 89 |
def __init__(self, renderer): |
| 90 |
self.specification = dict(width=renderer.figwidth, |
| 91 |
height=renderer.figheight, |
| 92 |
data=renderer.data, |
| 93 |
scales=renderer.scales, |
| 94 |
axes=renderer.axes, |
| 95 |
marks=renderer.marks) |
| 96 |
|
| 97 |
def html(self): |
| 98 |
"""Build the HTML representation for IPython.""" |
| 99 |
id = random.randint(0, 2 ** 16) |
| 100 |
html = '<div id="vis%d"></div>' % id |
| 101 |
html += '<script>\n' |
| 102 |
html += VEGA_TEMPLATE % (json.dumps(self.specification), id) |
| 103 |
html += '</script>\n' |
| 104 |
return html |
| 105 |
|
| 106 |
def _repr_html_(self): |
| 107 |
return self.html() |
| 108 |
|
| 109 |
|
| 110 |
def fig_to_vega(fig, notebook=False): |
| 111 |
"""Convert a matplotlib figure to vega dictionary |
| 112 |
|
| 113 |
if notebook=True, then return an object which will display in a notebook |
| 114 |
otherwise, return an HTML string. |
| 115 |
""" |
| 116 |
renderer = VegaRenderer() |
| 117 |
Exporter(renderer).run(fig) |
| 118 |
vega_html = VegaHTML(renderer) |
| 119 |
if notebook: |
| 120 |
return vega_html |
| 121 |
else: |
| 122 |
return vega_html.html() |
| 123 |
|
| 124 |
|
| 125 |
VEGA_TEMPLATE = """ |
| 126 |
( function() { |
| 127 |
var _do_plot = function() { |
| 128 |
if ( (typeof vg == 'undefined') && (typeof IPython != 'undefined')) { |
| 129 |
$([IPython.events]).on("vega_loaded.vincent", _do_plot); |
| 130 |
return; |
| 131 |
} |
| 132 |
vg.parse.spec(%s, function(chart) { |
| 133 |
chart({el: "#vis%d"}).update(); |
| 134 |
}); |
| 135 |
}; |
| 136 |
_do_plot(); |
| 137 |
})(); |
| 138 |
""" |