Search code examples
c#zedgraph

ZedGraph: Custom Labels on XAxis.DateAsOrdinal


I am confounded by this problem: I have a WinForm with ZedGraph object plotting a simple curve. It has a custom text object "C" above each point. When I change the XAxis.Type to DateAsOrdinal, the "C" text label disappears. How can I get "C" with DateAsOrdinal to work?

The code is below for anyone to try:

    private void DrawChart( ZedGraphControl zGraph ) {
        GraphPane pane = zGraph.GraphPane;
        pane.Title.Text = "My Sample Test";
        pane.XAxis.Title.Text = "Date";
        pane.YAxis.Title.Text = "Price";

        // Uncomment below line: all "C" labels disappear!
        //pane.XAxis.Type = AxisType.DateAsOrdinal;

        double x, y;
        PointPairList points = new PointPairList();
        DateTime day = new DateTime( 2012, 1, 1 );

        for ( int i = 0 ; i < 36 ; i++ ) {
            x = day.ToOADate();
            y = 1.5 + Math.Sin( (double)i * 0.2 );
            points.Add( x, y );

            // this label disappears when XAxis.Type = DateAsOrdinal!
            TextObj text = new TextObj( "C", x, y + 0.1, CoordType.AxisXYScale, AlignH.Center, AlignV.Center );
            text.ZOrder = ZOrder.A_InFront;
            text.FontSpec.Border.IsVisible = false;
            pane.GraphObjList.Add( text );

            day = day.AddDays( 1 );   // goto next day (x-coord)
        }

        LineItem curve = pane.AddCurve( "Stock", points, Color.Black, SymbolType.None );

        zGraph.AxisChange();
    }

Solution

  • Try

    new TextObj("C", i + 1, y + .....

    Ordinal XAxis means as expression implies that x values will run 1, 2, 3...