2014-03-27 163 views
2

我需要使用列或欄類型將4個系列數據繪製到MSChart。我可以繪製這些4系列的數據,以便數據重疊而不是堆疊。繪製重疊的列或條形圖

我剛剛發現辦公室Excel中有一個ChartGroup.Overlap屬性。

我該怎麼做MSChart?如果沒有,圖表控制可以做到這一點?任何信息將不勝感激。

+0

嘗試打開3D! – TaW

回答

1

不確定這是否是理想的解決方案,但是如果您在圖表中創建兩個ChartArea s,然後將其中一個放在另一個的頂部,則可以重疊系列。這需要大量的擺弄周圍的位置,大小,軸等,讓他們排隊,所以需要一點點的努力,但產生以下

enter image description here

Chart _chart = new Chart(); 
TabPage2.Controls.Add(_chart); 

_chart.Location = new Point(469, 37); 
_chart.Name = "chart1"; 
_chart.Size = new Size(448, 260); 


DataTable dt1 = new DataTable(); 
dt1.Columns.Add("XVals", typeof(string)); 
dt1.Columns.Add("YVals1", typeof(int)); 
dt1.Columns.Add("YVals2", typeof(int)); 

foreach (string c in "ABCDEF".ToCharArray()) { 
    dt1.Rows.Add(c, Convert.ToInt32(Math.Ceiling(VBMath.Rnd() * 20)), Convert.ToInt32(Math.Ceiling(VBMath.Rnd() * 20))); 
} 

ChartArea firstArea = _chart.ChartAreas.Add("First Area"); 
Series seriesFirst = _chart.Series.Add("First Series"); 
seriesFirst.ChartType = SeriesChartType.Column; 

ChartArea secondArea = _chart.ChartAreas.Add("Second Area"); 
secondArea.BackColor = Color.Transparent; 
secondArea.AlignmentOrientation = AreaAlignmentOrientations.All; 
secondArea.AlignmentStyle = AreaAlignmentStyles.All; 
secondArea.AlignWithChartArea = firstArea.Name; 
secondArea.AxisY.LabelStyle.Enabled = false; 
secondArea.AxisX.LabelStyle.Enabled = false; 

Series seriesSecond = _chart.Series.Add("Second Series"); 
seriesSecond.ChartType = SeriesChartType.Column; 
seriesSecond.ChartArea = secondArea.Name; 

_chart.DataSource = dt1; 
seriesFirst.Points.DataBind(dt1.DefaultView, "XVals", "YVals1", null); 
seriesSecond.Points.DataBind(dt1.DefaultView, "XVals", "YVals2", null); 

//Aligning the Y axis of the two chart areas 
//I am assuming here the x values for both series are similar and dont need to be altered 
//If using bar chart then x axis would be modifed not the y axis 
secondArea.AxisY = firstArea.AxisY; 

// *** Set locational values here for your first chart area*** 
int heightAboveChartArea = 20; 
int heightBelowChartArea = 20; 
int axisLabelHeight = 40; 
int widthLeftOfChartArea = 20; 
int widthRightOfChartArea = 20; 
int heightPerBar = 20; 
int numberOfPoints = _chart.Series(0).Points.Count; 

// *** The following code should not normally be modified *** 
_chart.ChartAreas(0).Position.X = widthLeftOfChartArea/_chart.Width * 100; 
_chart.ChartAreas(0).Position.Width = 100 - (widthRightOfChartArea/_chart.Width * 100) - _chart.ChartAreas(0).Position.X; 
_chart.ChartAreas(0).Position.Y = (heightAboveChartArea/_chart.Height * 100); 
_chart.ChartAreas(0).Position.Height = 100 - (heightBelowChartArea/_chart.Height * 100) - _chart.ChartAreas(0).Position.Y; 
+0

但是,如果第一個系列值小於第二個值,則不會在圖表中看到第一個列值。就像示例圖表中的列C,D,F一樣。 –

+0

要開始大量定製圖表[像excel],解決方案往往變得非常黑客。像這樣的東西會改變柱形圖的寬度,所以如果一個系列通常比另一個系列更大,並把它放在後面'_chart.Series(0)(「PixelPointWidth」)= 10; _chart.Series(1)(「PixelPointWidth」)= 15'。沒有像我知道的那樣建立像Excel這樣的重疊,所以任何解決方案都會非常黑客,並且需要手動放置列,重新排列座標軸等。 – TylerDurden