Page 1 of 1

Histogram Queries

Posted: Wed Nov 12, 2008 5:44 am
by 14047415
Hi,

Thanks for your previous reply. My queries related to the Cursor and the SetMinMax were solved.But the query related to NumOfBins is not yet solved.

1.I have Uploaded HistogramTestApplication.zip which contains a sample code, where i have tried changing the NumOfBins for the Histogram and it is not reflecting the change.
Could you please look into the above code.

2.Could you also explain how the calculation is done for various NumOfBins.I tried looking into the What's New?\Welcome !\New Functions\Histogram Function but since the data is not visible i was not able to understand the calculation.

Thanks
Sanyog.

Posted: Wed Nov 12, 2008 9:35 am
by narcis
Hi Sanyog,
1.I have Uploaded HistogramTestApplication.zip which contains a sample code, where i have tried changing the NumOfBins for the Histogram and it is not reflecting the change.
Could you please look into the above code.
As you can see in the example I posted here, you need a specific series to plot the function and another series as datasource. I strongly recommend you to read Tutorial 7 - Working with Functions. Tutorials can be found at TeeChart's program group. Therefore, you can modify PlotButton_Click method in your example like this:

Code: Select all

            this.HistogramClip.Add(x, y);
						this.HistogramClip.Function = null;

						Steema.TeeChart.Styles.Histogram FuncSeries = new Steema.TeeChart.Styles.Histogram(tChart1.Chart);
						FuncSeries.Function = histogramFunction1;
						FuncSeries.DataSource = HistogramClip;

						HistogramClip.Active = false;
2.Could you also explain how the calculation is done for various NumOfBins.I tried looking into the What's New?\Welcome !\New Functions\Histogram Function but since the data is not visible i was not able to understand the calculation.
Public Histogram procedure constructs bins (centerpoints) and each bin counts from data (implementation bellow). The lb,ub parametes are data lower and upper bound, min, max are (usually) data minimum and maximum value:

Code: Select all

    public static void Histogram(double[] data, int lb, int ub, ref double[] bins, ref double[] counts, int nbins, double min, double max)
    {
        // In case minimum is equal to maximum
        if (min==max)
        {
            min = min - System.Math.Floor(0.5*nbins) - 0.5;
            max = max + System.Math.Ceiling(0.5*nbins) + 0.5;
        }

        double range = max - min;
        double binwidth = range/(double)nbins;
        double invbinwidh = (double)nbins/range;

        // Setup bins centerpoints and count for each bin
        for (int i=0; i<nbins; i++)
        {
            bins[i] = min + 0.5 * binwidth + binwidth*i;
            counts[i] = 0;
        }

        // pretty fast, but can be a bit inaccurate if values are in range of SQRT(EPS)
        int j;
        int righttail = 0;
        for (int i = lb; i<=ub; i++)
        {
            j = (int)((data[i]-min)*invbinwidh);
            if ((j>=0) && (j<nbins)) counts[j] += 1;
            else if (j>=nbins) righttail += 1;
        }
        counts[nbins-1] += righttail;
    }