Search code examples
c#colorszedgraph

How can I set different colors in a ZedGraph histogram?


I got a histogram drawn in ZedGraph. And I have to set the specific color for a specific range of the values. For example:

Graph Pane = zedGraph.GraphPane;    
list = new PointPairList ();    
for (int i = 0; i < 256; i++)
{    
    list.Add(i, array_with_y_values[i]);    
}    
Pane.AddBar("", list, Color.Red);

And how I can set the other color for some of them?


Solution

  • Are you looking for something like this? This piece of code adds 50 bars with random y values between 0 and 15. It will color bars with y values <5 as red, 5-10 as yellow, and >10 as green.

    GraphPane pane = zedGraphControl1.GraphPane;
    PointPairList list = new PointPairList();
    Random rand = new Random();
    
    for (int i = 0; i < 50; i++)
    {
        list.Add(i, rand.Next(15));
    }
    
    BarItem myBar = pane.AddBar("", list, Color.Red);
    Color[] colors = { Color.Red, Color.Yellow, Color.Green };
    myBar.Bar.Fill = new Fill(colors);
    myBar.Bar.Fill.Type = FillType.GradientByY;
    myBar.Bar.Fill.RangeMin = 5;
    myBar.Bar.Fill.RangeMax = 10;
    
    zedGraphControl1.AxisChange();
    

    This is a modified example of the ZedGraph one here: http://www.zedgraph.org/wiki/index.php?title=Multi-Colored_Bar_Demo