//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PhotonGraph.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

__fastcall TPhotonGraph::TPhotonGraph()
{
}

__fastcall TPhotonGraph::TPhotonGraph(TImage* image)
{
        ImageGraph = image ;

        BackGround = new Graphics::TBitmap() ;
        Graph1 = new Graphics::TBitmap() ;
        Graph2 = new Graphics::TBitmap() ;
        Graph3 = new Graphics::TBitmap() ;
        Graph4 = new Graphics::TBitmap() ;

        BackGround->Width = image->Width ;
        BackGround->Height = image->Height ;

        Max_Current = 50 ;
        Max_Voltage = 2 ;
        Max_Power = 10 ;
        Max_SE = 0.8 ;
        Max_Resis = 30 ;

        Graph_Area() ;
}

__fastcall TPhotonGraph::~TPhotonGraph()
{
        delete Graph1 ;
        delete Graph2 ;
        delete Graph3 ;
        delete Graph4 ;
        delete BackGround ;
}

void __fastcall TPhotonGraph::Draw_BackGround(bool backcolor)
{
        int vert, Hori ;
        LOGFONT lf ; 

        if(backcolor)
        {
                BitBlt(BackGround->Canvas->Handle,0,0,BackGround->Width, BackGround->Height, NULL,0,0,WHITENESS) ;
                BackGround->Canvas->Brush->Color = clBlack ;
                BackGround->Canvas->FloodFill(20, 20,clBlack, 1) ;
        }
        else
        {
                BitBlt(BackGround->Canvas->Handle,0,0,BackGround->Width, BackGround->Height,
                             NULL,0,0,BLACKNESS) ;
                BackGround->Canvas->Brush->Color = clWhite ;
                BackGround->Canvas->FloodFill(20, 20,clWhite, 1) ;
        }

        vert = m_Rect.Width() / SECTION ;
        Hori = m_Rect.Height() / SECTION ;

        BackGround->Canvas->Pen->Style = psSolid ;

        if(backcolor) BackGround->Canvas->Pen->Color = clWhite ;
        else BackGround->Canvas->Pen->Color = clBlack ;

        BackGround->Canvas->Rectangle(m_Rect) ;

        BackGround->Canvas->MoveTo(m_Rect.Left / 2, m_Rect.Top) ;
        BackGround->Canvas->LineTo(m_Rect.Left / 2, m_Rect.Bottom) ;
        BackGround->Canvas->MoveTo((m_Rect.Left / 2)-5, m_Rect.Top) ;
        BackGround->Canvas->LineTo((m_Rect.Left / 2)+5, m_Rect.Top) ;
        BackGround->Canvas->MoveTo((m_Rect.Left / 2)-5, m_Rect.Bottom) ;
        BackGround->Canvas->LineTo((m_Rect.Left / 2)+5, m_Rect.Bottom) ;

        BackGround->Canvas->MoveTo(m_Rect.Right + (m_Rect.Left / 2), m_Rect.Top) ;
        BackGround->Canvas->LineTo(m_Rect.Right + (m_Rect.Left / 2), m_Rect.Bottom) ;
        BackGround->Canvas->MoveTo((m_Rect.Right + (m_Rect.Left / 2))-5, m_Rect.Top) ;
        BackGround->Canvas->LineTo((m_Rect.Right + (m_Rect.Left / 2))+5, m_Rect.Top) ;
        BackGround->Canvas->MoveTo((m_Rect.Right + (m_Rect.Left / 2))-5, m_Rect.Bottom) ;
        BackGround->Canvas->LineTo((m_Rect.Right + (m_Rect.Left / 2))+5, m_Rect.Bottom) ;

        BackGround->Canvas->Pen->Style = psDot ;

        if(backcolor) BackGround->Canvas->Font->Color = clWhite ;
        else BackGround->Canvas->Font->Color = clBlack ;

        for(int i = 1; i< SECTION;i++)
        {
                BackGround->Canvas->MoveTo(m_Rect.Left + (vert * i), m_Rect.Top) ;
                BackGround->Canvas->LineTo(m_Rect.Left + (vert * i), m_Rect.Bottom) ;
        }

        for(int i = 1; i< SECTION ;i++)
        {
                BackGround->Canvas->MoveTo(m_Rect.Left, m_Rect.Top + (Hori * i)) ;
                BackGround->Canvas->LineTo(m_Rect.Right,m_Rect.Top + (Hori * i)) ;
        }

        for(int i=0; i< SECTION+1 ; i++)
        {
                BackGround->Canvas->TextOut(m_Rect.Left + (vert * i)- 10 ,m_Rect.Bottom+5
                                          , FormatFloat("#0.0",(float)(Max_Current*i)/SECTION)) ;
                BackGround->Canvas->TextOut(m_Rect.Left-25 , m_Rect.Bottom -(Hori * i) - 8
                                          , FormatFloat("#0.0",(float)(Max_Power*i)/SECTION)) ;
                BackGround->Canvas->TextOut(m_Rect.Right+5 , m_Rect.Bottom -(Hori * i) - 8
                                          , FormatFloat("#0.0",(float)(Max_SE*i)/SECTION)) ;
                BackGround->Canvas->TextOut((m_Rect.Left / 2)-25 , m_Rect.Bottom -(Hori * i) - 8
                                          , FormatFloat("#0.0",(float)(Max_Voltage*i)/SECTION)) ;
                BackGround->Canvas->TextOut((m_Rect.Right + (m_Rect.Left / 2))+8 , m_Rect.Bottom -(Hori * i) - 8
                                          , FormatFloat("#0",(float)(Max_Resis*i)/SECTION)) ;
        }

        BackGround->Canvas->TextOut(BackGround->Width / 2  ,BackGround->Height - 20, "If(mA)") ;

        ZeroMemory(&lf, sizeof(LOGFONT));
        lf.lfHeight = 11;
        lf.lfEscapement = 10 * 90; // degrees to rotate
        BackGround->Canvas->Font->Handle = CreateFontIndirect(&lf);

        BackGround->Canvas->TextOut(BackGround->Width - 15,BackGround->Height/2,"Rd(Ohm)") ;
        BackGround->Canvas->TextOut(5,BackGround->Height/2,"Vf(V)") ;
        BackGround->Canvas->TextOut(43,BackGround->Height/2,"Po(mW)") ;
        BackGround->Canvas->TextOut(BackGround->Width - 55  ,BackGround->Height/2,"SE(W/A)") ;

        ImageGraph->Picture->Bitmap =  BackGround  ;

        BackGround->Canvas->Font->Handle = NULL ;
}

