Skip to content

Commit 6655709

Browse files
committed
Rename to charts
1 parent d9ebb61 commit 6655709

File tree

19 files changed

+239
-239
lines changed

19 files changed

+239
-239
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .main import graph_figure_to_graph
1+
from .main import chart_figure_to_chart, chart_figure_to_dict
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .base import ChartType, Chart
2+
from .bars import BarChart, BoxAndWhiskerChart
3+
from .pie import PieChart
4+
from .planar import ScatterChart, LineChart

chart_data_extractor/e2b_charts/graphs/bars.py renamed to chart_data_extractor/e2b_charts/charts/bars.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from matplotlib.axes import Axes
44
from pydantic import BaseModel, Field
55

6-
from .base import Graph2D, GraphType
6+
from .base import Chart2D, ChartType
77
from ..utils.rounding import dynamic_round
88

99

@@ -13,8 +13,8 @@ class BarData(BaseModel):
1313
value: float
1414

1515

16-
class BarGraph(Graph2D):
17-
type: Literal[GraphType.BAR] = GraphType.BAR
16+
class BarChart(Chart2D):
17+
type: Literal[ChartType.BAR] = ChartType.BAR
1818

1919
elements: List[BarData] = Field(default_factory=list)
2020

@@ -52,8 +52,8 @@ class BoxAndWhiskerData(BaseModel):
5252
outliers: List[float]
5353

5454

55-
class BoxAndWhiskerGraph(Graph2D):
56-
type: Literal[GraphType.BOX_AND_WHISKER] = GraphType.BOX_AND_WHISKER
55+
class BoxAndWhiskerChart(Chart2D):
56+
type: Literal[ChartType.BOX_AND_WHISKER] = ChartType.BOX_AND_WHISKER
5757

5858
elements: List[BoxAndWhiskerData] = Field(default_factory=list)
5959

chart_data_extractor/e2b_charts/graphs/base.py renamed to chart_data_extractor/e2b_charts/charts/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
from pydantic import BaseModel, Field
77

88

9-
class GraphType(str, enum.Enum):
9+
class ChartType(str, enum.Enum):
1010
LINE = "line"
1111
SCATTER = "scatter"
1212
BAR = "bar"
1313
PIE = "pie"
1414
BOX_AND_WHISKER = "box_and_whisker"
15-
SUPERGRAPH = "supergraph"
15+
SUPERCHART = "superchart"
1616
UNKNOWN = "unknown"
1717

1818

19-
class Graph(BaseModel):
20-
type: GraphType
19+
class Chart(BaseModel):
20+
type: ChartType
2121
title: Optional[str] = None
2222

2323
elements: List[Any] = Field(default_factory=list)
@@ -29,7 +29,7 @@ def __init__(self, ax: Optional[Axes] = None, **kwargs):
2929

3030
def _extract_info(self, ax: Axes) -> None:
3131
"""
32-
Function to extract information for Graph
32+
Function to extract information for Chart
3333
"""
3434
title = ax.get_title()
3535
if title == "":
@@ -38,15 +38,15 @@ def _extract_info(self, ax: Axes) -> None:
3838
self.title = title
3939

4040

41-
class Graph2D(Graph):
41+
class Chart2D(Chart):
4242
x_label: Optional[str] = None
4343
y_label: Optional[str] = None
4444
x_unit: Optional[str] = None
4545
y_unit: Optional[str] = None
4646

4747
def _extract_info(self, ax: Axes) -> None:
4848
"""
49-
Function to extract information for Graph2D
49+
Function to extract information for Chart2D
5050
"""
5151
super()._extract_info(ax)
5252
x_label = ax.get_xlabel()

chart_data_extractor/e2b_charts/graphs/pie.py renamed to chart_data_extractor/e2b_charts/charts/pie.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from matplotlib.axes import Axes
55
from pydantic import BaseModel, Field
66

7-
from .base import Graph, GraphType
7+
from .base import Chart, ChartType
88
from ..utils.rounding import dynamic_round
99

1010

@@ -14,8 +14,8 @@ class PieData(BaseModel):
1414
radius: float
1515

1616

17-
class PieGraph(Graph):
18-
type: Literal[GraphType.PIE] = GraphType.PIE
17+
class PieChart(Chart):
18+
type: Literal[ChartType.PIE] = ChartType.PIE
1919

2020
elements: List[PieData] = Field(default_factory=list)
2121

chart_data_extractor/e2b_charts/graphs/planar.py renamed to chart_data_extractor/e2b_charts/charts/planar.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from matplotlib.dates import _SwitchableDateConverter
88
from pydantic import BaseModel, field_validator, Field
99

10-
from .base import Graph2D, GraphType
10+
from .base import Chart2D, ChartType
1111
from ..utils import is_grid_line
1212

1313

