Solved

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

Posted on 2011-02-21
6
4,199 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

776 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