0
Iは、X軸上の複数の日付が表示されているこの単純d3.js単一線グラフ有する
表示する:使用D3.js単一の線グラフは、同じ日付が異なる温度
<script>
// Parse the date/time
var parseDate = d3.time.format("%d-%b-%y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom");
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
// Adds the svg canvas
var svg = d3.select("#air_temp_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 + ")");
// Get the data
d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
var tickValues = data.map(function(d) { return d.date; });
xAxis
.tickValues(tickValues)
.tickFormat(d3.time.format('%H:%M'));
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
を})。
data.csv:日付はすべてのために同じですが、時間と温度の測定値が異なる値になるように、私はデータを変更したい
date,close
1-May-12,58.13
30-Apr-12,53.98
27-Apr-12,67.00
。
私はにtime.formatラインを変更:私はこれを行うときの測定値が、グラフの休憩で時間を含めるように
var parseDate = d3.time.format("%d-%b-%y %H").parse;
。
私は同じ日付が異なる時間と温度の読みと、このデータを使用したい:
1-May-12 06:00,58.13
1-May-12 06:30,53.98
1-May-12 07:00,67.00
どのように私は上記の値で動作するように、x軸のコードを変更しますか?
ありがとうございました!あなたの提案を反映するためにコードを更新するように頼むことができますか?私はtickValues行をどこに配置するのか混乱しています。古いxAxis変数宣言を指定した変数に置き換えるだけですか?再度、感謝します! – cpcdev
@cpcdevより具体的に答えを編集しました。コードは行ごとに表示されます – iulian
ありがとうございます!私はvar tickValues行を正しく配置しましたが、xAxisはまだ問題があります。オリジナルの投稿を更新して、変更したxAxisコードを表示しました。私はここで何が欠けていますか? – cpcdev