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

Creating custom Label Text for points on X axis

I am working with a chart control(ChartSeriesType.Column) and have 3 series of data that has multiple weeks worth of points. I would like to have a label appear on the X axis every monday with the date. I can figure out how to determine if a given date is a monday, but cant figure out how to actually get the text to appear on just those days. Currently my grid is using Me.LaborChart.PrimaryXAxis.DateTimeFormat = "M/d" which gives dates of 12/29, 12/30... to the end date While I like the format, I dont need every point to have a date, and the start date is off. Any suggestions?

8 Replies

DJ Davis Jebaraj Syncfusion Team March 17, 2005 05:30 PM UTC

Hi Bob, You can handle the ChartFormatAxisLabelEvent of the ChartControl and check if the ValueAsDate is a Monday: private void chartControl1_ChartFormatAxisLabel(object sender, Syncfusion.Windows.Forms.Chart.ChartFormatAxisLabelEventArgs e) { if(e.AxisOrientation == Orientation.Horizontal)//X Axis { if(e.ValueAsDate.DayOfWeek != DayOfWeek.Monday) e.Label = String.Empty; e.Handled = true; } } To customize the start date of the axis, you can set a manual range for the axis by specifying: this.chartControl1.PrimaryXAxis.DateTimeRange = new ChartDateTimeRange(rangeMin,rangeMax,60,ChartDateTimeIntervalType.Minutes); rangeMin and rangeMax are computed start and end dates for your data. Thanks, Davis

AD Administrator Syncfusion Team April 22, 2005 09:22 PM UTC

For some reason, your code was working fine, and then something must have changed. Can you look through this and see if something strikes you? The two main cases are as follows: The code as is will show all the data, but there is only like 6 intervals on the X axis. And the only label printed on the X axis is the first date, none of the others show anything. The other time is when the following line is not commented out e.LaborChart.PrimaryXAxis.DateTimeRange = New ChartDateTimeRange(startdate, enddate, 21, ChartDateTimeIntervalType.Days) then the chart will show many intervals, it looks like it might be the correct number, ~ 52/3 since the interval is 21 days, but now no data is showing up nor do any of the labels. Thanks! Private Sub LaborChart_ChartFormatAxisLabel1(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Chart.ChartFormatAxisLabelEventArgs) Handles LaborChart.ChartFormatAxisLabel ''This code is handeling up to 1 year''s worth of daily points. ''The ''Interval'' refers to how often we want to print ''a date for a monday. If (e.AxisOrientation = Orientation.Horizontal) Then Dim Range, Interval As Integer If LaborChart.Series.Count > 0 Then Range = Me.LaborChart.Series(0).Points.Count If Range < 61 Then Interval = 7 '' 1 week ElseIf Range < 190 Then '' 6 months Interval = 7 '' 2 weeks Else Interval = 21 '' 3 weeks End If End If Dim startdate, enddate As Date startdate = "4/18/2005" enddate = startdate.AddDays(Range) ''Me.LaborChart.PrimaryXAxis.DateTimeRange = New ChartDateTimeRange(startdate, enddate, 21, ChartDateTimeIntervalType.Days) If (e.Value Mod Interval <> 0) Then e.Label = String.Empty e.Handled = True Else Dim lbl As Date = Schedule.TodayDate.AddDays(e.Value) e.Label = lbl.Month & "/" & lbl.Day e.Handled = True End If End If End Sub

DJ Davis Jebaraj Syncfusion Team April 25, 2005 09:38 PM UTC

Hi Bob, It appears from your code that you are changing the axis range everytime the ChartFormatAxisLabel event is raised. This is not needed and the range only needs to be changed when data in the series is changed. Please refer to the sample linked to below: F26219ChartDateInterval_3779.zip Thanks, Davis

AD Administrator Syncfusion Team April 27, 2005 03:18 PM UTC

I tried compiling the example, but there was a control or two that were not present that I couldn’t get to work. I then tried pull over some of the code from your example to see if I could get the dates to show up but it still wouldn’t work. Ive modified my code to generate some random numbers and pulled out just the chart form into a project. I renamed the chart simply ChartControl1 which will need to be added back in. Additionally I added some debug statements, and all the values were 0,50,100,150,200,250 which looks like this event is only considering the main chart lines, and not the actual data points. While it wouldn’t look as clean, I suppose it would be ok to have more vertical lines appearing at every interval. ChartExample_2875.zip

DJ Davis Jebaraj Syncfusion Team April 28, 2005 01:50 PM UTC

Hi Bob, Please change the last few lines of the Fill_Chart subroutine with the code below: ''Set the Global Var Interval once per chart load If ChartControl1.Series.Count > 0 Then If Me.ChartControl1.Series(0).Points.Count < 61 Then '' Under 2 months Interval = 7 '' 1 week ElseIf Me.ChartControl1.Series(0).Points.Count < 190 Then '' Under 6 months Interval = 14 '' 2 weeks Else Interval = 21 '' 3 weeks End If End If Dim PointsCount As Integer PointsCount = Me.ChartControl1.Series(0).Points.Count Me.ChartControl1.PrimaryXAxis.RangeType = ChartAxisRangeType.Set Me.ChartControl1.PrimaryXAxis.Range = New MinMaxInfo(0, PointsCount, Interval) This should display the labels as desired. Thanks, Davis

AD Administrator Syncfusion Team April 28, 2005 03:39 PM UTC

Perfect! Thanks!

AD Administrator Syncfusion Team April 28, 2005 08:51 PM UTC

One slight problem that Ive been able to find. If you zoom in on the chart then labels have a tendancy to disappear. If the chart is at the very begining then the labels will show up for whatever is present, but as soon as you scroll the the right by any amount they all dissappear.

DJ Davis Jebaraj Syncfusion Team April 29, 2005 02:08 PM UTC

Hi Bob, I can see the problem with the labels not being displayed when zoomed in. The reason the labels not appearing is that the values on the axis are not round values as in the non zoomed in range. Non Zoomed Values: 0,14,28,42,56... Zoomed in Values: 0, 1.27,2.54,... With these fractional values along the axis, the labels will not get through the Mod check with the interval of 7 or 14 or 21 One workaround is to round the label value before checking for the Mod: Dim modval As Double Dim roundedValue As Double roundedValue = System.Math.Round(e.Value) modval = roundedValue Mod Interval Even in this case, a label will be shown only if the fractional value falls within the rounding range. (For example, if 41.2 is a label vale, that will round to 42 and be displayed. However, if during another zoom, the label is at 40.8, that would round to 41 and no label would be displayed). Thanks, Davis

Live Chat Icon For mobile
Up arrow icon