如何使用zedgraph.dll制作实时图形 [英] How do I make a real-time graph with zedgraph.dll

查看:85
本文介绍了如何使用zedgraph.dll制作实时图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在VC ++ 2008项目的Windows窗体应用程序中有一个用zedgraph.dll制作的静态图,但是我需要它以动态形式移动,同时程序一步一步地计算两个变量的每个值. 此链接中的示例使用RollingPointPairlist()和一个计时器,但是我不知道.不需要它是因为我的程序可以快速地用数值方法求解方程式(以毫秒为单位),并且计时器非常慢;我的程序使用许多"for"循环,该示例仅使用一个简单的正弦函数.我同时尝试了Rolling和PoitnPairList,但它不起作用.

主要问题是点对点绘制图形,或逐步绘制变量x [0]和x [1],只需几毫秒或很长的时间即可解决.

静态图形式有一段代码.h:

Hello, I have a static graph made with zedgraph.dll in a windows forms application in a VC++ 2008 project, but I need that it move in dynamic form while the program calculate each value of two variables in a step. The sample in this link use RollingPointPairlist() and a timer, but I don''t need it because my program solve whith numerical methods an equation very fast, in milliseconds, and the timer is very slow; my program uses many "for" cycles and the example only a simple sine function. I tried with both Rolling and PoitnPairList and it doesn''t work.

The main problem is to graph point to point, or step by step the variables x[0] and x[1] it can be solved in only few ms or in long seconds.

There is a piece of code in a static graph form.h :

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 Graficar(zedGraphControl1);//here create the base graph
                 resolver();//here solve numerical methods
             }



creatheGraph()的一部分:



Part of creatheGraph():

void Graficar( ZedGraphControl ^zgc ){

            GraphPane ^myPane = zgc->GraphPane;

           // Set the titles and axis labels
            myPane->Title->Text = "ETDM";
            myPane->XAxis->Title->Text = "X1";
            myPane->YAxis->Title->Text = "X2";

            // Make up some data points from the Sine function
            PointPairList ^list = gcnew PointPairList();//++
            // Save 1200 points.  At 50 ms sample rate, this is one minute
            // The RollingPointPairList is an efficient storage class that always
            // keeps a rolling set of point data without needing to shift any data values
            //RollingPointPairList ^list = gcnew RollingPointPairList(5000);
            // Generate a blue curve without symbols, and " " in the legend
            LineItem ^myCurve = myPane->AddCurve( "ETD", list, Color::Blue,
            SymbolType::None );//++

           
            // Fill the axis background with a color gradient
            myPane->Chart->Fill = gcnew Fill( Color::White, Color::LightGray, 45.0f );

            // Muestra el grid
            myPane->YAxis->MajorGrid->IsVisible = true;
            myPane->XAxis->MajorGrid->IsVisible = true;

            // No muestra la línea de y=0
            myPane->YAxis->MajorGrid->IsZeroLine = false;

            // Fill the pane background with a color gradient
            //myPane->Fill = gcnew Fill( Color::White, Color::FromArgb(220, 220, 255), 45.0f );

            // Calculate the Axis Scale Ranges
            zgc->AxisChange();

        }



我的程序的一部分:



Part of my program:

void resolver(void){
        double num_tot_estrat;
        int selec_g;

        LineItem ^myCurve = dynamic_cast<LineItem ^>(zedGraphControl1->GraphPane->CurveList[0]);
        IPointListEdit ^list = dynamic_cast <IPointListEdit^>(myCurve->Points);
        //PointPairList ^list = gcnew PointPairList();

        num_tot_estrat = Math::Pow(2.0,M); //Número total de estrategias
        for(num_estrat=0; num_estrat < num_tot_estrat; num_estrat++)
        {
            num_ecu = 0;
            selec_g = num_estrat;

            for(j=M-1; j>=0; j--) //<- 0 Ecuación en el sistema no lineal
            {
                ec_pres_snl[j] = selec_g & 1;
                if(!ec_pres_snl[j])//<- 1 Ecuación en phi
                num_ecu ++;
                selec_g >>= 1; //Recorre el bit para u
            }
            condiciones_iniciales();
            if(num_estrat==1) {
                list->Add(xn[0],xn[1]);
                //zedGraphControl1->AxisChange();
                //zedGraphControl1->Invalidate();
            }

            do{
                gradiente();//Aquí va el gradiente
                for(j=0; j<K; j++)
                    xn[j] -= h*dFdx[j];  //integra por Euler de x[0] a x[K]
                for(j=K; j<N; j++)
                if(ec_pres_snl[j-K])
                    xn[j] -= h*dFdx[j];  //integra por Euler de x[K] a x[N]
                ///////GRaficas//////////////////////////////
                if (num_estrat==1)
                {
                    list->Add(xn[0],xn[1]);            //here I have the solution. I want to graph this
                                    }
                /////////////////////////////////////////////
                i++;
                
            }while((Fx > epsilon));//&& (i < 40.0));
        }
        zedGraphControl1->AxisChange();
        //zedGraphControl1->Invalidate();

    }





Any help, thank you!

推荐答案

如果要在每个新点之后刷新图形,则需要在添加该点后立即调用AxisChange. />
之后,如果您看不到图形动态更新,则可能是因为您在与主UI线程相同的线程中运行resolver.然后考虑使用BackgroundWorker并从BackgroundWorker调用解析器.

如果问题仅与刷新时间有关,则只需测量循环中经过的时间,以便您可以根据希望刷新图形的速度来决定是否减慢循环速度.例如:

If you want to refresh your graph after each new point, you need to call AxisChange just after adding the point.

After that, if you can''t see your graph updating dynamically, it is probably because you are running resolver in the same thread as the main UI thread --> then consider using a BackgroundWorker and call resolver from the BackgroundWorker.

If the problem is only about refreshing time, then just measure the time elapsed in your loop so you can decide to slow down your loop or not depending on how fast you want your graph to be refreshed. For example:

int t0 = Environment::TickCount;
//refresh every second
int refreshEvery_seconds = 1;
for (///...)
{
   ///your code
   ///...

   if (num_estrat==1)
   {
       list->Add(xn[0],xn[1]);
       ///deltaT measures the time spent since last refresh
       int t = Environment::TickCount;
       int deltaT = t - t0;
       int timeToWait = refreshEvery_seconds * 1000 - deltaT;
       if (timeToWait > 0)
       {
           ///wait a little bit before refreshing the graph
           System::Threading::Thread::Sleep(timeToWait);
       }
       zedGraphControl1->AxisChange();
       t0 = t;
    }
}


这篇关于如何使用zedgraph.dll制作实时图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