We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

x axis custom labels and grid

hi,

is there any way how to customize labels and grids on x axis? i mean when start date of axis is set to 3.1.2008 12.35 then i'd like to keep it as start date, but would like to show user labels and grids based on some sensible values - so it means that the first label and vertical line should be drawn e.g. on 3.1.2008 13:00 and next e.g. 1 hour later.

thanks in advance
Lubos


3 Replies

J. J.Nagarajan Syncfusion Team August 12, 2008 07:43 AM UTC

Hi Lubos,

Thanks for your interest in Syncufsion products.

We can display custom labels at certain dates along the X axis by using the ChartFormatAxisLabel event of the ChartControl. Please refer to the below code snippet.

this.chartControl1.ChartFormatAxisLabel += new ChartFormatAxisLabelEventHandler(chartControl1_ChartFormatAxisLabel);
//To add custom text to labels
void chartControl1_ChartFormatAxisLabel(object sender, ChartFormatAxisLabelEventArgs e)
{
if (e.IsAxisPrimary && e.AxisOrientation == ChartOrientation.Horizontal)
{
switch (e.ValueAsDate.ToString("d"))
{
case "11/8/2003":
e.Label = "11/8/2003/13:00";
break;
case "11/15/2003":
e.Label = "11/8/2003/14:00";
break;
}
}
}

Here is the working sample.

http://websamples.syncfusion.com/samples/Chart.Windows/F75532/main.htm

Please refer to it and let me know if this helps.

Thanks,
Nagaraj



AD Administrator Syncfusion Team September 1, 2008 06:15 AM UTC

hi,

thanks for the answer, but i probably asked wrong way... i needn't modify label but i need to move the label (+ grid) to the sensible position. is it what your code does?

thanks
Lubos



MA Manohari Syncfusion Team September 10, 2008 06:45 AM UTC

Hi Lubos,

We regret very much for the delayed response. Please find our responses to your queries below.

1) It is possible to move the Axis lines and labels to a specified values by specifying the offset value to the axis. ChartAxis.Offset will move the axis according to the value specified.

Sample code:

this.chartControl1.PrimaryXAxis.DateTimeOffset = new TimeSpan(60); // for DateTime values
this.chartControl1.PrimaryXAxis.Offset = 5; // for Double values

2) It is also possible to draw custom Labels and Grid at the specified position by using the ChartAreaPaint event.

Sample code:

this.chartControl1.ChartAreaPaint += new PaintEventHandler(chartControl1_ChartAreaPaint);
}

void chartControl1_ChartAreaPaint(object sender, PaintEventArgs e)
{
if (showCustomGridLines)
{
float minX = this.chartControl1.ChartArea.RenderBounds.Left;
...
...
for (int i = 0; i < intervals; i++)
{ ...
...
ChartPoint cp = new ChartPoint(dt, 0);
float x = (float)this.chartControl1.ChartArea.GetPointByValue(cp).X;
float y = this.chartControl1.ChartArea.RenderBounds.Bottom;
if (x >= minX && x <= maxX)
{

PointF pnt = new PointF(x, y + this.chartControl1.PrimaryXAxis.TickSize.Height);
e.Graphics.DrawString(dateX, this.chartControl1.PrimaryXAxis.Font, new SolidBrush(Color.Blue), new PointF(x - (sz.Width / 2), y + this.chartControl1.PrimaryXAxis.TickSize.Height + 4 + (sz.Height / 2))); // To Draw Labels
Pen p = new Pen(Color.FromArgb(0, 0, 255));
e.Graphics.DrawLine(Pens.Blue, new PointF(x, y), new PointF(x, y + this.chartControl1.PrimaryXAxis.TickSize.Height)); // To Draw Ticks
e.Graphics.DrawLine(p, new PointF(x, this.chartControl1.ChartArea.RenderBounds.Bottom), new PointF(x, this.chartControl1.ChartArea.RenderBounds.Top));
}
....
....

I have attached the sample that illustrates the above two methods in this link below.

http://websamples.syncfusion.com/samples/Chart.Windows/F75532/Sample1.htm

Kindly let us know if this meets your requirements. Thanks for your patience.

Regards,
Manohari.R




Loader.
Live Chat Icon For mobile
Up arrow icon