|
| 1 | +import matplotlib.dates as mdates |
| 2 | +from ._data import _locate_channel_data, _locate_channel_dtype |
1 | 3 |
|
| 4 | +def _allowed_ranged_marks(enc_channel, mark): |
| 5 | + """TODO: DOCS |
| 6 | + """ |
| 7 | + return mark in ['area', 'bar', 'rect', 'rule'] if enc_channel in ['x2', 'y2'] else True |
| 8 | + |
| 9 | +def _process_x(dtype, data): |
| 10 | + """Returns the MPL encoding equivalent for Altair x channel |
| 11 | + """ |
| 12 | + return ('x', data) |
| 13 | + |
| 14 | + |
| 15 | +def _process_y(dtype, data): |
| 16 | + """Returns the MPL encoding equivalent for Altair y channel |
| 17 | + """ |
| 18 | + return ('y', data) |
| 19 | + |
| 20 | + |
| 21 | +def _process_x2(dtype, data): |
| 22 | + """Returns the MPL encoding equivalent for Altair x2 channel |
| 23 | + """ |
| 24 | + raise NotImplementedError |
| 25 | + |
| 26 | + |
| 27 | +def _process_y2(dtype, data): |
| 28 | + """Returns the MPL encoding equivalent for Altair y2 channel |
| 29 | + """ |
| 30 | + raise NotImplementedError |
| 31 | + |
| 32 | + |
| 33 | +def _process_color(dtype, data): |
| 34 | + """Returns the MPL encoding equivalent for Altair color channel |
| 35 | + """ |
| 36 | + if dtype == 'quantitative': |
| 37 | + return ('c', data) |
| 38 | + elif dtype == 'nominal': |
| 39 | + raise NotImplementedError |
| 40 | + elif dtype == 'ordinal': |
| 41 | + return ('c', data) |
| 42 | + else: # temporal |
| 43 | + return ('c', data) |
| 44 | + |
| 45 | + |
| 46 | +def _process_fill(dtype, data): |
| 47 | + """Returns the MPL encoding equivalent for Altair fill channel |
| 48 | + """ |
| 49 | + return _process_color(dtype, data) |
| 50 | + |
| 51 | + |
| 52 | +def _process_shape(dtype, data): |
| 53 | + """Returns the MPL encoding equivalent for Altair shape channel |
| 54 | + """ |
| 55 | + raise NotImplementedError |
| 56 | + |
| 57 | + |
| 58 | +def _process_opacity(dtype, data): |
| 59 | + """Returns the MPL encoding equivalent for Altair opacity channel |
| 60 | + """ |
| 61 | + raise NotImplementedError |
| 62 | + |
| 63 | + |
| 64 | +def _process_size(dtype, data): |
| 65 | + """Returns the MPL encoding equivalent for Altair size channel |
| 66 | + """ |
| 67 | + if dtype == 'quantitative': |
| 68 | + return ('s', data) |
| 69 | + elif dtype == 'nominal': |
| 70 | + raise NotImplementedError |
| 71 | + elif dtype == 'ordinal': |
| 72 | + return ('s', data) |
| 73 | + elif dtype == 'temporal': |
| 74 | + raise NotImplementedError |
| 75 | + |
| 76 | + |
| 77 | +def _process_stroke(dtype, data): |
| 78 | + """Returns the MPL encoding equivalent for Altair stroke channel |
| 79 | + """ |
| 80 | + raise NotImplementedError |
| 81 | + |
| 82 | +_mappings = { |
| 83 | + 'x': _process_x, |
| 84 | + 'y': _process_y, |
| 85 | + 'x2': _process_x2, |
| 86 | + 'y2': _process_y2, |
| 87 | + 'color': _process_color, |
| 88 | + 'fill': _process_fill, |
| 89 | + 'shape': _process_shape, |
| 90 | + 'opacity': _process_opacity, |
| 91 | + 'size': _process_size, |
| 92 | + 'stroke': _process_stroke, |
| 93 | +} |
| 94 | + |
| 95 | +def _convert(chart): |
| 96 | + """Convert an altair encoding to a Matplotlib figure |
| 97 | +
|
| 98 | +
|
| 99 | + Parameters |
| 100 | + ---------- |
| 101 | + chart |
| 102 | + The Altair chart. |
| 103 | +
|
| 104 | + Returns |
| 105 | + ------- |
| 106 | + mapping : dict |
| 107 | + Mapping from parts of the encoding to the Matplotlib artists. This is |
| 108 | + for later customization. |
| 109 | + """ |
| 110 | + mapping = {} |
| 111 | + |
| 112 | + if not chart.to_dict().get('encoding'): |
| 113 | + raise ValueError("Encoding not provided with the chart specification") |
| 114 | + |
| 115 | + for enc_channel, enc_spec in chart.to_dict()['encoding'].items(): |
| 116 | + if not _allowed_ranged_marks(enc_channel, chart.to_dict()['mark']): |
| 117 | + raise ValueError("Ranged encoding channels like x2, y2 not allowed for Mark: {}".format(chart['mark'])) |
| 118 | + |
| 119 | + for channel in chart.to_dict()['encoding']: |
| 120 | + data = _locate_channel_data(chart, channel) |
| 121 | + dtype = _locate_channel_dtype(chart, channel) |
| 122 | + if dtype == 'temporal': |
| 123 | + try: |
| 124 | + data = mdates.date2num(data) # Convert dates to Matplotlib dates |
| 125 | + except AttributeError: |
| 126 | + raise |
| 127 | + mapping[_mappings[channel](dtype, data)[0]] = _mappings[channel](dtype, data)[1] |
| 128 | + |
| 129 | + return mapping |
0 commit comments