使用一个ajax调用填充三个高图图形

时间:2016-12-30 16:42:07

标签: ajax highcharts

我想弄清楚如何将3 ajax调用合并为1但到目前为止我没有找到正确的方法来做到这一点。我有以下三个高图:

chart1 = new Highcharts.Chart({
                chart: {
                    height: 155,
                    renderTo: 'chart1_div',
                    defaultSeriesType: 'spline',
                    events: {
                        load: GetData1(date)
                    }
                },
                plotOptions: {
                    series: {
                        fillOpacity: 0.1
                    }
                },
                xAxis: {
                    categories: c,
                    labels: {
                        staggerLines: 2
                    }
                },
                yAxis: {
                    gridLineColor: "#ffffcc",
                    title: {
                        text: ''
                    },
                    labels: {
                        x: 15,
                        y: 15,
                        style: {
                            color: "#999999",
                            //fontWeight: "bold",
                            fontSize: "10px"
                        }
                    },
                },
                series: [{
                    name: 'Chart1',
                    color: "#6bd9ec", //37f312
                    data: chart1_data
                }]
            });


chart2 = new Highcharts.Chart({
                    chart: {
                        height: 155,
                        renderTo: 'chart2_div',
                        defaultSeriesType: 'spline',
                        events: {
                            load: GetData2(date)
                        }
                    },
                    plotOptions: {
                        series: {
                            fillOpacity: 0.1
                        }
                    },
                    xAxis: {
                        categories: c,
                        labels: {
                            staggerLines: 2
                        }
                    },
                    yAxis: {
                        gridLineColor: "#ffffcc",
                        title: {
                            text: ''
                        },
                        labels: {
                            x: 15,
                            y: 15,
                            style: {
                                color: "#999999",
                                //fontWeight: "bold",
                                fontSize: "10px"
                            }
                        },
                    },
                    series: [{
                        name: 'Chart2',
                        color: "#6bd9ec", //37f312
                        data: chart2_data
                    }]
                });


chart3 = new Highcharts.Chart({
                        chart: {
                            height: 155,
                            renderTo: 'chart3_div',
                            defaultSeriesType: 'spline',
                            events: {
                                load: GetData3(date)
                            }
                        },
                        plotOptions: {
                            series: {
                                fillOpacity: 0.1
                            }
                        },
                        xAxis: {
                            categories: c,
                            labels: {
                                staggerLines: 2
                            }
                        },
                        yAxis: {
                            gridLineColor: "#ffffcc",
                            title: {
                                text: ''
                            },
                            labels: {
                                x: 15,
                                y: 15,
                                style: {
                                    color: "#999999",
                                    //fontWeight: "bold",
                                    fontSize: "10px"
                                }
                            },
                        },
                        series: [{
                            name: 'Chart3',
                            color: "#6bd9ec", //37f312
                            data: chart3_data
                        }]
                    });

目前我正在进行三种不同的ajax调用:

function GetData1(date) {
            $.ajax({
                url: '/GetAjaxCall',
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ date: date }),
                success: function (f) {
                    var cat= [];
                    var ser= [];
                    $.each(f.d, function (i, e) {
                        cat.push(e.date);
                        ser.push(parseInt(e.val1));
                    });
                    chart1_div.xAxis[0].setCategories(cat);
                    chart1_div.series[0].setData(ser);
                },
                error: function (e) {
                    alert(e.statusText);
                },
                cache: false
            });
        }

function GetData2(date) {
                $.ajax({
                    url: '/GetAjaxCall',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({ date: date }),
                    success: function (f) {
                        var cat= [];
                        var ser= [];
                        $.each(f.d, function (i, e) {
                            cat.push(e.date);
                            ser.push(parseInt(e.val2));
                        });
                        chart2_div.xAxis[0].setCategories(cat);
                        chart2_div.series[0].setData(ser);
                    },
                    error: function (e) {
                        alert(e.statusText);
                    },
                    cache: false
                });
            }

function GetData3(date) {
                $.ajax({
                    url: '/GetAjaxCall',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({ date: date }),
                    success: function (f) {
                        var cat= [];
                        var ser= [];
                        $.each(f.d, function (i, e) {
                            cat.push(e.date);
                            ser.push(parseInt(e.val3));
                        });
                        chart3_div.xAxis[0].setCategories(cat);
                        chart3_div.series[0].setData(ser);
                    },
                    error: function (e) {
                        alert(e.statusText);
                    },
                    cache: false
                });
            }

