2014-09-03 51 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見定義自己的自定義格式剔一個更爲複雜的例子。

相關問題