-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathplotWidget.cc
More file actions
898 lines (823 loc) · 29.9 KB
/
plotWidget.cc
File metadata and controls
898 lines (823 loc) · 29.9 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/*
@copyright Steve Keen 2012
@author Russell Standish
This file is part of Minsky.
Minsky 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 3 of the License, or
(at your option) any later version.
Minsky is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Minsky. If not, see <http://www.gnu.org/licenses/>.
*/
#include "minsky.h"
#include "plotWidget.h"
#include "canvas.h"
#include "variable.h"
#include "cairoItems.h"
#include "latexMarkup.h"
#include "pango.h"
#include <timer.h>
#include <cairo/cairo-ps.h>
#include <cairo/cairo-pdf.h>
#include <cairo/cairo-svg.h>
#include "CSVTools.xcd"
#include "itemT.rcd"
#include "plotOptions.rcd"
#include "plot.rcd"
#include "plot.xcd"
#include "tensorInterface.rcd"
#include "tensorInterface.xcd"
#include "tensorVal.rcd"
#include "tensorVal.xcd"
#include "plotWidget.rcd"
#include "minsky_epilogue.h"
#include <algorithm>
#include <numeric>
using namespace ecolab::cairo;
using namespace ecolab;
using namespace std;
using namespace boost;
using namespace boost::posix_time;
using namespace boost::gregorian;
namespace minsky
{
namespace
{
// orientation of bounding box ports
const double orient[PlotWidget::nBoundsPorts]={-0.4*M_PI, -0.6*M_PI, -0.2*M_PI, 0.2*M_PI, 1.2*M_PI, 0.8*M_PI};
// x coordinates of bounding box ports
const float boundX[PlotWidget::nBoundsPorts]={-0.46,0.45,-0.49,-0.49, 0.48, 0.48};
// y coordinates of bounding box ports
const float boundY[PlotWidget::nBoundsPorts]={0.49,0.49,0.47,-0.49, 0.47, -0.49};
// height of title, as a fraction of overall widget height
const double titleHeight=0.05;
}
PlotWidget::PlotWidget()
{
m_width=m_height=150;
nxTicks=nyTicks=10;
fontScale=2;
leadingMarker=true;
grid=true;
legendLeft=0.1; // override ecolab's default value
legendSide=boundingBox;
addPorts();
}
void PlotWidget::addPorts()
{
m_ports.clear();
unsigned i=0;
for (; i<nBoundsPorts; ++i) // bounds ports
m_ports.emplace_back(make_shared<InputPort>(*this));
for (; i<2*m_numLines+nBoundsPorts; ++i) // y data input ports
m_ports.emplace_back(make_shared<MultiWireInputPort>(*this));
for (; i<4*m_numLines+nBoundsPorts; ++i) // x data input ports
m_ports.emplace_back(make_shared<InputPort>(*this));
}
void PlotWidget::draw(cairo_t* cairo) const
{
CairoSave cs(cairo);
const double z=Item::zoomFactor();
cairo_scale(cairo,z,z);
const double w=iWidth();
double h=iHeight();
// if any titling, draw an extra bounding box (ticket #285)
if (!title.empty()||!xlabel().empty()||!ylabel().empty()||!y1label().empty())
{
cairo_rectangle(cairo,-0.5*w+10,-0.5*h,w-20,h-10);
cairo_set_line_width(cairo,1);
cairo_stroke(cairo);
}
cairo_translate(cairo,-0.5*w,-0.5*h);
yoffs=0;
if (!title.empty())
{
Pango pango(cairo);
pango.setFontSize(fabs(titleHeight*iHeight()));
pango.setMarkup(latexToPango(title));
cairo_set_source_rgb(cairo,0,0,0);
cairo_move_to(cairo,0.5*(w-pango.width())-pango.left(), -pango.top());
pango.show();
// allow some room for the title
yoffs=pango.height();
}
// draw bounding box ports
size_t i=0;
// draw bounds input ports
for (; i<nBoundsPorts; ++i)
{
// boundY is relative to total height h, from center.
const float x=boundX[i]*w;
const float y=boundY[i]*h;
if (!justDataChanged)
{
if (i < m_ports.size() && m_ports[i])
m_ports[i]->moveTo(x*z + this->x(), y*z + this->y());
}
if (mouseFocus && !cminsky().publicationMode())
{
if (!palette.empty())
drawTriangle(cairo, x+0.5*w, y+0.5*h, palette[(i/2)%palette.size()].colour, orient[i]);
}
}
const float plot_area_w = w - 2*portSpace;
const float plot_area_h = h - yoffs - portSpace;
if (m_numLines == 0) return;
const float dx = plot_area_w / (2*m_numLines + 1);
const float dy = plot_area_h / m_numLines;
// draw y data ports (left side)
for (; i<m_numLines+nBoundsPorts; ++i)
{
size_t k = i - nBoundsPorts;
const float y = yoffs + (k + 0.5) * dy;
const float x = portSpace;
if (!justDataChanged)
{
if (i < m_ports.size() && m_ports[i])
m_ports[i]->moveTo((x-0.5*w)*z + this->x(), (y-0.5*h)*z + this->y());
}
if (mouseFocus && !cminsky().publicationMode())
drawTriangle(cairo, x - portSpace, y, palette[k%palette.size()].colour, 0);
}
// draw RHS y data ports
for (; i<2*m_numLines+nBoundsPorts; ++i)
{
size_t k = i - m_numLines - nBoundsPorts;
const float y = yoffs + (k + 0.5) * dy;
const float x = w - portSpace;
if (!justDataChanged)
{
if (i < m_ports.size() && m_ports[i])
m_ports[i]->moveTo((x-0.5*w)*z + this->x(), (y-0.5*h)*z + this->y());
}
if (mouseFocus && !cminsky().publicationMode())
drawTriangle(cairo, x + portSpace, y, palette[k%palette.size()].colour, M_PI);
}
// draw x data ports (bottom side)
for (; i<m_ports.size(); ++i)
{
size_t k = i - 2*m_numLines - nBoundsPorts;
const float x = portSpace + (k + 1) * dx;
const float y = h - portSpace;
if (!justDataChanged)
{
if (i < m_ports.size() && m_ports[i])
m_ports[i]->moveTo((x-0.5*w)*z + this->x(), (y-0.5*h)*z + this->y());
}
if (mouseFocus && !cminsky().publicationMode())
drawTriangle(cairo, x, y + portSpace, palette[(k/2)%palette.size()].colour, -0.5*M_PI);
}
// if any titling, draw an extra bounding box (ticket #285)
if (mouseFocus && !cminsky().publicationMode() && (!title.empty()||!xlabel().empty()||!ylabel().empty()||!y1label().empty()))
{
cairo_rectangle(cairo, portSpace, yoffs, plot_area_w, plot_area_h);
cairo_set_line_width(cairo,1);
cairo_stroke(cairo);
}
// Translate to account for title and portSpace, then draw the plot content
cairo_translate(cairo, portSpace, yoffs);
Plot::draw(cairo, plot_area_w, plot_area_h);
// Draw port triangles for bounds again if they moved? No, done once.
justDataChanged=false;
cairo_set_line_width(cairo,1);
if (mouseFocus && legend)
{
double x,y,width,height;
legendSize(x,y,width,height,plot_area_w,plot_area_h);
// following code puts x,y at centre point of legend
x+=0.5*width;
const double arrowLength=6;
y=(h-yoffs-portSpace)-y+0.5*height;
cairo_move_to(cairo,x-arrowLength,y);
cairo_rel_line_to(cairo,2*arrowLength,0);
cairo_move_to(cairo,x,y-arrowLength);
cairo_rel_line_to(cairo,0,2*arrowLength);
cairo_move_to(cairo,x,y+0.5*height);
cairo_rel_line_to(cairo,0,arrowLength);
cairo_stroke(cairo);
drawTriangle(cairo,x-arrowLength,y,{0,0,0,1},M_PI);
drawTriangle(cairo,x+arrowLength,y,{0,0,0,1},0);
drawTriangle(cairo,x,y-arrowLength,{0,0,0,1},3*M_PI/2);
drawTriangle(cairo,x,y+arrowLength,{0,0,0,1},M_PI/2);
drawTriangle(cairo,x,y+0.5*height+arrowLength,{0,0,0,1},M_PI/2);
cairo_rectangle(cairo,x-0.5*width,y-0.5*height,width,height);
cairo_stroke(cairo);
}
cs.restore();
if (mouseFocus)
{
drawPorts(cairo);
displayTooltip(cairo,tooltip());
// Resize handles always visible on mousefocus. For ticket 92.
drawResizeHandles(cairo);
}
justDataChanged=false;
cairo_new_path(cairo);
cairo_rectangle(cairo,-0.5*w,-0.5*h,w,h);
cairo_clip(cairo);
if (selected) drawSelected(cairo);
}
void PlotWidget::scalePlot()
{
// set any scale overrides
setMinMax();
if (xminVar && xminVar->idx()>-1)
{
if (xIsSecsSinceEpoch && (xminVar->units.empty() || xminVar->units==Units("year")))
minx=yearToPTime(xminVar->value());
else
minx=xminVar->value();
}
else if (isfinite(xmin))
{
if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty() || cminsky().timeUnit=="year"))
minx=yearToPTime(xmin);
else
minx=xmin;
}
if (xmaxVar && xmaxVar->idx()>-1)
{
if (xIsSecsSinceEpoch && (xmaxVar->units.empty() || xmaxVar->units==Units("year")))
maxx=yearToPTime(xmaxVar->value());
else
maxx=xmaxVar->value();
}
else if (isfinite(xmax))
{
if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty() || cminsky().timeUnit=="year"))
maxx=yearToPTime(xmax);
else
maxx=xmax;
}
if (yminVar && yminVar->idx()>-1)
miny=yminVar->value();
else if (isfinite(ymin))
miny=ymin;
if (ymaxVar && ymaxVar->idx()>-1)
maxy=ymaxVar->value();
else if (isfinite(ymax))
maxy=ymax;
if (y1minVar && y1minVar->idx()>-1)
miny1=y1minVar->value();
else if (isfinite(y1min))
miny1=ymin;
if (y1maxVar && y1maxVar->idx()>-1)
maxy1=y1maxVar->value();
else if (isfinite(y1max))
maxy1=y1max;
autoscale=false;
if (!justDataChanged)
{
// label pens. In order or priority:
// 1. wire tooltip
// 2. from item tooltip
// 3. attached variable tooltip
// 4. attached variable name
size_t pen=0;
penLabels.clear();
assert(m_ports.size()>=2*m_numLines+nBoundsPorts);
for (auto portNo=nBoundsPorts; portNo<2*m_numLines+nBoundsPorts; ++portNo)
{
if (portNo<m_ports.size())
for (size_t i=0; i<m_ports[portNo]->wires().size(); ++i, ++pen)
{
auto wire=m_ports[portNo]->wires()[i];
if (!wire->tooltip().empty())
{
labelPen(pen, wire->tooltip());
continue;
}
if (auto from=wire->from(); !from->item().tooltip().empty())
{
labelPen(pen, from->item().tooltip());
continue;
}
if (portNo-nBoundsPorts<yvars.size() && i<yvars[portNo-nBoundsPorts].size())
if (auto v=yvars[portNo-nBoundsPorts][i]; !v->name.empty())
labelPen(pen, uqName(v->name));
}
}
}
}
void PlotWidget::mouseDown(float x,float y)
{
clickX=x;
clickY=y;
ct=clickType(x,y);
const double z=Item::zoomFactor();
const double gw=iWidth()*z-2*portSpace;
double gh=iHeight()*z-portSpace;
if (!title.empty()) gh=iHeight()*z-portSpace-titleHeight;
oldLegendLeft=legendLeft*gw+portSpace;
oldLegendTop=legendTop*gh;
oldLegendFontSz=legendFontSz;
}
void PlotWidget::mouseMove(float x,float y)
{
const double z=Item::zoomFactor();
//const double w=0.5*iWidth()*z, h=0.5*iHeight()*z;
//const double dx=x-this->x(), dy=y-this->y();
const double gw=iWidth()*z-2*portSpace;
double gh=iHeight()*z-portSpace;
if (!title.empty()) gh=iHeight()*z-portSpace-titleHeight;
const double yoffs=this->y()-(legendTop-0.5)*iHeight()*z;
switch (ct)
{
case ClickType::legendMove:
legendLeft = (oldLegendLeft + x - clickX-portSpace)/gw;
legendTop = (oldLegendTop + clickY - y)/gh;
if (!title.empty()) legendTop = (oldLegendTop + clickY - y + titleHeight)/gh;
break;
case ClickType::legendResize:
legendFontSz = oldLegendFontSz * (y-yoffs)/(clickY-yoffs);
if (!title.empty()) legendFontSz = oldLegendFontSz * (y-yoffs+titleHeight)/(clickY-yoffs);
break;
default:
{
auto& f=frameArgs();
if (Plot::mouseMove((x-f.offsetLeft)/f.childWidth, (f.childHeight-f.offsetTop-y)/f.childHeight,
10.0/std::max(f.childWidth,f.childHeight)))
requestRedraw();
}
break;
}
}
bool PlotWidget::onMouseOver(float x,float y)
{
const double z=Item::zoomFactor();
// coordinate system runs from bottom left to top right. Vertical coordinates must be flipped
const double dx=x-this->x()+0.5*iWidth()*z-portSpace;
const double dy=this->y()-y+0.5*iHeight()*z-portSpace;
const double gw=iWidth()*z-2*portSpace;
const double gh=iHeight()*z-portSpace-yoffs*z;
const double loffx=lh(gw,gh)*!Plot::ylabel.empty(), loffy=lh(gw,gh)*!Plot::xlabel.empty();
return Plot::mouseMove((dx-loffx)/gw, (dy-loffy)/gh, 10.0/std::max(gw,gh),formatter);
}
void PlotWidget::requestRedraw()
{
justDataChanged=true; // assume plot same size, don't do unnecessary stuff
// store previous min/max values to determine if plot scale changes
scalePlot();
if (surface.get())
surface->requestRedraw();
}
bool PlotWidget::redraw(int x0, int y0, int width, int height)
{
if (surface.get())
{
auto savedMouseFocus=mouseFocus;
auto savedSelected=selected;
auto savedJustDataChanged=justDataChanged;
mouseFocus=false; // suppress interactive elements
selected=false;
justDataChanged=true; // avoid moving ports
draw(surface->cairo());
surface->blit();
mouseFocus=savedMouseFocus;
selected=savedSelected;
justDataChanged=savedJustDataChanged;
}
return surface.get();
}
void PlotWidget::makeDisplayPlot() {
if (auto g=group.lock())
g->displayPlot=dynamic_pointer_cast<PlotWidget>(g->findItem(*this));
}
void PlotWidget::resize(const LassoBox& x)
{
const float invZ=1/Item::zoomFactor();
iWidth(abs(x.x1-x.x0)*invZ);
iHeight(abs(x.y1-x.y0)*invZ);
Item::moveTo(0.5*(x.x0+x.x1), 0.5*(x.y0+x.y1));
bb.update(*this);
}
// specialisation to avoid rerendering plots (potentially expensive)
ClickType::Type PlotWidget::clickType(float x, float y) const
{
// firstly, check whether a port has been selected
const double z=Item::zoomFactor();
for (auto& p: m_ports)
{
if (hypot(x-p->x(), y-p->y()) < portRadius*z)
return ClickType::onPort;
}
double legendWidth, legendHeight, lx, ly;
legendSize(lx, ly, legendWidth, legendHeight, z*(iWidth()-2*portSpace), z*(iHeight()-portSpace-yoffs));
// xx & yy are in plot user coordinates
const double xx= x-this->x() + z*(0.5*iWidth()-portSpace) - lx;
const double yy= z*(0.5*iHeight()-portSpace)-y+this->y() - ly+legendHeight;
if (legend && xx>0 && xx<legendWidth)
{
if (yy>0.2*legendHeight && yy<legendHeight)
return ClickType::legendMove;
if (yy>-6*z && yy<=0.2*legendHeight) // allow a bit of extra height for resize arrow
return ClickType::legendResize;
}
if (onResizeHandle(x,y)) return ClickType::onResize;
const double dx=x-this->x(), dy=y-this->y();
const double w=0.5*iWidth()*z, h=0.5*iHeight()*z;
return (abs(dx)<w && abs(dy)<h)?
ClickType::onItem: ClickType::outside;
}
static ptime epoch=microsec_clock::local_time(), accumulatedBlitTime=epoch;
static const size_t maxNumTensorElementsToPlot=10;
size_t PlotWidget::numLines(size_t n)
{
if (m_numLines!=n)
{
m_numLines=n;
addPorts();
}
return n;
}
double PlotWidget::barWidth() const
{
return accumulate(palette.begin(), palette.end(), 1.0,
[](double acc, const LineStyle& ls) {return std::min(acc, ls.barWidth);});
}
double PlotWidget::barWidth(double w)
{
for (auto& ls: palette) ls.barWidth=w;
return w;
}
void PlotWidget::addPlotPt(double t)
{
size_t pen=0;
for (size_t port=0; port<yvars.size(); ++port)
for (auto& yvar: yvars[port])
for (size_t i=0; i<min(maxNumTensorElementsToPlot,yvar->size()); ++i)
{
double x,y;
switch (xvars.size())
{
case 0: // use t, when x variable not attached
if (xIsSecsSinceEpoch && (cminsky().timeUnit.empty()||cminsky().timeUnit=="year"))
x=yearToPTime(t);
else
x=t;
y=(*yvar)[i];
break;
case 1: // use the value of attached variable
assert(xvars[0] && xvars[0]->idx()>=0); // xvars also vector of shared pointers and null derefencing error can likewise cause crash. for ticket 1248
if (xvars[0]->size()>1)
throw_error("Tensor valued x inputs not supported");
x=(*xvars[0])[0];
y=(*yvar)[i];
break;
default:
if (port < xvars.size() && xvars[port] && xvars[port]->idx()>=0) // xvars also vector of shared pointers and null derefencing error can likewise cause crash. for ticket 1248
{
if (xvars[port]->size()>1)
throw_error("Tensor valued x inputs not supported");
x=(*xvars[port])[0];
y=(*yvar)[i];
}
else
throw_error("x input not wired for port "+to_string(port+1));
break;
}
addPt(pen++, x, y);
}
// add markers
if (isfinite(miny) && isfinite(maxy))
{
for (auto& m: horizontalMarkers)
if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
{
auto eps=1e-4*(maxx-minx);
// skip marker if outside plot bounds
if (v->value()<miny||v->value()>maxy) continue;
double x[]{minx+eps,miny-eps};
double y[]{v->value(),v->value()};
setPen(pen,x,y,2);
assignSide(pen,marker);
labelPen(pen++,v->tooltip);
}
for (auto& m: verticalMarkers)
if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
{
auto value=v->value();
if (xIsSecsSinceEpoch && (v->units.empty() || v->units==Units("year")))
value=yearToPTime(value);
// skip marker if outside plot bounds
if (value<minx||value>maxx) continue;
auto eps=1e-4*(maxy-miny);
double x[]{value,value};
double y[]{miny+eps,maxy-eps};
setPen(pen,x,y,2);
assignSide(pen,marker);
labelPen(pen++,v->tooltip);
}
removePensFrom(pen);
}
// throttle plot redraws
static const time_duration maxWait=milliseconds(1000);
if ((microsec_clock::local_time()-(ptime&)lastAdd) >
min((accumulatedBlitTime-(ptime&)lastAccumulatedBlitTime) * 2, maxWait))
{
const ptime timerStart=microsec_clock::local_time();
requestRedraw();
lastAccumulatedBlitTime = accumulatedBlitTime;
lastAdd=microsec_clock::local_time();
accumulatedBlitTime += lastAdd - timerStart;
}
}
namespace {
struct TimeFormatter
{
std::string format;
TimeFormatter(const std::string& format): format(format) {}
std::string operator()(const string& label,double x,double y) const
{
ostringstream r;
r.precision(3);
r<<label<<":("<<str(ptime(date(1970,Jan,1))+microseconds(static_cast<long long>(1E6*x)),format)
<<","<<y<<")";
return r.str();
}
};
}
void PlotWidget::addConstantCurves()
{
std::vector<std::vector<std::pair<double,std::string>>> newXticks;
// determine if any of the incoming vectors has a ptime-based xVector
xIsSecsSinceEpoch=false;
for (auto& yv: yvars)
for (auto& i: yv)
{
if (i && !i->hypercube().xvectors.empty())
{
const auto& xv=i->hypercube().xvectors[0];
if (xv.dimension.type==Dimension::time)
{
xIsSecsSinceEpoch=true;
break;
}
}
}
formatter=defaultFormatter;
if (plotType!=automatic)
Plot::plotType=plotType;
size_t pen=0;
bool noLhsPens=true; // track whether any left had side ports are connected
clearPensOnLabelling=true; // arrange for penLabels to be cleared first time an entry is added
const OnStackExit setClearPensOnLabellingFalse([this]{clearPensOnLabelling=false;});
for (size_t port=0; port<yvars.size(); ++port)
for (size_t i=0; i<yvars[port].size(); ++i)
if (yvars[port][i])
{
if (port<m_numLines) noLhsPens=false;
auto& yv=yvars[port][i];
if (yv->size()>0) (*yv)[0]; // ensure cachedTensors are up to date
auto d=yv->hypercube().dims();
if (d.empty())
{
if (Plot::plotType==Plot::bar)
addPlotPt(0);
pen++;
continue;
}
// work out a reference to the x data
vector<double> xdefault;
double* x;
if (port<xvars.size() && xvars[port])
{
if (xvars[port]->hypercube().xvectors[0].size()!=d[0])
throw error("x vector not same length as y vectors");
if (xvars[port]->index().empty())
x=xvars[port]->begin();
else
{
xdefault.reserve(d[0]);
for (size_t i=0; i<d[0]; ++i)
xdefault.push_back(xvars[port]->atHCIndex(i));
x=xdefault.data();
}
}
else
{
xdefault.reserve(d[0]);
newXticks.emplace_back();
if (yv->hypercube().rank()) // yv carries its own x-vector
{
const auto& xv=yv->hypercube().xvectors[0];
assert(xv.size()==d[0]);
switch (xv.dimension.type)
{
case Dimension::string:
for (size_t i=0; i<xv.size(); ++i)
{
newXticks.back().emplace_back(i, str(xv[i]));
xdefault.push_back(i);
}
if (plotType==automatic)
Plot::plotType=Plot::bar;
break;
case Dimension::value:
if (xIsSecsSinceEpoch && xv.dimension.units=="year")
// interpret "year" as Gregorian year date
for (const auto& i: xv)
{
xdefault.push_back(yearToPTime(i.value));
if (abs(i.value-int(i.value))<0.05) // only label years
newXticks.back().emplace_back(xdefault.back(), str(int(i.value)));
}
else
for (const auto& i: xv)
xdefault.push_back(i.value);
if (plotType==automatic)
Plot::plotType=Plot::line;
break;
case Dimension::time:
{
const string format=xv.timeFormat();
formatter=TimeFormatter(xv.dimension.units);
for (const auto& i: xv)
{
const double tv=(i.time-ptime(date(1970,Jan,1))).total_microseconds()*1E-6;
newXticks.back().emplace_back(tv,str(i,format));
xdefault.push_back(tv);
}
}
if (plotType==automatic)
Plot::plotType=Plot::line;
break;
}
}
else // by default, set x to 0..d[0]-1
for (size_t i=0; i<d[0]; ++i)
xdefault.push_back(i);
x=xdefault.data();
}
const auto& idx=yv->index();
if (yv->rank()==1)
{
// 1D data's pen attributes should match the input port
if (idx.empty())
setPen(pen, x, yv->begin(), d[0]);
else
for (size_t j=0; j<idx.size(); ++j)
addPt(pen,x[idx[j]], (*yv)[j]);
pen++;
}
else
{
// higher rank y objects treated as multiple y vectors to plot
size_t startPen=pen;
if (idx.empty())
for (size_t j=0 /*d[0]*/; j<std::min(maxNumTensorElementsToPlot*d[0], yv->size()); j+=d[0])
{
setPen(pen++, x, yv->begin()+j, d[0]);
}
else // data is sparse
for (size_t j=0; j<idx.size(); ++j)
{
auto div=lldiv(idx[j], d[0]);
if (size_t(div.quot)<maxNumTensorElementsToPlot)
{
addPt(startPen+div.quot, x[div.rem], (*yv)[j]);
if (pen<=startPen+div.quot) pen=startPen+div.quot+1; // track highest pen used
}
}
// compute the pen labels
for (int j=0; startPen<pen; ++startPen, ++j)
{
string label;
size_t stride=1;
for (size_t i=1; i<yv->hypercube().rank(); ++i)
{
label+=str(yv->hypercube().xvectors[i][(j/stride)%d[i]])+" ";
stride*=d[i];
}
assignSide(startPen,port<m_numLines? Side::left: Side::right);
labelPen(startPen,defang(label));
}
}
}
justDataChanged=true;
scalePlot();
if (noLhsPens)
{
// set scale to RHS
miny=miny1;
maxy=maxy1;
}
// add markers
if (isfinite(miny) && isfinite(maxy))
{
for (auto& m: horizontalMarkers)
if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
{
if (v->value()<miny||v->value()>maxy) continue;
auto eps=1e-4*(maxx-minx);
addPt(pen,minx+eps,v->value());
addPt(pen,maxx-eps,v->value());
assignSide(pen,marker);
if (!v->tooltip.empty())
labelPen(pen++,v->tooltip);
else
labelPen(pen++,m);
}
for (auto& m: verticalMarkers)
if (auto v=cminsky().variableValues[valueId(group.lock(), ':'+m)])
{
auto value=v->value();
if (xIsSecsSinceEpoch && (v->units.empty() || v->units==Units("year")))
value=yearToPTime(value);
if (value<minx||value>maxx) continue;
auto eps=1e-4*(maxy-miny);
addPt(pen,value,miny+eps);
addPt(pen,value,maxy-eps);
assignSide(pen,marker);
if (!v->tooltip.empty())
labelPen(pen++,v->tooltip);
else
labelPen(pen++,m);
}
}
if (newXticks.size()==1) // nothing to disambiguate
xticks=std::move(newXticks.front());
else
{
xticks.clear();
// now work out which xticks we'll use See Ravel #173
for (auto& i: newXticks)
if (i.empty())
{
xticks.clear();
break; // value axes trump all
}
else if (xticks.empty())
xticks=std::move(i);
else
{// expand range of tick labels by each pen's tick labels in turn
auto j=i.begin();
for (; j!=i.end(); ++j)
if (j->first>=xticks.front().first)
break;
xticks.insert(xticks.begin(), i.begin(), j);
j=i.end();
for (; j!=i.begin(); --j)
if ((j-1)->first<=xticks.back().first)
break;
xticks.insert(xticks.end(), j, i.end());
}
}
}
size_t PlotWidget::startPen(size_t port) const
{
size_t pen=0;
for (size_t p=0; p<std::min(port, yvars.size()); ++p)
pen+=yvars[p].size();
return pen;
}
void PlotWidget::connectVar(const shared_ptr<VariableValue>& var, unsigned port)
{
assert(var);
if (port<nBoundsPorts)
switch (port)
{
case 0: xminVar=var; return;
case 1: xmaxVar=var; return;
case 2: yminVar=var; return;
case 3: ymaxVar=var; return;
case 4: y1minVar=var; return;
case 5: y1maxVar=var; return;
}
if (port-nBoundsPorts<2*m_numLines)
{
yvars.resize(port-nBoundsPorts+1);
yvars[port-nBoundsPorts].push_back(var);
// assign Side::right to pens belonging to the RHS
assignSide(startPen(port-nBoundsPorts+1)-1,port-nBoundsPorts<m_numLines? Side::left: Side::right);
}
else if (port-nBoundsPorts<4*m_numLines)
{
xvars.resize(port-nBoundsPorts-2*m_numLines+1);
xvars[port-nBoundsPorts-2*m_numLines]=var;
}
justDataChanged=false;
scalePlot();
}
void PlotWidget::disconnectAllVars()
{
xvars.clear();
yvars.clear();
xminVar=xmaxVar=yminVar=ymaxVar=y1minVar=y1maxVar=nullptr;
}
set<string> PlotWidget::availableMarkers() const
{
// search upwards through group heirarchy, looking for variable to add
set<string> r;
for (auto g=group.lock(); g; g=g->group.lock())
for (auto& i: g->items)
if (auto v=i->variableCast())
r.insert(uqName(v->rawName()));
return r;
}
}
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::PlotWidget);