2

I am calling a JS function from another and sending JSON array data. While sending the data to 2nd function I am getting value as undefined/null. How to send JSON array data from one JS function to Another ?

Function: 1

$(function createJsonArray() {

            var categories =  [
                    'Jan',
                    'Feb',
                    'Mar',
                    'Apr',
                    'May',
                    'Jun',
                    'Jul',
                    'Aug',
                    'Sep',
                    'Oct',
                    'Nov',
                    'Dec'
                ];

            var confidence = [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];

            var jsonArray = {
                issueData: []
            };

            jsonArray.issueData.push({
                "categories": categories,
                "confidence": confidence
            });
            //alert('categories: ' + jsonArray.issueData[0].categories);


            // calling function-2
            createGraph(JSON.stringify(jsonArray));
        });

Function: 2

$(function createGraph(graphData) {
            var jsonArray = $.parseJSON(graphData);
            alert('jsonArray: ' + jsonArray);
            $('#container').highcharts({
                chart: {
                    type: 'column'
                },
                title: {
                    text: 'Monthly Average Rainfall'
                },
                subtitle: {
                    text: 'Source: WorldClimate.com'
                },
                xAxis: {
                    categories: jsonArray.issueData[0].categories,
                    crosshair: true
                },
                yAxis: {
                    min: 0,
                    title: {
                        text: 'Rainfall (mm)'
                    }
                },
                tooltip: {
                    headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                    pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                        '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
                    footerFormat: '</table>',
                    shared: true,
                    useHTML: true
                },
                plotOptions: {
                    column: {
                        pointPadding: 0.2,
                        borderWidth: 0
                    }
                },
                series: [{
                    name: 'Tokyo',
                    data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]

                }]
            });
        });

JSfiddle link

4
  • There is no mention of value in your code..? Commented Mar 25, 2016 at 12:04
  • 1
    check this jsfiddle.net/hLkUz/69 Your createGraph method is not being called from createJsonArray method. It is being as the script is being loaded. Commented Mar 25, 2016 at 12:06
  • why do you wrap them both in $(function ? Commented Mar 25, 2016 at 12:11
  • there is no need to convert string your json data. You send directly, if you cant you can determine it on your window object. Commented Mar 25, 2016 at 12:15

1 Answer 1

1

Check this update fiddle

You need to invoke the createJSONArray() method and get rid of the jquery call of $(function functionName), instead just define a simple method.

Sign up to request clarification or add additional context in comments.

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.