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
close icon

MinorGrid Labels for DateTimeCategoryAxis

Hello, I have a chart with chart axis labels for each day with corresponding gridlines which are added manually to a DateTimeCategoryAxis via the GenerateVisibleLabels sub. Now, I would like to add additional labels for each hour from 9 am to 4 pm, but without gridlines. However, I cannot find a way to prevent gridlines being added to these hour labels.

Attached is an image showing what I mean, and a sample application. Run the sample and press the "Zoom 3 days" button. The sample works fine, expect I want to remove the vertical gridlines for the hour labels. How can this be achieved?

Thank you.

Attachment: sfChartNoGridlineLabels_f2cbed3.rar

7 Replies

MK Muneesh Kumar G Syncfusion Team December 17, 2018 06:55 AM UTC

Hi Tom, 
 
Greetings, we have analyzed your requirement and we have achieved this by adding TextAnnotations for hour labels instead of adding VisibleLabels. Because we have drawn gridlines based on the VisibleLabels count. Please find the modified code snippet below.  
 
Code snippet 
Protected Overrides Sub GenerateVisibleLabels() 
 
        If IntervalType = DateTimeIntervalType.Days Then 
 
            Dim area = CType(Me.GetType().GetProperty("Area", BindingFlags.GetProperty Or BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(Me), SfChart) 
            area.Annotations.Clear() 
.. 
 
                    Else   'ADD NEW HOUR LABEL 
                        If xDateTime >= nextHour Then 
 
                            If Not xDateTime.Hour = 16 Then 'do not add last hour of the day label to prevent intersection with the next date label 
                                Dim labelString As String = xDateTime.ToString("HH", Globalization.CultureInfo.CurrentCulture) 
                                Dim annotation As TextAnnotation = New TextAnnotation 
                                annotation.X1 = position 
                                annotation.Y1 = 100 
                                annotation.HorizontalAlignment = HorizontalAlignment.Center 
                                annotation.VerticalAlignment = VerticalAlignment.Center 
                                annotation.Foreground = New SolidColorBrush(Colors.White) 
                                annotation.Text = labelString 
                                area.Annotations.Add(annotation) 
 
                            End If 
 
                            nextHour = nextHour.AddHours(1) 
 
                        End If 
 
                    End If 
 
                    position += 1 
                Loop 
 
            End If 
        Else 
            MyBase.GenerateVisibleLabels() 
        End If 
 
 
    End Sub 
 
Here we have added the annotation at Y-Axis position of 100. You can modify this position based on you need.  
 
Please find the sample from the following location.  
 
 
Screenshot: 
 
 
Thanks, 
Muneesh Kumar G. 



TO Tom December 17, 2018 02:58 PM UTC

Thank you Muneesh, I've set the hour labels to the correct Y position with this code:

  Else   'ADD NEW HOUR LABEL
                        If xDateTime >= nextHour Then

                            If Not xDateTime.Hour = 16 Then 'do not add last hour of the day label to prevent intersection with the next date label
                                Dim labelString As String = xDateTime.ToString("HH", Globalization.CultureInfo.CurrentCulture)
                                Dim annotation As TextAnnotation = New TextAnnotation
                                annotation.CoordinateUnit = CoordinateUnit.Axis
                                annotation.XAxisName = MainWindow.XAxis.Name
                                annotation.YAxisName = MainWindow.YAxisPrice.Name
                                annotation.X1 = position
                                annotation.Y1 = MainWindow.chart.PointToValue(MainWindow.YAxisPrice, New Point(0, 14))
                                annotation.HorizontalAlignment = HorizontalAlignment.Center
                                annotation.VerticalAlignment = VerticalAlignment.Center
                                annotation.Foreground = New SolidColorBrush(Colors.White)
                                annotation.FontSize = 10
                                annotation.Text = labelString
                                area.Annotations.Add(annotation)
                            End If

                            nextHour = nextHour.AddHours(1)

                        End If

                    End If

Initially, it works fine. However, now a new issue has cropped up. To reproduce the issue, change to the above code, run the sample, press the 3 day zoom button. The hour labels are correctly aligned with the date labels. Now, click the maximize button to maximize the main window (assuming it started windowed). Now the hour labels appear below the date labels. After scrolling the chart, the hour labels are again correctly aligned.

I guess the PointToValue method doesn't calculate the updated YAxisPrice axis size, or the YAxisPrice size has not yet been updated with the new window size by the time the GenerateVisibleLabels sub is called. How can this issue be resolved? 

Thank you.


MK Muneesh Kumar G Syncfusion Team December 18, 2018 12:05 PM UTC

Hi Tom, 
 
Thanks for your update, we have analyzed your query and we have resolved that by updating annotation’s Y1 position in SfChart’s LayoutUpdated event as per the below code snippet.  
 
Code Snippet 
 
        <chart:SfChart LayoutUpdated="Chart_LayoutUpdated" > 
        … 
        </chart:SfChart> 
 
 
 
    Private Sub Chart_LayoutUpdated(sender As Object, e As EventArgs) 
        Dim chart = CType(sender, SfChart) 
        Dispatcher.BeginInvoke(DispatcherPriority.Normal, New Action(AddressOf UpdateAnnotation)) 
    End Sub 
 
    Private Sub UpdateAnnotation() 
        If chart.Annotations IsNot Nothing Then 
            For Each item As Annotation In chart.Annotations 
                item.Y1 = chart.PointToValue(YAxisPrice, New Point(0, 14)) 
            Next 
        End If 
    End Sub 
 
 
We have modified our sample based on this, please find the sample from the following location.  
 
 
Hope it helps you.  
 
Regards, 
Muneesh Kumar G.  
 



TO Tom December 18, 2018 03:37 PM UTC

Hi Muneesh, your sample freezes after clicking the 3 day zoom button. The Chart_LayoutUpdated sub gets called repeatedly.


MK Muneesh Kumar G Syncfusion Team December 19, 2018 04:59 AM UTC

Hi Tom, 
 
Thanks for your update, we have already the reported scenario with our sample. It works fine with all zoom button. I have attached the reference video for that scenario in below location, please find it.  
 
 
If you still face the problem, please share your machine details, Visual Studio version and used Syncfusion version in your application with us. This would be helpful for us to give better solution in this.  
 
Thanks, 
Muneesh Kumar G. 
 



TO Tom December 19, 2018 05:48 AM UTC

Hi Muneesh, thanks for checking this out. It is strange that it works on your machine but not mine. I'm using VS17 Community 15.9.4 (latest), with the latest 16.4460.0.42 sfchart version. 

However, if I cange 

Dispatcher.BeginInvoke(DispatcherPriority.Normal, New Action(AddressOf UpdateAnnotation))

to

Dispatcher.Invoke(DispatcherPriority.Normal, New Action(AddressOf UpdateAnnotation))

it works fine.

Also, if I change

Dispatcher.BeginInvoke(DispatcherPriority.Normal, New Action(AddressOf UpdateAnnotation))

to 

UpdateAnnotation()

it works fine.

I guess because BeginInvoke is asynchronous, there is some problem.

I found the best solution is to replace the Chart_LayoutUpdated sub with this:

Private Sub chart_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles chart.SizeChanged
        UpdateAnnotation()
End Sub

which seems to perform well, since it's only called once when the window size changes. So, for me the issue is solved, but it's unfortunate the BeginInvoke method doesn't work, because I guess it's potentially the best performing method. Anyway, thanks again.



MK Muneesh Kumar G Syncfusion Team December 19, 2018 06:01 AM UTC

Hi Tom, 
 
Glad that the issue has been resolved and please get back to us if you need any other assistance.  
 
Regards, 
Muneesh Kumar G. 
 


Loader.
Live Chat Icon For mobile
Up arrow icon