0

I have the following code that gets data from a database and plots a graph using HTML and the FusionCharts library. I'm just having some trouble with the graph formatting. I want the yAxisMin to be 10 values below the minimum value in the data array and the yAxisMax value to be 10 values above.

function createJSON(name, data){
    return(
        {
            "chart": {
                "caption": String(name),
                "subCaption": "15-Day Prices",
                "xAxisName": "Day",
                "yAxisName": "Price (USD)",
                "anchorRadius": "1",
                "showValues": "0",
                "borderThickness": "3",
                "yAxisMinValue": data.value.min() - 10,
                "yAxisMaxValue": data.value.max() + 10,
                "theme": "fint"
            },
            "data": data,
        }
    );
}

//I'm including this for context. This is the function that returns the data array
var getHistory = function(req,res,next){

    var data = [];
    History.findOne({"Symbol": String(req.params.sym)}, function(err, foundHistory){
        if(foundHistory){
            (foundHistory.Historical).forEach(function(day){
                data.push({
                    "label": day.Date,
                    "value": day.Close
                });
            });
            res.json(createJSON(foundHistory.Name, data));
            next();
        }
        else{
            next();
        }
    }); 
};


module.exports = getHistory;

Everything else works fine. I just need help with the two lines of code:

"yAxisMinValue": data.value.min() - 10,
"yAxisMaxValue": data.value.max() + 10,

How would I do this? This was my attempt but it does not compile.

0

3 Answers 3

1

How about this? Since your code sample is incomplete, I cannot test it myself.

function createJSON(name, data){
  var min = Number.MAX_SAFE_INTEGER;
  var max = Number.MIN_SAFE_INTEGER;
  for(var i = 0; i < data.length; i++) {
    if(data[i].value < min) min = data[i].value;
    if(data[i].value > max) max = data[i].value;
  }
  ...
      "yAxisMinValue": min - 10,
      "yAxisMaxValue": max + 10,
  ...
Sign up to request clarification or add additional context in comments.

4 Comments

Still gives me an error but thank you for the help! I'll try to find the error
What is the desired output and actual output? 'max does not' says nothing. I updated the code a few times. Please check again.
Min value is 110 and the graph min-y-value is correctly adjusting to 100. The max value is 150 but the graph just displays 150 instead of 160.
0

For 1-Dimensional Array of Numbers:

JavaScript's Math.max and Math.min will find the maximum or minimum of their arguments respectively, so, using an apply call, we can apply the array of numbers to these functions.

var values = [ 1, 2, 3, 4, 5 ]
Math.max.apply(null, values) // 5
Math.min.apply(null, values) // 1

For Anything Else:

If you want the index of the maximum or are comparing non-numeric values, a custom iterator is needed.

function getMax (values) 
  var maxIndex = -1
  var max = Number.MIN_VALUE // use Number.MAX_VALUE for min

  for (var i = 0; i < values.length; i++) {
    if (value[i] > max) { // do your "greater than" comparison here
      maxIndex = i
      max = value[i]
    }
  }

  // return what it is you need, max or maxIndex
  return max
end 

3 Comments

Does it matter that my array is 2 dimensional? I don't think this would work in that case
In that case, no. You would have to iterate over the array in question, storing the index (or the value itself) of the max/min. I will edit the answer to have this option as well.
This way too, to get min and max takes two loops, which can be done in just one loop.
0

Try something like that:

  "yAxisMinValue": getMinValue(data) - 10,
    "yAxisMaxValue": getMaxValue(data) + 10,   


 function getMinValue(data)
     {
       var min = Number.MAX_VALUE;
        for (var key in data)
         {
           if(data[key].value < min)
            {
               min = data[key].value;
            }
         }
        return min;
     }


 function getMaxValue(data)
     {
       var max = Number.MIN_VALUE;
        for (var key in data)
         {
           if(data[key].value > max)
            {
               max = data[key].value;
            }
         }
        return max;
     }

7 Comments

Idk what type is day.Close. If it isnt numeric, you have to check min in other way.
day.Close is a floating point number. I tried running this function and it returns -9999 + or - 10.
Obviously, no max calculation here.
Max has the same logic.
I tried this out and it works! Only thing is, the max value + 10 is returning just the max value and I can't seem to find the error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.