Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] New custom function to get the colors from categories used #288

Merged
merged 2 commits into from
Aug 20, 2022
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
54 changes: 54 additions & 0 deletions DataPlotly/core/plot_expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""
/***************************************************************************
DataPlotly
A QGIS plugin
D3 Plots for QGIS
-------------------
begin : 2022-06-08
git sha : $Format:%H$
copyright : (C) 2020 by matteo ghetta
email : matteo.ghetta@faunalia.it
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

from qgis.utils import qgsfunction
from qgis.core import QgsRenderContext


@qgsfunction(args='auto', group='DataPlotly')
def get_symbol_colors(feature, parent, context):
"""
Retrieve the color of each category as html code. You can use this function
to set the plot items (pie slices, bars, points, etc) to the same color
of the feature visible in the map.
<h4>Syntax</h4>
<p>
get_symbol_colors() -> '#da1ddd'
</p>
"""

layer = context.variable('layer')
renderer_context = QgsRenderContext()
renderer = layer.renderer()
renderer.startRender(renderer_context, layer.fields())

symbols = renderer.originalSymbolsForFeature(feature, renderer_context)

if symbols:
color = symbols[0].color().name()
else:
color = '#000000'

renderer.stopRender(renderer_context)

return color
11 changes: 10 additions & 1 deletion DataPlotly/data_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt, QUrl
from qgis.PyQt.QtGui import QDesktopServices
from qgis.PyQt.QtWidgets import QAction
from qgis.core import Qgis, QgsApplication
from qgis.core import Qgis, QgsApplication, QgsExpression
from qgis.gui import QgsGui

# Import the code for the dialog
Expand All @@ -39,6 +39,9 @@
from DataPlotly.layouts.plot_layout_item import PlotLayoutItemMetadata
from DataPlotly.gui.layout_item_gui import PlotLayoutItemGuiMetadata

# import custom expressions
from .core.plot_expressions import get_symbol_colors


class DataPlotly: # pylint: disable=too-many-instance-attributes
"""QGIS Plugin Implementation."""
Expand Down Expand Up @@ -132,6 +135,9 @@ def initGui(self):
self.iface.pluginHelpMenu().addAction(self.help_action)
self.help_action.triggered.connect(self.open_help)

# register the function
QgsExpression.registerFunction(get_symbol_colors)

def initProcessing(self):
"""Create the Processing provider"""
QgsApplication.processingRegistry().addProvider(self.provider)
Expand All @@ -151,6 +157,9 @@ def unload(self):
# Remove processing provider
QgsApplication.processingRegistry().removeProvider(self.provider)

# unregister the function
QgsExpression.unregisterFunction('get_symbol_colors')

@staticmethod
def open_help():
""" Open the online help. """
Expand Down