AxisRepColor.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 // for min()
14 #include "msdevstudio/MSconfig.h"
15 #endif
16 
17 #include "AxisRepColor.h"
18 
19 
20 #include "axes/AxisModelBase.h"
21 #include "axes/AxisTick.h"
22 #include "colorreps/BinToColor.h"
23 #include "graphics/DataView.h"
24 #include "pattern/string_convert.h"
26 #include "plotters/PlotterBase.h"
27 
28 
29 #include <algorithm>
30 #include <functional>
31 
32 #include <cmath>
33 
34 #include <cassert>
35 
36 #include <iostream>
37 
38 using std::min;
39 using std::string;
40 using std::vector;
41 
42 using namespace hippodraw;
43 
45  : AxisRepBase (),
46  m_axis_z_origin( 0.0 ),
47  m_axis_depth( 0.0 )
48 {
49 }
50 
52  : AxisRepBase( axis_rep ),
53  m_axis_z_origin( axis_rep.m_axis_z_origin ),
54  m_axis_depth( axis_rep.m_axis_depth )
55 {
56 }
57 
59 {
60  return new AxisRepColor( *this );
61 }
62 
63 
64 inline float XPADDING( ViewBase & view )
65 {
66  return ( view.getDrawRect().getWidth() * 0.01 );
67 }
68 
69 inline float YPADDING( ViewBase & view )
70 {
71  return ( view.getDrawRect().getHeight() * 0.01 );
72 }
73 
74 inline float ZPADDING( ViewBase & view )
75 {
76  return ( view.getDrawRect().getWidth() * 0.01 );
77 }
78 
81 void
83 drawZLabels ( const AxisModelBase &, // axisModel,
84  ViewBase & base,
85  const std::string & z_label )
86 {
87 
88  DataView & view = dynamic_cast < DataView & > ( base );
89 
90  // Draw Latex at the top of the plotter if in Latex format
91  if (String::ci_find(z_label, "tex:")==0) {
92  string tex_snippet = z_label.substr(4);
93  view.drawLatex ( tex_snippet, 4 );
94  return;
95  }
96 
97  Rect draw_rect = view.getDrawRect ();
98  float dh = draw_rect.getHeight ();
99 
100  const Rect & margin_rect = view.getMarginRect ();
101  float mx = margin_rect.getX ();
102  float mw = margin_rect.getWidth ();
103  float my = margin_rect.getY ();
104  float mh = margin_rect.getHeight ();
105 
106  float y_font_size = ( dh - mh ) / 2 * 0.20;
107  float x_font_size = ( mw * 2 ) / z_label.size() * 2 / 3;
108 
109  m_z_font_size = min ( x_font_size, y_font_size );
110 
111  float x = mx + 0.5 * mw;
112  float y = my - 37;
113 
114  if (m_titleFont != 0) {
115  y = y + 1.2 * m_titleFont->pointSize() - 11.0; //experimental
116  }
117 
118  if ( m_zLabelFont != 0 ) {
119  y = y + 1.2 * m_zLabelFont->pointSize() - 8.0; // experimental
120  view.drawText ( z_label, x, y, 0.0, 0.0, 'c', 'b',
121  false, m_zLabelFont );
122  } else {
123  view.drawText ( z_label, x, y, m_z_font_size, 0.0, 'c', 'b', false );
124  }
125 }
126 
128  const TransformBase & transform,
129  ViewBase & view )
130 {
131  drawZTickLines ( axisModel, transform, view );
132  drawZTickLabels ( axisModel, transform, view );
133 }
134 
136  const TransformBase & transform,
137  ViewBase & base )
138 {
139  AxisLoc loc = axisModel.getLabelLocation ();
140  assert ( loc == PLOTBOTTOM || PLOTTOP );
141 
142  const vector< AxisTick > & ticks = axisModel.getTicks ();
143  if ( ticks.empty() ) return;
144 
145  vector< double > xv;
146  vector< double > yv;
147 
148  unsigned int size = 2 * ticks.size ();
149  xv.reserve ( size );
150  yv.reserve ( size );
151 
152  const BinaryTransform & t
153  = dynamic_cast< const BinaryTransform & > ( transform );
154  DataView & view = dynamic_cast < DataView & > ( base );
155  const Rect & view_rect = view.getMarginRect ();
156  double tick_length = 5;
157  double y_base = view_rect.getY() - 24;
158 
159  for ( unsigned int i = 0; i < ticks.size(); i++ ) {
160  double user_z = ticks[i].value ();
161  t.transformZ ( user_z );
162  // Reinstate the following line when XYZ transform exists.
163  //t.transform ( user_z, y ); // Some valid value on Y.
164 
165  double view_x = view.userToDrawColor ( user_z );
166 
167  xv.push_back ( view_x );
168  yv.push_back ( y_base );
169  xv.push_back ( view_x );
170  yv.push_back ( y_base + tick_length );
171 
172  }
173 
174  view.drawViewLines ( xv, yv, Line::Solid, false, 0 );
175 
176 }
177 
179  const TransformBase & transform,
180  ViewBase & base )
181 {
182  vector < double > xv;
183  vector < double > yv;
184 
185  const vector< AxisTick > & ticks = axisModel.getTicks ();
186  unsigned int size = ticks.size ();
187  if ( size == 0 ) return;
188 
189  xv.reserve ( size );
190  yv.reserve ( size );
191 
192  const BinaryTransform & t
193  = dynamic_cast< const BinaryTransform & > ( transform );
194 
195  unsigned int i = 0; // For MS VC++.
196  for ( ; i < size; i++ ) {
197  double value = ticks[i].value ();
198  t.transformZ ( value );
199  xv.push_back ( value );
200  yv.push_back ( 1.0 ); // Use a valid Y value.
201  }
202 
203 
204  // Calculate the Y position in view coordinates.
205  DataView & view = dynamic_cast < DataView & > ( base );
206  const Rect & margin = view.getMarginRect ();
207  float y = margin.getY() - 42;
208 
209  i = 0; // For MS VC++.
210  for ( ; i < size; i++ ) {
211  float x = view.userToDrawColor ( xv[i] );
212  drawXTickLabel ( ticks[i].content (), x, y, view );
213  }
214 
215  if ( axisModel.needPMag () ) {
216  // Logarithmic graphs do not need the magnitude written.
217  if ( !(axisModel.isLog() ) ) {
218  const Rect & margin_rect = view.getMarginRect ();
219  float xx = margin_rect.getX()
220  + margin_rect.getWidth ()
221  - m_y_font_size;
222  float yy = margin_rect.getY() - 30. - m_y_tick_font_size;
223  view.drawText ( "x10", xx, yy, m_y_tick_font_size, 0., 'l', 'b' );
224  double mag = axisModel.getPMag ();
225  int i = static_cast < int > ( mag );
226  const string text = String::convert ( i );
227  xx += 1.75* m_y_tick_font_size;
228  yy -= 0.5 * m_y_tick_font_size;
229  view.drawText ( text, xx, yy, m_y_tick_font_size, 0., 'l', 'b' );
230  }
231  }
232 
233 }
234 
235 void
237 drawColorScale ( const BinToColor & bin_to_color, ViewBase & base )
238 {
239  DataView & view = dynamic_cast < DataView & > ( base );
240  PlotterBase * plotter = view.getPlotter ();
241 
242  const Rect & margin = view.getMarginRect();
243 
244  double y_base = margin.getY() - 22;
245 
246  const Range range = bin_to_color.getRange ();
247  double value = range.low ();
248  double delta_v = range.length () / margin.getWidth ();
249  if (delta_v == 0) {
250  return;
251  }
252  for ( float i = 0; i <= margin.getWidth() ; i++ ) {
253 
254  const Color & rep_color = plotter->repColor();
255 
256  Color color = rep_color;
257 
258  bin_to_color.doubleToColor ( value, color );
259  view.drawViewSquare ( margin.getX() + i,
260  y_base,
261  margin.getX() + i + 1.0,
262  y_base + 6,
263  color.getRed(),
264  color.getGreen(),
265  color.getBlue () );
266  value += delta_v;
267  }
268 
269  view.drawViewSquare ( margin.getX(),
270  y_base,
271  margin.getX(),
272  y_base + 8,
273  0, 0, 0 );
274 }

Generated for HippoDraw Class Library by doxygen