-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPPlot.cpp
2200 lines (1882 loc) · 70.6 KB
/
PPlot.cpp
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Modified by David Kopec:
// - Added SVGChart Namespace
//
// Copyright 2019 David Kopec
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// Original Copyright Notice:
/***************************************************************************
* *
* Copyright notice: *
* *
* This is free Pier ware. You may do whatever you want with this code, *
* except that you should not remove this copyright notice. *
* *
***************************************************************************/
#include "PPlot.h"
#include <algorithm>
#include <stdio.h>
#include <math.h>
// --- #include <stdlib.h>
#include <assert.h>
namespace SVGChart {
using namespace std;
static PPlot *sCurrentPPlot { nullptr };
const float kFloatSmall = 1e-20f;
const float kLogMin = 1e-10f;// min argument for log10 function
const float kExpMax = 1e10f;// max argument for pow10 function
const float kLogMinClipValue = 1e-10f;// pragmatism to avoid problems with small values in log plot
const float kEps = 1e-4f;
const float kRelMajorTickSize = 0.02f;
const float kRelMinorTickSize = 0.01f;
const int kMinMinorTickScreenSize = 1;// minor ticks should not become smaller than this
const float kMaxMajorTickSizeInFontHeight = 0.5f;// not larger than half the font height
const float kLittleIncrease = 1.0001f;
const float kLittleDecrease = 0.9999f;
const float kTickValueVeryBig = 1.0e4;// switch to scientific format
const float kTickValueVerySmall = (float)1.0e-3;
const float kMajorTickXInitialFac = 2.0f;
const float kMajorTickYInitialFac = 3.0f;
const PMargins kDefaultMargins = PMargins (40,20,5,42);
const float PPlot::kRangeVerySmall = (float)1.0e-3; // also in ZoomInteraction
template <class T> const T & PMin (const T &a, const T &b) {
return b< a ? b: a;
}
template <class T> const T & PMax (const T &a, const T &b) {
return b> a ? b: a;
}
float SafeLog (float inFloat, float inBase, float inFac) {
if (inFloat<kLogMin) {
inFloat = kLogMin;
}
return inFac*log10 (inFloat)/log10(inBase);
}
float SafeExp (float inFloat, float inBase, float inFac) {
if (inFloat>kExpMax) {
inFloat = kExpMax;
}
return pow(inBase, inFloat/inFac);
}
long PlotDataBase::GetSize () const {
if (GetRealPlotData ()) {
return GetRealPlotData ()->size ();
}
if (GetCalculatedData ()) {
return GetCalculatedData ()->GetSize ();
}
return 0;
}
float PlotDataBase::GetValue (long inIndex) const {
if (GetRealPlotData ()) {
return (*GetRealPlotData ())[inIndex];
}
if (GetCalculatedData ()) {
return GetCalculatedData ()->GetValue (inIndex);
}
return 0;
}
bool PlotDataBase::CalculateRange (float &outXMin, float &outXMax) {
const RealPlotData *theData = GetRealPlotData ();
if (theData && theData->size () >0) {
std::vector<float>::const_iterator imin = std::min_element (theData->begin (), theData->end ());
std::vector<float>::const_iterator imax = std::max_element (theData->begin (), theData->end ());
outXMin = *imin;
outXMax = *imax;
return true;
}
else {
const CalculatedDataBase *theCalculated = GetCalculatedData ();
if (theCalculated) {
outXMin = theCalculated->GetValue (0);
outXMax = theCalculated->GetValue (theCalculated->GetSize () - 1);
return true;
}
}
return false;
}
DummyData::DummyData (long inSize) {
for (int theI=0;theI<inSize;theI++) {
mRealPlotData.push_back (theI);// simple ascending data
}
}
void StringData::AddItem (const char *inString) {
mStringData.push_back (inString);
mRealPlotData.push_back (mStringData.size ()-1);
}
void LegendData::SetDefaultColor (int inPlotIndex) {
mColor = GetDefaultColor (inPlotIndex);
}
void LegendData::SetDefaultValues (int inPlotIndex) {
SetDefaultColor (inPlotIndex);
char theBuf[32];
snprintf (theBuf, 32, "plot %d", inPlotIndex);
mName = theBuf;
}
bool PlotDataSelection::IsSelected (long inIndex) const {
if (long(size()) <= inIndex) {
return false;
}
return (*this)[inIndex]>0;
}
long PlotDataSelection::GetSelectedCount () const {
long theCount = 0;
for (int theI=0; theI < int(size()); theI++) {
if (IsSelected (theI)) {
theCount++;
}
}
return theCount;
}
int PPlot::Round (float inFloat) {
return (int)floor (inFloat+0.5f);
}
PColor LegendData::GetDefaultColor (int inPlotIndex) {
PColor theC;
switch (inPlotIndex%7) {
case 0:
theC.mR = 255;
theC.mG = 0;
theC.mB = 0;
break;
case 1:
theC.mR = 0;
theC.mG = 0;
theC.mB = 255;
break;
case 2:
theC.mR = 0;
theC.mG = 255;
theC.mB = 0;
break;
case 3:
theC.mR = 0;
theC.mG = 255;
theC.mB = 255;
break;
case 4:
theC.mR = 255;
theC.mG = 0;
theC.mB = 255;
break;
case 5:
theC.mR = 255;
theC.mG = 255;
theC.mB = 0;
break;
default:
// black
break;
}
return theC;
}
float TickInfo::RoundSpan (float inSpan) {
// round it to something producing readable tick labels
// write it in the form inSpan = a*SafeExp10 (b)
if (inSpan<=0) {
// error
return (float)-1.234567;
}
int thePow = 0;
float theSpan = inSpan;
if (inSpan>1) {
while (theSpan>10) {
theSpan/=10;
if (theSpan == inSpan) { // not a number
return (float)-1.234567;
}
thePow++;
}
}
else {
while (theSpan<1) {
theSpan*=10;
thePow--;
}
}
int theRoundedFirstDigit = PPlot::Round (theSpan);
int thePreferredFirstDigit = 1;
switch (theRoundedFirstDigit) {
case 1:
thePreferredFirstDigit = 1;
break;
case 2:
case 3:
case 4:
thePreferredFirstDigit = 2;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
thePreferredFirstDigit = 5;
break;
case 10:
thePreferredFirstDigit = 1;
thePow++;
break;
default:
// error
return (float)-1.234567;
break;
}
float theRes = thePreferredFirstDigit*pow (10, thePow);
return theRes;
}
void TickInfo::MakeFormatString (float inValue, string &outFormatString) {
if (inValue<0) {
inValue = - inValue;
}
if (inValue > kTickValueVeryBig || inValue < kTickValueVerySmall) {
outFormatString = "%.1e";
}
else {
int thePrecision = 0;
if (inValue<1) {
float theSpan = inValue;
while (theSpan<1) {
thePrecision++;
theSpan *=10;
}
}
char theBuf[128] = "%.0f";
theBuf[2] = '0'+thePrecision;
outFormatString = theBuf;
}
}
PlotDataBase::~PlotDataBase (){
}
PlotDataContainer::PlotDataContainer (){
}
PlotDataContainer::~PlotDataContainer (){
ClearData ();
}
PlotDataBase * PlotDataContainer::GetXData (int inIndex) {
if (inIndex < 0 || inIndex >= int(mXDataList.size())) {
return nullptr;
}
return mXDataList[inIndex];
}
PlotDataBase * PlotDataContainer::GetYData (int inIndex) {
if (inIndex < 0 || inIndex >= int(mYDataList.size())) {
return nullptr;
}
return mYDataList[inIndex];
}
LegendData * PlotDataContainer::GetLegendData (int inIndex) {
if (inIndex < 0 || inIndex >= int(mLegendDataList.size())) {
return nullptr;
}
return mLegendDataList[inIndex];
}
DataDrawerBase * PlotDataContainer::GetDataDrawer (int inIndex) {
if (inIndex < 0 || inIndex >= int(mDataDrawerList.size())) {
return nullptr;
}
return mDataDrawerList[inIndex];
}
PlotDataSelection * PlotDataContainer::GetPlotDataSelection (int inIndex) {
if (inIndex < 0 || inIndex >= int(mPlotDataSelectionList.size())) {
return nullptr;
}
return mPlotDataSelectionList[inIndex];
}
const PlotDataBase * PlotDataContainer::GetConstXData (int inIndex) const {
if (inIndex < 0 || inIndex >= int(mXDataList.size())) {
return nullptr;
}
return mXDataList[inIndex];
}
const PlotDataBase * PlotDataContainer::GetConstYData (int inIndex) const {
if (inIndex < 0 || inIndex >= int(mYDataList.size())) {
return nullptr;
}
return mYDataList[inIndex];
}
const LegendData * PlotDataContainer::GetConstLegendData (int inIndex) const {
if (inIndex < 0 || inIndex >= int(mLegendDataList.size())) {
return nullptr;
}
return mLegendDataList[inIndex];
}
const DataDrawerBase * PlotDataContainer::GetConstDataDrawer (int inIndex) const {
if (inIndex < 0 || inIndex >= int(mDataDrawerList.size())) {
return nullptr;
}
return mDataDrawerList[inIndex];
}
const PlotDataSelection * PlotDataContainer::GetConstPlotDataSelection (int inIndex) const {
if (inIndex < 0 || inIndex >= int(mPlotDataSelectionList.size())) {
return nullptr;
}
return mPlotDataSelectionList[inIndex];
}
void PlotDataContainer::RemoveElement (int inIndex) {
if (!(inIndex < int(mXDataList.size()) && inIndex < int(mYDataList.size()) &&
inIndex < int(mLegendDataList.size()) && inIndex < int(mDataDrawerList.size()))) {
// Invalid index
return;
}
PlotDataList::iterator theXI = mXDataList.begin () + inIndex;
PlotDataList::iterator theYI = mYDataList.begin () + inIndex;
LegendDataList::iterator theLI = mLegendDataList.begin () + inIndex;
DataDrawerList::iterator theDI = mDataDrawerList.begin () + inIndex;
PlotDataSelectionList::iterator thePI = mPlotDataSelectionList.begin () + inIndex;
delete *theXI;
delete *theYI;
delete *theLI;
delete *theDI;
delete *thePI;
mXDataList.erase (theXI);
mYDataList.erase (theYI);
mLegendDataList.erase (theLI);
mDataDrawerList.erase (theDI);
mPlotDataSelectionList.erase (thePI);
}
void PlotDataContainer::ClearData () {
PlotDataList::iterator theXI = mXDataList.begin ();
PlotDataList::iterator theYI = mYDataList.begin ();
LegendDataList::iterator theLI = mLegendDataList.begin ();
DataDrawerList::iterator theDI = mDataDrawerList.begin ();
PlotDataSelectionList::iterator thePI = mPlotDataSelectionList.begin ();
for (;theXI!=mXDataList.end () && theYI!=mYDataList.end () && theLI!=mLegendDataList.end () && theDI != mDataDrawerList.end () && thePI != mPlotDataSelectionList.end ();) {
PlotDataBase *theX = *theXI;
PlotDataBase *theY = *theYI;
LegendData *theL = *theLI;
DataDrawerBase *theD = *theDI;
PlotDataSelection *theP = *thePI;
delete theX;
delete theY;
delete theL;
delete theD;
delete theP;
theXI++;
theYI++;
theLI++;
theDI++;
thePI++;
}
mXDataList.clear ();
mYDataList.clear ();
mLegendDataList.clear ();
mDataDrawerList.clear ();
mPlotDataSelectionList.clear ();
}
void PlotDataContainer::AddXYPlot (PlotDataBase *inXData, PlotDataBase *inYData, LegendData *inLegendData, DataDrawerBase *inDataDrawer, PlotDataSelection *inPlotDataSelection) {
if (!inYData || (!inYData->GetRealPlotData () && !inYData->GetCalculatedData ())) {
return;
}
PlotDataBase *theXData = inXData;
if (!theXData) {
theXData = new DummyData (inYData->GetSize ());
}
mXDataList.push_back (theXData);
mYDataList.push_back (inYData);
LegendData *theLegendData = inLegendData;
if (!theLegendData) {
theLegendData = new LegendData ();
theLegendData->SetDefaultValues (mLegendDataList.size ());
}
mLegendDataList.push_back (theLegendData);
DataDrawerBase *theDataDrawer = inDataDrawer;
if (!theDataDrawer) {
theDataDrawer = new LineDataDrawer ();
}
mDataDrawerList.push_back (theDataDrawer);
PlotDataSelection *thePlotDataSelection = inPlotDataSelection;
if (!thePlotDataSelection) {
thePlotDataSelection = new PlotDataSelection ();
}
else {
thePlotDataSelection->resize (inYData->GetSize ());
}
mPlotDataSelectionList.push_back (thePlotDataSelection);
}
void PlotDataContainer::SetXYPlot (int inIndex, PlotDataBase *inXData, PlotDataBase *inYData, LegendData *inLegendData, DataDrawerBase *inDataDrawer, PlotDataSelection *inPlotDataSelection) {
if (!inYData || !inYData->GetRealPlotData ()) {
return;
}
if (!CheckState ()) {
return;
}
long thePlotCount = GetPlotCount ();
if (inIndex<0||inIndex>thePlotCount) {
return;
}
PlotDataBase *theXData = inXData;
// int theSize = theXData->GetSize ();
if (!theXData) {
theXData = new DummyData (inYData->GetRealPlotData ()->size ());
}
LegendData *theLegendData = inLegendData;
DataDrawerBase *theDataDrawer = inDataDrawer;
if (!theLegendData) {
theLegendData = new LegendData ();
if (inIndex >= 0 && inIndex < int(mYDataList.size()) ) {
*theLegendData = *mLegendDataList[inIndex]; // copy old values...
} else {
theLegendData->SetDefaultValues (mLegendDataList.size ());
}
}
if (!theDataDrawer) {
theDataDrawer = new LineDataDrawer ();
}
PlotDataSelection *thePlotDataSelection = inPlotDataSelection;
if (!thePlotDataSelection) {
thePlotDataSelection = new PlotDataSelection (inYData->GetSize ());
// thePlotDataSelection = new PlotDataSelection ();
}
if (inIndex >= 0 && inIndex < int(mYDataList.size()) ) {
delete mXDataList[inIndex];
delete mYDataList[inIndex];
delete mLegendDataList[inIndex];
delete mDataDrawerList[inIndex];
delete mPlotDataSelectionList[inIndex];
mXDataList[inIndex] = theXData;
mYDataList[inIndex] = inYData;
mLegendDataList[inIndex] = theLegendData;
mDataDrawerList[inIndex] = theDataDrawer;
mPlotDataSelectionList[inIndex] = thePlotDataSelection;
} else { // add at end
mXDataList.push_back (theXData);
mYDataList.push_back (inYData);
mLegendDataList.push_back (theLegendData);
mDataDrawerList.push_back (theDataDrawer);
mPlotDataSelectionList.push_back (thePlotDataSelection);
}
}
bool PlotDataContainer::SetDataDrawer (int inIndex, DataDrawerBase* inDataDrawer) {
if (inIndex < 0 || inIndex >= int(mYDataList.size()) ) {
return false;
}
DataDrawerBase* theDataDrawer = inDataDrawer;
if (!inDataDrawer) {
theDataDrawer = new LineDataDrawer;
}
delete mDataDrawerList[inIndex];
mDataDrawerList[inIndex] = theDataDrawer;
return true;
}
int PlotDataContainer::GetPlotIndexByName (const string &inName) const {
if (CheckState ()) {
for (int theI = 0; theI < int(mLegendDataList.size());theI++) {
LegendData *theLegendData = mLegendDataList[theI];
if (theLegendData->mName == inName) {
return theI;
}
}
}
return -1;
}
bool PlotDataContainer::CalculateXRange (float &outXMin, float &outXMax) const {
bool theFirst = true;
outXMin = 0;
outXMax = 0;
for (PlotDataList::const_iterator theI=mXDataList.begin();theI!=mXDataList.end ();theI++) {
PlotDataBase *theXDataBase = *theI;
if (!theXDataBase) {
return false;
}
if (theXDataBase->GetSize () == 0) {
continue;
}
float theXMin;
float theXMax;
if (!theXDataBase->CalculateRange (theXMin, theXMax)) {
return false;
}
if (theXMax < theXMin) {
return false;
}
if (theFirst) {
outXMin = theXMin;
outXMax = theXMax;
theFirst = false;
}
if (theXMax>outXMax) {
outXMax = theXMax;
}
if (theXMin<outXMin) {
outXMin = theXMin;
}
}
if (outXMin == 0 && outXMax == 0) {
return false;
}
return true;
}
bool PlotDataContainer::CalculateYRange (float inXMin, float inXMax, float &outYMin, float &outYMax) const {
outYMin = 0;
outYMax = 0;
bool theFirst = true;
for (int theI=0; theI<GetPlotCount (); theI++) {
const PlotDataBase *theXDataBase = GetConstXData (theI);
const PlotDataBase *theYDataBase = GetConstYData (theI);
if (!theXDataBase || !theYDataBase) {
return false;
}
float theYMin;
float theYMax;
if (!CalculateYRangePlot (inXMin, inXMax, *theXDataBase, *theYDataBase, theYMin,theYMax)) {
return false;
}
if (theFirst) {
outYMin = theYMin;
outYMax = theYMax;
theFirst = false;
}
if (theYMin<outYMin) {
outYMin = theYMin;
}
if (theYMax>outYMax) {
outYMax = theYMax;
}
}
return true;
}
bool PlotDataContainer::CalculateYRangePlot (float inXMin, float inXMax, const PlotDataBase &inXData, const PlotDataBase &inYData, float &outYMin, float &outYMax) const {
outYMin = 0;
outYMax = 0;
// bool theEnteredXRange = false;
bool initialized = false;
if (inXData.GetSize () != inYData.GetSize ()) {
return false;
}
for (long theI = 0; theI < inXData.GetSize (); theI++) {
float theX = inXData.GetValue (theI);
float theY = inYData.GetValue (theI);
if (theX>=inXMin && theX <= inXMax) {
if (!initialized) {
initialized = true;
outYMin = theY;
outYMax = theY;
} else {
if (theY<outYMin) {
outYMin = theY;
}
if (theY>outYMax) {
outYMax = theY;
}
}
}
}
return true;
}
bool PlotDataContainer::CheckState () const {
long theSize1 = mXDataList.size ();
long theSize2 = mYDataList.size ();
long theSize3 = mLegendDataList.size ();
long theSize4 = mDataDrawerList.size ();
long theSize5 = mPlotDataSelectionList.size ();
if (theSize1!=theSize2 || theSize1!=theSize3 || theSize1!=theSize4 || theSize1!=theSize5) {
return false;
}
return true;
}
float LinTrafo::Transform (float inValue) const {
return inValue * mSlope + mOffset;
}
float LinTrafo::TransformBack (float inValue) const {
if (mSlope != 0) {
return (inValue - mOffset) / mSlope;
} else {
return 0;
}
}
float LogTrafo::Transform (float inValue) const{
if (inValue<kLogMinClipValue) {
inValue = kLogMinClipValue;
}
return SafeLog (inValue, mBase, mFactor)*mSlope+mOffset;
}
float LogTrafo::TransformBack (float inValue) const {
if (mSlope != 0) {
return SafeExp( (inValue - mOffset)/mSlope, mBase, mFactor);
} else {
return 0;
}
}
bool LinTickIterator::Init () {
if (!mAxisSetup) {
return false;
}
float theMin = mAxisSetup->mMin;
float theMajorTickSpan = mAxisSetup->mTickInfo.mMajorTickSpan;
int theDiv = mAxisSetup->mTickInfo.mTickDivision;
mDelta = theMajorTickSpan/theDiv;
mCount = ceil (theMin/mDelta);
mCurrentTick = mCount*mDelta;
mFormatString = mAxisSetup->mTickInfo.mFormatString;
return true;
}
bool LinTickIterator::GetNextTick (float &outTick, bool &outIsMajorTick, string &outFormatString) {
if (!mAxisSetup) {
return false;
}
if (mCurrentTick>mAxisSetup->mMax*kLittleIncrease) {
return false;
}
outTick = mCurrentTick;
outIsMajorTick = (mCount%mAxisSetup->mTickInfo.mTickDivision == 0);
outFormatString = mFormatString;
mCurrentTick += mDelta;
mCount++;
return true;
}
bool LinTickIterator::InitFromRanges (float inParRange, float inOrthoScreenRange, float inDivGuess, TickInfo &ioTickInfo) const {
if (inDivGuess <= kFloatSmall) {
return false;
}
float thePreferredSpan = TickInfo::RoundSpan (inParRange/inDivGuess);
if (thePreferredSpan < 0) {
return false;
}
float thePreferredNrOfTicks = inParRange/thePreferredSpan;
if (thePreferredNrOfTicks <1) {
ioTickInfo.mMajorTickSpan = inParRange;
}
else {
ioTickInfo.mMajorTickSpan = thePreferredSpan;
}
ioTickInfo.mTickDivision = 5;
if (ioTickInfo.mAutoTickSize) {
ioTickInfo.mMinorTickScreenSize = PMax (kMinMinorTickScreenSize, PPlot::Round (inOrthoScreenRange*kRelMinorTickSize));
ioTickInfo.mMajorTickScreenSize = PMax (ioTickInfo.mMinorTickScreenSize+1, PPlot::Round (inOrthoScreenRange*kRelMajorTickSize));
}
TickInfo::MakeFormatString (ioTickInfo.mMajorTickSpan, ioTickInfo.mFormatString);
return true;
}
bool LogTickIterator::Init () {
if (!mAxisSetup) {
return false;
}
float theMin = mAxisSetup->mMin;
// float theMax = mAxisSetup->mMax;
float theMajorTickSpan = mAxisSetup->mTickInfo.mMajorTickSpan;
int theDiv = mAxisSetup->mTickInfo.mTickDivision;
mDelta = theMajorTickSpan/theDiv;
float theBase = mAxisSetup->mLogBase;
long theLogFac = 1;//mAxisSetup->mLogFactor;
long thePowMin = (long)floor(SafeLog(theMin, theBase, theLogFac));
mCurrentTick = SafeExp (thePowMin, theBase, theLogFac);
mCount = 0;
// walk to the first tick
if (theMin<=0) {
return false;
// error
}
else {
// walk forward
float theNext = mCurrentTick+mDelta*SafeExp (thePowMin, theBase, theLogFac);;
while (theNext<=theMin*kLittleDecrease) {
mCurrentTick = theNext;
theNext += mDelta*SafeExp (thePowMin,theBase, theLogFac);
mCount++;
}
}
return true;
}
bool LogTickIterator::InitFromRanges ([[maybe_unused]] float inParRange, float inOrthoScreenRange, float inDivGuess, TickInfo &ioTickInfo) const {
if (inDivGuess<=kFloatSmall) {
return false;
}
/*
float thePreferredSpan = TickInfo::RoundSpan (inParRange/inDivGuess);
float thePreferredNrOfTicks = inParRange/thePreferredSpan;
if (thePreferredNrOfTicks <1) {
ioTickInfo.mMajorTickSpan = inParRange;
}
else {
ioTickInfo.mMajorTickSpan = thePreferredSpan;
}
*/
float theBase = mAxisSetup->mLogBase;
ioTickInfo.mMajorTickSpan = theBase-1;// relative
ioTickInfo.mTickDivision = PPlot::Round (ioTickInfo.mMajorTickSpan);
ioTickInfo.mMinorTickScreenSize = PMax (kMinMinorTickScreenSize, PPlot::Round (inOrthoScreenRange*kRelMinorTickSize));
ioTickInfo.mMajorTickScreenSize = PMax (ioTickInfo.mMinorTickScreenSize+1, PPlot::Round (inOrthoScreenRange*kRelMajorTickSize));
ioTickInfo.mFormatString = "%.1e";
return true;
}
bool LogTickIterator::GetNextTick (float &outTick, bool &outIsMajorTick, string &outFormatString) {
if (!mAxisSetup) {
return false;
}
if (mCurrentTick>mAxisSetup->mMax*kLittleIncrease) {
return false;
}
outTick = mCurrentTick;
outIsMajorTick = (mCount%mAxisSetup->mTickInfo.mTickDivision == 0);
TickInfo::MakeFormatString (outTick, outFormatString);
float theBase = mAxisSetup->mLogBase;
float theLogFac = 1;//mAxisSetup->mLogFactor;
float theLogNow = SafeLog(mCurrentTick, theBase, theLogFac);
int thePowNow = (int)floor(theLogNow);
outIsMajorTick = false;
if (fabs (theLogNow-thePowNow)<kEps) {
outIsMajorTick = true;
}
if (mAxisSetup->mLogFactor>1) {
char theBuf[128];
snprintf (theBuf, 128, "%d", thePowNow*20);
outFormatString = theBuf;
}
mCurrentTick += mDelta*SafeExp (thePowNow, theBase, theLogFac);
mCount++;
return true;
}
bool LogTickIterator::AdjustRange (float &ioMin, float &ioMax) const {
float theBase = mAxisSetup->mLogBase;
long theLogFac = 1;//mAxisSetup->mLogFactor;
if (mAxisSetup->mMaxDecades > 0) {
ioMin = ioMax/SafeExp (mAxisSetup->mMaxDecades, theBase, theLogFac);
}
if (ioMin == 0 && ioMax == 0) {
ioMin = kLogMinClipValue;
ioMax = 1.0f;
}
if (ioMin <= 0 || ioMax<=0) {
return false;
}
ioMin = RoundDown (ioMin*kLittleIncrease);
ioMax = RoundUp (ioMax*kLittleDecrease);
if (ioMin<kLogMinClipValue) {
ioMin = kLogMinClipValue;
}
if (mAxisSetup->mMaxDecades > 0) {
ioMin = ioMax/SafeExp (mAxisSetup->mMaxDecades, theBase, theLogFac);
}
return true;
}
float LogTickIterator::RoundUp (float inFloat) const {
float theBase = mAxisSetup->mLogBase;
float theLogFac = 1;//mAxisSetup->mLogFactor;
int thePow = (int)ceil(SafeLog(inFloat, theBase, theLogFac));
return pow (theBase, thePow);
}
float LogTickIterator::RoundDown (float inFloat) const {
float theBase = mAxisSetup->mLogBase;
long theLogFac = 1;//mAxisSetup->mLogFactor;
int thePow = (int)floor(SafeLog(inFloat,theBase, theLogFac));
return pow (theBase, thePow);
}
bool NamedTickIterator::GetNextTick (float &outTick, bool &outIsMajorTick, string &outFormatString) {
if (LinTickIterator::GetNextTick (outTick, outIsMajorTick, outFormatString)) {
int theIndex = PPlot::Round (outTick);
if (theIndex>=0 && theIndex < (int)mStringList.size ()) {
outFormatString = mStringList[theIndex];
return true;
}
}
return false;
}
bool NamedTickIterator::InitFromRanges (float inParRange, float inOrthoScreenRange, float inDivGuess, TickInfo &outTickInfo) const {
if (LinTickIterator::InitFromRanges (inParRange, inOrthoScreenRange, inDivGuess, outTickInfo)) {
outTickInfo.mTickDivision = 1;
return true;
}
return false;
}
bool PainterTester::Draw (Painter &inPainter) {
const char * theString = "The quick brown fox...";
int theWidth = inPainter.CalculateTextDrawSize (theString);
int theOffset = theWidth/10;
// a horizontal lines
int theHAscent_x = theOffset+2*inPainter.GetFontHeight ();
int theHAscent_y = 10;
int theHAscent_w = theWidth;
inPainter.DrawLine (theHAscent_x, theHAscent_y, theHAscent_x+theHAscent_w, theHAscent_y);
int theHDescent_x = theHAscent_x;
int theHDescent_y = theHAscent_y+inPainter.GetFontHeight ();
int theHDescent_w = theHAscent_w;
inPainter.DrawLine (theHDescent_x, theHDescent_y, theHDescent_x+theHDescent_w, theHDescent_y);
// a vertical lines
int theVAscent_x = theOffset;
int theVAscent_y = theHAscent_y+theWidth;
int theVAscent_h = -theWidth;
inPainter.DrawLine (theVAscent_x, theVAscent_y, theVAscent_x, theVAscent_y+theVAscent_h);
int theVDescent_x = theVAscent_x+inPainter.GetFontHeight ();
int theVDescent_y = theVAscent_y;
int theVDescent_h = theVAscent_h;
inPainter.DrawLine (theVDescent_x, theVDescent_y, theVDescent_x, theVDescent_y+theVDescent_h);
// Draw vertical text, followed by horizontal.
inPainter.DrawRotatedText (theVDescent_x, theVDescent_y, -90, theString);
inPainter.DrawText (theHDescent_x, theHDescent_y, theString);
return true;
}
PPlot::PPlot ():
mHasAnyModifyingCalculatorBeenActive (false),
mXTickIterator (&mXLinTickIterator),
mYTickIterator (&mYLinTickIterator),
mXTrafo (&mXLinTrafo),
mYTrafo (&mYLinTrafo),
mOwnsPPlotDrawer (true)
{
mMargins = kDefaultMargins;
mYAxisSetup.mAscending = false;
}
PPlot::~PPlot () {
if (mOwnsPPlotDrawer) {
delete mPPlotDrawer;
}
}
bool PPlot::Draw (Painter &inPainter) {
PRect theRect;
theRect.mX = mMargins.mLeft;
theRect.mY = mMargins.mTop;
theRect.mW = inPainter.GetWidth () - mMargins.mLeft - mMargins.mRight;
theRect.mH = inPainter.GetHeight () - mMargins.mTop - mMargins.mBottom;
if (mPPlotDrawer) {
mPPlotDrawer->Prepare (inPainter, *this);
return mPPlotDrawer->Draw (inPainter);
}
if (!mPlotDataContainer.GetPlotCount ()) {
return true;
}
if (!ConfigureSelf ()) {
return false;
}
bool theShouldRepeat = true;
long theRepeatCount = 0;
while (theShouldRepeat && theRepeatCount<2) {
theRepeatCount++;
if (!ValidateData ()) {
return false;
}
if (!CalculateAxisRanges ()) {
return false;
}
if (!this->CheckRange (mXAxisSetup)) {
return false;
}
if (!this->CheckRange (mYAxisSetup)) {