vb.net – 图表:在X轴上显示更多的值描述

前端之家收集整理的这篇文章主要介绍了vb.net – 图表:在X轴上显示更多的值描述前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我向用户显示一个图表,该图表有一个图表区域和折线图.在这方面,我得到了一个例子.这一行有大约200个值.这些值都有描述(例如“01.01.2013”​​,“05.02.2013”​​等等).

当图表显示时,我只能看到两个描述,即使有更多的描述空间.线路正确显示,但只描述了两点.

我垂直旋转文本,所以有更多的空间,但这没有帮助.如果显示的值较小(5或10),则说明显示正确.

这是它实际上是如何(描述实际上是字符串,而不是日期).

感谢您的帮助!

编辑:我的代码

chart.ChartAreas(0).AxisY.Maximum = 6
chart.ChartAreas(0).AxisY.Minimum = 1
chart.ChartAreas(0).AxisX.LabelStyle.Angle = -90
chart.Series.Clear()
chart.ChartAreas(0).AxisY.StripLines.Clear()
Dim myStripLine1 as new StripLine()
myStripLine1.IntervalOffset = 4
chart.ChartAreas(0).AxisY.StripLines.add(myStripLine1)

'now adding all series
chart.Series.Add("Chemie") 'just to take the example in the image above
chart.Series(chart.Series.Count - 1).ChartType = DataVisualization.Charting.SeriesChartType.Line
chart.Series(chart.Series.Count - 1).BorderWidth = 4

'now adding quite much values (on every date,every Serie has a value)
 chart.Series(chart.Series.Count - 1).Points.AddXY("01.03.2011",4.9)

在每个日期,为所有系列输入一个新的点,但只有那些具有重要值的点被突出显示.这些数值之间的数学计算.

一个例子来解释一下:我有两个系列,一个在“01.01.2013”​​和“03.01.2013”​​上有两个值(6和4).其他系列在“01.01.2013”​​,“02.01.2013”​​和“03.01.2013”​​上有3个值(4,6,5.5).当我只显示它们时,第一个系列将在第二个日期结束,即使有第三个日期的值.我通过在第一个系列中填入一个虚拟值来解决这个问题,日期为“02.01.2013”​​,这只是这个时候的平均值(= 5).这一点根本没有用标记项目符号突出显示.这是我绘制我的图形.

EDIT2:

Skippy’s 后回答和评论,我的新试用.变量MainForm.grades是一个Dictionary(Of Integer,Dictionary(Of String,String)),包含大约150个等级

Dim subjects As New Dictionary(Of Integer,ArrayList)
    Dim allgrades As New ArrayList
    For Each grade In MainForm.grades
        Dim cD As New Dictionary(Of String,String)
        cD.Add("SUBJECTID",grade.Value("SUBJECTID"))
        cD.Add("GRADE",grade.Value("GRADE"))
        cD.Add("DATE",grade.Value("DATE"))
        allgrades.Add(cD)
    Next

    cht_main.ChartAreas(0).AxisX.IntervalType = DateTimeIntervalType.Days
    cht_main.ChartAreas(0).AxisX.LabelStyle.Angle = -90
    Dim gradesDateSorter = New gradesDateSorter()
    allgrades.Sort(gradesDateSorter)
    For Each grade In allgrades
        If Not subjects.ContainsKey(Integer.Parse(grade("SUBJECTID"))) Then
            subjects.Add(Integer.Parse(grade("SUBJECTID")),New ArrayList)
        End If
        Dim gradeDict As New Dictionary(Of String,String)
        gradeDict.Add("DATE",grade("DATE"))
        gradeDict.Add("GRADE",grade("GRADE"))
        subjects(Integer.Parse(grade("SUBJECTID"))).Add(gradeDict)
    Next
    For Each subject In subjects
        'adding serie
        cht_main.Series.Add(MainForm.subjects(subject.Key)("NAME"))
        cht_main.Series(cht_main.Series.Count - 1).ChartType = DataVisualization.Charting.SeriesChartType.Line
        cht_main.Series(cht_main.Series.Count - 1).BorderWidth = 4
        'cht_main.Series(cht_main.Series.Count - 1).IsXValueIndexed = True
        For Each grade In subject.Value
            cht_main.Series(cht_main.Series.Count - 1).Points.AddXY(Date.Parse(grade("DATE")),Double.Parse(grade("GRADE")))
        Next
    Next

在第五行,我评论了IsXValueIndexed = True,因为当我激活它,图表生成与一个大的红色错误交叉.

设置X轴上的间隔是诀窍!

chart.ChartAreas(0).AxisX.Interval = 1

解决方Skippy

是的,我同意迈克尔.在这一点我只能补充说明.

通过设置间隔时间:

myStripLine1.IntervalOffset = 4

您保证您的X轴值只能在4“通用x轴”值的频率处绘制:

