내가 D3를 사용하여 차트를 만드는 방법에 대한 자습서 다음 해요 :서비스에서 d3 데이터를 요청하는 방법은 무엇입니까?
d3.tsv("data.tsv", type, function(error, data) {
data.tsv 로컬 파일입니다하지만 어떻게 할 수 있습니다 http://bost.ocks.org/mike/bar/3/
데이터를 읽을 수있는 것은 D3를 사용하여 표시 할 내가 사용 이 데이터는 URL에서 읽습니까?
$ 아약스 ({ URL : "\ 데이터 \" 상황 :는 document.body }) 내가 생각하고
나는 아약스 요청을 사용할 필요가
하지만 이후 그것 비동기 어떻게 이것을 d3의 일부로 사용할 수 있습니까?
업데이트 : 데이터가 이제 jQuery를 GET 요청에 싸여 :
letter frequency
test title 6 221
test title 8 218
D3는 막대 차트를 렌더링하고있다되지 않은 : 여기
$.get("getData.do", function(data) {
d3.tsv.parse(data, type, function(error, data) {
데이터의 형식입니다 콘솔에 오류가보고되지 않았습니다. 파일에서 읽을 때 다이어그램이 올바르게 표시되므로 데이터 형식이 정확합니다. 데이터에 올바르게 액세스하고 있습니까?
전체 코드 :
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: steelblue;
}
.bar:hover {
fill: brown;
}
.x.axis path {
display: none;
}
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
/* Creates a small triangle extender for the tooltip */
.d3-tip:after {
box-sizing: border-box;
display: inline;
font-size: 10px;
width: 100%;
line-height: 1;
color: rgba(0, 0, 0, 0.8);
content: "\25BC";
position: absolute;
text-align: center;
}
/* Style northward tooltips differently */
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var formatPercent = d3.format(".0%");
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(formatPercent);
var tip = d3.tip()
.attr('class', 'd3-tip')
.offset([-10, 0])
.html(function(d) {
return "<strong>Frequency:</strong> <span style='color:red'>" + d.frequency + "</span>";
})
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.call(tip);
$.get("http://getFile", function(data) {
d3.tsv(data, type, function(error, data) {
x.domain(data.map(function(d) { return d.letter; }));
y.domain([0, d3.max(data, function(d) { return d.frequency; })]);
svg.append("text")
.attr("x", (width/2))
.attr("y", 0 - (margin.top/2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Article Read Count");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
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("Frequency");
svg.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.letter); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.frequency); })
.attr("height", function(d) { return height - y(d.frequency); })
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
});
function type(d) {
d.frequency = +d.frequency;
return d;
}
});
</script>
어디서나 데이터를 가져 와서'd3.tsv.parse (datastring)'을 사용하십시오. https://github.com/mbostock/d3/wiki/CSV#wiki-tsv_parse를 참조하십시오. – Sacho
@Sacho 감사합니다. 질문의 업데이트를 참조하십시오. –
데이터를 가져 와서 구문 분석을 호출하는 것보다 더 많은 작업을 수행해야합니다. 모든 구문 분석은 TSV 형식의 데이터를 javascript 객체로 변환합니다. 그때부터 차트 축을 그려서 전체 d3 select-> data-> enter-> append주기를 사용해야합니다. 몇 가지 샘플 코드로 답변을 게시 하겠지만 링크 된 사이트에서 실제로 복사하여 붙여 넣을 것입니다. 실제로이 모든 작업을 수행하는 경우 코드를 게시하여 사람들이 무엇이 잘못되었는지 확인할 수 있습니다. – Sacho