Skip to content

Commit b5fc1c6

Browse files
authored
Merge pull request #362 from Dessia-tech/fix/pyshape
fix(pyshape): add Shape class in Python for handling tooltips on shapes
2 parents f2f9213 + 8f6b0ac commit b5fc1c6

File tree

6 files changed

+48
-39
lines changed

6 files changed

+48
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [0.22.2]
99
### Fix
1010
- Local import
11+
- Add tooltip on any shape with Python with Shape object
1112

1213
## [0.22.0]
1314
### Add

code_pylint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'trailing-whitespace': 11,
3636
'empty-docstring': 7,
3737
'missing-module-docstring': 4,
38-
'too-many-arguments': 22,
38+
'too-many-arguments': 23,
3939
'too-few-public-methods': 5,
4040
'unnecessary-comprehension': 5,
4141
'no-value-for-parameter': 2,

plot_data/core.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,19 @@ def plot(self, filepath: str = None, **kwargs):
155155
class ReferencedObject(PlotDataObject):
156156
""" PlotData object with reference_path. """
157157

158-
def __init__(self, type_: str, reference_path: str = "#", tooltip: str = None, name: str = ""):
158+
def __init__(self, type_: str, reference_path: str = "#", name: str = ""):
159159
self.reference_path = reference_path
160-
self.tooltip = tooltip
161160
super().__init__(type_=type_, name=name)
162161

163162

163+
class Shape(ReferencedObject):
164+
""" Shape object. """
165+
166+
def __init__(self, type_: str, reference_path: str = "#", tooltip: str = None, name: str = ""):
167+
self.tooltip = tooltip
168+
super().__init__(type_=type_, reference_path=reference_path, name=name)
169+
170+
164171
class Sample(ReferencedObject):
165172
""" Graph Point. """
166173

@@ -428,7 +435,7 @@ def __init__(self, point_color: str, point_index: List[int], name: str = ''):
428435
PlotDataObject.__init__(self, type_=None, name=name)
429436

430437

431-
class Text(ReferencedObject):
438+
class Text(Shape):
432439
"""
433440
A class for displaying texts on canvas. Text is a primitive and can be instantiated by PrimitiveGroup.
434441
@@ -471,7 +478,7 @@ def mpl_plot(self, ax=None, color='k', alpha=1., **kwargs):
471478
return ax
472479

473480

474-
class Line2D(ReferencedObject):
481+
class Line2D(Shape):
475482
"""
476483
An infinite line. Line2D is a primitive and can be instantiated by PrimitiveGroups.
477484
@@ -507,7 +514,7 @@ def mpl_plot(self, ax=None, edge_style=None, **kwargs):
507514
return ax
508515

509516

510-
class LineSegment2D(ReferencedObject):
517+
class LineSegment2D(Shape):
511518
"""
512519
A line segment. This is a primitive that can be called by PrimitiveGroup.
513520
@@ -558,7 +565,7 @@ def mpl_plot(self, ax=None, edge_style=None, **kwargs):
558565
return ax
559566

560567

561-
class Wire(ReferencedObject):
568+
class Wire(Shape):
562569
"""
563570
A set of connected lines. It also provides highlighting feature.
564571
@@ -589,7 +596,7 @@ def mpl_plot(self, ax=None, **kwargs):
589596
return ax
590597

591598

592-
class Circle2D(ReferencedObject):
599+
class Circle2D(Shape):
593600
"""
594601
A circle. It is a primitive and can be instantiated by PrimitiveGroup.
595602
@@ -644,7 +651,7 @@ def mpl_plot(self, ax=None, **kwargs):
644651
return ax
645652

646653

647-
class Rectangle(ReferencedObject):
654+
class Rectangle(Shape):
648655
""" Class to draw a rectangle. """
649656

650657
def __init__(self, x_coord: float, y_coord: float, width: float, height: float, edge_style: EdgeStyle = None,
@@ -697,7 +704,7 @@ def __init__(self, x_coord: float, y_coord: float, width: float, height: float,
697704
self.radius = radius
698705

699706

700-
class Point2D(ReferencedObject):
707+
class Point2D(Shape):
701708
"""
702709
A class for instantiating a point.
703710
@@ -709,11 +716,12 @@ class Point2D(ReferencedObject):
709716
:type point_style: PointStyle
710717
"""
711718

712-
def __init__(self, cx: float, cy: float, point_style: PointStyle = None, reference_path: str = "#", name: str = ''):
719+
def __init__(self, cx: float, cy: float, point_style: PointStyle = None, reference_path: str = "#",
720+
tooltip: str = None, name: str = ''):
713721
self.cx = cx
714722
self.cy = cy
715723
self.point_style = point_style
716-
super().__init__(type_='point', reference_path=reference_path, name=name)
724+
super().__init__(type_='point', reference_path=reference_path, tooltip=tooltip, name=name)
717725

718726
def bounding_box(self):
719727
""" Get 2D bounding box of current Circle2D. """
@@ -1113,7 +1121,7 @@ def _build_multiplot(self):
11131121
for row in sample_attributes for col in sample_attributes]
11141122

11151123

1116-
class Arc2D(ReferencedObject):
1124+
class Arc2D(Shape):
11171125
"""
11181126
A class for drawing arcs. Arc2D is a primitive and can be instantiated by PrimitiveGroup. By default,
11191127
the arc is drawn anticlockwise.
@@ -1181,7 +1189,7 @@ def mpl_plot(self, ax=None, **kwargs):
11811189
return ax
11821190

11831191

