score:3
Accepted answer
When you create the x axis, you can indicate the tick interval using
.ticks(d3.time.hour, 1)
which will place a tick every 1 hour. See the documentation of scale.ticks() for more info.
Here's the working code
exp1();
function exp1(){
var data = [
{
"CurrentQuantity": "50",
"LastUpdated": "/Date(1420793427983)/"
},
{
"CurrentQuantity": "76",
"LastUpdated": "/Date(1420721731353)/"
},
{
"CurrentQuantity": "20",
"LastUpdated": "/Date(1420714881920)/"
},
{
"CurrentQuantity": "24",
"LastUpdated": "/Date(1420713917820)/"
}//,
// {
// "CurrentQuantity": "25",
// "LastUpdated": "/Date(1418907098197)/"
// }
];
var margin = {top: 20, right: 20, bottom: 85, left: 50},
width = 1500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");
var x = d3.time.scale().range([0, width]);
x.tickFormat("%Y-%m-%d %I:%M:%S")
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(function(d) {
return formatDate(d);
})
.ticks(d3.time.hour, 1)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
data.forEach(function(d) {
var date = new Date(parseInt(d.LastUpdated.substr(6)));
console.log(date)
d.LastUpdated = date;
});
var line = d3.svg.line()
.x(function(d) {
return x( d.LastUpdated);
})
.y(function(d) {
return y(d.CurrentQuantity);
});
var svg = d3.select("#chart1").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 + ")");
x.domain(d3.extent(data, function(d) {
return d.LastUpdated;
}));
y.domain(d3.extent(data, function(d) {
return d.CurrentQuantity;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function(d) {
return "rotate(-45)"
});
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text('@Resource.Amount');
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
}
exp2();
function exp2(){
var data = [
{
"CurrentQuantity": "50",
"LastUpdated": "/Date(1420793427983)/"
},
{
"CurrentQuantity": "76",
"LastUpdated": "/Date(1420721731353)/"
},
{
"CurrentQuantity": "20",
"LastUpdated": "/Date(1420714881920)/"
},
{
"CurrentQuantity": "24",
"LastUpdated": "/Date(1420713917820)/"
},
{
"CurrentQuantity": "25",
"LastUpdated": "/Date(1418907098197)/"
}
];
var margin = {top: 20, right: 20, bottom: 85, left: 50},
width = 1500 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatDate = d3.time.format("%Y-%m-%d %H:%M:%S");
var x = d3.time.scale().range([0, width]);
x.tickFormat("%Y-%m-%d %I:%M:%S")
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(function(d) {
return formatDate(d);
})
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
data.forEach(function(d) {
var date = new Date(parseInt(d.LastUpdated.substr(6)));
console.log(date)
d.LastUpdated = date;
});
var line = d3.svg.line()
.x(function(d) {
return x( d.LastUpdated);
})
.y(function(d) {
return y(d.CurrentQuantity);
});
var svg = d3.select("#chart2").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 + ")");
x.domain(d3.extent(data, function(d) {
return d.LastUpdated;
}));
y.domain(d3.extent(data, function(d) {
return d.CurrentQuantity;
}));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function(d) {
return "rotate(-45)"
});
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text('@Resource.Amount');
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
}
chart {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
/*display: none;*/
}
.line {
stroke: rgb(142, 188, 0);
stroke-width: 2px;
stroke-linecap: square;
stroke-linejoin: round;
fill-opacity: 1;
stroke-opacity: 1;
fill: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<strong>Chart1</strong>
<div id="chart1"></div>
<br>
<strong>Chart2</strong>
<div id="chart2"></div>
Source: stackoverflow.com
Related Query
- How to spcify ticks on x axis for time scale in d3js?
- d3js set tickValues for time scale axis
- D3v4 time scale axis error in ticks display
- D3 time scale axis ticks are irregular on year boundaries
- How to add svg rectangles as background behind axis tick labels in d3js if ticks are inside chart
- How can I set a minimum range for an interval for a time scale in d3.js?
- D3js Changing ticks on Zoomable Time axis
- how to set proper domain for date time scale in d3
- How to force d3's date axis to draw ticks for first and last months that are not full [FIDDLE UPDATED]?
- How to set a scale for the time series, current x scale gives me NaN?
- Time scale axis ticks positions
- d3.js time scale axis ticks are not equally spaced
- d3js time axis ticks duration set dynamically
- How to overrideMin/Max for Time Axis in Dimple?
- How can I get the D3.js axis ticks and positions as an array?
- D3 - using strings for axis ticks
- How do I make a custom axis formatter for hours & minutes in d3.js?
- d3 time scale x axis with unix timestamp
- how to set the domain and scale on an axis on a nvd3.js multiBarChart
- How to change line color in d3js according to axis value?
- How to display the value for the final tick on axis in D3?
- How to force a specific amount of y axis ticks in d3 charts?
- what scale to use for representing years only on x axis in d3 js
- How to set fixed no. of ticks on axis in d3.js?
- How do I generate axis tick for every year in D3?
- d3.js - Time scale with ticks in milliseconds
- d3.js - Having a tree layout, how to change the X-axis to use a time scale in D3?
- d3 limit zoom level for time axis
- How to center & scale map in d3js (use projection.center/translate/rotate?)
- How to render end ticks always using d3 svg axis
More Query from same tag
- d3.js Treemap - Is there a way to specify display preference (sorting)?
- How to set the colors in a dendrogram based on cluster?
- d3 - Search tree and highlight node and path for d3 v5
- HTML2Canvas not rendering D3 font and font size
- How to show x and y coordinates of XY chart with mouseover using d3.js
- is the d3.js api object structure always the same
- How to fix truncated y-axis and x-axis
- D3.JS Browser Support
- D3 V4 Transition on entering elements using General Update Pattern with merge
- d3js time axis ticks duration set dynamically
- Binding some attributes of object to data
- Incomplete graph in d3.js using json
- LinePlusBarChart with date interval outside domain
- d3js v5 + Topojson v3 Map not rendering
- Rotate svg in place using d3.js
- Draw line chart on top of area chart
- A more elegant way to bind new elements using data bound to multiple group elements
- Reading from Json file to create graph javascript
- Update drag/scroll origin after an update of the data with transition in D3
- D3 Donut chart: Display Value on segment hover
- d3: svg image in zoom circle packing bis
- How can one scale GIS data in d3.js?
- D3.js time scale quirk with "11 PM" values
- is there a way to convert svg <line> to svg <path> in illustrator?
- d3.csv promise pending and getting its data
- D3, Typescript - Argument of type 'this' is not assignable to parameter of type 'BaseType'
- Append <div> to node in SVG with D3
- Remove or clear previously drawn dimple chart
- d3_Tooltips positioning using d3.select(this)
- How to have a definite boundary on a d3.js horizontal bar chart