Here I made a very basic graph with a button that does what you want. It is a little choppy as there are some dates missing. I hope it can help you, it is based on one of the simple samples at http://bl.ocks.org/mbostock
Here is the relevant part:
//Add new post to data
data.push({
"Date": new Date(new Date().setDate(new Date().getDate() + skip)),
"Close": price
});
//assign the new data to the graph
svg.select(".area")
.datum(data)
.attr("d", area)
.attr("transform", null);
//Make sure the X axis is updated (not updating the Y axis in the example, but same principle)
x.domain(d3.extent(data.map(function (d) {
return d.Date;
})));
svg.select(".x.axis").transition().call(xAxis);
data.shift();
var skip = 0;
var oldPrice = 11;
var data = [{
"Date": "2015-08-20",
"Close": 7
}, {
"Date": "2015-08-23",
"Close": 8
}, {
"Date": "2015-08-24",
"Close": 9
}, {
"Date": "2015-08-25",
"Close": 6
}, {
"Date": "2015-08-26",
"Close": 5
}, {
"Date": "2015-08-27",
"Close": 7
}, {
"Date": "2015-08-30",
"Close": 5
}, {
"Date": "2015-08-31",
"Close": 9
}, {
"Date": "2015-09-01",
"Close": 8
}, {
"Date": "2015-09-02",
"Close": 10
}, {
"Date": "2015-09-03",
"Close": 11
}, {
"Date": "2015-09-07",
"Close": 12
}, {
"Date": "2015-09-08",
"Close": 11
}, {
"Date": "2015-09-09",
"Close": 12
}, {
"Date": "2015-09-10",
"Close": 13
}, {
"Date": "2015-09-13",
"Close": 14
}, {
"Date": "2015-09-14",
"Close": 15
}, {
"Date": "2015-09-15",
"Close": 13
}, {
"Date": "2015-09-16",
"Close": 11
}, {
"Date": "2015-09-17",
"Close": 7
}, {
"Date": "2015-09-20",
"Close": 6
}, {
"Date": "2015-09-21",
"Close": 5
}, {
"Date": "2015-09-22",
"Close": 6
}, {
"Date": "2015-09-23",
"Close": 7
}, {
"Date": "2015-09-24",
"Close": 8
}, {
"Date": "2015-09-27",
"Close": 10
}, {
"Date": "2015-09-28",
"Close": 9
}, {
"Date": "2015-09-29",
"Close": 10
}, {
"Date": "2015-09-30",
"Close": 11
}];
var margin = {
top: 10,
right: 10,
bottom: 100,
left: 40
};
var width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d").parse;
var x = d3.time.scale().range([0, width]),
y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis().scale(x).orient("bottom"),
yAxis = d3.svg.axis().scale(y).orient("left");
var area = d3.svg.area()
.interpolate("monotone")
.x(function (d) {
return x(d.Date);
})
.y0(height)
.y1(function (d) {
return y(d.Close);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom);
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data.forEach(function (d) {
d.Date = parseDate(d.Date);
d.Close = +d.Close;
});
x.domain(d3.extent(data.map(function (d) {
return d.Date;
})));
y.domain([0, 200]);
focus.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
focus.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "y axis")
.call(yAxis);
function update() {
//Generate a new price
var price = Math.floor(Math.random() * 5) + oldPrice
//Add new post to data
data.push({
"Date": new Date(new Date().setDate(new Date().getDate() + skip)),
"Close": price
});
//assign the new data to the graph
svg.select(".area")
.datum(data)
.attr("d", area)
.attr("transform", null);
//Make sure the X axis is updated (not updating the Y axis in the example, but same principle)
x.domain(d3.extent(data.map(function (d) {
return d.Date;
})));
svg.select(".x.axis").transition().call(xAxis);
data.shift();
skip++;
oldPrice = price;
}
$(document).ready(function () {
$('#update').click(function () {
update();
});
});
svg {
font: 10px sans-serif;
}
.area {
fill: steelblue;
clip-path: url(#clip);
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.brush .extent {
stroke: #fff;
fill-opacity: .125;
shape-rendering: crispEdges;
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
</head>
<body>
<input type="button" id="update" value="update">
</body>
</html>