正如您所看到的,我现在正在进行3次相同的ajax调用,以便能够以不同方式填充3个图表。有谁知道我怎么能生成完全相同的3个图表但只能进行1个ajax调用?非常感谢

类似的东西:

function GetDataAll(date) {
                $.ajax({
                    url: '/GetAjaxCall',
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({ date: date }),
                    success: function (f) {
                        var cat1= [];
                        var ser1= [];
                        var cat2= [];
                        var ser2= [];
                        var cat3= [];
                        var ser3= [];
                        $.each(f.d, function (i, e) {
                            cat1.push(e.date);
                            ser1.push(parseInt(e.val1));
                        });
                        chart1_div.xAxis[0].setCategories(cat1);
                        chart1_div.series[0].setData(ser1);
                        chart2_div.xAxis[0].setCategories(cat2);
                        chart2_div.series[0].setData(ser2);
                        chart3_div.xAxis[0].setCategories(cat3);
                        chart3_div.series[0].setData(ser3);
                    },
                    error: function (e) {
                        alert(e.statusText);
                    },
                    cache: false
                });
            }

在加载事件下,我可以为3个图表调用GetData1(日期),但这样我将按3次ajax调用。我可能以错误的方式做这件事,必须是一种更有效的方式。我会很感激任何想法,非常感谢你

已编辑 - 基于@WERGELD帮助的最新更新

var chart1_options = { 
    chart: {
           renderTo: 'chart1_div',
    xAxis: {
           categories: []
    },
    series: [{
           name: 'Chart1',
           color: "#6bd9ec"
           }]
    };

var chart2_options = { 
    chart: {
           renderTo: 'chart2_div',
    xAxis: {
           categories: []
    },
    series: [{
           name: 'Chart2',
           color: "#6bd9ec"
           }]
    };

var chart3_options = { 
    chart: {
           renderTo: 'chart3_div',
    xAxis: {
           categories: []
    },
    series: [{
           name: 'Chart3',
           color: "#6bd9ec"
           }]
    };

function GetDataAll(date) {
$ajax({..

                        chart1_options.xAxis.categories = cat1;
                        chart1_options.series[0].data = ser1;

                        chart2_options.xAxis.categories = cat2;
                        chart2_options.series[0].data = ser2;

                        chart3_options.xAxis.categories = cat3;
                        chart3_options.series[0].data = ser3;

                        var chart1 = new Highcharts.Chart(Highcharts.merge(options, chart1_options));
                        var chart2 = new Highcharts.Chart(Highcharts.merge(options, chart2_options));
                        var chart3 = new Highcharts.Chart(Highcharts.merge(options, chart3_options));

});
}

1 个答案:

答案 0 :(得分:1)

这是一些伪造的代码,可以帮助您实现目标。您需要做的是设置一个图表选项对象,其中包含图表中的所有常量项目:

  var options = {
    chart: {
      height: 155,
      defaultSeriesType: 'spline'
    },
    plotOptions: {
      series: {
        fillOpacity: 0.1
      }
    },
    xAxis: {
      categories: c,
      labels: {
        staggerLines: 2
      }
    },
    yAxis: {
      gridLineColor: "#ffffcc",
      title: {
        text: ''
      },
      labels: {
        x: 15,
        y: 15,
        style: {
          color: "#999999",
          //fontWeight: "bold",
          fontSize: "10px"
        }
      },
    }
  };

接下来,您执行ajax调用并分配系列的数据和标题(以及任何其他项目),并为您要创建的每个图表创建一个对象:

var chartOptions1 = {
    chart: { renderTo: 'container1' },
    series: []
};

var chartOptions2 = {
    chart: { renderTo: 'container1' },
    series: []
};

接下来,您执行代码以获取您希望为每个图表提供的图表数据/名称/等,我将不会在此处进行说明。下一步是从合并选项创建图表:

var chart1 = new Highcharts.Chart(Highcharts.merge(options, chartOptions1));
var chart2 = new Highcharts.Chart(Highcharts.merge(options, chartOptions2));

这应该这样做。再次,它是伪造的,但你明白了。全球期权+ merge是你的朋友。完整的伪代码here