栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

使用WPF.LiveChart创建各个样式的Chart

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用WPF.LiveChart创建各个样式的Chart

使用WPF.LiveChart创建各个样式的Chart

制作一个usercontrol能随时插入chart控件,chart的style有标准的一套,当然可以更改style。

wpf 的view界面如下:


    
        
            
                
            
            
                
            
            
                
            
        
    


WPF.LiveChart属性详解:

1.设置折线属性
        #region 设置折线属性
        /// 
        /// 设置折线颜色
        /// 
        public SolidColorBrush lineStroke = new SolidColorBrush(Color.FromRgb(33, 149, 242));
        /// 
        /// 设置折线粗细
        /// 
        public int strokeThickness = 2;
        /// 
        /// 设置折线样式
        /// 
        public DoubleCollection strokeDashArray = null;
        /// 
        /// 设置折线是否光滑
        /// 
        public int lineSmoothness = 1;
        /// 
        /// 设置折线填充颜色
        /// 
        public SolidColorBrush lineFill = new SolidColorBrush(Color.FromRgb(222,239,253));
        #endregion
2.设置轴线 separator
        #region 设置轴线 separator
        /// 
        /// 设置轴线粗细
        /// 
        public int separator_StrokeThickness = 1;
        /// 
        /// 设置轴线颜色
        /// 
        public SolidColorBrush separator_Stroke = new SolidColorBrush(Color.FromRgb(240, 240, 240));
        /// 
        /// 设置轴线标题
        /// 
        public double separator_Step = 1;
        /// 
        /// 设置轴线标题位置
        /// 
        public AxisPosition separator_Position = AxisPosition.LeftBottom;
        /// 
        /// 设置坐标轴标签旋转角度
        /// 
        public int separator_LabelsRotation = 45;
        #endregion
3.设置图表属性
        #region 设置图表
        /// 
        /// 设置图表的背景颜色
        /// 
        public SolidColorBrush chartBackground = System.Windows.Media.Brushes.White;
        /// 
        /// legend位置
        /// 
        public LegendLocation legendLocation = LegendLocation.Right;
        #endregion
4.设置Chart提示
        #region 设置提示

        /// 
        /// 设置提示的背景颜色
        /// 
        public SolidColorBrush tooltipBackground = System.Windows.Media.Brushes.LightCyan;
        /// 
        /// 设置提示的选择模式
        /// 
        public TooltipSelectionMode SelectionMode  =TooltipSelectionMode.OnlySender;

        /// 
        /// 设置提示框的圆角半径
        /// 
        public CornerRadius CornerRadius = new CornerRadius(5);

        #region 设置提示框的边框颜色和大小

        /// 
        /// 边框颜色
        /// 
        public SolidColorBrush tooltipBorderBrush = System.Windows.Media.Brushes.Yellow;

        /// 
        /// 边框大小
        /// 
        public Thickness tooltipBorderThickness = new Thickness(2);
        #endregion
5.图表数据属性
        private string axisXName;

        public string AxisXName
        {
            get { return axisXName; }
            set { axisXName = value;
                //OnPropertyChanged("AxisXName");
            }
        }
        private string axisYName;

        public string AxisYName
        {
            get { return axisYName; }
            set { axisYName = value;
                OnPropertyChanged("AxisYName"); 
            }
        }
        
        /// 
        /// 图表所用数据
        /// 
        private SeriesCollection seriesCollection = new SeriesCollection();
        public SeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set { seriesCollection = value; OnPropertyChanged("SeriesCollection"); }
        }
        /// 
        /// 横轴标题
        /// 
        private string[] labels = null;
        public string[] Labels
        {
            get { return labels; }
            set { labels = value; OnPropertyChanged("Labels"); }
        }
        public Func Formatter { get; set; }
        #region ImContentPanel

        public string ContentToolTip
        {
            get
            {
                return "图表窗口";
            }
        }
        public ImageSource Icon
        {
            get
            {
                return null;
            }
        }
        private string _title = "图表窗口";
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
            }
        }
        #endregion
6.图表类型
    /// 
    /// 图表类型
    /// 
    public  enum ChartType
    {
        [Description("折线图 ")]
        Line,
        [Description("柱状图")]
        Bar,
        [Description("饼状图")]
        Pie
    }
7.绘制Chart
        #region 绘制Chart
       
        private void SetData(string title, List values)
        {
            try
            {
                switch (_chartType)
                {
                    case ChartType.Line:
                        GetLineChart(title, values);
                        break;
                    case ChartType.Bar:
                        GetBarChart(title, values);
                        break;
                    case ChartType.Pie:
                        GetPieChart(title, values);
                        break;
                    default:
                        GetLineChart(title, values);
                        break;
                }  
            }
            catch { }  
        }
        private LineSeries GetLineSeries(string title, List values)
        {
            LineSeries mylineseries = new LineSeries() {
                Title = title,//标题
                Stroke = lineStroke,//折线颜色
                LineSmoothness = lineSmoothness,//是否光滑
                StrokeThickness = strokeThickness,//设置折线粗细
                StrokeDashArray = strokeDashArray,//设置折线样式
                Fill = lineFill,//设置折线填充颜色
                Values = new ChartValues(values)
            };
            return mylineseries;
        }
        private void GetLineChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetLineSeries(title, values));

                mychart.LegendLocation = legendLocation;
                mychart.Background = chartBackground;

                myaxisx.Separator.StrokeThickness = separator_StrokeThickness;
                myaxisx.Separator.Stroke = separator_Stroke;
                myaxisx.Separator.Step = separator_Step;
                myaxisx.Position = separator_Position;
                myaxisx.LabelsRotation = separator_LabelsRotation;

                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }
        private void GetBarChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetBarSeries(title, values));
                mychart.LegendLocation = legendLocation;
                mychart.Foreground = chartBackground;
                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }
        private ColumnSeries GetBarSeries(string title, List values)
        {
            ColumnSeries mylineseries = new ColumnSeries()
            {

                Title = title,//标题
                Values = new ChartValues(values)
            };
            return mylineseries;
        }
        private PieSeries GetPieSeries(string title, List values)
        {
            PieSeries mylineseries = new PieSeries()
            {

                Title = title,//标题
                Values = new ChartValues(values)
            };
            return mylineseries;   
        }
        private void GetPieChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetPieSeries(title, values));
                mychart.LegendLocation = legendLocation;
                mychart.Foreground = chartBackground;
                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }

        private void SetLabels(string[] labels)
        {
            Labels = labels;
        }
         #endregion

8.更新Chart

        /// 
        /// update单chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void UpdateChart(List x, List values, string AxisXName, string AxisYName, string title, ChartType _charttype)
        {
            SeriesCollection.Clear();
            chartType = _charttype;
            Title = title;
            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());

            SetData(AxisYName, values);
            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }
        /// 
        /// update多chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void UpdateChart(List x, string AxisXName, Dictionary> dic_data, string title, ChartType _charttype)
        {
            SeriesCollection.Clear();
            chartType = _charttype;
            Title = title;

            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());
            foreach (var item in dic_data)
            {
                SetData(item.Key, item.Value);
            }

            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }

全部代码如下:

