WPF 自定义雷达图开发实例教程

自定义雷达图表如下:

WPF 自定义雷达图开发实例教程_第1张图片

1、创建UserControl,名为“RadarChartControl”

前台:


 
 

后台:

/// 
 /// RadarChartControl.xaml 的交互逻辑
 /// 
 public partial class RadarChartControl : UserControl
 {
 public RadarChartControl()
 {
  InitializeComponent();
 }
 #region 属性
 /// 
 /// 尺寸大小
 /// 高宽大小一样
 /// 
 public double Size
 {
  get { return (double)GetValue(SizeProperty); }
  set { SetValue(SizeProperty, value); }
 }
 public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size", typeof(double),
 typeof(RadarChartControl), new PropertyMetadata(400.0));
 /// 
 /// 标题
 /// 
 public List Arguments
 {
  get { return (List)GetValue(ArgumentsProperty); }
  set { SetValue(ArgumentsProperty, value); }
 }
 public static readonly DependencyProperty ArgumentsProperty = DependencyProperty.Register("Arguments", typeof(List),
 typeof(RadarChartControl), new PropertyMetadata(new List()));
 /// 
 /// 数据
 /// 
 public List Datas
 {
  get { return (List)GetValue(DatasProperty); }
  set { SetValue(DatasProperty, value); }
 }
 public static readonly DependencyProperty DatasProperty = DependencyProperty.Register("Datas", typeof(List),
 typeof(RadarChartControl), new PropertyMetadata(new List()));
 /// 
 /// 获取或设置线条颜色
 /// 
 public Brush BorderBrush
 {
  get { return (Brush)GetValue(BorderBrushProperty); }
  set { SetValue(BorderBrushProperty, value); }
 }
 public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.Register("BorderBrush", typeof(Brush),
 typeof(RadarChartControl), new PropertyMetadata(Brushes.RoyalBlue));
 /// 
 /// 连接点大小
 /// 
 public int EllipseSize = 7;
 /// 
 /// 控件大小
 /// 
 public double TotalSize
 {
  get
  {
  double size = Size + 200;
  return size;
  }
 }
 /// 
 /// 面板
 /// 
 public Canvas ChartCanvas = new Canvas();
 //声明和注册路由事件
 public static readonly RoutedEvent TitleClickRoutedEvent =
 EventManager.RegisterRoutedEvent("TitleClick", RoutingStrategy.Bubble, typeof(EventHandler), typeof(RadarChartControl));
 //CLR事件包装
 public event RoutedEventHandler TitleClick
 {
  add { this.AddHandler(TitleClickRoutedEvent, value); }
  remove { this.RemoveHandler(TitleClickRoutedEvent, value); }
 }
 //激发路由事件,借用Click事件的激发方法
 protected void OnClick(object sender, RoutedEventArgs e)
 {
  RoutedEventArgs args = new RoutedEventArgs(TitleClickRoutedEvent, e);
  this.RaiseEvent(args);
 }
 #endregion
 private void RadarChartControl_OnLoaded(object sender, RoutedEventArgs e)
 {
  if (!CheckData())
  {
  throw new Exception("RadarChart的数据之间不匹配!请重新配置!");
  }
  //获取最大数值
  int maxData = Datas.Max(i => i.DataList.Max(o => o.Data));
  //设置面板和背景
  SetCanvasAndBackground(maxData);
  //设置数据标题
  SetDataTitle(Datas);
  //获取半圈大小
  double length = Size / 2 / maxData;
  //连接点半径
  int ellipseR = EllipseSize / 2;
  foreach (var chartItem in Datas)
  {
  var color = chartItem.Color;
  //俩个多边形,一个设置背景,一个设置边框
  Polygon polygonArea = new Polygon() { Fill = color, Opacity = 0.2, StrokeThickness = 0 };
  Polygon polygonBorder = new Polygon() { Fill = Brushes.Transparent, Stroke = color, StrokeThickness = 0.8 };
  int index = 0;
  foreach (var data in chartItem.DataList)
  {
   double currentAngle = Angle * index + 90;
   double angle = (currentAngle / 360) * 2 * Math.PI;
   var r = data.Data * length;
   double x = Size / 2 + r * Math.Cos(angle);
   double y = Size / 2 - r * Math.Sin(angle);
   //多边形添加节点
   var point = new Point()
   {
   X = x,
   Y = y
   };
   polygonArea.Points.Add(point);
   polygonBorder.Points.Add(point);
   //设置节点Style
   var ellipse = new Ellipse() { Width = EllipseSize, Height = EllipseSize, Fill = color };
   Canvas.SetLeft(ellipse, x - ellipseR);
   Canvas.SetTop(ellipse, y - ellipseR);
   ChartCanvas.Children.Add(ellipse);
   index++;
  }
  ChartCanvas.Children.Add(polygonArea);
  ChartCanvas.Children.Add(polygonBorder);
  }
  //设置标题
  SetArguments();
 }
 /// 
 /// 设置数据标题
 /// 
 /// 
 private void SetDataTitle(List datas)
 {
  RadarChartTitleList titleList = new RadarChartTitleList();
  titleList.ItemSoure = datas;
  double angle = Math.PI * 0.25;
  double x = TotalSize / 2 + (TotalSize / 2) * Math.Sin(angle);
  Canvas.SetLeft(titleList, x);
  Canvas.SetTop(titleList, x);
  CanvasPanel.Children.Add(titleList);
 }
 /// 
 /// 设置标题
 /// 
 private void SetArguments()
 {
  int index = 0;
  foreach (var argument in Arguments)
  {
  var button = new ChartButton();
  button.Content = argument.Name;
  button.Icon = argument.IconSource;
  button.MyButton.Click += OnClick;
  //绘制XY
  double currentAngle = Angle * index + 90;
  double angle = (currentAngle / 360) * 2 * Math.PI;
  var r = TotalSize / 2;
  double x = r + r * Math.Cos(angle) - (button.Width / 2);
  double y = r - r * Math.Sin(angle) - (button.Height / 2);
  //添加按钮高度差异
  y = y + Math.Sin(angle) * (button.Width / 2 - button.Height / 2);
  Canvas.SetLeft(button, x);
  Canvas.SetTop(button, y);
  CanvasPanel.Children.Add(button);
  index++;
  }
 }
 /// 
 /// 检查数据
 /// 
 /// 
 private bool CheckData()
 {
  if (Datas == null)
  {
  return false;
  }
  foreach (var data in Datas)
  {
  bool result = !Datas.Any(i => i.DataList.Count != data.DataList.Count);
  if (!result)
  {
   return false;
  }
  }
  return true;
 }
 /// 
 /// 设置面板和背景
 /// 
 /// 
 private void SetCanvasAndBackground(int maxIndex)
 {
  CanvasPanel.Height = TotalSize;
  CanvasPanel.Width = TotalSize;
  //面板
  ChartCanvas.Height = Size;
  ChartCanvas.Width = Size;
  double canvasX = (TotalSize - Size) / 2;
  Canvas.SetLeft(ChartCanvas, canvasX);
  Canvas.SetTop(ChartCanvas, canvasX);
  CanvasPanel.Children.Add(ChartCanvas);
  //画圈和直线
  var color = BorderBrush;
  double length = Size / 2 / maxIndex;
  for (int i = 0; i < maxIndex; i++)
  {
  double height = length * 2 * (i + 1);
  double left = Size / 2 - length * (i + 1);
  var ellipse = new Ellipse() { Stroke = color, StrokeThickness = 0.5, Height = height, Width = height };
  Canvas.SetLeft(ellipse, left);
  Canvas.SetTop(ellipse, left);
  ChartCanvas.Children.Add(ellipse);
  }
  //暂时设定:4个标题时,画线
  if (Arguments.Count == 4)
  {
  //竖向直线
  Path verticalPath = new Path()
  {
   Stroke = color,
   StrokeThickness = 0.2,
  };
  //添加数据
  StreamGeometry geometry = new StreamGeometry();
  geometry.FillRule = FillRule.Nonzero; //声前F0还是F1,现在是F1
  using (StreamGeometryContext ctx = geometry.Open())
  {
   ctx.BeginFigure(new Point(Size / 2, 0), true, true);
   ctx.LineTo(new Point(Size / 2, Size), true, false);
  }
  geometry.Freeze();
  verticalPath.Data = geometry;
  ChartCanvas.Children.Add(verticalPath);
  //横向直线
  Path horizontalPath = new Path()
  {
   Stroke = color,
   StrokeThickness = 0.2,
  };
  //添加数据
  geometry = new StreamGeometry();
  geometry.FillRule = FillRule.Nonzero; //声前F0还是F1,现在是F1
  using (StreamGeometryContext ctx = geometry.Open())
  {
   ctx.BeginFigure(new Point(0, Size / 2), true, true);
   ctx.LineTo(new Point(Size, Size / 2), true, false);
  }
  geometry.Freeze();
  horizontalPath.Data = geometry;
  ChartCanvas.Children.Add(horizontalPath);
  }
 }
 /// 
 /// 分隔角度
 /// 
 private double Angle
 {
  get
  {
  int count = Arguments.Count;
  double angle = 360 / count;
  return angle;
  }
 }
 }
 /// 
 /// 类标题
 /// 
 public class ArgumentModel
 {
 public ImageSource IconSource { get; set; }
 public string Name { get; set; }
 }
 /// 
 /// 单组数据
 /// 
 public class ChartItem
 {
 public Brush Color { get; set; }
 List dataList = new List();
 public List DataList
 {
  get { return dataList; }
  set { dataList = value; }
 }
 public object Name { get; set; }
 }
 /// 
 /// 数据
 /// 
 public class ChartData
 {
 public string Name { get; set; }
 public int Data { get; set; }
 }

