2017-04-12 09:17:46 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
BarPlot.py
|
|
|
|
---------------------
|
|
|
|
Date : March 2015
|
|
|
|
Copyright : (C) 2017 by Matteo Ghetta
|
|
|
|
Email : matteo dot ghetta at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Matteo Ghetta'
|
|
|
|
__date__ = 'March 2017'
|
|
|
|
__copyright__ = '(C) 2017, Matteo Ghetta'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import plotly as plt
|
|
|
|
import plotly.graph_objs as go
|
|
|
|
|
2017-05-02 14:36:23 +10:00
|
|
|
from qgis.core import (QgsApplication,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-05-02 14:36:23 +10:00
|
|
|
QgsProcessingUtils)
|
2017-04-12 09:17:46 -05:00
|
|
|
from processing.core.parameters import ParameterTable
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.parameters import ParameterSelection
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-04-12 09:17:46 -05:00
|
|
|
from processing.core.outputs import OutputHTML
|
|
|
|
from processing.tools import vector
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class BoxPlot(QgisAlgorithm):
|
2017-04-12 09:17:46 -05:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
NAME_FIELD = 'NAME_FIELD'
|
|
|
|
VALUE_FIELD = 'VALUE_FIELD'
|
|
|
|
MSD = 'MSD'
|
|
|
|
|
|
|
|
def group(self):
|
|
|
|
return self.tr('Graphics')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-04-12 09:17:46 -05:00
|
|
|
self.addParameter(ParameterTable(self.INPUT, self.tr('Input table')))
|
|
|
|
self.addParameter(ParameterTableField(self.NAME_FIELD,
|
|
|
|
self.tr('Category name field'),
|
|
|
|
self.INPUT,
|
|
|
|
ParameterTableField.DATA_TYPE_ANY))
|
|
|
|
self.addParameter(ParameterTableField(self.VALUE_FIELD,
|
|
|
|
self.tr('Value field'),
|
|
|
|
self.INPUT,
|
|
|
|
ParameterTableField.DATA_TYPE_NUMBER))
|
|
|
|
msd = [self.tr('Show Mean'),
|
|
|
|
self.tr('Show Standard Deviation'),
|
|
|
|
self.tr('Don\'t show Mean and Standard Deviation')
|
|
|
|
]
|
|
|
|
self.addParameter(ParameterSelection(
|
|
|
|
self.MSD,
|
|
|
|
self.tr('Additional Statistic Lines'),
|
|
|
|
msd, default=0))
|
|
|
|
|
|
|
|
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Box plot')))
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'boxplot'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Box plot')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2017-04-12 09:17:46 -05:00
|
|
|
namefieldname = self.getParameterValue(self.NAME_FIELD)
|
|
|
|
valuefieldname = self.getParameterValue(self.VALUE_FIELD)
|
|
|
|
|
|
|
|
output = self.getOutputValue(self.OUTPUT)
|
|
|
|
|
2017-06-05 15:36:54 +10:00
|
|
|
values = vector.values(layer, valuefieldname)
|
2017-04-12 09:17:46 -05:00
|
|
|
|
|
|
|
x_var = [i[namefieldname] for i in layer.getFeatures()]
|
|
|
|
|
|
|
|
msdIndex = self.getParameterValue(self.MSD)
|
|
|
|
msd = True
|
|
|
|
|
|
|
|
if msdIndex == 1:
|
|
|
|
msd = 'sd'
|
|
|
|
elif msdIndex == 2:
|
|
|
|
msd = False
|
|
|
|
|
|
|
|
data = [go.Box(
|
|
|
|
x=x_var,
|
|
|
|
y=values[valuefieldname],
|
|
|
|
boxmean=msd)]
|
|
|
|
|
|
|
|
plt.offline.plot(data, filename=output, auto_open=False)
|