[ Launch: play1 ] ea0d932fcd363104181f by gelicia
-
-
Save gelicia/ea0d932fcd363104181f to your computer and use it in GitHub Desktop.
play1
This file contains hidden or 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
{"description":"play1","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"d3.legend":{"default":true,"vim":false,"emacs":false,"fontSize":12},"legend.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"thumbnail":"http://i.imgur.com/hV3yAdg.png"} |
This file contains hidden or 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
var data = [ | |
{ | |
"count": 3, | |
"name": "Critical", | |
"entityViewUrl": null | |
}, | |
{ | |
"count": 18, | |
"name": "High", | |
"entityViewUrl": null | |
}, | |
{ | |
"count": 135, | |
"name": "Normal", | |
"entityViewUrl": null | |
}, | |
{ | |
"count": 65, | |
"name": "Other", | |
"entityViewUrl": null | |
}]; | |
var svg = d3.select("svg"); | |
var valueLabelWidth = 40; // space reserved for value labels (right) | |
var barHeight = 20; // height of one bar | |
var barLabelWidth = 100; // space reserved for bar labels | |
var barLabelPadding = 5; // padding between bar and bar labels (left) | |
var gridLabelHeight = 18; // space reserved for gridline labels | |
var gridChartOffset = 3; // space between start of grid and first bar | |
var maxBarWidth = 420; // width of the bar with the max value | |
// accessor functions | |
var barLabel = function(d) { return d['name']; }; | |
var barValue = function(d) { return parseFloat(d['count']); }; | |
// scales | |
var yScale = d3.scale.ordinal().domain(d3.range(0, data.length)).rangeBands([0, data.length * barHeight]); | |
var y = function(d, i) { return yScale(i); }; | |
var yText = function(d, i) { return y(d, i) + yScale.rangeBand() / 2; }; | |
var x = d3.scale.linear().domain([0, d3.max(data, barValue)]).range([0, maxBarWidth]); | |
// svg container element | |
//var chart = d3.select('#chart').append("svg") | |
//.attr('width', maxBarWidth + barLabelWidth + valueLabelWidth) | |
//.attr('height', gridLabelHeight + gridChartOffset + data.length * barHeight); | |
// grid line labels | |
var gridContainer = svg.append('g') | |
.attr('transform', 'translate(' + barLabelWidth + ',' + gridLabelHeight + ')'); | |
gridContainer.selectAll("text").data(x.ticks(10)).enter().append("text") | |
.attr("x", x) | |
.attr("dy", -3) | |
.attr("text-anchor", "middle") | |
.text(String); | |
// vertical grid lines | |
gridContainer.selectAll("line").data(x.ticks(10)).enter().append("line") | |
.attr("x1", x) | |
.attr("x2", x) | |
.attr("y1", 0) | |
.attr("y2", yScale.rangeExtent()[1] + gridChartOffset) | |
.style("stroke", "#ccc"); | |
// bar labels | |
var labelsContainer = chart.append('g') | |
.attr('transform', 'translate(' + (barLabelWidth - barLabelPadding) + ',' + (gridLabelHeight + gridChartOffset) + ')'); | |
labelsContainer.selectAll('text').data(data).enter().append('text') | |
.attr('y', yText) | |
.attr('stroke', 'none') | |
.attr('fill', 'black') | |
.attr("dy", ".35em") // vertical-align: middle | |
.attr('text-anchor', 'end') | |
.text(barLabel); | |
// bars | |
var barsContainer = chart.append('g') | |
.attr('transform', 'translate(' + barLabelWidth + ',' + (gridLabelHeight + gridChartOffset) + ')'); | |
barsContainer.selectAll("rect").data(data).enter().append("rect") | |
.attr('y', y) | |
.attr('height', yScale.rangeBand()) | |
.attr('width', function(d) { return x(barValue(d)); }) | |
.attr('stroke', 'white') | |
.attr('fill', 'steelblue'); | |
// bar value labels | |
barsContainer.selectAll("text").data(data).enter().append("text") | |
.attr("x", function(d) { return x(barValue(d)); }) | |
.attr("y", yText) | |
.attr("dx", 3) // padding-left | |
.attr("dy", ".35em") // vertical-align: middle | |
.attr("text-anchor", "start") // text-align: right | |
.attr("fill", "black") | |
.attr("stroke", "none") | |
.text(function(d) { return d3.round(barValue(d), 2); }); | |
// start line | |
barsContainer.append("line") | |
.attr("y1", -gridChartOffset) | |
.attr("y2", yScale.rangeExtent()[1] + gridChartOffset) | |
.style("stroke", "#000"); |
This file contains hidden or 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
d3.svg.legend = function() { | |
var legendValues=[{color: "red", stop: [0,1]},{color: "blue", stop: [1,2]},{color: "purple", stop: [2,3]},{color: "yellow", stop: [3,4]},{color: "Aquamarine", stop: [4,5]}]; | |
var legendScale; | |
var cellWidth = 30; | |
var cellHeight = 20; | |
var adjustable = false; | |
var labelFormat = d3.format(".01f"); | |
var labelUnits = "units"; | |
var lastValue = 6; | |
var changeValue = 1; | |
var orientation = "horizontal"; | |
var cellPadding = 0; | |
function legend(g) { | |
function cellRange(valuePosition, changeVal) { | |
legendValues[valuePosition].stop[0] += changeVal; | |
legendValues[valuePosition - 1].stop[1] += changeVal; | |
redraw(); | |
} | |
function redraw() { | |
g.selectAll("g.legendCells").data(legendValues).exit().remove(); | |
g.selectAll("g.legendCells").select("rect").style("fill", function(d) {return d.color}); | |
if (orientation == "vertical") { | |
g.selectAll("g.legendCells").select("text.breakLabels").style("display", "block").style("text-anchor", "start").attr("x", cellWidth + cellPadding).attr("y", 5 + (cellHeight / 2)).text(function(d) {return labelFormat(d.stop[0]) + (d.stop[1].length > 0 ? " - " + labelFormat(d.stop[1]) : "")}) | |
g.selectAll("g.legendCells").attr("transform", function(d,i) {return "translate(0," + (i * (cellHeight + cellPadding)) + ")" }); | |
} | |
else { | |
g.selectAll("g.legendCells").attr("transform", function(d,i) {return "translate(" + (i * cellWidth) + ",0)" }); | |
g.selectAll("text.breakLabels").style("text-anchor", "middle").attr("x", 0).attr("y", -7).style("display", function(d,i) {return i == 0 ? "none" : "block"}).text(function(d) {return labelFormat(d.stop[0])}); | |
} | |
} | |
g.selectAll("g.legendCells") | |
.data(legendValues) | |
.enter() | |
.append("g") | |
.attr("class", "legendCells") | |
.attr("transform", function(d,i) {return "translate(" + (i * (cellWidth + cellPadding)) + ",0)" }) | |
g.selectAll("g.legendCells") | |
.append("rect") | |
.attr("height", cellHeight) | |
.attr("width", cellWidth) | |
.style("fill", function(d) {return d.color}) | |
.style("stroke", "black") | |
.style("stroke-width", "2px"); | |
g.selectAll("g.legendCells") | |
.append("text") | |
.attr("class", "breakLabels") | |
.style("pointer-events", "none"); | |
g.append("text") | |
.text(labelUnits) | |
.attr("y", -7); | |
redraw(); | |
} | |
legend.inputScale = function(newScale) { | |
if (!arguments.length) return scale; | |
scale = newScale; | |
legendValues = []; | |
if (scale.invertExtent) { | |
//Is a quantile scale | |
scale.range().forEach(function(el) { | |
var cellObject = {color: el, stop: scale.invertExtent(el)} | |
legendValues.push(cellObject) | |
}) | |
} | |
else { | |
scale.domain().forEach(function (el) { | |
var cellObject = {color: scale(el), stop: [el,""]} | |
legendValues.push(cellObject) | |
}) | |
} | |
return this; | |
} | |
legend.scale = function(testValue) { | |
var foundColor = legendValues[legendValues.length - 1].color; | |
for (el in legendValues) { | |
if(testValue < legendValues[el].stop[1]) { | |
foundColor = legendValues[el].color; | |
break; | |
} | |
} | |
return foundColor; | |
} | |
legend.cellWidth = function(newCellSize) { | |
if (!arguments.length) return cellWidth; | |
cellWidth = newCellSize; | |
return this; | |
} | |
legend.cellHeight = function(newCellSize) { | |
if (!arguments.length) return cellHeight; | |
cellHeight = newCellSize; | |
return this; | |
} | |
legend.cellPadding = function(newCellPadding) { | |
if (!arguments.length) return cellPadding; | |
cellPadding = newCellPadding; | |
return this; | |
} | |
legend.cellExtent = function(incColor,newExtent) { | |
var selectedStop = legendValues.filter(function(el) {return el.color == incColor})[0].stop; | |
if (arguments.length == 1) return selectedStop; | |
legendValues.filter(function(el) {return el.color == incColor})[0].stop = newExtent; | |
return this; | |
} | |
legend.cellStepping = function(incStep) { | |
if (!arguments.length) return changeValue; | |
changeValue = incStep; | |
return this; | |
} | |
legend.units = function(incUnits) { | |
if (!arguments.length) return labelUnits; | |
labelUnits = incUnits; | |
return this; | |
} | |
legend.orientation = function(incOrient) { | |
if (!arguments.length) return orientation; | |
orientation = incOrient; | |
return this; | |
} | |
legend.labelFormat = function(incFormat) { | |
if (!arguments.length) return labelFormat; | |
labelFormat = incFormat; | |
if (incFormat == "none") { | |
labelFormat = function(inc) {return inc}; | |
} | |
return this; | |
} | |
return legend; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment