【问题标题】:The use of a data array in the d3.js scatter file instead of an external data file在 d3.js 分散文件中使用数据数组而不是外部数据文件
【发布时间】:2013-09-16 19:37:23
【问题描述】:

我有 scatter d3.js 库的 javascript:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
font: 10px sans-serif;
}

.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}

.point {
fill: steelblue;
stroke: #000;
}

</style>
<body>
<script src="http://d3js.org/d3.v2.js?2.9.6"></script>
<script>

var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;

var x = d3.scale.linear()
.range([0, width]);

var y = d3.scale.linear()
.range([height, 0]);

var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.csv("data.csv", function(data) {

 // Coerce the strings to numbers.
data.forEach(function(d) {
d.x = +d.x;
d.y = +d.y;
});

// Compute the scales’ domains.
x.domain(d3.extent(data, function(d) { return d.x; })).nice();
y.domain(d3.extent(data, function(d) { return d.y; })).nice();

// Add the x-axis.
 svg.append("g")
  .attr("class", "x axis")
  .attr("transform", "translate(0," + height + ")")
  .call(d3.svg.axis().scale(x).orient("bottom"));

 // Add the y-axis.
svg.append("g")
  .attr("class", "y axis")
  .call(d3.svg.axis().scale(y).orient("left"));

// Add the points!
svg.selectAll(".point")
  .data(data)
  .enter().append("path")
  .attr("class", "point")
  .attr("d", d3.svg.symbol().type("triangle-up"))
  .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
});

</script>

文件:data.cvs

x,y
5,90
25,30
45,50
65,55
85,25

我没有从 cvs 文件中读取数据,而是在 javascript 中添加了一个数组,并尝试直接从该数组中提取 x 和 y 值:

var rows = new array(
 array(0,0),
 array(90,90),
 array(59,70),
 array(65,77),
 array(85,66)
  );

如何使用数组 rows 以获得相同的结果??

【问题讨论】:

    标签: javascript arrays d3.js


    【解决方案1】:

    您可以使用哈希数组:

    var data = [{"x":"5","y":"90"},{"x":"25","y":"30"},{"x":"45","y":"50"},
    {"x":"65","y":"55"},{"x":"85","y":"25"}];
    

    或者写一个函数把你的数组转换成这个哈希数组。

    var rows = new Array(
      Array(0,0),
      Array(90,90),
      Array(59,70),
      Array(65,77),
      Array(85,66)
    );
    
    for (var i = 0; i < rows.length; i++) {
      data.push({x: rows[i][0], y: rows[i][1]});
    }
    
    // Coerce the strings to numbers.
    data.forEach(function(d) {
      d.x = +d.x;
      d.y = +d.y;
    });
    

    这是实时代码:

    http://vida.io/documents/THpmgQDARSWPJqGG5

    【讨论】:

    • 问题是数组中的数据是我的javascript中的输入,可以编辑。因为相同的数据可以作为不同图形库的输入,所以数组的格式必须是通用的,如上所述。请问有什么办法吗?
    • 请提出另一个问题,这里是 x 轴和 y 轴取决于数组中给定的值,每个值都从值 min 开始,停止在值 max。我们可以将刻度更改为独立于用户值吗?
    • 您可以为它记录另一个问题吗?我会在那里回答。
    • 看来有人在我之前回答了:)
    猜你喜欢
    • 2014-09-05
    • 2016-09-13
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多