迭代可变长度为d3的多维数组

时间:2016-05-02 01:13:27

标签: javascript arrays d3.js multidimensional-array

我有一个多维数组的整数值,我想用它来创建d3.js中的条形图。问题是特定行中可以有可变数量的值。对于每一行,我想根据元素的值创建一个颜色编码的矩形。此外,每个矩形的高度取决于一行中存在多少个值以及每行中的值的平均值

请在此处查看图表的外观: 可变长度条形图:http://i.stack.imgur.com/gYZAe.png

在上图中,每列代表多维数组中一行中的值。现在我面临的问题是,在执行以下操作时,我无法迭代多维数组:

bar = svg.selectAll(".bar")
.data(allrankings)
.enter().append("rect")

注意:allrankings是多维数组。

我目前正在做的是创建另一个大数组,其中所有排名都在一个维度上并迭代它,同时跟踪该元素属于哪一行。有没有更好的方法解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用svg组和多个.data调用来访问多维数据。您可以阅读Mike Bostock关于嵌套选择的教程中的更多内容:https://bost.ocks.org/mike/nest/#data

在你的情况下,如果allrankings是一个多维数组,如[[0,1,2,3],[2,3]],代码可以是这样的:

  .checkbox.valid ~ strong {
color:#000!important;
}

input.error,  .checkbox.error  ~ strong{
    border: 1px dotted red!important;
  color:red!important;
}

label.error {    	
		display: none!important;  
}
  

然后您可以通过.avg,.val和.numBars属性分配值(例如<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script> <script> $.validator.setDefaults({ submitHandler: function() { alert("submitted!"); } }); $().ready(function() { // validate signup form on keyup and submit $("#signupForm").validate({ rules: { firstname: "required", lastname: "required", username: { required: true, minlength: 2 }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: "#password" }, email: { required: true, email: true }, topic: { required: "#newsletter:checked", minlength: 2 }, agree: "required" }, messages: { firstname: "Please enter your firstname", lastname: "Please enter your lastname", username: { required: "Please enter a username", minlength: "Your username must consist of at least 2 characters" }, password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long" }, confirm_password: { required: "Please provide a password", minlength: "Your password must be at least 5 characters long", equalTo: "Please enter the same password as above" }, email: "Please enter a valid email address", agree: "Please accept our policy", topic: "Please select at least 2 topics" } }); // propose username by combining first- and lastname $("#username").focus(function() { var firstname = $("#firstname").val(); var lastname = $("#lastname").val(); if (firstname && lastname && !this.value) { this.value = firstname + "." + lastname; } }); }); </script> <div id="main"> <form class="cmxform" id="signupForm" method="get" action=""> <fieldset> <legend>Validate a complete form</legend> <p> <label for="firstname">Firstname</label> <input id="firstname" name="firstname" type="text"> </p> <p> <label for="lastname">Lastname</label> <input id="lastname" name="lastname" type="text"> </p> <p> <label for="username">Username</label> <input id="username" name="username" type="text"> </p> <p> <label for="password">Password</label> <input id="password" name="password" type="password"> </p> <p> <label for="confirm_password">Confirm password</label> <input id="confirm_password" name="confirm_password" type="password"> </p> <p> <label for="email">Email</label> <input id="email" name="email" type="email"> </p> <p> <label for="agree">Please agree to our policy</label> <input type="checkbox" class="checkbox" id="agree" name="agree"> <strong>I Accept</strong> </p> <p> <input class="submit" type="submit" value="Submit"> </p> </fieldset> </form>

Gilsha在https://bl.ocks.org/mbostock/3886208

的堆积条形图中发布了一个很好的例子