Multi-Colored Bar Demo

From ZedGraphWiki

Jump to: navigation, search

Image:multi_colored_bar.png

C# Code Sample

// Call this method from the Form_Load method, passing your ZedGraphControl
private void CreateGraph_GradientByZBars( ZedGraphControl z1 )
{
   GraphPane myPane = z1.GraphPane;
   myPane.Title.Text = "Demonstration of Multi-Colored Bars with a Single BarItem";
   myPane.XAxis.Title.Text = "Bar Number";
   myPane.YAxis.Title.Text = "Value";

   PointPairList list = new PointPairList();
   Random rand = new Random();

   for ( int i = 0; i < 16; i++ )
   {
      double x = (double)i+1;
      double y = rand.NextDouble() * 1000;
      double z = i / 4.0;
      list.Add( x, y, z );
   }

   BarItem myCurve = myPane.AddBar( "Multi-Colored Bars", list, Color.Blue );
   Color[] colors = { Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple };
   myCurve.Bar.Fill = new Fill( colors );
   myCurve.Bar.Fill.Type = FillType.GradientByZ;

   myCurve.Bar.Fill.RangeMin = 0;
   myCurve.Bar.Fill.RangeMax = 4;

   myPane.Chart.Fill = new Fill( Color.White, Color.FromArgb( 220, 220, 255 ), 45 );
   myPane.Fill = new Fill( Color.White, Color.FromArgb( 255, 255, 225 ), 45 );
   // Tell ZedGraph to calculate the axis ranges
   z1.AxisChange();
}

VB Code Sample

'Call this method from the Form_Load method, passing your ZedGraphControl. 
'Dont forget to import ZedGraphControl.
Private Sub CreateGraph_GradientByZBars(z1 As ZedGraphControl)
   Dim myPane As GraphPane = z1.GraphPane
   myPane.Title.Text = "Demonstration of Multi-Colored Bars with a Single BarItem"
   myPane.XAxis.Title.Text = "Bar Number"
   myPane.YAxis.Title.Text = "Value"
   
   Dim list As New PointPairList()
   Dim rand As New Random()
   
   Dim i As Integer
   For i = 0 To 15
      Dim x As Double = CDbl(i) + 1
      Dim y As Double = rand.NextDouble() * 1000
      Dim z As Double = i / 4.0
      list.Add(x, y, z)
   Next i
   
   Dim myCurve As BarItem = myPane.AddBar("Multi-Colored Bars", list, Color.Blue)
   Dim colors As Color() =  {Color.Red, Color.Yellow, Color.Green, Color.Blue, Color.Purple}
   myCurve.Bar.Fill = New Fill(colors)
   myCurve.Bar.Fill.Type = FillType.GradientByZ
   
   myCurve.Bar.Fill.RangeMin = 0
   myCurve.Bar.Fill.RangeMax = 4
   
   myPane.Chart.Fill = New Fill(Color.White, Color.FromArgb(220, 220, 255), 45)
   myPane.Fill = New Fill(Color.White, Color.FromArgb(255, 255, 225), 45)
   ' Tell ZedGraph to calculate the axis ranges
   z1.AxisChange()
End Sub 'CreateGraph_GradientByZBars 

Go back to Sample Graphs

Personal tools