How can I get the return value of color

Asked by At

I am working on a project. I have used $.get() function. Using this function I'm calling a JSON file which contain information like Country name, polarity and similarity. In this $.get() function after getting data I'm doing some calculation like taking average polarityScore = polarityScore/count;. On the basis of its result I'm checking the conditions if(polarityScore <= -0.6){ console.log("red"); color='red';}. I just want to know that how can I get the color value like here red as return value for style("fill", function(d).

globe.selectAll(".countries")
.data(geoData)
.enter()
.append("path")
.attr("class", 'country')
.attr("d", path)
.style("stroke", "black")
.style("stroke-width", "1px")
.style("fill", function(d) { 

 if (d.id == '586' ) 
 {
  var JSONItems = [];
  var polarityScore = 0;
  var count = 0;
  color = '';
  $.get( "file.json", function(data){
    JSONItems = data;
    for(i=0; i<JSONItems.length; i++)
    {
      if(JSONItems[i].country == "Pakistan" || JSONItems[i].country == "PAKISTAN")
      {
        number = Number(JSONItems[i].polarity_score);
        polarityScore += number;
        count ++;
      }
    }
    polarityScore = polarityScore/count;

    console.log(polarityScore);
    if(polarityScore <= -0.6){  console.log("red"); color='red';}
    if(polarityScore > -0.4 && polarityScore <= -0.2){console.log("lightred"); color='lightred';}
    if(polarityScore > -0.2 && polarityScore <= 0.2){ console.log("purple");  color='purple';}
    if(polarityScore > 0.2 && polarityScore < 0.6){ console.log("lightgreen"); window.color='lightgreen';}
    if(polarityScore > 0.6){ console.log("green"); color='green';}
    console.log(color);
    return window.color;
    console.log("Error");
  });

  console.log(window.color);
} // if (d.id == '586' ) ends here...
 else {return color='#f4d47e'} 
})

0 Answers