0

I implemented a Javascript function ('setData()') and using another function ('getDataSet(data, id)') it creates the final code. getDataSet(data, id) will be called any number of times and gathering all the data sets, setData() function will be create the chart.

    function getDataSet(data, id){
        //data is a object and name is a element ID
        //do something
    }

    function setData(){
        var chartName;
        var x = new Array;
        for (var i = 0; i < arguments.length; i++) {
            if (i != 0) {
                console.log(arguments[i]);
                x.push(arguments[i]);
            }
            else{
                console.log(arguments[0]);
                chartName = arguments[0];
            }
        }
    }

When i use as follows it works.

setData("viewPoint", getDataSet(x, 'step1'), getDataSet(y, 'step2'), getDataSet(z, 'step3'));

But the number of calls to getDataSet(data, id) function will be change, according to the number of data sets. Number of arguments to setData() function will be change. There for I implemented showCharts() function as follows.

    //Main function
    function showCharts(){ // <- Not working function

        var objects;

        // Run an array to retrieve data and using them need to call getDataSet()
        // finally add their return values as arguments into setData()

        objects += getDataSet(data, id) + ","; // <- create data sets separatly

        setData(objects);   // <- show all data sets in one chart
    }

But still can't figure out how to Pass an Unknown Number of objects as arguments into showCharts() function. Please help me.

Here is the real code. Line #132.

3
  • Use an array instead of a large number of arguments, is probably your best bet. Commented Aug 12, 2022 at 5:21
  • Use logic to form a JSON object instead. For eg: { "viewPoint" : { "step1" : [...DataSetValuesArrayStep1..] , "step2" : [...DataSetValuesArrayForStep2...] } } Commented Aug 12, 2022 at 5:28
  • 1
    You can use dotted notation like: function showCharts(...args). Then you can pass an arbitrary number of parameters. In the function itself args is an array Commented Aug 12, 2022 at 5:31

3 Answers 3

3

I think you may significantly simplify if make a parameter which accepts an array for setData function:

    function showCharts(){
        let arr;
        arr.push(getDataSet(data, id))
        setData(arr);
    }

    function setData(array){
        var chartName;
        var x = new Array;
        for (var i = 0; i < array.length; i++) {
            if (i != 0) {
                console.log(array[i]);
                x.push(array[i]);
            }
            else{
                console.log(array[0]);
                chartName = array[0];
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

you can use

showCharts(...values)
{
    //values will be an array of the variables you passed that you can use
}

Comments

0

I modified the code and this way worked.
Used a function as showCharts() to check the code.

            function showCharts(){                
                var arr = new Array;
                var chartLocationID = "chartContainerSales";
                var chartTopic = "Chart Topic";
                var xAxisTitle = "Type code";
                var yAxixTitle = "Qty";

                console.log(xData);
                console.log(yData);
                console.log(zData);

                arr.push(createData(xData, 'MyName'));
                arr.push(createData(yData, 'YourName'));
                arr.push(createData(zData, 'Dog'));
                arr.push(createData(wData, 'New Goat'));

                console.log(arr);
                
                setData(chartTopic, xAxisTitle, yAxixTitle, chartLocationID, arr);
            }

Comments

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.