It seems like it thinks that your CNT column is of type string. That wasn't
what your original post implied. If that is indeed the case, you need to
convert them to numbers. How you do that is up to you. A simple way would
be to create a DataView with a calc column in order to convert it.
Something like this should work:
var view = new google.visualization.DataView(data);
view.setColumns([0, {type: 'number', calc: function(table, row) { return
parseInt(table.getValue(row, 1)); }}]);
And then you should use the view instead of data when you draw the chart.
On Tue Aug 19 2014 at 11:09:10 AM Tiago Correia <[email protected]>
wrote:
> // Load the Visualization API and the piechart package.
> google.load('visualization', '1', {'packages':['corechart']});
>
> // Set a callback to run when the Google Visualization API is loaded.
> google.setOnLoadCallback(drawChart);
>
> function drawChart() {
> var jsonData = $.ajax({
> url: "teste.cshtml",
> dataType:"json",
> async: false
> }).responseText;
>
>
>
>
> function jsonToTable(json, order) {
>
>
> var headers = [];
> var data = [headers];
> for (var i = 0; i < json.length; i++) {
> var row = [];
> for (var j = 0; j < order.length; j++) {
> if (headers.length <= j) {
> headers.push(order[j]);
> }
> row.push(json[i][order[j]]);
> }
> data.push(row);
> }
> return google.visualization.arrayToDataTable(data);
> }
>
>
>
> var data = jsonToTable(jsonData, ['ESTADO', 'CNT']);
>
>
>
> // Instantiate and draw our chart, passing in some options.
> var chart = new
> google.visualization.LineChart(document.getElementById('chart_div'));
> chart.draw(data, {width: 800, height: 600});
> }
>
>
> I've put like you said and it gives me the following error:
>
> Data column(s) for axis #0 cannot be of type string×
>
>
>
> Terça-feira, 19 de Agosto de 2014 13:38:46 UTC+1, Tiago Correia escreveu:
>
>> I'm using webmatrix to create a chart from my db. I have one cshtml file
>>
>> var db = Database.Open("MyDB");
>> var data = @"SELECT ESTADO, COUNT(*) CNT FROM graficos WHERE ESTADO IS
>> NOT NULL GROUP BY ESTADO";
>> var resultadoSql = db.Query(data);
>> Json.Write(resultadoSql, Response.Output);
>> Response.ContentType = "application/json";
>>
>> That is producing this result
>>
>> [{"ESTADO":"ABERTO","CNT":63},{"ESTADO":"ASSOCIADO A AVARIA
>> PAI","CNT":9},{"ESTADO":"AVARIA PAI COM EM
>> EXECUÇÃO","CNT":3},{"ESTADO":"AVARIA PAI EM
>> DESPISTE","CNT":3},{"ESTADO":"CANCELADO","CNT":3},{"ESTADO":"EM
>> DESPISTE","CNT":18},{"ESTADO":"EM
>> EXECUÇÃO","CNT":27},{"ESTADO":"FECHADO","CNT":189},{"ESTADO":"RESOLVIDO","CNT":51}]
>>
>> And on the page that the chart will be displayed (yeap google charts :x)
>>
>> // Load the Visualization API and the piechart package.
>> google.load('visualization', '1', {'packages':['corechart']});
>>
>> // Set a callback to run when the Google Visualization API is loaded.
>> google.setOnLoadCallback(drawChart);
>>
>> function drawChart() {
>> var jsonData = $.ajax({
>> url: "teste.cshtml",
>> dataType:"json",
>> async: false
>> }).responseText;
>>
>> // Create our data table out of JSON data loaded from server.
>> var data = new google.visualization.DataTable(jsonData);
>>
>> // Instantiate and draw our chart, passing in some options.
>> var chart = new
>> google.visualization.LineChart(document.getElementById('chart_div'));
>> chart.draw(data, {width: 800, height: 600});
>> }
>>
>> But i can't produce the chart it always says
>>
>> Table has no columns.×
>>
>> Then i've tried this that was on one example and it works, the chart is
>> created with the values.
>>
>> {"cols":[
>> {"type":"string"},{"type":"number"}
>> ],"rows":[
>> {"c": [{ "v": "ABERTO"},{"v":63} ]},
>> {"c": [{ "v": "ASSOCIADO A AVARIA PAI"},{"v":9} ]},
>> {"c": [{ "v": "AVARIA PAI COM EM"},{"v":3} ]},
>> {"c": [{ "v": "Resolvido"},{"v":55} ]}
>> ]}
>>
>> And i was thinking that perhaps the json file must be outputed this way ?
>> How can i achieve that ?
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Visualization API" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to
> [email protected].
> Visit this group at
> http://groups.google.com/group/google-visualization-api.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/d/optout.