<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<svg id="d3"></svg>
</body>
</html>
#d3 {
width: 380px;
height: 300px;
}
text {
font-family: sans-serif
}
//http://stackoverflow.com/q/33728173/371899
var createTestCase = function (inputNumber, expectedOutput) {
return Object.create({}, {
inputNumber: { value: inputNumber, enumerable: true },
expectedOutput: { value: expectedOutput, enumerable: true }
});
},
testCaseList = [
createTestCase(16,0),
createTestCase(17,10),
createTestCase(18,20),
createTestCase(19,30),
createTestCase(20,40),
createTestCase(21,50),
createTestCase(6,60),
createTestCase(7,70),
createTestCase(8,80),
createTestCase(9,90),
createTestCase(10,100)
],
svg = d3.select("svg"),
group = svg.append("g"),
width = 250,
height = 250,
domain1 = [16, 21],
domain2 = [6, 10],
range = [0, 100],
midPoint = [50, 60],
data = [16,17,18,19,20,21,6,7,8,9,10],
scaleX = d3.scale.linear().domain([6,21]).range([30, width]),
scaleY = d3.scale.linear().domain([10, 100]).range([height, 30]),
circle = group.selectAll("circle").data(data),
circleEnter = circle.enter(),
addCircles = circleEnter.append("circle").attr({
cx: scaleX,
cy: function (d) { return scaleY(multiScale(d, domain1, domain2, range, midPoint));},
r: 14,
stroke: "#000",
"stroke-width": "2px",
"fill": "#0f0"
}),
addText = group.selectAll("text").data(data).enter().append("text").text(function (d) {
return "" + d + " . scales to " + multiScale(d, domain1, domain2, range, midPoint);
}).attr({x: scaleX, y:function (d) { return scaleY(multiScale(d, domain1, domain2, range, midPoint));},
//"text-anchor": "middle",
"alignment-baseline": "middle",
dx: function (d) { return d<10 ? -5 : -9; }, dy: 2});
function multiScale(inputNumber, domain1, domain2, range, midPoint) {
var scale1 = d3.scale.linear().domain(domain1).range([range[0],midPoint[0]]),
scale2 = d3.scale.linear().domain(domain2).range([midPoint[1], range[1]]);
return (domain1[0] <= inputNumber && inputNumber <= domain1[1]) ? scale1(inputNumber) : scale2(inputNumber);
}
function runTestCases(sut, testCaseList) {
testCaseList.forEach(function (testCase) {
var actualOutput = sut.scale(testCase.inputNumber, domain1, domain2, range, midPoint),
because = JSON.stringify(testCase) + ", actualOutput: " + actualOutput;
console.log(actualOutput === testCase.expectedOutput ? "pass" : "fail: " + because);
});
}
runTestCases({scale: multiScale}, testCaseList);
console.log("end.");
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |