score:0
Because it is the convertion rate of bitcoin compared to other coins it does not make sense to plot them all in one graph. The JPY
would crush all the other bars.
Why buy/sell bitcoin using different coins if they just are conversions given the current rate.
If you draw a single coin values you get the following graph
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.axis--x path {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var x = d3.scaleBand().rangeRound([0, width]).padding(0.1),
y = d3.scaleLinear().rangeRound([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("https://blockchain.info/ticker", function(error, data) {
if (error) throw error;
data = data.EUR;
var keys = ["15m", "last", "buy", "sell"];
x.domain(keys);
y.domain([0, d3.max(keys, function(k) { return data[k]; })]);
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("price");
g.selectAll(".bar")
.data(keys)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(k) { return x(k); })
.attr("y", function(k) { return y(data[k]); })
.attr("width", x.bandwidth())
.attr("height", function(k) { return height - y(data[k]); });
});
</script>
Also not useful. Because all the variation is in the top few pixels.
What you need is a way to record the values from the JSON file over time for a particular coin and graph that like your original dataset.
Can be done by fetching the data every x minutes and then modify the graph with the enter/exit/remove data()
call or just redraw the graph like this.
Place comments before the DEBUG sections for the real data. And uncomment the following line
//setInterval(getData, 5 * 60 * 1000);
Here I generate dummy data every 5 seconds for the demo.
To prevent out of memory data
length is limited to 1000 samples.
Edit
It now shows the date of the sample on the x-axis.
<!DOCTYPE html>
<style>
.p15m { stroke: steelblue;}
.pbuy { stroke: red;}
.plast { stroke: green;}
.psell { stroke: orange;}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, left: 50},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom;
var data = [];
var x = d3.scaleTime()
.range([0, width]);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var line = d3.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); });
function getData() {
//DEBUG
data.push( {"15m": Math.random()*100 + 100, "last": Math.random()*100 + 100, "buy": Math.random()*100 + 100, "sell": Math.random()*100 + 100, "date": new Date() } );
updateGraph();
return;
// DEBUG END
d3.json("https://blockchain.info/ticker", function(error, dataNew) {
if (error) throw error;
var d = dataNew.EUR;
d.date = new Date();
data.push();
if (data.length > 1000) data = data.shift();
updateGraph();
});
}
getData();
setTimeout(getData, 5000);
//DEBUG
setInterval(getData, 5 * 1000);
//DEBUG END
//setInterval(getData, 5 * 60 * 1000);
function updateGraph() {
if (data.length < 2) return;
svg.select("g").remove(); // clean the graph
var keys = ["15m", "last", "buy", "sell"];
var g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
x.domain(d3.extent(data, d => d.date));
var flat = [];
data.map( d => keys.map(k => d[k]) ).forEach(e => { flat = flat.concat(e); });
y.domain(d3.extent(flat , function(d) { return d; }));
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.select(".domain")
.remove();
g.append("g")
.call(d3.axisLeft(y))
.append("text")
.attr("fill", "#000")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("text-anchor", "end")
.text("Price (EUR)");
g.selectAll("g.key")
.data(keys)
.enter()
.append("g")
.attr("class", d => "key p" + d )
.append("path")
.datum(k => data.map( (d, i) => [d.date, d[k]]))
.attr("fill", "none")
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("stroke-width", 1.5)
.attr("d", line);
}
</script>
Source: stackoverflow.com
Related Query
- how to code according to json data from api in d3.js?
- How to load external JSON data from a Web API in d3.js?
- how to make real time multi line chart using d3 where data is from api in json format
- How to load data from an internal JSON array rather than from an external resource / file for a collapsible tree in d3.js?
- D3 Library — How To Access Data from JSON in this example
- how can i group JSON data into the weeks according to calender?
- D3 linegraph using JSON data from API
- D3.js : How to read data from JSON in Grouped Bar Chart
- How to read JSON data from same json for tree layout insteadof reading from other .JSON file?
- How to fetch data from json file to draw a dynamic graph by using d3.js
- How to request data from json in d3?
- How do you call data from nested JSON array using d3?
- How to load data to D3 chart from JSON when there are only tuples
- How to get a data from an analysis in R in Json file format to get D3 visualisations?
- how to convert data selected from a postgres database to json or csv to use it with d3js lib?
- How can I pass json data returned from a webservice into D3.json(...)?
- How to link extra attributes from json data in a D3.js force layout?
- How to use attr fill with scalesqrt using data from another json file?
- How to modify tags of an already existing svg object with data from json file
- How to get json data from controller in jsp?
- How to transform code from a tsv import to direct data
- how to access data from nested json array D3
- D3: How to pull JSON from Github API
- D3 Charts, How to iterate through Json data NOT from file?
- How to update data on a page according to data from a CSV file instead of using fixed element data on the page?
- How to generate JSON from a pandas data frame for dynamic d3.js tree
- How to create histogram using D3.js and reading data from JSON
- How to link a JSON object from R source code to d3.js for building a shiny app - shiny doesn't render d3
- Angular How to use data from json in c3?
- D3.js: How can I tell apart JSON data from JSON file/endpoint
More Query from same tag
- Getting d3.events as null in angular2 typescript code
- Is there a way to make Internet Explorer understand this.style.setProperty?
- How are enter() and exit() detecting updated data in D3?
- D3 world-50m.json projection filling incorrectly
- How to get a value from a stylesheet into the code so that it can be used programmatically?
- D3 Optional Transitions
- make a simple pie chart directive with d3js
- How to add multiple d3 items to one page
- How to update data in stack bar chart in D3
- Update d3 data element after chart is drawn
- how to display data in which format using dc.js
- D3 onclick, onmouseover and mouseout behavior overriding
- D3 Reusable Chart Function Creating Multiple Charts
- Json file for Canada's province
- Can't refer to json data with D3 js if it has a whitespace
- Zoom Pan images with d3.js
- Error: parsing d="MNaN,260L on D3.js multiline graph with JSON data
- Binding ds.js Scatter Chart to JSON data
- d3.js forced directed graph effects on enter
- using d3.js as an external in webpack
- D3.js - add a button on click of a link connecting nodes in a TreeLayout
- loop through array and assign each item as a class in html tag using d3
- D3: Giving a linear scale a dynamic domain
- I am searching for way to customize Y-Axis in recharts react library?
- d3 force element(node) mouseout is triggering when moves out of contained circle or text
- Making a line with a different data format
- D3.js - Removing other bars except one
- d3js can't access data index
- Can you use if statements in .nest(). If not what is the best way to assign labels to array elements so they fit in several groups
- How to modify axis labels in d3 for a stacked bar chart when the axis labels are mapped as part of the scale's domain