home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool.zip / OOL / source / xchart.cpp < prev    next >
C/C++ Source or Header  |  1997-03-07  |  6KB  |  355 lines

  1. #include "XPie.h"
  2. #include "XColor.h"
  3. #include "xrect.h"
  4. #include "xmsgbox.h"
  5. #include "xstring.h"
  6. #include "xbar.h"
  7. #include "xtimesl.h"
  8. #include <stdlib.h>
  9.  
  10.  
  11. XChart::~XChart()
  12. {
  13.     int i;
  14.  
  15.     if (itemCount)
  16.     {
  17.         for (i = 0; i < itemCount; i++)
  18.             free(item[i]);
  19.         free(item);
  20.     }
  21.     delete graph;
  22. }
  23.  
  24.  
  25. XChart :: XChart(const XWindow * owner, const XRect * r, const LONG id, const LONG s, const char *t):XUserWindow(owner->GetHandle())
  26. {
  27.     WinSetWindowUShort(winhandle, QWS_ID, id);
  28.     style = s;
  29.     item = NULL;
  30.     itemCount = 0;
  31.     graph = new XGraphicDevice(this);
  32.     if (t)
  33.     {
  34.         titleFont = new XFont(graph, "Helv", 10);//FONT_BOLD
  35.         title = new XText(graph, titleFont, r, t, DT_CENTER | DT_BOTTOM);
  36.         XColor col(COL_RED);
  37.         title->SetColor(&col);
  38.     }
  39.     else
  40.     {
  41.         titleFont = NULL;
  42.         title = NULL;
  43.     }
  44.  
  45.     if (style & FS_BORDER)
  46.     {
  47.         XPoint p, p2;
  48.  
  49.         box = new XBox(graph, &p, &p2, FALSE);
  50.     }
  51.     else
  52.         box = NULL;
  53.  
  54.     SetSize(r);
  55. }
  56.  
  57.  
  58. void XChart::SetBackgroundColor(const XColor * c) const
  59. {
  60.     graph->SetBackgroundColor(c);
  61. }
  62.  
  63.  
  64. void XChart::GetBackgroundColor(XColor * c)
  65. {
  66.     graph->GetBackgroundColor(c);
  67. }
  68.  
  69.  
  70. void XChart::SetText(const char *text)
  71. {
  72.     title->SetText(text);
  73. }
  74.  
  75.  
  76. void XChart::SetSize(const XRect * r) const
  77. {
  78.     if (box)
  79.     {
  80.         box->SetHeight(r->GetHeight() - 1);
  81.         box->SetWidth(r->GetWidth() - 1);
  82.     }
  83.     if (title)
  84.     {
  85.         XPoint p(0, 3);
  86.  
  87.         title->Move(&p);
  88. /*
  89.          title->SetHeight( r->GetHeight() - 8);
  90.          title->SetWidth( r->GetWidth() - 1);
  91. */
  92.     }
  93.     XWindow::SetSize(r);
  94. }
  95.  
  96.  
  97. void XPie::SetItemColor(const USHORT index, const XColor * col)
  98. {
  99.     if (index < itemCount)
  100.         arc[index]->SetColor(col);
  101. }
  102.  
  103.  
  104. void XChart::SetItemValue(const USHORT index, const double value)
  105. {
  106.     if (index < itemCount)
  107.         item[index]->value = value;
  108. }
  109.  
  110. /*
  111. void XPie :: GetItemColor( const USHORT index, XColor * col)
  112. {
  113.    if(index < itemCount)
  114.       col->SetColor( item[index]->color );
  115. }
  116. */
  117.  
  118. double XChart::GetItemValue(const USHORT index)
  119. {
  120.     if (index < itemCount)
  121.         return item[index]->value;
  122.     return 0;
  123. }
  124.  
  125.  
  126. void XChart::SetItemCount(const USHORT count)
  127. {
  128.     int i;
  129.  
  130.     if (itemCount)
  131.     {
  132.         for (i = 0; i < itemCount; i++)
  133.             free(item[i]);
  134.         free(item);
  135.     }
  136.     itemCount = count;
  137.     item = (chartItem **) malloc(count * sizeof(void *));
  138.     for (i = 0; i < itemCount; i++)
  139.     {
  140.         item[i] = (chartItem *) malloc(sizeof(chartItem));
  141.         item[i]->value = 0;
  142. //      item[i]->color = XColor( i+2 ).GetColor();
  143.     }
  144. }
  145.  
  146.  
  147. XPie :: XPie(const XWindow * w, const XRect * r, const LONG id, const LONG s, const char *t):XChart(w, r, id, s, t)
  148. {
  149.     arc = NULL;
  150. }
  151.  
  152.  
  153. void XPie::GetItemColor(const USHORT index, XColor * col)
  154. {
  155.     if (index < itemCount)
  156.         arc[index]->GetColor(col);
  157. }
  158.  
  159.  
  160. void XPie::SetItemCount(const USHORT count)
  161. {
  162.     int i;
  163.     XRect r;
  164.  
  165.     GetSize(&r);
  166.  
  167.     if (arc)
  168.     {
  169.         for (i = 0; i < itemCount; i++)
  170.             graph->RemoveObject(arc[i], TRUE);
  171.     }
  172.  
  173.     XChart::SetItemCount(count);
  174.  
  175.     arc = (XArc **) realloc(arc, itemCount * sizeof(void *));
  176.     for (i = 0; i < itemCount; i++)
  177.     {
  178.         XPoint p(r.GetWidth() / 2, r.GetHeight() / 2 + 10);
  179.  
  180.         arc[i] = new XArc(graph, &p, r.GetWidth() / 2 - 15, 0, 0, TRUE, TRUE);
  181.         XColor col(i + 3);
  182.  
  183.         arc[i]->SetColor(&col);
  184.     }
  185. }
  186.  
  187.  
  188. void XPie::Calculate(void)
  189. {
  190.     double all = 0;
  191.     int i;
  192.  
  193.     for (i = 0; i < itemCount; i++)
  194.         all += item[i]->value;
  195.     if (all == 0)
  196.         return;
  197.  
  198.     float start = 0, akt;
  199.  
  200.     for (i = 0; i < itemCount; i++)
  201.     {
  202.         akt = (item[i]->value * 360) / all;
  203.         arc[i]->SetStart((USHORT) start);
  204.         arc[i]->SetWidth((USHORT) akt);
  205.         start += akt;
  206.     }
  207. }
  208.  
  209.  
  210. XBar :: XBar(const XWindow * w, const XRect * r, const LONG id, const LONG s, const char *t):XChart(w, r, id, s, t)
  211. {
  212.     box = NULL;
  213. }
  214.  
  215.  
  216. void XBar::SetItemCount(const USHORT count)
  217. {
  218.     int i;
  219.     XRect r;
  220.  
  221.     GetSize(&r);
  222.  
  223.     if (box)
  224.     {
  225.         for (i = 0; i < itemCount; i++)
  226.             graph->RemoveObject(box[i], TRUE);
  227.     }
  228.  
  229.     XChart::SetItemCount(count);
  230.  
  231.     box = (XBox **) realloc(box, itemCount * sizeof(void *));
  232.     SHORT width = (r.GetWidth() - 2) / itemCount;
  233.  
  234.     for (i = 0; i < itemCount; i++)
  235.     {
  236.         XPoint p(i * width + 1, 1);
  237.         XPoint p2((i + 1) * width, r.GetHeight() - 2);
  238.  
  239.         box[i] = new XBox(graph, &p, &p2, TRUE);
  240.         XColor col(i + 1);
  241.  
  242.         box[i]->SetColor(&col);
  243.     }
  244. }
  245.  
  246.  
  247. void XBar::SetItemColor(const USHORT index, const XColor * col)
  248. {
  249.     if (index < itemCount)
  250.         box[index]->SetColor(col);
  251. }
  252.  
  253.  
  254. void XBar::GetItemColor(const USHORT index, XColor * col)
  255. {
  256.     if (index < itemCount)
  257.         box[index]->GetColor(col);
  258. }
  259.  
  260.  
  261. void XBar::Calculate(void)
  262. {
  263.     int i;
  264.     float max = 0;
  265.  
  266.     XRect r;
  267.  
  268.     GetSize(&r);
  269.  
  270.     for (i = 0; i < itemCount; i++)
  271.         if (item[i]->value > max)
  272.             max = item[i]->value;
  273.  
  274.     for (i = 0; i < itemCount; i++)
  275.         box[i]->SetHeight((SHORT) item[i]->value / max * (r.GetHeight() - 21));
  276. }
  277.  
  278.  
  279. XTimeSlice :: XTimeSlice(const XWindow * w, const XRect * r, const LONG id, const LONG s, const char *t):XChart(w, r, id, s, t)
  280. {
  281.     lines = NULL;
  282.     maxValue = 100;
  283.     width = 1;
  284. }
  285.  
  286.  
  287. void XTimeSlice::SetItemCount(const USHORT count)
  288. {
  289.     int i;
  290.     XRect r;
  291.  
  292.     GetSize(&r);
  293.  
  294.     if(lines)
  295.     {
  296.         for (i = 0; i < itemCount-1; i++)
  297.             graph->RemoveObject(lines[i], TRUE);
  298.     }
  299.  
  300.     XChart::SetItemCount(count);
  301.  
  302.     free( lines );
  303.     lines = (XLine**) malloc( (count-1) * sizeof(XLine*));
  304.     
  305.     LONG wid = r.GetWidth() / (itemCount-1);
  306.     for(i=0; i < count-1; i++)
  307.     {
  308.         XPoint p1(wid * i, 10), p2(wid * (i+1), 10);
  309.         lines[i] = new XLine( graph, &p1, &p2);
  310.         lines[i]->SetColor( &color );
  311.         lines[i]->SetLineWidth( width );
  312.     }
  313. }
  314.  
  315.  
  316. void XTimeSlice::Calculate(void)
  317. {
  318.     int i;
  319.     XRect r;
  320.  
  321.     GetSize(&r);
  322.     LONG lastY;
  323.     for (i = 0; i < itemCount; i++)
  324.     {
  325.         XPoint p( r.GetWidth() / (itemCount-1) * i, item[i]->value / maxValue * (r.GetHeight()-20) + 20);
  326.         if( i < itemCount -1 )
  327.             lines[i]->Move( &p );
  328.         if( i > 0)
  329.             lines[i-1]->SetHeight( (item[i]->value / maxValue * (r.GetHeight()-20)) + 20 - lastY);
  330.         lastY = p.GetY();
  331.     }
  332. }
  333.  
  334.  
  335. void XTimeSlice::SetLineColor( const XColor * col)
  336. {
  337.    color = *col;
  338.     if(lines)
  339.     {
  340.         for(int i = 0; i < itemCount-1; i++)
  341.             lines[i]->SetColor( col );
  342.     }
  343. }
  344.  
  345.  
  346. void XTimeSlice::SetLineWidth(const LONG w)
  347. {
  348.    width = w;
  349.     if(lines)
  350.     {
  351.         for(int i = 0; i < itemCount-1; i++)
  352.             lines[i]->SetLineWidth( w );
  353.     }
  354. }
  355.