Skip to content

Commit

Permalink
feat: implement functionality required for plotly multiple y axes sub…
Browse files Browse the repository at this point in the history
…plots demo

PR: #13
  • Loading branch information
slaclau committed Nov 6, 2024
1 parent a60547c commit 39e9504
Showing 1 changed file with 93 additions and 15 deletions.
108 changes: 93 additions & 15 deletions src/plotly_gtk/_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,76 @@ def _draw_axis(self, context, width, height, axis):
if "linecolor" not in self.layout[axis]:
return
context.set_source_rgb(*parse_color(self.layout[axis]["linecolor"]))
# TODO: finish this
axis_letter = axis[0 : axis.find("axis")] # pylint: disable=unused-variable
domain = self.layout[axis]["domain"] # pylint: disable=unused-variable
position = self.layout[axis]["position"] # pylint: disable=unused-variable

print(f"{axis}: {domain}")

context.line_to(
self.layout["_margin"]["l"], height - self.layout["_margin"]["b"]
axis_letter = axis[0 : axis.find("axis")]
overlaying_axis = (
(
self.layout[axis]["overlaying"][0]
+ "axis"
+ self.layout[axis]["overlaying"][1:]
)
if "overlaying" in self.layout[axis]
else ""
)
context.line_to(
width - self.layout["_margin"]["r"],
height - self.layout["_margin"]["b"],
anchor_axis = (
"free"
if self.layout[axis]["anchor"] == "free"
else (
self.layout[axis]["anchor"][0]
+ "axis"
+ self.layout[axis]["anchor"][1:]
)
)
domain = (
self.layout[axis]["domain"]
if "overlaying" not in self.layout[axis]
else self.layout[overlaying_axis]["domain"]
)
position = (
self.layout[axis]["position"]
if anchor_axis == "free"
else (
self.layout[anchor_axis]["domain"][0]
if self.layout[axis]["side"] == "left"
or self.layout[axis]["side"] == "bottom"
else self.layout[anchor_axis]["domain"][-1]
)
)

if axis_letter == "x":
context.move_to(
self.layout["_margin"]["l"]
+ domain[0]
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"]),
self.layout["_margin"]["t"]
+ (1 - position)
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"]),
)
context.line_to(
self.layout["_margin"]["l"]
+ domain[-1]
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"]),
self.layout["_margin"]["t"]
+ (1 - position)
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"]),
)
elif axis_letter == "y":
context.move_to(
self.layout["_margin"]["l"]
+ position
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"]),
self.layout["_margin"]["t"]
+ (1 - domain[0])
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"]),
)
context.line_to(
self.layout["_margin"]["l"]
+ position
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"]),
self.layout["_margin"]["t"]
+ (1 - domain[-1])
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"]),
)
context.stroke()

def _calc_pos(
Expand Down Expand Up @@ -283,14 +339,36 @@ def _calc_pos(
x_pos = []
y_pos = []

x_overlaying_axis = (
(xaxis["overlaying"][0] + "axis" + xaxis["overlaying"][1:])
if "overlaying" in xaxis
else ""
)
y_overlaying_axis = (
(yaxis["overlaying"][0] + "axis" + yaxis["overlaying"][1:])
if "overlaying" in yaxis
else ""
)

xdomain = (
xaxis["domain"]
if "overlaying" not in xaxis
else self.layout[x_overlaying_axis]["domain"]
)
ydomain = (
yaxis["domain"]
if "overlaying" not in yaxis
else self.layout[y_overlaying_axis]["domain"]
)

if xaxis is not None:
xaxis_start = (
xaxis["domain"][0]
xdomain[0]
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"])
+ self.layout["_margin"]["l"]
)
xaxis_end = (
xaxis["domain"][-1]
xdomain[-1]
* (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"])
+ self.layout["_margin"]["l"]
)
Expand All @@ -303,13 +381,13 @@ def _calc_pos(

if yaxis is not None:
yaxis_start = (
-(yaxis["domain"][0])
-(ydomain[0])
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"])
+ height
- self.layout["_margin"]["b"]
)
yaxis_end = (
-(yaxis["domain"][-1])
-(ydomain[-1])
* (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"])
+ height
- self.layout["_margin"]["b"]
Expand Down

0 comments on commit 39e9504

Please sign in to comment.