|
10 | 10 |
|
11 | 11 | from ggsql._ggsql import ( |
12 | 12 | DuckDBReader, |
13 | | - VegaLiteWriter, |
| 13 | + VegaLiteWriter as _RustVegaLiteWriter, |
14 | 14 | Validated, |
15 | 15 | Spec, |
16 | 16 | validate, |
@@ -75,6 +75,64 @@ def register( |
75 | 75 | ) -> None: ... |
76 | 76 |
|
77 | 77 |
|
| 78 | +def _json_to_altair_chart(vegalite_json: str, **kwargs: Any) -> AltairChart: |
| 79 | + """Convert a Vega-Lite JSON string to the appropriate Altair chart type.""" |
| 80 | + spec = json.loads(vegalite_json) |
| 81 | + |
| 82 | + if "layer" in spec: |
| 83 | + return altair.LayerChart.from_json(vegalite_json, **kwargs) |
| 84 | + elif "facet" in spec or "spec" in spec: |
| 85 | + return altair.FacetChart.from_json(vegalite_json, **kwargs) |
| 86 | + elif "concat" in spec: |
| 87 | + return altair.ConcatChart.from_json(vegalite_json, **kwargs) |
| 88 | + elif "hconcat" in spec: |
| 89 | + return altair.HConcatChart.from_json(vegalite_json, **kwargs) |
| 90 | + elif "vconcat" in spec: |
| 91 | + return altair.VConcatChart.from_json(vegalite_json, **kwargs) |
| 92 | + elif "repeat" in spec: |
| 93 | + return altair.RepeatChart.from_json(vegalite_json, **kwargs) |
| 94 | + else: |
| 95 | + return altair.Chart.from_json(vegalite_json, **kwargs) |
| 96 | + |
| 97 | + |
| 98 | +class VegaLiteWriter: |
| 99 | + """Vega-Lite v6 JSON output writer. |
| 100 | +
|
| 101 | + Methods |
| 102 | + ------- |
| 103 | + render(spec) |
| 104 | + Render a Spec to a Vega-Lite JSON string. |
| 105 | + render_chart(spec, **kwargs) |
| 106 | + Render a Spec to an Altair chart object. |
| 107 | + """ |
| 108 | + |
| 109 | + def __init__(self) -> None: |
| 110 | + self._inner = _RustVegaLiteWriter() |
| 111 | + |
| 112 | + def render(self, spec: Spec) -> str: |
| 113 | + """Render a Spec to a Vega-Lite JSON string.""" |
| 114 | + return self._inner.render(spec) |
| 115 | + |
| 116 | + def render_chart(self, spec: Spec, **kwargs: Any) -> AltairChart: |
| 117 | + """Render a Spec to an Altair chart object. |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + spec |
| 122 | + The resolved visualization specification from ``reader.execute()``. |
| 123 | + **kwargs |
| 124 | + Additional keyword arguments passed to ``altair.Chart.from_json()``. |
| 125 | + Common options include ``validate=False`` to skip schema validation. |
| 126 | +
|
| 127 | + Returns |
| 128 | + ------- |
| 129 | + AltairChart |
| 130 | + An Altair chart object (Chart, LayerChart, FacetChart, etc.). |
| 131 | + """ |
| 132 | + vegalite_json = self.render(spec) |
| 133 | + return _json_to_altair_chart(vegalite_json, **kwargs) |
| 134 | + |
| 135 | + |
78 | 136 | def render_altair( |
79 | 137 | df: IntoFrame, |
80 | 138 | viz: str, |
@@ -120,21 +178,4 @@ def render_altair( |
120 | 178 | writer = VegaLiteWriter() |
121 | 179 | vegalite_json = writer.render(spec) |
122 | 180 |
|
123 | | - # Parse to determine the correct Altair class |
124 | | - spec = json.loads(vegalite_json) |
125 | | - |
126 | | - # Determine the correct Altair class based on spec structure |
127 | | - if "layer" in spec: |
128 | | - return altair.LayerChart.from_json(vegalite_json, **kwargs) |
129 | | - elif "facet" in spec or "spec" in spec: |
130 | | - return altair.FacetChart.from_json(vegalite_json, **kwargs) |
131 | | - elif "concat" in spec: |
132 | | - return altair.ConcatChart.from_json(vegalite_json, **kwargs) |
133 | | - elif "hconcat" in spec: |
134 | | - return altair.HConcatChart.from_json(vegalite_json, **kwargs) |
135 | | - elif "vconcat" in spec: |
136 | | - return altair.VConcatChart.from_json(vegalite_json, **kwargs) |
137 | | - elif "repeat" in spec: |
138 | | - return altair.RepeatChart.from_json(vegalite_json, **kwargs) |
139 | | - else: |
140 | | - return altair.Chart.from_json(vegalite_json, **kwargs) |
| 181 | + return _json_to_altair_chart(vegalite_json, **kwargs) |
0 commit comments