Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zedgraph - how to customize date based X-Axis tics

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.

like image 913
glaucon Avatar asked Mar 05 '13 01:03

glaucon


1 Answers

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!

like image 52
tmwoods Avatar answered Nov 01 '22 07:11

tmwoods