我会尝试尽可能简单地描述我的问题。我有这个AJAX调用GET一个Json_encoded数组。检索到“aht_value”后,我得到一个彩色背景 被从绿色绘制到红色(我正在做热图)。我的值正在正确输出。
问题: 我的“colorMe”功能。我不确定我做错了什么,或者我是否将我的所有功能都放在了错误的位置,因为我正在我的“成功”中完成所有工作,并且在我的FOR循环之后,如下所示。但是,如果我将“min”替换为100,将“max”替换为1800,则会显示颜色。
提前感谢。
show.php中的数组:
[{
"username":"OXGOR",
"aht_value":"241",
"station":"B20"
},
{
"username":"AISAI2",
"aht_value":"199",
"station":"B21"
},
{
"username":"CAPAP3",
"aht_value":"NA",
"station":"B10"
}]
map.php - JS部分
<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
$.ajax({
type:"GET",
url : "show_aht.php",
data:{ } , // do I need to pass data if im GET ting?
dataType: 'json',
success : function(data){
var min = data.reduce(function(prev, curr) {
return isNaN(curr['aht_value']) || prev < curr['aht_value'] ? prev : curr['aht_value'];
}, 1000000);
alert(min); //not returning the correct number
var max = data.reduce(function(prev, curr) {
return isNaN(curr['aht_value']) || prev > curr['aht_value'] ? prev : curr['aht_value'];
}, -1000000);
alert(max); //nor returning the correct number
//function for calculation of background color depending on aht_value
function conv(x){
return Math.floor((x - min) / (max - min) * 255);
}
//function for background color
function colorMe(v){
return "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
}
//going through all DIVs only once with this loop
for(var i = 0; i < data.length; i++) { // loop over results
var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
if(divForResult.length) { // if a div was found
divForResult.html(data[i]['aht_value']).css("background-color", colorMe(data[i]['aht_value']));
}//end if
}//end for
}//end success
});//end ajax
});//end click
});//end rdy
</script>
show.php
:我在哪里使用json_encode
$result = array();
foreach ($memo as $username => $memodata) {
if (in_array($username, array_keys($user))) {
// Match username against the keys of $user (the usernames)
$userdata = $user[$username];
//if AHT is null give N/A as value
if (is_null($memodata['aht_value'])) {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => 'NA',
'station' => $userdata['station']
);
}//end inner if
//else give the actual value of AHT without the decimals
else {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => substr($memodata['aht_value'],0,-3),
'station' => $userdata['station']
);
echo json_encode($result);
这是有效的如果我在colorMe函数中手动将min设置为100,最大值为1800: what is suposd to look like
当我使用min和max计算时,这会返回我错误的颜色,如下所示 not giving right colors
答案 0 :(得分:3)
您的数组在第一个对象后缺少逗号。
答案 1 :(得分:1)
您正在数组长度上执行map
,而不是在数组本身上执行var min = data.reduce(function(prev, curr) {
return isNaN(+curr['aht_value']) || prev < +curr['aht_value'] ? prev : +curr['aht_value'];
}, 1000000);
var max = data.reduce(function(prev, curr) {
return isNaN(+curr['aht_value']) || prev > +curr['aht_value'] ? prev : +curr['aht_value'];
}, -1000000);
。此外,我建议你reduce
你的阵列:
NA
设置特定背景颜色是否为function colorMe(v){
return v == 'NA' ? "#FFF" : "rgb(" + conv(v) + "," + (255-conv(v)) + ",0)";
}
:
{{1}}