File: Graph_drawAutoGrid.cpp

package info (click to toggle)
quickplot 0.8.6-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 1,019
  • sloc: cpp: 10,051; sh: 7,597; makefile: 176
file content (261 lines) | stat: -rw-r--r-- 7,672 bytes parent folder | download | duplicates (4)
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
/* Copyright (c) 1998, 1999, 2003, 2004  Lance Arsenault, (GNU GPL (v2+))
 */
#include "config.h"

#ifdef QP_ARCH_DARWIN
# include <limits.h>
# include <float.h>
#else
# include <values.h>
#endif

#include <stdio.h>
#include <math.h>

#include <gtkmm.h>
#include <pangomm/layout.h>


using namespace Gtk;
#include "value_t.h"
#include "ValueSlider.h"
#include "PlotLister.h"
#include "PlotConfig.h"
#include "MainMenuBar.h"
#include "ButtonBar.h"
#include "StatusBar.h"
#include "MainWindow.h"
#include "App.h"

#include "Field.h"
#include "Plot.h"
#include "ColorGen.h"
#include "Graph.h"
#include "Globel.h"



#define ABSVAL(x)  (((x) > ((value_t) 0.0)) ? (x) : (-(x)))
#define TICKMIN  5.0
#define TICKMAX  8.0

#define SMALL_NUM   ABSVAL(MINVALUE)
#define ZERO        ((value_t) 0.0)
#define ONE         ((value_t) 1.0)
#define TEN         ((value_t) 10.0)


// Auto draw a x-y grid.  The code in this method is not too pretty.
// This will draw a x-y grid with value labels on each line.  This
// grid keeps somewhat even spacing when the Graph window resizes.
// The x-y values of the grid lines try to stay "nice".