@@ -36,7 +36,7 @@ def _parse_point(point):
3636
return point
3737

3838

39-
class PointGraph(Graph2D):
39+
class PointChart(Chart2D):
4040
x_ticks: List[Union[str, float]] = Field(default_factory=list)
4141
x_tick_labels: List[str] = Field(default_factory=list)
4242
x_scale: str = Field(default="linear")
@@ -49,7 +49,7 @@ class PointGraph(Graph2D):
4949

5050
def _extract_info(self, ax: Axes) -> None:
5151
"""
52-
Function to extract information for PointGraph
52+
Function to extract information for PointChart
5353
"""
5454
super()._extract_info(ax)
5555

@@ -101,8 +101,8 @@ def _extract_ticks_info(converter: Any, ticks: Sequence) -> list:
101101
return list(ticks)
102102

103103

104-
class LineGraph(PointGraph):
105-
type: Literal[GraphType.LINE] = GraphType.LINE
104+
class LineChart(PointChart):
105+
type: Literal[ChartType.LINE] = ChartType.LINE
106106

107107
def _extract_info(self, ax: Axes) -> None:
108108
super()._extract_info(ax)
@@ -120,8 +120,8 @@ def _extract_info(self, ax: Axes) -> None:
120120
self.elements.append(line_data)
121121

122122

123-
class ScatterGraph(PointGraph):
124-
type: Literal[GraphType.SCATTER] = GraphType.SCATTER
123+
class ScatterChart(PointChart):
124+
type: Literal[ChartType.SCATTER] = ChartType.SCATTER
125125

126126
def _extract_info(self, ax: Axes) -> None:
127127
super()._extract_info(ax)

chart_data_extractor/e2b_charts/graphs/__init__.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

chart_data_extractor/e2b_charts/main.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,40 @@
99
from matplotlib.text import Text
1010
from pydantic import Field
1111

