您的位置:首页 > 其它

WPF Chart AreaSeries绘制阴影

2017-04-17 18:02 357 查看
WPF项目,Chart用的是 System.Windows.Controls.DataVisualization.Toolkit

自己公司的系统,之前有一版是用Winform做的,现在应技术要求做一版WPF的

Winform Chart绘制阴影可参考 http://blog.csdn.net/xiaoyiyz/article/details/48023205 

首先看看WPF做出来的效果 





(注意)上图中红色抛物线,没找到怎么设置成平滑的曲线,这样看起来更好看些,有人知道怎么设置请留言,我发现后也会及时更新的

用习惯了WinForm,WPF还真不习惯,设置样式的时候需要给Property设置Style,感觉真别扭,而且Property太多了,一个一个试 很无语

还有就是不喜欢在Xaml里面直接写样式,习惯了在后台写,这点也遇到很多困难,废话不多说,下面上代码 (还不太完善,注释不完整)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Media;
using System.Windows.Shapes;

namespace HAPrj.Common
{
/// <summary>
/// Chart图表工具类
/// Author:ZZH
/// Data:2017-04-15
/// </summary>
public class ChartTool
{
Chart _Chart;
public ChartTool(Chart chart)
{
this._Chart = chart;
}

public void SetX(string title, double min, double max, double interval, bool showLines, AxisLocation rl)
{
LinearAxis axis = new LinearAxis();
axis.Orientation = AxisOrientation.X;
axis.Location = rl;
axis.Title = title;
axis.Minimum = min;
axis.Maximum = max;
axis.Interval = interval;
axis.ShowGridLines = showLines;
_Chart.Axes.Add(axis);
}
public void SetY(string title, double min, double max, double interval, bool showLines, AxisLocation rl)
{
LinearAxis axis = new LinearAxis();
axis.Orientation = AxisOrientation.Y;
axis.Location = rl;
axis.Title = title;
axis.Minimum = min;
axis.Maximum = max;
axis.Interval = interval;
axis.ShowGridLines = showLines;
_Chart.Axes.Add(axis);
}
public void SetAreaSeries(string name, string title, Color scolor, bool isShowLegend, double[] xPoints, double[] yPoints)
{
AreaSeries series = new AreaSeries();
if(!string.IsNullOrEmpty(name))
series.Name = name;
if (!string.IsNullOrEmpty(title))
series.Title = title;

///////////////////////////尝试以下方法失败,花费了一天的时间(WPF真操蛋)20170415////////////////////
//System.Collections.ObjectModel.Collection<ResourceDictionary> c = this._Chart.Palette;

//ResourceDictionaryCollection rdc = new ResourceDictionaryCollection();
//ResourceDictionary rd = new ResourceDictionary();
//Style sy = new Style();
////DependencyProperty dpc = DependencyProperty.RegisterAttached("Backgroud", typeof(Brush),typeof(Panel));

//Setter st = new Setter();
////st.TargetName = "DataPointStyle";
//st.TargetName = name;
//st.Property = DataPointSeries.BackgroundProperty;
//st.Value = new SolidColorBrush(Colors.Red);
//sy.Setters.Add(st);
//rd.Add("Backgroud", sy);
////st.Property = DependencyProperty.Register("Background", null,null);
////st.Value =
////rd.Add("Background", "Red");
////rdc.Add(rd);
//_Chart.Style = sy;//.Palette.Add(rd);

//_Chart = new SolidColorBrush[] { new SolidColorBrush(Colors.Red), new SolidColorBrush(Colors.Yellow), new SolidColorBrush(Colors.Orange)};
//series.Foreground = new SolidColorBrush(Colors.Red);
//Style sy = new Style();
//Setter st = new Setter();
////st.TargetName = "DataPointStyle";
//st.Property = DataPointSeries.BackgroundProperty;
//st.Value = new SolidColorBrush(Colors.Red);
//sy.Setters.Add(st);
//_Chart.Style = sy;//.Palette.Add(rd);
////_Chart.Style.TargetType = typeof(Control);
//_Chart.Style.RegisterName("DataPointStyle", series);
///////////////////////////////////////////////////////////////////////////////////

series.IndependentValuePath = "Key";
series.DependentValuePath = "Value";
//绑定数据
var kvPair = new KeyValuePair<double, double>[xPoints.Count()];
for (int i = 0; i < xPoints.Count(); i++)
{
var kvp = new KeyValuePair<double, double>(xPoints[i], yPoints[i]);
kvPair[i] = kvp;
}
series.ItemsSource = kvPair;
_Chart.Series.Add(series);
//设置背景颜色
Style style = new Style(typeof(DataPoint));
style.Setters.Add(new Setter(AreaDataPoint.BackgroundProperty, new SolidColorBrush(scolor)));
series.DataPointStyle = style;
//去掉Legend
if(!isShowLegend)
series.LegendItems.Clear();

}

public void SetLineSeries(string name, string title, Color scolor, bool isShowLegend, bool isShowLinePoint, double[] xPoints, double[] yPoints)
{
LineSeries series = new LineSeries();
if (!string.IsNullOrEmpty(name))
series.Name = name;
if (!string.IsNullOrEmpty(title))
series.Title = title;

series.IndependentValuePath = "Key";
series.DependentValuePath = "Value";
//series.AnimationSequence = AnimationSequence.FirstToLast;
//series.IsSelectionEnabled = true;
//绑定数据
var kvPair = new KeyValuePair<double, double>[xPoints.Count()];
for (int i = 0; i < xPoints.Count(); i++)
{
var kvp = new KeyValuePair<double, double>(xPoints[i], yPoints[i]);
kvPair[i] = kvp;
}
series.ItemsSource = kvPair;
_Chart.Series.Add(series);

//设置背景颜色
Style style = new Style(typeof(DataPoint));
if (!isShowLinePoint)
{
style.Setters.Add(new Setter(LineDataPoint.WidthProperty, 0.0));
style.Setters.Add(new Setter(LineDataPoint.HeightProperty, 0.0));
}
style.Setters.Add(new Setter(LineDataPoint.BackgroundProperty, new SolidColorBrush(scolor)));
series.DataPointStyle = style;

//Style style2 = new Style(typeof(Polyline));
//Polyline.StrokeThicknessProperty 线的宽度
//Polyline.StrokeDashArrayProperty 可画虚线,value是数组
//style2.Setters.Add(new Setter(Polyline.StrokeMiterLimitProperty, 0.0));
//series.PolylineStyle = style2;

//去掉Legend
//可尝试用style去掉Legend,看Chart的宽高是否变化
if (!isShowLegend)
series.LegendItems.Clear();

}

}

}


有需求可联系我WX:xiaoyiyz 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息