Format DateAsOrdinal xAxis labels in ZedGraph
Asked Answered
G

2

2

I have now changed my x axis to DateAsOrdinal, but I would like to improve the label format. I currently handle the XAxis.ScaleFormatEvent like this:

Private Function OnXScaleFormatEvent(ByVal pane As GraphPane, ByVal axis As Axis, ByVal val As Double, ByVal index As Integer) As String
    Dim result As String = ""
    If val < priceBars.Count Then
        Dim time As Date = Date.FromOADate(priceBars(val).X)
        result = [String].Format("{0:D2}{1}{2:D2}", time.Hour, ":", time.Minute)
    End If

    Return result
End Function

How can I make the labels only print every whole 30 minutes? Or every 2 hours as in image below? I believe I still need to reference the bar x-values, because I need to plot multiple days continuously, with only some hours from each day, like the image shows; 09:00-23:00.

enter image description here

Genitor answered 10/2, 2011 at 1:1 Comment(0)
G
0

Fixed this by formatting each label individually.

Genitor answered 29/7, 2012 at 8:9 Comment(0)
K
1

I think, this will do:

chart.GraphPane.XAxis.Scale.MajorStepAuto = False
chart.GraphPane.XAxis.Scale.MajorUnit = DateUnit.Minute
chart.GraphPane.XAxis.Scale.MajorStep = 30
chart.GraphPane.XAxis.Scale.BaseTic = 0
Kovach answered 14/2, 2011 at 9:7 Comment(6)
Thanks! Though I can't make this work.. It gives a step of 30, but it will not be at the whole hour/half hour (00 and 30).Genitor
I added the baseTic to the example. So you can define the start point. Setting to zero should be your friend (default is undefined, so baseTic is where your x-values start).Kovach
Ah.. Now at last I think I understand how this works. As I'm using DateAsOrdinal axis, only the MajorStep and BaseTic will have effect, and will be ordinal values. However this falls short if you want a label every whole hour, and the day ends at time xx:30. Then next day will label the half-hours. Other solution may perhaps be to use MajorStep=1, and then handle all labels in ScaleFormat event. This event handler has the label-index, but is it possible to access the list of labels to set those I don't want to show as .IsInvisible? It's not part of GraphObjectList, so idk what list..?Genitor
I don't think there is such a list. A possible solution is to return an empty string in the ScaleFormatEventHandler when you don't want to show the current label.Kovach
Yes, I tried this. However it still seems to generate a label at those points (with empty string). Often these empty labels overlap and hide most of the labels I want to show. This behavior is intermittent based on the exact resize of the chart. But this is probably as far as the features go. So I may need to attempt to alter the ZedGraph library to not draw any label if the label string is empty, or Null. At least for me it seems this will be required.Genitor
Hmm.. No that seems not to be the case as I described. As I tried to modify the library. Not sure why the labels get hidden. But it looks like this: i55.tinypic.com/152hn44.png Sometimes showing, depending on the how much I resize.Genitor
G
0

Fixed this by formatting each label individually.

Genitor answered 29/7, 2012 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.