Reputation: 11
I am using d3 to create a chart with two scales on y axis one towards left and other towards right of the graph.
On Left Y Axis I am trying to plot Revenue vs Date and on right I am trying to plot Clicks vs Date.
The max value for Revenue is let say $1800 and for clicks is 4000. The blue line is the revenue line and orange line is the clicks.
Can someone help me how can I get the orange line fit in the same y domain?
Code I am using for Revenue is:
var y = d3.scale.linear()
.domain([min_y_value, max_y_value])
.range([height - margin, margin]);
var x = d3.time.scale()
.domain([start_date, end_date])
.range([0 + margin, width - margin]);
var color = d3.scale.category10().range();
var vis = d3.select("#chart")
.append("svg:svg")
.attr("width", width)
.attr("height", height);
var g = vis.append("svg:g")
.attr("transform", "translate("+margin+",0)");
var line = d3.svg.line()
.x(function(d,i) {
return x(get_date(d.date));
})
.y(function(d) {
return y(d[trend]);
});
for(var i = 0; i < report.length; i++) {
g.append("svg:path")
.attr("d", line(report[i]))
.attr("fill", "none")
.attr("stroke", color[i % 10])
.attr("id", "line-"+report[i][0]['offer_name'])
.attr("stroke-width", 2);
}
Code I am using for Clicks is
var trend1 = 'clicks';
var max_y1_value = max_min['max_'+trend1];
var min_y1_value = max_min['min_'+trend1];
var y1_range = max_y1_value - min_y1_value;
var y1_interval_value = Math.floor(y1_range/yticks);
max_y1_value = max_y_value + y_interval_value;
min_y1_value = (min_y_value - y_interval_value > 0)?(min_y_value - y_interval_value) : 0;
var y1 = d3.scale.linear().domain([min_y1_value, max_y1_value]).range([height - margin, margin]);
var x1 = d3.time.scale().domain([start_date, end_date]).range([0 + margin, width - margin]);
console.log('max_y_value : '+max_y_value+' min_y_value : '+min_y_value+' max_y1_value : '+max_y1_value+' min_y1_value : '+min_y1_value+' trend1: '+trend1);
var line1 = d3.svg.line()
.x(function(d,i) {
return x1(get_date(d.date));
})
.y(function(d) {
console.log(d[trend1]+' '+y1(d[trend1]));
return y1(d[trend1]);
});
for(var i = 0; i < report.length; i++) {
g.append("svg:path")
.attr("d", line1(report[i]))
.attr("fill", "none")
.attr("stroke", color[i + report.length % 10])
.attr("id", "line1-"+report[i][0]['offer_name'])
.attr("stroke-width", 2);
}
Can someone help me how can I get the orange line fit in the same y domain?
Upvotes: 1
Views: 972
Reputation: 51839
It looks like you are overwriting your max_y1_value
and min_y1_value
with the values from the other series. For example, you first define max_y1_value
:
var max_y1_value = max_min['max_'+trend1];
And then later you replace with with another value:
max_y1_value = max_y_value + y_interval_value;
I recommend using d3.extent to compute the domain from data.
Upvotes: 1