MSChart在vs2008中使用遇到一個問題,坐標軸的標題為中文時被圖表區域遮擋了一部分.
解決辦法:在說明文字前加\n實現換一行顯示.
//this.Chart1.ChartAreas[0].AxisX.LabelStyle.Angle =-90;//x軸傾斜的角度。
//設置坐標軸交錯顯示
//chartFreq.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = true;
//legend文字設置,Series的label文字設置,Series的label的Tooltip文字設置
LegendText="#VALX" Label="#PERCENT{P1}" LabelToolTip="#VALX"
//前台設置,交錯和文字角度.--IsStaggered="True" Angle="90"
<AxisX LineColor="64, 64, 64, 64" Interval="1.0" IsLabelAutoFit="False">
<LabelStyle Font="宋體, 13px" IsStaggered="True" Angle="90" />
<MajorGrid LineColor="64, 64, 64, 64" />
</AxisX>
X軸坐標如果超過9位的話,就不能完全顯示了,就會一個隔一個的顯示,這樣讓人很不爽,其實只要進行如下設置:
Chart1.ChartAreas[0].AxisX.Interval = 1; //設置X軸坐標的間隔為1
Chart1.ChartAreas[0].AxisX.IntervalOffset = 1; //設置X軸坐標偏移為1
Chart1.ChartAreas[0].AxisX.LabelStyle.IsStaggered = true; //設置是否交錯顯示,比如數據多的時間分成兩行來顯示
官方文檔:
http://msdn.microsoft.com/zh-cn/library/dd489237(v=vs.100).aspx
園友博客:
http://www.cnblogs.com/shuncy/archive/2008/11/07/1328738.html
http://www.cnblogs.com/shuncy/archive/2008/11/10/1330827.html
官方示例:
http://archive.msdn.microsoft.com/mschart
轉:簡介
http://blog.csdn.net/gzy11/article/details/5190228
參考一:
mschart總結:
1. mschart的坐標軸默認類型為decimal,所以不能太大。
2. 可以為每個點添加提示信息;
/// <summary>
/// 設置點提示信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void chartCWPBestMode_PreRender(object sender, EventArgs e)
{
for (int i = 0; i < chartCWPBestMode.Series["Series1"].Points.Count; i++)
{
chartCWPBestMode.Series["Series1"].Points[i].ToolTip = i.ToString();
}
}
3.再每次加載數據時先清空曲線,防止頁面刷新后曲線重疊。
chartCWPBestMode.Series["Series1"].Points.Clear();
chartCWPBestMode.Series["Series2"].Points.Clear();
chartCWPBestMode.Series["Series3"].Points.Clear();
chartCWPBestMode.Series["Series4"].Points.Clear();
4.以下是我在做的時候控制樣式時用到的方法
/// <summary>
/// 設置mschart樣式
/// </summary>
private void SetMSChartStyle()
{
//繪圖前期處理
chartCWPBestMode.Titles.Clear();
//標題設置
Title title = new Title();
title.Text = "循環水泵最佳運行方式";
title.Font = new Font("宋體", 16f, FontStyle.Bold);
//標題
chartCWPBestMode.Titles.Add(title);
// 坐標軸設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.IsMarginVisible = false;
//X 軸坐標最大最小值
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Minimum = 5;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Maximum = 35;
// 坐標軸刻度線不延長出來設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorTickMark.Enabled = false;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorTickMark.Enabled = false;
//X 次要輔助線設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.Enabled = true;
//X 次要輔助線間距
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.Interval = 1;
//X 次要輔助線顏色
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MinorGrid.LineColor = Color.LightGray;
//Y 次要輔助線設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.Enabled = true;
//Y 次要輔助線間距
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.Interval = 10;
//Y 次要輔助線顏色
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MinorGrid.LineColor = Color.LightGray;
//X 主要輔助線設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.Enabled = true;
//X 主要輔助線間距
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.Interval = 5;
//X 主要輔助線顏色
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.MajorGrid.LineColor = Color.Black;
//Y 主要輔助線設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.Enabled = true;
//Y 主要輔助線間距
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.Interval = 30;
//Y 主要輔助線顏色
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.MajorGrid.LineColor = Color.Black;
//坐標主要輔助線刻度間距
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Interval = 5;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.Interval = 30;
//坐標軸說明
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.Title = "凝汽器冷卻水進口溫度(℃)";
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.Title = "機組負荷(MW)";
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.TitleFont = new Font("宋體", 10f, FontStyle.Bold);
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.TitleFont = new Font("宋體", 10f, FontStyle.Bold);
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisX.TitleAlignment = StringAlignment.Far;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].AxisY.TitleAlignment = StringAlignment.Far;
//邊框樣式設置
chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderColor = Color.Black;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderDashStyle = ChartDashStyle.Solid;
chartCWPBestMode.ChartAreas["ChartAreaCWP"].BorderWidth = 2;
//圖例文字
chartCWPBestMode.Series["SeriesCurrentMode"].LegendText = "當前運行方式";
chartCWPBestMode.Series["SeriesTRAN1"].LegendText = "單泵高速切換曲線";
chartCWPBestMode.Series["SeriesTRAN2"].LegendText = "兩機三泵切換曲線";
chartCWPBestMode.Series["SeriesTRAN3"].LegendText = "一高一低切換曲線";
chartCWPBestMode.Series["SeriesTRAN4"].LegendText = "兩泵高速切換曲線";
//圖例位置、字體設置;坐標軸位置設定
chartCWPBestMode.Legends[0].Position = new ElementPosition(10, 10, 88, 7);
chartCWPBestMode.Legends[0].Font = new Font("宋體", 9);
chartCWPBestMode.ChartAreas[0].InnerPlotPosition = new ElementPosition(6, 5, 90, 82);
}
5.附MSChart屬性列表
ChartAreas:增加多個繪圖區域,每個繪圖區域包含獨立的圖表組、數據源,用於多個圖表類型在一個繪圖區不兼容時。
AlignmentOrientation:圖表區對齊方向,定義兩個繪圖區域間的對齊方式。
AlignmentStyle:圖表區對齊類型,定義圖表間用以對其的元素。
AlignWithChartArea:參照對齊的繪圖區名稱。
InnerPlotPosition:圖表在繪圖區內的位置屬性。
Auto:是否自動對齊。
Height:圖表在繪圖區內的高度(百分比,取值在0-100)
Width:圖表在繪圖區內的寬度(百分比,取值在0-100)
X,Y:圖表在繪圖區內左上角坐標
Position:繪圖區位置屬性,同InnerPlotPosition。
Name:繪圖區名稱。
Axis:坐標軸集合
Title:坐標軸標題
TitleAlignment:坐標軸標題對齊方式
Interval:軸刻度間隔大小
IntervalOffset:軸刻度偏移量大小
MinorGrid:次要輔助線
MinorTickMark:次要刻度線
MajorGrid:主要輔助線
MajorTickMark:主要刻度線
DataSourceID:MSChart的數據源。
Legends:圖例說明。
Palette:圖表外觀定義。
Series:最重要的屬性,圖表集合,就是最終看到的餅圖、柱狀圖、線圖、點圖等構成的合;可以將多種相互兼容的類型放在一個繪圖區域內,形成復合圖。
IsValueShownAsLabel:是否顯示數據點標簽,如果為true,在圖表中顯示每一個數據值
Label:數據點標簽文本
LabelFormat:數據點標簽文本格式
LabelAngle:標簽字體角度
Name:圖表名稱
Points:數據點集合
XValueType:橫坐標軸類型
YValueType:縱坐標軸類型
XValueMember:橫坐標綁定的數據源(如果數據源為Table,則填寫橫坐標要顯示的字段名稱)
YValueMembers:縱坐標綁定的數據源(如果數據源為Table,則填寫縱坐標要顯示的字段名稱,縱坐標可以有兩個)
ChartArea:圖表所屬的繪圖區域名稱
ChartType:圖表類型(柱形、餅形、線形、點形等)
Legend:圖表使用的圖例名稱
Titles:標題集合。
width:MSChart的寬度。
height:MSChart的高度。
注意:清空MSchart方法.
如果在同一個頁面中顯示不同的曲線。在曲線切換時會出現MSchart設置不能正確初始化。
方法是:在HTML中不能寫死<ChartAreas>等,必須在程序中動態添加。
如:mschart.Series.Add("1");
mschart.Series.Add("2");
mschart.ChartAreas.Add("11");
使用前必須
mschart.Series.Clear();
mschart.ChartAreas.Clear();
參考二:
//1.設置Chart1的相關屬性
//給X,Y軸賦標題(注釋)
Chart1.ChartAreas["ChartArea1"].AxisX.Title = "時間";
Chart1.ChartAreas["ChartArea1"].AxisY.Title = "數值";
//設置表數據對象屬於哪個繪圖區--默認series都屬於第一個繪圖區
Chart1.Series["Series3"].ChartArea = "ChartArea2";
//指點圖標元素的漸變樣式--中心向外,從左到右,從上到下
Chart1.BackGradientStyle = System.Web.UI.DataVisualization.Charting.GradientStyle.TopBottom;
//設置圖表的背景顏色
Chart1.BackColor = System.Drawing.Color.Gray;
//設置背景的輔助顏色
Chart1.BackSecondaryColor = Color.White;
//設置圖像邊框顏色
Chart1.BorderlineColor = Color.Green; ;
//設置圖像邊框線的樣式--實線,虛線,點線
Chart1.BorderlineDashStyle = System.Web.UI.DataVisualization.Charting.ChartDashStyle.Solid;
//設置邊框線的寬度
Chart1.BorderlineWidth = 2;
//設置圖片的邊框的外觀樣式
Chart1.BorderSkin.SkinStyle = System.Web.UI.DataVisualization.Charting.BorderSkinStyle.Emboss;
//2.數據樣式
//設置顏色
Chart1.Series["Series1"].Color = System.Drawing.Color.Red;
//設置圖表的類型
Chart1.Series["Series1"].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line;
//設置是否在chart中顯示坐標點值
Chart1.Series["Series1"].IsValueShownAsLabel = true;
//設置數據邊框顏色
Chart1.Series["Series1"].BorderColor = System.Drawing.Color.Yellow;
//設置數據的顏色
Chart1.Series["Series1"].Color = System.Drawing.Color.Blue;
//設置數據的名稱
Chart1.Series["Series1"].Name = "數據1";
//設置陰影偏移量
Chart1.Series["Series1"].ShadowOffset = 1;
//設置陰影顏色
Chart1.Series["Series1"].ShadowColor = System.Drawing.Color.PaleGreen;
//3.設置圖表區域樣式--Position/InnerPlotPosition
//圖表區域名字
Chart1.ChartAreas["ChartArea1"].Name = "圖表區域";
//設置是否自動設置合適的圖表元素
Chart1.ChartAreas["ChartArea1"].Position.Auto = true;
//設置圖表的陰影顏色
Chart1.ChartAreas["ChartArea1"].ShadowColor = System.Drawing.Color.YellowGreen;
//設置圖表元素左上角對應的X、Y坐標
Chart1.ChartAreas["ChartArea1"].Position.X = 5.089137f;
Chart1.ChartAreas["ChartArea1"].Position.Y = 5.89765f;
//設置圖表遠的高、寬
Chart1.ChartAreas["ChartArea1"].Position.Height = 86.760f;
Chart1.ChartAreas["ChartArea1"].Position.Width = 88f;
//設置是否在內部繪圖區域中自動設置合適的圖表元素
Chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Auto = false;
//設置圖表元素內部繪圖區域的高、寬
Chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Height = 85f;
Chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Width = 96f;
//設置圖表元素內部繪圖區域左上角對應的X,Y坐標
Chart1.ChartAreas["ChartArea1"].InnerPlotPosition.X = 8.693f;
Chart1.ChartAreas["ChartArea1"].InnerPlotPosition.Y = 5.6257f;
//3.設置圖表區域樣式--3D效果
//設置是否顯示3D效果
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
//設置三維圖表的旋轉角度
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Inclination = 10;
//設置條形圖或柱狀圖的數據系列是否為簇狀
Chart1.ChartAreas["ChartArea1"].Area3DStyle.IsClustered = true;
//設置圖表區域是否使用等角投影顯示
Chart1.ChartAreas["ChartArea1"].Area3DStyle.IsRightAngleAxes = true;
//設置圖表的照明類型--(色調隨旋轉角度改變而改變,不應用照明,色調補改變)
Chart1.ChartAreas["ChartArea1"].Area3DStyle.LightStyle =
System.Web.UI.DataVisualization.Charting.LightStyle.Realistic;
//設置三維圖區的透視百分比
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Perspective = 60;
//設置三維圖表區域繞垂直軸旋轉的角度
Chart1.ChartAreas["ChartArea1"].Area3DStyle.Rotation = 60;
//設置三位圖區顯示的牆的寬度
Chart1.ChartAreas["ChartArea1"].Area3DStyle.WallWidth = 2;
//3.設置圖表區域樣式--坐標
//設置X軸是否自動調整軸標簽
Chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
//設置X軸下方提示信息的字體屬性
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Font =
new System.Drawing.Font("微軟雅黑", 8.25f, System.Drawing.FontStyle.Bold);
//設置標簽文本中格式字符串
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format="";
//設置標簽間隔大小
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Interval=5d;
//設置間隔大小的衡量單位(有:自動,年,月,日,天,星期,時,分,秒,數字)
Chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IntervalType=
System.Web.UI.DataVisualization.Charting.DateTimeIntervalType.Number;
//設置X軸的線條顏色
Chart1.ChartAreas["ChartArea1"].AxisX.LineColor=System.Drawing.Color.Violet;
//設置主網格和次網格的間隔
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.Interval=50d;
//設置主網格和次網格間隔的衡量單位
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.IntervalType=
System.Web.UI.DataVisualization.Charting.DateTimeIntervalType.Number;
//設置主網格的顏色
Chart1.ChartAreas["ChartArea1"].AxisX.MajorGrid.LineColor=Color.Salmon;
//設置刻度線的間隔
Chart1.ChartAreas["ChartArea1"].AxisX.MajorTickMark.Interval=7d;
//設置刻度線間隔的衡量單位
Chart1.ChartAreas["ChartArea1"].AxisX.MajorTickMark.IntervalType=
System.Web.UI.DataVisualization.Charting.DateTimeIntervalType.Number;
//設置X軸是否自動調整軸標簽
Chart1.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
//設置是否自動將數據值均為正軸的最小值設置為0,存在負數值時,將使用數據軸最小值
Chart1.ChartAreas["ChartArea1"].AxisX.IsStartedFromZero=false;
//Y軸同X軸,同上
//設置Y軸最大、最小值
Chart1.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
Chart1.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
//4.圖例樣式
//圖例對齊方式--中間對齊,靠近邊緣對齊,遠離遠點對齊
Chart1.Legends["Legend1"].Alignment = System.Drawing.StringAlignment.Near;
//圖例背景顏色
Chart1.Legends["Legend1"].BackColor=Color.Silver;
//設置圖例要停靠在哪個區域上
Chart1.Legends["Legend1"].DockedToChartArea="ChartArea1";
//設置圖例停靠在圖表區域的位置(頂部,底部,左側,右側)
Chart1.Legends["Legend1"].Docking=System.Web.UI.DataVisualization.Charting.Docking.Bottom;
//設置圖例字體屬性
Chart1.Legends["Legend1"].Font=new System.Drawing.Font("換文行楷",6.6f);
//設置圖例文本是否可以自動調整大小
Chart1.Legends["Legend1"].IsTextAutoFit = false;
//設置顯示圖例項方式--列,行,表格
Chart1.Legends["Legend1"].LegendStyle = System.Web.UI.DataVisualization.Charting.LegendStyle.Column;