@@ -9,7 +9,8 @@ final float AXIS_COVERAGE = 0.75;
99final int LABEL_SZ = 16 ; // text sizes
1010final int TITLE_SZ = 20 ;
1111final int NUM_SZ = 12 ;
12- final int BG_COL = 75 ;
12+ final int MARGIN_SZ = 20 ; // between plots
13+ final int BG_COL = 75 ; //
1314final int PLOT_COL = 115 ;
1415final int TICK_LEN = 6 ;
1516final int NUM_TICKS = 4 ;
@@ -80,29 +81,18 @@ void plot_xy(int graph_index) {
8081 double y_scale = AXIS_COVERAGE * sub_height / (extremes_graphs[g][3 ] - extremes_graphs[g][2 ]);
8182 double y_offset = y_scale* extremes_graphs[g][3 ] + 0.5 * (1.0 - AXIS_COVERAGE )* sub_height;
8283
83- // Drawing setup
84+ // Plot Background
8485 fill (PLOT_COL );
8586 stroke (255 );
8687 rect (pos_graphs[g][0 ],pos_graphs[g][1 ],sub_width,sub_height);
87-
88- // Label graph
89- stroke (255 );
88+ // Title
89+ textSize (TITLE_SZ );
9090 fill (255 );
91- textSize (NUM_SZ );
92- float temp_x = pos_graphs[g][0 ] - TICK_LEN / 2 ;
93- float tick_offset = 0.5 * (1.0 - AXIS_COVERAGE )* sub_height;
94- float val = (float )extremes_graphs[g][3 ];
95- float val_interval = (float )(extremes_graphs[g][3 ] - extremes_graphs[g][2 ]) / (NUM_TICKS - 1 );
96- for (float temp_y = pos_graphs[g][1 ] + tick_offset;
97- temp_y <= pos_graphs[g][1 ] + sub_height - tick_offset;
98- temp_y += AXIS_COVERAGE * sub_height / (NUM_TICKS - 1 )) {
99- line (temp_x, temp_y, temp_x + TICK_LEN , temp_y);
100- text (Float . toString(val), temp_x + TICK_LEN + 1 , temp_y + NUM_SZ / 2 );
101- val -= val_interval;
102- }
103- // float temp_y = pos_graphs[g][1] + sub_height + TICK_LEN/2;
91+ textAlign (CENTER , TOP );
92+ text (titles[g], pos_graphs[g][0 ] + sub_width/ 2 , pos_graphs[g][1 ] + TITLE_SZ );
93+
94+ drawTicks(g);
10495
105- // line(temp_x, temp_x + TICK_LEN, pos_graphs[g][1] + y_offset, pos_graphs[g][1] + y_offset
10696 // ** add support for multiple paths in x-y here **
10797 stroke (COLORS [0 ]);
10898 for (int j = 0 ; j < num_points[g]; j++ ) {
@@ -122,16 +112,25 @@ void plot_time(int graph_index) {
122112 double y_scale = AXIS_COVERAGE * sub_height / (extremes_graphs[g][3 ] - extremes_graphs[g][2 ]);
123113 double y_offset = y_scale* extremes_graphs[g][3 ] + 0.5 * (1.0 - AXIS_COVERAGE )* sub_height;
124114
125- // Drawing setup
126- fill (115 );
115+ // Plot Background
116+ fill (PLOT_COL );
127117 stroke (255 );
128118 rect (pos_graphs[g][0 ],pos_graphs[g][1 ],sub_width,sub_height);
129- float textPos = pos_graphs[g][1 ] + 30 ;
119+ // Title
120+ textSize (TITLE_SZ );
121+ fill (255 );
122+ textAlign (CENTER , TOP );
123+ text (titles[g], pos_graphs[g][0 ] + sub_width/ 2 , pos_graphs[g][1 ] + TITLE_SZ );
124+
125+ // Plot legend
126+ float textPos = pos_graphs[g][1 ] + LABEL_SZ ;
127+ textAlign (RIGHT , TOP );
128+ textSize (LABEL_SZ );
130129 // Plot each line
131130 for (int i = 0 ; i < sz_graphs[g]; i++ ) {
132131 fill (COLORS [i]);
133- text (labels[k + i],pos_graphs[g][0 ] + 10 ,textPos);
134- textPos += 20 ;
132+ text (labels[k + i],pos_graphs[g][0 ] + sub_width - 10 ,textPos);
133+ textPos += ( LABEL_SZ + 3 ) ;
135134 stroke (COLORS [i]);
136135
137136 for (int j = 0 ; j < num_points[g]; j++ ) {
@@ -140,7 +139,46 @@ void plot_time(int graph_index) {
140139
141140 }
142141
143- }
142+ }
143+ drawTicks(g); // draw ticks over any data (only an issue with time plot)
144+ }
145+
146+ void drawTicks (int g ) {
147+ // Label graph with numbered tick marks
148+ stroke (255 );
149+ fill (255 );
150+ textSize (NUM_SZ );
151+ textAlign (LEFT , CENTER );
152+ // Draw ticks along y-axis
153+ float temp_x = pos_graphs[g][0 ] - TICK_LEN / 2 ;
154+ float tick_offset = 0.5 * (1.0 - AXIS_COVERAGE )* sub_height;
155+ float val = (float )extremes_graphs[g][3 ];
156+ float val_interval = (float )(extremes_graphs[g][3 ] - extremes_graphs[g][2 ]) / (NUM_TICKS - 1 );
157+ for (float temp_y = pos_graphs[g][1 ] + tick_offset;
158+ temp_y <= pos_graphs[g][1 ] + sub_height - tick_offset;
159+ temp_y += AXIS_COVERAGE * sub_height / (NUM_TICKS - 1 )) {
160+ line (temp_x, temp_y, temp_x + TICK_LEN , temp_y);
161+ text (Float . toString(val), temp_x + TICK_LEN + 5 , temp_y);
162+ val -= val_interval;
163+ }
164+ // Draw along x-axis
165+ float temp_y = pos_graphs[g][1 ] + sub_height - TICK_LEN / 2 ;
166+ tick_offset = 0.5 * (1.0 - AXIS_COVERAGE )* sub_width;
167+ val = (float )extremes_graphs[g][0 ];
168+ val_interval = (float )(extremes_graphs[g][1 ] - extremes_graphs[g][0 ]) / (NUM_TICKS - 1 );
169+ if (xvy[g]) {
170+ val *= AXIS_COVERAGE ;
171+ val_interval *= AXIS_COVERAGE ;
172+ }
173+ textAlign (CENTER , BOTTOM );
174+ for (temp_x = pos_graphs[g][0 ] + tick_offset;
175+ temp_x <= pos_graphs[g][0 ] + sub_width - tick_offset;
176+ temp_x += AXIS_COVERAGE * sub_width / (NUM_TICKS - 1 )) {
177+ line (temp_x, temp_y, temp_x, temp_y + TICK_LEN );
178+ text (Float . toString(val), temp_x, temp_y - 5 );
179+ val += val_interval;
180+ }
181+
144182}
145183
146184void serialEvent (Serial ser ) {
@@ -182,16 +220,16 @@ void serialEvent(Serial ser) {
182220 for (int i = 0 ; i < num_high; i++ ) {
183221 for (int j = 0 ; j < num_wide; j++ ) {
184222 if (k < num_graphs) {
185- pos_graphs[k][0 ] = j* sub_width + LABEL_SZ ;
186- pos_graphs[k][1 ] = i* sub_height;
223+ pos_graphs[k][0 ] = j* sub_width + MARGIN_SZ / 2 ;
224+ pos_graphs[k][1 ] = i* sub_height + MARGIN_SZ / 2 ;
187225 }
188226 k++ ;
189227 }
190228 }
191- sub_width -= LABEL_SZ ;
192- sub_height -= LABEL_SZ ;
229+ sub_width -= MARGIN_SZ ;
230+ sub_height -= MARGIN_SZ ;
193231
194- // Reset data storage arrays
232+ // Pull more values and reset datastore arrays as appropriate
195233 total_vars = Integer . parseInt(array_sub[1 ]);
196234 max_points = Integer . parseInt(array_sub[2 ]);
197235 labels = new String [total_vars];
0 commit comments