KSquare Utilities
KKB::Chart Class Reference

Used to Create chart's from defined series of data. More...

#include <Chart.h>

Classes

class  PlotPoint
 
class  Series
 
class  XLabel
 

Public Types

typedef PlotPointPlotPointPtr
 
typedef vector< SeriesPtrSeriesList
 
typedef SeriesSeriesPtr
 

Public Member Functions

 Chart (KKStr _title)
 
 ~Chart ()
 
void AddAValue (kkuint32 _seriesIDX, float _xVal, float _yVal)
 
RasterPtr CreateRaster ()
 
void DefineASeries (KKStr _name)
 
void DefineASeries (KKStr _name, PixelValue _color)
 
void DefineAXLabel (float _xVal, KKStr _name)
 
kkint32 NumOfSeries ()
 
void Save (KKStr _fileName)
 
void SaveAsImage (KKStr _fileName)
 

Detailed Description

Used to Create chart's from defined series of data.

This class is meant to allow you to define multiple series, their x and y ranges and the data for each series.

Todo:
This class Chart has not been used as of yet so requires testing.

Definition at line 24 of file Chart.h.

Member Typedef Documentation

Definition at line 33 of file Chart.h.

Definition at line 32 of file Chart.h.

Definition at line 29 of file Chart.h.

Constructor & Destructor Documentation

KKB::Chart::Chart ( KKStr  _title)

Definition at line 142 of file Chart.cpp.

References Chart(), KKB::FloatMax, KKB::FloatMin, and KKB::KKStr::KKStr().

Referenced by Chart().

142  :
143  title (_title),
144  series (),
145  xLabels (),
146  minXValue (FloatMax),
147  maxXValue (FloatMin),
148  minYValue (FloatMax),
149  maxYValue (FloatMin),
150 
151  maxPointsPlotedInASeries (0),
152  numOfDefaultColorsUsed (0),
153 
154  chartWidth (0),
155  chartHeight (0),
156  xOffset (0),
157  yOffset (0),
158  yRange (0.0),
159  xRange (0.0),
160  yScale (0.0),
161  xScale (0.0),
162  xMin (0.0),
163  xMax (0.0),
164  yMin (0.0),
165  yMax (0.0),
166  yIncrement (0.0)
167 
168 {
169 }
float FloatMin
Definition: KKBaseTypes.cpp:21
float FloatMax
Definition: KKBaseTypes.cpp:20
KKB::Chart::~Chart ( )

Definition at line 172 of file Chart.cpp.

173 {
174  for (kkuint32 x = 0; x < series.size (); x++)
175  {
176  delete series[x];
177  series[x] = NULL;
178  }
179 }
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89

Member Function Documentation

void KKB::Chart::AddAValue ( kkuint32  _seriesIDX,
float  _xVal,
float  _yVal 
)

Definition at line 184 of file Chart.cpp.

188 {
189  if ((_seriesIDX < 0) || (_seriesIDX >= series.size ()))
190  {
191  cerr << std::endl
192  << "*** ERROR *** Chart::AddAValue" << std::endl
193  << " Invalid SeriesIDX[" << _seriesIDX << "]" << std::endl
194  << " Only [" << series.size () << "] series defined." << std::endl
195  << std::endl;
196  exit (-1);
197  }
198 
199  series[_seriesIDX]->AddAValue (_xVal, _yVal);
200 
201  if (series[_seriesIDX]->Size () > maxPointsPlotedInASeries)
202  maxPointsPlotedInASeries = series[_seriesIDX]->Size ();
203 
204 
205  if (_xVal < minXValue)
206  minXValue = _xVal;
207 
208  if (_xVal > maxXValue)
209  maxXValue = _xVal;
210 
211  if (_yVal < minYValue)
212  minYValue = _yVal;
213 
214  if (_yVal > maxYValue)
215  maxYValue = _yVal;
216 
217 } /* AddAValue */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
RasterPtr Chart::CreateRaster ( )

Definition at line 287 of file Chart.cpp.

References KKB::Raster::DrawDot(), KKB::Raster::DrawLine(), KKB::PixelValue::PixelValue(), KKB::Chart::PlotPoint::PlotPoint(), and KKB::Raster::Raster().

Referenced by SaveAsImage().

288 {
289  PixelValue axisColor ( 0, 0, 0);
290  PixelValue scaleColor (190, 190, 190);
291  PixelValue zeroAxisColor (240, 132, 240);
292 
293  chartWidth = maxPointsPlotedInASeries * 10;
294 
295  if (chartWidth > 1000)
296  chartWidth = 1000;
297 
298  chartHeight = chartWidth;
299 
300  if (chartHeight > 500)
301  chartHeight = 500;
302 
303 
304  xOffset = 10;
305  yOffset = 10;
306 
307 
308  yRange = maxYValue - minYValue;
309  xRange = maxXValue - minXValue;
310 
311  yScale = (double)chartHeight / yRange;
312  xScale = (double)chartWidth / xRange;
313 
314  yMin = minYValue;
315  yMax = maxYValue;
316 
317  xMin = minXValue;
318  xMax = maxXValue;
319 
320 
321  {
322  // Lets refine the scaling
323  double sigTensPlace = floor (log10 (yRange));
324 
325  sigTensPlace = pow (10.0, sigTensPlace);
326 
327  yIncrement = floor (yRange / sigTensPlace) * sigTensPlace;
328 
329  kkint32 numOfYIncremets = (kkint32)((yRange / yIncrement) + 0.5);
330 
331  if (numOfYIncremets < 2)
332  {
333  yIncrement = yIncrement / 4.0;
334  }
335  else if (numOfYIncremets < 5)
336  {
337  yIncrement = yIncrement / 2.0;
338  }
339  else if (numOfYIncremets > 7)
340  {
341  yIncrement = yIncrement * 2.0;
342  }
343  else if (numOfYIncremets > 13)
344  {
345  yIncrement = yIncrement * 4.0;
346  }
347 
348  yMin = floor (minYValue / yIncrement) * yIncrement;
349  yMax = ceil (maxYValue / yIncrement) * yIncrement;
350 
351  yRange = yMax - yMin;
352  yScale = (double)chartHeight / yRange;
353  }
354 
355 
356  kkint32 height = chartHeight + 2 * yOffset;
357  kkint32 width = chartWidth + 2 * xOffset;
358 
359  RasterPtr chart = new Raster (height, width, true);
360 
361  {
362  // Draw Main Axis
363  chart->DrawLine (yOffset, xOffset, yOffset + chartHeight, xOffset, axisColor);
364  chart->DrawLine (yOffset, xOffset, yOffset , xOffset + chartWidth, axisColor);
365  }
366 
367 
368  {
369  // Draw y scales
370 
371  double y = yMin + yIncrement;
372 
373  while (y < yMax)
374  {
375  chart->DrawLine (RasterCoordinates (PlotPoint (0.0, (double)y)),
376  RasterCoordinates (PlotPoint (xMax, (double)y)),
377  scaleColor
378  );
379  y += yIncrement;
380  }
381  }
382 
383 
384  if ((yMin < 0.0) && (yMax > 0.0))
385  {
386  // Draw a line for y=0.0
387  chart->DrawLine (RasterCoordinates (PlotPoint (double ((kkint32)(xMin + 0.5)), 0.0)),
388  RasterCoordinates (PlotPoint (double ((kkint32)(xMax + 0.5)), 0.0)),
389  zeroAxisColor
390  );
391 
392  }
393 
394 
395 
396  if ((xMin < 0.0) && (xMax > 0.0))
397  {
398  // Draw a line for x=0.0
399  chart->DrawLine (RasterCoordinates (PlotPoint (0.0, double ((kkint32)(yMin + 0.5)))),
400  RasterCoordinates (PlotPoint (0.0, double ((kkint32)(yMax + 0.5)))),
401  zeroAxisColor
402  );
403 
404  }
405 
406 
407 
408  {
409  // Lets plot the individual series
410 
411  for (kkuint32 seriesIDX = 0; seriesIDX < series.size (); seriesIDX++)
412  {
413  SeriesPtr s = series[seriesIDX];
414 
415  if (s->points.size () == 0)
416  continue;
417 
418  PlotPoint lastPoint (s->points[0]);
419 
420  chart->DrawDot (RasterCoordinates (lastPoint), s->color, 3);
421 
422  for (kkuint32 plotIDX = 1; plotIDX < s->points.size (); plotIDX++)
423  {
424  PlotPoint point (s->points[plotIDX]);
425 
426  chart->DrawDot (RasterCoordinates (point), s->color, 3);
427 
428  chart->DrawLine (RasterCoordinates (lastPoint),
429  RasterCoordinates (point),
430  s->color
431  );
432 
433  lastPoint = point;
434  }
435  }
436  }
437 
438 
439  return chart;
440 } /* CreateRaster */
__int32 kkint32
Definition: KKBaseTypes.h:88
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
void DrawDot(const Point &point, const PixelValue &color, kkint32 size)
Definition: Raster.cpp:6948
void DrawLine(kkint32 bpRow, kkint32 bpCol, kkint32 epRow, kkint32 epCol)
Definition: Raster.cpp:6239
Used by the Raster Class to represent the contents of one pixel.
Definition: PixelValue.h:22
void KKB::Chart::DefineASeries ( KKStr  _name)

Definition at line 245 of file Chart.cpp.

