You are on page 1of 7

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde

Uso del control ZedGraph en C# Grficos circulares

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde

Uso del control ZedGraph en C# Grficos circulares


Vamos a ver algn ejemplo de los grficos circulares que podemos implementar. En estos ejemplos se sigue la dinmica del documento Uso del control ZedGraph en C#, siendo la nica diferencia la que se establece en el cdigo:

Grfico circular
using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

using ZedGraph; //Para poder usar los comandos relacionados con el grfico namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Representacion(zgcGrafico); SetSize(); } //Ajustar el tamao y la ubicacin del ZedGraphControl private void SetSize() { //El control est siempre insertado en 10 pxeles del rectngulo 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; //Ttulos del grfico Grafico.Title.Text = "Grfico de segmentos circulares"; Grafico.Title.FontSpec.IsItalic = true; Grafico.Title.FontSpec.Size = 24f;

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde


Grafico.Title.FontSpec.Family = "Times New Roman"; //Fondo del grfico y ejes Grafico.Fill = new Fill(Color.White, Color.Goldenrod, 45.0f); Grafico.Chart.Fill.Type = FillType.None; //Establecer la leyenda en una ubicacin 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 creacin del grfico 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();

} } }

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde El resultado es el siguiente:

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde

Grfico tabla circular


using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

using ZedGraph; //Para poder usar los comandos relacionados con el grfico namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); Representacion(zgcGrafico); SetSize(); } //Ajustar el tamao y la ubicacin del ZedGraphControl private void SetSize() { //El control est siempre insertado en 10 pxeles del rectngulo 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; //Ttulos del grfico Grafico.Title.Text = "Tabla grfico 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 slido 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");

Uso del control ZedGraph con C# (Grficos 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"); //Aadir las rodajas un poco ms 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; //Aadir 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); //Aadir un fondo de color detrs 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(); } } }

Uso del control ZedGraph con C# (Grficos circulares) Aintzane Conde El resultado es el siguiente:

You might also like