using JetBrains.Annotations;
using LiveCharts;
using LiveCharts.Wpf;
using System;
using Common;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Common.Control.View
{
   
    /// 
    /// LiveChartWin.xaml 的交互逻辑
    /// 
    public partial class LiveChartWin : UserControl, INotifyPropertyChanged, ImContentPanel
    {
        public LiveChartWin()
        {
            InitializeComponent();
            DataContext = this;
        }

        /// 
        /// 单chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public LiveChartWin(List x, List values, string AxisXName, string AxisYName, string title, ChartType _charttype) : this()
        {
            chartType = _charttype;
            Title = title;

            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());

            SetData(AxisYName, values);
            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }

        /// 
        /// 多列chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public LiveChartWin(List x,string AxisXName,Dictionary>dic_data, string title, ChartType _charttype) : this()
        {
            chartType = _charttype;
            Title = title;

            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());
            foreach (var item in dic_data)
            {
                SetData(item.Key, item.Value);
            }
            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region ImContentPanel

        public string ContentToolTip
        {
            get
            {
                return "图表窗口";
            }
        }
        public ImageSource Icon
        {
            get
            {
                return null;
            }
        }
        private string _title = "图表窗口";
        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
            }
        }
        #endregion

        #region  axis属性
        private string axisXName;

        public string AxisXName
        {
            get { return axisXName; }
            set
            {
                axisXName = value;
                //OnPropertyChanged("AxisXName");
            }
        }
        private string axisYName;

        public string AxisYName
        {
            get { return axisYName; }
            set
            {
                axisYName = value;
                OnPropertyChanged("AxisYName");
            }
        }
        /// 
        /// 图表类型
        /// 
        private ChartType _chartType = ChartType.Line;

        public ChartType chartType
        {
            get
            {
                return _chartType;
            }
            set
            {
                _chartType = value;
            }
        }
        #endregion

        #region 设置折线属性
        /// 
        /// 设置折线颜色
        /// 
        public SolidColorBrush lineStroke = new SolidColorBrush(Color.FromRgb(33, 149, 242));
        /// 
        /// 设置折线粗细
        /// 
        public int strokeThickness = 2;
        /// 
        /// 设置折线样式
        /// 
        public DoubleCollection strokeDashArray = null;
        /// 
        /// 设置折线是否光滑
        /// 
        public int lineSmoothness = 1;
        /// 
        /// 设置折线填充颜色
        /// 
        public SolidColorBrush lineFill = new SolidColorBrush(Color.FromRgb(222,239,253));
        #endregion

        #region 设置轴线 separator
        /// 
        /// 设置轴线粗细
        /// 
        public int separator_StrokeThickness = 1;
        /// 
        /// 设置轴线颜色
        /// 
        public SolidColorBrush separator_Stroke = new SolidColorBrush(Color.FromRgb(240, 240, 240));
        /// 
        /// 设置轴线标题
        /// 
        public double separator_Step = 1;
        /// 
        /// 设置轴线标题位置
        /// 
        public AxisPosition separator_Position = AxisPosition.LeftBottom;
        /// 
        /// 设置坐标轴标签旋转角度
        /// 
        public int separator_LabelsRotation = 45;
        #endregion

        #region 设置图表
        /// 
        /// 设置图表的背景颜色
        /// 
        public SolidColorBrush chartBackground = System.Windows.Media.Brushes.White;
        /// 
        /// legend位置
        /// 
        public LegendLocation legendLocation = LegendLocation.Right;
        #endregion

        #region 设置提示

        /// 
        /// 设置提示的背景颜色
        /// 
        public SolidColorBrush tooltipBackground = System.Windows.Media.Brushes.LightCyan;
        /// 
        /// 设置提示的选择模式
        /// 
        public TooltipSelectionMode SelectionMode  =TooltipSelectionMode.OnlySender;

        /// 
        /// 设置提示框的圆角半径
        /// 
        public CornerRadius CornerRadius = new CornerRadius(5);

        #region 设置提示框的边框颜色和大小

        /// 
        /// 边框颜色
        /// 
        public SolidColorBrush tooltipBorderBrush = System.Windows.Media.Brushes.Yellow;

        /// 
        /// 边框大小
        /// 
        public Thickness tooltipBorderThickness = new Thickness(2);
        #endregion

        private SolidColorBrush[] acBrsush = { System.Windows.Media.Brushes.Red,System.Windows.Media.Brushes.Yellow,System.Windows.Media.Brushes.Red,System.Windows.Media.Brushes.Blue,System.Windows.Media.Brushes.Red,System.Windows.Media.Brushes.Green,System.Windows.Media.Brushes.Red,System.Windows.Media.Brushes.Purple,System.Windows.Media.Brushes.Red,System.Windows.Media.Brushes.GreenYellow
    };


        /// 
        /// 图表所用数据
        /// 
        private SeriesCollection seriesCollection = new SeriesCollection();
        public SeriesCollection SeriesCollection
        {
            get { return seriesCollection; }
            set { seriesCollection = value; OnPropertyChanged("SeriesCollection"); }
        }
        /// 
        /// 横轴标题
        /// 
        private string[] labels = null;
        public string[] Labels
        {
            get { return labels; }
            set { labels = value; OnPropertyChanged("Labels"); }
        }
        public Func Formatter { get; set; }

        #endregion

        #region 绘制Chart

        /// 
        /// update单chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void UpdateChart(List x, List values, string AxisXName, string AxisYName, string title, ChartType _charttype)
        {
            SeriesCollection.Clear();
            chartType = _charttype;
            Title = title;
            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());

            SetData(AxisYName, values);
            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }
        /// 
        /// update多chart
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void UpdateChart(List x, string AxisXName, Dictionary> dic_data, string title, ChartType _charttype)
        {
            SeriesCollection.Clear();
            chartType = _charttype;
            Title = title;

            List labels = new List();
            for (int i = 0; i < x.Count; i++)
            {
                labels.Add(x[i].ToString());
            }
            SetLabels(labels.ToArray());
            foreach (var item in dic_data)
            {
                SetData(item.Key, item.Value);
            }

            this.AxisXName = AxisXName;
            this.AxisYName = AxisYName;
            Formatter = value => value.ToString("F");
        }

        private void SetData(string title, List values)
        {
            try
            {
                switch (_chartType)
                {
                    case ChartType.Line:
                        GetLineChart(title, values);
                        break;
                    case ChartType.Bar:
                        GetBarChart(title, values);
                        break;
                    case ChartType.Pie:
                        GetPieChart(title, values);
                        break;
                    default:
                        GetLineChart(title, values);
                        break;
                }  
            }
            catch { }  
        }
        private LineSeries GetLineSeries(string title, List values)
        {
            LineSeries mylineseries = new LineSeries() {
                Title = title,//标题
                Stroke = lineStroke,//折线颜色
                LineSmoothness = lineSmoothness,//是否光滑
                StrokeThickness = strokeThickness,//设置折线粗细
                StrokeDashArray = strokeDashArray,//设置折线样式
                Fill = lineFill,//设置折线填充颜色
                Values = new ChartValues(values)
            };
            return mylineseries;
        }
        private void GetLineChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetLineSeries(title, values));

                mychart.LegendLocation = legendLocation;
                mychart.Background = chartBackground;

                myaxisx.Separator.StrokeThickness = separator_StrokeThickness;
                myaxisx.Separator.Stroke = separator_Stroke;
                myaxisx.Separator.Step = separator_Step;
                myaxisx.Position = separator_Position;
                myaxisx.LabelsRotation = separator_LabelsRotation;

                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }
        private void GetBarChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetBarSeries(title, values));
                mychart.LegendLocation = legendLocation;
                mychart.Foreground = chartBackground;
                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }
        private ColumnSeries GetBarSeries(string title, List values)
        {
            ColumnSeries mylineseries = new ColumnSeries()
            {

                Title = title,//标题
                Values = new ChartValues(values)
            };
            return mylineseries;
        }
        private PieSeries GetPieSeries(string title, List values)
        {
            PieSeries mylineseries = new PieSeries()
            {

                Title = title,//标题
                Values = new ChartValues(values)
            };
            return mylineseries;   
        }
        private void GetPieChart(string title, List values)
        {
            try
            {
                SeriesCollection.Add(GetPieSeries(title, values));
                mychart.LegendLocation = legendLocation;
                mychart.Foreground = chartBackground;
                提示
                mytooltip.Background = tooltipBackground;
                mytooltip.SelectionMode = SelectionMode;
                mytooltip.CornerRadius = CornerRadius;
                mytooltip.BorderBrush = tooltipBorderBrush;
                mytooltip.BorderThickness = tooltipBorderThickness;
            }
            catch { }
        }

        private void SetLabels(string[] labels)
        {
            Labels = labels;
        }
        #endregion

        #region 窗体事件
        public void OnActive()
        {

        }
        public void OnClosing()
        {

        }
        public void OnClosed()
        {
            SeriesCollection.Clear();
        }
        #endregion

    }


    /// 
    /// 图表类型
    /// 
    public  enum ChartType
    {
        [Description("折线图 ")]
        Line,
        [Description("柱状图")]
        Bar,
        [Description("饼状图")]
        Pie
    }
}

UserControl继承接口ImContentPanel

using System.Windows.Media;

namespace Common
{
    public interface ImContentPanel
    {
        string Title { get; }
        ImageSource Icon { get; }
        string ContentToolTip { get; }
        object Tag { get; }

        void OnActive();
        void OnClosing();
        void OnClosed();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/909842.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号