2、创建标题类按钮控件,定义名称为ChartButton

前台:


 
 
 
 
 
 

后台:

/// 
 /// ChartButton.xaml 的交互逻辑
 /// 
 public partial class ChartButton : UserControl
 {
 public ChartButton()
 {
  InitializeComponent();
 }
 #region 属性
 /// 
 /// 工具提示
 /// 
 public string ToolTip
 {
  get { return (string)GetValue(ToolTipProperty); }
  set { SetValue(ToolTipProperty, value); }
 }
 public static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip",
 typeof(string), typeof(ChartButton), new PropertyMetadata());
 /// 
 /// 按钮内容
 /// 
 public string Content
 {
  get { return (string)GetValue(ContentProperty); }
  set { SetValue(ContentProperty, value); }
 }
 public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content",
 typeof(string), typeof(ChartButton), new PropertyMetadata("按钮"));
 /// 
 /// 图标
 /// 
 public ImageSource Icon
 {
  get { return (ImageSource)GetValue(IconProperty); }
  set { SetValue(IconProperty, value); }
 }
 public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon",
 typeof(ImageSource), typeof(ChartButton), new PropertyMetadata());
 /// 
 /// 图标高度
 /// 
 public double IconHeight
 {
  get { return (double)GetValue(IconHeightProperty); }
  set { SetValue(IconHeightProperty, value); }
 }
 public static readonly DependencyProperty IconHeightProperty = DependencyProperty.Register("IconHeight",
 typeof(double), typeof(ChartButton), new PropertyMetadata(25.0));
 /// 
 /// 图标宽度
 /// 
 public double IconWidth
 {
  get { return (double)GetValue(IconWidthProperty); }
  set { SetValue(IconWidthProperty, value); }
 }
 public static readonly DependencyProperty IconWidthProperty = DependencyProperty.Register("IconWidth",
 typeof(double), typeof(ChartButton), new PropertyMetadata(25.0));
 /// 
 /// 高度
 /// 
 public double Height
 {
  get { return (double)GetValue(HeightProperty); }
  set { SetValue(HeightProperty, value); }
 }
 public static readonly DependencyProperty HeightProperty = DependencyProperty.Register("Height",
 typeof(double), typeof(ChartButton), new PropertyMetadata(46.0));
 /// 
 /// 宽度
 /// 
 public double Width
 {
  get { return (double)GetValue(WidthProperty); }
  set { SetValue(WidthProperty, value); }
 }
 public static readonly DependencyProperty WidthProperty = DependencyProperty.Register("Width",
 typeof(double), typeof(ChartButton), new PropertyMetadata(170.0));
 #endregion
 private void ChartButton_OnLoaded(object sender, RoutedEventArgs e)
 {
  MyButton.ToolTip = ToolTip;
  MyButton.Content = Content;
  MyButton.Width = Width;
  MyButton.Height = Height;
  if (Icon != null)
  {
  MyButton.DataContext = new ChartButtonModel()
  {
   Icon = Icon,
   IconHeight = IconHeight,
   IconWidth = IconWidth
  };
  }
 }
 }
 public class ChartButtonModel
 {
 public ImageSource Icon { get; set; }
 public double IconHeight { get; set; }
 public double IconWidth { get; set; }
 }

3、定义数据组标题显示列表

前台:


 
 
 
 
 
  
  
   
  
  
  
  
   
   
    
    
   
   
    
    
    
    
   
   
   
  
  
 
 

后台:

/// 
 /// RadarChartTitleList.xaml 的交互逻辑
 /// 
 public partial class RadarChartTitleList : UserControl
 {
 public RadarChartTitleList()
 {
  InitializeComponent();
 }
 /// 
 /// 数据
 /// 
 public List ItemSoure
 {
  get { return (List)GetValue(ItemSoureProperty); }
  set { SetValue(ItemSoureProperty, value); }
 }
 public static readonly DependencyProperty ItemSoureProperty = DependencyProperty.Register("ItemSoure", typeof(List),
 typeof(RadarChartControl), new PropertyMetadata(new List()));
 private void RadarChartTitleList_OnLoaded(object sender, RoutedEventArgs e)
 {
  this.DataContext = ItemSoure;
 }
 }

4、界面引用控件


 
  
   
    
    
    
    
    
   
   
    
     
      
      
      
      
      
     
    
    
     
      
      
      
      
      
     
    
    
     
      
      
      
      
      
     
    
   
  
 

以上所述是小编给大家介绍的WPF 自定义雷达图开发实例教程,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之家网站的支持!

你可能感兴趣的:(WPF 自定义雷达图开发实例教程)