*[感谢 Gerardo Furtado 找到解决方案]
我是编码新手,目前正在从事太阳能/风能预测元素。在我的设计中,我需要一个图表来显示 future 一周的千瓦发电量预测。我一直在尝试根据找到的教程 here 将数据从 Dark Sky API 连接到下图。 。我现在尝试用随机生成的数字作为虚拟数据替换频率数据值,但不知道如何执行此操作。关于如何解决这个问题有什么建议吗?谢谢。
index.html:
<script>
var margin = {top: 40, right: 20, bottom: 30, left: 40},
width = 900 - 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>Predicted KW Total:</strong> <span style='color:#FFD15B'>" + d.frequency + "</span>";
})
var svg = d3.select("#chart").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);
d3.tsv("data.tsv", 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("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("Kilowatts (KW)");
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)
});
d3.select("#chart").attr("align","center");
function type(d) {
d.frequency = +d.frequency;
return d;
}
</script>
CSS:
<style>
#chart {
background-color: #FFFFFF;
float:left;
width: 80%;
height: 550px;
padding-top: 25px;
}
.light {
font-weight: bold;
color: #717070;
}
.tagline {
text-align: center;
color: #FFFFFF;
margin-top: 4px;
font-weight: lighter;
text-transform: uppercase;
letter-spacing: 1px;
}
bod {
font: 10px sans-serif;
}
.axis path,.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar:hover {
fill: #FFD15B;
}
.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;
}
.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;
}
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
.axis text {
font: 10px sans-serif;
}
.axis path,.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: #D47039;
}
.x.axis path {
display: none;
}
label {
position: absolute;
top: 10px;
right: 10px;
}
</style>
数据.tsv:
letter frequency
Sun 10
Mon 20
Tues 5
Wed 10
Thurs 30
Fri 15
Sat 8
最佳答案
由于您只需要用随机值替换该 TSV,因此创建一个包含日期的数组:
var days = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"];
并根据days
创建您自己的data
数组:
var data = [];
days.forEach(function(d) {
data.push({
letter: d,
frequency: ~~(Math.random() * 30)
})
})
这是一个工作演示:
var days = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"];
var data = [];
days.forEach(function(d) {
data.push({
letter: d,
frequency: ~~(Math.random() * 30)
})
})
var margin = {
top: 40,
right: 20,
bottom: 30,
left: 40
},
width = 900 - 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>Predicted KW Total:</strong> <span style='color:#FFD15B'>" + 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);
x.domain(data.map(function(d) {
return d.letter;
}));
y.domain([0, d3.max(data, function(d) {
return d.frequency;
})]);
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("Kilowatts (KW)");
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)
.light {
font-weight: bold;
color: #717070;
}
.tagline {
text-align: center;
color: #FFFFFF;
margin-top: 4px;
font-weight: lighter;
text-transform: uppercase;
letter-spacing: 1px;
}
bod {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar:hover {
fill: #FFD15B;
}
.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;
}
.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;
}
.d3-tip.n:after {
margin: -1px 0 0 0;
top: 100%;
left: 0;
}
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.bar {
fill: #D47039;
}
.x.axis path {
display: none;
}
label {
position: absolute;
top: 10px;
right: 10px;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.min.js"></script>
关于javascript - D3.js 图表随机数据或天气 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43771383/