diff --git a/src/plotly_gtk/_chart.py b/src/plotly_gtk/_chart.py index 67fe401..5e04c5e 100644 --- a/src/plotly_gtk/_chart.py +++ b/src/plotly_gtk/_chart.py @@ -12,7 +12,7 @@ PangoCairo, ) -DEBUG = False +DEBUG = True class _PlotlyGtk(Gtk.DrawingArea): @@ -237,6 +237,8 @@ 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"])) + if DEBUG: + context.set_source_rgb(*parse_color("green")) axis_letter = axis[0 : axis.find("axis")] overlaying_axis = ( @@ -250,7 +252,8 @@ def _draw_axis(self, context, width, height, axis): ) anchor_axis = ( "free" - if self.layout[axis]["anchor"] == "free" + if "anchor" not in self.layout[axis] + or self.layout[axis]["anchor"] == "free" else ( self.layout[axis]["anchor"][0] + "axis" @@ -264,7 +267,7 @@ def _draw_axis(self, context, width, height, axis): ) position = ( self.layout[axis]["position"] - if anchor_axis == "free" + if "anchor" not in self.layout[axis] or anchor_axis == "free" else ( self.layout[anchor_axis]["domain"][0] if self.layout[axis]["side"] == "left" @@ -339,29 +342,17 @@ 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: + x_overlaying_axis = ( + (xaxis["overlaying"][0] + "axis" + xaxis["overlaying"][1:]) + if "overlaying" in xaxis + else "" + ) + xdomain = ( + xaxis["domain"] + if "overlaying" not in xaxis + else self.layout[x_overlaying_axis]["domain"] + ) xaxis_start = ( xdomain[0] * (width - self.layout["_margin"]["l"] - self.layout["_margin"]["r"]) @@ -380,6 +371,16 @@ def _calc_pos( ) + xaxis_start if yaxis is not None: + y_overlaying_axis = ( + (yaxis["overlaying"][0] + "axis" + yaxis["overlaying"][1:]) + if "overlaying" in yaxis + else "" + ) + ydomain = ( + yaxis["domain"] + if "overlaying" not in yaxis + else self.layout[y_overlaying_axis]["domain"] + ) yaxis_start = ( -(ydomain[0]) * (height - self.layout["_margin"]["t"] - self.layout["_margin"]["b"]) diff --git a/src/plotly_gtk/chart.py b/src/plotly_gtk/chart.py index 4b6be7f..a320fee 100644 --- a/src/plotly_gtk/chart.py +++ b/src/plotly_gtk/chart.py @@ -100,7 +100,6 @@ def _update_ranges(self): if f"{axis_letter}axis" in plot and plot[f"{axis_letter}axis"] == axis.replace("axis", "") and ("visible" not in plot or plot["visible"]) - and ("_visible" not in plot or plot["_visible"]) ] hidden_plots_on_axis = [ plot @@ -464,6 +463,7 @@ def _update_layout(self): ), margin=dict(autoexpand=True, t=100, l=80, r=80, b=80), xaxis=dict( + anchor="y", automargin=True, autorange=True, autotickangles=[0, 30, 90], @@ -508,6 +508,7 @@ def _update_layout(self): zerolinewidth=1, ), yaxis=dict( + anchor="x", automargin=True, autorange=True, autotickangles=[0, 30, 90], diff --git a/src/plotly_gtk/demo.py b/src/plotly_gtk/demo.py index bae6228..dba66eb 100644 --- a/src/plotly_gtk/demo.py +++ b/src/plotly_gtk/demo.py @@ -24,7 +24,7 @@ multiple_axes_demos = [ "two_y_axes", "multiple_y_axes_subplots", - # "multiple_axes", + "multiple_axes", # "autoshift", # "shift_by_pixels", # "syncticks", @@ -195,6 +195,64 @@ def _get_multiple_axes_test_figure(reference): col=2, secondary_y=True, ) + elif reference == "multiple_axes": + fig = go.Figure() + + fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="yaxis1 data")) + + fig.add_trace( + go.Scatter(x=[2, 3, 4], y=[40, 50, 60], name="yaxis2 data", yaxis="y2") + ) + + fig.add_trace( + go.Scatter( + x=[4, 5, 6], y=[40000, 50000, 60000], name="yaxis3 data", yaxis="y3" + ) + ) + + fig.add_trace( + go.Scatter( + x=[5, 6, 7], y=[400000, 500000, 600000], name="yaxis4 data", yaxis="y4" + ) + ) + + # Create axis objects + fig.update_layout( + xaxis=dict(domain=[0.3, 0.7]), + yaxis=dict( + title=dict(text="yaxis title", font=dict(color="#1f77b4")), + tickfont=dict(color="#1f77b4"), + ), + yaxis2=dict( + title=dict(text="yaxis2 title", font=dict(color="#ff7f0e")), + tickfont=dict(color="#ff7f0e"), + anchor="free", + overlaying="y", + side="left", + position=0.15, + ), + yaxis3=dict( + title=dict(text="yaxis3 title", font=dict(color="#d62728")), + tickfont=dict(color="#d62728"), + anchor="x", + overlaying="y", + side="right", + ), + yaxis4=dict( + title=dict(text="yaxis4 title", font=dict(color="#9467bd")), + tickfont=dict(color="#9467bd"), + anchor="free", + overlaying="y", + side="right", + position=0.85, + ), + ) + + # Update layout properties + fig.update_layout( + title_text="multiple y-axes example", + width=800, + ) else: return return fig @@ -205,7 +263,7 @@ def test(app): paned = Gtk.Paned() window.set_content(paned) - fig = get_test_figure("multiple_y_axes_subplots") + fig = get_test_figure("multiple_axes") print(fig) # print(fig["layout"]["template"])