例:
モデル:
public class MyModel
{
public string DateColumn { get; set; }
public int Count { get; set; }
public static List<MyModel> GetList()
{
return new List<MyModel>
{
new MyModel {DateColumn = "09-05-2017",Count = 10},
new MyModel {DateColumn = "10-05-2017",Count = 30},
new MyModel {DateColumn = "11-05-2017",Count = 50},
new MyModel {DateColumn = "12-05-2017",Count = 10},
new MyModel {DateColumn = "13-05-2017",Count = 100},
new MyModel {DateColumn = "14-05-2017",Count = 20},
new MyModel {DateColumn = "15-05-2017",Count = 10},
new MyModel {DateColumn = "16-05-2017",Count = 150},
new MyModel {DateColumn = "17-05-2017",Count = 15},
new MyModel {DateColumn = "18-05-2017",Count = 11},
new MyModel {DateColumn = "19-05-2017",Count = 5},
};
}
}
Clontoller:
public class HomeController : Controller
{
public JsonResult GetCount()
{
var model = MyModel.GetList();//Lode data from database
return Json(model, JsonRequestBehavior.AllowGet);
}
}
ビュー:
<div class="box box-without-bottom-padding">
<canvas id="myChart"></canvas>
</div>
スクリプト:
<script type="text/javascript">
$(function() {
var data = getData();
AutoFollow(data);
});
function getData() {
var dateValue = [];
var countValue = [];
$.ajax({
url: "/home/GetCount/",
dataType: 'json',
async: false
}).done(function(data) {
data.forEach(function(obj) {
dateValue.push(obj.DateColumn);
countValue.push(obj.Count);
});
});
return {
dateValue: dateValue,
countValue: countValue
};
}
function AutoFollow(data) {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx,
{
type: 'bar',
data: {
labels: data.dateValue, // here i want to show my date value
datasets: [
{
label: 'AutoFollow',
data: data.countValue, // here show my total count date wise
backgroundColor: "rgba(153,255,51,1)"
}, {
label: 'Manual',
data: [30, 29, 5, 5, 20, 3, 10],
backgroundColor: "rgba(255,153,0,1)"
}
]
}
});
}
</script>
================================= ========================================== ========================================== ============
--------------------------
**As Per Your Request**
--------------------------
モデル:
public class MyModel
{
public string DateColumn { get; set; }
public string Lable { get; set; }
public int Count { get; set; }
public static List<MyModel> GetList()
{
return new List<MyModel>
{
new MyModel {DateColumn = "09-05-2017",Count = 10,Lable = "Lable1"},
new MyModel {DateColumn = "10-05-2017",Count = 30,Lable = "Lable1"},
new MyModel {DateColumn = "11-05-2017",Count = 50,Lable = "Lable1"},
new MyModel {DateColumn = "12-05-2017",Count = 10,Lable = "Lable1"},
new MyModel {DateColumn = "13-05-2017",Count = 100,Lable = "Lable1"},
new MyModel {DateColumn = "14-05-2017",Count = 20,Lable = "Lable2"},
new MyModel {DateColumn = "15-05-2017",Count = 10,Lable = "Lable2"},
new MyModel {DateColumn = "16-05-2017",Count = 150,Lable = "Lable2"},
new MyModel {DateColumn = "17-05-2017",Count = 15,Lable = "Lable2"},
new MyModel {DateColumn = "18-05-2017",Count = 11,Lable = "Lable2"},
new MyModel {DateColumn = "19-05-2017",Count = 5,Lable = "Lable2"},
};
}
}
処置:
public JsonResult GetCount()
{
var data = MyModel.GetList()
.GroupBy(s => s.Lable)
.Select(s => new
{
DateColumn = s.Select(d => d.DateColumn),
data = new
{
Lable = s.Key,
Count = s.Select(d => d.Count)
}
}); //Lode data from database
var model = new
{
DateColumn = data.First().DateColumn,
CountColumn = data.Select(s => s.data)
};
return Json(model, JsonRequestBehavior.AllowGet);
}
スクリプト:
<script type="text/javascript">
$(function() {
var data = getData();
AutoFollow(data);
});
function getData() {
var dateValue ;
var countValue;
$.ajax({
url: "/Chart/GetCount/",
dataType: 'json',
async: false
}).done(function (data) {
dateValue = data.DateColumn;
countValue = data.valueColumn;
});
return {
dateValue: dateValue,
countValue: countValue
};
}
function AutoFollow(data) {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx,
{
type: 'bar',
data: {
labels: data.dateValue, // here i want to show my date value
datasets: [
{
label: data.countValue[0].Lable,
data: data.countValue[0].Count, // here show my total count date wise
backgroundColor: "rgba(153,255,51,1)"
}, {
label: data.countValue[1].Lable,
data: data.countValue[1].Count,
backgroundColor: "rgba(255,153,0,1)"
}
]
}
});
}
</script>
あなたのチャートは、データの配列を必要とします。 AJAXを介してJSON Resultを使用してデータの配列を提供します。あなたのテーブルのデータは、常に1週間の曜日を表す7行を持っていますか? –