void Graph::drawAutoGrid(Glib::RefPtr<Gdk::Drawable> _win,
                         Glib::RefPtr<Gdk::GC> _gc)
{
  // check that the plot scales are consistant with this Graph.
  (*begin())->preDrawCheckResize();
  
  value_t scaleX = (*begin())->currentZoomLevel->scaleX;
  value_t shiftX = (*begin())->currentZoomLevel->shiftX;
  value_t scaleY = (*begin())->currentZoomLevel->scaleY;
  value_t shiftY = (*begin())->currentZoomLevel->shiftY;

  // Find the x,y max and min values in the window.
  value_t xmax = (get_width() - shiftX)/scaleX;
  value_t xmin = (0 - shiftX)/scaleX;
  value_t ymax = (0 - shiftY)/scaleY;
  value_t ymin = (get_height() - shiftY)/scaleY;


  /***************************************************/
  /*        get the x value lines calculated         */
  /***************************************************/

  //printf("get_width()=%d xmin=%g xmax=%g\n", get_width(), xmin, xmax);

  value_t delta = (xmax - xmin)/(2 + ((value_t) get_width()/gridXLineSpace));

  //printf("xdelta=%g\n", delta);

  int delta_p = (int) log10(ABSVAL(delta) > SMALL_NUM ? ABSVAL(delta) : SMALL_NUM);

  if(delta_p <= 0) delta_p--;

  // printf("xdelta_p=%d\n", delta_p);

  value_t xpow_part = POW(TEN, delta_p);
  {
    // strip off the round-off error off of xpow_part.
    char s[16];
    //for example: xpow_part=0.20005465 --> s=0.20 --> ypow_part=0.2000000
    sprintf(s, TWO_DIGIT_FORMAT, xpow_part);
    sscanf(s, SCAN_FORMAT, &xpow_part);
    //printf("s=%s  xpow_part=%.15g\n",s, xpow_part);
  }
  
  int64_t xmin_mat = (int64_t) (xmin/xpow_part);
  int64_t xmax_mat = (int64_t) (xmax/xpow_part);

  //printf(" xmin = %lldx10%+d xmax = %lldx10%+d\n",
  // xmin_mat, delta_p, xmax_mat, delta_p);

  int64_t xinc;

  if(get_width() > gridXLineSpace)
    xinc = (int64_t) ((xmax_mat-xmin_mat)/((value_t) get_width()/gridXLineSpace));
  else
    xinc = xmax_mat-xmin_mat;

  //printf("               xinc=%lld\n", xinc);

  // round to 1, 2, 5, times 10^N
  if     (xinc < (int64_t) 2)   xinc = 1;
  else if(xinc < (int64_t) 5)   xinc = 2;
  else if(xinc < (int64_t) 10)  xinc = 5;
  else if(xinc < (int64_t) 20)  xinc = 10;
  else if(xinc < (int64_t) 50)  xinc = 20;
  else if(xinc < (int64_t) 100) xinc = 50;
  else if(xinc < (int64_t) 200) xinc = 100;
  else                          xinc = 200;

  int64_t xstart = xmin_mat;

  //printf("xinc=%lld\n", xinc);

  if(xmin_mat > (int64_t) 0 && xmin_mat%xinc)
    xstart -= xmin_mat%xinc;
  else if(xmin_mat < (int64_t) 0 && xmin_mat%xinc)
    xstart -= xinc + xmin_mat%xinc;

  
  /***************************************************/
  /*        get the y value lines calculated         */
  /***************************************************/

  delta = (ymax - ymin)/(2 + ((value_t) get_height()/gridYLineSpace));
 
  delta_p = (int) log10(ABSVAL(delta) > SMALL_NUM ? ABSVAL(delta) : SMALL_NUM);

  if(delta_p <= 0) delta_p--;

  //printf("ydelta_p=%d\n", delta_p);

  value_t ypow_part = POW(TEN, delta_p);
  {
    // strip off the round-off error off of ypow_part.
    char s[16];
    //for example: ypow_part=0.20005465 --> s=0.20 --> ypow_part=0.2000000
    sprintf(s, "%.2g", ypow_part);
    sscanf(s, "%lf", &ypow_part);
  }

  int64_t ymin_mat = (int64_t) (ymin/ypow_part);
  int64_t ymax_mat = (int64_t) (ymax/ypow_part);

  //printf(" ymin = %lldx10%+d ymax = %lldx10%+d\n",
  // ymin_mat, delta_p, max_mat, delta_p);

  int64_t yinc;

  if(get_height() > gridYLineSpace)
    yinc = (int64_t) ((ymax_mat-ymin_mat)/((value_t) get_height()/gridYLineSpace));
  else
    yinc = ymax_mat-ymin_mat;

  if     (yinc < (int64_t) 2)   yinc = 1;
  else if(yinc < (int64_t) 5)   yinc = 2;
  else if(yinc < (int64_t) 10)  yinc = 5;
  else if(yinc < (int64_t) 20)  yinc = 10;
  else if(yinc < (int64_t) 50)  yinc = 20;
  else if(yinc < (int64_t) 100) yinc = 50;
  else if(yinc < (int64_t) 200) yinc = 100;
  else                          yinc = 200;


  int64_t ystart = ymin_mat;

  //printf("yinc=%lld\n", yinc);

  if(ymin_mat > (int64_t) 0 && ymin_mat%yinc)
    ystart -= ymin_mat%yinc;
  else if(ymin_mat < (int64_t) 0 && ymin_mat%yinc)
    ystart -= yinc + ymin_mat%yinc;


  /******************************************************/
  /* find where to put the value text for x and y lines */
  /******************************************************/

  int x_ylinetext = (int) ((xstart + xinc*0.2)*xpow_part*scaleX + shiftX);
  if(x_ylinetext < 3)
    x_ylinetext = (int) ((xstart + xinc*1.2)*xpow_part*scaleX + shiftX);

  int y_xlinetext = (int) ((ystart + yinc*0.5)*ypow_part*scaleY + shiftY);
  if(y_xlinetext > get_height() - 20)
    y_xlinetext = (int) ((ystart + yinc*1.5)*ypow_part*scaleY + shiftY);

  

  /****************************************************/
  /*              setup for drawing                   */
  /****************************************************/
  
  _gc->set_foreground(gridColor);
  _gc->set_line_attributes(gridLineWidth, Gdk::LINE_SOLID,
                           Gdk::CAP_ROUND, Gdk::JOIN_ROUND);
  if(!pangolayout && showGridNumbers)
  {
    pangolayout = create_pango_layout("");
  }

  
  /***************************************************/
  /*             draw the y lines                    */
  /***************************************************/

  int64_t i;

  
  for(i=ystart;i<=ymax_mat;i += yinc)
    {
      char str[64];
      snprintf(str,64, GRID_PRINT_FORMAT, i*ypow_part);
      //snprintf(str,64, "%*.*g", digit_count,digit_count, i*ypow_part);
      //printf(GRID_PRINT_FORMAT" ", str);
      int y = (int) (i*ypow_part*scaleY + shiftY);
      if(showGridNumbers)
      {
        pangolayout->set_text((const char *) str);
        _win->draw_layout(_gc, x_ylinetext, y+gridLineWidth/2, pangolayout);
      }
      
      _win->draw_line(_gc, 0, y, get_width(), y);
    }
  //printf("\n");


  /***************************************************/
  /*             draw the x lines                    */
  /***************************************************/

  for(i=xstart;i<=xmax_mat;i += xinc)
    {
      char str[64];
      snprintf(str,64, GRID_PRINT_FORMAT, i*xpow_part);

      //printf(" xpow_part=%.15g\n",xpow_part);
      
      //printf(GRID_PRINT_FORMAT" ", i*xpow_part);
      int x = (int) (i*xpow_part*scaleX + shiftX);
      
      if(showGridNumbers)
      {
        pangolayout->set_text((const char *) str);
        _win->draw_layout(_gc, x+6+gridLineWidth/2, y_xlinetext, pangolayout);
      }
      
      _win->draw_line(_gc, x, 0, x, get_height());
    }
  //printf("\n");
}