将其设置为1将为每个x轴值提供一个值,该值将作为整数增加(在这种情况下为天数)

chart.ChartAreas(0).AxisX.Interval = 1

并声明x轴值以键入:

DateTimeIntervalType.Days

'Declaration
    Public Sub Add( _
    labelsStep As Double,_
    intervalType As DateTimeIntervalType,_
    format As String _
)
End Sub

chart.ChartAreas(0).AxisX.IntervalType = DateTimeIntervalType.Days

'which as shown in Michael's answer is parsed to string.

Dim format as String = "MM.dd.yyyy"
Dim actualDate as Date = Date.ParseExact(yourDate,format)

正如迈克尔在评论中所提到的.
通过设置

mySeries.XValueIndexed = True

将绘制每个索引的X轴值.

如下面的引用所述,提供链接.

Each data point in a series has X and Y values that determine its position in the plotting area. With some charts,the X value of points is not important,and does not have to be provided. In this case,the point positions in the plotting area are determined only by their point index (i.e. their location in the Points collection) and their Y values.

When X values are “indexed”,the data point index,not a point’s X value,is used to determine the position of points along the categorical (X) axis. For example in Figure 1 below,two charts are shown displaying the same data. However,the first chart uses non-indexed X values,therefore the X values of those points determine their location along the x-axis. The second chart is indexed,therefore its point indices are used to determine their position along the x-axis. In this case the X values are only used for the axis labels,and nothing more.

http://support2.dundas.com/onlinedocumentation/winchart2005/Data_IndexedXValues.html

我在以下网站提供了有关间隔和间隔偏移的原始信息:

http://support2.dundas.com/Default.aspx?article=705

这里讨论数据类型并解决您突出显示的值的问题.

On every date,a new point gets entered for all series,but only those points where they have important values get highlighted

For example,assume you wish to create a re-occurring StripLine to highlight weekends. You set the interval to 7 and its type to Days. Since the first point is Sunday you set the IntervalOffset to 6 (to mean the 6th day of the week) and its type to Days. The resulting chart does not show the first StripLine.

这是设置间隔的一个解释.

A good rule of thumb to follow when using the Interval and IntervalOffset properties of the Chart is that the IntervalOffset should be a lower interval magnitude than the Interval (ie. Interval Days / IntervalOffset Hours,Interval Years / IntervalOffset Months,etc.).

我已经添加了这些来源:

>供您参考
>为了表明我在确定问题之后也做了我的研究,正如我上面的评论所述.

Florian,can you pls show the code for the labels,properties etc of the x-axis? – yvytty yesterday

Did you ever consider 3rd party plotting components,such as ZedGraph ? Most likely such little caveats are already covered there. Give it a shot! – Neolisk yesterday

为了回应ZedGraph,我建议:

并且:查看您的代码

Hi can I clarify,you WANT to plot values daily? I think I have your solution,just need clarification, you have all the tools within vb.net

@yvytty,nope,the dates do not have to be daily,there can also be no value for a long time and I don’t want a big span in my chart where no data is. Actually,I could also write some sample text at the X axis values,the dates are only confusing. The main problem is that the VB chart somehow calculates a very big margin on those descriptions at the X axis

它没有显示您格式化了日期和日期字符串.还需要考虑到,您没有使用en-US日期格式(我在澳大利亚,所以我们有与您相同的格式).默认日期类型是en-US.

请参考DateTime.ParseExact方法

http://msdn.microsoft.com/en-us/library/system.datetime.parseexact.aspx

我已经从MSDN获取了片段.

Dim dateString,format As String   
 Dim result As Date 
 Dim provider As CultureInfo = CultureInfo.InvariantCulture

 Parse date and time with custom specifier.
 dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
 format = "ddd dd MMM yyyy h:mm tt zzz"         
 result = Date.ParseExact(dateString,format,provider)

链接
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

The DateTime.ToString(IFormatProvider) method returns the string representation of a date and time value using the short date and long time pattern of a specific culture. The following example uses the DateTime.ToString(IFormatProvider) method to display the date and time using the short date and long time pattern for the fr-FR culture.

Dim date1 As Date = #3/1/2008 7:00AM#
Console.WriteLine(date1.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays 01/03/2008 07:00:00

请看这个链接
http://msdn.microsoft.com/en-us/library/system.datetime.aspx

所以应该去,这样的东西:

'note
Imports System.Globalization

Dim format as String = "dd.MM.yyyy"
Dim actualDate as Date = Date.ParseExact(yourDate,provider)

chart.ChartAreas(0).AxisX.LabelStyle.Format ="dd.MM.yyyy"

cht_main.ChartAreas(0).AxisX.IntervalType = DateTimeIntervalType.Days

cht_main.ChartAreas(0).AxisX.Interval = 1

也:

Double.Parse(grade("GRADE")
'grade is not of type double

猜你在找的VB相关文章