2016-03-18 78 views
0

我正在使用d3中的散點圖。 x軸表示YEAR。我從數據庫中獲取信息,並將其繪製爲圖上的圓圈。我的問題是如何解析x值,以便將其格式化爲一年?即。目前它顯示爲1,999,但應該看起來像1999。有人能告訴我,我應該如何正確解析這個來獲得正確的格式!這裏是相關的代碼:x軸正確格式年份d3

var margin = {top: 20, right: 20, bottom: 30, left: 60}, 
    width = 1800 - margin.left - margin.right, 
    height = 600- margin.top - margin.bottom; 

var YearFn = function(d) {return d.YEAR ;}; 

var Num_citationsFn = function(d) {return d.counter;}; 

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

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

var xAxis = d3.svg.axis().scale(x).orient("bottom");  

var yAxis = d3.svg.axis().scale(y).orient("left"); 

d3.json("connection4.php", function(error,data) { 

dataJson.forEach(function(d) { 
    d.YEAR = +d.YEAR; 
    d.counter = d.counter; 

x.domain([d3.min(data, YearFn)-1, d3.max(data, YearFn)+1]); 
y.domain([d3.min(data, Num_citationsFn)-1, d3.max(data, Num_citationsFn)+1]); 

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 + ")"); 

svg.append("g") 
    .attr("class", "x axis") 
    .attr("transform", "translate(0," + height + ")") 
    .call(xAxis) 
    .append("text") 
    .attr("class", "label") 
    .attr("x", width) 
    .attr("y", -6) 
    .style("text-anchor", "end") 
    .text("YEAR"); 

    // y-axis 
svg.append("g") 
    .attr("class", "y axis") 
    .call(yAxis) 
    .append("text") 
    .attr("class", "label") 
    .attr("transform", "rotate(-90)") 
    .attr("y", 6) 
    .attr("dy", ".71em") 
    .style("text-anchor", "end") 
    .text("Number of citations"); 

var circles = svg.selectAll("circle") 
     .data(data) 
     .enter() 
     .append("circle") 
     .attr("class", "dot") 
     .attr("r", 8.5) 
     .attr("cx", function(d) {return x(YearFn(d))}) 
     .attr("cy", function(d) {return y(Num_citationsFn(d))}) 
     .style("fill","blue") 

}); 

我很欣賞任何反饋,我是新的d3!提前致謝!

回答

1
xAxis.tickFormat(d3.format('0f')); 
+1

你救了我,謝謝你! –