@@ -76,6 +76,34 @@ namespace OpenCVGUI {
76
76
}
77
77
}
78
78
79
+ void OGUIPlotArea::drawLegends (){
80
+ NVGcontext* vg= (NVGcontext*)(window->vg );
81
+ // /Plot labels box
82
+ nvgBeginPath (vg);
83
+ nvgRect (vg, x + width - 100 -20 , y + 40 , 120 , 15 + data.rows *20 );
84
+ nvgFillColor (vg, nvgRGBA (0 , 0 , 0 , 100 ));
85
+ nvgFill (vg);
86
+ for (int p=0 ; p<data.rows ; p++) {
87
+ int c = p * 3 ;
88
+ NVGcolor color = nvgRGBA (color_scheme.at (c), color_scheme.at (c + 1 ), color_scheme.at (c + 2 ), 255 );
89
+ float dot_x=x + width - 100 ;
90
+ float dot_y=y + 60 + p*20 ;
91
+ nvgBeginPath (vg);
92
+ nvgCircle (vg, dot_x, dot_y, 4 .0f );
93
+ nvgFillColor (vg, color);
94
+ nvgFill (vg);
95
+
96
+ // Calculate text width for box
97
+ nvgFontSize (vg, 16 .0f );
98
+ nvgFontFace (vg, " sans" );
99
+ nvgTextAlign (vg, NVG_ALIGN_LEFT);
100
+
101
+ // Draw text
102
+ nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
103
+ nvgText (vg, dot_x + 10 , dot_y+3 , _labels.at (p).c_str (), NULL );
104
+ }
105
+ }
106
+
79
107
void OGUIPlotArea::drawSeriesPlot (){
80
108
float margin= 120 ;
81
109
float start_x= x+margin/2 .0f ;
@@ -162,31 +190,7 @@ namespace OpenCVGUI {
162
190
}
163
191
nvgScissor (vg, x, y, width, height);
164
192
165
- // /Plot labels box
166
- nvgBeginPath (vg);
167
- nvgRect (vg, x + width - 100 -20 , y + 40 , 120 , 15 + data.rows *20 );
168
- nvgFillColor (vg, nvgRGBA (0 , 0 , 0 , 100 ));
169
- nvgFill (vg);
170
- for (int p=0 ; p<data.rows ; p++) {
171
- int c = p * 3 ;
172
- NVGcolor color = nvgRGBA (color_scheme.at (c), color_scheme.at (c + 1 ), color_scheme.at (c + 2 ), 255 );
173
- float dot_x=x + width - 100 ;
174
- float dot_y=y + 60 + p*20 ;
175
- nvgBeginPath (vg);
176
- nvgCircle (vg, dot_x, dot_y, 4 .0f );
177
- nvgFillColor (vg, color);
178
- nvgFill (vg);
179
-
180
- // Calculate text width for box
181
- nvgFontSize (vg, 16 .0f );
182
- nvgFontFace (vg, " sans" );
183
- nvgTextAlign (vg, NVG_ALIGN_LEFT);
184
- // stringstream ss_value;
185
- // ss_value << _labels.at(p);
186
- // Draw text
187
- nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
188
- nvgText (vg, dot_x + 10 , dot_y+3 , _labels.at (p).c_str (), NULL );
189
- }
193
+ drawLegends ();
190
194
191
195
// X axis
192
196
nvgBeginPath (vg);
@@ -354,29 +358,7 @@ namespace OpenCVGUI {
354
358
}
355
359
nvgScissor (vg, x, y, width, height);
356
360
357
- // /Plot labels box
358
- nvgBeginPath (vg);
359
- nvgRect (vg, x + width - 100 -20 , y + 40 , 120 , 15 + data.rows *20 );
360
- nvgFillColor (vg, nvgRGBA (0 , 0 , 0 , 100 ));
361
- nvgFill (vg);
362
- for (int p=0 ; p<data.rows ; p++) {
363
- int c = p * 3 ;
364
- NVGcolor color = nvgRGBA (color_scheme.at (c), color_scheme.at (c + 1 ), color_scheme.at (c + 2 ), 255 );
365
- float dot_x=x + width - 100 ;
366
- float dot_y=y + 60 + p*20 ;
367
- nvgBeginPath (vg);
368
- nvgCircle (vg, dot_x, dot_y, 4 .0f );
369
- nvgFillColor (vg, color);
370
- nvgFill (vg);
371
-
372
- // Calculate text width for box
373
- nvgFontSize (vg, 16 .0f );
374
- nvgFontFace (vg, " sans" );
375
- nvgTextAlign (vg, NVG_ALIGN_LEFT);
376
- // Draw text
377
- nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
378
- nvgText (vg, dot_x + 10 , dot_y+3 , _labels.at (p).c_str (), NULL );
379
- }
361
+ drawLegends ();
380
362
381
363
// X axis
382
364
nvgBeginPath (vg);
@@ -528,29 +510,7 @@ namespace OpenCVGUI {
528
510
}
529
511
nvgScissor (vg, x, y, width, height);
530
512
531
- // /Plot labels box
532
- nvgBeginPath (vg);
533
- nvgRect (vg, x + width - 100 -20 , y + 40 , 120 , 15 + data.rows *20 );
534
- nvgFillColor (vg, nvgRGBA (0 , 0 , 0 , 100 ));
535
- nvgFill (vg);
536
- for (int p=0 ; p<data.rows ; p++) {
537
- int c = p * 3 ;
538
- NVGcolor color = nvgRGBA (color_scheme.at (c), color_scheme.at (c + 1 ), color_scheme.at (c + 2 ), 255 );
539
- float dot_x=x + width - 100 ;
540
- float dot_y=y + 60 + p*20 ;
541
- nvgBeginPath (vg);
542
- nvgCircle (vg, dot_x, dot_y, 4 .0f );
543
- nvgFillColor (vg, color);
544
- nvgFill (vg);
545
-
546
- // Calculate text width for box
547
- nvgFontSize (vg, 16 .0f );
548
- nvgFontFace (vg, " sans" );
549
- nvgTextAlign (vg, NVG_ALIGN_LEFT);
550
- // Draw text
551
- nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
552
- nvgText (vg, dot_x + 10 , dot_y+3 , _labels.at (p).c_str (), NULL );
553
- }
513
+ drawLegends ();
554
514
555
515
// X axis
556
516
nvgBeginPath (vg);
@@ -611,6 +571,145 @@ namespace OpenCVGUI {
611
571
612
572
}
613
573
574
+ void OGUIPlotArea::drawBarPlot (){
575
+ float margin= 120 ;
576
+ float start_x= x+margin/2 .0f ;
577
+ float x_length= data.cols ;
578
+ float dx= (width-margin)/(x_length);
579
+ float bar_margin=40 .0f ;
580
+ float bar_width= (width-margin-data.rows *bar_margin)/(x_length*data.rows );
581
+ float m=_max_value;
582
+ if (_auto_scale) {
583
+ minMaxLoc (data, &_min_value, &_max_value, NULL , NULL );
584
+ // float m= (_max_value-_min_value);
585
+ m= (_max_value);
586
+ }
587
+ float start_y= y+height-margin/2 .0f ;
588
+ float dy= (height-margin);
589
+
590
+
591
+ NVGcontext* vg= (NVGcontext*)(window->vg );
592
+
593
+ // Plot each graph
594
+ for (int p=0 ; p<data.rows ; p++) {
595
+ int c=p*3 ;
596
+ NVGcolor color= nvgRGBA (color_scheme.at (c), color_scheme.at (c+1 ), color_scheme.at (c+2 ), 255 );
597
+
598
+ // Bars
599
+ for (int i = 0 ; i < data.cols ; i++) {
600
+ float dot_x=start_x + i * dx + bar_width * p;
601
+ float dot_y=start_y - dy * ((data.at <float >(p,i)) / m);
602
+
603
+ nvgBeginPath (vg);
604
+ nvgRect (vg, dot_x, dot_y, bar_width, start_y-dot_y);
605
+ nvgFillColor (vg, color);
606
+ nvgFill (vg);
607
+
608
+ }
609
+ }
610
+
611
+ nvgResetScissor (vg);
612
+ // Plot hover labels
613
+ for (int p=0 ; p<data.rows ; p++) {
614
+ int c=p*3 ;
615
+ NVGcolor color= nvgRGBA (color_scheme.at (c), color_scheme.at (c+1 ), color_scheme.at (c+2 ), 255 );
616
+ // Dots
617
+ for (int i = 0 ; i < data.cols ; i++) {
618
+ float dot_x=start_x + i * dx;
619
+ float dot_y=start_y - dy * ((data.at <float >(p,i)) / m);
620
+ // Draw hover label
621
+ if (isMouseIn ()) {
622
+ if ( dot_y-4 <= window->mouse_y && window->mouse_y <= dot_y+4 &&
623
+ dot_x-4 <= window->mouse_x && window->mouse_x <= dot_x+4 ){
624
+
625
+ // Calculate text width for box
626
+ nvgFontSize (vg, 16 .0f );
627
+ nvgFontFace (vg, " sans" );
628
+ nvgTextAlign (vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
629
+ stringstream ss_value;
630
+ ss_value.precision (4 );
631
+ ss_value << _labels.at (p) <<" : " << (i*_x_step) << " , " <<data.at <float >(p,i);
632
+ float tw = nvgTextBounds (vg, 0 , 0 , ss_value.str ().c_str (), NULL , NULL );
633
+
634
+ nvgBeginPath (vg);
635
+ nvgRect (vg, dot_x-tw-20 , dot_y-32 , tw+20 , 32 );
636
+ nvgFillColor (vg, nvgRGBA (0 , 0 , 0 , 200 ));
637
+ nvgFill (vg);
638
+
639
+ // Draw text
640
+ nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
641
+ nvgText (vg, dot_x - tw - 10 , dot_y - 16 , ss_value.str ().c_str (), NULL );
642
+ }
643
+ }
644
+ }
645
+ }
646
+ nvgScissor (vg, x, y, width, height);
647
+
648
+ drawLegends ();
649
+
650
+ // X axis
651
+ nvgBeginPath (vg);
652
+ nvgMoveTo (vg, start_x, start_y);
653
+ nvgLineTo (vg, start_x+width-margin, start_y);
654
+ nvgStrokeColor (vg, nvgRGBA (255 ,255 ,255 ,255 ));
655
+ nvgStrokeWidth (vg, 1 .0f );
656
+ nvgStroke (vg);
657
+
658
+ // Lines x axis coord
659
+ for (int i=1 ; i<data.cols ; i++){
660
+ int s=5 ;
661
+ if ((i%s)==0 ) {
662
+ nvgBeginPath (vg);
663
+ nvgMoveTo (vg, start_x + i * dx, start_y);
664
+ nvgLineTo (vg, start_x + i * dx, start_y + 5 );
665
+ nvgStrokeColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
666
+ nvgStrokeWidth (vg, 1 .0f );
667
+ nvgStroke (vg);
668
+
669
+ // Text label
670
+ stringstream ss;
671
+ ss << i * _x_step;
672
+ nvgFontSize (vg, 12 .0f );
673
+ nvgFontFace (vg, " sans" );
674
+ float tw = nvgTextBounds (vg, 0 , 0 , ss.str ().c_str (), NULL , NULL );
675
+ nvgTextAlign (vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
676
+ nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
677
+ nvgText (vg, start_x + i * dx, start_y + 10 , ss.str ().c_str (), NULL );
678
+ }
679
+ }
680
+
681
+ // Y axis
682
+ nvgBeginPath (vg);
683
+ nvgMoveTo (vg, start_x, start_y);
684
+ nvgLineTo (vg, start_x, start_y-dy);
685
+ nvgStrokeColor (vg, nvgRGBA (255 ,255 ,255 ,255 ));
686
+ nvgStrokeWidth (vg, 1 .0f );
687
+ nvgStroke (vg);
688
+
689
+ // Lines y axis coord
690
+ for (int i=1 ; i<=10 ; i++){
691
+ nvgBeginPath (vg);
692
+ nvgMoveTo (vg, start_x, start_y-(i/10 .0f )*dy);
693
+ nvgLineTo (vg, start_x-5 , start_y-(i/10 .0f )*dy);
694
+ nvgStrokeColor (vg, nvgRGBA (255 ,255 ,255 ,255 ));
695
+ nvgStrokeWidth (vg, 1 .0f );
696
+ nvgStroke (vg);
697
+
698
+ // Text label
699
+ stringstream ss;
700
+ ss.precision (2 );
701
+ ss << (i/10 .0f )*m;
702
+ nvgFontSize (vg, 12 .0f );
703
+ nvgFontFace (vg, " sans" );
704
+ float tw = nvgTextBounds (vg, 0 , 0 , ss.str ().c_str (), NULL , NULL );
705
+ nvgTextAlign (vg, NVG_ALIGN_RIGHT | NVG_ALIGN_MIDDLE);
706
+ nvgFillColor (vg, nvgRGBA (255 , 255 , 255 , 255 ));
707
+ nvgText (vg, start_x-10 , start_y-(i/10 .0f )*dy, ss.str ().c_str (), NULL );
708
+ }
709
+ }
710
+
711
+
712
+
614
713
void OGUIPlotArea::drawPlot () {
615
714
616
715
switch (_plot_type){
@@ -623,6 +722,9 @@ namespace OpenCVGUI {
623
722
case PLOT_SCATTER:
624
723
drawScatterPlot ();
625
724
break ;
725
+ case PLOT_BARS:
726
+ drawBarPlot ();
727
+ break ;
626
728
}
627
729
628
730
0 commit comments