-
Notifications
You must be signed in to change notification settings - Fork 0
/
UScientificGraph.cpp
3930 lines (3693 loc) · 164 KB
/
UScientificGraph.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
//---------------------------------------------------------------------------
// TScientificGraph -- an object for scientific plots
// This version by Peter Miller 18/7/2019 as csv file grapher
//
// Loosely based on an original public domain version by Frank heinrich, mail@frank-heinrich.de
// Major tidy up 1/9/2019 to remove unused functionality
// 6/2/2021 for 2v0 major change to use float *x_vals,*y_vals rather than SDataPoints in a TLIST.
//
/*----------------------------------------------------------------------------
* Copyright (c) 2019,2022 Peter Miller
*
* 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 AUTHOR OR COPYRIGHT HOLDER 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.
*--------------------------------------------------------------------------*/
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#include <stdio.h>
#include <values.h>
#include <time.h>
#pragma hdrstop
#include <exception>
#include "UScientificGraph.h"
#include "UScalesWindow.h"
#include "UDataPlotWindow.h"
#include "Unit1.h"
#include "About.h"
#define NoForm1 /* says Form1 is defined in another file */
#include "rprintf.h"
#include "expr-code.h"
#include <cmath>
#include "kiss_fftr.h" // for fft
#include "_kiss_fft_guts.h"
#include "yasort2.h" /* needs to be set so we sort float's ie include "#define elem_type_sort2 float" */
#include "yamedian.h" /* needs to be set to work on floats eg #define elem_type_median float */
#include "interpolate.h"
#include "smooth_diff.h"
#include "smoothing_spline.h"
// #define USE_double_to_str_exp /* define to use double_to_str_exp() for y axis with max 12 chars, if not defined use gcvt() */
#ifdef USE_double_to_str_exp
#include "float_to_str.h"
#endif
//---------------------------------------------------------------------------
#pragma package(smart_init)
//#define P_UNUSED(x) (void)x; /* a way to avoid warning unused parameter messages from the compiler */
#define DOUBLE float /* define as double to go back to original, but as points are stored as floats we can use floats in several places */
// I'm sorry for the next 2 lines, but otherwise I get a lot of warnings "zero as null pointer constant"
#undef NULL
#define NULL (nullptr)
#define STR_CONV_BUF_SIZE 2000 // the largest string you may have to convert. depends on your project
static wchar_t* __fastcall Utf8_to_w(const char* c) // convert utf encoded string to wide chars - new function
{
static wchar_t w[STR_CONV_BUF_SIZE];
memset(w,0,sizeof(w));
MultiByteToWideChar(CP_UTF8, 0, c, -1, w, STR_CONV_BUF_SIZE-1); // utf-8 (this is a windows function )
return(w);
}
// Below from https://stackoverflow.com/questions/1031645/how-to-detect-utf-8-in-plain-c
// modified Peter Miller 29-5-2024
// returns true when not a pure ascii string , but is a valid utf8 string
static bool is_utf8(const char * string);
static bool is_utf8(const char * string)
{bool not_ascii=false;
if(!string)
return 0;
const unsigned char * bytes = (const unsigned char *)string; // in case char is signed
while(*bytes)
{
if( (// ASCII - only allow values that may reasonably be in a utf8 string
bytes[0] == 0x09 ||
bytes[0] == 0x0A ||
bytes[0] == 0x0D ||
(0x20 <= bytes[0] && bytes[0] <= 0x7E)
)
) {
bytes ++;
continue;
}
if( (// non-overlong 2-byte
(0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF)
)
) { not_ascii=true;
bytes += 2;
continue;
}
if( (// excluding overlongs
bytes[0] == 0xE0 &&
(0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) ||
(// straight 3-byte
((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
bytes[0] == 0xEE ||
bytes[0] == 0xEF) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
) ||
(// excluding surrogates
bytes[0] == 0xED &&
(0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF)
)
) { not_ascii=true;
bytes += 3;
continue;
}
if( (// planes 1-3
bytes[0] == 0xF0 &&
(0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) ||
(// planes 4-15
(0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
(0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
) ||
(// plane 16
bytes[0] == 0xF4 &&
(0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
(0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
(0x80 <= bytes[3] && bytes[3] <= 0xBF)
)
) { not_ascii=true;
bytes += 4;
continue;
}
return false; // not a valid utf-8 string
}
return not_ascii; // is valid utf8 string, but only return true if not ascii
}
extern TForm1 *Form1;
extern double actual_dXMin,actual_dXMax,actual_dYMin,actual_dYMax;
extern int zoom_fun_level;
double actual_dXMin=0,actual_dXMax=100,actual_dYMin=-1,actual_dYMax=1;// initialised to same values as below
int zoom_fun_level=0; // used to keep track of level of recursion in zoom function (allows partial display refresh for speed in multiple zooms)
TScientificGraph::TScientificGraph(int iBitmapWidthK, int iBitmapHeightK)
{
iBitmapWidth = iBitmapWidthK; //bitmap
iBitmapHeight = iBitmapHeightK;
iNumberOfGraphs = 0; //no graphs
pHistory = new TList(); //Graphs
pBitmap = new Graphics::TBitmap; //bitmap
pBitmap->Width = iBitmapWidth;
pBitmap->Height = iBitmapHeight;
ColBackGround = clBlack; //default settings
ColGrid = clOlive;
ColAxis = clRed;
ColText = clYellow;
iTextSize = 10; // size of most text
aTextSize = 12; // size of axis titles
PSAxis = psSolid;
PSGrid = psDot;
iTickLength=10;
iPenWidthAxis=1;
iPenWidthGrid=1;
iTextOffset=5;
sSizeX.dMin=0;
sSizeX.dMax=100;
sSizeY.dMin=-1;
sSizeY.dMax=1;
iGridsPerX=10;
iGridsPerY=10;
bGrids=true;
bZeroLine=true;
fLeftBorder = 0.13f;
fRightBorder = 0.025f;
fTopBorder = 0.04f;
fBottomBorder = 0.11f;
dInX=0.8;
dOutX=10.0/8.0;
dInY=0.8;
dOutY=10.0/8.0;
dShiftFactor=0.1;
XLabel="Horizontal axis title";
YLabel1="Vertical";
YLabel2="axis title";
/* note these defaults are changed in UDataPlotWindow.cpp */
dLegendStartX=0.5; // was 0.5
dLegendStartY=0.95; // was 0.95
dCaptionStartX=0.5; // was 0.5
dCaptionStartY=0.5; // was 0.5
iSkipLineLevel=2; // adjacent points
fnResize(); //put size to scales
};
//------------------------------------------------------------------------------
TScientificGraph::~TScientificGraph()
{ fnClearAll() ; // clear (delete) all lines + the data they hold
pHistory->Clear();
delete pHistory;
delete pBitmap;
pBitmap=NULL;
};
//-----------------------------------------------------------------------------
void TScientificGraph::resize_bitmap(int iBitmapWidthK, int iBitmapHeightK)
{ // resize bitmap
iBitmapWidth = iBitmapWidthK; //bitmap
iBitmapHeight = iBitmapHeightK;
if(pBitmap!=NULL)
{
pBitmap->Width = iBitmapWidth;
pBitmap->Height = iBitmapHeight;
}
}
//-----------------------------------------------------------------------------
bool TScientificGraph::fnKoord2Point(TPoint *pPoint, double dXValueF,
double dYValueF)
{
double dXKoord, dYKoord;
//calculate bitmap coordinates
dXKoord = (dXValueF-sScaleX.dMin)
/(sScaleX.dMax-sScaleX.dMin);
dXKoord *= iBitmapWidth*(1-fRightBorder-fLeftBorder);
dXKoord += iBitmapWidth*fLeftBorder;
dYKoord = (dYValueF-sScaleY.dMin)
/(sScaleY.dMax-sScaleY.dMin);
dYKoord *= iBitmapHeight*(1-fTopBorder-fBottomBorder);
dYKoord = iBitmapHeight-iBitmapHeight*fBottomBorder-dYKoord;
if(dXKoord>0) //round to integer
{dXKoord += 0.5;} else {dXKoord-=0.5;}
if(dYKoord>0)
{dYKoord += 0.5;} else {dYKoord-=0.5;}
#if 1
// pPoint values are ints (32 bits) max values are MaxInt 2147483647
if (dXKoord>MaxInt) {dXKoord=MaxInt;} //no integer overflow
if (dXKoord<-MaxInt) {dXKoord=-MaxInt;} // was -32768
if (dYKoord>MaxInt) {dYKoord=MaxInt;}
if (dYKoord<-MaxInt) {dYKoord=-MaxInt;} // was -32768
#else
if (dXKoord>32767) {dXKoord=32767;} //no integer overflow
if (dXKoord<-32767) {dXKoord=-32767;} // was -32768
if (dYKoord>32767) {dYKoord=32767;}
if (dYKoord<-32767) {dYKoord=-32767;} // was -32768
#endif
pPoint->x=(long)dXKoord; //to integer
pPoint->y=(long)dYKoord;
if ((dXKoord<iBitmapWidth*fLeftBorder) || //point in plot?
(dXKoord>iBitmapWidth*(1-fRightBorder)) ||
(dYKoord<iBitmapHeight*fTopBorder) ||
(dYKoord>iBitmapHeight*(1-fBottomBorder)))
{ return false;} //no.
return true; //yes.
};
//------------------------------------------------------------------------------
static double xs,ys; // start of next line (end of previous line)
/* Dan Cohen & Ivan Sunderland clipping algorithm - see Principles of interactive computer graphics 2nd Ed, pp 65-67 */
/* this version is functionally the same as the original with various bugs resolved and efficiency improvements */
#define LEFT 1 /* bits - must all be different powers of 2 */
#define RIGHT 2
#define BOTTOM 4
#define TOP 8
void TScientificGraph::graph_line(double xe,double ye,double xmin,double xmax,double ymin,double ymax)
{// draw line from xs,ys to xe,ye clipped by min/max . Afterwards set xs,ys to xe,ye.
int outcode1=0, outcode2=0;
int c=0;
bool line_visible; //decides if line is to be drawn
double x1=xs,y1=ys,x2=xe,y2=ye;
xs=xe; // save line end as start of next line , means we can just return if nothing to draw
ys=ye;
if(x1 < xmin) outcode1 =LEFT; // for 1st one we can just assign as we know previous value was 0
if(x2 < xmin) outcode2 =LEFT; // for 1st one we can just assign as we know previous value was 0
if(x1 > xmax) outcode1 |=RIGHT; // need to OR in the remainder of the values
if(x2 > xmax) outcode2 |=RIGHT;
if(outcode1&outcode2) return; // return if x values out of range , no need to check anything else
if(y1 > ymax) outcode1 |=TOP;
if(y1 < ymin) outcode1 |=BOTTOM;
if(y2 > ymax) outcode2 |=TOP;
if(y2 < ymin) outcode2 |=BOTTOM;
if(outcode1&outcode2) return; // return if y values out of range
//AND of both codes != 0 => Line is outside. Reject line
// if some of all line is visible find which bit
line_visible = true; // if we get here assume line will be visible
while(outcode1 | outcode2) // every iteration removes 1 bit from outcode1 or outcode2 as (outcode1 & outcode2)==0 at the start we can do at most 4 iterations of this loop
{
// line part in and part out
double m,x,y;
try
{
if(x1!=x2)
m=(y2-y1)/(x2-x1); // slope of line
else
{// vertical line (at x=x1) process as a special case here
if(y1>y2) {y=y1;y1=y2;y2=y;} // swap so y2>y1
if(y1>ymax || y2<ymin || x1<xmin || x1>xmax)
{// not visible
line_visible = false;
break;
}
if(y2>ymax) y2=ymax;
if(y1<ymin) y1=ymin;
break;
}
int temp;
//Decide if point1 is inside, if not, calculate intersection
if(outcode1==0)
{
temp = outcode2;
x=x2;
y=y2;
}
else
{
temp = outcode1;
x=x1;
y=y1;
}
if(temp & TOP) //Line clips top edge , need to check this is still true before we adjust x,y as previous adjustments may already have fixed this
{c= TOP;
if(y>ymax)
{
if(fabs(m)<1e-250)
{// y=mx+c if m=0 then y=c if y>ymax then cannot fix this so line is not visible
line_visible = false;
break;
}
else
x = x1+ (ymax-y1)/m;
y = ymax;
}
}
else if(temp & BOTTOM)
{ //Line clips bottom edge
c=BOTTOM;
if(y<ymin)
{
if(fabs(m)<1e-250)
{// y=mx+c if m=0 then y=c if y<ymin then cannot fix this so line is not visible
line_visible = false;
break;
}
else
x = x1+ (ymin-y1)/m;
y = ymin;
}
}
else if(temp & LEFT)
{ //Line clips left edge
c=LEFT;
if(x<xmin)
{
x = xmin;
y = y1+ m*(xmin-x1);
}
}
else if(temp & RIGHT)
{ //Line clips right edge
c=RIGHT;
if(x>xmax)
{
x = xmax;
y = y1+ m*(xmax-x1);
}
}
//Check which point we had selected earlier as temp, and replace its co-ordinates , and delete this limit from its list
if(outcode1 != 0)
{
x1 = x;
y1 = y;
outcode1 &= ~c;
}
else {
x2 = x;
y2 = y;
outcode2 &= ~c;
}
}
catch (...)
{// rprintf("CLIP:catch!\n");
line_visible = false;
break; // just in case there is a maths error not trapped in the code above as a special case (all should be trapped)
}
}// end while(...)
if(line_visible)
{// visible portion of line is from x1,y1 to x2,y2
TPoint Point1; // avoid dynamic memory allocation overhead if we used new and delete
TPoint *pPoint1=&Point1;
TPoint Point2;
TPoint *pPoint2=&Point2;
fnKoord2Point(pPoint1,x1,y1);
fnKoord2Point(pPoint2,x2,y2);
if((Point1.x!=Point2.x || Point1.y!=Point2.y))
{// if line start and end are different then draw line
pBitmap->Canvas->PenPos=Point1;
pBitmap->Canvas->LineTo(Point2.x,Point2.y);
}
}
return;
}
//------------------------------------------------------------------------------
void TScientificGraph::fnPaint()
{
double fMinGrid, fMaxGrid; // double to allow effectively unlimited zooming
size_t iCount;
int i,j;
double dADoub;
double dX, dY; // even in inner loops these need to be doubles [ due to my extra clipping code]
char szAString[31],lasttick[31];
AnsiString AAnsiString;
TRect LayoutRect;
TSize ASize;
SGraph *pAGraph;
TPoint Point; // avoid dynamic memory allocation overhead if we used new and delete
TPoint *pPoint=&Point;
TPoint Point2; // avoid dynamic memory allocation overhead if we used new and delete
TPoint *pPoint2=&Point2;
// rprintf("fnPaint()\n");
fnCheckScales(); //check scales
// double xd=sScaleX.dMax-sScaleX.dMin; // total span
//Background
LayoutRect.Left = 0;
LayoutRect.Top = 0;
LayoutRect.Right = pBitmap->Width;
LayoutRect.Bottom = pBitmap->Height;
pBitmap->Canvas->Brush->Color = ColBackGround;
pBitmap->Canvas->FillRect(LayoutRect);
pBitmap->Canvas->Font->Name="Arial";
//Gridlines, Ticks, Ticklabels
double gridsize=dGridSizeX; // inital grid size
//x-axis
fMinGrid = ceil(sScaleX.dMin/gridsize); //min grid
fMaxGrid = floor(sScaleX.dMax/gridsize); //max grid
const char *tickformat="%.8g"; // default tick format
lasttick[0]=0; // zero length string to start so 1st label always printed
iCount=0; // nos ticks values skipped
// rprintf("xaxis fMinGrid=%g fMaxGrid=%g iCount=%d\n",fMinGrid, fMaxGrid,iCount);
//paint grids,ticks
// check if all labels will be the same in .8g format
snprintf(lasttick,sizeof(lasttick),"%.8g",fMinGrid*gridsize);
snprintf(szAString,sizeof(szAString),"%.8g",fMaxGrid*gridsize);
if(strncmp(szAString,lasttick,sizeof(szAString))==0 || fMinGrid>=fMaxGrid )
{// same display fewer points to higher accuracy
gridsize=5.0;
fMinGrid = ceil(sScaleX.dMin/gridsize); //min grid
fMaxGrid = floor(sScaleX.dMax/gridsize); //max grid
tickformat="%.10g"; // high resolution tick format [was .14g but this is rather excessive when values are only floats ]
}
lasttick[0]=0; // zero length string to start so 1st label always printed
// rprintf("xaxis fMinGrid=%g fMaxGrid=%g (max-min=%g) gridsize=%g fmt=%s\n",fMinGrid, fMaxGrid,fMaxGrid-fMinGrid,gridsize,tickformat);
// see how many points will be skipped
i=0;
/* # pragma's below work for gcc and clang compilers , issue is that format argument to snprintf (tickformat) is a variable */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
if(fMinGrid<fMaxGrid)
for (double fi = fMinGrid; fi<=fMaxGrid && i< 100; fi=fi+1.0,++i) // && i< 100 stops looping forever if we run out of resolution
{
dADoub = fi * gridsize; //x-value grid
snprintf(szAString,sizeof(szAString),tickformat,dADoub); //tick labels .8g is the largest that will fit with full grid
if(strncmp(szAString,lasttick,sizeof(szAString))!=0 || (fi+1.0>fMaxGrid && iCount<2))
{// new tick label different to previous one , or last one and only printed 1 previously
snprintf(lasttick,sizeof(szAString),"%s",szAString); // save this one as last printed , cannot use strncpy as that does not guarantee a null terminated string
}
else ++iCount; // point skipped
}
// now actually draw axes for real
lasttick[0]=0; // zero length string to start so 1st label always printed
if(fMinGrid<fMaxGrid && iCount<=1 && i<15) // if at most one point skipped and 14 plotted
{i=0;
iCount=0;
for (double fi = fMinGrid; fi<=fMaxGrid && i< 100 && iCount<15 ; fi=fi+1.0,++i) // && i< 100 stops looping forever if we run out of resolution
{
dADoub = fi * gridsize; //x-value grid
if (bGrids) fnPaintGridX(dADoub); //paint grids
fnPaintTickX(dADoub,1); //paint ticks
snprintf(szAString,sizeof(szAString),tickformat,dADoub); //tick labels .8g is the largest that will fit with full grid
if(strncmp(szAString,lasttick,sizeof(szAString))!=0 || (fi+1.0>fMaxGrid && iCount<2))
{// new tick label different to previous one , or last one and only printed 1 previously
++iCount;
AAnsiString=AnsiString(szAString);
fnKoord2Point(pPoint,dADoub,sScaleY.dMin);
ASize = pBitmap->Canvas->TextExtent(AAnsiString);
pBitmap->Canvas->Font->Color=ColText;
pBitmap->Canvas->Font->Size=iTextSize;
pBitmap->Canvas->TextOut(pPoint->x-ASize.cx/2,pPoint->y+iTextOffset,AAnsiString);
snprintf(lasttick,sizeof(szAString),"%s",szAString); // save this one as last printed , cannot use strncpy as that does not guarantee a null terminated string
}
}// end for
}
else
#if 1 /* double step so we can increase resolution of numbers */
{i=0;
iCount=0;
tickformat="%.10g";
for (double fi = fMinGrid; fi<=fMaxGrid && i< 100 && iCount<11 ; fi=fi+2.0,++i) // && i< 100 stops looping forever if we run out of resolution
{
dADoub = fi * gridsize; //x-value grid 2* what it was above
if (bGrids) fnPaintGridX(dADoub); //paint grids
fnPaintTickX(dADoub,1); //paint ticks
snprintf(szAString,sizeof(szAString),tickformat,dADoub); //tick labels .8g is the largest that will fit with full grid
if(strncmp(szAString,lasttick,sizeof(szAString))!=0 || (fi+2.0>fMaxGrid && iCount<2))
{// new tick label different to previous one , or last one and only printed 1 previously
++iCount;
AAnsiString=AnsiString(szAString);
fnKoord2Point(pPoint,dADoub,sScaleY.dMin);
ASize = pBitmap->Canvas->TextExtent(AAnsiString);
pBitmap->Canvas->Font->Color=ColText;
pBitmap->Canvas->Font->Size=iTextSize;
pBitmap->Canvas->TextOut(pPoint->x-ASize.cx/2,pPoint->y+iTextOffset,AAnsiString);
snprintf(lasttick,sizeof(szAString),"%s",szAString); // save this one as last printed , cannot use strncpy as that does not guarantee a null terminated string
}
}// end for
}
#else /* print 5 ticks values evenly spaced along x axis*/
{
for(double z=0;z<=1.000000001;z+=0.25) //location of tick 0,0.25,0.5,0.75,1, check at <=1.000000001 to allow for 1 with small rounding erros
{
dADoub = sScaleX.dMin+z*(sScaleX.dMax-sScaleX.dMin);
if (bGrids) fnPaintGridX(dADoub); //paint grids
fnPaintTickX(dADoub,1); //paint ticks
snprintf(szAString,sizeof(szAString),"%.10g",dADoub); //tick labels can add more resolution here as plenty of space
AAnsiString=AnsiString(szAString);
fnKoord2Point(pPoint,dADoub,sScaleY.dMin);
ASize = pBitmap->Canvas->TextExtent(AAnsiString);
pBitmap->Canvas->Font->Color=ColText;
pBitmap->Canvas->Font->Size=iTextSize;
pBitmap->Canvas->TextOutA(pPoint->x-2*ASize.cx/3,pPoint->y+iTextOffset,AAnsiString); // was -ASize/2
}
}
#endif
#pragma GCC diagnostic pop /* GCC diagnostic ignored "-Wformat-nonliteral" */
//y-axis
fMinGrid = ceil(sScaleY.dMin/dGridSizeY); //min grid
fMaxGrid = floor(sScaleY.dMax/dGridSizeY); //max grid
lasttick[0]=0; // zero length string to start so 1st label always printed
iCount=0; // nos ticks values printed
// rprintf("yaxis fMinGrid=%g fMaxGrid=%g iCount=%d\n",fMinGrid, fMaxGrid,iCount);
//paint grids
i=0;
for (double fi = fMinGrid; fi<=fMaxGrid && i< 100 ; fi=fi+1.0,++i) // && i< 100 stops looping forever if we run out of resolution
{
dADoub = fi * dGridSizeY; //y-value grid
if (bGrids) fnPaintGridY(dADoub); //paint grids
fnPaintTickY(dADoub,1); //paint ticks
#ifdef USE_double_to_str_exp
/* void double_to_str_exp(double x, int sf,enum fpout_type out_type, int len, char *s) // sf = significant figures - max 19 */
{int sf=12; // have space for at most 12 chars
if(dADoub<0) sf--; // negative sign takes 1
do{// try until it fits in <= 12 chars
double_to_str_exp(dADoub,sf--,round_nearest | fmt_g,sizeof(szAString),szAString);
}
while(strlen(szAString)>12 && sf>1); // && sf>1 traps infinite loop (just in case!)
}
#else
// gcvt(dADoub,10, szAString); // max 12 characters so 10 digits plus sign, dp
{int sf=12; // have space for at most 12 chars
if(dADoub<0) sf--; // negative sign takes 1
do{// try until it fits in <= 12 chars
gcvt(dADoub,sf--,szAString);
}
while(strlen(szAString)>12 && sf>1); // && sf>1 traps infinite loop (just in case!)
}
#endif
if(strncmp(szAString,lasttick,sizeof(szAString))!=0 || (fi+1.0>fMaxGrid && iCount<2))
{ // new tick label different to previous one , or last one and only printed 1 previously
++iCount;
AAnsiString=AnsiString(szAString);
fnKoord2Point(pPoint,sScaleX.dMin,dADoub);
ASize = pBitmap->Canvas->TextExtent(AAnsiString);
pBitmap->Canvas->Font->Color=ColText;
pBitmap->Canvas->Font->Size=iTextSize;
pBitmap->Canvas->TextOut(pPoint->x-ASize.cx-iTextOffset,pPoint->y-ASize.cy/2,AAnsiString);
snprintf(lasttick,sizeof(szAString),"%s",szAString); // save this one as last printed , cannot use strncpy as that does not guarantee a null terminated string
}
}
// rprintf("yaxis2 fMinGrid=%g fMaxGrid=%g iCount=%d\n",fMinGrid, fMaxGrid,iCount);
if(iCount==0)
{ // zoomed too much so no values printed above - just print 1 value here (mid)
dADoub = 0.5* (sScaleY.dMin+sScaleY.dMax); //y-value grid - just mid point
if(fMinGrid==fMaxGrid)
{ // add tick/grid line as code above will not have printed one
if (bGrids) fnPaintGridY(dADoub); //paint grids
fnPaintTickY(dADoub,1); //paint ticks
}
#ifdef USE_double_to_str_exp
/* void double_to_str_exp(double x, int sf,enum fpout_type out_type, int len, char *s) // sf = significant figures - max 19 */
{int sf=12; // have space for at most 12 chars
if(dADoub<0) sf--; // negative sign takes 1
do{// try until it fits in <= 12 chars
double_to_str_exp(dADoub,sf--,round_nearest | fmt_g,sizeof(szAString),szAString);
}
while(strlen(szAString)>12 && sf>1); // && sf>1 traps infinite loop (just in case!)
}
#else
{int sf=12; // have space for at most 12 chars
if(dADoub<0) sf--; // negative sign takes 1
do{// try until it fits in <= 12 chars
gcvt(dADoub,sf--,szAString);
}
while(strlen(szAString)>12 && sf>1); // && sf>1 traps infinite loop (just in case!)
}
#endif
AAnsiString=AnsiString(szAString);
fnKoord2Point(pPoint,sScaleX.dMin,dADoub);
ASize = pBitmap->Canvas->TextExtent(AAnsiString);
pBitmap->Canvas->Font->Color=ColText;
pBitmap->Canvas->Font->Size=iTextSize;
pBitmap->Canvas->TextOut(pPoint->x-ASize.cx-iTextOffset,pPoint->y-ASize.cy/2,AAnsiString);
}
//Zeroline
if (!bGrids & bZeroLine) //zeroline not necess. if
//grids are enabled
if (fnInScaleY(0)) //zeroline in plot?
{
fnKoord2Point(pPoint,sScaleX.dMin,0); //paint line in gridstyle
pBitmap->Canvas->PenPos=*pPoint;
fnKoord2Point(pPoint,sScaleX.dMax,0);
pBitmap->Canvas->Pen->Color = ColGrid;
pBitmap->Canvas->Pen->Width = iPenWidthGrid;
pBitmap->Canvas->Pen->Style = PSGrid;
pBitmap->Canvas->LineTo(pPoint->x,pPoint->y);
}
#if 0 /* set to 1 to print legend 1st, before traces (means it may not be visible) */
//Legend
//calc position
dX=dLegendStartX*(sScaleX.dMax
-sScaleX.dMin)+sScaleX.dMin
;
dY=dLegendStartY*(sScaleY.dMax
-sScaleY.dMin)+sScaleY.dMin
;
fnKoord2Point(pPoint,dX,dY); //calc. coordinat.
pBitmap->Canvas->Font->Size=iTextSize;
for (j=0; j<iNumberOfGraphs; j++) //all graphs
{
pAGraph = ((SGraph*) pHistory->Items[j]);
if (pAGraph->Caption!="")
{
*pPoint2=*pPoint;
if (((pAGraph->ucStyle) & 1) == 1) //paint data point
{ //for legend
i=pBitmap->Canvas->TextHeight(pAGraph->Caption);
i/=2;
pPoint->y+=i;
pPoint->x+=pBitmap->Canvas->TextWidth("22");
LayoutRect.Left=(pPoint->x)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Right=(pPoint->x)+(pAGraph->iSizeDataPoint/2);
LayoutRect.Top=(pPoint->y)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Bottom=(pPoint->y)+(pAGraph->iSizeDataPoint/2);
pBitmap->Canvas->Pen->Width=1;
pBitmap->Canvas->Pen->Color=pAGraph->ColDataPoint;
pBitmap->Canvas->Pen->Style=psSolid;
pBitmap->Canvas->Brush->Color=pAGraph->ColDataPoint;
fnPaintDataPoint(LayoutRect,pAGraph->ucPointStyle);
if (((pAGraph->ucStyle) & 4) == 4)
{
*pPoint=*pPoint2;
}
else
{
pPoint->y-=i;
pPoint->x+=pBitmap->Canvas->TextWidth("333");
pBitmap->Canvas->Font->Color=pAGraph->ColDataPoint;
}
}
if (((pAGraph->ucStyle) & 4) == 4) //paint short line
{ //for legend
i=pBitmap->Canvas->TextHeight(pAGraph->Caption);
i/=2;
pPoint->y+=i;
pBitmap->Canvas->PenPos=*pPoint;
pPoint->x+=pBitmap->Canvas->TextWidth("4444");
pBitmap->Canvas->Pen->Width=pAGraph->iWidthLine;;
pBitmap->Canvas->Pen->Color=pAGraph->ColLine;
pBitmap->Canvas->Pen->Style=pAGraph->LineStyle;
pBitmap->Canvas->LineTo(pPoint->x,pPoint->y);
pPoint->y-=i;
pPoint->x+=pBitmap->Canvas->TextWidth("1");
pBitmap->Canvas->Font->Color=pAGraph->ColLine;
}
pBitmap->Canvas->Font->Size=iTextSize; //paint caption
pBitmap->Canvas->TextOut(pPoint->x,pPoint->y,Utf8_to_w(pAGraph->Caption));
*pPoint=*pPoint2;
if ((pAGraph->iSizeDataPoint>pBitmap->Canvas->TextHeight("0"))&&
(((pAGraph->ucStyle) & 1) == 1))
pPoint->y+=pAGraph->iSizeDataPoint+5;
else pPoint->y+=pBitmap->Canvas->TextHeight("0");
}
}
#endif
//Clip Rect
HRGN MyRgn;
fnKoord2Point(pPoint2,sScaleX.dMax,sScaleY.dMax);
fnKoord2Point(pPoint,sScaleX.dMin,sScaleY.dMin);
// rprintf("Clipping region from X=%.20g Y=%.20g to X=%.20g Y=%.20g\n",(double)(pPoint2->x),(double)(pPoint2->y),(double)(pPoint->x),(double)(pPoint->y));
double x_width_pixels= (double)(pPoint2->x)-(double)(pPoint->x);
if(x_width_pixels<0) x_width_pixels= -x_width_pixels;
#if 1
MyRgn = ::CreateRectRgn(pPoint2->x,pPoint2->y,pPoint->x,pPoint->y);
#else
MyRgn = ::CreateRectRgn(pPoint->x,pPoint2->y,pPoint2->x,pPoint->y);
#endif
::SelectClipRgn(pBitmap->Canvas->Handle,MyRgn);
//Data points and Error Bars, lines
for (j = 0; j < iNumberOfGraphs; j++)
{
pAGraph = ((SGraph*) pHistory->Items[j]);
if (((pAGraph->ucStyle) & 1) == 1) //style: data point
{ unsigned int istep;
/* new, more intelligent way to display points
if we have to skip points, show min/max by an vertical "error bar"
so that range is obvious
If you zoom in enough just points are shown
*/
DOUBLE ymax,ymin,x_ymax,x_ymin; // used to capture features in skipped data
DOUBLE lastx,lasty;
double xd=sScaleX.dMax-sScaleX.dMin; // total span
double xi=xd/x_width_pixels; // use all the pixels available
xd=sScaleX.dMin-xi; // -xi as add xi before its used
iCount=pAGraph->nos_vals ;
#if 1
// do binary search to find start of area thats visible on the screen
ssize_t starti; // index just before start
{
ssize_t low=0;
ssize_t high=(ssize_t)iCount-1;
bool found=false;
double key=sScaleX.dMin; // needs to be double as otherwise compare midval<key can generate an overflow if dMin -? dMax is a very large range
ssize_t mid=0;
while(low<=high && !found)
{mid=low+((high-low)>>1); /* (low+high)/2 but written so cannot overflow */
double midVal=pAGraph->x_vals[mid];
if(midVal<key)
low=mid+1;
else if (midVal>key)
high=mid-1;
else
found=true; // mid is exact match
}
if(found) starti=mid;
else starti=low-1; // not found want 1 before
if(starti<0) starti=0; // ensure not before start (don't worry if its past the end as for loop below deals with that case)
}
for (size_t ii=(size_t)starti; ii<iCount; ii++) // was i+=step
#else
for (size_t ii=0; ii<iCount; ii++) // was i+=step
#endif
{
dX = pAGraph->x_vals[ii];
if(!fnInScaleX(dX))
{if(dX> sScaleX.dMax) break; // past end so all done for this trace
else continue; // PMi optimisation - skip values before xmin
// this is important when zooming in as otherwise code below will see the whole file and will "compress" the graph incorrectly
}
ymax=ymin=pAGraph->y_vals[ii];// dY
x_ymax=x_ymin=(float)dX;
if(zoom_fun_level)
{Application->ProcessMessages(); /* allow windows to update (but not go idle) - potentially causes recursion ! */
if(zoom_fun_level>1)
goto fnpaint_end;// > 1 means we have recursion , abort present update as we need to do another with different scaling
}
// we know scaling so we can calculate how many points we need to skip
xd+=xi; // this works better when "skip equal y values is set" as x values are not then evenly spaced and this way points selected are evenly spaced
dX = pAGraph->x_vals[ii]; // dX,dY is 1st point examined, lastx,lasty is last point in this "segment"
dY = pAGraph->y_vals[ii];
for(istep=1;ii+istep<iCount && pAGraph->x_vals[ii+istep]<xd ;++istep)
{lastx = pAGraph->x_vals[ii+istep];
lasty = pAGraph->y_vals[ii+istep];
if(lasty>ymax) {ymax=lasty;x_ymax=lastx;}
if(lasty<ymin) {ymin=lasty;x_ymin=lastx;}
}
ii+=istep-1;
bool show_main_pt=false;
if (fnKoord2Point(pPoint,dX,dY))
{ show_main_pt=true;
LayoutRect.Left=(pPoint->x)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Right=(pPoint->x)+(pAGraph->iSizeDataPoint/2);
LayoutRect.Top=(pPoint->y)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Bottom=(pPoint->y)+(pAGraph->iSizeDataPoint/2);
pBitmap->Canvas->Pen->Width=1;
pBitmap->Canvas->Pen->Color=pAGraph->ColDataPoint;
pBitmap->Canvas->Pen->Style=psSolid;
pBitmap->Canvas->Brush->Color=pAGraph->ColDataPoint;
fnPaintDataPoint(LayoutRect,pAGraph->ucPointStyle);
}
if (ymin!=ymax) // draw points at min and max (so we have 3 vertical points) [assuming they are far enough away to show ]
{
if( fnKoord2Point(pPoint2,x_ymin,ymin) && (!show_main_pt ||
(abs(pPoint->x-pPoint2->x)+ abs(pPoint->y-pPoint2->y) >iSkipLineLevel))
)
{ // if point is far enough away to be worthwhile drawing
LayoutRect.Left=(pPoint2->x)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Right=(pPoint2->x)+(pAGraph->iSizeDataPoint/2);
LayoutRect.Top=(pPoint2->y)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Bottom=(pPoint2->y)+(pAGraph->iSizeDataPoint/2);
pBitmap->Canvas->Pen->Width=1;
pBitmap->Canvas->Pen->Color=pAGraph->ColDataPoint;
pBitmap->Canvas->Pen->Style=psSolid;
pBitmap->Canvas->Brush->Color=pAGraph->ColDataPoint;
fnPaintDataPoint(LayoutRect,pAGraph->ucPointStyle);
}
if( fnKoord2Point(pPoint2,x_ymax,ymax) && (!show_main_pt ||
(abs(pPoint->x-pPoint2->x)+ abs(pPoint->y-pPoint2->y) >iSkipLineLevel))
)
{ // if point is far enough away to be worthwhile drawing
LayoutRect.Left=(pPoint2->x)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Right=(pPoint2->x)+(pAGraph->iSizeDataPoint/2);
LayoutRect.Top=(pPoint2->y)-(pAGraph->iSizeDataPoint/2);
LayoutRect.Bottom=(pPoint2->y)+(pAGraph->iSizeDataPoint/2);
pBitmap->Canvas->Pen->Width=1;
pBitmap->Canvas->Pen->Color=pAGraph->ColDataPoint;
pBitmap->Canvas->Pen->Style=psSolid;
pBitmap->Canvas->Brush->Color=pAGraph->ColDataPoint;
fnPaintDataPoint(LayoutRect,pAGraph->ucPointStyle);
}
} // end if error "bar"
} // end for(ii)
} // end if (((pAGraph->ucStyle) & 1) == 1) (if style: data point)
if (((pAGraph->ucStyle)&4)==4) //style: line
{ unsigned int istep;
bool first=True; // used to trap start and end of region we wish to view (when zoomed)
bool last=False;
DOUBLE ymax,ymin;
double x_ymax,x_ymin; // used to capture features in skipped data (need to be double due to clipping)
DOUBLE lastx,lasty;
iCount=pAGraph->nos_vals;
pBitmap->Canvas->Pen->Width=pAGraph->iWidthLine;
pBitmap->Canvas->Pen->Color=pAGraph->ColLine;
pBitmap->Canvas->Pen->Style=pAGraph->LineStyle;
//iCount=pAList->Count;
if (iCount!=0)
{
dX = pAGraph->x_vals[0];
dY = pAGraph->y_vals[0];
fnKoord2Point(pPoint,dX,dY);
pBitmap->Canvas->PenPos=*pPoint;
*pPoint2=*pPoint;
}
double xd=sScaleX.dMax-sScaleX.dMin; // total span
double xi=xd/x_width_pixels; // use all the pixels available
xd=sScaleX.dMin-xi; // -xi as add xi before its used
#if 1
// do binary search to find start of area thats visible on the screen
ssize_t starti; // index just before start
{
ssize_t low=0;
ssize_t high=(ssize_t)iCount-1;
bool found=false;
double key=sScaleX.dMin; // without this being a double we get floating point overflow on midVal<key below when dMin->dMax is very large
ssize_t mid=0;
while(low<=high && !found)
{mid=low+((high-low)>>1); /* (low+high)/2 but written so cannot overflow */
double midVal=pAGraph->x_vals[mid];
if(midVal<key)
low=mid+1;
else if (midVal>key)
high=mid-1;
else
found=true; // mid is exact match
}
if(found) starti=mid;
else starti=low-1; // not found want 1 before
if(starti<0) starti=0; // ensure not before start (don't worry if its past the end as for loop below deals with that case)
}
for (size_t ii=(size_t)starti; ii<iCount; ii++) // start processing just where we need to.
#else
for (size_t ii=0; ii<iCount; ii++) // linear search from start
#endif
{
dX = pAGraph->x_vals[ii];
if(first && dX >sScaleX.dMin)
{ // 1st point after min x value - want to process this
}
else if(last==false && dX>sScaleX.dMax)
{ // 1st point after max x value - also want to process this
last=true;
}
else if(!fnInScaleX(dX))
{if(dX> sScaleX.dMax) break; // past end so all done for this trace
else continue; // PMi optimisation - skip values before xmin
// this is important when zooming in as otherwise code below will see the whole file and will "compress" the graph incorrectly
}
if(first)
{// first point to be displayed - need to define start of the 1st line
if(ii>0)
{xs= pAGraph->x_vals[ii-1];
ys =pAGraph->y_vals[ii-1];
}
else
{xs= pAGraph->x_vals[0];
ys = pAGraph->y_vals[0] ;
}
}
dX = pAGraph->x_vals[ii];
dY = pAGraph->y_vals[ii] ;
ymax=ymin=(float)dY;
x_ymin=x_ymax=(float)dX;
if(zoom_fun_level)
{Application->ProcessMessages(); /* allow windows to update (but not go idle) - potentially causes recursion ! */
if(zoom_fun_level>1)
goto fnpaint_end;// > 1 means we have recursion , abort present update as we need to do another with different scaling
}
// we know scaling so we can calculate how many points we need to skip
xd+=xi; // this works better when "skip equal y values is set" as x values are not then evenly spaced and this way points selected are evenly spaced
lastx=(float)dX ; // dX,dY is 1st point examined, lastx,lasty is last point in this "segment"
lasty=(float)dY ;
for(istep=1;ii+istep<iCount && pAGraph->x_vals[ii+istep]<xd ;++istep)
{lastx = pAGraph->x_vals[ii+istep];
lasty = pAGraph->y_vals[ii+istep];
if(lasty>ymax) {ymax=lasty;x_ymax=lastx;}
if(lasty<ymin) {ymin=lasty;x_ymin=lastx;}
}
ii+=istep-1;
{
if(x_ymin>x_ymax)
{double tx,ty; // swap around to make sure we stay in order of increasing x
tx=x_ymin;ty=ymin;
x_ymin=x_ymax; ymin=ymax;
x_ymax=tx; ymax=(float)ty;
}
// graph_line(double xe,double ye,double xmin,double xmax,double ymin,double ymax)
graph_line(x_ymin,ymin,sScaleX.dMin,sScaleX.dMax,sScaleY.dMin,sScaleY.dMax); // draw line (clipped)
if(ymin!=ymax)
{// print min and max if they are different (only happens when points skipped)
graph_line(x_ymax,ymax,sScaleX.dMin,sScaleX.dMax,sScaleY.dMin,sScaleY.dMax); // draw line (clipped)
}
if(lasty!=ymax)
{// end point of this region (if different)
graph_line(lastx,lasty,sScaleX.dMin,sScaleX.dMax,sScaleY.dMin,sScaleY.dMax); // draw line (clipped)
}
}
first=False;
}
}