2012-10-12 33 views
2

我正在用C#創建一個Microsoft Chart。它工作正常,但是當沒有數據時,Y軸消失。我希望Y軸始終可見。我試圖找到解決方法,但我沒有辦法。Microsoft Charting - 使Y軸始終可見

這是我的代碼:

Chart chart = new Chart { 
    AntiAliasing = AntiAliasingStyles.All, 
    TextAntiAliasingQuality = TextAntiAliasingQuality.High, 
    BackColor = Color.FromArgb(250, 250, 250), 
    Height = size.Height, 
    Width = size.Width 
}; 

chart.Legends.Clear(); 

ChartArea area = new ChartArea { 
    BackColor = Color.Transparent, 
    BorderColor = Color.FromArgb(240, 240, 240), 
    BorderWidth = 1, 
    BorderDashStyle = ChartDashStyle.Solid, 
    AxisX = new Axis { 
    Enabled = AxisEnabled.True, 
    IntervalAutoMode = IntervalAutoMode.VariableCount, 
    IsLabelAutoFit = true, 
    IsMarginVisible = true, 
    LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) }, 
    LineColor = Color.FromArgb(220, 220, 220), 
    MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid }, 
    MajorTickMark = new TickMark { LineColor = Color.FromArgb(220, 220, 220), Size = 4.0f }, 
    }, 
    AxisY = new Axis { 
    Enabled = AxisEnabled.True, 
    IntervalAutoMode = IntervalAutoMode.VariableCount, 
    IsLabelAutoFit = true, 
    IsMarginVisible = true, 
    LabelStyle = new LabelStyle { ForeColor = Color.FromArgb(100, 100, 100), Font = new Font("Arial", 10, FontStyle.Regular) }, 
    LineColor = Color.Transparent, 
    MajorGrid = new Grid { LineColor = Color.FromArgb(240, 240, 240), LineDashStyle = ChartDashStyle.Solid }, 
    MajorTickMark = new TickMark { LineColor = Color.FromArgb(240, 240, 240), Size = 2.0f } 
    }, 
    Position = new ElementPosition { Height = 100, Width = 100, X = 0, Y = 0 } 
}; 

chart.ChartAreas.Add(area); 

area.AxisX.LabelStyle.Format = "H:mm"; 
area.AxisX.LabelStyle.IntervalType = DateTimeIntervalType.Hours; 

Series series = new Series { 
    CustomProperties = "PointWidth = 1", 
    IsXValueIndexed = true, 
    XValueType = (ChartValueType)Enum.Parse(typeof(ChartValueType), x.Data.GetType().GetGenericArguments()[0].Name) 
}; 

series.BorderWidth = 2; 
series.BorderColor = Color.FromArgb(84, 164, 232); 
series.ChartType = SeriesChartType.Area; 
series.Color = Color.FromArgb(222, 234, 244); 

series.Points.DataBindXY(x.Data, s.Data); 

chart.Series.Add(series); 

回答

1

據我知道這是不可能的。

作爲解決方案,您可以添加另一個虛擬「行」系列,其中包含一個數據點。對於單個數據點,線條不會被繪製 - 但是軸線將被顯示。