查看代码
$.getJSON('Transacciones/getData',{}, function (data) {
dataModulo3 = data;
dibujarColumnChart( transformarData(data) , "divModulo3");
});
控制器,我从中获取数据
public ActionResult getData(){
List<String[]> aux = new List<String[]>();
aux.Add(new String[] { "string", "number", "number", "number", "number", "number", "number" });//tipo cabeceras
aux.Add(new String[] { "Year", "Austria", "Belgium", "Czech Republic", "Finland", "France", "Germany" });//nombre cabeceras
aux.Add(new String[] { "2003", "1336060", "3817614", "974066", "21104797", "6651824", "15727003"});
return Json(aux, JsonRequestBehavior.AllowGet);
}
transformarData js函数如下
function transformarData(data) {
var rows = data.length;
var cols = data[0].length;
var tdata = new google.visualization.DataTable();
for (var i = 0; i < cols; i++) {
tdata.addColumn(data[0][i], data[1][i]);
}
tdata.addRows(data.length);
for (var i = 2; i < data.length; i++) {
for (var j = 0; j < cols; j++) {
var value = data[i][j];
if (data[0][j] == 'number') {
value = parseInt(data[i][j]);
}
tdata.setCell(i, j, value);
}
}
return tdata;
}
dibujarColumnChart 函数 js 波纹管
function dibujarColumnChart(datos, idDiv) {
var data = datos;
var columnchart = new google.visualization.ColumnChart(document.getElementById(idDiv));
columnchart.draw(data, {
backgroundColor: "transparent"
});
}
但是,如果我不使用来自控制器的数据,而是使用 javascript 数据:
$.getJSON('Transacciones/getData',dataJson , function (data) {
dataModulo3 = data;
var datajs = google.visualization.arrayToDataTable([
['Year', 'Austria', 'Belgium', 'Czech Republic', 'Finland', 'France', 'Germany'],
['2003', 1336060, 3817614, 974066, 1104797, 6651824, 15727003]
]);
dibujarColumnChart( datajs , "divModulo3");
});
我得到居中的柱形图:
我想知道我是否可以将这个从 json 调用中获取数据的柱形图居中。