{
var svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
var focusChartMargin = { top: 20, right: 20, bottom: 170, left: 60 };
var contextChartMargin = { top: 360, right: 20, bottom: 90, left: 60 };
var chartWidth = width - focusChartMargin.left - focusChartMargin.right;
var focusChartHeight = height - focusChartMargin.top - focusChartMargin.bottom;
var contextChartHeight = height - contextChartMargin.top - contextChartMargin.bottom;
svg
.append("svg")
.attr("width", chartWidth + focusChartMargin.left + focusChartMargin.right)
.attr("height", focusChartHeight + focusChartMargin.top + focusChartMargin.bottom)
.append("g")
.attr("transform", "translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")");
var parseTime = d3.timeParse("%H:%M");
var dates = [];
for (let key of Object.keys(data)) {
data[key].forEach(bucketRecord => {
dates.push(parseTime(bucketRecord.date));
});
}
//get max Y axis value by searching for the highest conversion rate
var maxYAxisValue = -Infinity;
for (let key of Object.keys(data)) {
let maxYAxisValuePerBucket = Math.ceil(d3.max(data[key], d => d["conversion"]));
maxYAxisValue = Math.max(maxYAxisValuePerBucket, maxYAxisValue);
}
// set the height of both y axis
var yFocus = d3.scaleLinear().range([focusChartHeight, 0]);
var yContext = d3.scaleLinear().range([contextChartHeight, 0]);
// set the width of both x axis
var xFocus = d3.scaleTime().range([0, chartWidth]);
var xContext = d3.scaleTime().range([0, chartWidth]);
// create both x axis to be rendered
var xAxisFocus = d3
.axisBottom(xFocus)
.ticks(10)
.tickFormat(d3.timeFormat("%I:%M%p"));
var xAxisContext = d3
.axisBottom(xContext)
.ticks(10)
.tickFormat(d3.timeFormat("%I:%M%p"));
// create the one y axis to be rendered
var yAxisFocus = d3.axisLeft(yFocus).tickFormat(d => d + "%");
// build brush
var brush = d3
.brushX()
.extent([
[0, -10],
[chartWidth, contextChartHeight],
])
.on("brush end", brushed);
// build zoom for the focus chart
// as specified in "filter" - zooming in/out can be done by pinching on the trackpad while mouse is over focus chart
// zooming in can also be done by double clicking while mouse is over focus chart
var zoom = d3
.zoom()
.scaleExtent([1, Infinity])
.translateExtent([
[0, 0],
[chartWidth, focusChartHeight],
])
.extent([
[0, 0],
[chartWidth, focusChartHeight],
])
.on("zoom", zoomed)
.filter(() => d3.event.ctrlKey || d3.event.type === "dblclick" || d3.event.type === "mousedown");
// create a line for focus chart
var lineFocus = d3
.line()
.x(d => xFocus(parseTime(d.date)))
.y(d => yFocus(d.conversion));
// create line for context chart
var lineContext = d3
.line()
.x(d => xContext(parseTime(d.date)))
.y(d => yContext(d.conversion));
// es lint disabled here so react won't warn about not using variable "clip"
/* eslint-disable */
// clip is created so when the focus chart is zoomed in the data lines don't extend past the borders
var clip = svg
.append("defs")
.append("svg:clipPath")
.attr("id", "clip")
.append("svg:rect")
.attr("width", chartWidth)
.attr("height", focusChartHeight)
.attr("x", 0)
.attr("y", 0);
// append the clip
var focusChartLines = svg
.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")")
.attr("clip-path", "url(#clip)");
/* eslint-enable */
// create focus chart
var focus = svg
.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")");
// create context chart
var context = svg
.append("g")
.attr("class", "context")
.attr("transform", "translate(" + contextChartMargin.left + "," + (contextChartMargin.top + 50) + ")");
// add data info to axis
xFocus.domain(d3.extent(dates));
yFocus.domain([0, maxYAxisValue]);
xContext.domain(d3.extent(dates));
yContext.domain(yFocus.domain());
// add axis to focus chart
focus
.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0," + focusChartHeight + ")")
.call(xAxisFocus);
focus
.append("g")
.attr("class", "y-axis")
.call(yAxisFocus);
// get list of bucket names
var bucketNames = [];
for (let key of Object.keys(data)) {
bucketNames.push(key);
}
// match colors to bucket name
var colors = d3
.scaleOrdinal()
.domain(bucketNames)
.range(["#3498db", "#3cab4b", "#e74c3c", "#73169e", "#2ecc71"]);
// go through data and create/append lines to both charts
for (let key of Object.keys(data)) {
let bucket = data[key];
focusChartLines
.append("path")
.datum(bucket)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", d => colors(key))
.attr("stroke-width", 1.5)
.attr("d", lineFocus);
context
.append("path")
.datum(bucket)
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", d => colors(key))
.attr("stroke-width", 1.5)
.attr("d", lineContext);
}
// add x axis to context chart (y axis is not needed)
context
.append("g")
.attr("class", "x-axis")
.attr("transform", "translate(0," + contextChartHeight + ")")
.call(xAxisContext);
// add bush to context chart
var contextBrush = context
.append("g")
.attr("class", "brush")
.call(brush);
// style brush resize handle
var brushHandlePath = d => {
var e = +(d.type === "e"),
x = e ? 1 : -1,
y = contextChartHeight + 10;
return (
"M" +
0.5 * x +
"," +
y +
"A6,6 0 0 " +
e +
" " +
6.5 * x +
"," +
(y + 6) +
"V" +
(2 * y - 6) +
"A6,6 0 0 " +
e +
" " +
0.5 * x +
"," +
2 * y +
"Z" +
"M" +
2.5 * x +
"," +
(y + 8) +
"V" +
(2 * y - 8) +
"M" +
4.5 * x +
"," +
(y + 8) +
"V" +
(2 * y - 8)
);
};
var brushHandle = contextBrush
.selectAll(".handle--custom")
.data([{ type: "w" }, { type: "e" }])
.enter()
.append("path")
.attr("class", "handle--custom")
.attr("stroke", "#000")
.attr("cursor", "ew-resize")
.attr("d", brushHandlePath);
// overlay the zoom area rectangle on top of the focus chart
svg
.append("rect")
.attr("cursor", "move")
.attr("fill", "none")
.attr("pointer-events", "all")
.attr("class", "zoom")
.attr("width", chartWidth)
.attr("height", focusChartHeight)
.attr("transform", "translate(" + focusChartMargin.left + "," + focusChartMargin.top + ")")
.call(zoom);
contextBrush.call(brush.move, [0, chartWidth / 2]);
// focus chart x label
focus
.append("text")
.attr("transform", "translate(" + chartWidth / 2 + " ," + (focusChartHeight + focusChartMargin.top + 25) + ")")
.style("text-anchor", "middle")
.style("font-size", "18px")
.text("Time (UTC)");
// focus chart y label
focus
.append("text")
.attr("text-anchor", "middle")
.attr("transform", "translate(" + (-focusChartMargin.left + 20) + "," + focusChartHeight / 2 + ")rotate(-90)")
.style("font-size", "18px")
.text("Conversion Rate");
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || xContext.range();
xFocus.domain(s.map(xContext.invert, xContext));
focusChartLines.selectAll(".line").attr("d", lineFocus);
focus.select(".x-axis").call(xAxisFocus);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity.scale(chartWidth / (s[1] - s[0])).translate(-s[0], 0));
brushHandle
.attr("display", null)
.attr("transform", (d, i) => "translate(" + [s[i], -contextChartHeight - 20] + ")");
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
var t = d3.event.transform;
xFocus.domain(t.rescaleX(xContext).domain());
focusChartLines.selectAll(".line").attr("d", lineFocus);
focus.select(".x-axis").call(xAxisFocus);
var brushSelection = xFocus.range().map(t.invertX, t);
context.select(".brush").call(brush.move, brushSelection);
brushHandle
.attr("display", null)
.attr("transform", (d, i) => "translate(" + [brushSelection[i], -contextChartHeight - 20] + ")");
}
return svg.node()
}