.NET 图表类 - 如何在同一图表上显示不同类型的间隔
本文关键字:显示 同类型 NET | 更新日期: 2023-09-27 18:31:31
我目前正在做一个项目,我需要在今年的图表上显示 2011 年和 2012 年数据的合并版本。今年的图表是每月显示的。
这是我的数据。它是从程序中得出的,所以不用担心:
Date Value Coefficient
01/01/2011 15,6 0,1586
01/01/2012 17,88 0,1468
01/01/2013 11,92 0,1872
01/02/2013 1703,85 0,17
01/03/2013 1693,49 0,16
01/04/2013 1716,1 0,17
01/05/2013 1732,31 0,17
01/06/2013 1692,79 0,17
01/07/2013 1691,38 0,17
看到前两行是全年的合并,其余的逐月合并。"值"列应填充附加到主 Y 轴的列系列。系数是连接到辅助 Y 轴的一条线。
我有这段代码,目前正在显示所有混乱的内容,每月间隔:
<asp:Chart ID="Chart4" runat="server" CssClass="Chart" BorderlineDashStyle="DashDotDot"
Palette="Pastel" DataSourceID="ObjectDataSource2" ImageStorageMode="UseImageLocation"
Height="650px">
<Series>
<asp:Series Name="value" XValueMember="date" Legend="Legend1"
YValueMembers="value" YValueType="Double" ChartArea="ChartArea1" Color="CornflowerBlue"
IsValueShownAsLabel="True" LabelFormat="{0:0.##}">
</asp:Series>
<asp:Series Name="coef" XValueMember="date" Legend="Legend1" YValueMembers="coefCost"
YValuesPerPoint="4" XValueType="Date" Color="YellowGreen" ChartType="Line" IsValueShownAsLabel="True"
MarkerColor="Green" MarkerStyle="Diamond" YAxisType="Secondary" YValueType="Double"
LabelFormat="{0:0.##'%}" BorderWidth="4" ChartArea="ChartArea1">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BackColor="Transparent" ShadowOffset="5">
<AxisY Title="US$ / 1000">
<MajorGrid Enabled="False" />
<LabelStyle Format="{0:#,##0}" />
</AxisY>
<AxisX Interval="1" IntervalOffsetType="Months" IntervalType="Months">
<MajorGrid Enabled="False" IntervalOffsetType="Auto" IntervalType="Auto" />
<LabelStyle Interval="Auto" Format="{MMM/yy}" />
<ScaleBreakStyle Spacing="1" />
<ScaleView SizeType="Months" />
</AxisX>
<AxisX2>
<MinorGrid Enabled="True" />
<MajorTickMark Enabled="False" />
</AxisX2>
<AxisY2 Title="(%) Coef">
<MajorGrid Enabled="False" />
</AxisY2>
</asp:ChartArea>
</ChartAreas>
<Legends>
<asp:Legend Name="Legend1" Alignment="Center" Docking="Bottom">
</asp:Legend>
</Legends>
<Titles>
<asp:Title Font="Arial Narrow, 14pt" Name="Title1">
</asp:Title>
</Titles>
</asp:Chart>
如您所见,我已将 ObjectDataSource2 设置为填充系列,从而填充图表。我已经试图搞砸间隔的事情和所有的东西,但我无法实现我想要的。
这是我到目前为止得到的图片:
https://i.stack.imgur.com/3eYkn.png
回顾一下:我需要这张图表只显示我从手术中带回来的东西。我知道问题出在轴的间隔属性(每月间隔、间隔 = 1 等)上,但我似乎找不到解决方法。
如果有人经历过类似的事情并且可能有一些指示,那就太好了!
提前谢谢!!
[已编辑]
我离我需要实现的目标更近了一点。
看看这张图片:
https://i.stack.imgur.com/KRYcX.png
我将其添加到 X 轴(以及对属性的其他修改,但这就是诀窍):
<AxisX IntervalAutoMode="VariableCount" Interval="0">
。它自己"跳过"了几个月。不知道现在去哪里,但我正在搜索从 MSDN 下载的 WebSamples 应用程序(找不到链接,很抱歉),也许那里有东西。
好吧,它开始了。
我决定重写整个事情而不使用ObjectDataSource。我从 Chart4 对象中删除了所有属性(所有间隔、每月内容等等),并将所有内容都写在后面的代码上。我还必须相应地格式化 X 轴。
PS:我觉得有点愚蠢,因为我没有考虑清楚这一点,而且看起来也不漂亮,但是由于我已经连续三天挣扎了,所以必须这样做。一旦我有时间,我会重新重构并使其更加优雅和高效。另外,很抱歉这 VB.NET,我没有关于语言选择的说法,哈哈。
看看吧:
<asp:Chart ID="Chart4" runat="server" CssClass="Chart" BorderlineDashStyle="DashDotDot" Palette="Pastel"
ImageStorageMode="UseImageLocation" Height="650px">
<Legends>
<asp:Legend Name="Legend1" Alignment="Center" Docking="Bottom">
</asp:Legend>
</Legends>
<Titles>
<asp:Title Font="Arial Narrow, 14pt" Name="Title1">
</asp:Title>
</Titles>
</asp:Chart>
在后端,这就是我所做的:
Dim date As String
Dim value As Double
Dim coef As Double
Dim chartArea As New ChartArea("chartEficOp")
chartArea.BackColor = Drawing.Color.Transparent
chartArea.ShadowOffset = 5
Dim valueSeries As New Series("valueY")
valueSeries.ChartArea = "chartEficOp"
valueSeries.Color = System.Drawing.ColorTranslator.FromHtml("#4f81bd")
valueSeries.YAxisType = AxisType.Primary
valueSeries.IsValueShownAsLabel = True
valueSeries.ChartType = SeriesChartType.Column
valueSeries.Legend = "Legend1"
valueSeries.YValueType = ChartValueType.Double
Dim serieEfic As New Series("valueY2")
coefSeries.ChartArea = "chartEficOp"
coefSeries.Color = System.Drawing.ColorTranslator.FromHtml("#9bbb59")
coefSeries.YAxisType = AxisType.Secondary
coefSeries.IsValueShownAsLabel = True
coefSeries.ChartType = SeriesChartType.Line
coefSeries.Legend = "Legend1"
coefSeries.YValueType = ChartValueType.Double
coefSeries.BorderWidth = 4
coefSeries.LabelFormat = "{0:0.##'%}"
Dim targetSerie As New Series("0,15%")
targetSerie.ChartArea = "chartEficOp"
targetSerie.Color = System.Drawing.Color.Red
targetSerie.YAxisType = AxisType.Secondary
targetSerie.IsValueShownAsLabel = False
targetSerie.ChartType = SeriesChartType.Line
targetSerie.Legend = "Legend1"
targetSerie.YValueType = ChartValueType.Double
targetSerie.BorderWidth = 2
targetSerie.BorderDashStyle = ChartDashStyle.Dash
Dim dt as DataTable = <called the stored proc here>
For Each item As DataRow In dt.Rows
date = String.Format("{0:MMM/yy}", item("DATA"))
Select Case <date here, to sort the x-label out>
Case "01/01/2013"
If date = "jan/11" Then
date = "2011"
ElseIf date = "jan/12" Then
date = "2012"
End If
Case "01/01/2012"
If date = "jan/11" Then
date = "2011"
End If
End Select
value = item("VALUE")
coef = item("COEF")
valueSeries.Points.AddXY(date, value)
coefSeries.Points.AddXY(date, coef)
targetSerie.Points.AddXY(date, 0.15)
Next
Chart4.ChartAreas.Add(chartArea)
Chart4.Series.Add(valueSeries)
Chart4.Series.Add(coefSeries)
Chart4.Series.Add(targetSerie)
Chart4.ChartAreas(0).AxisX.MajorGrid.Enabled = False
Chart4.ChartAreas(0).AxisX.Interval = 1
Chart4.ChartAreas(0).AxisY.MajorGrid.Enabled = False
Chart4.ChartAreas(0).AxisY.Title = "US$ / 1000"
Chart4.ChartAreas(0).AxisY2.MajorGrid.Enabled = False
Chart4.ChartAreas(0).AxisY2.Title = "% Cost"
Chart4.ChartAreas(0).AxisY2.Interval = 0.04
。结果是:
https://i.stack.imgur.com/HGKCa.png
希望这将在未来对某人有所帮助。