我正在使用 JQPlot 饼图。我正在尝试附加“jqplotDataClick”事件以深入了解图表。它工作正常,它向下钻取点击的图表,但我还需要根据向下钻取过滤其他图表数据。所以我再次获取数据并将新数据重新绑定到其他图表。在我单击向下钻取时将数据绑定到其他图表后,它会多次触发它。
我正在使用 Safari 浏览器
以下是我的代码步骤:
1) 在 VS 2010 & MVC 3 和 JQPlot 库中创建项目 2) 在 Home/Index.cshtml 中添加视图代码 3) 在 Home/HomeController.cs 中添加控制器代码并运行解决方案 4) 单击第一个图表,它将在哪里显示警报您单击并向下钻取并显示过滤后的数据 5) 再次单击同一个图表,它将显示新旧数据的警报 6) 再次单击同一个图表,它将增加相同图表的事件
看法
"
<p id="p_chartCustomer1" />
$(document).ready(function () {
var urlGetChartsData = rootPath + 'Home/GetData';
var filterParam = new Array();
jQuery.ajaxSettings.traditional = true;
filterParam.push('1');
$.ajax(urlGetChartsData,
{
data: {
filter: '', filterParam: filterParam
},
success: function (data) {
if (data.length > 0) {
var dispdata = [];
for (var i = 0; i < (data.length); i++) {
dispdata.push([data[i].Textt, data[i].val]);
}
}
LoadData(dispdata, 'p_chartCustomer1', 'chart2');
}
});
});
function fetchData(filterParam, chartName) {
var dispdata = [];
var urlGetChartsData = rootPath + 'Home/GetData';
$.ajax(urlGetChartsData,
{
data: {
filter: filterParam
},
success: function (data) {
if (data.length > 0) {
for (var i = 0; i < (data.length); i++) {
dispdata.push([data[i].Textt, data[i].val]);
}
}
LoadData(dispdata, chartName, filterParam);
}
});
}
function LoadData(dispdata, chartName, filterText) {
var plot2 = jQuery.jqplot(chartName, [dispdata],
{
captureRightClick: true,
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, diameter: 200
}
},
legend: { show: true, location: 'e' }
});
$('#' + chartName).bind('jqplotDataClick', function (ev, seriesIndex, pointIndex, data) {
alert('chartName:' + chartName + ' Data - ' + plot2.series[seriesIndex].data[pointIndex]);
fetchData(plot2.series[seriesIndex].data[pointIndex].toString(), chartName);
});
}
"
控制器
public ActionResult GetData(string filter, List<string> filterParam)
{
List<TestData> testList = new List<TestData>();
if (filter.Length > 0)
{
testList.Add(new TestData(filter.Split(',')[0], Convert.ToInt32(filter.Split(',')[1])));
testList.Add(new TestData(filter.Split(',')[0], Convert.ToInt32(filter.Split(',')[1])));
}
else
{
testList.Add(new TestData("Data1", 20));
testList.Add(new TestData("Data2", 50));
testList.Add(new TestData("Data3", 12));
testList.Add(new TestData("Data4", 20));
testList.Add(new TestData("Data5", 89));
}
return Json(testList, JsonRequestBehavior.AllowGet);
}
[Serializable]
public class TestData
{
public TestData(string text, int val)
{
this.Textt = text;
this.val = val;
}
public string Textt { get; set; }
public int val { get; set; }
}