diff --git a/pynecone/components/graphing/__init__.py b/pynecone/components/graphing/__init__.py index eef486969..fe2aa5bca 100644 --- a/pynecone/components/graphing/__init__.py +++ b/pynecone/components/graphing/__init__.py @@ -13,8 +13,8 @@ from .victory import ( BoxPlot, Histogram, ErrorBar, - Group, - Stack, + ChartGroup, + ChartStack, Voronoi, ) diff --git a/pynecone/components/graphing/victory.py b/pynecone/components/graphing/victory.py index 89f1da5dc..4d0691ac3 100644 --- a/pynecone/components/graphing/victory.py +++ b/pynecone/components/graphing/victory.py @@ -4,6 +4,7 @@ from typing import Any, Dict, Union, List, Optional from pynecone.components.component import Component from pynecone.components.tags import Tag +from pynecone.style import Style from pynecone.var import Var @@ -292,7 +293,7 @@ def format_error_bar(x: List, y: List, error_x: List, error_y: List) -> List: raise ValueError("x, y, error_x, and error_y must be the same length") else: return [ - {"x": x[i], "y": y[i], "error_x": error_x[i], "error_y": error_y[i]} + {"x": x[i], "y": y[i], "errorX": error_x[i], "errorY": error_y[i]} for i in range(len(x)) ] @@ -375,9 +376,52 @@ class Victory(Component): # The sort order for the chart: "ascending", "descending" sort_order: Var[str] + # The padding for the chart. + padding: Var[Dict] + + # Domain padding for the chart. + domain_padding: Var[Dict] + + # A custom style for the code block. + custom_style: Var[Dict[str, str]] + + @classmethod + def create(cls, *children, **props): + """Create a chart component. + + Args: + *children: The children of the component. + **props: The props to pass to the component. + + Returns: + The chart component. + """ + # This component handles style in a special prop. + custom_style = props.pop("style", {}) + + # Transfer style props to the custom style prop. + for key, value in props.items(): + if key not in cls.get_fields(): + custom_style[key] = value + + # Create the component. + return super().create( + *children, + **props, + custom_style=Style(custom_style), + ) + + def _add_style(self, style): + self.custom_style = self.custom_style or {} + self.custom_style.update(style) # type: ignore + + def _render(self): + out = super()._render() + return out.add_props(style=self.custom_style).remove_props("custom_style") + class Chart(Victory): - """Display a victory graph.""" + """Wrapper component that renders a given set of children on a set of Cartesian or polar axes.""" tag = "VictoryChart" @@ -453,6 +497,15 @@ class Pie(Victory): # Specifies the radius of the pie. When this prop is not given, it will be calculated based on the width, height, and padding props. radius: Var[float] + # Specifies the inner radius of the pie. When this prop is not given, it will default to 0. + inner_radius: Var[float] + + # Specifies the start angle of the first slice in number of degrees. Default is 0. + start_angle: Var[float] + + # Specifies the end angle of the last slice in number of degrees. Default is 360. + end_angle: Var[float] + class Candlestick(Victory): """Display a victory candlestick.""" @@ -520,13 +573,13 @@ class ErrorBar(Victory): border_width: Var[float] -class Group(Victory): +class ChartGroup(Victory): """Display a victory group.""" tag = "VictoryGroup" # Optional prop that defines a color scale to be applied to the children of the group. Takes in an array of colors. Default color scale are: "grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue". - color_scale: Var[List[str]] + color_scale: Var[str] # Optional prop that defines a single color to be applied to the children of the group. Overrides color_scale. color: Var[str] @@ -535,7 +588,7 @@ class Group(Victory): offset: Var[float] -class Stack(Victory): +class ChartStack(Victory): """Display a victory stack.""" tag = "VictoryStack" diff --git a/tests/components/graphing/test_victory_data.py b/tests/components/graphing/test_victory_data.py index 1b96e5ce5..84d1390f2 100644 --- a/tests/components/graphing/test_victory_data.py +++ b/tests/components/graphing/test_victory_data.py @@ -153,10 +153,10 @@ def test_candlestick(): def test_errorbar(): output = data(graph="error_bar", x=x_num, y=y1, error_y=y1, error_x=y1) expected = [ - {"x": 1, "y": 5, "error_y": 5, "error_x": 5}, - {"x": 2, "y": 12, "error_y": 12, "error_x": 12}, - {"x": 3, "y": 4, "error_y": 4, "error_x": 4}, - {"x": 4, "y": 6, "error_y": 6, "error_x": 6}, - {"x": 5, "y": 1, "error_y": 1, "error_x": 1}, + {"x": 1, "y": 5, "errorY": 5, "errorX": 5}, + {"x": 2, "y": 12, "errorY": 12, "errorX": 12}, + {"x": 3, "y": 4, "errorY": 4, "errorX": 4}, + {"x": 4, "y": 6, "errorY": 6, "errorX": 6}, + {"x": 5, "y": 1, "errorY": 1, "errorX": 1}, ] assert output == expected