g3w-admin-qplotly 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- g3w_admin_qplotly-0.0.1.dist-info/METADATA +52 -0
- g3w_admin_qplotly-0.0.1.dist-info/RECORD +139 -0
- g3w_admin_qplotly-0.0.1.dist-info/WHEEL +5 -0
- g3w_admin_qplotly-0.0.1.dist-info/licenses/LICENSE +373 -0
- g3w_admin_qplotly-0.0.1.dist-info/top_level.txt +1 -0
- qplotly/__init__.py +1 -0
- qplotly/admin.py +34 -0
- qplotly/api/__init__.py +0 -0
- qplotly/api/plots/__init__.py +0 -0
- qplotly/api/plots/views.py +211 -0
- qplotly/api/widgets/__init__.py +0 -0
- qplotly/api/widgets/permissions.py +79 -0
- qplotly/api/widgets/serializers.py +109 -0
- qplotly/api/widgets/views.py +287 -0
- qplotly/apiurls.py +132 -0
- qplotly/apps.py +11 -0
- qplotly/locale/bg/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/bg/LC_MESSAGES/django.po +162 -0
- qplotly/locale/de/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/de/LC_MESSAGES/django.po +126 -0
- qplotly/locale/de/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/de/LC_MESSAGES/djangojs.po +221 -0
- qplotly/locale/es/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/es/LC_MESSAGES/django.po +151 -0
- qplotly/locale/es/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/es/LC_MESSAGES/djangojs.po +232 -0
- qplotly/locale/fi/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/fi/LC_MESSAGES/django.po +126 -0
- qplotly/locale/fi/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/fi/LC_MESSAGES/djangojs.po +222 -0
- qplotly/locale/fr/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/fr/LC_MESSAGES/django.po +160 -0
- qplotly/locale/fr/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/fr/LC_MESSAGES/djangojs.po +222 -0
- qplotly/locale/it/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/it/LC_MESSAGES/django.po +163 -0
- qplotly/locale/it/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/it/LC_MESSAGES/djangojs.po +220 -0
- qplotly/locale/pl/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/pl/LC_MESSAGES/django.po +129 -0
- qplotly/locale/pl/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/pl/LC_MESSAGES/djangojs.po +217 -0
- qplotly/locale/pt/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/pt/LC_MESSAGES/django.po +160 -0
- qplotly/locale/ro/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/ro/LC_MESSAGES/django.po +128 -0
- qplotly/locale/ro/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/ro/LC_MESSAGES/djangojs.po +222 -0
- qplotly/locale/se/LC_MESSAGES/django.mo +0 -0
- qplotly/locale/se/LC_MESSAGES/django.po +126 -0
- qplotly/locale/se/LC_MESSAGES/djangojs.mo +0 -0
- qplotly/locale/se/LC_MESSAGES/djangojs.po +222 -0
- qplotly/migrations/0001_initial.py +26 -0
- qplotly/migrations/0002_qplotlywidget_type.py +18 -0
- qplotly/migrations/0003_auto_20200925_0838.py +18 -0
- qplotly/migrations/0004_auto_20200925_1535.py +25 -0
- qplotly/migrations/0005_qplotlywidget_show_on_start_client.py +18 -0
- qplotly/migrations/0006_alter_qplotlywidget_options_qplotlywidget_order.py +23 -0
- qplotly/migrations/0007_alter_qplotlywidget_options_and_more.py +22 -0
- qplotly/migrations/0008_alter_qplotlywidget_order.py +18 -0
- qplotly/migrations/0009_qplotlywidget_show_in_sidebar.py +18 -0
- qplotly/migrations/0010_remove_qplotlywidget_show_in_sidebar_and_more.py +22 -0
- qplotly/migrations/0011_rename_show_poistion_qplotlywidget_show_position.py +18 -0
- qplotly/migrations/0012_qplotlywidgetrelation_qplotlywidget_related_widgets.py +34 -0
- qplotly/migrations/0013_alter_qplotlywidgetrelation_source_and_more.py +24 -0
- qplotly/migrations/0014_alter_qplotlywidgetrelation_source_and_more.py +24 -0
- qplotly/migrations/0015_qplotlywidgetrelation_project.py +34 -0
- qplotly/migrations/0016_remove_qplotlywidget_project.py +17 -0
- qplotly/migrations/__init__.py +0 -0
- qplotly/models.py +142 -0
- qplotly/receivers.py +356 -0
- qplotly/requirements.txt +1 -0
- qplotly/server_filters/__init__.py +1 -0
- qplotly/server_filters/relations.py +32 -0
- qplotly/static/qplotly/icons/2dhistogram.svg +2048 -0
- qplotly/static/qplotly/icons/barplot.svg +2048 -0
- qplotly/static/qplotly/icons/boxplot.svg +2048 -0
- qplotly/static/qplotly/icons/contour.svg +2056 -0
- qplotly/static/qplotly/icons/histogram.svg +2048 -0
- qplotly/static/qplotly/icons/pie.svg +2048 -0
- qplotly/static/qplotly/icons/polar.svg +2055 -0
- qplotly/static/qplotly/icons/scatterplot.svg +2048 -0
- qplotly/static/qplotly/icons/scatterternary.svg +2055 -0
- qplotly/static/qplotly/icons/violin.svg +2878 -0
- qplotly/static/qplotly/js/data-widget-qplotlyWidgetList.js +373 -0
- qplotly/static/qplotly/js/i18n/bg.js +19 -0
- qplotly/static/qplotly/js/i18n/de.js +19 -0
- qplotly/static/qplotly/js/i18n/en.js +19 -0
- qplotly/static/qplotly/js/i18n/fi.js +19 -0
- qplotly/static/qplotly/js/i18n/fr.js +19 -0
- qplotly/static/qplotly/js/i18n/index.js +19 -0
- qplotly/static/qplotly/js/i18n/it.js +19 -0
- qplotly/static/qplotly/js/i18n/ro.js +19 -0
- qplotly/static/qplotly/js/i18n/se.js +19 -0
- qplotly/static/qplotly/js/i18n/uk.js +19 -0
- qplotly/static/qplotly/js/plugin.js +927 -0
- qplotly/static/qplotly/js/sidebar.js +373 -0
- qplotly/static/qplotly/js/widget.js +9 -0
- qplotly/static/qplotly/plotly-1.52.2.min.js +7 -0
- qplotly/static/qplotly/polyfill.min.js +3 -0
- qplotly/templates/qplotly/include/plot_type_icon.html +52 -0
- qplotly/templates/qplotly/layer_action.html +13 -0
- qplotly/templates/qplotly/plots_list_order.html +68 -0
- qplotly/templates/qplotly/plots_list_order_action.html +14 -0
- qplotly/templatetags/__init__.py +0 -0
- qplotly/templatetags/qplotly_tags.py +28 -0
- qplotly/urls.py +52 -0
- qplotly/utils/__init__.py +13 -0
- qplotly/utils/models.py +86 -0
- qplotly/utils/qplotly_factory.py +699 -0
- qplotly/utils/qplotly_settings.py +37 -0
- qplotly/vendor/DataPlotly/__init__.py +0 -0
- qplotly/vendor/DataPlotly/core/__init__.py +0 -0
- qplotly/vendor/DataPlotly/core/plot_factory.py +843 -0
- qplotly/vendor/DataPlotly/core/plot_settings.py +322 -0
- qplotly/vendor/DataPlotly/core/plot_types/__init__.py +20 -0
- qplotly/vendor/DataPlotly/core/plot_types/bar_plot.py +83 -0
- qplotly/vendor/DataPlotly/core/plot_types/box.py +56 -0
- qplotly/vendor/DataPlotly/core/plot_types/contour.py +43 -0
- qplotly/vendor/DataPlotly/core/plot_types/histogram.py +66 -0
- qplotly/vendor/DataPlotly/core/plot_types/histogram2d.py +39 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/2dhistogram.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/barplot.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/boxplot.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/contour.svg +2056 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/histogram.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/pie.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/polar.svg +2055 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/scatterplot.svg +2048 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/scatterternary.svg +2055 -0
- qplotly/vendor/DataPlotly/core/plot_types/icons/violin.svg +2878 -0
- qplotly/vendor/DataPlotly/core/plot_types/pie.py +60 -0
- qplotly/vendor/DataPlotly/core/plot_types/plot_type.py +170 -0
- qplotly/vendor/DataPlotly/core/plot_types/polar.py +63 -0
- qplotly/vendor/DataPlotly/core/plot_types/scatter.py +74 -0
- qplotly/vendor/DataPlotly/core/plot_types/ternary.py +129 -0
- qplotly/vendor/DataPlotly/core/plot_types/violin.py +62 -0
- qplotly/vendor/__init__.py +0 -0
- qplotly/views.py +148 -0
|
@@ -0,0 +1,843 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Plot creation
|
|
3
|
+
|
|
4
|
+
.. note:: This program is free software; you can redistribute it and/or modify
|
|
5
|
+
it under the terms of the GNU General Public License as published by
|
|
6
|
+
the Free Software Foundation; either version 2 of the License, or
|
|
7
|
+
(at your option) any later version.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import tempfile
|
|
12
|
+
import os
|
|
13
|
+
import re
|
|
14
|
+
import plotly
|
|
15
|
+
import plotly.graph_objs as go
|
|
16
|
+
from copy import deepcopy
|
|
17
|
+
from plotly import subplots
|
|
18
|
+
|
|
19
|
+
from qgis.core import (
|
|
20
|
+
QgsProject,
|
|
21
|
+
QgsExpression,
|
|
22
|
+
QgsExpressionContext,
|
|
23
|
+
QgsExpressionContextUtils,
|
|
24
|
+
QgsFeatureRequest,
|
|
25
|
+
NULL,
|
|
26
|
+
QgsReferencedRectangle,
|
|
27
|
+
QgsCoordinateTransform,
|
|
28
|
+
QgsExpressionContextGenerator,
|
|
29
|
+
QgsReferencedGeometryBase,
|
|
30
|
+
QgsGeometry,
|
|
31
|
+
QgsCsException,
|
|
32
|
+
QgsSymbolLayerUtils
|
|
33
|
+
)
|
|
34
|
+
from qgis.PyQt.QtCore import (
|
|
35
|
+
QUrl,
|
|
36
|
+
QObject,
|
|
37
|
+
pyqtSignal,
|
|
38
|
+
QDate,
|
|
39
|
+
QDateTime,
|
|
40
|
+
Qt
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
from qgis.PyQt.QtGui import QColor
|
|
44
|
+
# Change for original for g3w-suite
|
|
45
|
+
from .plot_settings import PlotSettings
|
|
46
|
+
from .plot_types.plot_type import PlotType
|
|
47
|
+
from .plot_types import * # pylint: disable=W0401,W0614
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class FilterRegion(QgsReferencedGeometryBase): # pylint: disable=too-few-public-methods
|
|
51
|
+
"""
|
|
52
|
+
Filter region, consisting of a geometry with CRS
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, geometry, crs):
|
|
56
|
+
super().__init__(crs)
|
|
57
|
+
self.geometry = geometry
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class PlotFactory(QObject): # pylint:disable=too-many-instance-attributes
|
|
61
|
+
"""
|
|
62
|
+
Plot factory which creates Plotly Plot objects
|
|
63
|
+
|
|
64
|
+
Console usage:
|
|
65
|
+
|
|
66
|
+
.. code-block:: python
|
|
67
|
+
# create (and customize) plot settings, where
|
|
68
|
+
# plot_type (string): 'scatter'
|
|
69
|
+
# plot_properties (dictionary): {'x':[1,2,3], 'marker_width': 10}
|
|
70
|
+
# layout_properties (dictionary): {'legend'; True, 'title': 'Plot Title'}
|
|
71
|
+
settings = PlotSettings(plot_type, plot_properties, layout_properties)
|
|
72
|
+
# create the factory, which will create plots using the specified settings
|
|
73
|
+
factory = PlotFactory(settings)
|
|
74
|
+
# Use the factory to build a plot
|
|
75
|
+
output_file_path = factory.build_figure()
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
# create fixed class variables as paths for local javascript files
|
|
79
|
+
POLY_FILL_PATH = QUrl.fromLocalFile(
|
|
80
|
+
os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'jsscripts/polyfill.min.js'))).toString()
|
|
81
|
+
PLOTLY_PATH = QUrl.fromLocalFile(
|
|
82
|
+
os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'jsscripts/plotly-1.52.2.min.js'))).toString()
|
|
83
|
+
|
|
84
|
+
PLOT_TYPES = {
|
|
85
|
+
t.type_name(): t for t in PlotType.__subclasses__()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
plot_built = pyqtSignal()
|
|
89
|
+
|
|
90
|
+
# Add function to QDate and QDateTime classes that the PlotlyJSONEncoder expects from date objects
|
|
91
|
+
# NOTE: use the namespaced Qt.DateFormat.ISODate for PyQt6 (QGIS 4.2)
|
|
92
|
+
# compatibility; PyQt5 (QGIS 3.44) supports the namespaced form as well.
|
|
93
|
+
if not hasattr(QDate, 'isoformat'):
|
|
94
|
+
QDate.isoformat = lambda d: d.toString(Qt.DateFormat.ISODate)
|
|
95
|
+
if not hasattr(QDateTime, 'isoformat'):
|
|
96
|
+
QDateTime.isoformat = lambda d: d.toString(Qt.DateFormat.ISODate)
|
|
97
|
+
|
|
98
|
+
def __init__(self, settings: PlotSettings = None, context_generator: QgsExpressionContextGenerator = None,
|
|
99
|
+
visible_region: QgsReferencedRectangle = None, polygon_filter: FilterRegion = None):
|
|
100
|
+
super().__init__()
|
|
101
|
+
if settings is None:
|
|
102
|
+
self.settings = PlotSettings('scatter')
|
|
103
|
+
else:
|
|
104
|
+
self.settings = deepcopy(settings)
|
|
105
|
+
self.context_generator = context_generator
|
|
106
|
+
self.raw_plot = None
|
|
107
|
+
self.plot_path = None
|
|
108
|
+
self.selected_features_only = self.settings.properties['selected_features_only']
|
|
109
|
+
self.visible_region = visible_region
|
|
110
|
+
self.polygon_filter = polygon_filter
|
|
111
|
+
self.trace = None
|
|
112
|
+
self.layout = None
|
|
113
|
+
self.source_layer = QgsProject.instance().mapLayer(
|
|
114
|
+
self.settings.source_layer_id) if self.settings.source_layer_id else None
|
|
115
|
+
self.plot_path = os.path.join(
|
|
116
|
+
tempfile.gettempdir(),
|
|
117
|
+
f'temp_plot_name_{self.settings.dock_id}.html')
|
|
118
|
+
|
|
119
|
+
self.rebuild()
|
|
120
|
+
|
|
121
|
+
if self.source_layer:
|
|
122
|
+
self.source_layer.layerModified.connect(self.rebuild)
|
|
123
|
+
if self.selected_features_only:
|
|
124
|
+
self.source_layer.selectionChanged.connect(self.rebuild)
|
|
125
|
+
|
|
126
|
+
def fetch_values_from_layer(self): # pylint: disable=too-many-locals, too-many-branches, too-many-statements
|
|
127
|
+
"""
|
|
128
|
+
(Re)fetches plot values from the source layer.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
# Note: we keep things nice and efficient and only iterate a single time over the layer!
|
|
132
|
+
|
|
133
|
+
if not self.context_generator:
|
|
134
|
+
context = QgsExpressionContext()
|
|
135
|
+
context.appendScopes(
|
|
136
|
+
QgsExpressionContextUtils.globalProjectLayerScopes(self.source_layer))
|
|
137
|
+
else:
|
|
138
|
+
context = self.context_generator.createExpressionContext()
|
|
139
|
+
# add a new scope corresponding to the source layer -- this will potentially overwrite any other
|
|
140
|
+
# layer scopes which may be present in the context (e.g. from atlas layers), but we need to ensure
|
|
141
|
+
# that source layer fields and attributes are present in the context
|
|
142
|
+
context.appendScope(
|
|
143
|
+
self.source_layer.createExpressionContextScope())
|
|
144
|
+
|
|
145
|
+
self.settings.data_defined_properties.prepare(context)
|
|
146
|
+
|
|
147
|
+
self.fetch_layout_properties(context)
|
|
148
|
+
|
|
149
|
+
def add_source_field_or_expression(field_or_expression):
|
|
150
|
+
field_index = self.source_layer.fields().lookupField(field_or_expression)
|
|
151
|
+
if field_index == -1:
|
|
152
|
+
expression = QgsExpression(field_or_expression)
|
|
153
|
+
if not expression.hasParserError():
|
|
154
|
+
expression.prepare(context)
|
|
155
|
+
return expression, expression.needsGeometry(), expression.referencedColumns()
|
|
156
|
+
|
|
157
|
+
return None, False, {field_or_expression}
|
|
158
|
+
|
|
159
|
+
x_expression, x_needs_geom, x_attrs = add_source_field_or_expression(self.settings.properties['x_name']) if \
|
|
160
|
+
self.settings.properties[
|
|
161
|
+
'x_name'] else (None, False, set())
|
|
162
|
+
y_expression, y_needs_geom, y_attrs = add_source_field_or_expression(self.settings.properties['y_name']) if \
|
|
163
|
+
self.settings.properties[
|
|
164
|
+
'y_name'] else (None, False, set())
|
|
165
|
+
z_expression, z_needs_geom, z_attrs = add_source_field_or_expression(self.settings.properties['z_name']) if \
|
|
166
|
+
self.settings.properties[
|
|
167
|
+
'z_name'] else (None, False, set())
|
|
168
|
+
additional_info_expression, additional_needs_geom, additional_attrs = add_source_field_or_expression(
|
|
169
|
+
self.settings.layout['additional_info_expression']) if self.settings.layout[
|
|
170
|
+
'additional_info_expression'] else (None, False, set())
|
|
171
|
+
|
|
172
|
+
attrs = set().union(self.settings.data_defined_properties.referencedFields(),
|
|
173
|
+
x_attrs,
|
|
174
|
+
y_attrs,
|
|
175
|
+
z_attrs,
|
|
176
|
+
additional_attrs)
|
|
177
|
+
|
|
178
|
+
request = QgsFeatureRequest()
|
|
179
|
+
|
|
180
|
+
if self.settings.data_defined_properties.property(PlotSettings.PROPERTY_FILTER).isActive():
|
|
181
|
+
expression = self.settings.data_defined_properties.property(
|
|
182
|
+
PlotSettings.PROPERTY_FILTER).asExpression()
|
|
183
|
+
request.setFilterExpression(expression)
|
|
184
|
+
request.setExpressionContext(context)
|
|
185
|
+
|
|
186
|
+
request.setSubsetOfAttributes(attrs, self.source_layer.fields())
|
|
187
|
+
|
|
188
|
+
if not x_needs_geom and not y_needs_geom and not z_needs_geom and not additional_needs_geom and not self.settings.data_defined_properties.hasActiveProperties():
|
|
189
|
+
request.setFlags(QgsFeatureRequest.NoGeometry)
|
|
190
|
+
|
|
191
|
+
visible_geom_engine = None
|
|
192
|
+
if self.settings.properties.get('visible_features_only', False) and self.visible_region is not None:
|
|
193
|
+
ct = QgsCoordinateTransform(self.visible_region.crs(), self.source_layer.crs(),
|
|
194
|
+
QgsProject.instance().transformContext())
|
|
195
|
+
try:
|
|
196
|
+
rect = ct.transformBoundingBox(self.visible_region)
|
|
197
|
+
request.setFilterRect(rect)
|
|
198
|
+
except QgsCsException:
|
|
199
|
+
pass
|
|
200
|
+
elif self.settings.properties.get('visible_features_only', False) and self.polygon_filter is not None:
|
|
201
|
+
ct = QgsCoordinateTransform(self.polygon_filter.crs(), self.source_layer.crs(),
|
|
202
|
+
QgsProject.instance().transformContext())
|
|
203
|
+
try:
|
|
204
|
+
rect = ct.transformBoundingBox(
|
|
205
|
+
self.polygon_filter.geometry.boundingBox())
|
|
206
|
+
request.setFilterRect(rect)
|
|
207
|
+
g = self.polygon_filter.geometry
|
|
208
|
+
g.transform(ct)
|
|
209
|
+
|
|
210
|
+
visible_geom_engine = QgsGeometry.createGeometryEngine(
|
|
211
|
+
g.constGet())
|
|
212
|
+
visible_geom_engine.prepareGeometry()
|
|
213
|
+
except QgsCsException:
|
|
214
|
+
pass
|
|
215
|
+
|
|
216
|
+
if self.selected_features_only:
|
|
217
|
+
it = self.source_layer.getSelectedFeatures(request)
|
|
218
|
+
else:
|
|
219
|
+
it = self.source_layer.getFeatures(request)
|
|
220
|
+
|
|
221
|
+
# Some plot types don't draw individual glyphs for each feature, but aggregate them instead.
|
|
222
|
+
# In that case it doesn't make sense to evaluate expressions for settings like marker size or color for each
|
|
223
|
+
# feature. Instead, the evaluation should be executed only once for these settings.
|
|
224
|
+
aggregating = self.settings.plot_type in ['box', 'histogram']
|
|
225
|
+
executed = False
|
|
226
|
+
|
|
227
|
+
xx = []
|
|
228
|
+
yy = []
|
|
229
|
+
zz = []
|
|
230
|
+
additional_hover_text = []
|
|
231
|
+
marker_sizes = []
|
|
232
|
+
colors = []
|
|
233
|
+
stroke_colors = []
|
|
234
|
+
stroke_widths = []
|
|
235
|
+
for f in it:
|
|
236
|
+
if visible_geom_engine and not visible_geom_engine.intersects(f.geometry().constGet()):
|
|
237
|
+
continue
|
|
238
|
+
|
|
239
|
+
self.settings.feature_ids.append(f.id())
|
|
240
|
+
context.setFeature(f)
|
|
241
|
+
|
|
242
|
+
x = None
|
|
243
|
+
if x_expression:
|
|
244
|
+
x = x_expression.evaluate(context)
|
|
245
|
+
if x == NULL or x is None:
|
|
246
|
+
continue
|
|
247
|
+
elif self.settings.properties['x_name']:
|
|
248
|
+
x = f[self.settings.properties['x_name']]
|
|
249
|
+
if x == NULL or x is None:
|
|
250
|
+
continue
|
|
251
|
+
|
|
252
|
+
y = None
|
|
253
|
+
if y_expression:
|
|
254
|
+
y = y_expression.evaluate(context)
|
|
255
|
+
if y == NULL or y is None:
|
|
256
|
+
continue
|
|
257
|
+
elif self.settings.properties['y_name']:
|
|
258
|
+
y = f[self.settings.properties['y_name']]
|
|
259
|
+
if y == NULL or y is None:
|
|
260
|
+
continue
|
|
261
|
+
|
|
262
|
+
z = None
|
|
263
|
+
if z_expression:
|
|
264
|
+
z = z_expression.evaluate(context)
|
|
265
|
+
if z == NULL or z is None:
|
|
266
|
+
continue
|
|
267
|
+
elif self.settings.properties['z_name']:
|
|
268
|
+
z = f[self.settings.properties['z_name']]
|
|
269
|
+
if z == NULL or z is None:
|
|
270
|
+
continue
|
|
271
|
+
|
|
272
|
+
if additional_info_expression:
|
|
273
|
+
additional_hover_text.append(
|
|
274
|
+
additional_info_expression.evaluate(context))
|
|
275
|
+
elif self.settings.layout['additional_info_expression']:
|
|
276
|
+
additional_hover_text.append(
|
|
277
|
+
f[self.settings.layout['additional_info_expression']])
|
|
278
|
+
|
|
279
|
+
if x is not None:
|
|
280
|
+
xx.append(x)
|
|
281
|
+
if y is not None:
|
|
282
|
+
yy.append(y)
|
|
283
|
+
if z is not None:
|
|
284
|
+
zz.append(z)
|
|
285
|
+
|
|
286
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_MARKER_SIZE):
|
|
287
|
+
default_value = self.settings.properties['marker_size']
|
|
288
|
+
context.setOriginalValueVariable(default_value)
|
|
289
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_MARKER_SIZE,
|
|
290
|
+
context, default_value)
|
|
291
|
+
marker_sizes.append(value)
|
|
292
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_STROKE_WIDTH):
|
|
293
|
+
default_value = self.settings.properties['marker_width']
|
|
294
|
+
context.setOriginalValueVariable(default_value)
|
|
295
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_STROKE_WIDTH,
|
|
296
|
+
context, default_value)
|
|
297
|
+
stroke_widths.append(value)
|
|
298
|
+
|
|
299
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_COLOR) and (not aggregating or not executed):
|
|
300
|
+
default_value = QColor(self.settings.properties['in_color'])
|
|
301
|
+
value, conversion_success = self.settings.data_defined_properties.valueAsColor(PlotSettings.PROPERTY_COLOR, context,
|
|
302
|
+
default_value)
|
|
303
|
+
if conversion_success:
|
|
304
|
+
# We were given a valid color specification, use that color
|
|
305
|
+
colors.append(value.name())
|
|
306
|
+
else:
|
|
307
|
+
try:
|
|
308
|
+
# Attempt to interpret the value as a list of color specifications
|
|
309
|
+
value_list = self.settings.data_defined_properties.value(
|
|
310
|
+
PlotSettings.PROPERTY_COLOR, context)
|
|
311
|
+
color_list = [QgsSymbolLayerUtils.decodeColor(
|
|
312
|
+
item).name() for item in value_list]
|
|
313
|
+
colors.extend(color_list)
|
|
314
|
+
except TypeError:
|
|
315
|
+
# Not a list of color specifications, use the default color instead
|
|
316
|
+
colors.append(default_value.name())
|
|
317
|
+
|
|
318
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_STROKE_COLOR) and (not aggregating or not executed):
|
|
319
|
+
default_value = QColor(self.settings.properties['out_color'])
|
|
320
|
+
value, conversion_success = self.settings.data_defined_properties.valueAsColor(PlotSettings.PROPERTY_STROKE_COLOR, context,
|
|
321
|
+
default_value)
|
|
322
|
+
if conversion_success:
|
|
323
|
+
# We were given a valid color specification, use that color
|
|
324
|
+
stroke_colors.append(value.name())
|
|
325
|
+
else:
|
|
326
|
+
try:
|
|
327
|
+
# Attempt to interpret the value as a list of color specifications
|
|
328
|
+
value_list = self.settings.data_defined_properties.value(
|
|
329
|
+
PlotSettings.PROPERTY_STROKE_COLOR, context)
|
|
330
|
+
color_list = [QgsSymbolLayerUtils.decodeColor(
|
|
331
|
+
item).name() for item in value_list]
|
|
332
|
+
stroke_colors.extend(color_list)
|
|
333
|
+
except TypeError:
|
|
334
|
+
# Not a list of color specifications, use the default color instead
|
|
335
|
+
stroke_colors.append(default_value.name())
|
|
336
|
+
|
|
337
|
+
executed = True
|
|
338
|
+
|
|
339
|
+
self.settings.additional_hover_text = additional_hover_text
|
|
340
|
+
self.settings.x = xx
|
|
341
|
+
self.settings.y = yy
|
|
342
|
+
self.settings.z = zz
|
|
343
|
+
if marker_sizes:
|
|
344
|
+
self.settings.data_defined_marker_sizes = marker_sizes
|
|
345
|
+
if colors:
|
|
346
|
+
self.settings.data_defined_colors = colors
|
|
347
|
+
if stroke_colors:
|
|
348
|
+
self.settings.data_defined_stroke_colors = stroke_colors
|
|
349
|
+
if stroke_widths:
|
|
350
|
+
self.settings.data_defined_stroke_widths = stroke_widths
|
|
351
|
+
|
|
352
|
+
def fetch_layout_properties(self, context): # pylint: disable=too-many-statements
|
|
353
|
+
"""
|
|
354
|
+
(Re)fetches layout properties.
|
|
355
|
+
"""
|
|
356
|
+
|
|
357
|
+
title = ''
|
|
358
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_TITLE):
|
|
359
|
+
default_value = self.settings.layout['title']
|
|
360
|
+
context.setOriginalValueVariable(default_value)
|
|
361
|
+
value, _ = self.settings.data_defined_properties.valueAsString(PlotSettings.PROPERTY_TITLE,
|
|
362
|
+
context, default_value)
|
|
363
|
+
title = value
|
|
364
|
+
legend_title = ''
|
|
365
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_LEGEND_TITLE):
|
|
366
|
+
default_value = self.settings.layout['legend_title']
|
|
367
|
+
context.setOriginalValueVariable(default_value)
|
|
368
|
+
value, _ = self.settings.data_defined_properties.valueAsString(PlotSettings.PROPERTY_LEGEND_TITLE,
|
|
369
|
+
context, default_value)
|
|
370
|
+
legend_title = value
|
|
371
|
+
x_title = ''
|
|
372
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_X_TITLE):
|
|
373
|
+
default_value = self.settings.layout['x_title']
|
|
374
|
+
context.setOriginalValueVariable(default_value)
|
|
375
|
+
value, _ = self.settings.data_defined_properties.valueAsString(PlotSettings.PROPERTY_X_TITLE,
|
|
376
|
+
context, default_value)
|
|
377
|
+
x_title = value
|
|
378
|
+
y_title = ''
|
|
379
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_Y_TITLE):
|
|
380
|
+
default_value = self.settings.layout['y_title']
|
|
381
|
+
context.setOriginalValueVariable(default_value)
|
|
382
|
+
value, _ = self.settings.data_defined_properties.valueAsString(PlotSettings.PROPERTY_Y_TITLE,
|
|
383
|
+
context, default_value)
|
|
384
|
+
y_title = value
|
|
385
|
+
z_title = ''
|
|
386
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_Z_TITLE):
|
|
387
|
+
default_value = self.settings.layout['z_title']
|
|
388
|
+
context.setOriginalValueVariable(default_value)
|
|
389
|
+
value, _ = self.settings.data_defined_properties.valueAsString(PlotSettings.PROPERTY_Z_TITLE,
|
|
390
|
+
context, default_value)
|
|
391
|
+
z_title = value
|
|
392
|
+
x_min = None
|
|
393
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_X_MIN):
|
|
394
|
+
default_value = self.settings.layout['x_min']
|
|
395
|
+
context.setOriginalValueVariable(default_value)
|
|
396
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_X_MIN,
|
|
397
|
+
context, default_value)
|
|
398
|
+
x_min = value
|
|
399
|
+
x_max = None
|
|
400
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_X_MAX):
|
|
401
|
+
default_value = self.settings.layout['x_max']
|
|
402
|
+
context.setOriginalValueVariable(default_value)
|
|
403
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_X_MAX,
|
|
404
|
+
context, default_value)
|
|
405
|
+
x_max = value
|
|
406
|
+
y_min = None
|
|
407
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_Y_MIN):
|
|
408
|
+
default_value = self.settings.layout['y_min']
|
|
409
|
+
context.setOriginalValueVariable(default_value)
|
|
410
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_Y_MIN,
|
|
411
|
+
context, default_value)
|
|
412
|
+
y_min = value
|
|
413
|
+
y_max = None
|
|
414
|
+
if self.settings.data_defined_properties.isActive(PlotSettings.PROPERTY_Y_MAX):
|
|
415
|
+
default_value = self.settings.layout['y_max']
|
|
416
|
+
context.setOriginalValueVariable(default_value)
|
|
417
|
+
value, _ = self.settings.data_defined_properties.valueAsDouble(PlotSettings.PROPERTY_Y_MAX,
|
|
418
|
+
context, default_value)
|
|
419
|
+
y_max = value
|
|
420
|
+
|
|
421
|
+
self.settings.data_defined_title = title
|
|
422
|
+
self.settings.data_defined_legend_title = legend_title
|
|
423
|
+
self.settings.data_defined_x_title = x_title
|
|
424
|
+
self.settings.data_defined_y_title = y_title
|
|
425
|
+
self.settings.data_defined_z_title = z_title
|
|
426
|
+
self.settings.data_defined_x_min = x_min
|
|
427
|
+
self.settings.data_defined_x_max = x_max
|
|
428
|
+
self.settings.data_defined_y_min = y_min
|
|
429
|
+
self.settings.data_defined_y_max = y_max
|
|
430
|
+
|
|
431
|
+
def set_visible_region(self, region: QgsReferencedRectangle):
|
|
432
|
+
"""
|
|
433
|
+
Sets the visible region associated with the factory, possibly triggering a rebuild
|
|
434
|
+
of a filtered plot
|
|
435
|
+
"""
|
|
436
|
+
if self.settings.properties.get('visible_features_only', False):
|
|
437
|
+
self.visible_region = region
|
|
438
|
+
self.rebuild()
|
|
439
|
+
|
|
440
|
+
def rebuild(self):
|
|
441
|
+
"""
|
|
442
|
+
Rebuilds the plot, re-fetching current values from the layer
|
|
443
|
+
"""
|
|
444
|
+
if self.source_layer:
|
|
445
|
+
self.fetch_values_from_layer()
|
|
446
|
+
|
|
447
|
+
self.trace = self._build_trace()
|
|
448
|
+
self.layout = self._build_layout()
|
|
449
|
+
self.plot_built.emit()
|
|
450
|
+
|
|
451
|
+
def _build_trace(self):
|
|
452
|
+
"""
|
|
453
|
+
Builds the final trace calling the go.xxx plotly method
|
|
454
|
+
this method here is the one performing the real job
|
|
455
|
+
|
|
456
|
+
From the initial object created (e.g. p = Plot(plot_type, plot_properties,
|
|
457
|
+
layout_properties)) this methods checks the plot_type and elaborates the
|
|
458
|
+
plot_properties dictionary passed
|
|
459
|
+
|
|
460
|
+
:return: the final Plot Trace (final Plot object, AKA go.xxx plot type)
|
|
461
|
+
"""
|
|
462
|
+
assert self.settings.plot_type in PlotFactory.PLOT_TYPES
|
|
463
|
+
|
|
464
|
+
return PlotFactory.PLOT_TYPES[self.settings.plot_type].create_trace(self.settings)
|
|
465
|
+
|
|
466
|
+
def _build_layout(self):
|
|
467
|
+
"""
|
|
468
|
+
Builds the final layout calling the go.Layout plotly method
|
|
469
|
+
|
|
470
|
+
From the initial object created (e.g. p = Plot(plot_type, plot_properties,
|
|
471
|
+
layout_properties)) this methods checks the plot_type and elaborates the
|
|
472
|
+
layout_properties dictionary passed
|
|
473
|
+
|
|
474
|
+
:return: the final Plot Layout (final Layout object, AKA go.Layout)
|
|
475
|
+
"""
|
|
476
|
+
assert self.settings.plot_type in PlotFactory.PLOT_TYPES
|
|
477
|
+
|
|
478
|
+
return PlotFactory.PLOT_TYPES[self.settings.plot_type].create_layout(self.settings)
|
|
479
|
+
|
|
480
|
+
@staticmethod
|
|
481
|
+
def js_callback(_):
|
|
482
|
+
"""
|
|
483
|
+
Returns a string that is added to the end of the plot. This string is
|
|
484
|
+
necessary for the interaction between plot and map objects
|
|
485
|
+
|
|
486
|
+
WARNING! The string ReplaceTheDiv is a default string that will be
|
|
487
|
+
replaced in a second moment
|
|
488
|
+
"""
|
|
489
|
+
|
|
490
|
+
js_str = '''
|
|
491
|
+
<script>
|
|
492
|
+
// additional js function to select and click on the data
|
|
493
|
+
// returns the ids of the selected/clicked feature
|
|
494
|
+
|
|
495
|
+
var plotly_div = document.getElementById('ReplaceTheDiv')
|
|
496
|
+
var plotly_data = plotly_div.data
|
|
497
|
+
|
|
498
|
+
// selecting function
|
|
499
|
+
plotly_div.on('plotly_selected', function(data){
|
|
500
|
+
var dds = {};
|
|
501
|
+
dds["mode"] = 'selection'
|
|
502
|
+
dds["type"] = data.points[0].data.type
|
|
503
|
+
|
|
504
|
+
featureIds = [];
|
|
505
|
+
featureIdsTernary = [];
|
|
506
|
+
|
|
507
|
+
data.points.forEach(function(pt){
|
|
508
|
+
featureIds.push(parseInt(pt.id))
|
|
509
|
+
featureIdsTernary.push(parseInt(pt.pointNumber))
|
|
510
|
+
dds["id"] = featureIds
|
|
511
|
+
dds["tid"] = featureIdsTernary
|
|
512
|
+
})
|
|
513
|
+
//console.log(dds)
|
|
514
|
+
window.status = JSON.stringify(dds)
|
|
515
|
+
})
|
|
516
|
+
|
|
517
|
+
// clicking function
|
|
518
|
+
plotly_div.on('plotly_click', function(data){
|
|
519
|
+
var featureIds = [];
|
|
520
|
+
var dd = {};
|
|
521
|
+
dd["fidd"] = data.points[0].id
|
|
522
|
+
dd["mode"] = 'clicking'
|
|
523
|
+
|
|
524
|
+
// loop and create dictionary depending on plot type
|
|
525
|
+
for(var i=0; i < data.points.length; i++){
|
|
526
|
+
|
|
527
|
+
// scatter plot
|
|
528
|
+
if(data.points[i].data.type == 'scatter'){
|
|
529
|
+
dd["uid"] = data.points[i].data.uid
|
|
530
|
+
dd["type"] = data.points[i].data.type
|
|
531
|
+
|
|
532
|
+
data.points.forEach(function(pt){
|
|
533
|
+
dd["fid"] = pt.id
|
|
534
|
+
})
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// pie
|
|
538
|
+
|
|
539
|
+
else if(data.points[i].data.type == 'pie'){
|
|
540
|
+
dd["type"] = data.points[i].data.type
|
|
541
|
+
dd["label"] = data.points[i].label
|
|
542
|
+
dd["field"] = data.points[i].data.name
|
|
543
|
+
console.log(data.points[i].label)
|
|
544
|
+
console.log(data.points[i])
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// histogram
|
|
548
|
+
else if(data.points[i].data.type == 'histogram'){
|
|
549
|
+
dd["type"] = data.points[i].data.type
|
|
550
|
+
dd["uid"] = data.points[i].data.uid
|
|
551
|
+
dd["field"] = data.points[i].data.name
|
|
552
|
+
|
|
553
|
+
// correct axis orientation
|
|
554
|
+
if(data.points[i].data.orientation == 'v'){
|
|
555
|
+
dd["id"] = data.points[i].x
|
|
556
|
+
dd["bin_step"] = data.points[i].fullData.xbins.size
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
dd["id"] = data.points[i].y
|
|
560
|
+
dd["bin_step"] = data.points[i].fullData.ybins.size
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// box plot
|
|
565
|
+
else if(data.points[i].data.type == 'box'){
|
|
566
|
+
dd["uid"] = data.points[i].data.uid
|
|
567
|
+
dd["type"] = data.points[i].data.type
|
|
568
|
+
dd["field"] = data.points[i].data.customdata[0]
|
|
569
|
+
|
|
570
|
+
// correct axis orientation
|
|
571
|
+
if(data.points[i].data.orientation == 'v'){
|
|
572
|
+
dd["id"] = data.points[i].x
|
|
573
|
+
}
|
|
574
|
+
else {
|
|
575
|
+
dd["id"] = data.points[i].y
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// violin plot
|
|
580
|
+
else if(data.points[i].data.type == 'violin'){
|
|
581
|
+
dd["uid"] = data.points[i].data.uid
|
|
582
|
+
dd["type"] = data.points[i].data.type
|
|
583
|
+
dd["field"] = data.points[i].data.customdata[0]
|
|
584
|
+
|
|
585
|
+
// correct axis orientation (for violin is viceversa)
|
|
586
|
+
if(data.points[i].data.orientation == 'v'){
|
|
587
|
+
dd["id"] = data.points[i].x
|
|
588
|
+
}
|
|
589
|
+
else {
|
|
590
|
+
dd["id"] = data.points[i].y
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// bar plot
|
|
595
|
+
else if(data.points[i].data.type == 'bar'){
|
|
596
|
+
dd["uid"] = data.points[i].data.uid
|
|
597
|
+
dd["type"] = data.points[i].data.type
|
|
598
|
+
dd["field"] = data.points[i].data.customdata[0]
|
|
599
|
+
|
|
600
|
+
// correct axis orientation
|
|
601
|
+
if(data.points[i].data.orientation == 'v'){
|
|
602
|
+
dd["id"] = data.points[i].x
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
dd["id"] = data.points[i].y
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// ternary
|
|
610
|
+
else if(data.points[i].data.type == 'scatterternary'){
|
|
611
|
+
dd["uid"] = data.points[i].data.uid
|
|
612
|
+
dd["type"] = data.points[i].data.type
|
|
613
|
+
dd["field"] = data.points[i].data.customdata
|
|
614
|
+
dd["fid"] = data.points[i].pointNumber
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
}
|
|
618
|
+
window.status = JSON.stringify(dd)
|
|
619
|
+
});
|
|
620
|
+
</script>'''
|
|
621
|
+
|
|
622
|
+
return js_str
|
|
623
|
+
|
|
624
|
+
def build_html(self, config) -> str:
|
|
625
|
+
"""
|
|
626
|
+
Creates the HTML for the plot
|
|
627
|
+
|
|
628
|
+
Calls the go.Figure plotly method and builds the figure object adjust the
|
|
629
|
+
html file and add some line (including the js_string for the interaction)
|
|
630
|
+
save the html plot file in a temporary directory and return the path
|
|
631
|
+
that can be loaded in the QWebView
|
|
632
|
+
|
|
633
|
+
This method is directly usable after the plot object has been created and
|
|
634
|
+
the 2 methods (buildTrace and buildLayout) have been called
|
|
635
|
+
|
|
636
|
+
params:
|
|
637
|
+
config (dict): config = {'scrollZoom': True, 'editable': False}
|
|
638
|
+
|
|
639
|
+
config argument is necessary to specify which buttons should appear in
|
|
640
|
+
the plotly toolbar, if the user can edit the plot inline, etc.
|
|
641
|
+
With this parameter is possible to hide the toolbar only in print layouts
|
|
642
|
+
and not in the normal plot canvas.
|
|
643
|
+
|
|
644
|
+
:return: the final html content representing the plot
|
|
645
|
+
|
|
646
|
+
Console usage:
|
|
647
|
+
.. code-block:: python
|
|
648
|
+
# create the initial object
|
|
649
|
+
settings = PlotSettings(plot_type, plot_properties, layout_properties)
|
|
650
|
+
factory = PlotFactory(settings)
|
|
651
|
+
# finally create the Figure
|
|
652
|
+
html_content = factory.build_html()
|
|
653
|
+
"""
|
|
654
|
+
fig = go.Figure(data=self.trace, layout=self.layout)
|
|
655
|
+
|
|
656
|
+
# first lines of additional html with the link to the local javascript
|
|
657
|
+
raw_plot = '<head><meta charset="utf-8" /><script src="{}">' \
|
|
658
|
+
'</script><script src="{}"></script></head>'.format(
|
|
659
|
+
self.POLY_FILL_PATH, self.PLOTLY_PATH)
|
|
660
|
+
# set some configurations
|
|
661
|
+
# call the plot method without all the javascript code
|
|
662
|
+
raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False, show_link=False,
|
|
663
|
+
config=config)
|
|
664
|
+
# insert callback for javascript events
|
|
665
|
+
raw_plot += self.js_callback(raw_plot)
|
|
666
|
+
|
|
667
|
+
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
|
|
668
|
+
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', raw_plot)
|
|
669
|
+
substr = match.group(1)
|
|
670
|
+
raw_plot = raw_plot.replace('ReplaceTheDiv', substr)
|
|
671
|
+
return raw_plot
|
|
672
|
+
|
|
673
|
+
def build_figure(self) -> str:
|
|
674
|
+
"""
|
|
675
|
+
Creates the final plot (single plot)
|
|
676
|
+
|
|
677
|
+
Calls the go.Figure plotly method and builds the figure object adjust the
|
|
678
|
+
html file and add some line (including the js_string for the interaction)
|
|
679
|
+
save the html plot file in a temporary directory and return the path
|
|
680
|
+
that can be loaded in the QWebView
|
|
681
|
+
|
|
682
|
+
This method is directly usable after the plot object has been created and
|
|
683
|
+
the 2 methods (buildTrace and buildLayout) have been called
|
|
684
|
+
|
|
685
|
+
:return: the final html path containing the plot
|
|
686
|
+
|
|
687
|
+
Console usage:
|
|
688
|
+
.. code-block:: python
|
|
689
|
+
# create the initial object
|
|
690
|
+
settings = PlotSettings(plot_type, plot_properties, layout_properties)
|
|
691
|
+
factory = PlotFactory(settings)
|
|
692
|
+
# finally create the Figure
|
|
693
|
+
path_to_output = factory.build_figure()
|
|
694
|
+
"""
|
|
695
|
+
|
|
696
|
+
config = {
|
|
697
|
+
'scrollZoom': True,
|
|
698
|
+
'editable': True,
|
|
699
|
+
'modeBarButtonsToRemove': ['toImage', 'sendDataToCloud', 'editInChartStudio']
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
with open(self.plot_path, "w", encoding="utf8") as f:
|
|
703
|
+
f.write(self.build_html(config))
|
|
704
|
+
|
|
705
|
+
return self.plot_path
|
|
706
|
+
|
|
707
|
+
def build_figures(self, plot_type, ptrace, config=None) -> str:
|
|
708
|
+
"""
|
|
709
|
+
Overlaps plots on the same map canvas
|
|
710
|
+
|
|
711
|
+
params:
|
|
712
|
+
plot_type (string): 'scatter'
|
|
713
|
+
ptrace (list of Plot Traces): list of all the different Plot Traces
|
|
714
|
+
|
|
715
|
+
plot_type argument in necessary for Bar and Histogram plots when the
|
|
716
|
+
options stack is chosen.
|
|
717
|
+
In this case the layouts of the firsts plot are deleted and only the last
|
|
718
|
+
one is taken into account (so to have the stack option).
|
|
719
|
+
|
|
720
|
+
self.layout is DELETED, so the final layout is taken from the LAST plot
|
|
721
|
+
configuration added
|
|
722
|
+
|
|
723
|
+
:return: the final html path containing the plot with the js_string for
|
|
724
|
+
the interaction
|
|
725
|
+
|
|
726
|
+
Console usage:
|
|
727
|
+
.. code-block:: python
|
|
728
|
+
# create the initial object
|
|
729
|
+
settings = PlotSettings(plot_type, plot_properties, layout_properties)
|
|
730
|
+
factory = PlotFactory(settings)
|
|
731
|
+
# finally create the Figures
|
|
732
|
+
path_to_output = factory.build_figures(plot_type, ptrace)
|
|
733
|
+
"""
|
|
734
|
+
|
|
735
|
+
# assign the variables from the kwargs arguments
|
|
736
|
+
# plot_type = kwargs['plot_type']
|
|
737
|
+
# ptrace = kwargs['pl']
|
|
738
|
+
|
|
739
|
+
# check if the plot type and render the correct figure
|
|
740
|
+
if plot_type in ('bar', 'histogram'):
|
|
741
|
+
del self.layout
|
|
742
|
+
self.layout = PlotFactory.PLOT_TYPES[plot_type].create_layout(
|
|
743
|
+
self.settings)
|
|
744
|
+
figures = go.Figure(data=ptrace, layout=self.layout)
|
|
745
|
+
|
|
746
|
+
else:
|
|
747
|
+
figures = go.Figure(data=ptrace, layout=self.layout)
|
|
748
|
+
|
|
749
|
+
# set some configurations
|
|
750
|
+
if config is None:
|
|
751
|
+
config = {'scrollZoom': True, 'editable': True}
|
|
752
|
+
# first lines of additional html with the link to the local javascript
|
|
753
|
+
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}">' \
|
|
754
|
+
'</script><script src="{}"></script></head>'.format(
|
|
755
|
+
self.POLY_FILL_PATH, self.PLOTLY_PATH)
|
|
756
|
+
# call the plot method without all the javascript code
|
|
757
|
+
self.raw_plot += plotly.offline.plot(figures, output_type='div', include_plotlyjs=False, show_link=False,
|
|
758
|
+
config=config)
|
|
759
|
+
# insert callback for javascript events
|
|
760
|
+
self.raw_plot += self.js_callback(self.raw_plot)
|
|
761
|
+
|
|
762
|
+
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
|
|
763
|
+
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', self.raw_plot)
|
|
764
|
+
substr = match.group(1)
|
|
765
|
+
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)
|
|
766
|
+
|
|
767
|
+
with open(self.plot_path, "w", encoding="utf8") as f:
|
|
768
|
+
f.write(self.raw_plot)
|
|
769
|
+
|
|
770
|
+
return self.plot_path
|
|
771
|
+
|
|
772
|
+
def build_sub_plots(self, grid, row, column, ptrace): # pylint:disable=too-many-arguments
|
|
773
|
+
"""
|
|
774
|
+
Draws plot in different plot canvases (not overlapping)
|
|
775
|
+
|
|
776
|
+
params:
|
|
777
|
+
grid (string): 'row' or 'col'. Plot are created in rows or columns
|
|
778
|
+
row (int): number of rows (if row is selected)
|
|
779
|
+
column (int): number of columns (if column is selected)
|
|
780
|
+
ptrace (list of Plot Traces): list of all the different Plot Traces
|
|
781
|
+
|
|
782
|
+
:return: the final html path containing the plot with the js_string for
|
|
783
|
+
the interaction
|
|
784
|
+
|
|
785
|
+
Console usage:
|
|
786
|
+
.. code-block:: python
|
|
787
|
+
# create the initial object
|
|
788
|
+
settings = PlotSettings(plot_type, plot_properties, layout_properties)
|
|
789
|
+
factory = PlotFactory(settings)
|
|
790
|
+
# finally create the Figures
|
|
791
|
+
path_to_output = factory.build_sub_plots('row', 1, gr, pl, tt)
|
|
792
|
+
"""
|
|
793
|
+
|
|
794
|
+
if grid == 'row':
|
|
795
|
+
|
|
796
|
+
fig = subplots.make_subplots(rows=row, cols=column)
|
|
797
|
+
|
|
798
|
+
for i, itm in enumerate(ptrace):
|
|
799
|
+
fig.add_trace(itm, row, i + 1)
|
|
800
|
+
|
|
801
|
+
elif grid == 'col':
|
|
802
|
+
|
|
803
|
+
fig = subplots.make_subplots(rows=row, cols=column)
|
|
804
|
+
|
|
805
|
+
for i, itm in enumerate(ptrace):
|
|
806
|
+
fig.add_trace(itm, i + 1, column)
|
|
807
|
+
|
|
808
|
+
# set some configurations
|
|
809
|
+
config = {'scrollZoom': True, 'editable': True}
|
|
810
|
+
# first lines of additional html with the link to the local javascript
|
|
811
|
+
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script>' \
|
|
812
|
+
'<script src="{}"></script></head>'.format(
|
|
813
|
+
self.POLY_FILL_PATH, self.PLOTLY_PATH)
|
|
814
|
+
# call the plot method without all the javascript code
|
|
815
|
+
self.raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False, show_link=False,
|
|
816
|
+
config=config)
|
|
817
|
+
# insert callback for javascript events
|
|
818
|
+
self.raw_plot += self.js_callback(self.raw_plot)
|
|
819
|
+
|
|
820
|
+
# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
|
|
821
|
+
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', self.raw_plot)
|
|
822
|
+
substr = match.group(1)
|
|
823
|
+
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)
|
|
824
|
+
|
|
825
|
+
with open(self.plot_path, "w", encoding="utf8") as f:
|
|
826
|
+
f.write(self.raw_plot)
|
|
827
|
+
|
|
828
|
+
return self.plot_path
|
|
829
|
+
|
|
830
|
+
def build_plot_dict(self) -> dict:
|
|
831
|
+
"""
|
|
832
|
+
Returns a python dictionary of the whole Figure object.
|
|
833
|
+
|
|
834
|
+
This method is not used in the plugin itself, but it is used in the
|
|
835
|
+
testing suite to avoid finding the Figure parameters with weird regex
|
|
836
|
+
from the html
|
|
837
|
+
|
|
838
|
+
:return: dictionary of the Figure object
|
|
839
|
+
"""
|
|
840
|
+
|
|
841
|
+
fig = go.Figure(data=self.trace, layout=self.layout)
|
|
842
|
+
|
|
843
|
+
return fig.to_dict()
|