12-
from .graphs import (
13-
GraphType,
14-
Graph,
15-
LineGraph,
16-
BarGraph,
17-
BoxAndWhiskerGraph,
18-
PieGraph,
19-
ScatterGraph,
12+
from .charts import (
13+
ChartType,
14+
Chart,
15+
LineChart,
16+
BarChart,
17+
BoxAndWhiskerChart,
18+
PieChart,
19+
ScatterChart,
2020
)
2121
from .utils.filtering import is_grid_line
2222

2323

24-
class SuperGraph(Graph):
25-
type: Literal[GraphType.SUPERGRAPH] = GraphType.SUPERGRAPH
24+
class SuperChart(Chart):
25+
type: Literal[ChartType.SUPERCHART] = ChartType.SUPERCHART
2626
elements: List[
27-
LineGraph | ScatterGraph | BarGraph | PieGraph | BoxAndWhiskerGraph
27+
LineChart | ScatterChart | BarChart | PieChart | BoxAndWhiskerChart
2828
] = Field(default_factory=list)
2929

3030
def __init__(self, figure: Figure):
3131
title = figure.get_suptitle()
3232
super().__init__(title=title)
3333

34-
self.elements = [get_graph_from_ax(ax) for ax in figure.axes]
34+
self.elements = [get_chart_from_ax(ax) for ax in figure.axes]
3535

3636

37-
def _get_type_of_graph(ax: Axes) -> GraphType:
37+
def _get_type_of_chart(ax: Axes) -> ChartType:
3838
objects = list(filter(lambda obj: not isinstance(obj, Text), ax._children))
3939

4040
# Check for Line plots
4141
if all(isinstance(line, Line2D) for line in objects):
42-
return GraphType.LINE
42+
return ChartType.LINE
4343

4444
if all(isinstance(box_or_path, (PathPatch, Line2D)) for box_or_path in objects):
45-
return GraphType.BOX_AND_WHISKER
45+
return ChartType.BOX_AND_WHISKER
4646

4747
filtered = []
4848
for obj in objects:
@@ -54,41 +54,41 @@ def _get_type_of_graph(ax: Axes) -> GraphType:
5454

5555
# Check for Scatter plots
5656
if all(isinstance(path, PathCollection) for path in objects):
57-
return GraphType.SCATTER
57+
return ChartType.SCATTER
5858

5959
# Check for Pie plots
6060
if all(isinstance(artist, Wedge) for artist in objects):
61-
return GraphType.PIE
61+
return ChartType.PIE
6262

6363
# Check for Bar plots
6464
if all(isinstance(rect, Rectangle) for rect in objects):
65-
return GraphType.BAR
65+
return ChartType.BAR
6666

67-
return GraphType.UNKNOWN
67+
return ChartType.UNKNOWN
6868

6969

70-
def get_graph_from_ax(
70+
def get_chart_from_ax(
7171
ax: Axes,
72-
) -> LineGraph | ScatterGraph | BarGraph | PieGraph | BoxAndWhiskerGraph | Graph:
73-
graph_type = _get_type_of_graph(ax)
74-
75-
if graph_type == GraphType.LINE:
76-
graph = LineGraph(ax=ax)
77-
elif graph_type == GraphType.SCATTER:
78-
graph = ScatterGraph(ax=ax)
79-
elif graph_type == GraphType.BAR:
80-
graph = BarGraph(ax=ax)
81-
elif graph_type == GraphType.PIE:
82-
graph = PieGraph(ax=ax)
83-
elif graph_type == GraphType.BOX_AND_WHISKER:
84-
graph = BoxAndWhiskerGraph(ax=ax)
72+
) -> LineChart | ScatterChart | BarChart | PieChart | BoxAndWhiskerChart | Chart:
73+
chart_type = _get_type_of_chart(ax)
74+
75+
if chart_type == ChartType.LINE:
76+
chart = LineChart(ax=ax)
77+
elif chart_type == ChartType.SCATTER:
78+
chart = ScatterChart(ax=ax)
79+
elif chart_type == ChartType.BAR:
80+
chart = BarChart(ax=ax)
81+
elif chart_type == ChartType.PIE:
82+
chart = PieChart(ax=ax)
83+
elif chart_type == ChartType.BOX_AND_WHISKER:
84+
chart = BoxAndWhiskerChart(ax=ax)
8585
else:
86-
graph = Graph(ax=ax, type=graph_type)
86+
chart = Chart(ax=ax, type=chart_type)
8787

88-
return graph
88+
return chart
8989

9090

91-
def graph_figure_to_graph(figure: Figure) -> Optional[Graph]:
91+
def chart_figure_to_chart(figure: Figure) -> Optional[Chart]:
9292
"""
9393
This method is used to extract data from the figure object to a dictionary
9494
"""
@@ -98,14 +98,14 @@ def graph_figure_to_graph(figure: Figure) -> Optional[Graph]:
9898
if not axes:
9999
return
100100
elif len(axes) > 1:
101-
return SuperGraph(figure=figure)
101+
return SuperChart(figure=figure)
102102
else:
103103
ax = axes[0]
104-
return get_graph_from_ax(ax)
104+
return get_chart_from_ax(ax)
105105

106106

107-
def graph_figure_to_dict(figure: Figure) -> dict:
108-
graph = graph_figure_to_graph(figure)
109-
if graph:
110-
return graph.model_dump()
107+
def chart_figure_to_dict(figure: Figure) -> dict:
108+
chart = chart_figure_to_chart(figure)
109+
if chart:
110+
return chart.model_dump()
111111
return {}

chart_data_extractor/tests/graphs/test_bar.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import matplotlib.pyplot as plt
22

3-
from e2b_charts import graph_figure_to_graph
4-
from e2b_charts.graphs import BarGraph, GraphType
3+
from e2b_charts import chart_figure_to_chart
4+
from e2b_charts.charts import BarChart, ChartType
55

66

7-
def _prep_graph_figure():
7+
def _prep_chart_figure():
88
# Prepare data
99
authors = ["Author A", "Author B", "Author C", "Author D"]
1010
sales = [100, 200, 300, 400]
1111

12-
# Create and customize the bar graph
12+
# Create and customize the bar chart
1313
plt.figure(figsize=(10, 6))
1414
plt.bar(authors, sales, label="Books Sold", color="blue")
1515
plt.xlabel("Authors")
1616
plt.ylabel("Number of Books Sold")
1717
plt.title("Book Sales by Authors")
1818

19-
# Display the graph
19+
# Display the chart
2020
plt.tight_layout()
2121
return plt.gcf()
2222

2323

24-
def test_graph_bar():
25-
figure = _prep_graph_figure()
26-
graph = graph_figure_to_graph(figure)
27-
assert graph
24+
def test_chart_bar():
25+
figure = _prep_chart_figure()
26+
chart = chart_figure_to_chart(figure)
27+
assert chart
2828

29-
assert isinstance(graph, BarGraph)
30-
assert graph.type == GraphType.BAR
31-
assert graph.title == "Book Sales by Authors"
29+
assert isinstance(chart, BarChart)
30+
assert chart.type == ChartType.BAR
31+
assert chart.title == "Book Sales by Authors"
3232

33-
assert graph.x_label == "Authors"
34-
assert graph.y_label == "Number of Books Sold"
33+
assert chart.x_label == "Authors"
34+
assert chart.y_label == "Number of Books Sold"
3535

36-
assert graph.x_unit is None
37-
assert graph.y_unit is None
36+
assert chart.x_unit is None
37+
assert chart.y_unit is None
3838

39-
bars = graph.elements
39+
bars = chart.elements
4040
assert len(bars) == 4
4141

4242
assert [bar.value for bar in bars] == [100, 200, 300, 400]

0 commit comments

Comments
 (0)