I want to make a Google doughnut chart populated with data from a sql query.
I searched for example code but has not been able to make a chart appear.
Script:
<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>
Code behind:
<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
I want a dynamic doughnut chart to appear, showing the use of the units (typically from 2 10 units) distributed on the chart.
first,
jsapi
is an old version of google charts and should no longer be used.use
loader.js
instead, note the path...this will only change the
load
statement.next, google charts will wait for the page to load by default.
no need to place your code inside the jquery short-hand document ready function -->
$(function () {
placing the
DrawDonut
function inside the ready function,takes it out of scope from the google callback.
since it cannot be reached, it does not run.
last, the
success
&error
callbacks on$.ajax
have been deprecated,use the
done
&fail
promises instead.recommend setup similar to the following snippet...
note: you are defining your
options
object, here...but not using it to draw the chart, here...