简体   繁体   English

在D3中创建时间刻度

[英]Creating time scale in d3

I have the following data: 我有以下数据:

   parseDate = d3.time.format("%Y").parse,
   format = d3.time.format("%Y");

  var myData = [
   { dataField: "A", coords: [
    {xCoord: parseDate('1990') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1991') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1992') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1993') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1994') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1995') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1996') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1997') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1998') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1999') , yCoord: Math.random() * 100}
    ]
  },
  { dataField: "B",coords: [
    {xCoord: parseDate('1990') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1991') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1992') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1993') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1994') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1995') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1996') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1997') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1998') , yCoord: Math.random() * 100},
    {xCoord: parseDate('1999') , yCoord: Math.random() * 100}
    ]
  }

and I am trying to create a line graph using d3. 我正在尝试使用d3创建折线图。 Additionally, I have the code: 另外,我有代码:

    var data = function (d) { return d.coords; };
    var xValue = function (d) { return d.xCoord; };
    var yValue = function (d) { return d.yCoord; };

I have been creating my scale as such: 我一直在这样创建自己的秤:

    xScale = d3.time.scale()
      .domain([new Date(1988,10,30),new Date(2000,01,30)])
      .range([margin.left, 600 - margin.right]);

However, my goal is to have my domain adjust itself to be the smallest year to the largest year (or mabye even extending an extra year in each direction). 但是,我的目标是使自己的域调整为最小的年份到最大的年份(甚至mabye甚至在每个方向上都延长了一年)。 But so far, I have been unsuccessful. 但是到目前为止,我一直没有成功。 I have attempted to use 我尝试使用

    .domain(d3.extent(data, xValue)); 

But haven't had luck. 但是还没有运气。 I'm fairly new to javascript and think my struggles lie within the way I stored my data. 我对javascript相当陌生,认为我的挣扎在于存储数据的方式之内。 Any ideas would be great. 任何想法都很棒。

You can use the built in d3.extent function to get the min and max of an array. 您可以使用内置的d3.extent函数来获取数组的最小值和最大值。 It takes an an array and an optional accessor function. 它需要一个数组和一个可选的访问器函数。 It will return an array of 2 values, the min and max: 它将返回2个值的数组,最小值和最大值:

[min, max]

I would try 我会尝试

xScale.domain(d3.extent(myData[0].coords, function(d) {return d.xCoord; }));

and then rinse and repeat for the other data labels You could do it in a for loop if there are a bunch of different data sets. 然后冲洗并重复其他数据标签如果有许多不同的数据集,则可以在for循环中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM