2

我正在尝试创建条形图。我在处理一堆代码时遇到了问题,但最大的问题是我似乎无法创建一个循环来获取我的 sampleData 数组中的数据并每次创建一个新的 div 并将其附加到下一个 div等等。现在我已经简单地创建了 5 个 div,但我不需要太多。

另外,我想将鼠标悬停在栏上并查看项目数量。

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="testMyJson.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script >
    $(document).ready(function() {
    $.support.cors = true;
    $.ajax({
    url:'https://www.sciencebase.gov/catalog/items?q=Fish&max=20&format=json',
    type: "GET",
    dataType: "json",
    success: function( response ) {
    var sampleData = [25,7,19,22,150];

    //for (var i = 0; i < 5; i++) {
        $('#super-skill-1').animate( { height:sampleData[0] + 'px' } );
        $('#super-skill-2').animate( { height:sampleData[1] + 'px' } );
        $('#super-skill-3').animate( { height:sampleData[2] + 'px' } );
        $('#super-skill-4').animate( { height:sampleData[3] + 'px' } );
        $('#super-skill-5').animate( { height:sampleData[4] + 'px' } );
    //}


    },
    error: function(jqXHR, textStatus, errorThrown) {
            alert("Ajax Call Failed - textStatus =" +  textStatus + ", errorThrown = " + errorThrown);
}
});
});
</script>
<style>
.the-container { width:400px; height:250px; border-style: solid; border-width:3px; border-color:black }
.the-container ul { margin:0; padding:0; list-style:none; }
.the-container li { width:30px; height:250px; margin:0 5px; position:relative; float:left; }
.the-container li a { position:absolute; bottom:0; width:100%; height:0; background-color:#ccc; }
.the-container li a:hover { background-color:green; }
</style>
</head>
<body>
<div class="the-container">
<ul>
<li><a id="super-skill-1" href="#" class="tooltip" title=sampleData[0]></a></li>
<li><a id="super-skill-2" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-3" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-4" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-5" href="#" class="tooltip" title="TESTING!!!!"></a></li>
</ul>
</div>
<form>
<input type="button" id="showdata"value="Show Data" >


</form>
</body>
</html>

谢谢

4

6 回答 6

1

遍历数组可以由$.each函数执行。要从数组创建div集合,可以使用以下方法:

myArray- 为其创建 div 的数组, #divContainer- 放置新 div 的元素的 id

1)使用追加。如果您需要设置应用于新元素的属性或样式,那就太好了

$.each(myArray, function(elem) {
      $("#divContainer").append($("<div></div>").css("background", "red").html(elem));
});

2)使用字符串连接。比追加更快。

var result = "";
$.each(myArray, function(elem) {
        result += "<div>" + elem + "</div>";
    });
$("#divContainer").append($(result));

3) 使用jquery.tmpl - jQuery 模板插件。快速简单的方法,但需要额外的插件

$.tmpl("{{each}}<div>{{html $value}}",
       myArray)
 .appendTo("#divContainer" );
于 2012-10-04T20:43:43.700 回答
0

没有真正回答你的问题,但有点奖金。

您不需要将 px 添加到动画或 css、宽度、高度等。如果您将单位排除在外,jQuery 会假定为 px。

你的

$('#super-skill-' + i).animate( { height:sampleData[i] + 'px' } );

例子

$('#foo').animate({top: 100, left: 80 }, 1200);

http://jsfiddle.net/UqPmB/

于 2012-10-04T20:50:50.913 回答
0

我删除了 AJAX 调用来测试它。这似乎适用于循环:

for (var i = 0; i < 5; i++) {      
    $('#super-skill-' + i).animate( { height:sampleData[i] + 'px' } );
}

我建议让 sampleData 中的索引与 id 中的索引匹配。

<li><a id="super-skill-0" href="#" class="tooltip" title=sampleData[0]></a></li>
<li><a id="super-skill-1" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-2" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-3" href="#" class="tooltip" title="TESTING!!!!"></a></li>
<li><a id="super-skill-4" href="#" class="tooltip" title="TESTING!!!!"></a></li>
于 2012-10-04T20:44:29.917 回答
0

如果您的示例数据来自响应(作为数组),那么您可以这样做

for (var i in response) {
    var newDiv = $('<div id="super-skill-'+i+'"/>)
    $('#parentDiv').append(newDiv);
    newDiv.animate( { height:sampleData[i] + 'px' } );
}
于 2012-10-04T20:45:04.183 回答
0

不同的方法

您是否考虑过使用专门设计用于绘制图表的库,例如gRaphaël

使用 gRaphaël 您只需编写:

r.barchart(10, 10, 400, 250, [sampleData]);

而且您不在乎sampleData中有多少元素,如何迭代它们,如何缩放它们,考虑到它们的可变数量,条形的良好宽度应该是多少,最大值是多少,所以您知道如何缩放它垂直等。

这就是您的程序的外观,包括悬停时出现的值:

条形图示例

var sampleData = [25,7,19,22,150,200,25,77,30,105,5];

var r = Raphael("chart");

// hover handlers:                      
function fin() {
    this.flag = r.popup(this.bar.x, this.bar.y,
                  this.bar.value || "0").insertBefore(this);
}
function fout() {
    this.flag.animate({
        opacity: 0
    }, 600, function() {
        this.remove();
    });
}

r.barchart(10, 10, 400, 250, [sampleData]).hover(fin, fout);

就是这样。

看演示

(悬停事件处理程序是 gRaphaël 网站上示例中处理程序的修改版本。)

它适用于 Firefox 3.0+、Safari 3.0+、Opera 9.5+ 和 Internet Explorer 6.0+。

于 2012-10-04T21:50:03.220 回答
0

您可以使用$.each函数循环并动态添加 li/anchor 标签

var sampleData = [25, 7, 19, 22, 150,60,99,56];

$('input[type=button]').click(function() {
    $.each(sampleData, function(i, v) { // loop through each item in array 
                        //i=indexInArray,v=valueOfElement
        var $lis = $('<li><a></a></li>'); // create new li's with children anchors
        $lis.find('a') // find anchor
            .attr('id', 'super-skill-' + (i + 1)) // add id
            .attr('href', '#') // add href
            .attr('title', v) // add title
            .addClass('tooltip'); // add class tooltip
        $('div.the-container ul').append($lis); // add to ul
        $('#super-skill-'+ (i + 1)).animate( { height:v  } ,1000);  // animate
    });
});​

JSFIDDLE

于 2012-10-04T20:56:10.240 回答