| 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 = [ |
| 22 | dict(type="x", scale="x", ticks=10), |
| 23 | dict(type="y", scale="y", ticks=10), |
| 24 | ] |
| 25 | self.scales = [ |
| 26 | dict( |
| 27 | name="x", |
| 28 | domain=props["xlim"], |
| 29 | type="linear", |
| 30 | range="width", |
| 31 | ), |
| 32 | dict( |
| 33 | name="y", |
| 34 | domain=props["ylim"], |
| 35 | type="linear", |
| 36 | range="height", |
| 37 | ), |
| 38 | ] |
| 39 | |
| 40 | def draw_line(self, data, coordinates, style, label, mplobj=None): |
| 41 | if coordinates != "data": |
| 42 | warnings.warn("Only data coordinates supported. Skipping this") |
| 43 | dataname = "table{0:03d}".format(len(self.data) + 1) |
| 44 | |
| 45 | # TODO: respect the other style settings |
| 46 | self.data.append( |
| 47 | {"name": dataname, "values": [dict(x=d[0], y=d[1]) for d in data]} |
| 48 | ) |
| 49 | self.marks.append( |
| 50 | { |
| 51 | "type": "line", |
| 52 | "from": {"data": dataname}, |
| 53 | "properties": { |
| 54 | "enter": { |
| 55 | "interpolate": {"value": "monotone"}, |
| 56 | "x": {"scale": "x", "field": "data.x"}, |
| 57 | "y": {"scale": "y", "field": "data.y"}, |
| 58 | "stroke": {"value": style["color"]}, |
| 59 | "strokeOpacity": {"value": style["alpha"]}, |
| 60 | "strokeWidth": {"value": style["linewidth"]}, |
| 61 | } |
| 62 | }, |
| 63 | } |
| 64 | ) |
| 65 | |