vb.net 柱形图 - 为 X 轴设置字符串标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39418382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:03:42  来源:igfitidea点击:

Column Chart - Set string labels for X axis

vb.netwinformschartsmschart

提问by Oak_3260548

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.

我有一个简单的数据表 dtChart,其中 3 列(字符串、int32、int32)作为数据源附加到 Chart1(两个系列)并为 YValueMembers 设置整数。图表显示良好,到目前为止还不错,但有些比例数字低于列。

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):

在所有列上显示标签,但带有零。当我尝试将 XValueMember 设置为 dtCharts 的第一个字符串列(1 个或两个系列)时:

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

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

...然后图表faisl的绘画(出现带十字的红色矩形)我也试过这个:

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

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

...with no progress.

……毫无进展。

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

如何在数据绑定图表中为 X 轴设置标签?

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".

编辑:顺便说一句,我知道我可以遍历积分集合并为每个点分别设置标签,但我认为这是一种解决方法,而不是解决方案。必须有一种直接使用绑定列的方法,一种类似于“DisplayMember”的方法。

回答by Reza Aghaei

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

在此处输入图片说明