Setting X-Axis labels with a ChartSpace

GroganJ
GroganJ used Ask the Experts™
on
I'm using an OWC11 ChartSpace object on a userform. It contains 1 chart and the x-axis is a set of dates to correspond to the y-values. The ChartType is a LineType.

How do I set the X-axis labels to my dates using VBA? I'm using Office 2003.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
start recording a macro and do it manually.
then look at the code and modify it to suit

there is no vba code to set the axis and cart labels dynamically
Most Valuable Expert 2011
Top Expert 2011

Commented:
Do you have a spreadsheet control that you are using for the data or is the whole thing populated in code?

Author

Commented:
rorya - the entire thing is programmed from VBA. I'm not using Excel at all.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Werafa - I had tried that, but the methods that a recorded macro picks up do not all exist in the OWC11 object. And specifically, the methods around setting the x-axis.

If vba doesn't let you change the axes, can anyone recommend a third party object that does allow me more control over graphing? Cheap is good, free is better :-)
Most Valuable Expert 2011
Top Expert 2011

Commented:
You don't actually need to be in Excel to use a spreadsheet control! :)
I should be back in the office shortly - I'll check my OWC code samples - I'm pretty sure it is possible to do what you want. Do you have the dates in an array already?

Author

Commented:
Rorya - thanks for that, I appreciate the help. I have 2 arrays that I read from a database. One contains the dates for the x-axis and the other contains the y-values.
Most Valuable Expert 2011
Top Expert 2011

Commented:
Assuming your chart variable is called cht and your date array is adteCategories, the code would be:

cht.SetData chDimCategories, chDataLiteral, adteCategories


I assume you know you could bind the chart to the database?

Author

Commented:
Rorya - I can't get this to work. Do you have a code sample that you could post?

I don't want to bind to a database as there is some manipulation of the data before displaying.
Most Valuable Expert 2011
Top Expert 2011
Commented:
Sure - here's a rough sample:

Dim ser As ChSeries
    Dim cht As ChChart
    Dim objAxis As ChAxis
    ' note the data and category arrays must be of Variant type
    Dim varDataCats(1 To 50), varDataVals(1 To 50)
    Dim n As Long
    
    For n = 1 To 50
        varDataCats(n) = DateSerial(2010, 1, n)
        varDataVals(n) = n
    Next n
    
    With Me.ChartSpace1
        Debug.Print .Charts.Count
        .Charts.Add
        Set cht = .Charts(0)
    End With
    
    With cht
        .Type = chChartTypeLineMarkers
        .SeriesCollection.Add
        .SetData chDimCategories, chDataLiteral, varDataCats
        .SeriesCollection(0).SetData chDimValues, chDataLiteral, varDataVals
        .HasLegend = False
        .HasTitle = True
        Set objAxis = .Axes(0)
        With objAxis
            .Font.Size = 6
            .NumberFormat = "MM/DD/YYYY"
            ' don't want the dates grouped
            .GroupingType = chAxisGroupingNone
        End With

    End With

Open in new window

Author

Commented:
Thanks Rorya - my mistake was in not making the date array a variant array.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial