score:1
Below you find a line graph by adjusting your code. Make sure the statements end with semicolons according to the convention.
Your variable s1 was not created correctly and at the end look how the line is appended correctly.
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var data=[{"key":[2009,0],"value":200},{"key":[2009,1],"value":201},{"key":[2009,2],"value":204},{"key":[2009,3],"value":204},{"key":[2009,4],"value":206},{"key":[2009,5],"value":200},{"key":[2009,6],"value":100},{"key":[2009,7],"value":208},{"key":[2009,8],"value":600},{"key":[2009,9],"value":290},{"key":[2009,10],"value":270},{"key":[2009,11],"value":400},
{"key":[2010,0],"value":200},{"key":[2010,1],"value":201},{"key":[2010,2],"value":204},{"key":[2010,3],"value":204},{"key":[2010,4],"value":206},{"key":[2010,5],"value":200},{"key":[2010,6],"value":100},{"key":[2010,7],"value":208},{"key":[2010,8],"value":600},{"key":[2010,9],"value":290},{"key":[2010,10],"value":270},{"key":[2010,11],"value":400},
{"key":[2011,0],"value":200},{"key":[2011,1],"value":201},{"key":[2011,2],"value":204},{"key":[2011,3],"value":204},{"key":[2011,4],"value":206},{"key":[2011,5],"value":200},{"key":[2011,6],"value":100},{"key":[2011,7],"value":208},{"key":[2011,8],"value":600},{"key":[2011,9],"value":290},{"key":[2011,10],"value":270},{"key":[2011,11],"value":400}];
var parseDate = d3.time.format("%x").parse;
// construct the date according to the parsed format: %m/%d/%Y
var s1 = data.map(function (d,i){return {key:parseDate((d.key[1]+1)+"/1/"+d.key[0]),value:+d.value}});
console.log(s1);
var margin = {top: 15, right: 20, bottom: 70, left: 85},
width = 440,height = 450;
var x = d3.time.scale().domain(d3.extent(s1,function (d,i){return d.key}))
.range([0,width]);
var y = d3.scale.linear().domain(d3.extent(s1,function (d,i){return d.value}))
.range([height,0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom");
var yAxis = d3.svg.axis().scale(y).orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.key); })
.y(function(d) { return y(d.value); })
.interpolate("cardinal");
var div = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "x axis x-axis")
.attr("transform", "translate(0," + height + ")")
.attr("fill","steelblue")
.call(xAxis);
svg.append("g")
.attr("class", "y axis y-axis")
.attr("fill","steelblue")
.call(yAxis);
svg.append("path")
.datum(s1)
.attr("class", "line")
.attr("d", line);
</script>
</body>
</html>
Source: stackoverflow.com
Related Query
- How to get the data from given array of objects in d3.js
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- In d3, how to get the interpolated line data from a SVG line?
- How to get the data from the c3.js
- How to get the specific JSON array from a value in that array
- how to get data with tsv or csv to array in d3.js from a txt file?
- How do I get the index number from the array in d3?
- How to get the minimum and maximum value between a given range in data set in d3.js
- How to reshaping Data in Javascript from array to object without losing some of the data
- In d3 4.0, how can I get extra data from the stratified object?
- How to create a new array using the data from the old one?
- How to get a D3.js pie chart to render data dynamically from the DOM
- How to properly get data from a d3.js nested array
- D3.js: Get an array of the data attached to a selection?
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- How to update elements of an HTML that the elements are created using data from a CSV file?
- dc.js - how to get the average of a column in data set
- How do I get D3 Data Driven Document to display a moment.js datetime in the correct time zone?
- How to get value from the element using selection in d3
- How to get web element (id) from the element position in D3.js force graph
- How to access the inner array resulting from d3.groups?
- How to get SVG path data of TopoJSON feature without adding it to the DOM?
- How to get the index of the data element in a histogram on mouseover?
- How do I get data from a rectangle in D3?
- Passing JSON from Grails Controller to d3 - how to get the .gsp side working?
- How to get a value from a stylesheet into the code so that it can be used programmatically?
- Chart is not showing the data array of objects
- D3 How to update the chart after selection from drop down menu with new data
- How to get the background attribute from the nodes of treemap in d3?
- How to get the X value of a ticks from the x axis?
More Query from same tag
- D3 listening to "end" event of a transition
- How can I get the natural width and height of an svg image element?
- Iterate through Key/Values in D3 graph for line chart
- How to adjust the size of a d3.js globe?
- Matching leaflet zoom level with d3 geo scale
- How can i set each text label vertically in d3?
- d3-force: keep graph compact when filtering/removing nodes
- Loop through array of arrays in D3
- Call Controller Function from Directive (with params passed from directive)
- Holding Shift prevents mouse wheel zoom in firefox
- Can d3 voronoi map work well with any other projection besides geoAlbers?
- trying to rotate a globe in d3, centering on a particular country
- Changing a Json Array in D3 for Bar Chart
- How to determine if D3 tree node falls outside SVG container
- Adding labels to the middle of a sunburst on mouseover
- Making Cubism.js work with Rails app
- Converting Csv to json in chile/name format
- Multi-page Flex Dashboard plots with incorrect zoom levels
- JavaScript remove equivalent object in array
- Filter with underscorejs and d3 data
- Slowing down link animation in D3 force layout
- why is there a 5-10 second delay during meteor Tracker.autorun before database changes are updated in UI?
- D3.JS Grouped Donut Chart from JSON
- D3 getting JSON data within a JSON
- Vega-lite bar chart space between bars
- d3js add tag inside another tag
- Adding color dynamically to a multiple line chart but it is not working in d3.js
- group by object attributes in array
- D3 library + Javascript + function
- D3 - Stacked bar chart, position bars