Я пытаюсь дать объект JSON для отображения в диаграмме Google.
Я могу отобразить диаграмму Google, когда я вызываю в ajax файл json, но когда я пытаюсь получить данные из codebehind,я получаю ошибку.
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetData()
{
var chartData = new object[10+ 1];
int j = 0;
for (int i=1; i <= 10; i++)
{
chartData[j] = new object[] { i.ToString(), i*i, i};
j++;
}
//return chartData;
string json = JsonConvert.SerializeObject(chartData);
return json;
}
Строка Json, которую я здесь получаю, действительна в соответствии с форматером json. "[[\" 1 \», 1,1], [\ "2 \", 4,2], [\ "3 \", 9,3], [\ "4 \", 16,4],[\ "5 \", 25,5], [\ "6 \", 36,6], [\ "7 \", 49,7], [\ "8 \", 64,8], [\"9 \", 81,9], [\ "10 \", 100,10]] "
Итак, у меня есть строка, число, число в виде массива.
Мой вызов выглядиткак это (у меня это из https://www.encodedna.com/google-chart/create-line-charts-with-dynamic-json-data-using-google-charts.htm), и это работает нормально, если я использую файл json для данных, когда я пытаюсь использовать данные из кода, который за ним прыгает в ошибку:
<script>
// Visualization API with the 'corechart' package. url: "data.json",
google.charts.load('visualization', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawLineChart);
function drawLineChart() {
$.ajax({
url: "Chart.aspx/GetData",
dataType: "json",
type: "GET",
contentType: "application/json; charset=utf-8",
success: function (data) {
var arrSales = [['RejectReason', 'Count', 'Perc. (%)']]; // Define an array and assign columns for the chart.
// Loop through each data and populate the array.
$.each(data, function (index, value) {
arrSales.push([value.RejectReason, value.Count, value.Perc]);
});
// Set chart Options.
var options = {
title: 'Sorting',
curveType: 'function',
legend: { position: 'bottom', textStyle: { color: '#555', fontSize: 14 } } // You can position the legend on 'top' or at the 'bottom'.
};
// Create DataTable and add the array to it.
var figures = google.visualization.arrayToDataTable(arrSales)
// Define the chart type (LineChart) and the container (a DIV in our case).
var chart = new google.visualization.LineChart(document.getElementById('chart'));
chart.draw(figures, options); // Draw the chart with Options.
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('Got an Error');
}
});
}
Я хочу, чтобы код был показан на диаграмме Google. Позже я хочу использовать извлеченные и обогащенные данные из базы данных, которая будет отображаться, поэтому я не буду получать к ней доступ из файла json.