graphwindow.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:10k
- /*
- * ===========================================================================
- * PRODUCTION $Log: graphwindow.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 20:50:01 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: graphwindow.cpp,v 1000.2 2004/06/01 20:50:01 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- *
- */
- #include <ncbi_pch.hpp>
- #include <util/range.hpp>
- #include <corelib/ncbistd.hpp>
- #include <gui/opengl/glcolortable.hpp>
- // graphs
- #include <gui/graph/pie_graph.hpp>
- #include <gui/graph/scatter_graph.hpp>
- #include <gui/graph/combo_chart.hpp>
- #include "graphwindow.hpp"
- #include <math.h>
- #include <stdio.h>
- USING_NCBI_SCOPE;
- ////////////////////////////////////////////////////////////////////////////////
- /// class CGraphWindow
- CGraphWindow::CGraphWindow(int x, int y, int w, int h)
- : CGlCanvas2d(x, y, w, h, "Graph Demo"),
- m_pScrollCont(NULL)
- {
- m_Panel.Create();
- m_Panel.SetLayout(100, 25, 100, 60);
- m_Panel.SetBackColor(CGlColor(0.95f, 0.95f, 1.0f), CGlColor(0.9f, 0.9f, 1.0f));
-
- // create on the graphs
- int iG = 0;
- switch (iG) {
- case 0: x_CreateComboChart(); break;
- case 1: x_CreatePie(); break;
- case 2: x_CreateScatter(); break;
- }
- resize(x, y, w, h);
- m_Panel.GetGraphPane().ZoomAll(true);
- }
- CGraphWindow::~CGraphWindow()
- {
- RemoveGraphsAndSources();
- }
- void CGraphWindow::ZoomAll()
- {
- m_Panel.GetGraphPane().ZoomAll();
-
- x_UpdateScrollBars();
- redraw();
- }
- void CGraphWindow::ZoomIn()
- {
- m_Panel.GetGraphPane().ZoomInCenter();
-
- x_UpdateScrollBars();
- redraw();
- }
- void CGraphWindow::ZoomOut()
- {
- m_Panel.GetGraphPane().ZoomOutCenter();
-
- x_UpdateScrollBars();
- redraw();
- }
- void CGraphWindow::AddGraph(IGraph* pGraph)
- {
- m_Panel.AddGraph(pGraph);
- m_vpGraphs.push_back(pGraph);
- }
- void CGraphWindow::AddDataSource(IGraphDataSource* pDS)
- {
- m_vpSources.push_back(pDS);
- }
- void CGraphWindow::RemoveGraphsAndSources()
- {
- m_Panel.RemoveAllGraphs();
- destroy_and_erase_elems(m_vpGraphs);
- destroy_and_erase_elems(m_vpSources);
- }
- #define BORDER 5
- void CGraphWindow::resize(int x, int y, int w, int h)
- {
- CGlCanvas2d::resize(x, y, w, h);
-
- // resize m_Panel
- m_Panel.SetRect(TVPRect(BORDER, BORDER, w - 1 - BORDER, h - 1 - BORDER));
-
- x_UpdateScrollBars();
- }
- #define MAX_SCROLL 0x10000
- void CGraphWindow::x_UpdateScrollBars()
- {
- if (m_pScrollCont) {
- TModelRect rcV = m_Panel.GetGraphPane().GetVisibleRect();
- TModelRect rcL = m_Panel.GetGraphPane().GetModelLimitsRect();
- double PageN = rcV.Width() / rcL.Width();
- double PosN = (rcV.Left() - rcL.Left()) / rcL.Width();
- m_pScrollCont->SetValuesX((int) (PosN * MAX_SCROLL), (int)(PageN * MAX_SCROLL), 1, MAX_SCROLL);
- PageN = rcV.Height() / rcL.Height();
- PosN = (rcL.Top() - rcV.Top()) / rcL.Height();
- m_pScrollCont->SetValuesY((int)(PosN * MAX_SCROLL), (int)(PageN * MAX_SCROLL), 1, MAX_SCROLL);
- }
- }
- void CGraphWindow::SetContainer(IScrollContainer* pCont)
- {
- m_pScrollCont = pCont;
- if(m_pScrollCont)
- x_UpdateScrollBars();
- }
- void CGraphWindow::OnScrollX(Fl_Scrollbar* pBar)
- {
- double PosN = ((double) pBar->value()) / MAX_SCROLL;
-
- TModelRect rcV = m_Panel.GetGraphPane().GetVisibleRect();
- TModelRect rcL = m_Panel.GetGraphPane().GetModelLimitsRect();
- double Pos = PosN* rcL.Width() + rcL.Left();
- double Shift = Pos - rcV.Left();
- rcV.Offset(Shift, 0);
- m_Panel.GetGraphPane().SetVisibleRect(rcV);
-
- redraw();
- }
- void CGraphWindow::OnScrollY(Fl_Scrollbar* pBar)
- {
- double PosN = ((double) pBar->value()) / MAX_SCROLL;
-
- TModelRect rcV = m_Panel.GetGraphPane().GetVisibleRect();
- TModelRect rcL = m_Panel.GetGraphPane().GetModelLimitsRect();
- double Pos = rcL.Top() - (PosN * rcL.Height());
- double Shift = Pos - rcV.Top();
- rcV.Offset(0, Shift);
- m_Panel.GetGraphPane().SetVisibleRect(rcV);
-
- redraw();
- }
- void CGraphWindow::draw()
- {
- m_Panel.Render();
- }
- void CGraphWindow::x_CreateComboChart()
- {
- RemoveGraphsAndSources();
-
- int ElemsN = 20;
- int SeriesN = 4;
-
- CComboChartDataSource *pChartDS = new CComboChartDataSource(SeriesN, ElemsN);
- pChartDS->CreateArrays();
-
- for( int i = 0; i< SeriesN; i++ ) {
- CComboChartDataSource::TValueCont& Cont = pChartDS->GetValueContainer(i);
- for( int j = 0; j < ElemsN; j++ ) {
- Cont[j] = sin(i + (i/10 + 0.05)*j) + j * 0.01;
-
- char S[30];
- sprintf(S, "Series %d", i);
- pChartDS->GetLabelContainer()[i] = S;
- }
- }
-
- CComboChart* pChart = new CComboChart();
- //pChart->SetStyle(CComboChart::eBarChart); //eStackedBarChart, ePercentBarChart
-
- pChart->SetDataSource(pChartDS);
- pChart->AssignAutoMarkers();
- pChart->AssignAutoColors();
-
- //m_Panel.GetGraphPane().SetModelLimitsRect(pChart->GetLimits());
-
- AddGraph(static_cast<IGraph*>(pChart));
- AddDataSource(static_cast<IGraphDataSource*>(pChartDS));
-
- m_Panel.GetLegend()->SetDataSource(static_cast<IGraphDataSource*>(pChart));
- m_Panel.GetGraphPane().EnableZoom(true, false);
- m_Panel.SetIntegerMode(true, false);
- }
- void CGraphWindow::x_CreatePie()
- {
- RemoveGraphsAndSources();
- int ElemsN = 10;
- CGlColorTable Table(ElemsN);
- CPieDataSource* pPieDS = new CPieDataSource(ElemsN);
- pPieDS->CreateArrays();
- for( int i=0; i<ElemsN; i++ ) {
- pPieDS->GetValueContainer()[i] = i + 1;
- pPieDS->GetColorContainer()[i] = Table.GetColor(i);
-
- char S[30];
- sprintf(S, "Sector N %d", i);
- pPieDS->GetStringContainer()[i] = S;
- }
- IGraphDataSource* pIPDS = static_cast<IGraphDataSource*>(pPieDS);
- CPieGraph* pGraph = new CPieGraph();
- pGraph->SetDataSource(pIPDS);
- m_Panel.SetDrawAxes(false);
- m_Panel.SetDrawGrid(false);
- m_Panel.GetGraphPane().SetModelLimitsRect(pGraph->GetLimits());
- AddGraph(static_cast<IGraph*>(pGraph));
- AddDataSource(static_cast<IGraphDataSource*>(pPieDS));
- m_Panel.GetLegend()->SetDataSource(static_cast<IGraphDataSource*>(pGraph));
- m_Panel.GetGraphPane().EnableZoom(true, true); //###
- m_Panel.GetGraphPane().SetProportionalMode(true); //###
- }
- typedef CScatterDataSource<double, double> TScatterDS;
- void CGraphWindow::x_CreateScatter()
- {
- RemoveGraphsAndSources();
-
- int ElemsN = 500;
- TScatterDS* pScatterDS = new TScatterDS(ElemsN);
- pScatterDS->CreateArrays();
- for( int i=0; i<ElemsN; i++) {
- pScatterDS->GetXContainer()[i] = 1000 * (sin(0.013*i)*2 + sin(0.1 * i));
- pScatterDS->GetYContainer()[i] = cos(0.17 *i) + sin(0.04 * i)*3;
- }
- CScatterGraph* pGraph = new CScatterGraph();
- pGraph->SetDataSource(pScatterDS);
- pGraph->SetColor(CGlColor(0.0f, 0.0f, 1.0f));
- pGraph->SetMarkerType(CGraphDotMarker::eDiamond);
- pGraph->SetDrawLines(false);
- TModelRect rcAll(pGraph->GetLimits());
- AddGraph(static_cast<IGraph*>(pGraph));
- AddDataSource(static_cast<IGraphDataSource*>(pScatterDS));
- pScatterDS = new TScatterDS(ElemsN);
- pScatterDS->CreateArrays();
- for( int i=0; i<ElemsN; i++) {
- pScatterDS->GetXContainer()[i] = - 600 + 1200 * (sin(0.07*i)*2 + sin(0.1 * i) );
- pScatterDS->GetYContainer()[i] = cos(0.17 *i) + sin(0.04 * i)*5;
- }
- pGraph = new CScatterGraph();
- pGraph->SetDataSource(pScatterDS);
- pGraph->SetColor(CGlColor(0.0f, 1.f, 0.0f));
- pGraph->SetMarkerType(CGraphDotMarker::eNone);
- pGraph->SetDrawLines(true);
-
- AddGraph(static_cast<IGraph*>(pGraph));
- AddDataSource(static_cast<IGraphDataSource*>(pScatterDS));
- rcAll.CombineWith(pGraph->GetLimits());
- m_Panel.GetGraphPane().SetModelLimitsRect(rcAll);
- }
- /*
- * ===========================================================================
- * $Log: graphwindow.cpp,v $
- * Revision 1000.2 2004/06/01 20:50:01 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.9
- *
- * Revision 1.9 2004/05/21 22:27:43 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.8 2003/12/10 21:32:24 ucko
- * +<stdio.h> for sprintf()
- *
- * Revision 1.7 2003/09/24 19:55:41 yazhuk
- * Enforced zooming in the first ZoomAll() call.
- *
- * Revision 1.6 2003/08/28 19:26:21 yazhuk
- * Changed CGlPane function names
- *
- * Revision 1.5 2003/08/14 18:04:00 yazhuk
- * Refactored CGraphWindow in order to use CGraphPanel
- *
- * Revision 1.4 2003/08/11 19:20:35 yazhuk
- * Cosmetic changes
- *
- * Revision 1.3 2003/08/11 16:10:58 yazhuk
- * Compilation fixes for GCC
- *
- * Revision 1.2 2003/08/08 15:59:37 yazhuk
- * Comments added
- *
- * ===========================================================================
- */