How do I incorporate rotation into this code snippet:
<html>
<head>
<script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8acfb88fbe6fde6fb">[email protected]</a>" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
</head>
<body>
<script>
var w = 300,
h = 300,
data = [{
t: "Now",
v: 50
},{
t: "is",
v: 25
},{
t: "the",
v: 10
},{
t: "winter",
v: 30
}];
var svg = d3.select('body')
.append('svg')
.attr('width', w)
.attr('height', h);
var yScale = d3.scale.linear()
.range([0, h])
.domain([60, 0]);
var label = svg.selectAll("text")
.data(data)
.enter()
.append("text")
.text(function(d) {
return d.t;
})
.attr("transform", function(d,i){
var xText = i * (w / data.length);
var yText = h - yScale(d.v);
return "translate(" + xText + "," + yText + ") rotate(90)";
})
.attr("fill", "black")
.attr("font-family", "sans-serif")
.attr("font-size", "11px");
</script>
</body>
</html>
Using the following code instead:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="http://d3js.org/d3.v2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style type="text/css">
#canvas {
width: 300px;
height: 300px;
background-color: red;
}
</style>
</head>
<body>
<div id="canvas" style="border:solid"></div>
<script type="text/javascript">
var w = 300,
h = 300;
var data = {
name: "root",
children: [{
name: '1',
size: 70
},
{
name: '2',
size: 70
},
{
name: '3',
size: 70
},
{
name: '4',
size: 70
},
{
name: '5',
size: 70
},
{
name: '6',
size: 70
},
{
name: '7',
size: 70
},
]
}
jQuery.fn.rotate = function(degrees) {
$(this).css({'-webkit-transform' : 'rotate('+ degrees +'deg)',
'-moz-transform' : 'rotate('+ degrees +'deg)',
'-ms-transform' : 'rotate('+ degrees +'deg)',
'transform' : 'rotate('+ degrees +'deg)'});
return $(this);
var r = getRandomInt(0, 180);
};
var canvas = d3.select("#canvas")
.append("svg:svg")
.attr('width', w)
.attr('height', h);
var nodes = d3.layout.pack()
.value(function(d) {
return d.size;
})
.size([w, h])
.nodes(data);
nodes.shift();
var yScale = d3.scale.linear()
.range([0, h])
.domain([60, 0]);
canvas.selectAll('circles')
.data(nodes)
.enter().append('svg:circle')
.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
})
.attr('r', function(d) {
return d.r;
})
.attr('fill', 'white')
.attr('x', 0)
.attr('y', 0)
.attr('width', 6)
.attr('height', 6);
canvas.selectAll("text")
.data(nodes)
.enter()
.append("text")
.attr("x", function(d) {
return d.x;
})
.attr("y", function(d) {
return d.y;
})
.text(function(d) {
return "Pomidorek";
})
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "18px")
.attr("color", "black")
.attr("transform", function(d,i){
var xText = i * (w / children.length);
var yText = h - yScale(d.size);
return "translate(" + xText + "," + yText + ") rotate(505)";
})
</script>
</body>
</html>
I have been attempting to implement this but it seems to be not functioning as expected. This is crucial for me and I'm struggling to identify where I might have gone wrong. The first code successfully rotates text without any issues, whereas in the second code, I am uncertain about how to achieve the same effect.