D3 multiseries chart y-axis label issue
I tried to use the d3 multiseries chart. The example works perfectly. But
when I applied my data to the chart, I found the labels not aligned with
the actual value of the data. For example, the value 1 is aligned with
about 0.75 in the y-axis. My code is basically the same as the example
code. Could anyone please tell me which part I should revise? Thanks!
var margin = { top: 150, right: 150, bottom: 30, left: 50 },
width = 1080 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%H:%M").parse;
var x = d3.time.scale()
.range([0, width + 30]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
// var color = d3.scale.ordinal().range(["#98abc5", "#8a89a6",
"#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.interpolate("basis")
.x(function (d) { return x(d.time); })
.y(function (d) { return y(d.count); });
var svg = d3.select("#line2").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 + ")");
d3.tsv(filename_weekend, function (error, data) {
color.domain(d3.keys(data[0]).filter(function (key) {
// selectedColumns
return key !== "time" && key !== "Receiving Notification";
}
));
data.forEach(function (d) {
d.time = parseDate(d.time);
});
var triggers = color.domain().map(function (name) {
return {
name: name,
values: data.map(function (d) {
return { time: d.time, count: +d[name] };
})
};
});
x.domain(d3.extent(data, function (d) { return d.time; }));
y.domain([
d3.min(triggers, function (c) { return d3.min(c.values,
function (v) { return v.count; }); }),
d3.max(triggers, function (c) { return d3.max(c.values,
function (v) { return v.count; }); })
]);
//x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
//y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Total");
//legend
var chart = svg.selectAll('.trigger')
.data(triggers)
.enter()
.append('g')
.attr('class', 'trigger');
chart.append("path")
.attr("class", "line")
.attr("d", function (d) { return line(d.values); })
.style("stroke", function (d) { return color(d.name); });
No comments:
Post a Comment