Skip to content

Instantly share code, notes, and snippets.

@ChandrakantThakkarDigiCorp
Last active May 26, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChandrakantThakkarDigiCorp/0620e41591f278431b8b98bfdf361b40 to your computer and use it in GitHub Desktop.
Save ChandrakantThakkarDigiCorp/0620e41591f278431b8b98bfdf361b40 to your computer and use it in GitHub Desktop.
Manhattan Chart D3 V4
<html>
<body>
<script>
window.addEventListener('resize', function (event) {
$("#chart").width(window.innerWidth * 0.9);
$("#chart").height(window.innerHeight);
});
</script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="manhattanChart.js"></script>
<div id="chart" style="width: 800;height: 500">
</div>
<script>
var groupChartData = [{"2614":8,"4449":15,"over":1,"circle_4449":[],"circle_2614":[]},{"2614":7,"4449":2,"over":2,"circle_4449":[{"commentary":"1.4 Cric Jagat Malay OUT","how_to_out":" b Mohit nathani 14 (9b 2x4 1x6) SR: 155.00","fall_of_wicket":"16/1"}],"circle_2614":[{"commentary":"1.4 Savvy Jineshbhai OUT","how_to_out":" b Cric Bitu Malay 5 (6b 1x4 0x6) SR: 83.00","fall_of_wicket":"12/1"},{"commentary":"1.5 Krupal Cricket OUT","how_to_out":" run out Cric Bitu Malay 0 (1b 0x4 0x6) SR: 0.00","fall_of_wicket":"14/2"}]},{"2614":4,"4449":5,"over":3,"circle_4449":[],"circle_2614":[]},{"2614":19,"4449":8,"over":4,"circle_4449":[],"circle_2614":[]},{"2614":3,"4449":7,"over":5,"circle_4449":[],"circle_2614":[]},{"2614":6,"4449":1,"over":6,"circle_4449":[{"commentary":"5.6 Nandish Thackar OUT","how_to_out":" c †Mohit nathani b Savvy Rishi 16 (16b 2x4 0x6) SR: 100.00","fall_of_wicket":"38/2"}],"circle_2614":[]},{"2614":7,"4449":6,"over":7,"circle_4449":[],"circle_2614":[{"commentary":"6.6 Alpesh Patel OUT","how_to_out":" run out Nandish Thackar 10 (11b 1x4 0x6) SR: 90.00","fall_of_wicket":"54/3"}]},{"2614":13,"4449":2,"over":8,"circle_4449":[{"commentary":"7.2 Chirag Panchal OUT","how_to_out":" c †Mohit nathani b Savvy Rishi 1 (3b 0x4 0x6) SR: 33.00","fall_of_wicket":"44/3"}],"circle_2614":[]},{"2614":1,"4449":8,"over":9,"circle_4449":[],"circle_2614":[]},{"2614":8,"4449":9,"over":10,"circle_4449":[],"circle_2614":[]},{"2614":4,"4449":8,"over":11,"circle_4449":[],"circle_2614":[{"commentary":"10.1 Savvy Nikunj OUT","how_to_out":" c Nandish Thackar b Raj deep Gohil 9 (14b 0x4 0x6) SR: 64.00","fall_of_wicket":"76/4"}]},{"2614":0,"4449":20,"over":12,"circle_4449":[],"circle_2614":[]},{"2614":8,"4449":3,"over":13,"circle_4449":[],"circle_2614":[{"commentary":"12.4 Tejas Shah OUT","how_to_out":" c Kushal Jindal b Cric Malay 54 (39b 5x4 4x6) SR: 138.00","fall_of_wicket":"88/5"}]},{"2614":5,"4449":9,"over":14,"circle_4449":[{"commentary":"13.5 Cric Malay OUT","how_to_out":" c Savvy Rishi b Mohit nathani 39 (25b 5x4 2x6) SR: 156.00","fall_of_wicket":"102/4"}],"circle_2614":[]},{"2614":7,"4449":5,"over":15,"circle_4449":[],"circle_2614":[]},{"2614":9,"4449":14,"over":16,"circle_4449":[{"commentary":"15.3 Keval Gandhi OUT","how_to_out":" b Mohit nathani 3 (4b 0x4 0x6) SR: 75.00","fall_of_wicket":"113/5"}],"circle_2614":[]},{"2614":17,"4449":15,"over":17,"circle_4449":[{"commentary":"16.1 Kushal Jindal OUT","how_to_out":" c Savvy Sunil Kakani b Lokesh Thakkar 34 (38b 4x4 0x6) SR: 89.00","fall_of_wicket":"122/6"}],"circle_2614":[{"commentary":"16.1 Savvy Rishi OUT","how_to_out":" st †Cric Malay b Raj deep Gohil 9 (15b 1x4 0x6) SR: 60.00","fall_of_wicket":"109/6"}]},{"2614":21,"4449":10,"over":18,"circle_4449":[],"circle_2614":[]},{"2614":10,"4449":3,"over":19,"circle_4449":[{"commentary":"18.2 Raj deep Gohil OUT","how_to_out":" c Himanshu Cricket b Savvy Nikunj 3 (4b 0x4 0x6) SR: 75.00","fall_of_wicket":"148/7"},{"commentary":"18.4 Cric Bapu Malay OUT","how_to_out":" c †Savvy Rishi b Savvy Nikunj 0 (1b 0x4 0x6) SR: 0.00","fall_of_wicket":"149/8"}],"circle_2614":[]},{"2614":0,"4449":5,"over":20,"circle_4449":[{"commentary":"19.5 Ketan Thaker OUT","how_to_out":" run out Savvy Rishi 31 (16b 2x4 3x6) SR: 193.00","fall_of_wicket":"154/9"}]}];
var columnsInfo = { "2614": "Team A", "4449": "Team B" };
$("#chart").empty();
var barChartConfig = {
mainDiv: "#chart",
colorRange: ["#2a98cd", "#df7247"],
data: groupChartData,
columnsInfo: columnsInfo,
xAxis: "over",
yAxis: "runs",
label: {
xAxis: "Over",
yAxis: "Runs"
},
requireLegend: true
};
var groupChart = new groupBarChart(barChartConfig);
</script>
</body>
</html>
MIT License
Copyright (c) [2017] [DIGI-CORP]
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 groupBarChart(config) {
function setReSizeEvent(data) {
var resizeTimer;
window.removeEventListener('resize', function () {
});
window.addEventListener('resize', function (event) {
if (resizeTimer !== false) {
clearTimeout(resizeTimer);
}
resizeTimer = setTimeout(function () {
$(data.mainDiv).empty();
drawgroupBarChartChart(data);
clearTimeout(resizeTimer);
}, 500);
});
}
drawgroupBarChartChart(config);
setReSizeEvent(config);
}
function creategroupBarChartLegend(mainDiv, columnsInfo, colorRange) {
var z = d3.scaleOrdinal()
.range(colorRange);
var mainDivName = mainDiv.substr(1, mainDiv.length);
$(mainDiv).before("<div id='Legend_" + mainDivName + "' style='margin-top:0; margin-bottom:0;'></div>");
var keys = Object.keys(columnsInfo);
keys.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;'>&nbsp;</span>\
<span style='padding-top: 0;font-family:Source Sans Pro, sans-serif;font-size: 13px;display: inline;'>" + columnsInfo[d] + " </span>\
</span>");
});
}
function drawgroupBarChartChart(config) {
var data = config.data;
var columnsInfo = config.columnsInfo;
var xAxis = config.xAxis;
var yAxis = config.yAxis;
var colorRange = config.colorRange;
var mainDiv = config.mainDiv;
var mainDivName = mainDiv.substr(1, mainDiv.length);
var label = config.label;
var requireLegend = config.requireLegend;
d3.select(mainDiv).append("svg").attr("width", $(mainDiv).width()).attr("height", $(mainDiv).height() * 0.9);
var svg = d3.select(mainDiv + " svg"),
margin = { top: 20, right: 20, bottom: 30, left: 40 },
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 + ")");
if (requireLegend != null && requireLegend != undefined && requireLegend != false) {
$("#Legend_" + mainDivName).remove();
creategroupBarChartLegend(mainDiv, columnsInfo, colorRange);
}
var x0 = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.1);
var x1 = d3.scaleBand()
.padding(0.05);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range(colorRange);
var keys = Object.keys(columnsInfo);
x0.domain(data.map(function (d) {
return d[xAxis];
}));
x1.domain(keys).rangeRound([0, x0.bandwidth()]);
y.domain([0, d3.max(data, function (d) {
return d3.max(keys, function (key) {
return d[key];
});
})]).nice();
var element = g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function (d) {
return "translate(" + x0(d[xAxis]) + ",0)";
});
var rect = element.selectAll("rect")
.data(function (d, i) {
return keys.map(function (key) {
return { key: key, value: d[key], index: key + "_" + i + "_" + d[xAxis] };
});
})
.enter().append("rect")
.attr("x", function (d) {
return x1(d.key);
})
.attr("y", function (d) {
return y(d.value);
})
.attr("width", x1.bandwidth())
.attr("data-index", function (d, i) {
return d.index;
})
.attr("height", function (d) {
return height - y(d.value);
})
.attr("fill", function (d) {
return z(d.key);
});
//CBT:add tooltips
var self = {};
self.svg = svg;
self.cssPrefix = "groupBar0_";
self.data = data;
self.keys = keys;
self.height = height;
self.width = width;
self.label = label;
self.yAxis = yAxis;
self.xAxis = xAxis;
barTooltip.addTooltips(self);
rect.on("mouseover", function () {
var currentEl = d3.select(this);
var index = currentEl.attr("data-index");
barTooltip.showTooltip(self, index);
});
rect.on("mouseout", function () {
var currentEl = d3.select(this);
var index = currentEl.attr("data-index");
barTooltip.hideTooltip(self, index);
});
rect.on("mousemove", function () {
barTooltip.moveTooltip(self);
});
var circleRadius = x1.bandwidth() * 0.4;
var circles = element.selectAll("circle")
.data(function (d) {
var teamWiseData = keys.map(function (key) {
return { key: key, value: d[key], circles: d["circle_" + key], over: d.over };
});
var dataObj = [];
teamWiseData.forEach(function (teamData) {
if (teamData.circles != undefined && teamData.circles != 0 && teamData.circles.length > 0) {
teamData.circles.forEach(function (data, i) {
var tempObj = {};
tempObj["key"] = teamData["key"];
tempObj["value"] = teamData["value"];
tempObj["circles"] = [data];
tempObj["over"] = teamData["over"];
tempObj["circleNumber"] = i;
dataObj.push(tempObj);
});
} else {
teamData["circleNumber"] = 0;
dataObj.push(teamData);
}
});
return dataObj;
})
.enter().append("circle")
.attr("cx", function (d) {
return x1(d.key) + (x1.bandwidth() / 2);
})
.attr("cy", function (d) {
if ((y(d.value) - (circleRadius * 0.9)) - (d.circleNumber * (circleRadius * 2)) < 0) {
return (y(d.value) + (circleRadius)) + (d.circleNumber * (circleRadius * 2));
} else {
return (y(d.value) - (circleRadius * 0.9)) - (d.circleNumber * (circleRadius * 2));
}
})
.attr("r", circleRadius)//x1.bandwidth() * 0.37)
.attr("fill", "#fff")
.attr("stroke", function (d) {
return z(d.key);
})
.attr("data", function (d) {
var data = {};
data["over"] = d.over;
data["runs"] = d.value;
if (d.circles != undefined && d.circles.length > 0) {
data["circles"] = d.circles
}
else {
data["circles"] = [];
}
return JSON.stringify(data);
})
.attr("stroke-width", "2px").attr("display", function (d) {
if (d.circles == undefined || d.circles.length <= 0) {
return "none";
} else {
return "block";
}
});
circles.on("mouseover", function () {
var currentEl = d3.select(this);
currentEl.attr("r", parseFloat(currentEl.attr("r")) + 1);
var fadeInSpeed = 120;
d3.select("#circletooltip_" + mainDivName)
.transition()
.duration(fadeInSpeed)
.style("opacity", function () {
return 1;
});
d3.select("#circletooltip_" + mainDivName)
.attr("transform", function (d) {
var mouseCoords = d3.mouse(this.parentNode);
var xCo = 0;
if (mouseCoords[0] + 10 >= width * 0.80) {
xCo = mouseCoords[0] - parseFloat(d3.selectAll("#circletooltipRect_" + mainDivName)
.attr("width"));
} else {
xCo = mouseCoords[0] + 10;
}
var x = xCo;
var yCo = 0;
if (mouseCoords[0] + 10 >= width * 0.80) {
yCo = mouseCoords[1] + 10;
} else {
yCo = mouseCoords[1];
}
var x = xCo;
var y = yCo;
return "translate(" + x + "," + y + ")";
});
//CBT:calculate tooltips text
var wicketData = JSON.parse(currentEl.attr("data"));
var tooltipsText = "";
d3.selectAll("#circletooltipText_" + mainDivName).text("");
var yPos = 0;
d3.selectAll("#circletooltipText_" + mainDivName).append("tspan").attr("x", 0).attr("y", yPos * 10).attr("dy", "1.9em").text(label.xAxis + ": " + wicketData.over);
yPos = yPos + 1;
d3.selectAll("#circletooltipText_" + mainDivName).append("tspan").attr("x", 0).attr("y", yPos * 10).attr("dy", "1.9em").text(label.yAxis + ": " + wicketData.runs);
wicketData.circles.forEach(function (d, i) {
yPos = yPos + 1;
d3.selectAll("#circletooltipText_" + mainDivName).append("tspan").attr("x", 0).attr("y", yPos * 10).attr("dy", "1.9em").text(d["commentary"] + "(" + d.fall_of_wicket + ")");
});
//CBT:calculate width of the text based on characters
var dims = helpers.getDimensions("circletooltipText_" + mainDivName);
d3.selectAll("#circletooltipText_" + mainDivName + " tspan")
.attr("x", dims.w + 2);
d3.selectAll("#circletooltipRect_" + mainDivName)
.attr("width", dims.w + 10)
.attr("height", dims.h + 20);
});
circles.on("mousemove", function () {
var currentEl = d3.select(this);
d3.selectAll("#circletooltip_" + mainDivName)
.attr("transform", function (d) {
var mouseCoords = d3.mouse(this.parentNode);
var xCo = 0;
if (mouseCoords[0] + 10 >= width * 0.80) {
xCo = mouseCoords[0] - parseFloat(d3.selectAll("#circletooltipRect_" + mainDivName)
.attr("width"));
} else {
xCo = mouseCoords[0] + 10;
}
var yCo = 0;
if (mouseCoords[0] + 10 >= width * 0.80) {
yCo = mouseCoords[1] + 10;
} else {
yCo = mouseCoords[1];
}
var x = xCo;
var y = yCo;
return "translate(" + x + "," + y + ")";
});
});
circles.on("mouseout", function () {
var currentEl = d3.select(this);
currentEl.attr("r", parseFloat(currentEl.attr("r")) - 1);
d3.select("#circletooltip_" + mainDivName)
.style("opacity", function () {
return 0;
});
d3.select("#circletooltip_" + mainDivName).attr("transform", function (d, i) {
// klutzy, but it accounts for tooltip padding which could push it onscreen
var x = -500;
var y = -500;
return "translate(" + x + "," + y + ")";
});
});
//CBT:circle tooltips start
var circleTooltipg = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.attr("id", "circletooltip_" + mainDivName)
.attr("style", "opacity:0")
.attr("transform", "translate(-500,-500)");
circleTooltipg.append("rect")
.attr("id", "circletooltipRect_" + mainDivName)
.attr("x", 0)
.attr("width", 120)
.attr("height", 80)
.attr("opacity", 0.71)
.style("fill", "#000000");
circleTooltipg
.append("text")
.attr("id", "circletooltipText_" + mainDivName)
.attr("x", 30)
.attr("y", 15)
.attr("fill", function () {
return "#fff"
})
.style("font-size", function (d) {
return 10;
})
.style("font-family", function (d) {
return "arial";
})
.text(function (d, i) {
return "";
});
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x0))
.append("text")
.attr("x", width / 2)
.attr("y", margin.bottom * 0.9)
.attr("dx", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text(label.xAxis);
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 0)
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("font-weight", "bold")
.text(label.yAxis);
}
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 };
}
}
var barTooltip = {
addTooltips: function (pie) {
var keys = pie.keys;
// group the label groups (label, percentage, value) into a single element for simpler positioning
var element = pie.svg.append("g")
.selectAll("g")
.data(pie.data)
.enter().append("g")
.attr("class", function (d, i) {
return pie.cssPrefix + "tooltips" + "_" + i
});
tooltips = element.selectAll("g")
.data(function (d, i) {
return keys.map(function (key) {
return { key: key, value: d[key], index: key + "_" + i + "_" + d[pie.xAxis] };
});
})
.enter()
.append("g")
.attr("class", pie.cssPrefix + "tooltip")
.attr("id", function (d, i) {
return pie.cssPrefix + "tooltip" + d.index;
})
.style("opacity", 0)
.append("rect")
.attr("rx", 2)
.attr("ry", 2)
.attr("x", -2)
.attr("opacity", 0.71)
.style("fill", "#000000");
element.selectAll("g")
.data(function (d, i) {
return keys.map(function (key) {
return { key: key, value: d[key], index: key + "_" + i + "_" + d[pie.xAxis] };
});
})
.append("text")
.attr("fill", function (d) {
return "#efefef"
})
.style("font-size", function (d) {
return 10;
})
.style("font-family", function (d) {
return "arial";
})
.text(function (d, i) {
var caption = "Runs:{runs}";
return barTooltip.replacePlaceholders(pie, caption, i, {
runs: d.value,
});
});
element.selectAll("g rect")
.attr("width", function (d, i) {
var dims = helpers.getDimensions(pie.cssPrefix + "tooltip" + d.index);
return dims.w + (2 * 4);
})
.attr("height", function (d, i) {
var dims = helpers.getDimensions(pie.cssPrefix + "tooltip" + d.index);
return dims.h + (2 * 4);
})
.attr("y", function (d, i) {
var dims = helpers.getDimensions(pie.cssPrefix + "tooltip" + d.index);
return -(dims.h / 2) + 1;
});
},
showTooltip: function (pie, index) {
var fadeInSpeed = 250;
if (barTooltip.currentTooltip === index) {
fadeInSpeed = 1;
}
barTooltip.currentTooltip = index;
d3.select("#" + pie.cssPrefix + "tooltip" + index)
.transition()
.duration(fadeInSpeed)
.style("opacity", function () {
return 1;
});
barTooltip.moveTooltip(pie);
},
moveTooltip: function (pie) {
d3.selectAll("#" + pie.cssPrefix + "tooltip" + barTooltip.currentTooltip)
.attr("transform", function (d) {
var mouseCoords = d3.mouse(this.parentNode);
var x = mouseCoords[0] + 4 + 2;
var y = mouseCoords[1] - (2 * 4) - 2;
return "translate(" + x + "," + y + ")";
});
},
hideTooltip: function (pie, index) {
d3.select("#" + pie.cssPrefix + "tooltip" + index)
.style("opacity", function () {
return 0;
});
// move the tooltip offscreen. This ensures that when the user next mouseovers the segment the hidden
// element won't interfere
d3.select("#" + pie.cssPrefix + "tooltip" + barTooltip.currentTooltip)
.attr("transform", function (d, i) {
// klutzy, but it accounts for tooltip padding which could push it onscreen
var x = pie.width + 1000;
var y = pie.height + 1000;
return "translate(" + x + "," + y + ")";
});
},
replacePlaceholders: function (pie, str, index, replacements) {
var replacer = function () {
return function (match) {
var placeholder = arguments[1];
if (replacements.hasOwnProperty(placeholder)) {
return replacements[arguments[1]];
} else {
return arguments[0];
}
};
};
return str.replace(/\{(\w+)\}/g, replacer(replacements));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment