00001 // Graph.cpp: implementation of the Graph class. 00002 // 00004 00005 #include "Graph.h" 00006 00008 // Construction/Destruction 00010 00011 Graph::Graph(int width, int height, int bufferSize, int nSubGraphs, float min, float max): 00012 width(width), height(height), bufferSize(bufferSize), nSubGraphs(nSubGraphs), 00013 min(min), max(max){ 00014 subGraphs=new GraphData*[nSubGraphs]; 00015 for(int i=0;i<nSubGraphs;i++){ 00016 subGraphs[i]=new GraphData(bufferSize); 00017 } 00018 00019 bitmap.Create(width,height); 00020 dc.SelectObject(bitmap); 00021 dc.SetBackground(*wxWHITE_BRUSH); 00022 } 00023 00024 Graph::~Graph(){ 00025 for(int i=0;i<nSubGraphs;i++){ 00026 delete subGraphs[i]; 00027 } 00028 delete[] subGraphs; 00029 } 00030 00031 void Graph::push(float value, int nSubGraph){ 00032 subGraphs[nSubGraph]->push(value); 00033 if(value<min) min=value; 00034 if(value>max) max=value; 00035 } 00036 00037 void Graph::drawBuffer(){ 00038 dc.Clear(); 00039 dc.SetBrush(*wxTRANSPARENT_BRUSH); 00040 dc.SetPen(*wxBLACK_PEN); 00041 dc.DrawRectangle(0,0, width, height); 00042 int effectiveWidth=width-1; 00043 int effectiveHeight=height-1; 00044 int dx=1; 00045 int dy=1; 00046 float zx=(float)effectiveWidth/bufferSize; 00047 float zy=(float)effectiveHeight/(max-min); 00048 00049 GraphData* graphData; 00050 int i, g; 00051 int x, xOld, y, yOld; 00052 for(g=0;g<nSubGraphs;g++){ 00053 graphData=subGraphs[g]; 00054 if(g==0) dc.SetPen(*wxRED_PEN); 00055 else dc.SetPen(*wxGREEN_PEN); 00056 if(graphData->dataSize){ 00057 xOld=dx; 00058 yOld=height-dy-zy*(graphData->getValue(0)-min); 00059 for(i=1;i<graphData->dataSize;i++){ 00060 x=dx+i*zx; 00061 y=height-dy-zy*(graphData->getValue(i)-min); 00062 dc.DrawLine(xOld,yOld,x,y); 00063 xOld=x; 00064 yOld=y; 00065 } 00066 } 00067 } 00068 wxString s; 00069 s.Printf("%g",max); 00070 dc.DrawText(s,dx,dy); 00071 s.Printf("%g",min); 00072 dc.DrawText(s,dx,height-dy-15); 00073 00074 } 00075 00076 void Graph::draw(wxDC* outdc, int x, int y){ 00077 drawBuffer(); 00078 outdc->Blit(x,y,width,height,&dc,0,0); 00079 }
Thyrix homepage Users' guide
(C) Arxia 2004-2005