2014-09-03 237 views
0

这是我的代码,包括JSON示例以及我如何解析我的日期。如何使用tickFormat只显示年份(如世纪和最后两位数字)

眼下的日期显示出来,如:2013年10月1日例如

我如何写一个tickFormat函数返回全年(含世纪)x轴在规定的第一年tickValues函数和'14(例如2014年)的所有其他年份?

  JSON structure: 
      var shortData5= [ 
      {"date":"10/1/2013","shortFig":12}, 
      {"date":"11/1/2013","shortFig":34}, 
      {"date":"12/1/2013","shortFig":-25}] 


      //fyi- var shortData5 is passed in my chart building function as "thedata" 

      var parseDate = d3.time.format("%x").parse; 

      thedata.forEach(function(d) {    
       parseDate(d.date); 
      }); 

      //Set up the X scales 
      //for bars 
      var xScaleOrdinal = d3.scale.ordinal() 
       .rangeRoundBands([0, width], .1) 
       .domain(thedata.map(function(d) { return d.date; })); 

      //With the X scales, set up the X axis 
      var xAxis= d3.svg.axis() 
       .orient("bottom") 
       .tickFormat() //need function here; 

      if(theDiv=="#contructionSingleMulti"|| theDiv=="#volumeExistingLong"|| theDiv=="#volumeNewLong"){ 
       xAxis.scale(xScaleOrdinal) 
.tickValues([thedata[4].date,thedata[16].date,thedata[28].date,thedata[40].date,thedata[52].date,thedata[64].date,thedata[76].date,thedata[88].date,thedata[100].date,thedata[112].date]);    
      }else{ 
       xAxis.scale(xScaleOrdinal) 
       .tickValues([thedata[0].date,thedata[3].date]);    
      } 


      //Call the X axis 
      baseGroup.append("g") 
       .attr("class", "xaxis") 
       .attr("transform", "translate(0," + height + ")") 
       .call(xAxis); 

回答

0

我不确定您想如何格式化您的报价值的方式提出您的问题。也许一个例子会更有帮助。您可以创建一个自定义刻度格式功能,以传递给刻度的.tickFormat访问器方法。

你可以做这样的事情:

scale.tickFormat(myCustomTickFormatter); 

function myCustomTickFormatter(datum, index) 
{ 
    return /** some manipulation of datum **/ 
} 

http://bl.ocks.org/mbostock/4149176见定义自己的自定义格式剔一个更为复杂的例子。

相关问题