Solved

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

Posted on 2011-02-21
6
4,498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…

734 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