標籤:style color 使用 width 資料 os
1、調用wcf 擷取資訊
private void svc_GetSingleChartDataCompleted(object sender, GetSingleChartDataCompletedEventArgs e)
{
ObservableCollection<ListItem> lBaseOilBar = e.Result;
CreateChart(this.gridVisifire, lBaseOilBar);
}
2、構造圖表 ,根據解析xml獲得的ListItem 畫圖
public void CreateChart(Grid oGrid, ObservableCollection<ListItem> lBaseOilBar)
{
foreach (ListItem li in lBaseOilBar)
{
#region 圖表大小,框線,位置
Chart chart = new MyCharts();
chart.Width = 800;
chart.Height = 600;
chart.ScrollingEnabled = false;
//chart.Margin = new Thickness(50, 50, 10, 20);
#endregion
#region 設定 chart 的PlotArea陰影
PlotArea plot = new PlotArea();
plot.ShadowEnabled = false; //陰影
chart.PlotArea = plot;
#endregion
#region 圖表標題
Title title = new Title();
string sTitle = li.Title;
title.Text = sTitle;
title.FontSize = 16;
chart.Titles.Add(title);
#endregion
#region X 座標軸
Axis axisX = new Axis();
AxisLabels xal = new AxisLabels
{
FontSize = 14//設定文字大小
};
axisX.AxisLabels = xal;
//axisX.AxisMaximum = 52;//X軸最大值
//axisX.AxisMinimum = 1;//X軸最小值
//axisX.Interval = 2;//X軸座標間距值
//axisX.IntervalType = IntervalTypes.Number;
chart.AxesX.Add(axisX);
#endregion
#region Y 座標軸
Axis axisY = new Axis();
AxisLabels yal = new AxisLabels
{
FontSize = 14//設定文字大小
};
axisY.AxisLabels = yal;
if (li.AxisYMinimum != "")
{
axisY.AxisMinimum = Convert.ToDouble(li.AxisYMinimum);//Y軸最小值
}
if (li.AxisYMaximum != "")
{
axisY.AxisMaximum = Convert.ToDouble(li.AxisYMaximum);//Y軸最大值
}
//Y軸資料單位
string sYUint = li.YUint;
axisY.Title = sYUint;
axisY.TitleFontSize = 15;//字型大小
// axisY.StartFromZero = false;//座標是否從0開始
//axisY.AxisMinimum = 10; //y軸刻度最小值
//axisY.AxisMaximum = 100; //y軸刻度最大值
chart.AxesY.Add(axisY);
#endregion
#region 雙座標軸
string IsTwoY = "";
IsTwoY = li.TWOY;
if (IsTwoY == "true")
{
Axis axisYT = new Axis() { AxisType = AxisTypes.Secondary };
AxisLabels yalT = new AxisLabels
{
FontSize = 14//設定文字大小
};
axisYT.AxisLabels = yalT;
axisYT.ValueFormatString = "##";//座標軸資料格式
if (li.AxisYTMinimum != "")
{
axisYT.AxisMinimum = Convert.ToDouble(li.AxisYTMinimum);//Y軸最小值
}
if (li.AxisYTMaximum != "")
{
axisYT.AxisMaximum = Convert.ToDouble(li.AxisYTMaximum);//Y軸最大值
}
//Y軸資料單位
string sYUintT = li.YUintT;
axisYT.Title = sYUintT;
axisYT.TitleFontSize = 15;//設定文字大小
chart.AxesY.Add(axisYT);
}
#endregion
//標題事件
title.MouseLeftButtonDown += new MouseButtonEventHandler(title_MouseLeftButtonDown);
#region 設定表徵圖字型大小
Legend legend = new Legend();
legend.FontSize = 13;//設定文字大小
chart.Legends.Add(legend);
#endregion
//X軸資料
string[] sXLabel = li.XAXIS.ToArray();
//設定圖的顏色
ColorSet cs = new ColorSet();
cs.Id = "colorset1";
int iMax = 0;
for (int j = 0; j < li.YAXISs.Count(); j++)
{
YAXIS oYAXIS = (YAXIS)li.YAXISs[j];
string sYTitle = "";
sYTitle = oYAXIS.Name;
double[] dYValue = null;
dYValue = oYAXIS.YValue.ToArray();
double[] dYValueBengin = null;
DataSeries dataSeries = new DataSeries();
#region 滑鼠顯示資料樣式設定 傾斜角度、是否顯示、座標點資料樣式、字型大小、原油年產量圖特殊處理、 資料內嵌外嵌
dataSeries.LegendText = sYTitle;
string[] dXLabValue = null;
if (oYAXIS.XLabValue != null)
{
dXLabValue = oYAXIS.XLabValue.ToArray();
dataSeries.ToolTipText = "#AxisXLabel,#YValue";//滑鼠顯示資料格式
}
//設定座標點資料扭曲角度(-90至90)
string LabelAngle = "";
LabelAngle = oYAXIS.LabelAngle;
if (oYAXIS.LabelAngle != "")
{
dataSeries.LabelAngle = Convert.ToDouble(LabelAngle);
}
//是否顯示座標點資料 //(true ,false)
dataSeries.LabelEnabled = isShowData;
//滑鼠顯示資料座標點資料樣式 ("##","#,#",".")
dataSeries.YValueFormatString = "##";
//設定表徵圖(滑鼠顯示資料)字型大小
dataSeries.LabelFontSize = 12;
if (sXLabel.Length > 20)//圖特殊處理
{
axisX.AxisLabels.FontSize = 11;//設定文字大小
axisX.AxisLabels.Angle = -90;
axisX.Interval = 1;
dataSeries.LabelFontSize = 11;//設定表徵圖(滑鼠顯示資料)字型大小
}
#region 資料內嵌外嵌
string LabelStyle = "";
LabelStyle = oYAXIS.LabelStyle;
switch (LabelStyle)
{
case "Inside":
dataSeries.LabelStyle = LabelStyles.Inside;//資料內嵌
break;
case "Outside":
dataSeries.LabelStyle = LabelStyles.OutSide;//資料外嵌
break;
case "Defaultside":
break;
}
#endregion
#endregion
#region 座標軸是否是雙座標
if (oYAXIS.AxisYType == "Secondary")
{
dataSeries.AxisYType = AxisTypes.Secondary;
}
else
{
dataSeries.AxisYType = AxisTypes.Primary;
}
#endregion
#region 設定柱狀圖的顏色
string strColor = oYAXIS.Color;
switch (strColor)
{
case "Red":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x45, 0x00))); //#FFFF4500 orangeRed
break;
case "Yellow":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xda, 0xa5, 0x20)));//FFDAA520 Goldenrod
break;
case "Orange":
cs.Brushes.Add(new SolidColorBrush(Colors.Orange));
break;
case "Green":
cs.Brushes.Add(new SolidColorBrush(Colors.Green));
break;
case "Blue":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x41, 0x69, 0xe1))); //FF4169E1 RoyalBlue
break;
case "White":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0xff, 0xff))); //FFFFFFFF RoyalBlue Color.FromArgb(0xff, 0xff, 0xff, 0xff) 透明色;00ff00ff
break;
case "ComflowerBlue":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x64, 0x95, 0xed))); //FF6495ED ComflowerBlue
break;
case "IndianRed":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xcd, 0x5c, 0x5c))); //FFCD5C5C IndianRed
break;
//case "SlateBlue":
// cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x6a, 0x5a, 0xcd))); //
// break;
case "PaleGreen":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x98, 0xfb, 0x98))); ; //
break;
case "YellowGreen":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x9a, 0xcd, 0x32))); //
break;
case "OrangeRed":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x00, 0x00))); //#FFFF0000 Red
break;
case "DarkGreen":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x00, 0x80, 0x00)));//Green
break;
default:
break;
}
#endregion
#region 圖表類型(柱狀,餅狀……)
string sChartType = li.ChartType;
RenderAs oRenderAs = new RenderAs();
switch (sChartType)
{
case "Column":
oRenderAs = RenderAs.Column;
#region 柱子寬度自適應
//if (li.DataPointWidth != "")
//{
// chart.DataPointWidth = Convert.ToDouble(li.DataPointWidth);//設定固定寬度
//}
if (sXLabel.Length < 10)
{
chart.DataPointWidth = 6;
}
else
{
chart.DataPointWidth = chart.Width * 0.08 / sXLabel.Length;
}
#endregion
break;
case "Pie":
oRenderAs = RenderAs.Pie;
dataSeries.LabelFontSize = 14;//設定表徵圖字型大小
dataSeries.LabelEnabled = true; //顯示座標點資料
break;
case "Line":
oRenderAs = RenderAs.Line;
break;
case "Area":
oRenderAs = RenderAs.Area;
if (j > 0)
{
YAXIS oYAXISBengin = (YAXIS)li.YAXISs[0];
dYValueBengin = oYAXISBengin.YValue.ToArray();
}
break;
}
dataSeries.RenderAs = oRenderAs;
#endregion
#region 構造資料 Y軸座標最大值設定
DataPoint dp;
if (sXLabel != null)
{
//最大值
int Max = 0;
Max = (int)dYValue[0];
for (int i = 0; i < sXLabel.Length; i++)
{
//最大值
Max = Max > (int)dYValue[i] ? Max : (int)dYValue[i];
dp = new DataPoint();
dp.AxisXLabel = sXLabel[i];
dp.YValue = dYValue[i];
dataSeries.DataPoints.Add(dp);
}
#region 最大值
if (j == 0)
{
iMax = Max;
}
else
{
iMax = iMax > Max ? iMax : Max;
}
#endregion
}
#endregion
chart.Series.Add(dataSeries);
}
#region 最大值 設定Y軸表徵圖 解決滑鼠顯示資料部全問題
Double YAxisMaximum = Math.Round((iMax / 0.85), 0);
int len = YAxisMaximum.ToString().Length;
string lastS = "";
if (len > 2)
{
for (int m = 0; m < len - 2; m++)
{
lastS += "0";
}
YAxisMaximum = Convert.ToInt32((Convert.ToInt32(YAxisMaximum.ToString().Substring(0, 2)) + 1).ToString() + lastS);
axisY.AxisMaximum = YAxisMaximum;
}
#endregion
#region 設定 Chart 使用自訂的顏色集合 colorset1
if (cs.Brushes.Count > 0)
{
chart.ColorSets.Clear();
chart.ColorSets.Add(cs);
chart.ColorSet = "colorset1";
}
# endregion
oGrid.Children.Add(chart);
}
}