如何获得颜色的返回值

时间:2019-04-21 21:24:55

标签: javascript jquery arrays object ecmascript-6

我正在做一个项目。我使用过$.get()函数。使用此函数,我将调用一个JSON文件,其中包含诸如国家/地区名称,极性和相似性之类的信息。在获取数据后的此$.get()函数中,我进行了一些计算,例如取平均值polarityScore = polarityScore/count;。根据其结果,我正在检查条件if(polarityScore <= -0.6){ console.log("red"); color='red';}。我只想知道如何获得像red这样的颜色值作为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 个答案:

没有答案