4

I am creating pie charts with JSON and Flot. The JS function to create the pie chart receives a JSON array from Django in this format:

[1, 3, 2, 5, 4]

If there is no data, the JSON array is:

[0, 0, 0, 0, 0]

I'm trying to adjust the function so that if there is no data, then the pie will not be plotted and some text will appear instead (e.g. "Nothing to show yet"). So far I have tried:

function loadWeekChart(theData) {
    var blankData = [0, 0, 0, 0, 0];
    if ($.data(theData) == $.data(blankData)){
    $('#week-pie-chart').empty().append('Nothing to show yet');
    } else {
        $.plot($("#week-pie-chart"), theData ,
            {
                series: {
                    pie: { 
                        show: true
                    }
                }
            });
     }
}

The JS doesn't fail, but it neither prints a pie chart (there is no data) nor does it give me the text replacement.

Please can someone show me where I'm going wrong!

Sirko
  • 72,589
  • 19
  • 149
  • 183
Erve1879
  • 845
  • 1
  • 14
  • 26

5 Answers5

2

Personaly i would do the following heres the psuedo code...

set boolean to true
for each element in the JSON
    compare it with blank data element 
    if they are not equal boolean false
    else continue
return boolean

Then you will know if there same as that function returns true if they are false if they aren't.

Please let me know if you need help coding this. Shouldn't that hard

This may also help: Similar Question

function checkJsons(otherJson,newJson)
{
    var sameJson = true;
     for (var key in otherJson) {
        if(otherJson[key] != newJson[key]) {sameJson=false;} return sameJson;
     }
}

That should help, not test though

A nicer way to do this but harder to read is

function checkJsons(otherJson,newJson)
{
  for (var key in otherJson) {if(otherJson[key] != newJson[key]) {return false;}}
  return true;
}

function pieChartData(theData)
{
  var blankData = [0, 0, 0, 0, 0];
 if(checkJsons(blankData,theData)){$('#week-pie-chart').empty().append('Nothing to show yet');} else { // do your code here // }
} 
Community
  • 1
  • 1
Lemex
  • 3,772
  • 14
  • 53
  • 87
  • There you go, might need some tweaking but thats basicly it, please mark question correct if you agree – Lemex Jul 24 '12 at 09:35
  • Theres more elegant code added, and alot faster than the first one – Lemex Jul 24 '12 at 09:40
  • Thanks Liam - I'm afraid I need a bit more help though. How do I integrate that with my existing code to generate the pie chart? To clarify a bit more, the only array which should generate the replacement text should be: `[0, 0, 0, 0, 0]`. `[0, 1, 0, 0, 0]` should still generate a pie chart.... – Erve1879 Jul 24 '12 at 09:41
  • That should help my new function returns blank data or the data – Lemex Jul 24 '12 at 09:45
  • That got it! Works a treat, thanks Liam. Just FYI, there's a typo: `if(checkJson(` should be: `if(checkJsons(` – Erve1879 Jul 24 '12 at 09:54
  • Perhaps you can help me out with this question: http://stackoverflow.com/questions/11627040/how-do-i-pass-a-simple-json-array-to-jquery-then-flot – Erve1879 Jul 24 '12 at 09:54
1

I should think something like this should work:

var replace = true;
for(var i = 0; i < theData.length; i++)
{
    if(theData.[i] != 0)
    {
        replace = false;
        break;
    }
}

if(replace)
{
     $('#week-pie-chart').empty().append('Nothing to show yet');
}

else
{
    $.plot($("#week-pie-chart"), theData ,
        {
            series: {
                pie: { 
                    show: true
                }
            }
        });
}
Zéychin
  • 4,135
  • 2
  • 28
  • 27
  • I tried this, but I get the following error: SyntaxError: Unexpected token 'in' – Erve1879 Jul 24 '12 at 09:47
  • This is a long way for a short cut, plus your not comparing to JSONs your comparing a JSON and value 0? Isnt good for maintence – Lemex Jul 24 '12 at 09:50
  • Oops. I looks like I wrote the `for` loop incorrectly; I don't program in Javascript often. Take out `int` and put `var` instead. – Zéychin Jul 24 '12 at 09:50
1

how about use JSON.stringify?

l1 = [0, 0, 0, 0];
l2 = [0, 0, 0, 1];
var bEqual = JSON.stringify(l1) == JSON.stringify(l2);
console.log(bEqual);
Hacker Wins
  • 1,289
  • 9
  • 20
  • The preformance of this may not be as good as your stringify two objects then compareing strings. – Lemex Jul 24 '12 at 09:48
0

You don't need to compare with 0-filled array. Just check the input array is 0-filled. I think reduce() is the best for this:

if(arr.reduce(function(a, b){ return a + b; }) == 0) {
    //Nothing to show
} else {
    ...

...but older browsers don't support reduce(), so I suggest using $.grep() instead (because you're already using jQuery).

if($.grep(theData, function (a) { return a != 0; }).length == 0) {
    //Nothing to show
} else {
    ...
Mics
  • 1,420
  • 14
  • 19
0

LmC's answer is good for comparing to JSON objects. I adapted it to compare two JSON arrays which can even have nested JSON arrays. Here is the code:

var areJSONArraysEqual = function(jsonArray1,
                                    jsonArray2) {

                                if(jsonArray1.length===0||jsonArray2.length===0){
                                    if(jsonArray1.length===0 && jsonArray2.length===0){
                                        return true;
                                    }else{
                                        return false;
                                    }
                                }

                                for(var i=0;i<jsonArray1.length;i++){
                                    for ( var key in jsonArray1[i]) {
                                        if(jsonArray1[i][key].length>1){
                                            return areJSONArraysEqual(jsonArray1[i][key],jsonArray2[i][key])
                                        }
                                        if (jsonArray1[i][key] != jsonArray2[i][key]) {
                                            return false;
                                        }
                                    }
                                }
                                return true;
                            };
Price
  • 2,683
  • 3
  • 17
  • 43