void __fastcall TPhotonGraph::Graph_Area(void)
{
        int temp, temp1 ;

        m_Rect.Left = SIDE_SIZE ;
        m_Rect.Top = TOP_SIZE ;
        m_Rect.Right = ImageGraph->Width - SIDE_SIZE;
        m_Rect.Bottom = ImageGraph->Height - BOTTOM_SIZE ;

        temp = m_Rect.Width() % SECTION;
        temp1 = temp / 2 ;
        m_Rect.Left += temp1  ;
        m_Rect.Right -= temp - temp1 ;

        temp = m_Rect.Height() % SECTION ;
        m_Rect.Bottom -= temp ;

        Graph1->Width = m_Rect.Width() ;
        Graph1->Height = m_Rect.Height() ;

        Graph2->Width = m_Rect.Width() ;
        Graph2->Height = m_Rect.Height() ;

        Graph3->Width = m_Rect.Width() ;
        Graph3->Height = m_Rect.Height() ;

        Graph4->Width = m_Rect.Width() ;
        Graph4->Height = m_Rect.Height() ;
}

void __fastcall TPhotonGraph::Max_Value_Change(double value, int item)
{
        switch(item)
        {
        case 0 : Max_Current = value ;
                 break ;
        case 1 : Max_Voltage = value ;
                 break ;
        case 2 : Max_Power = value ;
                 break ;
        case 3 : Max_SE = value ;
                 break ;
        case 4 : Max_Resis = value ;
                 break ;
        }
}

void __fastcall TPhotonGraph::Delete_Graph(int spec)
{
        Graphics::TBitmap *Graph ;

        switch(spec)
        {
        case 1 : Graph = Graph1 ;   break ;
        case 2 : Graph = Graph2 ;   break ;
        case 3 : Graph = Graph3 ;   break ;
        case 4 : Graph = Graph4 ;   break ;
        }

        BitBlt(Graph->Canvas->Handle,0,0,m_Rect.Width(), m_Rect.Height(),
                        NULL,0,0,WHITENESS) ;
        Graph->Canvas->Brush->Color = clBlack ;
        Graph->Canvas->FloodFill(20, 20,clBlack, 1) ;
}

void __fastcall TPhotonGraph::Display_Graph(int spec)
{
        Graphics::TBitmap *Graph ;
        switch(spec)
        {
        case 1 : Graph = Graph1 ;  break ;
        case 2 : Graph = Graph2 ;  break ;
        case 3 : Graph = Graph3 ;  break ;
        case 4 : Graph = Graph4 ;  break ;
        }

        Graph->Transparent = true ;
        Graph->TransparentColor = clBlack ;
        Graph->TransparentMode = tmAuto ;

        BackGround->Canvas->Draw(m_Rect.Left, m_Rect.Top,Graph) ;

        ImageGraph->Picture->Bitmap =  BackGround  ;
}

void __fastcall TPhotonGraph::Draw_Graph(double x, double y, int spec, TColor color)
{
        Graphics::TBitmap *Graph ;
        double Max_Value ;
        int X, Y ;

        switch(spec)
        {
        case 1 : Graph = Graph1 ;
                 Max_Value = Max_Resis  ;
                 break ;
        case 2 : Graph = Graph2 ;
                 Max_Value = Max_Voltage  ;
                 break ;
        case 3 : Graph = Graph3 ;
                 Max_Value = Max_Power  ;
                 break ;
        case 4 : Graph = Graph4 ;
                 Max_Value = Max_SE  ;
                 break ;
        }
        Graph->Canvas->Pen->Color = color ;

        X = (m_Rect.Width() * x) / Max_Current ;
        Y = (m_Rect.Height() * y) / Max_Value  ;

        Y =  m_Rect.Height() - Y ;

        if(preX[spec-1]==0 && preY[spec-1]==m_Rect.Height())
        {
                Graph->Canvas->MoveTo(X,Y) ;
        }
        else    Graph->Canvas->MoveTo(preX[spec-1], preY[spec-1]) ;

        Graph->Canvas->LineTo(X, Y) ;

        preX[spec-1] = X ;
        preY[spec-1] = Y ;
}

void __fastcall TPhotonGraph::Reset_XY(void)
{
        for(int i=0;i<4;i++)
        {
                preX[i] = 0 ;
                preY[i] = m_Rect.Height() ;
        }
}


