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.

glaucon
  • 8,112
  • 9
  • 41
  • 63
  • Also if you want to say a specific day as a static variable you can use `DateTime day = new DateTime( 2012, 1, 1 );` – tmwoods Mar 05 '13 at 13:35
  • Have you tried setting `myPane.XAxis.Type = AxisType.DateAsOrdinal` and used a custom label? Use [this](http://stackoverflow.com/questions/9951465/changing-axis-type-in-zedgraph) as a reference. Also you may need to set `Scale.Format = "MM-yy"` and see if that works. I've never tried it myself. – tmwoods Mar 08 '13 at 14:18
  • 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! – tmwoods Mar 19 '13 at 23:13

1 Answers1

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!

Community
  • 1
  • 1
tmwoods
  • 2,353
  • 7
  • 28
  • 55
  • Also if you want to say a specific day as a static variable you can use `DateTime day = new DateTime( 2012, 1, 1 );` – tmwoods Mar 05 '13 at 13:35
  • 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. – glaucon Mar 08 '13 at 04:53
  • tmwoods: I meant to say I apologise for slow response, got tied up on something else. – glaucon Mar 08 '13 at 05:02
  • Wondering if there is a specific reason why this answer is not accepted as the right one – Martin Apr 10 '13 at 05:31
  • 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. – tmwoods Apr 10 '13 at 17:12
  • @tmwoods Using your example I could show the dates along X axis but am facing syncing problems as described here.[http://stackoverflow.com/questions/15996603/how-to-sync-three-graphpanes-in-zedgraph] – Martin Apr 14 '13 at 07:05
  • 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 :) – tmwoods Apr 14 '13 at 20:20