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

时间:2013-08-13 15:51:39

标签: jquery asp.net-mvc-3 jquery-mobile jqplot

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

我正在使用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);
    });
}

控制器

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

1 个答案:

答案 0 :(得分:4)

在添加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);
});
相关问题