简体   繁体   中英

Turn $get result string into javascript array

I'm looking to use the jqplot plugin ( http://www.jqplot.com/tests/pie-donut-charts.php ) to produce a pie chart, however I am having trouble getting the results from my $get function into something that the jqplot function will accept as useable data.

You can see in the jqplot link above how I need to create the data variable for displaying the pie chart.

Here's my $.get function which currently returns a string:

//data is returned as a string: 'some_field_name',10,'some_other_field,33 etc
$.get("notesajax.php", {month:monthFilter, area:areaFilter}, function(data) {
    var arr = data.split(",");
    var results = [];
    for (var i = 0; i < arr.length; i++) {
        mini = [];
        mini[arr[i]] = "'"+arr[i]+"',"+arr[i+1];
        results.push(mini);
        i++;
    }

For easy reference here is the jqplot function, including a defined 'data' variable at the start to illustrate how jqplot expects to receive data.

//this variable is just for illustrative purposes
var data = [
    ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14], 
    ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
];

var plot1 = jQuery.jqplot ('chartdiv', [results], 
    { 
        seriesDefaults: 
        {
            // Make this a pie chart.
            renderer: jQuery.jqplot.PieRenderer, 
            rendererOptions: {
                // Put data labels on the pie slices.
                // By default, labels show the percentage of the slice.
                showDataLabels: true
            }
        }, 
        legend: { show:true, location: 'e' }
    }
);

So far however I cannot get my $get returned data into a format that the jqplot function will accept.

var field, value, data = [], str = "'some_field_name',10,'some_other_field,33";
var arr = str.split(',');
// just add 2 to each iteration to get every second item (last argument is i += 2):
for (var i = 0; i < arr.length; i += 2) {
  field = arr[i].replace(/'/g, ""); // replace ', because otherwise your string will be "'some_field_name'"
  value = parseInt(arr[i+1], 10); // parseInt because you want numbers, but got a string
  data.push([field, value]); // push into your data-array an array with your field and value
}

jsfiddle here: http://jsfiddle.net/wLxyZ/

You are generating wrong array. you should either send directly JSON object back from the server which is recommended and easy or make your parsing script ok.

$.get("notesajax.php", {month:monthFilter, area:areaFilter}, function(data) {
var arr = data.split(",");
var results = [];
for (var i = 0; i < arr.length; i++) {
    var mini = new Array();
    mini.push(arr[i]);
    mini.push(arr[i+1]);
    results.push(mini);
}

i played with your code and came to the following solution, there was couple of issues with your original code especially on how you constructed your mini array

var arr = ['some_field_name',10,'some_other_field',33];
var results = [];
for (var i = 0; i < arr.length; i+=2) {        
    var mini = ["'"+arr[i]+"'",+arr[i+1]];
    results.push(mini);
}
alert(results[0][0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM