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);
}
}
silverlight visifire控件图表制作——silverlight 后台方法画图