zoukankan      html  css  js  c++  java
  • 在WinForm中用Visifire实现数据统计转自蓝月天南

    我们不是为了赚钱而工作、投资,我们的最终目的只有一个:提升我们的幸福感。那么什么是幸福感呢?幸福感是一个比例,它的分子
    是“你已经拥有的东西”,分母是“你想要拥有的东西”,所以,提升幸福感有两个途径,一个是提高分子,那就是得到自己想得到的
    东西;还有一个就是降低分母,也就是降低自己的目标和要求。
                                                                                                        ——摘自:Jimmy Zhang:感悟、资产和幸福感


    一、Visifire概述:                                                         

      一个基于Silverlight的Chart组件,是Visifrie公司开发的一套数据可视化图表控件,它支持Silverlight和WPF应用程序,可以显示为柱形、 馅饼、快线、堆积、

    漏斗等多种图表类型,还可以显示为圆形和线性仪表样式。

      Visifire不论是在桌面应用程序、Web应用程序,还是WP7系列的移动应用程序,都可以使用同样的API,在几分钟之内创建出一个视觉、动画很炫的图表或仪表。

      Visifire现已经更新到4.1版本(4.5的测试版已出),而且不再开源,但不管如何,Visifire都可以说是现今为止效果,性能和灵活性最好的silverlight chart组件。

         

    二、Visifire用法:

    1添加引用:

      首先需要到Visifire网站购买正式版或下载试用版,获得Visifire项目Silverlight开发包,然后在新建的项目中添加对WPFVisifire.Charts.dll和

    WPFVisifire.Gauges.dll程序集的引用,并添加命名空间。

    2图表Chart的属性:

    1、是否3D显示:

      Chart默认是2D的显示,可以通过View3D来设置是否以3D显示。

    1 Chart _chart = new Chart();    //new一个图表元素
    2 _chart.View3D = true; //图表以3D展示

    2、标题样式的设置

      在Visifire中,也可以为Chart的Title和Axis的Title分别设置不同样式。

    1 Axis axis = new Axis();
    2 axis.Title = "标题Text";
    3 axis.TitleFontColor = new SolidColorBrush(Colors.Green);         //颜色
    4 FontFamilyConverter fontfamilyConver = new FontFamilyConverter();    //new一个FontFamily转换器
    5 axis.TitleFontFamily = (System.Windows.Media.FontFamily)fontfamilyConver.ConvertFrom("Arial");//FontFamily
    6 axis.TitleFontSize = 20;    //大小

    3、图表的Theme的设置

      Visifire为Chart内置了三种Themes,分别表示不同的动画类型和颜色集合,可以直接用字符串为Chart的Theme属性赋值。

    1 Chart _chart = new Chart();   //new一个图表元素
    2 //_chart.Theme = "Theme1"; //默认
    3 //_chart.Theme = "Theme2";
    4 _chart.Theme = "Theme3";

    4、是否动态绘制图表:

      当AnimationEnabled设置为false时,打开界面后,图表直接显示,不再动态绘制。

    1 Chart _chart = new Chart(); 
    2 _chart.AnimationEnabled = false; //不绘制

    5、图表的颜色设置:

      Visifire为Chart内置了很多颜色集合,我们可以用特定的字符串为Chart的ColorSet属性赋值,为最终生成的图表设置颜色。

    1 Chart _chart = new Chart();    //new一个图表元素
    2 _chart.ColorSet = "VisiRed"; //VisiGreen、VisiBlue……

    6、显示类型设置:

      一个Chart可以有多个DataSeries,可以通过枚举RenderAs为每个DataSeries设置显示类型,用来确定此DataSeries内的数据点在Chart是如何显示。

    1 DataSeries dataSeries = new DataSeries(); //数据系列
    2 dataSeries.RenderAs = RenderAs.Column; //柱形展示

      可以看下RenderAs这个枚举:

    View Code
     1  public enum RenderAs
    2 {
    3 Column = 0,
    4 Line = 1,
    5 Pie = 2,
    6 Bar = 3,
    7 Area = 4,
    8 Doughnut = 5,
    9 StackedColumn = 6,
    10 StackedColumn100 = 7,
    11 StackedBar = 8,
    12 StackedBar100 = 9,
    13 StackedArea = 10,
    14 StackedArea100 = 11,
    15 Bubble = 12,
    16 Point = 13,
    17 StreamLineFunnel = 14,
    18 SectionFunnel = 15,
    19 Stock = 16,
    20 CandleStick = 17,
    21 StepLine = 18,
    22 Spline = 19,
    23 Radar = 20,
    24 Polar = 21,
    25 Pyramid = 22,
    26 QuickLine = 23,
    27 }

    3仪表Gauge的属性:

      Gauge的类型只有Circular和Linear两种,下面分别看下:

    1、Linear矩形仪表:

      当Gauge的Type是Linear时,Gauge内可以添加并显示一个BarIndicator指示器。

    1  Gauge gauge = new Gauge();                  //new 一个仪表元素
    2 gauge.Type = GaugeTypes.Linear;
    3 BarIndicator indicator2 = new BarIndicator();//指示条
    4 indicator2.Value =100;
    5 gauge.Indicators.Add(indicator1);

    2、Circular圆形仪表:

      当Gauge的Type是Circular时,Gauge除了可以添加一个BarIndicator指示器,还需添加一个NeedleIndicator指示器。

    1 Gauge gauge = new Gauge();            //new一个仪表元素
    2 gauge.Type = GaugeTypes.Circular;
    3 BarIndicator indicator2 = new BarIndicator(); //指示条
    4 indicator2.Value = rnd.Next(0, 100);
    5 NeedleIndicator indicator1 = new NeedleIndicator();//指示针
    6 indicator1.Value = rnd.Next(0, 100);



    三、Visifire例子:

    1、创建一个单数据系列的图表:

      实现代码:

    View Code
     1  private void CreateChar1()
    2 {
    3 Chart _chart = new Chart(); //new一个图表元素
    4
    5 Title title = new Title();
    6 title.Text = "图表标题";
    7 _chart.Titles.Add(title);//为图表添加一个Title
    8
    9 Axis charAxisX = new Axis();
    10 charAxisX.Title = "图表X轴";
    11 _chart.AxesX.Add(charAxisX);//为图表添加一个AxesX
    12
    13 Axis charAxisY = new Axis();
    14 charAxisY.Title = "图表Y轴";
    15 _chart.AxesY.Add(charAxisY);//我图表添加一个AxesY
    16
    17 _chart.View3D = true;//图表以3D展示
    18 Random rnd = new Random();//new一个随机数生成器
    19
    20 DataPoint dataPoint;//数据点
    21 DataSeries dataSeries = new DataSeries();//数据系列
    22 dataSeries.RenderAs = RenderAs.Column;//柱形展示
    23 for (int i = 0; i <= 7; i++)
    24 {
    25 dataPoint = new DataPoint();
    26 dataPoint.AxisXLabel = i + "";//x轴标签:时间
    27 dataPoint.YValue = rnd.Next(1, 100);//y轴值
    28 dataSeries.DataPoints.Add(dataPoint);//为数据系列添加一个数据点
    29 }
    30 _chart.Series.Add(dataSeries);//为图表添加一个数据系列
    31 elementHost1.Child = _chart;
    32 }

    2、创建一个双数据系列的图表

      实现代码:

    View Code
     1   private void CreateChar2()
    2 {
    3 Chart _chart = new Chart();//new一个图表
    4 _chart.View3D = true;//3D格式显示
    5
    6 Title title = new Title();//图表标题
    7 Axis axisX = new Axis();//图表X轴
    8 Axis axisY = new Axis(); //图表Y轴
    9 Axis axisY2 = new Axis();
    10 title.Text = "数据统计";//赋值
    11 axisX.Title = "2011年";
    12 axisY.Title = "统计";
    13 axisY2.Title = "温度";
    14 axisY2.AxisType = AxisTypes.Secondary;//次要的轴线
    15 _chart.Titles.Add(title);//添加
    16 _chart.AxesX.Add(axisX);
    17 _chart.AxesY.Add(axisY);
    18 _chart.AxesY.Add(axisY2);
    19
    20 Random random = new Random();//随即数
    21 DataPoint dataPoint;//数据点
    22
    23 DataSeries dataSeries = new DataSeries(); //数据系列
    24 dataSeries.LegendText = "统计说明"; //说明文本
    25 dataSeries.RenderAs = RenderAs.Column;
    26 for (int i = 0; i < 10; i++)
    27 {
    28 dataPoint = new DataPoint();
    29 dataPoint.AxisXLabel = i + "";
    30 dataPoint.YValue = random.Next(1, 100);
    31 dataSeries.DataPoints.Add(dataPoint);//数据点添加到数据系列
    32 }
    33 _chart.Series.Add(dataSeries); //添加到图表
    34
    35
    36 DataSeries dataSeries2 = new DataSeries();//数据系列2
    37 dataSeries2.LegendText = "温度说明"; //说明文本
    38 dataSeries2.RenderAs = RenderAs.Line;
    39 for (int i = 0; i < 10; i++)
    40 {
    41 dataPoint = new DataPoint();
    42 dataPoint.AxisXLabel = i + "";
    43 dataPoint.YValue = random.Next(1, 100);
    44 dataSeries2.AxisYType = AxisTypes.Secondary;//次要轴线赋值
    45 dataSeries2.DataPoints.Add(dataPoint);
    46 }
    47 _chart.Series.Add(dataSeries2);//数据系列添加到图表.系列
    48
    49 elementHost1.Child = _chart;
    50 }

    3、两个图表的创建

      实现代码:

    View Code
      1         Chart _chart1;
    2 Chart _chart2;
    3 Double[] _data = new Double[20];
    4 Boolean _oddState = false;
    5
    6 public Form1()
    7 {
    8 InitializeComponent();
    9 CreateColumnChart();
    10 CreateLineChart();
    11 timer1.Start();//打开定时器
    12 timer1.Interval = 1000;
    13 }
    14
    15 // 柱形图表
    16 private void CreateColumnChart()
    17 {
    18 _chart1 = new Chart();//new图表
    19 _chart1.Width = 70;
    20 _chart1.Height = 120;
    21 _chart1.Background = new SolidColorBrush(Colors.Black);//背景颜色填充
    22
    23 Title title = new Title();//图表À的标题
    24 title.VerticalAlignment = VerticalAlignment.Bottom;//标题:底部
    25 title.FontColor = new SolidColorBrush(Colors.White);//前景色:白色
    26 _chart1.Titles.Add(title);
    27
    28 Axis axisX = new Axis();
    29 axisX.Enabled = false;
    30 _chart1.AxesX.Add(axisX);//添加X轴线
    31
    32 Axis axisY = new Axis();
    33 axisY.Enabled = false; // 不显示title
    34 ChartGrid grid = new ChartGrid();//表格
    35 grid.Enabled = false;
    36 axisY.Grids.Add(grid); //在轴线上添加不显示的网格
    37 _chart1.AxesY.Add(axisY); //添加Y轴线
    38
    39 _chart1.Series.Add(new DataSeries() { RenderAs = RenderAs.Column });//柱形显示
    40 _chart1.Series[0].Color = new SolidColorBrush(System.Windows.Media.Color.FromArgb((Byte)0xff, (Byte)0x00, (Byte)0xff, (Byte)0x00));//颜色
    41 _chart1.Series[0].DataPoints.Add(new DataPoint());//数据点
    42 elementHost1.Child = _chart1;//宿主
    43 }
    44
    45 // 线性图表
    46 private void CreateLineChart()
    47 {
    48 _chart2 = new Chart();
    49 _chart2.Width = 200;
    50 _chart2.Height = 120;
    51 _chart2.AnimationEnabled = false;//不绘制
    52 _chart2.ScrollingEnabled = false;//不卷动
    53 _chart2.Background = new SolidColorBrush(Colors.Black);//背景黑色
    54
    55 Axis axisX = new Axis(); //new一个轴线
    56 ChartGrid grid = new ChartGrid(); //网格
    57 grid.LineColor = new SolidColorBrush(System.Windows.Media.Color.FromArgb((Byte)0xff, (Byte)0x00, (Byte)0x80, (Byte)0x40));//颜色
    58 grid.LineThickness = 1; //线的厚度
    59 axisX.Grids.Add(grid); //把网格添加到轴线
    60 Ticks tickX = new Ticks(); //new个十字叉
    61 tickX.Enabled = false;
    62 axisX.Ticks.Add(tickX); //把十字叉加到轴
    63 _chart2.AxesX.Add(axisX);//为图表添加X轴
    64
    65 Axis axisY = new Axis();
    66 //这里不赋3值则显示-100到100的范围
    67 //axisY.AxisMinimum = -100;
    68 //axisY.AxisMaximum = 100;
    69 //axisY.AxisLabels.Enabled = false;
    70 ChartGrid gridY = new ChartGrid();
    71 gridY.LineColor = new SolidColorBrush(System.Windows.Media.Color.FromArgb((Byte)0xff, (Byte)0x00, (Byte)0x80, (Byte)0x40));
    72 gridY.LineThickness = 1;
    73 axisY.Grids.Add(gridY);
    74 Ticks tickY = new Ticks();
    75 tickY.Enabled = false;
    76 axisY.Ticks.Add(tickY);
    77 _chart2.AxesY.Add(axisY);//为图表添加Y轴
    78
    79 _chart2.Series.Add(new DataSeries() { RenderAs = RenderAs.Line });//线形显示
    80 _chart2.Series[0].Color = new SolidColorBrush(System.Windows.Media.Color.FromArgb((Byte)0xff, (Byte)0x00, (Byte)0xff, (Byte)0x00));
    81
    82 for (Int32 i = 0; i < _data.Length; i++)//循环赋值
    83 _chart2.Series[0].DataPoints.Add(new DataPoint());
    84
    85 elementHost2.Child = _chart2;//宿主
    86 }
    87
    88 private void timer1_Tick(object sender, EventArgs e)
    89 {
    90 UpdateData();//更新数据
    91 for (Int32 i = 0; i < _data.Length; i++)
    92 {
    93 _chart2.Series[0].DataPoints[i].YValue = _data[i];//把新数据赋值给_chart2
    94 }
    95
    96 _chart1.Series[0].DataPoints[0].YValue = Math.Abs(_data[_data.Length - 1]);//_chart1的值是数组末数绝对值
    97 _chart1.Titles[0].Text = Convert.ToString(Math.Abs(_data[_data.Length - 1])) + "%";//Title是%
    98 }
    99
    100 //更新Date
    101 private Double[] UpdateData()
    102 {
    103 Random rand = new Random();
    104 Int32 i;
    105 for (i = 0; i < _data.Length - 1; i++)
    106 {
    107 _data[i] = _data[i + 1];
    108 }
    109 if (!_oddState)//不是奇数,则新添加
    110 {
    111 _oddState = true;
    112 _data[i] = rand.Next(0, 100);
    113 }
    114 else//加最好一个数的负数
    115 {
    116 _oddState = false;
    117 _data[i] = -_data[i];
    118 }
    119 return _data;
    120 }


     

    4、仪表的创建:

        

      实现代码:

    View Code
     1         /// <summary>
    2 /// 圆形仪表的创建
    3 /// </summary>
    4 private void CreateGauge1()
    5 {
    6 Gauge gauge1 = new Gauge();//new一个仪表
    7 Random rnd = new Random();
    8
    9 gauge1.Width = 50;
    10 gauge1.Height = 30;
    11 gauge1.Type = GaugeTypes.Circular;//圆形
    12
    13 NeedleIndicator circularNeedle = new NeedleIndicator();//指示针
    14 circularNeedle.Value = rnd.Next(0, 100);
    15 gauge1.Indicators.Add(circularNeedle);
    16 BarIndicator circularBar = new BarIndicator(); //指示条
    17 circularBar.Value = rnd.Next(0, 100);
    18 gauge1.Indicators.Add(circularBar);
    19 elementHost1.Child = gauge1;
    20 }
    21 /// <summary>
    22      /// 矩形仪表的创建
    23 /// </summary>
    24 private void CreateGauge2()
    25 {
    26 Gauge gauge2 = new Gauge();
    27 gauge2.Type = GaugeTypes.Linear;//new一个矩形仪表
    28 Random rnd = new Random();
    29
    30 BarIndicator LinearBar = new BarIndicator();//指示条
    31 LinearBar.Value = rnd.Next(0, 100);
    32 gauge2.Indicators.Add(LinearBar);
    33 elementHost1.Child = gauge2;
    34 }
  • 相关阅读:
    PAT 甲级 1132 Cut Integer (20 分)
    AcWing 7.混合背包问题
    AcWing 9. 分组背包问题
    AcWing 5. 多重背包问题 II
    AcWing 3. 完全背包问题
    AcWing 4. 多重背包问题
    AcWing 2. 01背包问题
    AcWing 875. 快速幂
    AcWing 874. 筛法求欧拉函数
    AcWing 873. 欧拉函数
  • 原文地址:https://www.cnblogs.com/xinweichen/p/2280447.html
Copyright © 2011-2022 走看看