Zedgraph - how to customize date based X-Axis tics
Asked Answered
F

1

7

SITUATION enter image description here

I am building a chart using ZedGraph of price (Y axis) against time (X axis). The duration of time is three years.

At the moment I'm getting X axis labels of : Jan 11; Jan 12; Jan 13 for a set of data that runs from 3-Mar-2010 to 2-Mar-2013.

As far as I can see this is default behaviour if the axis is of type DateTime.

QUESTION

How do I change the X axis labelling so that I get: Mar 11; Mar 12; Mar 13 ? And more generally so that I can change the labels used to coincide with the start/end month of the data.


EDIT:

My initial attempt at this question was a little ambiguous so I'm just going to try to clarify.

It's not that I want the labels to be dd-MMM-yy - what I want is to able to control the locations on the X axis where the labels/tics appear.

So that, for an X-axis that spanned 3-Mar-2010 to 2-Mar-2013, instead of the labels always appearing in January

  • Jan 11 [that is January 2011];
  • Jan 12 [that is January 2012];
  • Jan 13 [that is January 2013)

as shown in my screen dump I can choose which month the label/tic appears in . So for that data set I would like to have labels at :

  • March 2010 (appearing as Mar10)
  • March 2011 (appearing as Mar11)
  • March 2012 (appearing as Mar12)
  • March 2013 (appearing as Mar13)

I hope that's clearer.

Flaxen answered 5/3, 2013 at 1:51 Comment(3)
Also if you want to say a specific day as a static variable you can use DateTime day = new DateTime( 2012, 1, 1 );Ireful
Have you tried setting myPane.XAxis.Type = AxisType.DateAsOrdinal and used a custom label? Use this as a reference. Also you may need to set Scale.Format = "MM-yy" and see if that works. I've never tried it myself.Ireful
Check out my edit in my answer. I think it will work; you may have to fiddle with it a little bit but hopefully it will work out!Ireful
I
8

You need to set the x-axis properties to

myPane.XAxis.Title.Text = "Date";
myPane.XAxis.Type = AxisType.Date;
myPane.XAxis.Scale.Format = "dd-MMM-yy";
myPane.XAxis.Scale.MajorUnit = DateUnit.Day;
myPane.XAxis.Scale.MajorStep = 1;
myPane.XAxis.Scale.Min = new XDate(DateTime.Now.AddDays(8));
myPane.XAxis.Scale.Max = new XDate(DateTime.Now.AddDays(11));

That would give you the dates you requested; I know you can put a minus sign in the AddDays method if you want to count down from today instead, and you can set dates specifically too (just look at the autocomplete when you start typing it).

Hope this helps! Good luck!

EDIT:

So here is what I could figure out to get those custom ticks: You have to use TextObj labels. You'll also have to get rid of the original ticks:

pane1.MasterPane[0].XAxis.Scale.IsVisible = false;
pane1.MasterPane[0].XAxis.MajorTic.IsAllTics = false;

foreach (double val in x_values)
{
    TextObj text = new TextObj(val.ToString(), pane1.MasterPane[0].YAxis.Scale.Min, val);
    text.Location.AlignH = AlignH.Right;
    text.FontSpec.Border.IsVisible = false;
    text.FontSpec.Fill.IsVisible = false;
    pane1.MasterPane[0].GraphObjList.Add("Mar10"); 

    LineObj line = new LineObj(pane1.MasterPane[0].YAxis.Scale.Min, val, pane1.MasterPane[0].YAxis.Scale.Max, val);
    line.Line.Style = System.Drawing.Drawing2D.DashStyle.Dash;
    line.Line.Width = 1f;
    pane1.MasterPane[0].GraphObjList.Add(line);
}

I modified this from this thread which I think is similar to yours, except it is for the Y-axis. It is a bit of a hack and you'll have to add each one manually. You don't really have to do it in a foreach loop, I just put it in one because that is how I copied the code from the other post. I hope it works!

Ireful answered 5/3, 2013 at 5:57 Comment(7)
Also if you want to say a specific day as a static variable you can use DateTime day = new DateTime( 2012, 1, 1 );Ireful
tmwoods : Thanks for your response. I'm afraid I didn't make myself clear. I've tried to explain myself better in an edit to the question.Flaxen
tmwoods: I meant to say I apologise for slow response, got tied up on something else.Flaxen
Wondering if there is a specific reason why this answer is not accepted as the right oneFukien
I'm also curious how this turned out. I edited this to match his question edit. I wonder if maybe he missed that or if still didn't work out for him.Ireful
@Ireful Using your example I could show the dates along X axis but am facing syncing problems as described here.[#15997103Fukien
Aah, I'll take a look. You should either mark this question as closed or else post your own answer since these seem to be two unique problems (with a bit of overlap). I will try to screw around with your other problem in the next couple days tho :)Ireful

© 2022 - 2024 — McMap. All rights reserved.