7
Uso del control ZedGraph con C# (Gráficos circulares) Aintzane Conde Uso del control ZedGraph en C# Gráficos circulares

Uso del control ZedGraph en C# - Gráficos circulares

Embed Size (px)

Citation preview

Page 1: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

Uso del control ZedGraph en C#Gráficos circulares

Page 2: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

Uso del control ZedGraph en C#Gráficos circulares

Vamos a ver algún ejemplo de los gráficos circulares que podemos implementar.

En estos ejemplos se sigue la dinámica del documento Uso del control ZedGraph en C#, siendo la única diferencia la que se establece en el código:

Gráfico circularusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

using ZedGraph; //Para poder usar los comandos relacionados con el gráfico

namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Representacion(zgcGrafico); SetSize(); }

//Ajustar el tamaño y la ubicación del ZedGraphControl private void SetSize() { //El control está siempre insertado en 10 píxeles del rectángulo formulario Rectangle formRect = this.ClientRectangle; formRect.Inflate(-10, -10);

if (zgcGrafico.Size != formRect.Size) { zgcGrafico.Location = formRect.Location; zgcGrafico.Size = formRect.Size; } } private void Representacion(ZedGraphControl zgcControl) { GraphPane Grafico = zgcGrafico.GraphPane;

//Títulos del gráfico Grafico.Title.Text = "Gráfico de segmentos circulares"; Grafico.Title.FontSpec.IsItalic = true; Grafico.Title.FontSpec.Size = 24f;

Page 3: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

Grafico.Title.FontSpec.Family = "Times New Roman";

//Fondo del gráfico y ejes Grafico.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f); Grafico.Chart.Fill.Type = FillType.None;

//Establecer la leyenda en una ubicación arbritraria Grafico.Legend.Position = LegendPos.Float; Grafico.Legend.Location = new Location(0.95f, 0.15f, CoordType.PaneFraction,

AlignH.Right, AlignV.Top); Grafico.Legend.FontSpec.Size = 10f; Grafico.Legend.IsHStack = false;

//Segmentos y creación del gráfico PieItem S1 = Grafico.AddPieSlice(20, Color.Navy, Color.White, 45f, 0, "1"); PieItem S2 = Grafico.AddPieSlice(40, Color.SandyBrown, Color.White, 45f, 0.2,

"2"); PieItem S3 = Grafico.AddPieSlice(30, Color.Purple, Color.White, 45f, .0, "3"); PieItem S4 = Grafico.AddPieSlice(10.21, Color.LimeGreen, Color.White, 45f, 0,

"4"); PieItem S5 = Grafico.AddPieSlice(250, Color.Red, Color.White, 45f, 0, "6"); PieItem S6 = Grafico.AddPieSlice(50, Color.Blue, Color.White, 45f, 0.2, "7"); PieItem S7 = Grafico.AddPieSlice(400, Color.Green, Color.White, 45f, 0, "8"); PieItem S8 = Grafico.AddPieSlice(50, Color.Yellow, Color.White, 45f, 0.2, "9");

S2.LabelDetail.FontSpec.FontColor = Color.Red;

CurveList curves = Grafico.CurveList; double total = 0; for (int x = 0; x < curves.Count; x++) total += ((PieItem)curves[x]).Value;

//Etiqueta del valor total TextObj text = new TextObj("Valor " + total.ToString(), 0.18F, 0.40F, CoordType.PaneFraction); text.Location.AlignH = AlignH.Center; text.Location.AlignV = AlignV.Bottom; text.FontSpec.Border.IsVisible = false; text.FontSpec.Fill = new Fill(Color.White, Color.FromArgb(255, 100, 100), 45F); text.FontSpec.StringAlignment = StringAlignment.Center; Grafico.GraphObjList.Add(text);

TextObj text2 = new TextObj(text); text2.FontSpec.Fill = new Fill(Color.Black); text2.Location.X += 0.008f; text2.Location.Y += 0.01f; Grafico.GraphObjList.Add(text2);

//Se asegura que el eje Y se reajustará para dar cabida a los datos reales zgcGrafico.AxisChange();

//Forzar un redibujo zgcGrafico.Invalidate(); } }}

Page 4: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

El resultado es el siguiente:

Page 5: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

Gráfico tabla circularusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

using ZedGraph; //Para poder usar los comandos relacionados con el gráfico

namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); Representacion(zgcGrafico); SetSize(); }

//Ajustar el tamaño y la ubicación del ZedGraphControl private void SetSize() { //El control está siempre insertado en 10 píxeles del rectángulo formulario Rectangle formRect = this.ClientRectangle; formRect.Inflate(-10, -10);

if (zgcGrafico.Size != formRect.Size) { zgcGrafico.Location = formRect.Location; zgcGrafico.Size = formRect.Size; } }

private void Representacion(ZedGraphControl zgcControl) { GraphPane Grafico = zgcGrafico.GraphPane;

//Títulos del gráfico Grafico.Title.Text = "Tabla gráfico circular\n (Unidades)";

//Algunos datos para implementar double[] values = { 15, 15, 40, 20 }; double[] values2 = { 250, 50, 400, 50 }; Color[] colors = { Color.Red, Color.Blue, Color.Green, Color.Yellow }; double[] displacement = { .0, .0, .0, .0 }; string[] labels = { "Dato 1", "Dato 2", "Dato 3", "Dato 4" };

//Rellenar el panel de fondo y el eje con un color sólido Grafico.Fill = new Fill(Color.Cornsilk); Grafico.Chart.Fill = new Fill(Color.Cornsilk); Grafico.Legend.Position = LegendPos.Right;

//Crear algunas porciones de tarta PieItem segment1 = Grafico.AddPieSlice(20, Color.Navy, .20, "Segmento 1"); PieItem segment2 = Grafico.AddPieSlice(40, Color.Salmon, 0, "Segmento 2");

Page 6: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

PieItem segment3 = Grafico.AddPieSlice(30, Color.Yellow, .0, "Segmento 3"); PieItem segment4 = Grafico.AddPieSlice(10.21, Color.LimeGreen, 0, "Segmento 4"); PieItem segment5 = Grafico.AddPieSlice(10.5, Color.Aquamarine, .3, "Segmento

5");

//Añadir las rodajas un poco más como una matriz PieItem[] slices = new PieItem[values2.Length]; slices = Grafico.AddPieSlices(values2, labels);

//Modificar los tipos de etiqueta de corte ((PieItem)slices[0]).LabelType = PieLabelType.Name_Value; ((PieItem)slices[1]).LabelType = PieLabelType.Name_Value_Percent; ((PieItem)slices[2]).LabelType = PieLabelType.Name_Value; ((PieItem)slices[3]).LabelType = PieLabelType.Name_Value; ((PieItem)slices[1]).Displacement = .2; segment1.LabelType = PieLabelType.Name_Percent; segment2.LabelType = PieLabelType.Name_Value; segment3.LabelType = PieLabelType.Percent; segment4.LabelType = PieLabelType.Value; segment5.LabelType = PieLabelType.Name_Value; segment2.LabelDetail.FontSpec.FontColor = Color.Red;

//Resumen los valores

CurveList curves = Grafico.CurveList; double total = 0; for (int x = 0; x < curves.Count; x++) total += ((PieItem)curves[x]).Value;

//Añadir un elemento de texto para poner de relieve las soluciones totales TextObj text = new TextObj("Total - " + "unidades" + total.ToString() +

"cantidad", 0.85F, 0.80F, CoordType.PaneFraction); text.Location.AlignH = AlignH.Center; text.Location.AlignV = AlignV.Bottom; text.FontSpec.Border.IsVisible = false; text.FontSpec.Fill = new Fill(Color.White, Color.PowderBlue, 45F); text.FontSpec.StringAlignment = StringAlignment.Center; Grafico.GraphObjList.Add(text);

//Añadir un fondo de color detrás de la tarta BoxObj box = new BoxObj(0, 0, 1, 1, Color.Empty, Color.PeachPuff); box.Location.CoordinateFrame = CoordType.ChartFraction; box.Border.IsVisible = false; box.Location.AlignH = AlignH.Left; box.Location.AlignV = AlignV.Top; box.ZOrder = ZOrder.E_BehindCurves; Grafico.GraphObjList.Add(box);

//Se asegura que el eje Y se reajustará para dar cabida a los datos reales zgcGrafico.AxisChange();

//Forzar un redibujo zgcGrafico.Invalidate(); } }}

Page 7: Uso del control ZedGraph en C# - Gráficos circulares

Uso del control ZedGraph con C# (Gráficos circulares)Aintzane Conde

El resultado es el siguiente: