【发布时间】:2017-09-16 07:50:00
【问题描述】:
我正在尝试稳定此图表 - 我尝试比较日期对象 - 但后来遇到了夏令时差异的问题 - 我不确定为什么数据的变化会破坏图表 - 我遇到了一个未定义的问题.
//工作js小提琴 http://jsfiddle.net/Qh9X5/10379/
//破碎的js小提琴 http://jsfiddle.net/Qh9X5/10380/
-- 所以当向图表添加额外的数据时 - 它会产生一个未定义的错误
, {
"text-start": "Text9",
"date-start": "2012-05-01",
"text-end": "Text10",
"date-end": "2012-05-03"
}
起初我认为可能需要强制 yAxis 显示 1 天的增量 - .ticks(d3.time.days(min, max).length) 好像中间不存在坐标天。
这是工作图表的代码
var data = [{
"text-start": "Text1",
"date-start": "2012-04-21",
"text-end": "Text2",
"date-end": "2012-04-26"
}, {
"text-start": "Text3",
"date-start": "2012-04-22",
"text-end": "Text4",
"date-end": "2012-04-25"
}, {
"text-start": "Text5",
"date-start": "2012-04-26",
"text-end": "Text6",
"date-end": "2012-04-28"
}, {
"text-start": "Text7",
"date-start": "2012-04-21",
"text-end": "Text8",
"date-end": "2012-04-23"
}];
var margin = {
top: 40,
right: 40,
bottom: 40,
left: 60
},
width = 600,
height = 500;
function colores_google(n) {
var colores_g = ["#e9168a", "#f8dd2f", "#448875", "#c3bd75", "#2b2d39", "#311854", "#553814", "#f7b363", "#89191d", "#c12f34", "#2b2a2c", "#c5b8a6", "#57585b"];
return colores_g[n % colores_g.length];
}
var y = d3.time.scale().range([height - margin.top - margin.bottom, 0]);
var arrayofDates = [];
$.each(data, function(index, value) {
arrayofDates.push(value["date-start"]);
arrayofDates.push(value["date-end"]);
});
var parseDate = d3.time.format("%m/%d/%Y").parse;
var min = new Date(d3.min(arrayofDates.map(d=>d)));
var max = new Date(d3.max(arrayofDates.map(d=>d)));
y.domain([min,max]);
var chart = d3.select('#timelines').append('svg')
.attr('class', 'chart')
.attr('width', width)
.attr('height', height)
.append('g')
.attr('transform', 'translate(' + margin.left + ', ' + margin.top + ')');
//http://stackoverflow.com/questions/22722952/how-can-i-get-the-d3-js-axis-ticks-and-positions-as-an-array
var axisArray = [];
var yAxis = d3.svg.axis()
.scale(y)
.orient('left')
.tickPadding(1);
chart.append('g')
.attr('class', 'y axis')
.call(yAxis)
.selectAll(".tick").each(function(data) {
var tick = d3.select(this);
// pull the transform data out of the tick
var transform = d3.transform(tick.attr("transform")).translate;
// passed in "data" is the value of the tick, transform[0] holds the X value
//console.log("each tick", data, transform);
var month = data.getMonth() + 1;
if (month < 10) {
month = "0" + month;
}
var datestring = data.getFullYear() + "-" + month + "-" + data.getDate();
var obj = {
"data": datestring,
"transform": transform
}
axisArray.push(obj);
});
var links = chart.append('g')
.attr('class', 'links')
.attr("transform", "translate(0,0)");
function getRadius(d) {
var count = 2;
var ratio = count * 2.8;
if (count == 1) {
ratio = 10;
}
return ratio;
}
function getPos(date) {
var trans;
$.each(axisArray, function(index, value) {
var a = value.data;
var b = date;
//console.log("a", a)
//console.log("b", b)
if (a == b) {
//console.log("value", value.transform)
//if (a.getTime() === b.getTime()) {
trans = value.transform;
}
});
return trans;
}
var distanceBetween = 70;
var paths = links.selectAll("path")
.data(data);
paths.enter()
.append("path")
.attr("class", "link")
.attr("d", function(d, i) {
var sx = 0;
var tx = 0;
//console.log("d", d);
var posEnd = getPos(d["date-start"]);
var posStart = getPos(d["date-end"]);
var sy = posStart[1];
var ty = posEnd[1];
var dx = 0,
dy = getRadius(d) + 15 + (distanceBetween * i),
dr = 10 //Math.sqrt(dx * dx + dy * dy);
return "M" + sx + "," + sy + "A" + dr + "," + dr + " 0 0,1 " + tx + "," + ty;
})
.attr('stroke-width', 1.2)
.attr("stroke", function(d, i) {
//console.log("d", i);
return colores_google(i);
})
【问题讨论】:
-
posEnd[1] - 变得未定义 - 但我不确定为什么 - 或者是什么导致了这个错误
-
问题似乎是
chart.call(yAxis).selectAll(".tick")的结果比你的arrayofDates少一个,那么getPos()-if (a == b)就不会匹配。搜索“d3 missing first item”时有一些结果,但我不知道这如何适用于您的代码。 -
@chrki 是什么导致了这个缺失的勾号——也许一旦我计算了最小/最大日期——应该在前一天/后一天突破界限?
-
我尝试将域推送过去/未来 -- 但仍然失败 -- jsfiddle.net/Qh9X5/10389
-
var past = new Date(min.getTime());过去.setDate(min.getDate() - 1); var future = new Date(max.getTime());未来.setDate(max.getDate() + 1); y.domain([过去,未来]);
标签: javascript jquery d3.js