ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Histograms
Posted by Ron on Sep-18-2008 23:11
Hello,

Which is the best way to create a histogram chart with ChartDirector.  I want to be able to pass values to the chartdir module and create graphs on the fly.  I need the x and y axis to be variables.  I want to set the x-axis as frecuency (within a range) and the y-axis as time

Can this be done?

Thanks,
Ron

  Re: Histograms
Posted by Tom C. on Sep-19-2008 02:06
I don't know if this is the most efficient way or not, but in .NET (C#) I'm using the following to create a histogram:

            System.Data.IDbCommand sqlCmd = myConnection.CreateCommand();
                sqlCmd.CommandText = sqlHistData;

                // Read the data into the DBTable object
                DBTable table = new DBTable(sqlCmd.ExecuteReader());

                myConnection.Close();

                // Get the data as arrays
                double[] timestamp = table.getCol(0);
                double[] HistData = table.getCol(3);

                double[] tempCnt1 = new double[6];
                int tempStep1 = 0;
                int tempStep2 = 0;
                int tempStep3 = 0;
                int tempStep4 = 0;
                int tempStep5 = 0;
                int tempStep6 = 0;

                for (int i = 0; i < HistData.Length; i++)
                {

                    if (HistData[i] <= 940)
                    {
                        tempStep1++;
                    }
                    else if (HistData[i] > 940 & HistData[i] <= 950)
                    {
                        tempStep2++;
                    }
                    else if (HistData[i] > 950 & HistData[i] <= 960)
                    {
                        tempStep3++;
                    }
                    else if (HistData[i] > 960 & HistData[i] <= 970)
                    {
                        tempStep4++;
                    }
                    else if (HistData[i] > 970 & HistData[i] <= 980)
                    {
                        tempStep5++;
                    }
                    else if (HistData[i] > 980)
                    {
                        tempStep6++;
                    }
                }

                tempCnt1[0] = tempStep1;
                tempCnt1[1] = tempStep2;
                tempCnt1[2] = tempStep3;
                tempCnt1[3] = tempStep4;
                tempCnt1[4] = tempStep5;
                tempCnt1[5] = tempStep6;

                // Set the plotarea at (60, 40) and of size 480 x 280 pixels. Use a vertical
                // gradient color from light blue (eeeeff) to deep blue (0000cc) as background.
                // Set border and grid lines to white (ffffff).
                c.setPlotArea(50, 30, 350, 200, 0xffffff, 0xffffff, 0xc0c0c0, 0xc0c0c0, 0xc0c0c0);

                // Add a title to the chart using 18pts Times Bold Italic font
                c.addTitle("Line " + lineSel + " " + chrtTitle, "Times New Roman Bold", 12);

                c.yAxis().setLinearScale(lowerLimit, upperLimit, scaleInc);

                // Add a multi-color bar chart layer using the supplied data
                BarLayer layer = c.addBarLayer2(Chart.Stack, 8);


I'm just looping through the DBTable column that has the data I want to set the histogram up for and fill an array by going through an if-else statement.  I then use this array for my histogram.  I hope this helps.

- Tom

  Re: Histograms
Posted by Peter Kwan on Sep-19-2008 02:15
Attachments:
Hi Ron,

In ChartDirector, a histogram is just an application of a normal bar chart.

Consider the "Simple Bar Chart" attached in this message (it comes from the "Simple Bar Chart" sample code that is included in ChartDirector). If you change the x-axis label to "0 - 4", "5 - 9", "10 - 14", "15 - 19", "20 - 24", etc., then it becomes a histogram.

There is also a more complicated example in ChartDirector - the "Dual Horizontal Bar Charts" - which shows a demographic chart. You can see that it is two histograms side by side.

Hope this can help.

Regards
Peter Kwan
simplebar.png

  Re: Histograms
Posted by Ron on Sep-20-2008 01:35
Thanks for the help.  I do have another question....can I set the Y-Axis as a variable and not a "Label"?  I am creating multiple .png's in a for loop and want to use [Key:Value] pairs in Python to populate the X-Axis (number of occurances of a Value) Y-Axis (time in seconds of Values where I would then calculate the Bin and pass it to ChartDirecotor.

Thoughts?

Ron

  Re: Histograms
Posted by Peter Kwan on Sep-20-2008 05:42
Hi Ron,

