在 x 轴 d3 上正确格式化年份
Correctly format year on x axis d3
我正在使用 d3 中的散点图。x 轴表示年份。我从数据库中获取信息并将其绘制为图形上的圆圈。我的问题是如何解析 x 值以将其格式化为年份?即。目前它显示为1,999
,但应该看起来像1999
。有人可以告诉我如何正确解析它以获得正确的格式!以下是相关代码:
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的新手!提前感谢!
xAxis.tickFormat(d3.format('0f'));
相关文章:
- D3在一个调用中绘制不同的SVG形状,没有可见性
- jQuery中是否内置了任何字符串格式化函数
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 如何在d3.js中返回路径的y坐标
- d3基于用户选择动态更新节点
- 有条件更新d3.js力图中节点的最佳方法
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 使用D3.js计算带有字母间距的文本长度
- 为D3中的网络图收集一些数据.我应该如何格式化它
- 在 x 轴 d3 上正确格式化年份
- 尝试使用 d3.time.format 格式化日期时,我收到 JavaScript 错误“d.getMonth 不是一个
- 格式化D3.js月底图表
- 如何在(Angular)JS中为D3树形图格式化JS对象?
- d3.在变量上格式化千位分隔符
- 使用d3.js工具在javascript中格式化日期
- 如何在D3.js /C3.js中格式化数字
- D3:使用. tickvalues()方法格式化年份
- D3在格式化时间时添加19小时
- 格式化大美元值与单一数据点在D3
- I'在D3.js中使用工具提示(或标签?)时,我在格式化方面遇到了问题