score:4
Accepted answer
see another example on jsfiddle. https://jsfiddle.net/ermineia/cmqzh33x/
var data = [{
x: '1-may-12',
y: 5
}, {
x: '30-apr-12',
y: 28
}, {
x: '27-apr-12',
y: 58
}, {
x: '26-apr-12',
y: 88
}, {
x: '25-apr-12',
y: 8
}, {
x: '24-apr-12',
y: 48
}, {
x: '23-apr-12',
y: 28
}, {
x: '20-apr-12',
y: 68
}, {
x: '19-apr-12',
y: 8
}, {
x: '18-apr-12',
y: 58
}, {
x: '17-apr-12',
y: 5
}, {
x: '16-apr-12',
y: 80
}, {
x: '13-apr-12',
y: 38
}];
var margin = {
top: 30,
right: 20,
bottom: 35,
left: 50
},
width = 1200 - (margin.left + margin.right);
height = 360 - 2 * (margin.top + margin.bottom);
// parse the date / time
var parsedate = d3.time.format("%d-%b-%y");
var xscale = d3.time.scale().range([0, width]);
var yscale = d3.scale.linear().range([height, 0]);
xscale.domain(d3.extent(data, function(d) {
return parsedate.parse(d.x);
}));
yscale.domain([0, d3.max(data, function(d) {
return d.y;
})]);
var xaxis = d3.svg.axis().scale(xscale)
.orient("bottom").ticks(0).ticksize(0)
.tickformat("").outerticksize(0);
var yaxis = d3.svg.axis().scale(yscale)
.orient("left").ticksize(0).ticks(0)
.tickformat("");
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("class", "bg-color")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
function make_x_axis() {
return d3.svg.axis()
.scale(xscale)
.orient("bottom")
.ticks(25)
}
// function for the y grid lines
function make_y_axis() {
return d3.svg.axis()
.scale(yscale)
.orient("left")
.ticks(25);
}
svg.append("g")
.attr("class", "grid")
.call(make_x_axis()
.ticksize(height, 0, 0)
.tickformat("")
);
// draw the y grid lines
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.ticksize(-width, 0, 0)
.tickformat("")
);
data.sort(function(a, b) {
return parsedate.parse(a.x) - parsedate.parse(b.x);
});
// add the x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xaxis);
// add the y axis
svg.append("g")
.attr("class", "y axis")
.call(yaxis);
// define the line
var linegen = d3.svg.line()
.interpolate("monotone")
.x(function(d) {
return xscale(parsedate.parse(d.x));
})
.y(function(d) {
return yscale(d.y);
});
svg.append('path')
.attr("class", "line")
.style("stroke", "red")
.attr('d', linegen(data));
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.overlay {
fill: none;
pointer-events: all;
}
.focus circle {
fill: none;
stroke: steelblue;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 2;
shape-rendering: crispedges;
}
.grid .tick {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispedges;
}
.grid path {
stroke-width: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- D3.js line chart grid issue
- D3 - adding grid to simple line chart
- d3.js multiseries line chart tooltip issue
- d3 tooltip position issue in line chart using javascript
- Adding Grid to D3.js Line Chart
- D3.js line chart tooltip issue
- grouped Bar chart using D3 grid issue and console errors
- Having issue to display line chart using d3.js in .NET MVC C# project
- issue in re-rendering the path in a line chart based on user selection
- D3 v4 Multi line chart Brush Issue NaN when moving brush
- Issue with tooltip D3 Line Chart
- Redrawing grid lines on a line chart (remove and redraw) in d3.js
- NVD3 line chart single record issue
- d3.js - Multi series line chart tool tip issue
- D3JS line chart inverted issue
- D3 Interactive Legend on Multi Line Chart Issue
- D3.js Combined Bar and Line Chart x-Axis Misalignement Issue
- dc.js line chart - Date Value issue
- D3 Line chart with ordinal scale issue
- Line chart Dot update issue in D3
- Issue with incorrect dates displayed in line chart
- d3.js line chat grid issue
- D3: Issue while Displaying a Line chart inside tooltip
- D3.js issue using the line chart defined function
- Issue with d3 Multi Line Chart
- Multiple line chart issue from json
- How to add vertical grid line to my d3 chart
- is it possible to create a dynamic grid line chart in d3?
- x axis domain issue in D3 Line chart
- nvd3 line chart with string values on x-axis
More Query from same tag
- How to center a map on something other than the prime meridian in d3.js?
- searching a d3 tree - reset after search
- redrawing polygon after adding points on edges and dragging those points draws weird lines
- explanation of animating a sine wave example
- Zooming lines in d3js
- What is String or where does this come from?
- Raphaƫl and D3.JS - Better Browser Compatibility
- Data not formatted to date properly in d3
- Amend d3 graph to include all points
- How to add Custom value in timeFormat d3?
- nvd3 data holes in charts
- How to append the arrow marker dynamically in force layout d3.js
- Text shadows and zooming
- Turn NaN into a number
- How can I clip a trendline on a d3 scatterplot chart?
- Redux Store updated, but isn't reflected in mapStateToProps
- plotting multi dimensions in DC for D3
- d3 axis labeling
- Padding and margin on pack layout
- Plotting extra linear data on D3 js graph
- D3 Spinning Globe: trouble incorporating zoom function
- Text Not Showing Over SVG Rects
- d3js code working in online editor but not in html file
- When will this design pattern break?
- How to add URLs to bubble chart?
- How to cancel the mouseover transition in d3.js
- d3.js Strange dragging behaviour when using dynamic scaling
- Zoomable area in d3 v4 not working
- D3 Multi Line Graph with Dots
- optimize tween() transition in d3.js