Javascript 重用函数将数据加载到Highchart中

Javascript 重用函数将数据加载到Highchart中,javascript,jquery,highcharts,Javascript,Jquery,Highcharts,我有一个Highchart对象和一个加载数据的函数: graficaPropuestas = new Highcharts.Chart({ chart: { height: 300, renderTo: "graficaPropuestas", plotBackgroundColor: null, plotBorderWidth: null, plotShado

我有一个Highchart对象和一个加载数据的函数:

graficaPropuestas = new Highcharts.Chart({
        chart: {
            height: 300,
            renderTo: "graficaPropuestas",
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            events: {
                load: recogerDatosInforme('propuesta', $(this))
            }
        }
 })
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Licencia/recogerDatosUsuariosSistema?tipo' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Informes/recogerDatosInforme?tipo=' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
以及加载数据的功能:

graficaPropuestas = new Highcharts.Chart({
        chart: {
            height: 300,
            renderTo: "graficaPropuestas",
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            events: {
                load: recogerDatosInforme('propuesta', $(this))
            }
        }
 })
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Licencia/recogerDatosUsuariosSistema?tipo' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Informes/recogerDatosInforme?tipo=' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
我想把graficapropurestas对象传递给这个函数,这样我就可以重用同一个函数来加载更多的图形,但是我不能让它工作。如果我直接将函数放在success:method上的graficapropestas对象中,它会工作得很好


任何帮助都将不胜感激。

如果您只有一个
graficaPropuestas
的实例,则更好、更高效的方法是使用该解决方案:

var graficaPropuestas = new Highcharts.Chart( ... 

function recogerDatosInforme(tipo) {
        $.ajax({
            url: '/Licencia/recogerDatosUsuariosSistema?tipo' + tipo,
            datatype: "json",
            success: function(data) {
                graficaPropuestas.series[0].setData(data);
            },
            cache: false
        });
    }

我是这样解决的:

获取数据的函数:

graficaPropuestas = new Highcharts.Chart({
        chart: {
            height: 300,
            renderTo: "graficaPropuestas",
            plotBackgroundColor: null,
            plotBorderWidth: null,
            plotShadow: false,
            events: {
                load: recogerDatosInforme('propuesta', $(this))
            }
        }
 })
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Licencia/recogerDatosUsuariosSistema?tipo' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
function recogerDatosInforme(tipo, gr) {
        $.ajax({
            url: '/Informes/recogerDatosInforme?tipo=' + tipo,
            datatype: "json",
            success: function(data) {
                gr.series[0].setData(data);
            },
            cache: false
        });
    }
然后,我在init上创建了不带events方法的图表:

graficaPropuestas = new Highcharts.Chart({
    chart: {
        height: 300,
        renderTo: "graficaPropuestas",
        plotBackgroundColor: null,
        plotBorderWidth: null,
        plotShadow: false
    }
})

我以graph对象作为参数调用函数:

recogerDatosInforme('propuesta', graficaPropuestas)

因此,现在我可以制作更多的图表对象,并使用相同的函数来获取数据。

我真的不明白您希望如何在成功函数中使用
graficaPropuestas
对象?您好!我想使用graficaPropuestas对象,因为代码中有更多的charts对象,所以我只想使用一个函数从服务器获取数据。问题是这是否是将对象传递给函数的正确方法。我假设您在
gr
参数中传递
graficapropurestas
?是的。。。这应该是正确的方法吗?