[英]ZedGraph stacked bar with multiple points in C#
我所擁有的是一個看起來像這樣的配置文件:
Item|Size|Directory|Data
A|12|D_01|11/28/10
C|13|D_01|11/28/10
B|12|D_01|11/28/10
Back|122|D_02|11/28/10
Body|112|D_02|11/28/10
A|12|D_01|11/29/10
C|13|D_01|11/29/10
B|12|D_01|11/29/10
Back|122|D_01|11/29/10
Body|112|D_01|11/29/10
Arm|20|D_01|11/29/10
我的x軸是日期,我沒有問題,並且如果我只有一個固定數量的實例來構成我的YI會很好,但是如何創建一個單獨的堆疊條形圖來自動為每個項目取點每個目錄?
因此,堆疊的條形圖1將是ABC,Body,Back,Arm,並且在29所示的示例中,我添加了一個Arm項。 因此,第28條的條形圖1的得分為5分,第29條的堆疊條形圖2的得分為6分。
我希望這是有道理的。除了如何正確顯示條形,我不需要任何幫助。
問題尚不清楚,假設目錄僅需要兩個小節(2個日期中的每個日期為1個),那么目錄將如何影響數據顯示。 有兩種格式化日期軸和處理空數據的方法: 1使用AxisType.Date = DateAsOrdinal並為數據為空的日期添加零值,或使用AxisType.Date = Date的[2]而不為以下值添加值數據為空的日期。 如果數據為空,則DateAsOrdinal似乎將數據應用於錯誤的日期-這似乎是一個錯誤或至少不直觀。 該示例使用AxisType.Date = Date方法,該方法需要對日期軸進行額外的格式化。
using ZedGraph;
...
GraphPane myPane = zg1.GraphPane; // zg1 is a ZedGraph control.
// Set the title and axis label
myPane.Title.Text = "Stacked Bar, ZedGraph ver 5.1.5.28844";
myPane.YAxis.Title.Text = "Value";
// Create two dates.
XDate date1 = new XDate(2010, 11, 28); // First date.
XDate date2 = new XDate(2010, 11, 29); // Second date.
// Create data lists and bars for A, B, C, Back, Body and Arm (6 total).
ZedGraph.PointPairList listA = new ZedGraph.PointPairList();
listA.Add( date1, 12 );
listA.Add( date2, 13);
BarItem barA = zg1.GraphPane.AddBar("A", listA, Color.Red );
barA.Bar.Fill = new Fill( Color.Red );
ZedGraph.PointPairList listB = new ZedGraph.PointPairList();
listB.Add(date1, 12);
listB.Add(date2, 12);
BarItem barB = zg1.GraphPane.AddBar("B", listB, Color.Blue);
barB.Bar.Fill = new Fill(Color.Blue);
ZedGraph.PointPairList listC = new ZedGraph.PointPairList();
listC.Add(date1, 13);
listC.Add(date2, 12);
BarItem barC = zg1.GraphPane.AddBar("C", listC, Color.Green);
barC.Bar.Fill = new Fill(Color.Green);
ZedGraph.PointPairList listBack = new ZedGraph.PointPairList();
listBack.Add(date1, 122);
listBack.Add(date2, 122);
BarItem barBack = zg1.GraphPane.AddBar("Back", listBack, Color.Red);
barBack.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
ZedGraph.PointPairList listBody = new ZedGraph.PointPairList();
listBody.Add(date1, 112);
listBody.Add(date2, 112);
BarItem barBody = zg1.GraphPane.AddBar("Body", listBody, Color.Blue);
barBody.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);
ZedGraph.PointPairList listArm = new ZedGraph.PointPairList();
// listArm.Add(date1, 0); // Not needed for XAxis.Type = AxisType.Date.
listArm.Add(date2, 20);
BarItem barArm = zg1.GraphPane.AddBar("Arm", listArm, Color.Green);
barArm.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);
// Done creating bars.
myPane.BarSettings.Type = BarType.Stack; // stacks bar rather than side-by-side.
myPane.BarSettings.ClusterScaleWidth = 1; // Widen bars somewhat.
// Format the X axis.
XAxis x = myPane.XAxis;
x.Type = AxisType.Date;
x.Scale.Format = "yyyy-mm-dd";
x.Scale.BaseTic = date1; // Puts the first major tic at the first date.
x.Scale.Min = date1 - 0.5; // Manually set the left of the graph window just prior to the first date.
x.Scale.Max = date2 + 0.5; // Manually set the right of the graph window just after the last date.
// Setting the step size...
// Isn't required if AxisType.Date is DateAsOrdinal.
// Docs says that MinorStep is ignore for AxisType.Date but seems inaccurate.
// Step value of 24 was derived by trial and error. Seems it should be 1 if Unit = DateUnit.Day.
// A value of 24 implies that the Units are acting like DateUnit.Hour.
x.Scale.MajorUnit = DateUnit.Day;
x.Scale.MinorUnit = DateUnit.Day;
x.Scale.MinorStep = 24;
x.Scale.MajorStep = 24;
zg1.AxisChange(); // Update graph.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.