|
XYGraph custom X-axis labels
Learn how to add custom X-axis labels
to your graph.
The method AddCustomLabelX is used in the
following to add custom labels on the x-axis.
Step 1: Basic Properties
After adding an XYGraph to your form you
can select the graph and press F4 to view and set its
properties.
Properties can also
be set at runtime as shown below.
Step 2: Basic Graph with custom
labels
A graph is created using the method AddGraph. Data is added using AddValue and the graph is
redisplayed with Invalidate.
private void
Form1_Load(object sender, EventArgs e)
{
MyGraph.AddGraph("Stock value", DashStyle.Solid,
Color.BlueViolet, 1, false);
MyGraph.XtraXmax = 365 * 24;
Random r = new Random(1);
for (int i = 0; i < 365*24; i++)
MyGraph.AddValue(0,
(float)i, (float)(300 + 8*i/365 +
(r.NextDouble() - 0.5) * r.Next(10, 150)));
ShowMonths();
MyGraph.Invalidate();
}
private void
ShowMonths()
{
string[] months3letters = { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };
int days = 0;
MyGraph.XtraLabelX = "Months";
for (int m = 0; m < 12; m++)
{
MyGraph.AddCustomLabelX(days
* 24, months3letters[m],
(m == 0));
days += System.DateTime.DaysInMonth(2007, m + 1);
}
}
The function ShowMonths above adds 12 labels to
MyGraph using the method AddCustomLabelX. First parameter is
the value to be replaced, second parameter is the replacement
string and the third is reset that deletes all previously
added pairs of values and strings.
This example show the stock value of a
company called 'Random Ltd' where a random stock value is
added for every hour in year 2007.

The image below shows the
result of zooming using these 12
custom labels.

Step 3:
Change custom labels on zoom (advanced version only)
With the advanced version
of XYGraph is it possible use to the event XtraZoomChanged to change the labels
on zoom:
private
void MyGraph_XtraZoomChanged(object sender,
componentXtra.XYGraph.ZoomChangeEventArgs
e)
{
float hours = e.xTo - e.xFrom;
float days = hours / 24;
float weeks = days / 7;
if (days > 90)
ShowMonths();
else if (days > 7)
{
MyGraph.XtraLabelX = "Week";
for (int w = 0; w <= (int)weeks; w++)
{
DateTime dt = new DateTime(2007, 1, 1);
dt = dt + new TimeSpan((int)(e.xFrom / 24) +
w*7,
0,
0, 0);
MyGraph.AddCustomLabelX(e.xFrom + (float)(w * 24
* 7),
(1+(dt.DayOfYear/7)).ToString(), (w == 0));
}
}
else
{
MyGraph.XtraLabelX = "Date";
for (int d = 0; d <= (int)days; d++)
{
DateTime dt = new DateTime(2007, 1, 1);
dt = dt + new TimeSpan((int)(e.xFrom / 24) + d,
0, 0, 0);
MyGraph.AddCustomLabelX(e.xFrom + (float)(d *
24),
dt.ToShortDateString(),
(d == 0));
}
}
}
In this example are we
showing weeks instead of when less 3 months but more than 7
days in zoom.

When 7 days or less in zoom
is the actual dates shown:

|