473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

vertical histogram in javascript

All the information is asked for. so the number of values is unknown, the data points are unknown and so forth. this is what i have thus far...




Expand|Select|Wrap|Line Numbers
  1. var array = userValues;
  2. maximumValue(array);
  3. var maxRows = max;
  4. var maxCols = valuesToPlot.length;
  5. var secondArray = createTwoDimArray(maxRows, maxCols);                                
  6.  
  7.  

the number of values and the datas points have been stored in the userValues array.
Jan 19 '10 #1
7 3911
RamananKalirajan
608 512MB
Can you please elaborate the problem you are facing with this code?

Thanks and Regards
Ramanan Kalirajan
Jan 19 '10 #2
I don't know how/what to do next.
for example: given 5 entries with the following data points [2,3, 4, 5, 6] we are suppose to make a vertical histogram using functions made [maximum value of array and createTwoDimensionalArray]
Jan 19 '10 #3
gits
5,390 Expert Mod 4TB
given that the array would be:

Expand|Select|Wrap|Line Numbers
  1. [2,2,3,4,5,5,6]
  2.  
the output should look like:

Expand|Select|Wrap|Line Numbers
  1. 2     *     *
  2. 1     * * * * *
  3.   0 1 2 3 4 5 6
  4.  
or what should be produced?

kind regards
Jan 19 '10 #4
given that the array would be

[2,2,3,4,5,5,6]

the output would be

**
**
***
****
*****
*****
******

but vertical.. once vertical, reading from left to right there is a positive slope
Jan 19 '10 #5
gits
5,390 Expert Mod 4TB
so basicly you need to sort the array ascending and then transform it to a 'vertical' structure ... that would mean you could do something like the following steps:

1. sort the array - have a look at the array's sort() method
2. the last value in the array should then be the max

now you could produce:

Expand|Select|Wrap|Line Numbers
  1. a0 = [' ', ... , '*']; // where the length == length of the base array
  2. ...
  3. an = ['*', ... , '*']; // where n == max value of the base array
  4.  
then display a0 ... an linewise on the page ... for a simple line concat for every array you might use the join()-method.

kind regards
Jan 19 '10 #6
it doesnt have to be ascending though and I am not allowed to use join method or any method.

i am having problems in creating the two dimensional array right now to create the histogram.

this is what i have so far

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. function createTwoDimArray(maxRows, maxCols) {
  4.      var newArray = new Array(maxRows);
  5.      for (var row=0; row<maxRows; row++) {
  6.                newArray[row] = new Array(maxCols);
  7.      }
  8.      return newArray;
  9. }        
  10.  
  11.  
  12. function createArrayVerticalGraph(valuesToPlot, symbol) {
  13.                                 var array = valuesToPlot;
  14.                                 var maxRows = maximumValue(array);
  15.                                 var maxCols = valuesToPlot.length;
  16.                                 var secArray = createTwoDimArray(maxRows, maxCols);
  17.                                 var secArray = newArray[row];
  18.                                 var row = (maxRows - 1);
  19.                                 for (col = 0; col < secArray[row].length; col++) {
  20.                                         for (value = valuesToPlot[col]; value > 0; value--) {                                           
  21.                                                 secArray[row][col] = symbol;
  22.                                                 row--;
  23.                                             }
  24.                                     }
  25.                                 return secArray;
  26.                         }
  27.  
  28.  
  29.  
Jan 19 '10 #7
gits
5,390 Expert Mod 4TB
basicly it could look like this - i just use the document.write() method for simplicity ... it shouldn't be used in reality for such purposes:

Expand|Select|Wrap|Line Numbers
  1. var a = [2,2,3,7,4,5,6];
  2.  
  3. function createPlotArray(a) {
  4.     var plotArray       = [];
  5.     var plotArrayLength = 0;
  6.  
  7.     var check = function(arr, max) {
  8.         var ret = [];
  9.  
  10.         for (var k = 0, l = arr.length; k < l; ++k) {
  11.             var r = arr[k];
  12.             var val = r >= max ? '*' : ' ';
  13.             ret.push(val);
  14.         }
  15.  
  16.         return ret;
  17.     };
  18.  
  19.     for (var i = 0, l = a.length; i < l; ++i) {
  20.         if (a[i] > plotArrayLength) {
  21.             plotArrayLength = max = a[i];
  22.         }
  23.     }
  24.  
  25.     for (var j = 0; j < plotArrayLength; ++j) {
  26.         plotArray.push(check(a, max).join(''));
  27.         max--;
  28.     }
  29.  
  30.     return plotArray;
  31. }
  32.  
  33. var p = createPlotArray(a);
  34.  
  35. document.write('<pre>');
  36.  
  37. for (var i = 0, l = p.length; i < l; ++i) {
  38.     document.write(p[i] + '<br/>');
  39. }
  40.  
  41. document.write('</pre>');
  42.  
when you need to avoid the join() then use an iteration for that step.
Jan 19 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Oracle3001 | last post by:
Hi All, I am trying to use JAI to build a histogram of an image i have. I have posted the code below, and the error I get at runtime. I have taken the code from the offical java examples, so I am...
27
by: ext_u | last post by:
Ok I thought I would try to take the program one thing at a time. (If you remember my last post I am trying to make a histogram with data on the size of each word) Anways first .. I obviously...
1
by: P2P | last post by:
Hi Does anyone know a simple cross-broswers vertical scrolling javascript that can call up its scrolling contents off an external html page/file? Much appreciated -- P2P...
4
by: Greg | last post by:
I'm guessing the problem I'm having has something to do with Master Pages or DetailsView because the exact same code works fine on a page without a Master Page and DetailsView controls. The...
11
by: c19h28o2 | last post by:
Hi, Guy's I know there are several posts about this, however I do not want to read them as answers are undoubtedly posted! Here is my attempt but I'm slightly stuck. I'm not looking for the...
12
by: arnuld | last post by:
i was able to create a solution for a Horizontal-Histogram. i was completely unable to understand this Vertical-Histogram phenomenon. even though i have looked at the solution at this page: ...
4
by: sindhu | last post by:
Hello please some one tell me how to make a menu appear horizontal or vertical in javascript and css. I'm bugged up after searching for one factor that makkes a menu8 appear horizontal or vertical...
15
by: zaturn | last post by:
To student: The this the new assignment. Ex7 VERTICAL HISTOGRAM
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.