<div id="bar">
</div>
<script>
const data = [10,4,30,100,200,119];
const barWidth = 50;
const translate = function (d, i) {
const x = (barWidth + 3) * i;
return "translate(" + x + "," + yPos(d) + ")";
};
const max = d3.max(data);
function yPos(d) {
return max - d;
}
d3.select('body')
.append('svg')
.attr('width', 500)
.attr('height', 205)
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("x",150)
.attr("y", 5)
.attr("fill", 'steelblue')
.attr("width", barWidth)
.attr("height", function (d) {
return d;
})
.attr("transform", translate);
</script>
Visualization Articles
Data driven bar
D3 Data driven DSL
<div id="sameLengthBar">
</div>
<script>
const data = [10,4,30];
const barWidth = 50;
const translate = function (d, i) {
const x = (barWidth + 3) * i;
const y = "5";
return "translate(" + x + "," + y + ")";
};
d3.select('div#sameLengthBar')
.append('svg')
.attr('width', 500)
.attr('height', 250)
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr("x",150)
.attr("y", 5)
.attr("fill", 'steelblue')
.attr("width", barWidth)
.attr("height", 155)
.attr("transform", translate);
</script>
D3 DSL
<div id="justRect">
</div>
<script>
d3.select('div#justRect')
.append('svg')
.attr('width', 500)
.attr('height', 155)
.append('rect')
.attr("x",150)
.attr("y", 5)
.attr("fill", 'steelblue')
.attr("width", 50)
.attr("height", 150);
</script>
Group
<svg width="500" height="70">
<g transform="scale(0.3) rotate(45, 150, 150)">
<path d="M100 29 H 150" stroke="black" stroke-width="8"/>
<path d="M149.6 25 V 70" stroke="black" stroke-width="8"/>
<line x1="100" y1="25" x2="95" y2="70" stroke="black" stroke-width="8"/>
<rect x="59.6" y="50" width="94" height="35"/>
<path d="M100 80 H 250" stroke="black" stroke-width="9"/>
<circle cx="100" cy="100" r="15" fill="black" stroke="black" stroke-width="2"/>
<circle cx="200" cy="100" r="15" fill="black" stroke="black" stroke-width="2"/>
</g>
</svg>
Opacity
<style>
svg circle {fill: white; stroke-width: 4}
svg circle#blue {stroke: blue}
svg circle#black {stroke: black}
svg circle#red {stroke: red}
svg circle#yellow {stroke: yellow}
svg circle#green {stroke: green}
</style>
<svg width="500" height="200">
<circle cx="100" cy="52" r="50" id="blue" opacity="0.9"/>
<circle cx="210" cy="52" r="50" id="black" opacity="0.9"/>
<circle cx="320" cy="52" r="50" id="red" opacity="0.9"/>
<circle cx="160" cy="102" r="50" id="yellow" opacity="0.9"/>
<circle cx="250" cy="102" r="50" id="green" opacity="0.9"/>
</svg>
Rotate
<svg width="500" height="500">
<line x1="20", y1="20" x2="100" y2="100" stroke-width="5" stroke="black"/>
<text x="15" y="15" style="font-family: Arial; fill: red">
Rotate 30
</text>
<line x1="20", y1="20" x2="100" y2="100" stroke-width="5" stroke="grey" transform="rotate(30)"/>
<rect x="100" y="100 " width="100" height="100" fill="red"/>
<rect x="100" y="100 " width="100" height="100" fill="orange" transform="rotate(5)"/>
<rect x="100" y="100 " width="100" height="100" fill="yellow" transform="rotate(10)"/>
<rect x="100" y="100 " width="100" height="100" fill="green" transform="rotate(15)"/>
<rect x="200" y="300 " width="100" height="100" fill="red"/>
<rect x="200" y="300 " width="100" height="100" fill="#FF9966" transform="rotate(5)"/>
<rect x="200" y="300 " width="100" height="100" fill="#EC5800" transform="rotate(10)"/>
<rect x="200" y="300 " width="100" height="100" fill="#D99058" transform="rotate(15)"/>
<rect x="200" y="300 " width="100" height="100" fill="orange" transform="rotate(20)"/>
<rect x="200" y="300 " width="100" height="100" fill="yellow" transform="rotate(20, 200, 300)"/>
</svg>
Translate
<svg width="500" height="100">
<path d="M100 5 H 200" stroke="black"/>
<path d="M100 5 H 200" stroke="black" transform="translate(100,50)"/>
</svg>
Subscribe to:
Posts (Atom)