score:1
Accepted answer
The example code is actually malformed and is missing a body tag. I think it works because when the browser parses the <form>
tag it automatically wraps it in a <body>
. So, when you take out the form, add in a body.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
width: 960px;
height: 500px;
position: relative;
}
form {
position: absolute;
top: 1em;
left: 1em;
}
path {
fill-rule: evenodd;
stroke: #333;
stroke-width: 2px;
}
.sun path {
fill: #6baed6;
}
.planet path {
fill: #9ecae1;
}
.annulus path {
fill: #c6dbef;
}
</style>
<script src="https://d3js.org/d3.v4.js"></script>
<body>
<script>
var width = 960,
height = 500,
radius = 80,
x = Math.sin(2 * Math.PI / 3),
y = Math.cos(2 * Math.PI / 3);
var offset = 0,
speed = 4,
start = Date.now();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(.55)")
.append("g");
var frame = svg.append("g")
.datum({
radius: Infinity
});
frame.append("g")
.attr("class", "annulus")
.datum({
teeth: 80,
radius: -radius * 5,
annulus: true
})
.append("path")
.attr("d", gear);
frame.append("g")
.attr("class", "sun")
.datum({
teeth: 16,
radius: radius
})
.append("path")
.attr("d", gear);
frame.append("g")
.attr("class", "planet")
.attr("transform", "translate(0,-" + radius * 3 + ")")
.datum({
teeth: 32,
radius: -radius * 2
})
.append("path")
.attr("d", gear);
frame.append("g")
.attr("class", "planet")
.attr("transform", "translate(" + -radius * 3 * x + "," + -radius * 3 * y + ")")
.datum({
teeth: 32,
radius: -radius * 2
})
.append("path")
.attr("d", gear);
frame.append("g")
.attr("class", "planet")
.attr("transform", "translate(" + radius * 3 * x + "," + -radius * 3 * y + ")")
.datum({
teeth: 32,
radius: -radius * 2
})
.append("path")
.attr("d", gear);
// all 3 options
var radius1 = radius * 5;
var radius1 = Infinity;
var radius1 = -radius;
var radius0 = frame.datum().radius,
angle = (Date.now() - start) * speed;
frame.datum({
radius: radius1
});
svg.attr("transform", "rotate(" + (offset += angle / radius0 - angle / radius1) + ")");
function gear(d) {
var n = d.teeth,
r2 = Math.abs(d.radius),
r0 = r2 - 8,
r1 = r2 + 8,
r3 = d.annulus ? (r3 = r0, r0 = r1, r1 = r3, r2 + 20) : 20,
da = Math.PI / n,
a0 = -Math.PI / 2 + (d.annulus ? Math.PI / n : 0),
i = -1,
path = ["M", r0 * Math.cos(a0), ",", r0 * Math.sin(a0)];
while (++i < n) path.push(
"A", r0, ",", r0, " 0 0,1 ", r0 * Math.cos(a0 += da), ",", r0 * Math.sin(a0),
"L", r2 * Math.cos(a0), ",", r2 * Math.sin(a0),
"L", r1 * Math.cos(a0 += da / 3), ",", r1 * Math.sin(a0),
"A", r1, ",", r1, " 0 0,1 ", r1 * Math.cos(a0 += da / 3), ",", r1 * Math.sin(a0),
"L", r2 * Math.cos(a0 += da / 3), ",", r2 * Math.sin(a0),
"L", r0 * Math.cos(a0), ",", r0 * Math.sin(a0));
path.push("M0,", -r3, "A", r3, ",", r3, " 0 0,0 0,", r3, "A", r3, ",", r3, " 0 0,0 0,", -r3, "Z");
return path.join("");
}
d3.timer(function() {
var angle = (Date.now() - start) * speed,
transform = function(d) {
return "rotate(" + angle / d.radius + ")";
};
frame.selectAll("path").attr("transform", transform);
frame.attr("transform", transform); // frame of reference
});
</script>
</body>
Source: stackoverflow.com
Related Query
- How to Set Initial State of Gear Example without Using User Input Forms
- How can I show a graph using d3 force layout with initial positions set and no movement at all?
- How to write vertical text from bottom to top without using transform rotate?
- How do I create a tree layout using JSON data in d3.v4 - without stratify()
- How to set circle at the right end of the last bar / item using D3
- Using Javascript D3 library, how can I determine mouse position in data set of an area element on mousemove event?
- How to create correlogram using D3 as in the example picture
- How can i get the startAngle and endAngle of each arc in the sunburst example using d3.js?
- How to make label in histogram respond to dynamic user input
- How can I set an initial .focus() on a node in a d3.js sunburst chart?
- How to set up brush extent in d3 js while using time scale?
- d3 js - how to mimic distance/depth of field using randomized opacity on fillStyle() & fillRect() without strobe effect?
- Using d3 in meteor on windows 8.1, how can I display a world map, zoomed in on the US, with country borders and US state borders?
- How to set background color for svg text tspan without foreign object?
- How can I draw a continuous line using d3 without needing to click&drag the mouse, but rather just use the mouseover event?
- How to display each state name in d3 india map using GeoJSON
- How to fix d3 parsing error when using function and return value to set "d"?
- How to set ticks for yAxis using a logarithmic (Symlog) scale?
- How do I set the X values for a singled stacked horizontal bar chart using d3.js?
- How to set the last option from a drop-down list as selected using D3.js
- how to set color and grid in areaplot using d3.js?
- Using Django, AJAX and JSONs to update charts based on user input
- dc.js with crossfilter and d3, how to load multiple csv files at once, without knowing how many files the user will upload
- How can I group data to be filtered without losing crossfilter functionality using dc.js?
- How to re-sort a barchart into it's initial state after sorting in d3js
- How to set cx and cy with only one function using d3.js, force and network?
- How to get url for D3.json dynamically using input tag type=file?
- How to set hover state to show/hide menu items
- How do I update a D3 SVG chart based on user input
- How to select an existing SVG container in D3 without creating that container using D3?
More Query from same tag
- Can't add anything to my d3 bar graph
- Modify Cartesian DIstortion for D3js V4
- d3.js dynamic Tooltip for Chord diagram
- consume nested data in d3/javascript
- c3.js Accessing data objects/lines
- Map elements in dataset to div id
- Transitioning circles, one by one
- Making bar chart adjust to data passed in
- How to make a dashed line legend with d3.js
- No new elements on page using D3
- D3 append a line or rect to fill pie chart values
- Vue: data is not binding to props therefore not rendering BarChart using vue-D3-charts
- Can I update DOM elements in a D3 selector based on a data join to a different data object?
- How do I create a new scale() function in d3.js? I would like to create a cumulative distribution function
- Replacing fixed json variable with a json value returned from a php file
- D3.js - show tick values on the Y axis
- Reset or Clear Cubism Graph
- in Angular- nvD3 donut chart how to remove radial buttons
- d3 image is rendering outside the table
- How to wrap the text inside the SVG polygon without cutting the text
- Creating Tool Tips Correctly Based on Individual Sums
- Check for variable, if false, create object
- D3.js advanced node connections (find the path)
- How do you move number labels further away from the x-axis in d3.js?
- Add attributes without values using d3.js
- how to merge collapsible feature in bounded force directed graph?
- Linear piecewise scale in D3
- Continuously change the position of range input element on mouse down event (in single steps)
- Error: <line> attribute y1: Expected length, "NaN"
- NVD3 arcTween func throwing undefined error