【问题标题】:Timeseries line-chart in dc.js - time Dimension not workingdc.js 中的时间序列折线图 - 时间维度不起作用
【发布时间】:2016-01-09 02:54:26
【问题描述】:

我是 d3 和 dc.js 的新手,正在尝试制作股票价格的时间序列图。

由于某种原因,我无法让 Dimension 工作。我感觉这与我的日期格式有关,我已经尝试过尝试,但未能解决问题。

我的代码如下

//create global variable 'stockpricedata' to store CSV data;  
var stockpricedata = [];

//create global variable to enable parsing of dateFormat
var dateFormat = d3.time.format('%d/%m/%Y');


//create function that will input CSV data and draw a timechart.
//this function is called in a seperate piece of code using queue.js
//the code that loads the CSV data is also executed separately to this code

function makeChart(error, data) {

  //First lets create a nested function that converts 'Test_Score' variable 
  //from char to numeric

  function convData(data) {

    stockpricedata = data;
    stockpricedata.forEach(function(d){ 
      d['Open'] = +d['Open']; 
      d['High'] = +d['High']; 
      d['Low'] = +d['Low']; 
      d['Close'] = +d['Close']; 
      d['Volume'] = +d['Volume'];    
      d['Adj Close'] = +d['Adj Close']; 
      d['Adj Close'] = +d['Adj Close'];
      d.daydate = dateFormat.parse(d.Date);


    });

    };

  //Call the function
  convData(data);


  //initiate crossfilter

  var ndx = crossfilter(stockpricedata);
  var all = ndx.groupAll();

  //test ndx object to ensure that it initiated correctly
  var n = ndx.groupAll().reduceCount().value();
  console.log(stockpricedata);
  console.log("Lets say there are " + n + " datapoints in my file"); //yes, this is showing a valid number, so this is working.


  //Create 'Date' dimension that returns the DAY.
  var DateDim = ndx.dimension(function(d) 
    {return d.daydate;}
    );
  console.log(DateDim.top(5))



  //Create 'Sum' grouping to Sum the price (there will only be 1 for each day, so should just show the stockprice) to calculate y-axis

  var PriceGroup = DateDim.group().reduceSum(function(d){return d.Close;}); //d.Close is the 'closing price' datapoint


  //create chart object and link it to HTML element
  var StockPriceChart  = dc.barChart('#histprice-line-chart');


  //create minDate and maxDate variables so that we can set the x-axis scale.

  var minDate = DateDim.bottom(1)[0].date;
  var maxDate = DateDim.top(1)[0].date;
  console.log("min date is " + minDate + " and max date is " + maxDate);

  //chart attributes
   StockPriceChart
      .width(600)
      .height(180)
      .dimension(DateDim) //x-axis (range of scores)
      .group(PriceGroup) //y-axis 
      .x(d3.time.scale().domain([minDate,maxDate]))
      .elasticY(true)
      .xAxisLabel('Time')
      .yAxisLabel('Price')
     //.xAxis();
     // .margins({top: 10, right: 20, bottom: 50, left: 50});

     // showtime!
    dc.renderAll();


};

数据似乎可以正常加载并显示在控制台中。在我的“daydate”变量中,我原来的“日期”字段被格式化为 D3 格式。控制台中的示例数据点如下:

200:对象 调整关闭:90.22737 关闭:93.8913 日期:“2015 年 3 月 4 日” 高:93.8913 低:93.8913 打开:93.8913 音量:0 日期:Fri Apr 03 15 00:00:00 GMT+1100 (AEDT)

但由于某种原因,以下代码似乎不起作用。

var DateDim = ndx.dimension(function(d) 
{return d.daydate;}
);
console.log(DateDim).top(5); // ?!?!? why is this not working ?!?!?!?

当我尝试将 DateDim 记录到控制台时,我还在控制台中收到以下错误:'Uncaught TypeError: Cannot read property 'top' of undefined'。

对此的任何帮助将不胜感激!

谢谢,

J

【问题讨论】:

  • 对不起,我也意识到存在语法错误 - 我已将 console.log(DateDim).top(5) 更改为 console.log(DateDim.top(5));对此进行更正。
  • 尽管如此,我仍然在控制台中收到以下消息:“min date is undefined and max date is undefined”

标签: d3.js time-series dc.js crossfilter timeserieschart


【解决方案1】:

你有密码

var minDate = DateDim.bottom(1)[0].date;
var maxDate = DateDim.top(1)[0].date;

您的数据记录包含属性daydateDate,但不包含date。所以DateDim.bottom(1)[0].dateDateDim.top(1)[0].date 是未定义的。将其更改为:

var minDate = DateDim.bottom(1)[0].daydate;
var maxDate = DateDim.top(1)[0].daydate;

【讨论】:

  • 谢谢伊桑!这已经解决了 1 个问题,并且这些值不再显示为“未定义”。但是,我仍然遇到两个问题。 (A) 最小/最大日期似乎不反映实际日期。我的实际数据的日期范围从 04/01/1999 到 08/01/2016,但是日志显示最小日期是 Tue Jan 04 0 00:00:00 GMT+1100 (AEDT),最大日期是 Wed Dec 30 99 00:00:00 GMT+1100 (AEDT)
  • 查看 DateDim.top(Infinity)。是否正确排序?我猜不是因为您的 DateDim 访问器返回一个对象,而不仅仅是返回 d.daydate。
  • 其次,(B)在绘制图表时,它没有值(即只有一个 x 和 y 轴,没有显示实际的线),所以它似乎仍然在解释数据时遇到问题.我尝试通过更改 var DateDim = ndx.dimension(function(d) {return d.daydate;}); --> 专门将 return d.daydate 更改为 return d3.time.day(d.daydate) 但这没有用...
  • 啊,对不起。我错了。被花括号和移动设备甩掉了。您能否在问题中分享您的一些数据?或者更好的是,在 jsFiddle 或类似网站上放置一个工作示例?
  • 感谢 Ethan 的超快速响应 :) 我对此很陌生 - 我如何查看排序?
【解决方案2】:

问题最终是由条形图中呈现的数据点过多引起的。减少 1 个图表中的数据点数,以及从 .barChart 更改为 .lineChart 解决了这个问题。非常感谢 @Ethan 帮助解决此问题并解决了许多其他问题!

【讨论】:

    最近更新 更多