我有地圖上這種形式顯示許多圖表具有相同源的JavaScript的數據(地圖(鍵,值))
Object{485:Array[4],2072:Array[4],9665:Array[3]...}
我想顯示每個鍵的圖表的。
我有一個例子是類似我正在尋找我試圖在許多圖表,顯示了我的價值觀:在這個例子中,我的關鍵是:car_id
第一步:排序我的陣列來獲得我的鑰匙地圖。它工作正常。但第二個不工作...我想顯示一個基於car_id和每個面板bootstrap每個數據的圖表... 我做錯了什麼?
謝謝
<html >
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
</head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript --><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js"></script>
<script src=" https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js"></script>
<body >
<div >
<div class="panel panel-default">
<div class="panel-heading">Panel heading without title</div>
<div class="panel-body">
<canvas id="myChart" width="400" height="400"></canvas>
</div>
</div>
</div>
</body>
<script>var json = {
'cars': [{
"car_id": "1",
"price": "925",
"full_option": "EEEE"
}, {
"car_id": "1",
"price": "990",
"full_option": "DDDDD"
}, {
"car_id": "2",
"price": "500",
"full_option": "FFF"
}, {
"car_id": "2",
"price": "900",
"full_option": "GGGGGGG"
}, {
"car_id": "4",
"price": "900",
"full_option": "JJJ"
}]
};
var car_array = json.cars.reduce((prev, t, index, arr) => {
if (typeof prev[t.car_id] === 'undefined') {
prev[t.car_id] = [];
}
prev[t.car_id].push(t);
return prev;
}, {});
Object.keys(car_array).forEach(i => {
var array_of_cars_with_same_id = car_array[i];
for (var i=0; i<=array_of_cars_with_same_id.length-1;i++){
console.log(array_of_cars_with_same_id[i].price);
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [array_of_cars_with_same_id[i].full_option],
datasets: [{
label: '# of Votes',
data: [array_of_cars_with_same_id[i].price]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
console.log("AAAAAAAAAAAAA");
});
</script>
是'Object {485:Array [4],2072:Array [4],9665:Array [3] ...}'應該和變量'json'一樣嗎?代碼是否爲'Object.keys(car_array).forEach(i => {'你指的是'第二個代碼不工作'?什麼是不工作? – whipdancer
@whipdancer Object {485:Array [4 ],2072:Array [4],9665:Array [3] ...}與變量json不同。變量json是一個與「Object {485:Array [4],2072:Array [4],9665:Array [3] ...}」類似的小例子,它太大了。這就是爲什麼我選擇了一個更簡單的例子。不工作的部分是應該爲每個鍵創建圖表的部分(在for循環中)。我只得到了所有按鍵的圖表。 – John
請提供'對象{485:數組[4],2072:數組[4],9665:數組[3] ...}' - 一個小版本。 3個鍵或類似的東西,以便有一個有代表性的樣本 - 或者構建您的樣本,以便它完全模仿您正在處理的數據。 – whipdancer