Skip to content
Merged
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
55 changes: 14 additions & 41 deletions DataPlotly/core/plot_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class PlotFactory(QObject): # pylint:disable=too-many-instance-attributes
"""

# create fixed class variables as paths for local javascript files
POLY_FILL_PATH = QUrl.fromLocalFile(
os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'jsscripts/polyfill.min.js'))).toString()
if Qgis.versionInt() >= 40000:
plotly_version = "3.0.1"
else:
Expand Down Expand Up @@ -541,7 +539,7 @@ def js_callback(_):
Returns a string that is added to the end of the plot. This string is
necessary for the interaction between plot and map objects

WARNING! The string ReplaceTheDiv is a default string that will be
WARNING! The string DataPlotly is a default string that will be
replaced in a second moment
"""

Expand All @@ -560,7 +558,7 @@ def js_callback(_):
// additional js function to select and click on the data
// returns the ids of the selected/clicked feature

var plotly_div = document.getElementById('ReplaceTheDiv')
var plotly_div = document.getElementById('DataPlotly')
var plotly_data = plotly_div.data

// selecting function
Expand Down Expand Up @@ -698,7 +696,7 @@ def js_callback(_):
// additional js function to select and click on the data
// returns the ids of the selected/clicked feature

var plotly_div = document.getElementById('ReplaceTheDiv')
var plotly_div = document.getElementById('DataPlotly')
var plotly_data = plotly_div.data

// selecting function
Expand Down Expand Up @@ -859,21 +857,12 @@ def build_html(self, config) -> str:
"""
fig = go.Figure(data=self.trace, layout=self.layout)

# first lines of additional html with the link to the local javascript
raw_plot = '<head><meta charset="utf-8" /><script src="{}">' \
'</script><script src="{}"></script></head>'.format(
self.POLY_FILL_PATH, self.PLOTLY_PATH)
# set some configurations
# call the plot method without all the javascript code
raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False,
config=config)
# create the html plot as string with the local javascript
raw_plot = fig.to_html(include_plotlyjs=self.PLOTLY_PATH, config=config, div_id='DataPlotly')

# insert callback for javascript events
raw_plot += self.js_callback(raw_plot)

# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', raw_plot)
substr = match.group(1)
raw_plot = raw_plot.replace('ReplaceTheDiv', substr)
return raw_plot, fig

def build_figure(self) -> str:
Expand Down Expand Up @@ -955,21 +944,13 @@ def build_figures(self, plot_type, ptrace, config=None) -> str:
# set some configurations
if config is None:
config = {'scrollZoom': True, 'editable': True}
# first lines of additional html with the link to the local javascript
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}">' \
'</script><script src="{}"></script></head>'.format(
self.POLY_FILL_PATH, self.PLOTLY_PATH)
# call the plot method without all the javascript code
self.raw_plot += plotly.offline.plot(figures, output_type='div', include_plotlyjs=False,
config=config)

# create the html plot as string with the local javascript
self.raw_plot = figures.to_html(include_plotlyjs=self.PLOTLY_PATH, config=config, div_id='DataPlotly')

# insert callback for javascript events
self.raw_plot += self.js_callback(self.raw_plot)

# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', self.raw_plot)
substr = match.group(1)
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)

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

Expand Down Expand Up @@ -1013,21 +994,13 @@ def build_sub_plots(self, grid, row, column, ptrace): # pylint:disable=too-many

# set some configurations
config = {'scrollZoom': True, 'editable': True}
# first lines of additional html with the link to the local javascript
self.raw_plot = '<head><meta charset="utf-8" /><script src="{}"></script>' \
'<script src="{}"></script></head>'.format(
self.POLY_FILL_PATH, self.PLOTLY_PATH)
# call the plot method without all the javascript code
self.raw_plot += plotly.offline.plot(fig, output_type='div', include_plotlyjs=False,
config=config)

# create the html plot as string with the local javascript
self.raw_plot = fig.to_html(include_plotlyjs=self.PLOTLY_PATH, config=config, div_id='DataPlotly')

# insert callback for javascript events
self.raw_plot += self.js_callback(self.raw_plot)

# use regex to replace the string ReplaceTheDiv with the correct plot id generated by plotly
match = re.search(r'Plotly.newPlot\(\s*[\'"](.+?)[\'"]', self.raw_plot)
substr = match.group(1)
self.raw_plot = self.raw_plot.replace('ReplaceTheDiv', substr)

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

Expand Down
Loading
Loading