[英]How to change X-axis datetime value jan-01 to Oct-01 using .net chart control
我試圖以date(MMM-yy)的形式顯示x軸值,但它始終以jan-01開頭。 因此,請提供解決方案以顯示除jan-01之外的其他內容,我的意思是代替Jan-01 show oct-01。
請找到模擬功能:
private static void drawGraph()
{
List<GraphPoints> listGP = new List<GraphPoints>();
listGP.Add(new GraphPoints()
{
RecordDate = "01/10/1984",
benchmark = "10000.00"
});
listGP.Add(new GraphPoints()
{
RecordDate = "29/06/1987",
benchmark = "30396.00"
});
listGP.Add(new GraphPoints()
{
RecordDate = "31/05/1989",
benchmark = "10000.00"
});
listGP.Add(new GraphPoints()
{
RecordDate = "30/09/1993",
benchmark = "310137.88"
});
listGP.Add(new GraphPoints()
{
RecordDate = "31/12/2015",
benchmark = "440037.28"
});
Graph.Chart chart;
chart = new Graph.Chart();
chart.Location = new System.Drawing.Point(10, 10);
chart.Size = new System.Drawing.Size(800, 300);
chart.ChartAreas.Add("draw");
chart.ChartAreas["draw"].AxisX.IntervalType = Graph.DateTimeIntervalType.Years;
chart.ChartAreas["draw"].AxisX.LabelStyle.Format = "MMM-yyyy";
chart.ChartAreas["draw"].AxisX.MajorGrid.LineColor = Color.Black;
chart.ChartAreas["draw"].AxisX.MajorGrid.LineDashStyle = Graph.ChartDashStyle.NotSet;
chart.ChartAreas["draw"].AxisY.IntervalAutoMode = Graph.IntervalAutoMode.VariableCount;
chart.ChartAreas["draw"].AxisY.MajorGrid.LineColor = Color.Black;
chart.ChartAreas["draw"].AxisY.MajorGrid.LineDashStyle = Graph.ChartDashStyle.NotSet;
chart.ChartAreas["draw"].BackColor = Color.White;
chart.Series.Add("Bench-Mark");
chart.Series["Bench-Mark"].XValueType = Graph.ChartValueType.Date;
chart.Series["Bench-Mark"].ChartType = Graph.SeriesChartType.Line;
chart.Series["Bench-Mark"].Color = Color.Red;
chart.Series["Bench-Mark"].BorderWidth = 1;
foreach (var item in listGP)
{
chart.Series["Bench-Mark"].Points.AddXY(Convert.ToDateTime(item.RecordDate).ToOADate(), item.benchmark);
}
chart.SaveImage("MyImage.jpg", Graph.ChartImageFormat.Jpeg);
}
請參見Graph軸點顯示自1988年1月。
在MSDN上有一個漂亮的頁面,上面有各種類型的標簽。
它解釋了三種標簽類型:
AxisX.LabelStyle.Format
和Axis.Interval
屬性的Label創建未真正連接到數據點的常規標簽網格 DataPoint.AxisLabel
屬性分別標記每個DataPoint
CustomLabels
在軸上的任意點設置標簽。 您使用的是第一個選項,但是這會將Label
放置在AxisX.IntervalType
的一個單元的開頭,即使您切換到Months
由於您的點AxisX.IntervalType
,在您的情況下,這也是幾年。
這確實應該很容易糾正。 由於不希望將IntervalType
單位標記為單個DataPoints
,因此應將AxisLabels
添加到每個DataPoint
並且所有標記都應該很好:
Series S1 = chart.Series["Bench-Mark"];
foreach (var item in listGP)
{
DateTime dt = Convert.ToDateTime(item);
int p = S1.Points.AddXY(dt, listGP[item]);
string l = dt.ToString("MMM-yyyy");
S1.Points[p].AxisLabel = l;
}
不幸的是,對於您的數據, Chart
控件的內置“智能”使此操作變得更加困難。 問題是,無論您選擇Interval
和IntervalType
哪種組合,它都永遠不會顯示每個 AxisLabel
。 實際上,我只能顯示那些實際達到Interval
,例如, DataPoints
在每月的1號秋天。 但是您的數據會隨機散布到幾年中。
但是您可以使用CustomLabels
作為解決方法。 在上面的代碼中添加AxisLabels
之后,調用這個小幫助函數來添加CustomLabels
; 我們需要告訴Chart
分別顯示哪個范圍,並在足夠大的范圍內將它們全部顯示在應顯示的位置(根據我的數據):
void addCustomLabels(Chart chart)
{
Series S1 = chart.Series[0];
ChartArea CA = chart.ChartAreas[0];
CA.AxisX.CustomLabels.Clear();
for (int i = 0; i < S1.Points.Count; i++)
{
CustomLabel cl = new CustomLabel();
cl.FromPosition = S1.Points[i].XValue - 10; // 10 day back and ahead..
cl.ToPosition = S1.Points[i].XValue + 10; //..will be enough space for one label
cl.Text = S1.Points[i].AxisLabel;
CA.AxisX.CustomLabels.Add(cl);
}
}
請注意,僅當您的數據點與示例中一樣稀疏時 ,這種解決方案才有效。 對於許多點,標簽會使軸混亂 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.