gpt4 book ai didi

jquery - JQPlot饼图 "jqplotDataClick"事件多次触发

转载 作者:行者123 更新时间:2023-12-01 07:16:37 25 4
gpt4 key购买 nike

我正在使用 JQPlot 饼图。我正在尝试附加“jqplotDataClick”事件来深入分析图表。它工作正常,它向下钻取单击的图表,但我还需要根据向下钻取过滤其他图表数据。因此,我再次获取数据并将新数据重新绑定(bind)到其他图表。将数据绑定(bind)到其他图表后,当我单击“向下钻取”时,它会多次触发它。

我正在使用 Safari 浏览器

下面是我的代码步骤:

1) 在 VS 2010 & MVC 3 和 JQPlot 库中创建项目2)在Home/Index.cshtml中添加查看代码3)在Home/HomeController.cs中添加 Controller 代码并运行解决方案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);
});
}

"

Controller

    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; }
}

最佳答案

在添加绑定(bind)函数之前,请取消绑定(bind)相应的事件:

$('#' + chartName).unbind('jqplotDataClick');
$('#' + 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);
});

关于jquery - JQPlot饼图 "jqplotDataClick"事件多次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18213702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com