Skip to content

Instantly share code, notes, and snippets.

@ChandrakantThakkarDigiCorp
Last active November 8, 2017 13:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChandrakantThakkarDigiCorp/6792d8c559c1939e3313162061747b14 to your computer and use it in GitHub Desktop.
Save ChandrakantThakkarDigiCorp/6792d8c559c1939e3313162061747b14 to your computer and use it in GitHub Desktop.
Step Chart D3 V4
<html>
<head>
<title>Chandrakant Thakkar</title>
</head>
<body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="lineSteps.js"></script>
<script>
window.addEventListener('resize', function (event) {
$("#chart").width(window.innerWidth * 0.9);
$("#chart").height(window.innerHeight);
});
</script>
<div id="chart" style="width: 800;height: 100px;">
</div>
<div id="tooltipDiv" style="display:none;background:#3880aa;opacity:0.8;position: absolute;padding: 3;">
<div id="startDate"></div>
<div id="endDate"></div>
<div id="processType"></div>
</div>
<script>
let data = [{
start_date: "2017-09-04 07:00:00",
end_date: "2017-09-08 5:00:00",
processType: "cleaning"
}, {
start_date: "2017-09-08 5:00:00",
end_date: "2017-09-15 6:00:00",
processType: "batch"
}, {
start_date: "2017-09-15 6:00:00",
end_date: "2017-09-20 7:00:00",
processType: "waiting"
}, {
start_date: "2017-09-20 7:00:00",
end_date: "2017-09-24 8:00:00",
processType: "batch"
}, {
start_date: "2017-09-24 8:00:00",
end_date: "2017-09-28 9:00:00",
processType: "cleaning"
}, {
start_date: "2017-09-28 9:00:00",
end_date: "2017-10-07 10:00:00",
processType: "waiting"
}, {
start_date: "2017-10-07 10:00:00",
end_date: "2017-10-12 11:00:00",
processType: "batch"
}, {
start_date: "2017-10-12 11:00:00",
end_date: "2017-10-15 12:00:00",
processType: "cleaning"
}, {
start_date: "2017-10-15 12:00:00",
end_date: "2017-10-20 13:00:00",
processType: "batch"
}, {
start_date: "2017-10-20 13:00:00",
end_date: "2017-10-29 14:00:00",
processType: "waiting"
}, {
start_date: "2017-10-29 14:00:00",
end_date: "2017-11-04 15:00:00",
processType: "batch"
}];
$("#chart").empty();
var chartConfig = {
mainDiv: "#chart",
data: data,
lineColor: " #3880aa",
upLineValue: ["batch"],
typeFields: "processType",
minValueField: "start_date",
maxValueField: "end_date"
};
var lineStepCharts = new lineStepChart(chartConfig);
</script>
</body>
</html>
MIT License
Copyright (c) [2017] [Chandrakant Thakkar]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
function lineStepChart(config) {
function setReSizeEvent(data) {
var resizeTimer;
var interval = 500;
window.removeEventListener("resize", function() {});
window.addEventListener("resize", function(event) {
if (resizeTimer !== false) {
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function() {
$(data.mainDiv).empty();
drawLineStepsChart(data);
clearTimeout(resizeTimer);
}, interval);
});
}
drawLineStepsChart(config);
setReSizeEvent(config);
}
function createLineStepChartLegend(mainDiv, data) {
var z = d3.scaleOrdinal(d3.schemeCategory20);
var mainDivName = mainDiv.substr(1, mainDiv.length);
$(mainDiv).before(
"<div id='Legend_" +
mainDivName +
"' class='pmd-card-body' style='margin-left:20px; margin-top:0; margin-bottom:0;'></div>"
);
data.forEach(function(d) {
var cloloCode = z(d);
$("#Legend_" + mainDivName).append(
"<span class='team-graph team1' style='display: inline-block; margin-right:10px;'>\
<span style='background:" +
cloloCode +
";width: 10px;height: 10px;display: inline-block;vertical-align: middle;opacity:0.3 '>&nbsp;</span>\
<span style='padding-top: 0;font-family:Source Sans Pro, sans-serif;font-size: 13px;display: inline;'>" +
d +
" </span>\
</span>"
);
});
}
function drawLineStepsChart(config) {
var data = config.data;
var lineColor = config.lineColor;
var upLineValue = config.upLineValue;
var typeFields = config.typeFields;
var mainDiv = config.mainDiv;
var minValueField = config.minValueField;
var maxValueField = config.maxValueField;
var mainDivName = mainDiv.substr(1, mainDiv.length);
var z = d3.scaleOrdinal(d3.schemeCategory20);
var legendData = new Set(
data.map(function(d) {
return d[typeFields];
})
);
createLineStepChartLegend(mainDiv, legendData);
d3
.select(mainDiv)
.append("svg")
.attr("width", $(mainDiv).width())
.attr("height", $(mainDiv).height());
var svg = d3.select(mainDiv + " svg"),
margin = {
top: 20,
right: 20,
bottom: 40,
left: 20
},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var g = svg
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var minDateLabelg = svg
.append("g")
.attr("transform", "translate(" + 0 + "," + 0 + ")");
var maxDateLabelg = svg
.append("g")
.attr("transform", "translate(" + 0 + "," + 0 + ")");
data.map(function(d) {
d[minValueField] = new Date(d[minValueField]);
d[maxValueField] = new Date(d[maxValueField]);
});
var minValueDate = d3.min(data, function(d) {
return d[minValueField];
});
var maxValueDate = d3.max(data, function(d) {
return d[maxValueField];
});
var minValue = new Date(minValueDate).getTime() / (1000 * 60);
var maxValue = new Date(maxValueDate).getTime() / (1000 * 60);
var diffInMinutes = maxValue - minValue;
var x = d3.scaleLinear().range([0, width]);
x.domain([0, diffInMinutes]);
var y = d3.scaleLinear().range([height, 0]);
y.domain([0, height]);
minDateLabelg
.append("text")
.attr("id", "textMinDateLabelg")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("font-size", "12px")
.append("tspan")
.attr("x", 0)
.attr("y", height + margin.bottom)
.text(d3.timeFormat("%Y-%m-%d %H:%M:%S")(minValueDate));
maxDateLabelg
.append("text")
.attr("id", "textmaxDateLabelg")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("font-size", "12px")
.append("tspan")
.attr("x", $(mainDiv).width())
.attr("y", height + margin.bottom)
.text(d3.timeFormat("%Y-%m-%d %H:%M:%S")(maxValueDate));
var dims = helpers.getDimensions("textmaxDateLabelg");
d3
.selectAll("#textmaxDateLabelg tspan")
.attr("x", $(mainDiv).width() - dims.w);
var prevX = 0;
var prevY = 0;
var yPos = 0;
var pathg = "";
var rectg = "";
data.map(function(d, i) {
if (upLineValue.indexOf(d[typeFields]) != -1) {
yPos = height / 2;
} else {
yPos = height;
}
if (i == 0) {
var startDateValue = d[minValueField].getTime() / (1000 * 60) - minValue;
var endDateValue = d[maxValueField].getTime() / (1000 * 60) - minValue;
pathg = g
.append("path")
.attr("class", "line")
.attr("stroke", lineColor)
.attr("stroke-width", "3px")
.attr(
"d",
"M " +
x(startDateValue) +
" " +
yPos +
" L " +
x(endDateValue) +
" " +
yPos
);
rectg = g
.append("rect")
.attr("class", "rectBackground")
.attr("x", x(startDateValue))
.attr("width", x(endDateValue) - x(startDateValue))
.attr("height", height)
.style("fill", z(d[typeFields]))
.style("opacity", "0.3");
prevX = x(endDateValue);
prevY = yPos;
} else {
var startDateValue = d[minValueField].getTime() / (1000 * 60) - minValue;
var endDateValue = d[maxValueField].getTime() / (1000 * 60) - minValue;
if (prevY != yPos) {
g
.append("path")
.attr("class", "line")
.attr("stroke", lineColor)
.attr("stroke-width", "3px")
.attr(
"d",
"M " +
x(startDateValue) +
" " +
prevY +
" L " +
x(startDateValue) +
" " +
yPos
);
pathg = g
.append("path")
.attr("class", "line")
.attr("stroke", lineColor)
.attr("stroke-width", "3px")
.attr(
"d",
"M " +
x(startDateValue) +
" " +
yPos +
" L " +
x(endDateValue) +
" " +
yPos
);
} else {
pathg = g
.append("path")
.attr("class", "line")
.attr("stroke", lineColor)
.attr("stroke-width", "3px")
.attr(
"d",
"M " +
x(startDateValue) +
" " +
yPos +
" L " +
x(endDateValue) +
" " +
yPos
);
}
rectg = g
.append("rect")
.attr("class", "rectBackground")
.attr("x", x(startDateValue))
.attr("width", x(endDateValue) - x(startDateValue))
.attr("height", height)
.style("fill", z(d[typeFields]))
.style("opacity", "0.3");
prevY = yPos;
}
rectg
.on("mouseover", () => d3.select("#tooltipDiv").style("display", null))
.on("mouseout", () => d3.select("#tooltipDiv").style("display", "none"))
.on("mousemove", () => mousemove(this, d));
});
function mousemove(e, data) {
d3.select("#tooltipDiv").style("top", d3.event.pageY + 15);
d3.select("#tooltipDiv").style("left", d3.event.pageX - 10);
d3
.select("#startDate")
.html(
"Start Date:" + d3.timeFormat("%Y-%m-%d %H:%M:%S")(data[minValueField])
);
d3
.select("#endDate")
.html(
"End Date:" + d3.timeFormat("%Y-%m-%d %H:%M:%S")(data[maxValueField])
);
d3.select("#processType").html("Process Type:" + data[typeFields]);
}
}
var helpers = {
getDimensions: function(id) {
var el = document.getElementById(id);
var w = 0,
h = 0;
if (el) {
var dimensions = el.getBBox();
w = dimensions.width;
h = dimensions.height;
} else {
console.log("error: getDimensions() " + id + " not found.");
}
return {
w: w,
h: h
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment