Search code examples
datetimezedgraphmanaged-c++

How can I draw a graph with a date axis using C++ and ZedGraph?


I have successfully used the C++ example project to draw graphs from my C++ project using ZedGraph. However there is no example with Date axis for C++.

The following code is taken from the C# example found at http://zedgraph.org/wiki/index.php?title=Tutorial:Date_Axis_Chart_Demo. Please see my comments with the text //JEM// to see where my problem is

    PointPairList list = new PointPairList();
    for ( int i=0; i<36; i++ )
    {
        double x = (double) new XDate( 1995, 5, i+11 );

        >  //JEM //This line above doesn't work in
        > C++.

        double y = Math.Sin( (double) i * Math.PI / 15.0 );
        list.Add( x, y );
    }

    ....missing code...

    // Set the XAxis to date type
    myPane.XAxis.Type = AxisType.Date;

    //JEM //This one also doesn't work even if I change it to the
    //syntax that C++ understands, that is,
    myPane->XAxis->Type = AxisType->Date;

Solution

  • Thanks Gacek. This is how it finally went down. Your answer was the turning point!!!

        for ( int i = 0; i < basin.DY; i++ ){
               XDate dato(1995,9,i,0,0,0); //date
               double x = (double)dato;
                //double x =  i;     
               double y =  basin.Qsim[i];     
               double y2 = basin.Qobs[i];     
                list->Add( x, y );     
                list2->Add( x, y2 );
        }
        //set the XAXis to date type
        myPane->XAxis->Type = AxisType::Date;
    

    here is the constructor for the Xdate type for c++ from sourceforge dot net documentation/html/M_ZedGraph_XDate__ctor_3.htm. XDate (int year, int month, int day, int hour, int minute, double second)

    I have also found a detailed example on this link http://www.c-plusplus.de/forum/viewtopic-var-t-is-186422-and-view-is-next.html with the following code

            /// ZedGraph Kurve //////////
    private: void CreateGraph( ZedGraphControl ^zgc )
    {
       GraphPane ^myPane = zgc->GraphPane;
    
       // Set the titles and axis labels
       myPane->Title->Text = "Gewichtskurve";
       myPane->XAxis->Title->Text = "Tag";
       myPane->YAxis->Title->Text = "Gewicht in Kg";
    
    
       // Make up some data points from the Sine function
        double x,y;
       PointPairList ^list = gcnew PointPairList();
       for ( int i=0; i<36; i++ )
       {
         x = (double) gcnew XDate( 1995, 5, i+11 );
          y = Math::Sin( (double) i * Math::PI / 15.0 );
          list->Add( x, y );
       }
    
       // Generate a blue curve with circle symbols, and "My Curve 2" in the legend
           LineItem ^myCurve = myPane->AddCurve( "Trainingskurve", list, Color::Blue,
           SymbolType::Circle );
    
           XDate ^xdatum = gcnew XDate ( 1995, 1, 1);
           xdatum->AddDays ( 1 );
    
           myPane->XAxis->Type = AxisType::Date;