我想制作一个用SQL查询数据填充的Google甜甜圈图。
我搜索了示例代码,但无法显示图表。
脚本:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Global variable to hold data
google.load('visualization', '1', { packages: ['corechart'] });
google.setOnLoadCallback(DrawDonut);
$(function DrawDonut() {
var options = {
pieHole: 0.4,
colors: ['#286090', '#d7d7d7']
};
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'Default.aspx/GetChartData',
data: '{}',
success:
function (response) {
drawchart(response.d);
},
error: function () {
alert("Error loading data! Please try again.");
}
});
})
function drawchart(dataValues) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'UnitID');
data.addColumn('number', 'TotalUse');
for (var i = 0; i < dataValues.length; i++) {
data.addRow([dataValues[i].UnitID, dataValues[i].TotalUse]);
}
new google.visualization.PieChart(document.getElementById('chart')).
draw(data, { title: "Use distributed on units" });
}
</script>
HTML:
<div id="chart" style="width: 900px; height: 600px;"></div>
后面的代码:
<WebMethod()>
Public Shared Function GetChartData(ByVal MemberIndex As String) As List(Of Object)
Dim query As String = "SELECT Log.[UnitID], COUNT(*) As [TotalUse] FROM Log WHERE (MemberIndex_Log = @MemberIndex AND (GramSup1 + GramSup2) > 0 AND Log.[UnitID] > 5000) GROUP BY Log.[UnitID]"
Dim constr As String = ConfigurationManager.ConnectionStrings("DefaultConnection").ConnectionString
Dim chartData As List(Of Object) = New List(Of Object)()
chartData.Add(New Object() {"UnitID", "TotalUse"})
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Connection = con
cmd.Parameters.AddWithValue("@MemberIndex", Convert.ToInt32(HttpContext.Current.Session("MemberIndex")))
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
chartData.Add(New Object() {sdr("UnitID"), sdr("TotalUse")})
End While
End Using
con.Close()
Return chartData
End Using
End Using
End Function
我希望显示一个动态甜甜圈图,以显示分布在图表上的单位(通常为2个10个单位)的使用。
答案 0 :(得分:0)
首先,jsapi
是Google图表的旧版本,不应再使用。
请改用loader.js
,记下路径...
<script src="https://www.gstatic.com/charts/loader.js"></script>
这只会更改load
语句。
接下来,谷歌图表将默认等待页面加载。
无需将代码放入jquery速记文档就绪函数内-> $(function () {
将DrawDonut
函数放置在ready函数中,
将其从google回调中移除。
由于无法达到,因此无法运行。
最后,不推荐使用success
上的error
和$.ajax
回调,
改用done
和fail
的诺言。
推荐类似于以下代码段的设置...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var options = {
title: 'Use distributed on units',
pieHole: 0.4,
colors: ['#286090', '#d7d7d7'],
width: 900,
height: 600
};
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: 'Default.aspx/GetChartData',
data: '{}'
}).done(function (response) {
drawchart(response.d);
}).fail(function () {
console.log('Error loading data! Please try again.');
});
function drawchart(dataValues) {
var data = new google.visualization.DataTable();
data.addColumn('string', 'UnitID');
data.addColumn('number', 'TotalUse');
for (var i = 0; i < dataValues.length; i++) {
data.addRow([dataValues[i].UnitID, dataValues[i].TotalUse]);
}
new google.visualization.PieChart(document.getElementById('chart')).
draw(data, options); // <-- use options already defined above
}
});
</script>
<div id="chart"></div>
注意:您正在此处定义options
对象
var options = {
pieHole: 0.4,
colors: ['#286090', '#d7d7d7']
};
但不使用它绘制图表,这里...
draw(data, { title: "Use distributed on units" });