我是D3和javascript的新手,所以如果這很明顯,我很抱歉。 我有一組數據在Flot中工作,但我似乎無法在d3中識別它,它是沒有標識符的json。如處理沒有標識符的JSON數據
{
"dataLine": [ [134, 43.39], [144, 45.34], [154, 47.45], [164, 48.25], [174, 48.14], [184, 48.21], [194, 47.64], [204, 47.58], [214, 52.18], [224, 58.18], [234, 53.19], [244, 61.28], [254, 56.50], [264, 53.92], [274, 57.27], [284, 58.73], [294, 57.28], [304, 52.91], [314, 55.07], [324, 60.58], [334, 61.16], [344, 62.64], [354, 60.39], [364, 62.79], [372, 65.24] ]
}
我可以,如果我每個條目前面加上了「X」和「Y」標籤中的數據的工作,但我會如何處理數據,而標籤?假設第一個條目是「x」,第二個條目是「y」。我目前正在使用的代碼是
d3.json('3.json', function (data)
{
data.forEach(function(d)
{
d.x = d.x;
d.y = d.y;
});
x.domain(d3.extent(data, function(d) { return d.x; }));
y.domain([0,d3.max(data, function(d) { return d.y; })]);
svg.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area);
//.attr("d", valueline);
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("Force (N)");
});
而且數據看起來像
[
{"x":134, "y":43.39}, {"x":144, "y":45.34}, {"x":154, "y":47.45}, {"x":164,"y":48.25}, {"x":174, "y":48.14}, {"x":184, "y":48.21}, {"x":194, "y":47.64}, {"x":204, "y":47.58}, {"x":214, "y":52.18}, {"x":224, "y":58.18}, {"x":234, "y":53.19}, {"x":244, "y":61.28}, {"x":254, "y":56.50}, {"x":264, "y":53.92}, {"x":274, "y":57.27}, {"x":284, "y":58.73}, {"x":294, "y":57.28}, {"x":304, "y":52.91}, {"x":314, "y":55.07}, {"x":324, "y":60.58}, {"x":334, "y":61.16}, {"x":344, "y":62.64}, {"x":354, "y":60.39}, {"x":364, "y":62.79}, {"x":372, "y":65.24}
]
我可以繪製它像這樣,但我所有的歷史數據,就像是第一個例子,我知道我做錯了什麼因爲我現在正在盲目地跟隨教程和示例,並且非常感謝任何幫助或提示。爲了簡單起見,有沒有辦法繪製我發佈的第一組數據?如果是,那麼讓我進一步考慮一下,我如何繪製條目「dataLine」,並在同一個json文件中還有5個其他條目進行圖形化,這些條目的格式相同但標籤不同?
你能從它做一個jsfiddle?我認爲更正這種方式更容易。 – Peter