Solved

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

Posted on 2011-02-21
6
4,325 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…

828 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