Este artículo es un artículo espejo de traducción automática, por favor haga clic aquí para saltar al artículo original.

Vista: 21090|Respuesta: 0

[WPF] Los proyectos WPF y Silverlight utilizan gráficos de barras, gráficos circulares y diagramas de líneas

[Copiar enlace]
Publicado en 9/12/2015 15:46:45 | | | |
En el proceso de desarrollo, puede que te encuentres con gráficos de barras, gráficos circulares y gráficos de líneas para mostrar mejor los datos. Este artículo es solo una pantalla sencilla; si quieres efectos de visualización complejos, consulta el ejemplo oficial del código de la web web. ---- el código de este artículo utiliza WPF, código similar de Silverlight y controles wpf_visifire_v5.1.2-0_trial de terceros.

Pongamos primero un conjunto de capturas de pantalla:


Datos públicos:

  1. private List<string> strListx = new List<string>() { "苹果", "樱桃", "菠萝", "香蕉", "榴莲", "葡萄", "桃子", "猕猴桃" };
  2.         private List<string> strListy = new List<string>() { "13", "75", "60", "38", "97", "22", "39", "80" };

  3.         private List<DateTime> LsTime = new List<DateTime>()
  4.             {
  5.                new DateTime(2012,1,1),
  6.                new DateTime(2012,2,1),
  7.                new DateTime(2012,3,1),
  8.                new DateTime(2012,4,1),
  9.                new DateTime(2012,5,1),
  10.                new DateTime(2012,6,1),
  11.                new DateTime(2012,7,1),
  12.                new DateTime(2012,8,1),
  13.                new DateTime(2012,9,1),
  14.                new DateTime(2012,10,1),
  15.                new DateTime(2012,11,1),
  16.                new DateTime(2012,12,1),
  17.             };
  18.         private List<string> cherry = new List<string>() { "33", "75", "60", "98", "67", "88", "39", "45", "13", "22", "45", "80" };
  19.         private List<string> pineapple = new List<string>() { "13", "34", "38", "12", "45", "76", "36", "80", "97", "22", "76", "39" };
Copiar código

Histograma:

  1. public void CreateChartColumn(string name, List<string> valuex, List<string> valuey)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = Name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             Axis yAxis = new Axis();
  22.             //设置图标中Y轴的最小值永远为0           
  23.             yAxis.AxisMinimum = 0;
  24.             //设置图表中Y轴的后缀         
  25.             yAxis.Suffix = "斤";
  26.             chart.AxesY.Add(yAxis);

  27.             // 创建一个新的数据线。               
  28.             DataSeries dataSeries = new DataSeries();

  29.             // 设置数据线的格式
  30.             dataSeries.RenderAs = RenderAs.StackedColumn;//柱状Stacked


  31.             // 设置数据点              
  32.             DataPoint dataPoint;
  33.             for (int i = 0; i < valuex.Count; i++)
  34.             {
  35.                 // 创建一个数据点的实例。                  
  36.                 dataPoint = new DataPoint();
  37.                 // 设置X轴点                    
  38.                 dataPoint.AxisXLabel = valuex[i];
  39.                 //设置Y轴点                  
  40.                 dataPoint.YValue = double.Parse(valuey[i]);
  41.                 //添加一个点击事件        
  42.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  43.                 //添加数据点                  
  44.                 dataSeries.DataPoints.Add(dataPoint);
  45.             }

  46.             // 添加数据线到数据序列。               
  47.             chart.Series.Add(dataSeries);

  48.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  49.             Grid gr = new Grid();
  50.             gr.Children.Add(chart);
  51.             Simon.Children.Add(gr);
  52.         }
Copiar código

Gráfico circular:

  1. public void CreateChartPie(string name, List<string> valuex, List<string> valuey)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             //Axis yAxis = new Axis();
  22.             ////设置图标中Y轴的最小值永远为0           
  23.             //yAxis.AxisMinimum = 0;
  24.             ////设置图表中Y轴的后缀         
  25.             //yAxis.Suffix = "斤";
  26.             //chart.AxesY.Add(yAxis);

  27.             // 创建一个新的数据线。               
  28.             DataSeries dataSeries = new DataSeries();

  29.             // 设置数据线的格式
  30.             dataSeries.RenderAs = RenderAs.Pie;//柱状Stacked


  31.             // 设置数据点              
  32.             DataPoint dataPoint;
  33.             for (int i = 0; i < valuex.Count; i++)
  34.             {
  35.                 // 创建一个数据点的实例。                  
  36.                 dataPoint = new DataPoint();
  37.                 // 设置X轴点                    
  38.                 dataPoint.AxisXLabel = valuex[i];

  39.                 dataPoint.LegendText = "##" + valuex[i];
  40.                 //设置Y轴点                  
  41.                 dataPoint.YValue = double.Parse(valuey[i]);
  42.                 //添加一个点击事件        
  43.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  44.                 //添加数据点                  
  45.                 dataSeries.DataPoints.Add(dataPoint);
  46.             }

  47.             // 添加数据线到数据序列。               
  48.             chart.Series.Add(dataSeries);

  49.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  50.             Grid gr = new Grid();
  51.             gr.Children.Add(chart);
  52.             Simon.Children.Add(gr);
  53.         }
Copiar código
Gráfico de líneas:

  1. public void CreateChartSpline(string name, List<DateTime> lsTime, List<string> cherry, List<string> pineapple)
  2.         {
  3.             //创建一个图标
  4.             Chart chart = new Chart();

  5.             //设置图标的宽度和高度
  6.             chart.Width = 580;
  7.             chart.Height = 380;
  8.             chart.Margin = new Thickness(100, 5, 10, 5);
  9.             //是否启用打印和保持图片
  10.             chart.ToolBarEnabled = false;

  11.             //设置图标的属性
  12.             chart.ScrollingEnabled = false;//是否启用或禁用滚动
  13.             chart.View3D = true;//3D效果显示

  14.             //创建一个标题的对象
  15.             Title title = new Title();

  16.             //设置标题的名称
  17.             title.Text = name;
  18.             title.Padding = new Thickness(0, 10, 5, 0);

  19.             //向图标添加标题
  20.             chart.Titles.Add(title);

  21.             //初始化一个新的Axis
  22.             Axis xaxis = new Axis();
  23.             //设置Axis的属性
  24.             //图表的X轴坐标按什么来分类,如时分秒
  25.             xaxis.IntervalType = IntervalTypes.Months;
  26.             //图表的X轴坐标间隔如2,3,20等,单位为xAxis.IntervalType设置的时分秒。
  27.             xaxis.Interval = 1;
  28.             //设置X轴的时间显示格式为7-10 11:20           
  29.             xaxis.ValueFormatString = "MM月";
  30.             //给图标添加Axis            
  31.             chart.AxesX.Add(xaxis);

  32.             Axis yAxis = new Axis();
  33.             //设置图标中Y轴的最小值永远为0           
  34.             yAxis.AxisMinimum = 0;
  35.             //设置图表中Y轴的后缀         
  36.             yAxis.Suffix = "斤";
  37.             chart.AxesY.Add(yAxis);


  38.             // 创建一个新的数据线。               
  39.             DataSeries dataSeries = new DataSeries();
  40.             // 设置数据线的格式。               
  41.             dataSeries.LegendText = "樱桃";

  42.             dataSeries.RenderAs = RenderAs.Spline;//折线图

  43.             dataSeries.XValueType = ChartValueTypes.DateTime;
  44.             // 设置数据点              
  45.             DataPoint dataPoint;
  46.             for (int i = 0; i < lsTime.Count; i++)
  47.             {
  48.                 // 创建一个数据点的实例。                  
  49.                 dataPoint = new DataPoint();
  50.                 // 设置X轴点                    
  51.                 dataPoint.XValue = lsTime[i];
  52.                 //设置Y轴点                  
  53.                 dataPoint.YValue = double.Parse(cherry[i]);
  54.                 dataPoint.MarkerSize = 8;
  55.                 //dataPoint.Tag = tableName.Split('(')[0];
  56.                 //设置数据点颜色                  
  57.                 // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                  
  58.                 dataPoint.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  59.                 //添加数据点                  
  60.                 dataSeries.DataPoints.Add(dataPoint);
  61.             }

  62.             // 添加数据线到数据序列。               
  63.             chart.Series.Add(dataSeries);


  64.             // 创建一个新的数据线。               
  65.             DataSeries dataSeriesPineapple = new DataSeries();
  66.             // 设置数据线的格式。         

  67.             dataSeriesPineapple.LegendText = "菠萝";

  68.             dataSeriesPineapple.RenderAs = RenderAs.Spline;//折线图

  69.             dataSeriesPineapple.XValueType = ChartValueTypes.DateTime;
  70.             // 设置数据点              

  71.             DataPoint dataPoint2;
  72.             for (int i = 0; i < lsTime.Count; i++)
  73.             {
  74.                 // 创建一个数据点的实例。                  
  75.                 dataPoint2 = new DataPoint();
  76.                 // 设置X轴点                    
  77.                 dataPoint2.XValue = lsTime[i];
  78.                 //设置Y轴点                  
  79.                 dataPoint2.YValue = double.Parse(pineapple[i]);
  80.                 dataPoint2.MarkerSize = 8;
  81.                 //dataPoint2.Tag = tableName.Split('(')[0];
  82.                 //设置数据点颜色                  
  83.                 // dataPoint.Color = new SolidColorBrush(Colors.LightGray);                  
  84.                 dataPoint2.MouseLeftButtonDown += new MouseButtonEventHandler(dataPoint_MouseLeftButtonDown);
  85.                 //添加数据点                  
  86.                 dataSeriesPineapple.DataPoints.Add(dataPoint2);
  87.             }
  88.             // 添加数据线到数据序列。               
  89.             chart.Series.Add(dataSeriesPineapple);

  90.             //将生产的图表增加到Grid,然后通过Grid添加到上层Grid.           
  91.             Grid gr = new Grid();
  92.             gr.Children.Add(chart);
  93.             
  94.             Simon.Children.Add(gr);
  95.         }
Copiar código

Descarga de código VisifireShow.rar (475.66 KB, Número de descargas: 2, 售价: 1 粒MB)





Anterior:¿Cuántos bucles puede saltar un break? ¿Cómo puedo saltar de todos los bucles?
Próximo:Crea una imagen de fondo para Visual Studio 2013 o VS2010, ¡es genial!
Renuncia:
Todo el software, materiales de programación o artículos publicados por Code Farmer Network son únicamente para fines de aprendizaje e investigación; El contenido anterior no se utilizará con fines comerciales o ilegales; de lo contrario, los usuarios asumirán todas las consecuencias. La información de este sitio proviene de Internet, y las disputas de derechos de autor no tienen nada que ver con este sitio. Debes eliminar completamente el contenido anterior de tu ordenador en un plazo de 24 horas desde la descarga. Si te gusta el programa, por favor apoya el software genuino, compra el registro y obtén mejores servicios genuinos. Si hay alguna infracción, por favor contáctanos por correo electrónico.

Mail To:help@itsvse.com