0

I use google line chart but I cant success to assing source to chart.

script

<script type="text/javascript">

// 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: '@Url.Action("AylikOkumalar", "Enerji")',
        dataType: "json",
        async: false
    }).responseText;

    var rows = new google.visualization.DataTable(jsonData);

    var data = new google.visualization.DataTable();
    data.addColumn('date', 'Date');
    data.addColumn('number', 'T1');
    data.addColumn('number', 'T2');
    data.addColumn('number', 'T3');

    data.addRows(rows);


    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.LineChart(document.getElementById('chart_div')).draw(data, { curveType: "function",
        width: 700, height: 400,
        vAxis: { maxValue: 10 }
    }
    );
}
</script>

action

public ActionResult AylikOkumalar()
{
   IEnumerable<TblSayacOkumalari> sayac_okumalari = entity.TblSayacOkumalari;

   var sonuc = sayac_okumalari.Select(x => new
   {
       okuma_tarihi = ((DateTime)x.okuma_tarihi).ToShortDateString(),
       T1 = (int)x.toplam_kullanim_T1,
       T2 = (int)x.toplam_kullanim_T2,
       T3 = (int)x.toplam_kullanim_T3
   });

   return Json(sonuc, JsonRequestBehavior.AllowGet);
}

json output

enter image description here

and script error:

Argument given to addRows must be either a number or an array.

I use it first time. So I dont know what should I do. How can use charts with mvc 3. there is no example enough.

Thanks.

4

1 回答 1

-1
data.addRows(rows);

'rows' 应该是一个数组 - 类似于:

data.addRows([
  [new Date(1977,2,28)], 1, 2, 3],
  [new Date(1977,2,28)], 1, 2, 3]
]);

或者您的 jsonData 可能是整个结构:

{
    "cols": [
        {"id": "", "label": "Date", "type": "date"},
        {"id": "", "label": "T1",   "type": "number"}
    ],
    "rows": [
        {"c":[{"v": "Apr 24th"}, {"v": 56000} ]},
        {"c":[{"v": "May 3rd" }, {"v": 68000} ]}
    ]
}
于 2012-07-19T23:05:45.003 回答