JSON
と の両方に非常に新しいjQuery
ですが、これが私がやろうとしていることです -レポートが生成されると同時にJSON
( を使用して)多数のグラフを作成するために、より大きなレポートモデルの一部として文字列をWebページに返していますjqPlot
.
これらのチャートを練習するために、私は次のチュートリアルを使用していました -チュートリアル
これがjQuery
このチュートリアルの です -
jQuery(document).ready(function() {
urlDataJSON = '/Graph/HearthRateDataJSON';
$.getJSON(urlDataJSON, "", function(data) {
var dataLines = [];
var dataLabels = "";
$.each(data, function(entryindex, entry) {
dataLines.push(entry['Serie']);
dataLabels = dataLabels + entry['Name'];
});
Plot(dataLines, dataLabels);
});
});
function Plot(dataLines, dataLabels) {
var line1 = "{ label: 'line1.0' }";
options = {
legend: { show: true },
title: 'Heart rate overview',
axesDefaults: { pad: 1 },
seriesDefaults: { showMarker: false, trendline: { show: false }, lineWidth: 3 },
axes: {
yaxis: { min: 0, autoscale: true, label: 'HR[bpm]', labelRenderer: $.jqplot.CanvasAxisLabelRenderer },
xaxis: { autoscale: true, label: 'Time', labelRenderer: $.jqplot.CanvasAxisLabelRenderer }
}
};
//Data from database is already an array!
plot = $.jqplot('chartdiv', dataLines, options);
plot.redraw(); // gets rid of previous axis tick markers
}
データを取得するためにJSON
、このチュートリアルでは getJson() メソッドを使用してリンクを指定します。ただし、JSON
文字列を渡す準備ができています。たとえば、jQuery
[
{"Name":"Patient","Serie":[[1,4],[2,25],[3,7],[4,14],[5,13]]},
{"Name":"Test","Serie":[[1,13],[2,5],[3,7],[4,20],[5,17]]}
]
例ではjqPlot
、次のようにハードコーディングされたデータを渡します。
$(document).ready(function(){
var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]);
});
ただし、自動生成されJSON
た文字列には中括弧などが含まれjQuery
ています。getJson メソッドを使用せずにこれを渡す方法はありますか?
EDIT-円グラフ Jquery コード
$(document).ready(function () {
var data4 = '[{ "Label": "Car", "Value": 9 },{ "Label": "Bus", "Value": 2 },{ "Label": "Train", "Value": 7 },{ "Label": "Plane", "Value": 8 },{ "Label": "Boat", "Value": 4 }]';
var data = $.parseJSON(data4);
var plot1 = jQuery.jqplot('pieChart', [data],
{
seriesDefaults: {
// Make this a pie chart.
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
// Put data labels on the pie slices.
// By default, labels show the percentage of the slice.
showDataLabels: true
}
},
legend: { show: true, location: 'e' }
}
);
});