A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: generic rounded values

  1. #1
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472

    generic rounded values

    OK, I seriously need help

    Based on a maxValue I'm looking for a way to make some nicely rounded numbers to display in a chart.

    Let me give some examples of what I mean.

    maxValue = 61029
    // returns
    10000 - 20000 - 30000 - 40000 - 50000 - 60000

    maxValue = 412
    // returns
    100 - 200 - 300 - 400

    maxValue = 7
    // returns
    1 - 2 - 3 - 4 - 5 - 6

    maxValue = 0.45
    // returns
    0.1 - 0.2 - 0.3 - 0.4

    Any clues out there???
    //pod

    If it is any help I found this proto
    *************
    Number.prototype.roundTo = function(num) {
    var resto = this%num;
    if (resto <= (num/2)) {
    return this-resto;
    } else {
    return this+num-resto;
    }
    }
    ************

  2. #2
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    Here's something I threw together that seems to do the trick.
    code:

    function fixNums(maxVal){
    myString = "";
    if(maxVal > 1){
    for(i = 0;true;i ++){
    if(maxVal < Math.pow(10,i+1)){
    for(n = 1;n <= Math.floor(maxVal/Math.pow(10,i));n ++){
    myString += n*Math.pow(10,i)+" - ";
    }
    return myString;
    }
    }
    }else{
    for(i = -1;true;i --){
    if(maxVal > Math.pow(10,i)){
    for(n = 1;n <= Math.floor(maxVal/Math.pow(10,i));n ++){
    myString += n*Math.pow(10,i)+" - ";
    }
    return myString;
    }
    }
    }
    }



    Hope this helps

    Neal

  3. #3
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472
    Hey,

    I've been superbusy so I haven't gotten round to thank you.
    Well thank you ... it works like a charm.

    I've modified it at bit so that it returns an array
    Code:
    // proto that returns an array of genericly rounded values based on maxVal
    Array.prototype.getValueArray=function(maxVal){
    	var maxVal=maxVal
    	var arrTemp = new Array()
    	arrTemp[0]=0
    	if(maxVal > 1){
    		for(i = 0;true;i ++){
    			if(maxVal < Math.pow(10,i+1)){
    				for(n = 1;n <= Math.floor(maxVal/Math.pow(10,i));n ++){
    					myString += n*Math.pow(10,i)+" - ";
    					arrTemp.push(n*Math.pow(10,i))
    				}
    				return arrTemp;
    			}
    		}
    	}else{
    		for(i = -1;true;i --){
    			if(maxVal > Math.pow(10,i)){
    				for(n = 1;n <= Math.floor(maxVal/Math.pow(10,i));n ++){
    					myString += n*Math.pow(10,i)+" - ";
    					arrTemp.push(n*Math.pow(10,i))
    				}
    				return arrTemp;
    			}
    		}
    	}
    }
    Can you help me out with returning an array based on both a maxValue and a minValue?

    The minimum value should be able to handle negative values.

    Thanks again ... hope to hear from you
    //poden

  4. #4
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    Glad you liked it.

    I'm not entirely sure how the minimum value would affect the output. Why don't you give a few examples like you did in your first post?

    Neal

  5. #5
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472
    Hi Nialsh

    ... your karma-rating is rocketing

    Here are som examples of min and max values and the desired output

    minValue = -11000
    maxValue = 61029
    // returns
    -10000, 0, 10000, 20000, 30000, 40000, 50000, 60000

    minValue = -742
    maxValue = 412
    // returns
    -700, -600, -500, -400, -300, -200, -100, 0, 100, 200, 300, 400

    minValue = -1
    maxValue = 7
    // returns
    -1, 0, 1, 2, 3, 4, 5, 6

    minValue = -0.45
    maxValue = 0.45
    // returns
    -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4

    //poden

  6. #6
    Bacon-wrapped closures Nialsh's Avatar
    Join Date
    Dec 2003
    Location
    Houston!
    Posts
    338
    Flash 7 doesn't seem to like your code (or maybe I'm too ignorant to have learned how to use prototype and v7 neglects to say anything about it except that it's archaic) so I reverted back to my function. I cleaned it up too because it started getting confusing.

    code:

    function fixNums(maxVal,minVal){
    my_array = [];
    if(maxVal > 1){
    for(i = 0;maxVal > Math.pow(10,i+1);i ++);
    }else{
    for(i = -1;maxVal < Math.pow(10,i);i --);
    }
    for(n = Math.floor(maxVal/Math.pow(10,i));n*Math.pow(10,i) >= minVal;n --){
    my_array.unshift(n*Math.pow(10,i));
    }
    return my_array.toString();
    }



    Seems to work for me. I'm interested to know what you're doing with it.

    Neal

  7. #7
    Bob (the singing sock)
    Join Date
    Aug 2000
    Location
    Århus, Denmark
    Posts
    472
    Hey Nialsh,

    Thanks a kazillion for your code example ... works like a charm.

    The project I'm working on deals with statistical data in different geografical areas and involves ...

    - drawing a map based on global coordinates loaded from text file data.
    - loading different statistical data from text files and calculating 5 segments, min and max values for each theme. These text file hold references to ID's in the map file.

    - a generic slider that lets you change the current period. The selected data file then colors the different areas on the map accordingly.

    - User settings that allows the user to change graf types (bar, pie. line)

    - Scatterplots that combines 2 data files on the x and y axis.


    Your code is very usefull for displaying x and y value axis on diffenrebt grafs. All in all it's a pretty huge project that is now finished with your help.

    I can send you some screendumps if you are interested?

    Best regards,
    Poden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center