Simple y(x) plot with Microsoft Chart Controls. How to add data points at run-time? RRS feed

  • Frage

  • Hello, I want to plot a simple 2D graph in Visual Studio C++.
    I think that the Microsoft Chart Controls, which are already
    in Visual Studio 2010, are suited for this.
    Unfortunately I have problems getting started.

    I have made a small test project which I would like to attach, if possible.
    Here is a description of the test project:

    To generate (x,y) values, a function y(x) is defined:
    double MyFunc(double x)

    For the data points, arrays x[] and y[] are allocated in the constructor:
        x = new double[NMAX];
        y = new double[NMAX];

    In the Form, there is a 'button1' with the text "Generate values (x,y)".
    When the user clicks button1, the (x,y) values are printed
    in a multi-line textbox.
    ( Event function button1_Click(...) )

    Now I want to make a diagram where the function y(x) is plotted,
    a line-plot of y versus x.
    Thus, in the Windows Forms Designer I dragged a chart from the toolbox
    into the Windows Form, and Visual Studio made the necessary definitions
    in the InitializeComponent() function. So far so good.

    But now comes the barrier. How can I add the data points to the chart
    at run-time?
    I find it difficult to understand the Online Help ("F1" key on chart control).
    I have found that there is a class "Chart".
    It has the property "Series" (Chart.Series),
    which gets a "SeriesCollection" object.
    The class SeriesCollection has the method Add().
    Then, there is also a "DataPointCollection" class,
    but I don't know how this class is connected to Chart and SeriesCollection.
    The DataPointCollection class has the method AddXY(Double, Double).
    Then, there is also a class "DataPoint".
    So, how I can combine these classes?
    What I need is simple example(s) how to do add datapoints into the chart
    with Visual C++.

    Thank you very much for any help. Best regards,

    Freitag, 3. September 2010 17:54


  • Hi Nayel,

    this is a german speaking forum; for an english forum see Chart Controls for .NET Framework

    As there are no C++/CLI samples you should look at the
    Samples Environment for Microsoft Chart Controls

    and translate the C# Sample Code or start learning C# ;-)

    A short sample using the data from question I answered yesterday :
      // additional namespaces
      using namespace System::Collections::Generic;
      using namespace System::Windows::Forms::DataVisualization::Charting;
      System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
        // Legend Title
        this->chart1->Legends[0]->Title = L"Legend Title";
        Series^ serie = this->chart1->Series[0]; // Default Series
        // Series Title in Legend
        serie->Name = L"Series Name";
        // Default Color
        serie->Color = Color::DarkBlue;
        // Chart Type
        serie->ChartType = SeriesChartType::Line; // SeriesChartType::Column; 
        // Clear old values and adding new DataPoints
        for each(DataPoint^ point in this->GetDataPoints())
      System::Collections::Generic::List<DataPoint^>^ GetDataPoints()
        // List of DataPoints
        auto dataPoints = gcnew List<DataPoint^>();
        // Emulation LINQ Count 
        int teiler = 90;
        int limit = teiler;
        int group = 0;
        int groupCount = 0;
        for each(int value in this->GetList())
          if (value >= limit)
            DataPoint^ point = gcnew DataPoint(group + 1, groupCount);
            // Individual Color
            //point->Color = Color::DarkBlue;
            group++, groupCount = 0, limit += teiler;
        return dataPoints;
      System::Collections::Generic::List<int>^ GetList() 
        // Generate sample data 0 - 4095
        auto list = gcnew List<int>();
        Random^ r = gcnew Random();
        for (int index = 0; index <= 10000; index++)
        return list;

    (LINQ emulated as it also missing)

    HTH Elmar

    Samstag, 4. September 2010 07:57