Skip to content

Commit

Permalink
Line plot: Customizable labels
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Jun 18, 2020
1 parent ba9011a commit 206049f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
95 changes: 91 additions & 4 deletions Orange/widgets/visualize/owlineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pyqtgraph.functions import mkPen
from pyqtgraph.graphicsItems.ViewBox import ViewBox

from orangewidget.utils.visual_settings_dlg import VisualSettingsDialog

from Orange.data import Table, DiscreteVariable
from Orange.data.sql.table import SqlTable
from Orange.statistics.util import countnans, nanmean, nanmin, nanmax, nanstd
Expand All @@ -27,6 +29,9 @@
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.utils.state_summary import format_summary_details
from Orange.widgets.visualize.owdistributions import LegendItem
from Orange.widgets.visualize.utils.customizableplot import Updater, \
BaseParameterSetter as Setter
from Orange.widgets.visualize.utils.plotutils import AxisItem
from Orange.widgets.widget import OWWidget, Input, Output, Msg


Expand Down Expand Up @@ -92,7 +97,7 @@ class LinePlotStyle:
MEAN_DARK_FACTOR = 110


class LinePlotAxisItem(pg.AxisItem):
class BottomAxisItem(AxisItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ticks = {}
Expand Down Expand Up @@ -183,12 +188,89 @@ def reset(self):
self._graph_state = SELECT


class LinePlotGraph(pg.PlotWidget):
class ParameterSetter(Setter):
initial_settings = {
Setter.LABELS_BOX: {
Setter.FONT_FAMILY_LABEL: Updater.FONT_FAMILY_SETTING,
Setter.TITLE_LABEL: Updater.FONT_SETTING,
Setter.AXIS_TITLE_LABEL: Updater.FONT_SETTING,
Setter.AXIS_TICKS_LABEL: Updater.FONT_SETTING,
Setter.LEGEND_LABEL: Updater.FONT_SETTING,
},
Setter.ANNOT_BOX: {
Setter.TITLE_LABEL: {Setter.TITLE_LABEL: ("", "")},
Setter.X_AXIS_LABEL: {Setter.TITLE_LABEL: ("", "")},
Setter.Y_AXIS_LABEL: {Setter.TITLE_LABEL: ("", "")},
}
}

def __init__(self):
def update_font_family(**settings):
for label, setter in self.setters[self.LABELS_BOX].items():
if label != self.FONT_FAMILY_LABEL:
setter(**settings)

def update_title(**settings):
Updater.update_plot_title_font(self.title_item, **settings)

def update_axes_titles(**settings):
Updater.update_axes_titles_font(self.axis_items, **settings)

def update_axes_ticks(**settings):
Updater.update_axes_ticks_font(self.axis_items, **settings)

def update_legend(**settings):
self.legend_settings.update(**settings)
Updater.update_legend_font(self.legend_items, **settings)

def update_title_text(**settings):
Updater.update_plot_title_text(
self.title_item, settings[self.TITLE_LABEL])

def update_axis(axis, **settings):
Updater.update_axis_title_text(
self.getAxis(axis), settings[self.TITLE_LABEL])

self.legend_settings = {}
self.setters = {
self.LABELS_BOX: {
self.FONT_FAMILY_LABEL: update_font_family,
self.TITLE_LABEL: update_title,
self.AXIS_TITLE_LABEL: update_axes_titles,
self.AXIS_TICKS_LABEL: update_axes_ticks,
self.LEGEND_LABEL: update_legend,
},
self.ANNOT_BOX: {
self.TITLE_LABEL: update_title_text,
self.X_AXIS_LABEL: lambda **kw: update_axis("bottom", **kw),
self.Y_AXIS_LABEL: lambda **kw: update_axis("left", **kw),
}
}

@property
def title_item(self):
return self.getPlotItem().titleLabel

@property
def axis_items(self):
return [value["item"] for value in self.getPlotItem().axes.values()]

@property
def legend_items(self):
return self.legend.items


# Customizable plot widget
class LinePlotGraph(pg.PlotWidget, ParameterSetter):
def __init__(self, parent):
self.bottom_axis = LinePlotAxisItem(orientation="bottom")
self.bottom_axis = BottomAxisItem(orientation="bottom")
self.bottom_axis.setLabel("")
left_axis = AxisItem(orientation="left")
left_axis.setLabel("")
super().__init__(parent, viewBox=LinePlotViewBox(),
background="w", enableMenu=False,
axisItems={"bottom": self.bottom_axis})
axisItems={"bottom": self.bottom_axis,
"left": left_axis})
self.view_box = self.getViewBox()
self.selection = set()
self.legend = self._create_legend(((1, 0), (1, 0)))
Expand All @@ -211,6 +293,7 @@ def update_legend(self, variable):
dots = pg.ScatterPlotItem(pen=c, brush=c, size=10, shape="s")
self.legend.addItem(dots, escape(name))
self.legend.show()
Updater.update_legend_font(self.legend_items, **self.legend_settings)

def select(self, indices):
keys = QApplication.keyboardModifiers()
Expand Down Expand Up @@ -463,6 +546,7 @@ def __init__(self, parent=None):
self.graph_variables = []
self.setup_gui()

VisualSettingsDialog(self, LinePlotGraph.initial_settings)
self.graph.view_box.selection_changed.connect(self.selection_changed)
self.enable_selection.connect(self.graph.view_box.enable_selection)

Expand Down Expand Up @@ -771,6 +855,9 @@ def clear(self):
def __in(obj, collection):
return collection is not None and obj in collection

def set_visual_settings(self, *args):
self.graph.set_parameter(*args)


if __name__ == "__main__":
data = Table("brown-selected")
Expand Down
6 changes: 3 additions & 3 deletions Orange/widgets/visualize/tests/test_owlineplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
import scipy.sparse as sp

from AnyQt.QtCore import Qt
from AnyQt.QtCore import Qt, QPointF

from pyqtgraph import PlotCurveItem
from pyqtgraph.Point import Point
Expand Down Expand Up @@ -152,8 +152,8 @@ def test_selection_line(self):

# set view-dependent click coordinates
vb = self.widget.graph.view_box
event.buttonDownPos.return_value = vb.mapFromView(Point(2.49, 5.79))
event.pos.return_value = vb.mapFromView(Point(2.99, 4.69))
event.buttonDownPos.return_value = QPointF(2.38, 4.84)
event.pos.return_value = QPointF(3.58, 4.76)

self.widget.graph.view_box.mouseDragEvent(event)
line = self.widget.graph.view_box.selection_line
Expand Down

0 comments on commit 206049f

Please sign in to comment.