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 }
double m_z_font_size
Font size for the Z axis label.
Definition: AxisRepBase.h:95
unsigned int i
virtual float userToDrawColor(double c) const =0
Converts a coordinate in user space to drawing space along the color (X) axis.
double m_y_font_size
Font size for the Y axis label.
Definition: AxisRepBase.h:91
void transformZ(double &z) const
Transforms the z coordinate.
double getHeight() const
A shortcut to get size.height.
Definition: Rectangle.cxx:113
A transform that transforms coordinates from one coordinate system to another.
Definition: TransformBase.h:35
The base class for the value to the color transformation.
Definition: BinToColor.h:30
virtual void drawLatex(const std::string &eq, int position=0)
Draws a Latex equation.
Definition: ViewBase.cxx:119
size_t ci_find(const string &str1, const string &str2)
Case insensitive find.
virtual void doubleToColor(double value, Color &color) const =0
This function does the actual calculation that transforms the given value into a color using the curr...
Range getRange() const
Get the Range of expected values.
Definition: BinToColor.cxx:55
virtual Rect getDrawRect() const =0
Returns the drawing Rectangle in the devices coordinate system.
virtual bool needPMag() const
The following functions are used by the AxisRepBase * object.
int getRed() const
Definition: Color.cxx:164
void drawZTickLines(const AxisModelBase &axisModel, const TransformBase &transform, ViewBase &view)
Draws the Z tick lines.
float YPADDING(ViewBase &view)
The namespace for conversion to string.
double getWidth() const
A shortcut to get size.width.
Definition: Rectangle.cxx:108
virtual void drawViewLines(const std::vector< double > &x, const std::vector< double > &y, Line::Style style, bool color, float size)=0
Draws multiple line segments.
The abstract base class for views that have a region for drawing data points with area around it for ...
Definition: DataView.h:30
A Color class for creating the color object following the standard RGB color space.
Definition: Color.h:37
virtual void drawViewSquare(float x1, float y1, float x2, float y2, int red, int green, int blue)=0
Draws a colored square in view space.
The class for the 2D axes drawing with color.
Definition: AxisRepColor.h:38
void drawAllZTicks(const AxisModelBase &axis_model, const TransformBase &transform, ViewBase &view)
Takes care of the z ticks drawing (that is, the ticks on the top of the plot).
The base class for the PlotterBase hierarchy.
Definition: PlotterBase.h:55
PlotterBase * getPlotter() const
Returns the plotter used by this view.
Definition: ViewBase.cxx:50
AxisLoc
The base class for the binner hierarchy.
Definition: AxisLoc.h:17
int getGreen() const
Definition: Color.cxx:169
FontBase * m_zLabelFont
The font to be used to for the Z label overriding the default font.
Definition: AxisRepBase.h:104
hippodraw::DataView class interface
stream * mw
Definition: CanvasView.cxx:169
hippodraw::BinaryTransform class interface
hippodraw::BinToColor class interface
intp size(numeric::array arr)
Definition: num_util.cpp:296
string convert(int i)
Converts an integer to a string.
The AxisModelBase class maintains the Range and scaling of an axis.
Definition: AxisModelBase.h:33
double length() const
Returns the length of the range object.
Definition: Range.h:156
void drawXTickLabel(const std::string &label, float x, float y, ViewBase &view)
Draws a tick label for X axis.
Class representing a rectangle.
Definition: Rectangle.h:34
virtual bool isLog() const =0
Returns a boolean describing the type of the scale of the axis.
The base class for the axis representation hierarchy.
Definition: AxisRepBase.h:49
double m_y_tick_font_size
Font size for the y tick labels.
Definition: AxisRepBase.h:83
AxisLoc getLabelLocation() const
The abstract base class for views.
Definition: ViewBase.h:62
virtual void drawColorScale(const BinToColor &, ViewBase &)
Draws the color scale bar.
const std::vector< AxisTick > & getTicks() const
Returns a reference of generated ticks.
hippodraw::AxisRepColor class interface
float ZPADDING(ViewBase &view)
hippodraw::AxisModelBase class interface
AxisRepBase * clone()
The clone function returns an object of its own kind which is a copy of this object at this moment...
virtual void drawText(const std::string &s, float x, float y, float fontsize, float angle=0.0, char xp= 'l', char yp= 't', bool resize=false, const FontBase *font=0, const Color *color=0)=0
Draws a text string at a point in the view&#39;s coordinate system.
double getPMag() const
Returns the magnitude of the power of ten for the tick labels.
double getX() const
A shortcut to get origin.X.
Definition: Rectangle.h:154
A transform that transforms coordinates from one 2D coordinate system to another. ...
void drawZLabels(const AxisModelBase &axis_model, ViewBase &view, const std::string &z_label)
Draws the labels of the z axis.
const Rect & getMarginRect() const
Returns the rectangle area in which data points are drawn.
Definition: DataView.cxx:45
double low() const
Returns the minimum of the range object.
Definition: Range.cxx:87
void drawZTickLabels(const AxisModelBase &axisModel, const TransformBase &transform, ViewBase &view)
Draws the labels for the Z ticks.
virtual int pointSize() const =0
Get the pointsize of the font.
Expresses a range of values.
Definition: Range.h:33
float XPADDING(ViewBase &view)
AxisRepColor()
The default constructor.
int getBlue() const
Definition: Color.cxx:174
double getY() const
A shortcut to get origin.Y.
Definition: Rectangle.h:162
FontBase * m_titleFont
The font to be used to for the Z label overriding the default font.
Definition: AxisRepBase.h:107
hippodraw::PlotterBase class interface.
AxisTick class interface.

Generated for HippoDraw Class Library by doxygen