2

This must be trivial, but I cannot find anything on the net.

I have a simple datatable dtChart with 3 columns (string, int32, int32) attached as datasource to Chart1 (to a two series) and set the ints for YValueMembers. The chart displays well, so far so good, but some scale numbers bellow columns.

Chart1.ChartAreas(0).AxisX.LabelStyle.Interval = 1

Displays labels on all columns, but with zeros. When I try to set XValueMember to the first string column from dtCharts (either 1 or both series):

Chart1.Series(0).XValueMember = "ProcesName"

... then the painting of chart faisl (red rectangle with cross appears) I tried this too:

Chart1.Series(0).AxisLabel = "#VALX"

...with no progress.

How do I set labels for the X axis in a data-bound chart?

EDIT: By the way, I know I can go throu the points collection and set the labels separately for each of them, but I would consider that a workaround, not a solution. There must be a direct way to use bound column, a sort-of "DisplayMember".

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • There's an entire microsoft help page on chart labels - https://msdn.microsoft.com/en-us/library/dd456628.aspx – soohoonigan Sep 09 '16 at 19:32
  • ...and the entire page is missing keywords like "bound", "valuemember", etc. So this will not help me, because the problem will be in the concept of the setting the labels, not in format and such. It can be achieved, I can see the results on the net, but it is not typical that the X values has strings as value (usually there are int32, data, months, etc.) – Oak_3260548 Sep 09 '16 at 21:35

1 Answers1

5

Just drop a new chart control on the form and use such code to show data in the chart:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Me.Chart1.DataSource = GetData()
    Me.Chart1.Series.Clear()
    Chart1.ChartAreas.Clear()
    Chart1.ChartAreas.Add("Area0")
    Me.Chart1.Series.Add("Math")
    Me.Chart1.Series.Add("Physics")

    Chart1.Series(0).XValueMember = "Name"
    Chart1.Series(0).YValueMembers = "Math"
    Chart1.Series(0).IsValueShownAsLabel = True
    Chart1.ChartAreas(0).AxisX.LabelStyle.Angle = -90

    Chart1.Series(1).XValueMember = "Name"
    Chart1.Series(1).YValueMembers = "Physics"
    Chart1.Series(1).IsValueShownAsLabel = True
End Sub

Public Function GetData() As DataTable
    Dim dt = New DataTable()
    dt.Columns.Add("Name", GetType(String))
    dt.Columns.Add("Math", GetType(Integer))
    dt.Columns.Add("Physics", GetType(Integer))
    dt.Rows.Add("Alex", 12, 17)
    dt.Rows.Add("Richard", 19, 20)
    dt.Rows.Add("Alice", 14, 16)
    Return dt
End Function

And the result would be this chart:

enter image description here

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This seems to be the same procedure I tried already (first). So, do I understand it correctly, that it works with XValueMember, but not for more than one series in the chart? I still think there must be a solution, I found meanwhile i.e. this example http://stackoverflow.com/questions/2684857/asp-net-chart-controls-how-do-i-create-this-bar-chart, I'll see if I can use it. – Oak_3260548 Sep 11 '16 at 04:51
  • Check the edit. I believe this is what you need. I made some changes to show grades of 2 course (Math and Physics) for 3 Students (Alex, Richard and Alice). I don't know if this is what you have tried, but I'm sure it works simply using the code which I shared and the result is what you see in screenshot. Hope you find the answer useful :) – Reza Aghaei Sep 11 '16 at 11:47
  • Yes, this is helpful and I will mark it as answer. This is what I tried, but knowing it should work like that helps a lot. Just to clarify: The 2nd column in your dt sample should be an Iteger too, right? – Oak_3260548 Sep 12 '16 at 08:12
  • Yes it should be integer like `Physics` column . String is a typo (but it also works with string) :) – Reza Aghaei Sep 12 '16 at 08:56
  • All credits to you. I haevn't found any problem with my code even after 2 hours of trying and checking. When I copied your code in and replaced dt data for mine, it works... I have no idea why, but something was breaking the chart paint event in the original code. – Oak_3260548 Sep 12 '16 at 10:10
  • You're welcome :) Maybe you have set some properties (which I can't guess using designer). – Reza Aghaei Sep 12 '16 at 10:15
  • Btw, When you accept an answer, it would be great if you also vote for the answer by click on up arrow near the post. It's not compulsory at all, but it's common, reasonable and recommended. For more information about how does accepting answers work see this [post](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow). – Reza Aghaei Sep 12 '16 at 10:15