Skip to content

Commit 4198b23

Browse files
committed
Bar plotting functionality
1 parent 7344ded commit 4198b23

File tree

4 files changed

+188
-71
lines changed

4 files changed

+188
-71
lines changed

include/OpenCVGUI/OGUIPlotArea.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class OGUIPlotArea : public OGUIArea {
4141
void drawLinePlot();
4242
void drawSeriesPlot();
4343
void drawScatterPlot();
44+
void drawBarPlot();
45+
void drawLegends();
4446
void drawPlot();
4547
Mat data;
4648
bool data_lock, is_drawing;

src/OpenCVGUI/OGUIArea.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,19 @@ OGUIArea::OGUIArea(OGUIWindow* window)
9494
this->layout= NULL;
9595
this->window= window;
9696
isMaximized= false;
97+
9798
r= 28;
9899
g= 30;
99100
b= 34;
101+
/*
102+
r= 76;
103+
g= 82;
104+
b= 89;
105+
106+
r= 56;
107+
g= 62;
108+
b= 69;
109+
*/
100110
/*r= rand()%255;
101111
g= rand()%255;
102112
b= rand()%255;*/

src/OpenCVGUI/OGUIPlotArea.cpp

Lines changed: 173 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ namespace OpenCVGUI {
7676
}
7777
}
7878

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+
79107
void OGUIPlotArea::drawSeriesPlot(){
80108
float margin= 120;
81109
float start_x= x+margin/2.0f;
@@ -162,31 +190,7 @@ namespace OpenCVGUI {
162190
}
163191
nvgScissor(vg, x, y, width, height);
164192

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();
190194

191195
// X axis
192196
nvgBeginPath(vg);
@@ -354,29 +358,7 @@ namespace OpenCVGUI {
354358
}
355359
nvgScissor(vg, x, y, width, height);
356360

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();
380362

381363
// X axis
382364
nvgBeginPath(vg);
@@ -528,29 +510,7 @@ namespace OpenCVGUI {
528510
}
529511
nvgScissor(vg, x, y, width, height);
530512

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();
554514

555515
// X axis
556516
nvgBeginPath(vg);
@@ -611,6 +571,145 @@ namespace OpenCVGUI {
611571

612572
}
613573

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+
614713
void OGUIPlotArea::drawPlot() {
615714

616715
switch (_plot_type){
@@ -623,6 +722,9 @@ namespace OpenCVGUI {
623722
case PLOT_SCATTER:
624723
drawScatterPlot();
625724
break;
725+
case PLOT_BARS:
726+
drawBarPlot();
727+
break;
626728
}
627729

628730

src/example_2dplot.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ void cv_process() {
101101

102102
window->plot2D("2D plot Lines", &lines_data, {"star","circle"}, 1, OpenCVGUI::PLOT_LINE);
103103

104+
resize(data, data, Size(20,3));
105+
window->plot2D("2D plot Bars", &data, {"g1","g2","g3"}, 1, OpenCVGUI::PLOT_BARS);
106+
104107
if(sayHello) {
105108
window->popup("2D Plot Demo", "In this sample you can check different 2d plot types", POPUP_ALERT);
106109
sayHello= false;

0 commit comments

Comments
 (0)