I am not too sure the exact data structure you are using. Would you mind to clarify what data you are planning to pass to ChartDirector?

To create a bar chart, ChartDirector needs the following data:

(a) A list of numbers (as the height of the bars)

(b) (Optional) A list of numbers, or dates, or labels, to be used on the horizontal axis or as the position of the bars.

May be you can provide me an example of (a) and (b) that you want to pass to ChartDierctor. (Just a random sequence of 5 elements are sufficient.)

Hope this can help.

Regards
Peter Kwan

  Re: Histograms
Posted by Ron on Sep-22-2008 21:13
Hi Peter,

As mentioned, I want to create a dynamic histogram that a user can access via out internal intranet.  I want to display a Histogram that displayes, the number of transactions (displayed as bars) on the X-Axis and the Historgams goups (bins) in seconds ie: "1-5" secs on the Y-Axis.

Therefore it would look like: '108' transactions happened between "1-5" secs.  So the X-Axis would display a bar at the value of 108 and the Y-Axis ("label") would be "1-5".

I need to make the X and Y Axis dynamic where the value(s) of X would passed into ChartDir and the values for the Y ("labels" and amount of "labels or bars)would also be passed into CharDir as well.  In short, both X and Y would be passed "data" and that '108' on the X-Axis is married with "1-5" on the Y-Axis.

Thanks again,
Ron

  Re: Histograms
Posted by Peter Kwan on Sep-23-2008 02:25
Hi Ron,

In all ChartDirector sample code, the data and labels are always variables. For example, in the "Simple Bar Chart" sample code that comes with ChartDirector, the code is like (in VB/VBScript):

' Add a bar chart layer using the given data
Call c.addBarLayer(data)

' Set the labels on the x axis.
Call c.xAxis().setLabels(labels)

The "data" and "labels" above are VBScript variables. If you are using other programming language, then they should be variables in your programming language.

In the sample code, the variables are initialized with hard coded data. In your real code, you would need to use your own method to set up the variables. For example, you can read data from a database to fill the variables, or load data from a file to the variables, or fill the variables based on user input, etc..

After your code have set up the variables, you can pass the variables to ChartDirector to create charts.

Hope this can help.

Regards
Peter Kwan

  Re: Histograms
Posted by Surya on Oct-27-2011 18:34
Peter,
i am having set of values related to packet delay in network. I would like to see the packet delay count for different ranges, say 1-10ms, 10-20ms, 20-30ms and so on.
I saw example code where we need to calculate the count in the application and provide the count for each range to chart director to display count against its range in brachart.

Is there any way that chartDirector itself can calculate frequency in each range and plot if we simply provide whole data set and interval?

My requirement is like this.
I have a set of values in the range of 1-100.
dataset ()={23,45,56,1,2,45,78,90,53,27,17,82,85}. I would like to have a plot showing the frequency for the interval width of 10.

1-10    2
10-20  1
20-30  2
30-40 0
40-50 2
...
80-90 3
I don't want to do the frequency calculation in my application.


Thanks,
Surya

  Re: Histograms
Posted by Peter Kwan on Oct-28-2011 03:59
Hi Surya,

For most programming languages, it only takes 2 or 3 lines of simple code to compute the frequency. For example, for your case, in Java, it is:

double[] frequency = new double[10];
for (int i = 0; i < data.length; ++i) ++(frequency[(data[i] - 1) / 10]);

Hope this can help.

Regards
Peter Kwan

  Re: Histograms
Posted by joaquin lopez on Nov-20-2015 03:11
In my case I use code from this link
http://www.advsofteng.com/doc/cdnetdoc/histogram.htm to compute histogram; I adapt some part of the example with this code to hand decimal values:

double minX = m.min();
            double maxX = m.max();
            double slotSize =  (maxX - minX) / 30;
            if (slotSize <= 0) return;




            // We can now determine the number of slots
            int slotCount = (int)((maxX - minX) / slotSize)+1;
            if (slotCount < 0) return;
            double[] frequency = new double[slotCount];

            double xval=minX;
            int xIndex=0;
            while (xval< maxX)
            {
                var f = from d in samples where d >= xval && d < xval+slotSize select d;
                //labels[]
                frequency[xIndex] = f.Count();
                xIndex++;
                xval += slotSize;
            }