1184-
class Contour2D(ReferencedObject):
1192+
class Contour2D(Shape):
11851193
"""
11861194
A Contour2D is a closed polygon that is formed by multiple primitives.
11871195

script/test_objects/primitive_group_test.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@
2626
# Symmetric shapes
2727
points_text = plot_data.Text(comment="Points: ", text_scaling=True, position_x=0, position_y=21.2, text_style=title_style)
2828
round_rect = plot_data.RoundRectangle(0, 0, 21, 21, 1, edge_style=edge_style_dashed, surface_style=surface_style_empty)
29-
cross = plot_data.Point2D(3, 3, plot_data.PointStyle(color_stroke=colors.ORANGE, stroke_width=5, shape="cross", size = 20))
30-
circle = plot_data.Point2D(8, 3, plot_data.PointStyle(color_fill=colors.ORANGE, color_stroke=colors.BLACK, stroke_width=2, shape="circle", size = 20))
31-
square = plot_data.Point2D(13, 3, plot_data.PointStyle(color_fill=colors.PINK, color_stroke=colors.PURPLE, stroke_width=3, shape="square", size = 20))
32-
mark = plot_data.Point2D(18, 3, plot_data.PointStyle(color_stroke=colors.BLUE, stroke_width=3, shape="mark", size = 20))
29+
cross = plot_data.Point2D(3, 3, plot_data.PointStyle(color_stroke=colors.ORANGE, stroke_width=5, shape="cross", size = 20), tooltip="cross")
30+
circle = plot_data.Point2D(8, 3, plot_data.PointStyle(color_fill=colors.ORANGE, color_stroke=colors.BLACK, stroke_width=2, shape="circle", size = 20), tooltip="circle")
31+
square = plot_data.Point2D(13, 3, plot_data.PointStyle(color_fill=colors.PINK, color_stroke=colors.PURPLE, stroke_width=3, shape="square", size = 20), tooltip="square")
32+
mark = plot_data.Point2D(18, 3, plot_data.PointStyle(color_stroke=colors.BLUE, stroke_width=3, shape="mark", size = 20), tooltip="mark")
3333

3434
## Triangles
35-
up_triangle = plot_data.Point2D(3, 8, plot_data.PointStyle(color_fill=colors.RED, color_stroke=colors.DARK_PURPLE, stroke_width=4, shape="triangle", orientation="up", size = 20))
36-
down_triangle = plot_data.Point2D(8, 8, plot_data.PointStyle(color_fill=colors.YELLOW, color_stroke=colors.EMPIRE_YELLOW, stroke_width=4, shape="triangle", orientation="down", size = 20))
37-
left_triangle = plot_data.Point2D(13, 8, plot_data.PointStyle(color_fill=colors.GREEN, color_stroke=colors.EMERALD, stroke_width=4, shape="triangle", orientation="left", size = 20))
38-
right_triangle = plot_data.Point2D(18, 8, plot_data.PointStyle(color_fill=colors.BLUE, color_stroke=colors.DARK_BLUE, stroke_width=4, shape="triangle", orientation="right", size = 20))
35+
up_triangle = plot_data.Point2D(3, 8, plot_data.PointStyle(color_fill=colors.RED, color_stroke=colors.DARK_PURPLE, stroke_width=4, shape="triangle", orientation="up", size = 20), tooltip="triangle_up")
36+
down_triangle = plot_data.Point2D(8, 8, plot_data.PointStyle(color_fill=colors.YELLOW, color_stroke=colors.EMPIRE_YELLOW, stroke_width=4, shape="triangle", orientation="down", size = 20), tooltip="triangle_down")
37+
left_triangle = plot_data.Point2D(13, 8, plot_data.PointStyle(color_fill=colors.GREEN, color_stroke=colors.EMERALD, stroke_width=4, shape="triangle", orientation="left", size = 20), tooltip="triangle_left")
38+
right_triangle = plot_data.Point2D(18, 8, plot_data.PointStyle(color_fill=colors.BLUE, color_stroke=colors.DARK_BLUE, stroke_width=4, shape="triangle", orientation="right", size = 20), tooltip="triangle_right")
3939

4040
## Half Lines
41-
up_halfline = plot_data.Point2D(3, 13, plot_data.PointStyle(color_stroke=colors.PINK, stroke_width=4, shape="halfline", orientation="up", size = 20))
42-
down_halfline = plot_data.Point2D(8, 13, plot_data.PointStyle(color_stroke=colors.PURPLE, stroke_width=4, shape="halfline", orientation="down", size = 20))
43-
left_halfline = plot_data.Point2D(13, 13, plot_data.PointStyle(color_stroke=colors.ORANGE, stroke_width=4, shape="halfline", orientation="left", size = 20))
44-
right_halfline = plot_data.Point2D(18, 13, plot_data.PointStyle(color_stroke=colors.BLACK, stroke_width=4, shape="halfline", orientation="right", size = 20))
41+
up_halfline = plot_data.Point2D(3, 13, plot_data.PointStyle(color_stroke=colors.PINK, stroke_width=4, shape="halfline", orientation="up", size = 20), tooltip="halfline_up")
42+
down_halfline = plot_data.Point2D(8, 13, plot_data.PointStyle(color_stroke=colors.PURPLE, stroke_width=4, shape="halfline", orientation="down", size = 20), tooltip="halfline_down")
43+
left_halfline = plot_data.Point2D(13, 13, plot_data.PointStyle(color_stroke=colors.ORANGE, stroke_width=4, shape="halfline", orientation="left", size = 20), tooltip="halfline_left")
44+
right_halfline = plot_data.Point2D(18, 13, plot_data.PointStyle(color_stroke=colors.BLACK, stroke_width=4, shape="halfline", orientation="right", size = 20), tooltip="halfline_right")
4545

4646
## Lines
47-
vline = plot_data.Point2D(3, 18, plot_data.PointStyle(color_stroke=colors.LIGHTBLUE, stroke_width=2, shape="line", orientation="vertical", size = 20))
48-
hline = plot_data.Point2D(8, 18, plot_data.PointStyle(color_stroke=colors.LIGHTPURPLE, stroke_width=2, shape="line", orientation="horizontal", size = 20))
49-
slash = plot_data.Point2D(13, 18, plot_data.PointStyle(color_stroke=colors.ANGEL_BLUE, stroke_width=2, shape="line", orientation="slash", size = 20))
50-
backslash = plot_data.Point2D(18, 18, plot_data.PointStyle(color_stroke=colors.BRIGHT_LIME_GREEN, stroke_width=2, shape="line", orientation="backslash", size = 20))
47+
vline = plot_data.Point2D(3, 18, plot_data.PointStyle(color_stroke=colors.LIGHTBLUE, stroke_width=2, shape="line", orientation="vertical", size = 20), tooltip="vertical")
48+
hline = plot_data.Point2D(8, 18, plot_data.PointStyle(color_stroke=colors.LIGHTPURPLE, stroke_width=2, shape="line", orientation="horizontal", size = 20), tooltip="horizontal")
49+
slash = plot_data.Point2D(13, 18, plot_data.PointStyle(color_stroke=colors.ANGEL_BLUE, stroke_width=2, shape="line", orientation="slash", size = 20), tooltip="slash")
50+
backslash = plot_data.Point2D(18, 18, plot_data.PointStyle(color_stroke=colors.BRIGHT_LIME_GREEN, stroke_width=2, shape="line", orientation="backslash", size = 20), tooltip="backslash")
5151

5252
points = [cross, circle, square, mark,
5353
up_triangle, down_triangle, left_triangle, right_triangle,
@@ -56,17 +56,17 @@
5656

5757
# ============================================= SHAPES =================================================================
5858
# Lines
59-
line_2d = plot_data.Line2D(point1=[-10, 24.5], point2=[22, 24.5], edge_style=edge_style_purple)
60-
round_rect_shapes = plot_data.RoundRectangle(24, 0, 60, 37, 1, edge_style=edge_style_dashed, surface_style=surface_style_empty)
59+
line_2d = plot_data.Line2D(point1=[-10, 24.5], point2=[22, 24.5], edge_style=edge_style_purple, tooltip="line2d")
60+
round_rect_shapes = plot_data.RoundRectangle(24, 0, 60, 37, 1, edge_style=edge_style_dashed, surface_style=surface_style_empty, tooltip="round_rect")
6161
points_text_shapes = plot_data.Text(comment="Shapes: ", text_scaling=True, position_x=24, position_y=37.2, text_style=title_style)
6262

6363
# Arcs
6464
circle = plot_data.Circle2D(cx=31, cy=10.5, r=5, edge_style=edge_style_red, surface_style=surface_style_yellow, tooltip="It's a circle")
65-
arc = plot_data.Arc2D(cx=43, cy=10.5, r=5, start_angle=math.pi/4, end_angle=2*math.pi/3, edge_style=edge_style_red, clockwise=True)
66-
arc_anti = plot_data.Arc2D(cx=43, cy=10.5, r=5, start_angle=math.pi/4, end_angle=2*math.pi/3, edge_style=edge_style_blue, clockwise=False)
67-
line_segment_1 = plot_data.LineSegment2D(point1=[50, 1], point2=[53, 20], edge_style=edge_style_black)
68-
line_segment_2 = plot_data.LineSegment2D(point1=[75, 20], point2=[78, 1], edge_style=edge_style_black)
69-
rectangle = plot_data.Rectangle(57, 26, 25, 9, surface_style=surface_style_green, edge_style=edge_style_red)
65+
arc = plot_data.Arc2D(cx=43, cy=10.5, r=5, start_angle=math.pi/4, end_angle=2*math.pi/3, edge_style=edge_style_red, clockwise=True, tooltip="arc2d")
66+
arc_anti = plot_data.Arc2D(cx=43, cy=10.5, r=5, start_angle=math.pi/4, end_angle=2*math.pi/3, edge_style=edge_style_blue, clockwise=False, tooltip="arc2d_anticlockwise")
67+
line_segment_1 = plot_data.LineSegment2D(point1=[50, 1], point2=[53, 20], edge_style=edge_style_black, tooltip="linesegment")
68+
line_segment_2 = plot_data.LineSegment2D(point1=[75, 20], point2=[78, 1], edge_style=edge_style_black, tooltip="linesegment")
69+
rectangle = plot_data.Rectangle(57, 26, 25, 9, surface_style=surface_style_green, edge_style=edge_style_red, tooltip="rectangle")
7070

7171
# Contours
7272
star_lines_closed = [plot_data.LineSegment2D([57, 1.5], [60, 8.5]),

src/primitives.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,6 @@ export class Point extends Shape {
668668
return marker
669669
}
670670

671-
protected updateTooltipOrigin(matrix: DOMMatrix): void { this.tooltipOrigin = this.center.copy() }
672-
673671
get markerOrientation(): string { return this._markerOrientation };
674672

675673
set markerOrientation(value: string) { this._markerOrientation = value };

src/shapes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ export class ScatterPoint extends Point {
665665
this.update();
666666
}
667667

668+
protected updateTooltipOrigin(matrix: DOMMatrix): void { this.tooltipOrigin = this.center.copy() }
669+
668670
public updateTooltipMap() { this._tooltipMap = new Map<string, any>([["Number", this.values.length], ["X mean", this.mean.x], ["Y mean", this.mean.y],]) };
669671

670672
public updateTooltip(tooltipAttributes: string[], features: Map<string, number[]>, axes: Axis[], xName: string, yName: string) {

0 commit comments

Comments
 (0)