Last active
May 28, 2017 12:21
-
-
Save ChandrakantThakkarDigiCorp/5f3abb974af37d8a5066d97a5a7a73a3 to your computer and use it in GitHub Desktop.
Partnership Chart D3 V4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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="partnershipChart.js"></script> | |
<div id="chart" style="width: 900;height: 500"> | |
</div> | |
<script> | |
var data = [{ | |
wicket: 1, | |
player_a_name: "Chintu Thakkar", | |
player_b_name: "Harsh Thakkar", | |
runs: 120, | |
player_a_runs: 60, | |
player_b_runs: 50 | |
}, { | |
wicket: 2, | |
player_a_name: "Chintu Thakkar", | |
player_b_name: "Vishal Thakkar", | |
runs: 140, | |
player_a_runs: 70, | |
player_b_runs: 60 | |
}, { | |
wicket: 3, | |
player_a_name: "Vishal Thakkar", | |
player_b_name: "Kumar Thakkar", | |
runs: 100, | |
player_a_runs: 40, | |
player_b_runs: 35 | |
}, { | |
wicket: 4, | |
player_a_name: "Kumar Thakkar", | |
player_b_name: "Madhav Thakkar", | |
runs: 130, | |
player_a_runs: 60, | |
player_b_runs: 55 | |
}, { | |
wicket: 5, | |
player_a_name: "Madhav Thakkar", | |
player_b_name: "Hariom Thakkar", | |
runs: 90, | |
player_a_runs: 45, | |
player_b_runs: 40 | |
}, { | |
wicket: 6, | |
player_a_name: "Haiom Thakkar", | |
player_b_name: "Sagar Thakkar", | |
runs: 110, | |
player_a_runs: 55, | |
player_b_runs: 50 | |
}, { | |
wicket: 7, | |
player_a_name: "Ravi Thakkar", | |
player_b_name: "Ajay Thakkar", | |
runs: 120, | |
player_a_runs: 60, | |
player_b_runs: 55 | |
}]; | |
$("#chart").empty(); | |
var chartConfig = { | |
mainDiv: "#chart", | |
colorRange: ["#2a98cd", "#df7247"], | |
data: data | |
}; | |
var partnershipChart = new partnershipChart(chartConfig); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function partnershipChart(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(); | |
drawPartnershipChart(data); | |
clearTimeout(resizeTimer); | |
}, 500); | |
}); | |
} | |
drawPartnershipChart(config); | |
setReSizeEvent(config); | |
} | |
function drawPartnershipChart(config) { | |
var data = config.data; | |
var colorRange = config.colorRange; | |
var mainDiv = config.mainDiv; | |
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: 30, | |
left: 40 | |
}, | |
width = +svg.attr("width") - margin.left - margin.right, | |
height = +svg.attr("height") - margin.top - margin.bottom; | |
var middleLineg = svg.append("g").attr("transform", "translate(" + margin.top + "," + margin.left + ")"); | |
var g = svg.append("g").attr("transform", "translate(" + margin.top + "," + margin.left + ")"); | |
var x = d3.scaleLinear() | |
.rangeRound([0, (width - (margin.left + margin.right)) / 2]); | |
var revX = d3.scaleLinear() | |
.rangeRound([(width - (margin.left + margin.right)) / 2, 0]); | |
var y0 = d3.scaleBand() | |
.rangeRound([height-15, 0+15]) | |
.paddingInner(0); | |
var z = d3.scaleOrdinal() | |
.range(colorRange); | |
var y1 = d3.scaleBand() | |
.padding(0); | |
var player_a_maxRuns = d3.max(data, function(d) { | |
return d["player_a_runs"]; | |
}); | |
var player_b_maxRuns = d3.max(data, function(d) { | |
return d["player_b_runs"]; | |
}); | |
var xCo_max = player_a_maxRuns > player_b_maxRuns ? player_a_maxRuns : player_b_maxRuns; | |
x.domain([0, xCo_max]).nice(); | |
revX.domain([0, xCo_max]).nice(); | |
y0.domain(data.map(function(d,i) { | |
return d["player_a_name"]+"_"+i; | |
})); | |
y1.domain(["data"]).rangeRound([0, y0.bandwidth() - (margin.bottom)]); | |
//CBT:rec at right side start | |
var rectHeight=0.5; | |
var element = g.append("g") | |
.selectAll("g") | |
.data(data) | |
.enter().append("g") | |
.attr("transform", function(d,i) { | |
return "translate(0," + y0(d["player_a_name"]+"_"+i) + ")"; | |
}); | |
var rectRight = element.selectAll("rect") | |
.data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("rect") | |
.attr("y", function(d) { | |
return y1("data"); | |
}) | |
.attr("x", (width - (margin.left + margin.right)) / 2) | |
.attr("width", function(d) { | |
return x(d["player_a_runs"]); | |
}) | |
.attr("data-index", function(d, i) { | |
return i; | |
}) | |
.attr("height", y1.bandwidth() * rectHeight) | |
.attr("fill", colorRange[0]); | |
var textRight = element.selectAll("text") | |
.data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("text") | |
.attr("x", (width - (margin.left + margin.right)) / 2) | |
.attr("y", y1.bandwidth() * (rectHeight+0.8)) | |
.attr("dx", "0.32em") | |
.attr("fill", "#000") | |
.attr("font-size", "14px") | |
.attr("font-weight", "bold") | |
.attr("text-anchor", "start") | |
.text(function(d) { | |
return d["player_a_name"] + " (" + d["player_a_runs"] + ")"; | |
}); | |
//CBT:rec at right side end | |
//CBT:rec at left side start | |
y0.domain(data.map(function(d,i) { | |
return d["player_b_name"]+"_"+i; | |
})); | |
var element = g.append("g") | |
.selectAll("g") | |
.data(data) | |
.enter().append("g") | |
.attr("transform", function(d,i) { | |
return "translate(0," + y0(d["player_b_name"]+"_"+i) + ")"; | |
}); | |
var rectLeft = element.selectAll("rect") | |
.data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("rect") | |
.attr("y", function(d) { | |
return y1("data"); | |
}) | |
.attr("x", function(d) { | |
return ((((width - (margin.left + margin.right)) / 2)) - x(d["player_b_runs"])); | |
}) | |
.attr("width", function(d) { | |
return x(d["player_b_runs"]); | |
}) | |
.attr("data-index", function(d, i) { | |
return i; | |
}) | |
.attr("height", y1.bandwidth() * rectHeight) | |
.attr("fill", colorRange[1]); | |
var textLeft = element.selectAll("text") | |
.data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("text") | |
.attr("x", function(d) { | |
return (width - (margin.left + margin.right)) / 2-10; | |
}) | |
.attr("y", y1.bandwidth() * (rectHeight+0.8)) | |
.attr("dx", "0.32em") | |
.attr("fill", "#000") | |
.attr("font-size", "14px") | |
.attr("font-weight", "bold") | |
.attr("text-anchor", "end") | |
.text(function(d) { | |
return d["player_b_name"] + " (" + d["player_b_runs"] + ")"; | |
}); | |
//CBT:rec at left side end | |
var circles = element.selectAll("circle").data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("circle").attr("cx", (width - (margin.left + margin.right)) / 2) | |
.attr("cy", (y1.bandwidth() * rectHeight) / 2) | |
.attr("r", 12) | |
.attr("fill", "#fff") | |
.attr("stroke", "#000"); | |
var circlesText = element.selectAll(".text").data(function(d, i) { | |
return [d]; | |
}) | |
.enter().append("text").attr("x", (width - (margin.left + margin.right)) / 2+7) | |
.attr("y", (y1.bandwidth() * rectHeight) / 2+5) | |
.attr("dx", "0.32em") | |
.attr("fill", "#000") | |
.attr("class", "text") | |
.attr("font-size", "14px") | |
.attr("font-weight", "bold") | |
.attr("text-anchor", "end") | |
.text(function(d) { | |
return d["runs"] ; | |
}); | |
g.append("g") | |
.attr("class", "axis") | |
.attr("transform", "translate(" + (width - (margin.left + margin.right)) / 2 + "," + (height - margin.bottom) + ")") | |
.call(d3.axisBottom(x).ticks(5)) | |
.append("text") | |
.attr("x", 0) | |
.attr("y", margin.bottom * 0.9) | |
.attr("dx", "0.32em") | |
.attr("fill", "#000") | |
.attr("font-weight", "bold") | |
.attr("text-anchor", "start") | |
.text("Runs"); | |
g.append("g") | |
.attr("class", "axis") | |
.attr("transform", "translate(" + 0 + "," + (height - margin.bottom) + ")") | |
.call(d3.axisBottom(revX).ticks(5)); | |
var middleLine = middleLineg.append("line") | |
.attr("class", "middleLine") | |
.attr("x1", (width - (margin.left + margin.right)) / 2) | |
.attr("y1", 0) | |
.attr("x2", (width - (margin.left + margin.right)) / 2) | |
.attr("y2", height - margin.bottom) | |
.attr("stroke", "#000"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment