简体   繁体   English

如何在执行时更新图表中的数据(在C ++构建器中)?

[英]How can I update data in a chart in execution time (in C++ builder)?

I'm using Borland C++ Builder to create this. 我正在使用Borland C ++ Builder来创建它。 The code is very simple because its only purpose, for now, is to help me learn how to use the TChart functions. 该代码非常简单,因为到目前为止,它的唯一目的是帮助我学习如何使用TChart函数。 I'll use what I learn to create a more complex program later. 稍后,我将使用所学的知识来创建一个更复杂的程序。

I have a series of numbers that must be shown on a memo box and on a chart. 我有一系列数字,必须在备注框和图表上显示这些数字。 The values in the chart are displayed after the program finishes its exectuion, however, I need the values to be updated in real time - I mean, everytime the program calculates a new number, it must be immediately show on the chart. 程序完成执行后将显示图表中的值,但是,我需要实时更新这些值-我的意思是,每次程序计算一个新数字时,都必须立即在图表上显示它。 Is it possible to do that? 有可能这样做吗? If so, how can I do it? 如果是这样,我该怎么办?

Thanks in advance. 提前致谢。

#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

   TChartSeries* series1 = Chart1->Series[0];
   series1->Clear();

   int num = 0;

   Memo1->Clear();

     for(int i=0; i<5000; i++)
     {
            num = num++;
            Memo1->Lines->Add(IntToStr(num));
            series1->AddXY(i, num, "", clGreen);

           }
   }

You should force a chart repaint whenever you want: 您应该在任何需要的时候强制重绘图表:

Chart1->Repaint();

So you could have: 因此,您可以:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TChartSeries* series1 = Chart1->Series[0];
    series1->Clear();

    int num = 0;

    Memo1->Clear();

    for(int i=0; i<5000; i++)
    {
        num = num++;
        Memo1->Lines->Add(IntToStr(num));
        series1->AddXY(i, num, "", clGreen);
        Chart1->Repaint();
    }
}

Or, to improve the performance, you could force a chart repaint after adding some values instead of after each addition. 或者,为了提高性能,您可以在添加一些值后而不是每次添加后强制重新绘制图表。 Ie: 即:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TChartSeries* series1 = Chart1->Series[0];
    series1->Clear();

    int num = 0;

    Memo1->Clear();

    for(int i=0; i<5000; i++)
    {
        num = num++;
        Memo1->Lines->Add(IntToStr(num));
        series1->AddXY(i, num, "", clGreen);

        if (i % 100 == 0) {
            Chart1->Repaint();
        }
    }
}

Yes, this is an old thread but I have a suggestion for anyone else who runs across it. 是的,这是一个旧线程,但是我建议其他遇到该线程的人。 You can also repaint just the series which maybe requires less overhead than repainting the entire chart. 您也可以只重绘系列,这可能比重绘整个图表所需的开销更少。 To do that use the TChartSeries repaint method. 为此,请使用TChartSeries重绘方法。 For the given example then you would put a "series1->Repaint();" 对于给定的示例,您将放置“ series1-> Repaint();” somewhere, inside the for loop I guess. 我猜在for循环内的某个地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM