Skip to content

Instantly share code, notes, and snippets.

@ChandrakantThakkarDigiCorp
Last active May 27, 2017 13:05
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/be18bb176b5050b55a32c05060bad11e to your computer and use it in GitHub Desktop.
Save ChandrakantThakkarDigiCorp/be18bb176b5050b55a32c05060bad11e to your computer and use it in GitHub Desktop.
Multi-Series Line Chart
<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="multiSeriesLineChart.js"></script>
<div id="chart" style="width: 800;height: 500">
</div>
<script>
var groupChartData = [{ "2614": 8, "4449": 15, "over": 1 }, { "2614": 15, "4449": 17, "over": 2 }, { "2614": 19, "4449": 22, "over": 3 }, { "2614": 38, "4449": 30, "over": 4 }, { "2614": 41, "4449": 37, "over": 5 }, { "2614": 47, "4449": 38, "over": 6 }, { "2614": 54, "4449": 44, "over": 7 }, { "2614": 67, "4449": 46, "over": 8 }, { "2614": 68, "4449": 54, "over": 9 }, { "2614": 76, "4449": 63, "over": 10 }, { "2614": 80, "4449": 71, "over": 11 }, { "2614": 80, "4449": 91, "over": 12 }, { "2614": 88, "4449": 94, "over": 13 }, { "2614": 93, "4449": 103, "over": 14 }, { "2614": 100, "4449": 108, "over": 15 }, { "2614": 109, "4449": 122, "over": 16 }, { "2614": 126, "4449": 137, "over": 17 }, { "2614": 147, "4449": 147, "over": 18 }, { "2614": 157, "4449": 150, "over": 19 }, { "2614": null, "4449": 155, "over": 20 }];
var columnsInfo = { "2614": "Team A", "4449": "Team B" };
$("#chart").empty();
var muliSeriesChartConfig = {
mainDiv: "#chart",
colorRange: ["#2a98cd", "#df7247"],
data: groupChartData,
columnsInfo: columnsInfo,
xAxis: "over",
yAxis: "runs",
label: {
xAxis: "Over",
yAxis: "Runs"
},
requireCircle: false,
requireLegend: true
};
var muliSeriesChart = new multiSeriesLineChart(muliSeriesChartConfig);
</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 multiSeriesLineChart(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();
drawmultiSeriesLineChartCharts(data);
clearTimeout(resizeTimer);
}, 500);
});
}
drawmultiSeriesLineChartCharts(config);
setReSizeEvent(config);
}
function createmultiSeriesLineChartLegend(mainDiv, columnsInfo, colorRange) {
var z = d3.scaleOrdinal()
.range(colorRange);
var mainDivName = mainDiv.substr(1, mainDiv.length);
$(mainDiv).before("<div id='Legend_" + mainDivName + "' class='pmd-card-body' 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 drawmultiSeriesLineChartCharts(config) {
var keys = Object.keys(config.data[0]);
var tempObj = {};
keys.forEach(function (d) {
tempObj[d] = 0;
});
config.data.splice(0, 0, tempObj);
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 requireCircle = config.requireCircle || false;
var requireLegend = config.requireLegend;
var imageData = config.imageData;
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: 50 },
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();
createmultiSeriesLineChartLegend(mainDiv, columnsInfo, colorRange);
}
var x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal()
.range(colorRange);
var line = d3.line()
.x(function (d) {
return x(d[xAxis]);
})
.y(function (d) {
return y(d[yAxis]);
});
var columns = Object.keys(columnsInfo);
var groupData = columns.map(function (id) {
return {
id: id,
values: data.filter(function (d, i) {
//CBT:remove last blank or value is 0 data to show only that much of line
if ((d[id] != 0 && d[id] != null && d[id] != undefined) || i == 0) return d;
}).map(function (d, i) {
var tempObj = {};
tempObj[xAxis] = d[xAxis];
tempObj[yAxis] = d[id];
return tempObj;
})
};
});
x.domain(d3.extent(data, function (d) {
return d[xAxis];
}));
y.domain([
d3.min(groupData, function (c) {
return d3.min(c.values, function (d) {
return d[yAxis];
});
}),
d3.max(groupData, function (c) {
return d3.max(c.values, function (d) {
return d[yAxis];
});
})
]);
z.domain(groupData.map(function (c) {
return c.id;
}));
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.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 axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.text(label.yAxis);
var city = g.selectAll(".city")
.data(groupData)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function (d) {
return line(d.values);
})
.style("stroke", function (d) {
return z(d.id);
}).style("fill", "none").style("stroke-width", "3px");
//CBT:for wicket Circles in multiseries line chart
var circleRadius = 6;
var keys = Object.keys(columnsInfo);
var element = g.append("g")
.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function (d) {
return "translate(" + x(d[xAxis]) + ",0)";
});
var circles = element.selectAll("circle")
.data(function (d) {
return keys.map(function (key) {
return { key: key, value: d[key], over: d.over };
});
})
.enter().append("circle")
.attr("cx", function (d) {
return 0;
})
.attr("cy", function (d) {
return y(d.value);
})
.attr("r", circleRadius)
.attr("fill", "#fff")
.attr("stroke", function (d) {
if (d.circles == undefined || d.circles.length <= 0) {
return "#fff";
} else {
return z(d.key);
}
})
.attr("data", function (d) {
var data = {};
data["over"] = d.over;
data["runs"] = d.value;
return JSON.stringify(data);
})
.attr("stroke-width", "2px")
.attr("fill-opacity", function (d) {
return 0.05;
})
.attr("stroke-opacity", function (d) {
return 0.05;
});
circles.on("mouseover", function () {
var currentEl = d3.select(this);
currentEl.attr("r", 7);
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 tooltipData = 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 + ": " + tooltipData.over);
yPos = yPos + 1;
d3.selectAll("#circletooltipText_" + mainDivName).append("tspan").attr("x", 0).attr("y", yPos * 10).attr("dy", "1.9em").text(label.yAxis + ": " + tooltipData.runs);
//CBT:calculate width of the text based on characters
var dims = helpers.getDimensions("circletooltipText_" + mainDivName);
d3.selectAll("#circletooltipText_" + mainDivName + " tspan")
.attr("x", dims.w + 4);
d3.selectAll("#circletooltipRect_" + mainDivName)
.attr("width", dims.w + 10)
.attr("height", dims.h + 20);
});
circles.on("mousemove", function () {
var currentEl = d3.select(this);
currentEl.attr("r", 7);
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 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 + ")";
});
});
circles.on("mouseout", function () {
var currentEl = d3.select(this);
currentEl.attr("r", 6);
d3.select("#circletooltip_" + mainDivName)
.style("opacity", function () {
return 0;
})
.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 + ")";
});
});
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 "";
});
}
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