Pie Chart Demo

From ZedGraphWiki

Jump to: navigation, search

NOTE: The code on this page is for ZedGraph version 5. You can view the code for version 4 here.


Image:piedemo.png

C# Sample Code

// Call this method from the Form_Load method, passing your ZedGraphControl
public void CreateChart( ZedGraphControl zgc )
{
   GraphPane myPane = zgc.GraphPane;

   // Set the GraphPane title
   myPane.Title.Text = "2004 ZedGraph Sales by Region\n($M)";
   myPane.Title.FontSpec.IsItalic = true;
   myPane.Title.FontSpec.Size = 24f;
   myPane.Title.FontSpec.Family = "Times New Roman";

   // Fill the pane background with a color gradient
   myPane.Fill = new Fill( Color.White, Color.Goldenrod, 45.0f );
   // No fill for the chart background
   myPane.Chart.Fill.Type = FillType.None;

   // Set the legend to an arbitrary location
   myPane.Legend.Position = LegendPos.Float;
   myPane.Legend.Location = new Location( 0.95f, 0.15f, CoordType.PaneFraction,
                  AlignH.Right, AlignV.Top );
   myPane.Legend.FontSpec.Size = 10f;
   myPane.Legend.IsHStack = false;

   // Add some pie slices
   PieItem segment1 = myPane.AddPieSlice( 20, Color.Navy, Color.White, 45f, 0, "North" );
   PieItem segment3 = myPane.AddPieSlice( 30, Color.Purple, Color.White, 45f, .0, "East" );
   PieItem segment4 = myPane.AddPieSlice( 10.21, Color.LimeGreen, Color.White, 45f, 0, "West" );
   PieItem segment2 = myPane.AddPieSlice( 40, Color.SandyBrown, Color.White, 45f, 0.2, "South" );
   PieItem segment6 = myPane.AddPieSlice( 250, Color.Red, Color.White, 45f, 0, "Europe" );
   PieItem segment7 = myPane.AddPieSlice( 1500, Color.Blue, Color.White, 45f, 0.2, "Pac Rim" );
   PieItem segment8 = myPane.AddPieSlice( 400, Color.Green, Color.White, 45f, 0, "South America" );
   PieItem segment9 = myPane.AddPieSlice( 50, Color.Yellow, Color.White, 45f, 0.2, "Africa" );

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

   // Sum up the pie values                                                               
   CurveList curves = myPane.CurveList;
   double total = 0;
   for ( int x = 0; x < curves.Count; x++ )
      total += ( (PieItem)curves[x] ).Value;

   // Make a text label to highlight the total value
   TextObj text = new TextObj( "Total 2004 Sales\n" + "$" + total.ToString() + "M",
                  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;
   myPane.GraphObjList.Add( text );

   // Create a drop shadow for the total value text item
   TextObj text2 = new TextObj( text );
   text2.FontSpec.Fill = new Fill( Color.Black );
   text2.Location.X += 0.008f;
   text2.Location.Y += 0.01f;
   myPane.GraphObjList.Add( text2 );

   // Calculate the Axis Scale Ranges
   zgc.AxisChange();

}

VB Sample Code

' Call this method from the Form_Load method, passing your ZedGraphControl
Private Sub CreateGraph(ByVal zgc as ZedGraphControl)
   Dim myPane As GraphPane = zgc.GraphPane

   ' Set the GraphPane title
   myPane.Title.Text = "2004 ZedGraph Sales by Region" & Chr(10) & "($M)"
   myPane.Title.FontSpec.IsItalic = True
   myPane.Title.FontSpec.Size = 24.0F
   myPane.Title.FontSpec.Family = "Times New Roman"

   ' Fill the pane background with a color gradient
   myPane.Fill = New Fill(Color.White, Color.Goldenrod, 45.0F)
   ' No fill for the chart background
   myPane.Chart.Fill.Type = FillType.None

   ' Set the legend to an arbitrary location
   myPane.Legend.Position = LegendPos.Float
   myPane.Legend.Location = New Location(0.95F, 0.15F, CoordType.PaneFraction, _
               AlignH.Right, AlignV.Top)
   myPane.Legend.FontSpec.Size = 10.0F
   myPane.Legend.IsHStack = False

   ' Add some pie slices
   Dim segment1 As PieItem = myPane.AddPieSlice(20, Color.Navy, Color.White, 45.0F, 0, "North")
   Dim segment3 As PieItem = myPane.AddPieSlice(30, Color.Purple, Color.White, 45.0F, 0.0, "East")
   Dim segment4 As PieItem = myPane.AddPieSlice(10.21, Color.LimeGreen, Color.White, 45.0F, 0, "West")
   Dim segment2 As PieItem = myPane.AddPieSlice(40, Color.SandyBrown, Color.White, 45.0F, 0.2, "South")
   Dim segment6 As PieItem = myPane.AddPieSlice(250, Color.Red, Color.White, 45.0F, 0, "Europe")
   Dim segment7 As PieItem = myPane.AddPieSlice(50, Color.Blue, Color.White, 45.0F, 0.2, "Pac Rim")
   Dim segment8 As PieItem = myPane.AddPieSlice(400, Color.Green, Color.White, 45.0F, 0, "South America")
   Dim segment9 As PieItem = myPane.AddPieSlice(50, Color.Yellow, Color.White, 45.0F, 0.2, "Africa")

   segment2.LabelDetail.FontSpec.FontColor = Color.Red

   ' Sum up the pie values                                                               
   Dim curves As CurveList = myPane.CurveList
   Dim total As Double = 0, i As Integer
   Dim pie As PieItem
   For i = 0 To curves.Count - 1
      pie = curves(i)
      total += pie.Value
   Next i

   ' Make a text label to highlight the total value
   Dim text As New TextObj("Total 2004 Sales" + Chr(10) + "$" + total.ToString() + "M", _
               0.18F, 0.4F, 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), 45.0F)
   text.FontSpec.StringAlignment = StringAlignment.Center
   myPane.GraphObjList.Add(text)

   ' Create a drop shadow for the total value text item
   Dim text2 As New TextObj(text)
   text2.FontSpec.Fill = New Fill(Color.Black)
   text2.Location.X += 0.008F
   text2.Location.Y += 0.01F
   myPane.GraphObjList.Add(text2)

   ' Calculate the Axis Scale Ranges
   zgc.AxisChange()
End Sub

Go back to Sample Graphs

Personal tools