Main Page   Class Hierarchy   Data Structures   File List   Data Fields  

chartdrawing.cpp

00001 /***************************************************************************
00002                           chartdrawing.cpp
00003     description          :
00004     begin                : 
00005     copyright            : (C) 2002 by Michael Otto
00006     email                : Michael.Otto@saskathex.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #define SIZE_X 300
00019 #define SIZE_Y 300
00020 #define LINES 10 
00021 #define DAYS 30
00022 #define OFFSET_X 40
00023 #define OFFSET_Y 7
00024 #define VALUE_COLUMN_SIZE 60
00025 #define VALUE_ROW_SIZE 30
00026 
00027 #include "chartdrawing.h"
00028 
00029 #include "stock.h"
00030 #include "config.h"
00031 
00035 ChartDrawing::ChartDrawing(const Stock * const stock) {
00036   ChartDrawing::stock = stock;
00037   values = stock->getValuesVector();
00038   
00039   stepX = SIZE_X / DAYS;
00040   stepY = SIZE_Y / LINES;
00041 
00042   set_size_request (SIZE_X-stepX+1+OFFSET_X+VALUE_COLUMN_SIZE, SIZE_Y+1+OFFSET_Y+VALUE_ROW_SIZE);
00043   set_events (Gdk::EXPOSURE_MASK);
00044   gdk_rgb_init();
00045 }
00046 
00047 
00050 ChartDrawing::~ChartDrawing() {
00051 }
00052 
00053 
00054 
00061 bool ChartDrawing::on_expose_event(GdkEventExpose *event) {
00062 
00063   Glib::RefPtr<Gdk::GC> some_gc;
00064   some_gc = Gdk::GC::create(get_window());
00065   Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create(Gtk::Widget::create_pango_context());
00066 
00067   // find max
00068   float max = 0;
00069   float step;
00070   for (int i = 0; i < values.size(); i++) {
00071     if (values[i] > max) max = values[i];
00072   }
00073   if (stock->getCurrentMarketPrice() > max) max = stock->getCurrentMarketPrice();
00074 #ifdef DEBUG
00075   cout << "maximum is " << max << endl;
00076 #endif
00077   max += 0.05 * max;
00078   step = max / 10;
00079 #ifdef DEBUG
00080   cout << "stepping is " << step << endl;
00081 #endif
00082   // vertical lines = No. of Days
00083   for (int i = 0; i < DAYS; i++) {
00084     get_window()->draw_line(this->get_style()->get_white_gc(), i*stepX + OFFSET_X, 0 + OFFSET_Y, i*stepX + OFFSET_X, LINES*stepY + OFFSET_Y);
00085   }
00086 
00087   // horizontal lines, day value of the stock
00088   for (int i = 0; i <= LINES; i++) {
00089     get_window()->draw_line(this->get_style()->get_white_gc(), 0 + OFFSET_X, i*stepY + OFFSET_Y, (DAYS-1)*stepX + OFFSET_X, i*stepY + OFFSET_Y);
00090   }    
00091 
00092   // Draw values on the y axis
00093   char c[8];
00094   for (int i = 0; i < LINES; i++) {
00095     sprintf(c,"%.2f", (LINES-i)*step);
00096     layout->set_text(c);
00097     get_window()->draw_layout(this->get_style()->get_black_gc(), SIZE_X+OFFSET_X+3, i*stepY, layout);
00098   }
00099   // Draw day values on the x axis
00100   for (int i = 0; i <= DAYS; i++) {
00101     if ((i % 5) == 0) {
00102       if (i == 0) sprintf(c,"%d", i);
00103       else sprintf(c,"%d", -i);
00104       layout->set_text(c);
00105       get_window()->draw_layout(this->get_style()->get_black_gc(), (DAYS-i)*stepX + OFFSET_X-12, LINES*stepY + OFFSET_Y+5, layout);
00106     }
00107   }
00108   //layout->set_text("days");
00109   //get_window()->draw_layout(this->get_style()->get_black_gc(), (DAYS)*stepX + OFFSET_X + 15, LINES*stepY + OFFSET_Y+10, layout);
00110     
00111   
00112   int value, valueBefore;
00113   int x = DAYS - 2;
00114   for (int i = values.size()-1; i >= 0; i--) {
00115     if (x > 0) {
00116       // getting & scaling the values
00117       value = (int) ((values[i] / max) * SIZE_Y);
00118       valueBefore = (int) ((values[i-1] / max) * SIZE_Y);
00119       // drawing the values
00120       get_window()->draw_point(some_gc, x*stepX + OFFSET_X, SIZE_Y - value + OFFSET_Y);
00121       if (i != 0) get_window()->draw_line(some_gc, (x-1)*stepX + OFFSET_X, SIZE_Y-valueBefore + OFFSET_Y, x*stepX + OFFSET_X, SIZE_Y-value + OFFSET_Y);
00122       x--;
00123     }
00124     else {
00125 #ifdef DEBUG
00126       cout << "more than 30 days stored!" << endl;
00127 #endif
00128       break;
00129     }
00130   }
00131 
00132   value = (int) ((stock->getCurrentMarketPrice() / max) * SIZE_Y);
00133   valueBefore = (int) ((stock->getCurrentMarketPriceDayBefore() / max) * SIZE_Y);
00134   get_window()->draw_point(some_gc, (DAYS-1)*stepX + OFFSET_X, SIZE_Y - value + OFFSET_Y);
00135   get_window()->draw_line(some_gc, (DAYS-2)*stepX + OFFSET_X, SIZE_Y-valueBefore + OFFSET_Y, (DAYS-1)*stepX + OFFSET_X, SIZE_Y-value + OFFSET_Y);
00136   
00137   return true;
00138 }

Generated on Tue May 27 21:24:01 2003 for gstockcalc by doxygen1.3-rc3