-
Notifications
You must be signed in to change notification settings - Fork 1
/
chartHandler.py
379 lines (337 loc) · 13.6 KB
/
chartHandler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Part of Dana-Cube Proyect by Werner Llácer (c) 2012-2018
Distributed according to the terms of the GNU LGPL v2.0 license or higher numbered versions.
The text of that particular version is available at https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html
Please see https://github.com/wllacer/dana-cube#license for further particulars about licencing of the Dana-Cube Project
"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
'''
Documentation, License etc.
@package estimaciones
# 0.3
Usos SimpleChart
base/danacube.py:631: self.chart = SimpleChart()
100:from support.gui.mplwidget import SimpleChart
631: self.chart = SimpleChart()
632: self.chartType = None #'barh'
634: self.tree.clicked.connect(self.drawChart)
637: split.addWidget(self.chart)
641: self.drawChart(None)
644: dialog = GraphDlg(self.chartType, parent=self)
646: self.chartType = dialog.result
647: self.drawChart()
654: self.drawChart()
656: def drawChart(self,index=None):
657: if self.chartType:
658: self.processChartItem(index,tipo=self.chartType)
660: self.chart.hide()
662: def processChartItem(self,index=None,tipo='bar',visibleOnly=True):
711: self.chart.axes.cla()
713: self.chart.loadData(tipo,cabeceras,datos,titulo,x_text,y_text,rowid.getFullDesc())
714: self.chart.draw()
715: self.chart.show()
1213: dialog = GraphDlg(self.parent.tabulatura.currentWidget().chartType, source, self)
1215: chart = self.parent.tabulatura.currentWidget().chart
1258: chart.axes.cla()
1261: chart.loadData(dialog.result,etiquetas,datos,titulo,x_text,y_text)
1262: chart.draw()
1263: chart.show()
1265: chart.hide()
loadData
draw
show
hide
'''
from pprint import pprint
import sys
import datetime
import argparse
from decimal import *
from random import randint
#from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog, QTreeView, QSplitter, QMenu, \
QDialog, QInputDialog, QLineEdit, QComboBox, QMessageBox,QGridLayout, \
QAbstractItemView, QTableView,QTabWidget
from PyQt5.QtPrintSupport import *
from base.core import Cubo,Vista,printArray
from base.tree import * #GuideItem,GuideItemModel,_getHeadColumn
from support.util.decorators import stopwatch,model_change_control
from support.util.jsonmgr import load_cubo
from support.util.record_functions import norm2List
from support.gui.mplwidget import *
from support.gui.dialogs import GraphDlg
import sys
#class pepe(QDialog):
#def __init__(self,parent=None):
#super().__init__(parent)
#hugo = WComboBox()
#hugo.addItems(puntuacion)
#meat = QVBoxLayout()
#meat.addWidget(hugo)
#self.setLayout(meat)
#def processChartItem(TabMgr,index=None,tipo='bar',visibleOnly=True):
#if index:
#if index.isValid():
#item = TabMgr.tree.model().itemFromIndex(index)
#rowid = item.getHead()
#else:
#return
#elif len(TabMgr.tree.selectedIndexes()) > 0:
#indice = TabMgr.tree.selectedIndexes()[0]
#item = TabMgr.tree.model().itemFromIndex(indice)
#rowid = item.getHead()
#elif TabMgr.lastItemUsed is not None:
#item = TabMgr.lastItemUsed
#rowid = item
#else:
#item = TabMgr.tree.model().invisibleRootItem().child(0)
#rowid = item
#TabMgr.lastItemUsed = rowid
#x_text = TabMgr.tree.vista.col_hdr_idx.name
#y_text = ''
#titleParms = {}
#if tipo == 'multibar':
#datos,kcabeceras = rowid.simplifyHierarchical() #msimplify(mdatos,self.textos_col)
#titleParms = {'format':'string', 'delimiter':' > '}
#else:
#datos,kcabeceras = rowid.simplify() #item.getPayload(),self.textos_col)
##suprimo las columnas ocultas
#if visibleOnly:
#for k in range(len(kcabeceras)-1,-1,-1):
#pos = kcabeceras[k]
#if TabMgr.tree.isColumnHidden(pos +1):
#del kcabeceras[k]
#if tipo == 'multibar':
#for j in range(len(datos)):
#del datos[j][k]
#else:
#del datos[k]
#titulo = TabMgr.tree.vista.row_hdr_idx.name+'> '+rowid.getFullHeadInfo(**titleParms) + '\n' + \
#'{}({})'.format(TabMgr.tree.vista.agregado,TabMgr.tree.vista.campo)
#cabeceras = [ TabMgr.tree.colHdr[k] for k in kcabeceras ]
#if len(datos) == 0:
#TabMgr.chart.axes.cla()
#else:
#TabMgr.chart.loadData(tipo,cabeceras,datos,titulo,x_text,y_text,rowid.getFullDesc())
#TabMgr.chart.draw()
#TabMgr.chart.show()
#def drawGraph(danaCube,source,id,visibleOnly=True):
#dialog = GraphDlg(danaCube.parent.tabulatura.currentWidget().chartType, source, danaCube)
#if dialog.exec_():
#chart = danaCube.parent.tabulatura.currentWidget().chart
#if dialog.result:
#if source == 'row':
#item = danaCube.model().itemFromIndex(id)
#titulo = item['value']
#datos = []
#etiquetas = []
#for k,entrada in enumerate(item.getPayload()):
#if entrada is None:
#continue
#elif visibleOnly and danaCube.isColumnHidden(k+1):
#continue
#else:
#datos.append(entrada)
#etiquetas.append(danaCube.colHdr[k])
#elif source == 'col':
#titulo = danaCube.colHdr[id - 1]
#datos = []
#etiquetas = []
#for entrada in danaCube.model().traverse():
#if dialog.hojas and not entrada.isLeaf():
#continue
#if entrada.gpi(id -1) is None:
#continue
#if visibleOnly:
#entry = entrada
#hidden = False
#while entry:
#row = entry.row()
#pai = entry.parent().index() if entry.parent() else QModelIndex()
#if danaCube.isRowHidden(row,pai):
#hidden = True
#break
#entry = entry.parent()
#if hidden:
#continue
#datos.append(entrada.gpi(id -1))
#etiquetas.append(entrada.getFullHeadInfo())
#x_text = danaCube.model().name
#y_text = ''
#if len(datos) == 0:
#chart.axes.cla()
#else:
##print(dialog.result,etiquetas,datos,titulo,x_text,y_text)
#chart.loadData(dialog.result,etiquetas,datos,titulo,x_text,y_text)
#chart.draw()
#chart.show()
#else:
#chart.hide()
def getGraphTexts(vista,head,xlevel,dir='row'):
if dir == 'row':
fmodel = vista.row_hdr_idx
xmodel = vista.col_hdr_idx
fidx = vista.row_id
xidx = vista.col_id
fdim = vista.dim_row
xdim = vista.dim_col
else:
fmodel = vista.col_hdr_idx
xmodel = vista.row_hdr_idx
fidx = vista.col_id
xidx = vista.row_id
fdim = vista.dim_col
xdim = vista.dim_row
fixedParm = fmodel.name
if fdim > 1:
j = head.depth() -1 if (dir == 'row' and vista.totalizado) else head.depth()
if j == -1:
pass
else:
fixedParm = vista.cubo.lista_guias[fidx]['contexto'][j]['name']
ejeX = xmodel.name
if xdim > 1:
if dir == 'col' and vista.totalizado:
ejeX = vista.cubo.lista_guias[xidx]['contexto'][xlevel -1]['name']
else:
ejeX = vista.cubo.lista_guias[xidx]['contexto'][xlevel]['name']
titulo = '{}: {} \n por {} '.format(fixedParm,head.getFullHeadInfo(),ejeX)
ejeY = '{}({})'.format(vista.agregado,vista.campo) #TODO depende de las funciones ejecutadas
return titulo,ejeX,ejeY
#def getData():
#mis_cubos = load_cubo()
#cubo = Cubo(mis_cubos['datos locales'])
#lista = cubo.getGuideNames()
##print(lista)
##exit()
#xdesc = 'fecha'
#ydesc = 'geo'
#vista = Vista(cubo,xdesc,ydesc,'sum','votes_presential',totalizado=True,cartesian=False)
#vista.toTree2D()
##printArray(vista)
#xrecs = vista.row_hdr_idx.numRecords()
#yrecs = vista.col_hdr_idx.numRecords()
#dlg = charter()
##obtengo una fila al azar
##xrow = 0 #randint(0,xrecs -1)
##xhead = vista.row_hdr_idx.pos2item(xrow)
##resultado = vista.getVector(xhead,dir='row') #,keyfmt='sparse')
##print(xhead)
##for k in range(len(resultado)):
##texto = resultado[k][0]
##valores = [ elem.data(Qt.UserRole +1) for elem in resultado[k][1] ]
##titulo,ejeX,ejeY = getGraphTexts(vista,xhead,k,dir='row')
##dlg.chart.loadData('bar',texto,valores,titulo,ejeX,ejeY)
##dlg.chart.draw()
##dlg.show()
##if dlg.exec_():
##pass
#yrow = randint(0,yrecs -1)
#yhead = vista.col_hdr_idx.pos2item(yrow)
#resultado = vista.getVector(yhead,dir='col')
#print(yhead)
#for k in range(len(resultado)):
#if k == 0 and vista.totalizado:
#continue
#texto = resultado[k][0]
#valores = [ elem.data(Qt.UserRole +1) for elem in resultado[k][1] ]
#titulo,ejeX,ejeY = getGraphTexts(vista,yhead,k,dir='col')
#dlg.chart.loadData('bar',texto,valores,titulo,ejeX,ejeY)
#dlg.chart.draw()
#dlg.show()
#if dlg.exec_():
#pass
##x_text = vista.col_hdr_idx.name
##y_text = ''
##titleParms = {'format':'array'}
##datos,kcabeceras = head.simplify() #Hierarchical() #msimplify(mdatos,self.textos_col)
##print(head,datos,kcabeceras)
#Usos SimpleChart
#base/danacube.py:631: self.chart = SimpleChart()
#100:from support.gui.mplwidget import SimpleChart
#631: self.chart = SimpleChart()
#632: self.chartType = None #'barh'
#634: self.tree.clicked.connect(self.drawChart)
#637: split.addWidget(self.chart)
#641: self.drawChart(None)
#644: dialog = GraphDlg(self.chartType, parent=self)
#646: self.chartType = dialog.result
#647: self.drawChart()
#654: self.drawChart()
#656: def drawChart(self,index=None):
#657: if self.chartType:
#658: self.processChartItem(index,tipo=self.chartType)
#660: self.chart.hide()
#662: def processChartItem(self,index=None,tipo='bar',visibleOnly=True):
#711: self.chart.axes.cla()
#713: self.chart.loadData(tipo,cabeceras,datos,titulo,x_text,y_text,rowid.getFullDesc())
#714: self.chart.draw()
#715: self.chart.show()
#1213: dialog = GraphDlg(self.parent.tabulatura.currentWidget().chartType, source, self)
#1215: chart = self.parent.tabulatura.currentWidget().chart
#1258: chart.axes.cla()
#1261: chart.loadData(dialog.result,etiquetas,datos,titulo,x_text,y_text)
#1262: chart.draw()
#1263: chart.show()
#1265: chart.hide()
#loadData
#draw
#show
#hide
#class ChartTab(QTabWidget):
#def __init__(self,parent=None):
#super().__init__(parent)
#def loadData(self,vista,head,graphType='bar',dir='row',filter=None):
##borro los tabs
#for k in range(self.count()-1,-1,-1):
#self.removeTab(k)
#if graphType is None:
#self.hide()
#resultado = vista.getVector(head,dir=dir,filter=filter)
#for k in range(len(resultado)):
#if k == 0 and dir=='col' and vista.totalizado:
#continue
#texto = resultado[k][0]
#valores = [ elem.data(Qt.UserRole +1) for elem in resultado[k][1] ]
#titulo,ejeX,ejeY = getGraphTexts(vista,head,k,dir=dir)
#self.addTab(SimpleChart(),ejeY)
#self.setCurrentIndex(self.count() -1)
#self.currentWidget().loadData(graphType,texto,valores,titulo,ejeX,ejeY)
#self.currentWidget().draw()
#self.setCurrentIndex(0)
#def draw(self):
#for k in range(self.count()):
#self.setCurrentIndex(k)
#self.currentWidget().draw()
class charter(QDialog):
def __init__(self,parent=None):
super().__init__(parent)
self.chart =ChartTab()
self.meat = QGridLayout()
self.meat.addWidget(self.chart,0,0)
self.setLayout(self.meat)
if __name__ == '__main__':
app = QApplication(sys.argv)
dlg = charter()
mis_cubos = load_cubo()
cubo = Cubo(mis_cubos['datos locales'])
xdesc = 'partidos importantes'
ydesc = 'geo'
vista = Vista(cubo,xdesc,ydesc,'sum','votes_presential',totalizado=True,cartesian=False)
vista.toTree2D()
#printArray(vista)
xrecs = vista.row_hdr_idx.numRecords()
xrow = randint(0,xrecs -1)
xhead = vista.row_hdr_idx.pos2item(xrow)
dlg.chart.loadData(vista,xhead,dir='row')
dlg.show()
if dlg.exec_():
pass
sys.exit()