2013-06-24 66 views
1

我是因爲看了這樣的一個表:創建二級x軸

Date |    1/1/13    |    1/8/13    | ... 
     | Group1 | Group2 | Group3 | Group1 | Group2 | Group3 | ... 
Type1 |  1 |  2 |  3 |  5 |  6 |  7 | ... 
Type2 |  6 |  5 |  4 |  4 |  8 |  0 | ... 
Type3 |  7 |  8 |  9 |  9 |  3 |  2 | ... 

我的任務是創建使用MS圖表來表示這個數據的圖表。該表格表示在顯示日期結束的一週內某個組已完成的任務(類型1/2/3)。我的想法是先創建一個條形圖,然後由該組按日期分組。我希望我的x軸看起來像上面的表格標題。

我查了一下第二軸主題,所有我能找到的都是第二Y軸。這與我所期望的不同,因爲它代表了每個y軸上的不同系列。我希望能夠代表一個系列,但有兩個x軸標籤。這甚至有可能嗎?我不相信它是,因爲數據點CA只有兩個值

var chart = new Chart(); 
chart.ChartAreas.Add(new ChartArea()); 
var series= new Series("series"); 
series.ChartType = SeriesChartType.Column; 
series.XAxisType = AxisType.Primary; 
//Magic secondary axis code  

//Add data points 

chart.Series.Add(series); 

而且僅作參考,這裏是我計劃使用的類。

public class ChartGroup 
{ 
    public string GroupName 
    public int Type1 
    public int Type2 
    public int Type3 
} 
public class ChartDate 
{ 
    public DateTime Date 
    public List<ChartGroup> GroupData 
} 
public class Chart 
{ 
    public List<ChartDate> ChartData 
} 

編輯:我相信,可以通過進行一系列對於每種類型的,並且繪圖它的Date x軸來創建這樣的圖表。這是唯一的方法嗎?

回答

3

可能有第二個X軸。這裏是一個例子(它不完全是你想要做的,但應該告訴你如何使用輔助X軸):

DateTime firstDay = new DateTime(2013, 01, 01); 
DateTime secondDay = new DateTime(2013, 01, 02); 

int[] group1 = new int[6] { 1, 6, 7, 5, 4, 9 }; 
int[] group2 = new int[6] { 2, 5, 8, 6, 8, 3 }; 

DateTime[] days = new DateTime[6] { firstDay, firstDay, firstDay, secondDay, secondDay, secondDay}; 

chart.Series.Add(new Series("Group 1")); 

chart.Series[0].Points.DataBindXY(days, group1); 
chart.Series[0].ChartType = SeriesChartType.Column; 

chart.Series.Add(new Series("Group 2")); 
chart.Series[1].Points.DataBindXY(days, group2); 
chart.Series[1].ChartType = SeriesChartType.Column; 

double start = chart.Series[0].Points[0].XValue; 
double end = chart.Series[0].Points[chart.Series[0].Points.Count -1].XValue; 
double half = (start + end)/2; 

chart.ChartAreas[0].AxisX2.Enabled = AxisEnabled.True; 

chart.ChartAreas[0].AxisX2.CustomLabels.Add(start, end, "General Label", 0, LabelMarkStyle.Box); 
chart.ChartAreas[0].AxisX2.CustomLabels.Add(start, half, "Day 1", 1, LabelMarkStyle.LineSideMark); 
chart.ChartAreas[0].AxisX2.CustomLabels.Add(half, end, "Day 2", 1, LabelMarkStyle.LineSideMark);