public void CreateChart(Grid oGrid,
ObservableCollection<ListItem>
lBaseOilBar)
{
foreach
(ListItem li in
//圖表大小,框線
Chart chart = new
MyCharts();
chart.Width =
800;
chart.Height =
600;
chart.ScrollingEnabled =
false;
//圖表标題
Title title = new
Title();
string sTitle =
li.Title;
title.Text =
sTitle;
title.FontSize =
16;
chart.Titles.Add(title);
// X
坐标軸
Axis axisX = new
Axis();
AxisLabels xal = new
AxisLabels
FontSize =
14//設定文字大小
};
axisX.AxisLabels =
xal;
chart.AxesX.Add(axisX);
// Y
Axis axisY = new
AxisLabels yal = new
axisY.AxisLabels =
yal;
//Y軸資料機關
string sYUint =
li.YUint;
axisY.Title =
sYUint;
axisY.TitleFontSize =
15;
chart.AxesY.Add(axisY);
if (li.ChartType == "Column")
//柱狀圖
switch
(li.YAXISs.Count())
case
1:
chart.DataPointWidth =
4;//設定柱子寬度
break;
2:
2.5;
3:
1.8;
}
title.MouseLeftButtonDown += new
MouseButtonEventHandler(title_MouseLeftButtonDown);
////設定圖示字型大小
//Legend legend = new
Legend();
//legend.FontSize =
13;
//chart.Legends.Add(legend);1
//X軸資料
string[] sXLabel =
li.XAXIS.ToArray();
string sTableName =
"";
#region
點選是否觸發事件,鑽取資料圖表指派一個實體表名
bool ifGetSig =
(li.ChartName)
"Stock":
sTableName =
"StockSingleOildom";
ifGetSig =
true;
"Drill_JC":
"Drill_JCSingleOildom";
"EXTRACTIONOIL":
"EXTRACTIONOILSingleOildom";
"Drill_MACHINE":
"Drill_MACHINESingleOildom";
"PROVEDRILL":
"PROVEDRILLSingleOildom";
#endregion
圖表柱狀個數,循環加載資料
ColorSet cs = new
ColorSet();
cs.Id = "colorset1"; // 設定ColorSet 的 Id 為
colorset1
//foreach (YAXIS oYAXIS in
li.YAXISs)
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();
DataSeries dataSeries = new
DataSeries();
dataSeries.LegendText =
sYTitle;
#region 雙坐标軸
string IsTwoY = "";
IsTwoY = oYAXIS.TWOY;
if (IsTwoY == "true")
Axis axisYT = new Axis() { AxisType = AxisTypes.Secondary };
AxisLabels yalT = new AxisLabels
//Enabled = true, //設定是否顯示坐标軸上的文本,預設值為true
//Angle = 45,//設定文本顯示的角度,取值為 –90 至 90
FontSize = 14//設定文字大小
axisYT.AxisLabels = yalT;
axisYT.AxisMinimum = 0;
axisYT.AxisMaximum = 100;
string sYUintT = li.YUintT;
axisYT.Title = sYUintT;
axisYT.TitleFontSize = 15;
chart.AxesY.Add(axisYT);
dataSeries.AxisYType = AxisTypes.Secondary;
else
dataSeries.AxisYType = AxisTypes.Primary;
#region 設定柱狀圖的顔色
待開發
//ColorSet cs = new
//string strColor =
oYAXIS.Color;
//////Colors oColors = new
Colors();
//cs.Id = "colorset1"; // 設定ColorSet 的 Id 為
//cs.Brushes.Add(new
SolidColorBrush(Colors.Green));
SolidColorBrush(Colors.Red));
SolidColorBrush(Colors.Blue));
////cs.Brushes.Add(new
SolidColorBrush(Colors.Yellow));
SolidColorBrush(Colors.Orange));
//chart.ColorSets.Add(cs);
//chart.ColorSet = "colorset1"; // 設定 Chart 使用自定義的顔色集合
string strColor =
(strColor)
"Red":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x45, 0x00)));
//#FFFF4500
orangeRed
"Yellow":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0xda, 0xa5,
0x20)));//FFDAA520
Goldenrod
"Orange":
cs.Brushes.Add(new
"Green":
"Blue":
cs.Brushes.Add(new SolidColorBrush(Color.FromArgb(0xff, 0x41, 0x69, 0xe1)));
//FF4169E1
RoyalBlue
default:
圖表類型(柱狀,餅狀……)
string sChartType =
li.ChartType;
RenderAs oRenderAs = new
RenderAs();
(sChartType)
"Column":
oRenderAs =
RenderAs.Column;
"Pie":
RenderAs.Pie;
//dataSeries.LabelFontSize =
14;//設定圖示字型大小
"Line":
RenderAs.Line;
dataSeries.RenderAs =
oRenderAs;
構造資料
DataPoint
dp;
if (sXLabel !=
null)
for (int i = 0; i < sXLabel.Length;
i++)
dp = new
DataPoint();
dp.AxisXLabel =
sXLabel[i];
dp.YValue =
dYValue[i];
if
(ifGetSig)
dp.Tag =
sTableName;
//dp.MouseLeftButtonUp += new
MouseButtonEventHandler(dataPoint_MouseLeftButtonUp);
//餅狀圖去除值為0的oildom
if ((dataSeries.RenderAs == RenderAs.Pie || dataSeries.RenderAs ==
RenderAs.Line) && dp.YValue ==
0)
dataSeries.DataPoints.Add(dp);
chart.Series.Add(dataSeries);
// 設定 Chart 使用自定義的顔色集合
if (cs.Brushes.Count >
chart.ColorSets.Clear();
chart.ColorSets.Add(cs);
chart.ColorSet =
"colorset1";
oGrid.Children.Add(chart);
}