I am trying to stabilize this chart - I've tried to compare date objects - but then it ran into the issue of summer time differences - I am unsure why the change in data breaks the chart - I am getting an undefined issue.
//working js fiddle http://jsfiddle.net/Qh9X5/10379/
//broken js fiddle http://jsfiddle.net/Qh9X5/10380/
-- so when adding the extra bit of data to the chart - it creates an undefined error
, {
"text-start": "Text9",
"date-start": "2012-05-01",
"text-end": "Text10",
"date-end": "2012-05-03"
}
I thought at first that maybe the yAxis needs to be forced to show 1 day increments -- .ticks(d3.time.days(min, max).length) as if maybe the coordinates do not exist for in-between days.
here is the code for the working chart
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);
})
chart.call(yAxis).selectAll(".tick")
has one less result than yourarrayofDates
, then there won't be a match ingetPos()
-if (a == b)
. There are some results when searching for "d3 missing first item", but I have no idea how this applies to your code.