Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Json响应作为jquery火花线图的工具提示值查找_Javascript_Jquery_Json_Sparklines - Fatal编程技术网

Javascript Json响应作为jquery火花线图的工具提示值查找

Javascript Json响应作为jquery火花线图的工具提示值查找,javascript,jquery,json,sparklines,Javascript,Jquery,Json,Sparklines,我试图使用json响应作为图形中的tooltipValueLookups参数,但没有成功。工具提示只显示0:5和1:8,而不是穆德:5和史高丽:8 如果我只使用完全相同的json声明变量代理,它将非常有效: var agents = {"names":{"0":"Mulder", "1":"Scully"}} 但当我尝试使用服务器响应时,每件事都会像预期的那样发生。谁能告诉我,我做错了什么 var agents = $.ajax({ url : "/ajaxAgents",

我试图使用json响应作为图形中的tooltipValueLookups参数,但没有成功。工具提示只显示0:5和1:8,而不是穆德:5和史高丽:8

如果我只使用完全相同的json声明变量代理,它将非常有效:

var agents = {"names":{"0":"Mulder", "1":"Scully"}}
但当我尝试使用服务器响应时,每件事都会像预期的那样发生。谁能告诉我,我做错了什么

var agents = $.ajax({
    url     : "/ajaxAgents",
    type    : "get",
    dataType: 'json'
});
响应:{“名称”:{“0”:“Mulder”,“1”:“Scully”}

然后,解析响应并对其进行过滤:

var agents = $.parseJSON(response);
var filteredNames = $.map(agents.names, function(el) {
        return el;
    });
最后,火花线功能:

    $("#mini-chart").sparkline(agentsData, {
    type: 'bar',
    height: '30px',
    barWidth: 6,
    barSpacing: 2,
    barColor: '#0aa699',
    tooltipFormat: '<span style="color: {{color}}">&#9679;</span> {{offset:offset}}: {{value}}',
    tooltipValueLookups:{offset:filteredNames},
    negBarColor: '#0aa699'});
$(“#迷你图”).迷你图(agentsData{
类型:'bar',
高度:“30px”,
条宽:6,
杆间距:2,
barColor:“#0aa699”,
tooltipFormat:“●;{{offset:offset}}:{{value}}”,
tooltipValueLookups:{offset:filteredNames},
negBarColor:'#0aa699'});
@Hüseyin:感谢您的帮助,这非常有帮助。

使用过滤json

并在你的功能中使用像

tooltipValueLookups:{offset: filteredNames}
您可以在这里看到演示:

注意:如果您从服务器返回字符串,则需要使用

var agents = jQuery.parseJSON(response);

对不起,它坏了。我不能重新声明json,它是来自服务器的响应。另外,我非常确定tooltipValueLookups选项必须有键。正如我所说,如果我直接声明JSON对象,函数的工作就像一个符咒。当我尝试使用响应时出现了问题,这与预期完全一致。无论如何,谢谢:)@Sam,好的,json可以保持不变。我已经更新了我的代码和演示,请看我更新的asnwer。这让我发疯。parseJSON返回null,这是有意义的,因为json已经是一个对象了。mapping agent.names抛出一个类型错误。再次感谢您的回复@Hüseyin。但是,它已经是对象,所以不要使用json解析。直接使用它。看看它是否有效我直接测试了解析。它抛出了一个错误TypeError,无法找出原因。不管怎样,我找到了解决办法。我用找到的答案编辑了这个问题。谢谢:)
var filteredNames = $.map(agents.names, function(el, i) {
    return el;
});
tooltipValueLookups:{offset: filteredNames}
var agents = jQuery.parseJSON(response);