Search code examples
wpfhistogramteechart

TeeChart WPF Histogram color of bins


I am using TeeChart and the Histogram Series to display data. I would like to color the bins individually depending on the values but all I found was the option to color each one differently. I want the bins displaying the same value to have the same color. Is that possible with TeeChart?


Solution

  • Yes, this is possible. You can achieve that either providing the color value when populating the series using the appropriate Add method override or using the BeforeDrawPoint event as shown here:

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }
    
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
    
      Histogram histogram1 = new Histogram(tChart1.Chart);
    
      histogram1.LinePen.Visible = false;
      histogram1.LinesPen.Visible = false;
    
      for (int i = 0; i < 20; i++)
      {
        histogram1.Add(i);
      }
    
      histogram1.BeforeDrawPoint += histogram1_BeforeDrawPoint;
    }
    
    void histogram1_BeforeDrawPoint(Series series, BeforeDrawPointEventArgs e)
    {
      series.Colors[e.ValueIndex] = (series.YValues[e.ValueIndex] > 10) ? Color.Red : Color.Blue;
    }