Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions DataPlotly/core/plot_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def build_html(self, config) -> str:
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', raw_plot)
substr = match.group(1)
raw_plot = raw_plot.replace('ReplaceTheDiv', substr)
return raw_plot
return raw_plot, fig

def build_figure(self) -> str:
"""
Expand Down Expand Up @@ -753,9 +753,11 @@ def build_figure(self) -> str:
'modeBarButtonsToRemove': ['toImage', 'sendDataToCloud', 'editInChartStudio']
}

raw_plot, fig = self.build_html(config)

with open(self.plot_path, "w", encoding="utf8") as f:
f.write(self.build_html(config))
return self.plot_path
f.write(raw_plot)
return self.plot_path, fig

def build_figures(self, plot_type, ptrace, config=None) -> str:
"""
Expand Down Expand Up @@ -819,7 +821,7 @@ def build_figures(self, plot_type, ptrace, config=None) -> str:
with open(self.plot_path, "w", encoding="utf8") as f:
f.write(self.raw_plot)

return self.plot_path
return self.plot_path, figures

def build_sub_plots(self, grid, row, column, ptrace): # pylint:disable=too-many-arguments
"""
Expand Down Expand Up @@ -877,7 +879,7 @@ def build_sub_plots(self, grid, row, column, ptrace): # pylint:disable=too-many
with open(self.plot_path, "w", encoding="utf8") as f:
f.write(self.raw_plot)

return self.plot_path
return self.plot_path, fig

def build_plot_dict(self) -> dict:
"""
Expand Down
3 changes: 3 additions & 0 deletions DataPlotly/data_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ def unload(self):
# unregister the function
QgsExpression.unregisterFunction('get_symbol_colors')

# TODO QGIS 4
# QgsGui.layoutItemGuiRegistry().removeLayoutItemGuiMetadata(self.plot_item_gui_metadata)

# disconnect signals for easy dev when using plugin reloader
QgsProject.instance().cleared.disconnect(self.dock_manager.removeDocks)
QgsProject.instance().readProject.disconnect(
Expand Down
43 changes: 34 additions & 9 deletions DataPlotly/gui/plot_settings_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,14 @@ def __init__(self, mode=MODE_CANVAS, parent=None, override_iface=None, message_b
self.z_combo.fieldChanged.connect(self.setLegend)

self.draw_btn.clicked.connect(self.create_plot)
self.update_btn.clicked.connect(self.UpdatePlot)
self.update_btn.clicked.connect(self.updatePlot)
self.clear_btn.clicked.connect(self.clearPlotView)
self.save_plot_btn.clicked.connect(self.save_plot_as_image)
self.save_plot_html_btn.clicked.connect(self.save_plot_as_html)
self.save_plot_json_btn.clicked.connect(self.save_plot_as_json)
self.save_plot_btn.setIcon(GuiUtils.get_icon('save_as_image.svg'))
self.save_plot_html_btn.setIcon(GuiUtils.get_icon('save_as_html.svg'))
self.save_plot_json_btn.setIcon(GuiUtils.get_icon('save_as_json.svg'))

# initialize the empty dictionary of plots
self.plot_factories = {}
Expand Down Expand Up @@ -1496,7 +1498,7 @@ def refresh_plot(self, factory):
"""
Refreshes the plot built by the specified factory
"""
self.plot_path = factory.build_figure()
self.plot_path, _ = factory.build_figure()
self.refreshPlotView()

def create_plot(self):
Expand All @@ -1519,15 +1521,15 @@ def create_plot(self):

# plot single plot, check the object dictionary length
if len(self.plot_factories) <= 1 or self.ptype == 'radar':
self.plot_path = plot_factory.build_figure()
self.plot_path, self.fig = plot_factory.build_figure()

# to plot many plots in the same figure
else:
# plot list ready to be called within go.Figure
pl = []
for _, v in self.plot_factories.items():
pl.append(v.trace[0])
self.plot_path = plot_factory.build_figures(self.ptype, pl)
self.plot_path, self.fig = plot_factory.build_figures(self.ptype, pl)

# choice to draw subplots instead depending on the combobox
elif self.subcombo.currentData() == 'subplots':
Expand All @@ -1541,13 +1543,13 @@ def create_plot(self):
# plot in single row and many columns
if self.radio_rows.isChecked():

self.plot_path = plot_factory.build_sub_plots(
self.plot_path, self.fig = plot_factory.build_sub_plots(
'row', 1, gr, pl)

# plot in single column and many rows
elif self.radio_columns.isChecked():

self.plot_path = plot_factory.build_sub_plots(
self.plot_path, self.fig = plot_factory.build_sub_plots(
'col', gr, 1, pl)
except: # pylint: disable=bare-except # noqa: F401
if self.message_bar:
Expand All @@ -1559,7 +1561,7 @@ def create_plot(self):
# connect to a simple function that reloads the view
self.refreshPlotView()

def UpdatePlot(self):
def updatePlot(self):
"""
updates only the LAST plot created
get the key of the last plot created and delete it from the plot container
Expand Down Expand Up @@ -1642,7 +1644,7 @@ def save_plot_as_html(self):
"""

plot_file, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Plot"), "", "*.html")
self, self.tr("Save Plot as HTML"), "", "*.html")
if not plot_file:
return

Expand All @@ -1657,6 +1659,29 @@ def save_plot_as_html(self):
plot_file).toString(),
QDir.toNativeSeparators(plot_file)))

def save_plot_as_json(self):
"""
Saves the plot as a local json file. The whole plot canvas is saves,
even if there are stacked plots or different subplots
"""

plot_file, _ = QFileDialog.getSaveFileName(
self, self.tr("Save Plot as JSON"), "", "*.json")
if not plot_file:
return

plot_file = QgsFileUtils.ensureFileNameHasExtension(plot_file, [
'json'])

self.fig.write_json(plot_file, validate=True, pretty=True)

if self.message_bar:
self.message_bar.pushSuccess(self.tr('DataPlotly'),
self.tr('Saved plot to <a href="{}">{}</a>').format(
QUrl.fromLocalFile(
plot_file).toString(),
QDir.toNativeSeparators(plot_file)))

def showPlotFromDic(self, plot_input_dic):
"""
Allows to call the plugin from the python console
Expand Down Expand Up @@ -1720,7 +1745,7 @@ def showPlotFromDic(self, plot_input_dic):
# create Plot instance
factory = PlotFactory(settings)

standalone_plot_path = factory.build_figure()
standalone_plot_path, _ = factory.build_figure()
standalone_plot_url = QUrl.fromLocalFile(standalone_plot_path)

self.plot_view.load(standalone_plot_url)
Expand Down
73 changes: 73 additions & 0 deletions DataPlotly/icons/save_as_json.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions DataPlotly/layouts/plot_layout_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def create_plot(self):
if len(self.plot_settings) == 1:
plot_factory = PlotFactory(self.plot_settings[0], self, polygon_filter=polygon_filter)
self.plot_settings[0].properties['visible_features_only'] = visible_features_only
return plot_factory.build_html(config)
raw_plot, _ = plot_factory.build_html(config)
return raw_plot

# to plot many plots in the same figure
elif len(self.plot_settings) > 1:
Expand All @@ -194,7 +195,7 @@ def create_plot(self):
factory = PlotFactory(plot_setting, self, polygon_filter=polygon_filter)
pl.append(factory.trace[0])

plot_path = plot_factory.build_figures(self.plot_settings[0].plot_type, pl, config=config)
plot_path, _ = plot_factory.build_figures(self.plot_settings[0].plot_type, pl, config=config)
with open(plot_path) as myfile:
return myfile.read()

Expand Down
78 changes: 44 additions & 34 deletions DataPlotly/ui/dataplotly_dockwidget_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ QListWidget::item::selected {
<property name="geometry">
<rect>
<x>0</x>
<y>-537</y>
<width>673</width>
<height>1252</height>
<y>0</y>
<width>666</width>
<height>1666</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_9">
Expand Down Expand Up @@ -1012,8 +1012,8 @@ QListWidget::item::selected {
<rect>
<x>0</x>
<y>0</y>
<width>406</width>
<height>803</height>
<width>477</width>
<height>1043</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_25" columnstretch="0,0,0">
Expand Down Expand Up @@ -1504,7 +1504,24 @@ QListWidget::item::selected {
</property>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_28">
<item row="0" column="0" colspan="6">
<item row="2" column="0">
<widget class="QPushButton" name="reload_btn">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="save_plot_html_btn">
<property name="toolTip">
<string>Export as html</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="0" colspan="7">
<widget class="QWidget" name="plot_qview" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
Expand All @@ -1514,20 +1531,7 @@ QListWidget::item::selected {
</property>
</widget>
</item>
<item row="2" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="4" colspan="2">
<item row="2" column="5" colspan="2">
<widget class="QPushButton" name="save_plot_btn">
<property name="toolTip">
<string>Export as image</string>
Expand All @@ -1546,22 +1550,18 @@ QListWidget::item::selected {
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="save_plot_html_btn">
<property name="toolTip">
<string>Export as html</string>
</property>
<property name="text">
<string/>
<item row="2" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QPushButton" name="reload_btn">
<property name="text">
<string/>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</widget>
</spacer>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="live_update_check">
Expand All @@ -1573,6 +1573,16 @@ QListWidget::item::selected {
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="QPushButton" name="save_plot_json_btn">
<property name="toolTip">
<string>Export as json</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand Down