246 {
247  series.push_back (new Series (_name, defaultColors[numOfDefaultColorsUsed % numOfDefinedDefaultColors]));
248  numOfDefaultColorsUsed++;
249 }
static PixelValue defaultColors[]
Definition: Chart.cpp:223
static const kkint32 numOfDefinedDefaultColors
Definition: Chart.cpp:241
void KKB::Chart::DefineASeries ( KKStr  _name,
PixelValue  _color 
)

Definition at line 253 of file Chart.cpp.

256 {
257  series.push_back (new Series (_name, _color));
258 }
void KKB::Chart::DefineAXLabel ( float  _xVal,
KKStr  _name 
)

Definition at line 263 of file Chart.cpp.

266 {
267  xLabels.push_back (XLabel (_xVal, _name));
268 }
kkint32 KKB::Chart::NumOfSeries ( )
inline

Definition at line 40 of file Chart.h.

Referenced by Save().

40 {return (kkint32)series.size ();}
__int32 kkint32
Definition: KKBaseTypes.h:88
void Chart::Save ( KKStr  _fileName)

Definition at line 487 of file Chart.cpp.

References NumOfSeries(), KKB::Chart::PlotPoint::XVal(), and KKB::Chart::XLabel::XVal().

488 {
489  ofstream o (_fileName.Str ());
490  if (!o.is_open ())
491  {
492  cerr << std::endl
493  << "*** ERROR *** Chart::Save, Error Opening file[" << _fileName << "]." << std::endl
494  << std::endl;
495  return;
496  }
497 
498 
499  {
500  // Make sure we have a XLabel for all Points Plotted
501  for (kkuint32 seriesIDX = 0; seriesIDX < series.size (); seriesIDX++)
502  {
503  SeriesPtr s = series[seriesIDX];
504  for (kkuint32 plotIDX = 0; plotIDX < s->points.size (); plotIDX++)
505  {
506  PlotPoint& p = s->points[plotIDX];
507  if (LookUpXLableIDX (p.XVal ()) < 0)
508  {
509  char buff[80];
510 
511 # ifdef USE_SECURE_FUNCS
512  sprintf_s (buff, sizeof (buff), "%g", p.XVal ());
513 # else
514  sprintf (buff, "%g", p.XVal ());
515 # endif
516 
517  xLabels.push_back (XLabel (p.XVal (), buff));
518  }
519  }
520  }
521  }
522 
523 
524  std::sort (xLabels.begin (), xLabels.end ());
525 
526 
527  {
528  // Output Header Info
529  o << "Title" << "\t" << title << std::endl;
530  o << "MinAndMaxXValues:" << "\t" << minXValue << "\t" << maxXValue << std::endl;
531  o << "MinAndMaxYValues:" << "\t" << minYValue << "\t" << maxYValue << std::endl;
532 
533  o << "MaxPointsPlottedInASeries" << "\t" << maxPointsPlotedInASeries << std::endl;
534  o << std::endl;
535  }
536 
537 
538  o << std::endl;
539  o << std::endl;
540 
541 
542  {
543  // Column Headers
544  o << "XLabel" << "\t" << "XValue";
545  for (kkint32 seriesIDX = 0; seriesIDX < NumOfSeries (); seriesIDX++)
546  {
547  o << "\t" << series[seriesIDX]->name;
548  }
549  o << std::endl;
550  }
551 
552 
553 
554  {
555  kkuint32 x;
556 
557  for (x = 0; x < xLabels.size (); x++)
558  {
559  XLabel& xLabel = xLabels[x];
560 
561  double xVal = xLabel.XVal ();
562 
563 
564  o << xLabel.Name () << "\t" << xVal;
565 
566  kkint32 seriesIDX = 0;
567 
568  for (seriesIDX = 0; seriesIDX < NumOfSeries (); seriesIDX++)
569  {
570  o << "\t";
571 
572  PlotPointPtr p = LookUpPoint (seriesIDX, xVal);
573  if (p)
574  {
575  o << p->YVal ();
576  }
577  }
578 
579  o << std::endl;
580  }
581  }
582 
583 
584  o.close ();
585 } /* Save */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
PlotPoint * PlotPointPtr
Definition: Chart.h:33
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
kkint32 NumOfSeries()
Definition: Chart.h:40
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void Chart::SaveAsImage ( KKStr  _fileName)

Definition at line 445 of file Chart.cpp.

References CreateRaster(), and KKB::SaveImage().

446 {
447  RasterPtr raster = CreateRaster ();
448  SaveImage (*raster, _fileName);
449 } /* SaveAsImage */
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
void SaveImage(const Raster &image, const KKStr &imageFileName)
Definition: ImageIO.cpp:617
RasterPtr CreateRaster()
Definition: Chart.cpp:287

The documentation for this class was generated from the following files: