Microsoft Chart Control 生成与 set 属性不同的视觉对象。基于属性的它始终显示正确的值,但是如果您使用 Photoshop 等图形软件打开生成的图表,它会显示像素与设置的属性不匹配。
我在尝试将所有 InnerPlot 位置检索到 HiddenField 时发现了这个错误。目前基于此错误,无法获得 ChartAreas 或 InnerPlots 的正确绝对位置。如果您认为有解决方法,请告诉我...
感谢您的时间和关注...
重现步骤
测试.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>
<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Chart ID="Chart1" runat="server"></asp:Chart>
</div>
</form>
</body>
</html>
测试.aspx.vb
Imports System.Web.UI.DataVisualization.Charting
Partial Class Test
Inherits System.Web.UI.Page
Private Sub DataHelper()
Dim Historical() As String = _
{ _
"Date,Open,High,Low,Close,Volume", _
"2009-03-05,1.75,2.00,1.73,1.81,47339300", _
"2009-03-04,1.90,1.90,1.83,1.87,22306600", _
"2009-03-03,1.91,1.91,1.80,1.81,15412400", _
"2009-03-02,1.91,1.94,1.83,1.88,19585500", _
"2009-02-27,1.93,2.00,1.80,2.00,31469500", _
"2009-02-26,2.08,2.09,1.81,1.98,32307100", _
"2009-02-25,2.10,2.16,2.00,2.01,54325200", _
"2009-02-24,1.80,2.00,1.80,2.00,33935300", _
"2009-02-23,1.65,1.91,1.61,1.73,44444100", _
"2009-02-20,1.60,1.61,1.50,1.58,37889100" _
}
Dim Query As IEnumerable = From HistoricalLine In Historical Skip 1 _
Where Not String.IsNullOrEmpty(HistoricalLine) _
Let HistoricalFields = HistoricalLine.Split(",") _
Let HistoricalDate = CDate(HistoricalFields(0)) _
Order By HistoricalDate Ascending _
Select New With { _
.Date = HistoricalDate, _
.Open = CDbl(HistoricalFields(1)), _
.High = CDbl(HistoricalFields(2)), _
.Low = CDbl(HistoricalFields(3)), _
.Close = CDbl(HistoricalFields(4)), _
.Volume = CDbl(HistoricalFields(5)) _
}
Chart1.DataSource = Query
End Sub
Private Sub CreateChartArea(ByVal areaname As String)
Chart1.ChartAreas.Add(New ChartArea)
With Chart1.ChartAreas.Last
.Name = areaname
.AxisY2.Enabled = AxisEnabled.False
.AxisX2.Enabled = AxisEnabled.False
With .AxisX
.MajorTickMark.Enabled = False
.MinorTickMark.Enabled = False
.MinorGrid.Enabled = False
.MajorGrid.LineColor = Drawing.Color.Yellow
End With
With .AxisY
.MajorTickMark.Enabled = False
.MinorTickMark.Enabled = False
.MinorGrid.Enabled = False
.MajorGrid.LineColor = Drawing.Color.Red
.IsStartedFromZero = False
End With
.BackColor = Drawing.Color.SkyBlue
.BorderDashStyle = ChartDashStyle.Solid
.BorderColor = Drawing.Color.Brown
.BorderWidth = 0
.ShadowOffset = 0
End With
End Sub
Private Sub CreateSeries(ByVal seriename As String, ByVal areaname As String)
Chart1.Series.Add(New Series)
With Chart1.Series.Last
.Name = seriename
.ChartArea = areaname
.YValuesPerPoint = 4
.ChartType = SeriesChartType.Candlestick
.XValueType = ChartValueType.DateTime
.IsXValueIndexed = True
.XValueMember = "Date"
.YValueMembers = "High,Low,Open,Close"
.BorderColor = Drawing.Color.Green
.IsValueShownAsLabel = True
End With
End Sub
Private Sub ChartHelper()
Chart1.ImageStorageMode = ImageStorageMode.UseImageLocation
Chart1.ImageLocation = "~/_temp/HG_#SEQ(300,60)" '<= make sure you have "_temp" directory
Chart1.ImageType = ChartImageType.Png
Chart1.RenderType = RenderType.ImageTag
Chart1.Titles.Add(New Title())
Chart1.Legends.Add(New Legend())
Chart1.Legends.Last.Enabled = False
Chart1.Palette = ChartColorPalette.BrightPastel
Chart1.BackColor = Drawing.Color.Pink
CreateChartArea("A1")
CreateSeries("S1", "A1")
CreateChartArea("A2")
CreateSeries("S2", "A2")
CreateChartArea("A3")
CreateSeries("S3", "A3")
CreateChartArea("A4")
CreateSeries("S4", "A4")
Chart1.DataBind()
Chart1.SaveXml(Server.MapPath("~/_temp/MyChart.xml"))
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataHelper()
ChartHelper()
TestPart()
End If
End Sub
Private Sub TestPart()
Dim SpaceBetweenChartAreas As Single = 1 '<= %1 of Chart's Height
Chart1.Height = 500
Chart1.Width = 1000
Dim locator As Integer = 0
For Each chartareaItem In Chart1.ChartAreas
With chartareaItem
.Position.Auto = False
.Position.Height = 20 '500px/100*20 = 100px
.Position.Width = 80 '1000px/100*80 = 800px
.Position.X = 0
.Position.Y = locator
.InnerPlotPosition.Auto = False
.InnerPlotPosition.Height = 100 '%100 of ChartArea Position's Height
.InnerPlotPosition.Width = 100 ' %100 of ChartArea Position's Width
.InnerPlotPosition.X = 0
.InnerPlotPosition.Y = 0
locator += SpaceBetweenChartAreas + .Position.Height
End With
Next
End Sub
End Class
请不要忘记创建一个目录 "_temp"
你也可以在http://ilerler.com/-bug4ms/Test.png查看实际结果
结果
第一个 ChartArea 高度
实际 101px | 预期 100 像素
空间
实际 4 像素 | 预期 5px
第二个 ChartArea 高度
实际 101px | 预期 100 像素
空间
实际 4 像素 | 预期 5px
第三个 ChartArea 高度
实际 100px | 预期 100 像素
空间
实际 4 像素 | 预期 5px
第 4 个 ChartArea 高度
实际 101px | 预期 100 像素
空间
实际 85 像素 | 预计 85 像素