Solved

Microsoft Chart vb.net - How to add labels to the x-axis

Posted on 2011-02-21
6
3,983 Views
Last Modified: 2012-05-11
I'm using the code below to create a chart. The chart is created fine and it gives me 31 tickmarks on the x-axis, but there are no labels. I'd like the labels to be on the tickmarks on the x-axis. The labels will be numbered according to the number of tickmarks, in this case 1 through 31. I've tried to find examples for custom labels but can't get anything to work. How can I generate these labels? Thanks!
dHRInterval = xValues.Count / RideDetail.RideDetailDistance.Text
            Chart1.Series.Add("HR")
            Chart1.Series("HR").ChartType = SeriesChartType.Line

            'Bind chart to ArrayList
            Chart1.Series("HR").Points.DataBindXY(xValues, yValues)
            Chart1.ChartAreas("ChartArea1").AxisX.Interval = dHRInterval
            Chart1.ChartAreas("ChartArea1").AxisX.IntervalOffset = 0

Open in new window

0
Comment
Question by:myester
  • 4
  • 2
6 Comments
 
LVL 10

Expert Comment

by:kdwood
ID: 34945778

myester,

I have a couple of chart projects I can take a look for you.   Off the top of my head, did you try setting the Axis label styel format as follows:

Chart1.ChartAreas("ChartArea1").AxisX.LabelStyle.Format = "####.####"  (or whatever number format you need)

Regards,

Keith


0
 

Author Comment

by:myester
ID: 34946199
Keith thanks for the reply. I tried that and I still don't have any labels on the x-axis. I'm trying some different things with custom labels but can't get anything to work yet.
0
 
LVL 10

Expert Comment

by:kdwood
ID: 34946580
myester,

I have a feeling it has to do with your dHRInterval.   I created a simple chart project, set the AxisX interval to 1 and the tick labels show up fine.  However, as I played with the interval and set it to values that exceeded my datapoints, I was able to get the label to disappear.

Here is my sample code:


Dim xValues As String() = {"Red", "Blue", "Green"}
        Dim yValues As Double() = {14, 8, 11}
        
        Chart1.Series.Add("HR")
        Chart1.Series("HR").ChartType = SeriesChartType.Line

        'Bind chart to ArrayList
        Chart1.Series("HR").Points.DataBindXY(xValues, yValues)
        Chart1.ChartAreas("ChartArea1").AxisX.Interval = 1
        Chart1.ChartAreas("ChartArea1").AxisX.IntervalOffset = 0

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:myester
ID: 34947532
Keith, in this case the dHRInterval is a little over 42 and there are 1318 datapoints. The dHRInterval is going to vary from say 25 to 75 or a 100. The datapoints will vary too and will always exceed the values generated by the interval. In this chart the datapoints represent a heart rate value and the x-axis will be the mileage ridden on a bike ride. The calculation that generates dHRInterval gives me one tickmark for each mile so I need to keep that. I think I may be on to something with custom labels. If you can think of anything else please let me know. In the meantime when I get some more time to work on this I'll see if I can get the custom labels working and let you know how it goes. Thanks!
0
 

Accepted Solution

by:
myester earned 0 total points
ID: 34948000
Keith,

I have the custom labels working with the code below. I appreciate your response.

Dim dHRIntervalConstant As Double = dHRInterval
            Dim i As Integer = 1
            For index As Double = 0 To RideDetail.RideDetailDistance.Text
                Chart1.ChartAreas(0).AxisX.CustomLabels.Add(New CustomLabel(dHRInterval - 10, dHRInterval + 10, i.ToString, 1, LabelMarkStyle.None))
                dHRInterval = dHRInterval + dHRIntervalConstant
                i = i + 1
            Next
0
 

Author Closing Comment

by:myester
ID: 34986603
I found the solution on my own.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now