【问题标题】:Pass c3js chart options to function将 c3js 图表选项传递给函数
【发布时间】:2017-03-27 23:14:43
【问题描述】:

我有几个不同的图表,我通过 ajax 使用 c3js 渲染,我目前有很多重复的 ajax 代码我想减少。

每次成功调用 ajax 都会生成一个 c3js 图表,但具有不同的显示类型/选项。有没有办法让我生成一个通用的 c3js 并传入选项?

$.ajax({
    url: url,
    data: {'chart': chart, 'start': start, 'end': end},
    type: 'post',
    dataType: 'json',
    beforeSend: function () {
        //code
    },
    success: function (data) {
        if (data.success) {
            //code                      
            c3.generate({
                bindto: '#chart-data',
                data: {
                    columns: data.active,
                    type: 'area',
                    groups: [data.groups]
                },
                axis: {
                    x: {
                        type: 'category',
                        categories: data.span
                    },
                    y: {
                        label: 'Label'
                    }
                 }
            });
        } else {
            //code
        }
    }
}); 

使用各种图表数据类型、组、列、轴等重复多次。

我想要什么:

var chart_options = {bindto: '#chart-data', data:{columns.data.active} //...
function generateChart(param1, param2, param3, chart_options) {
    //do some stuff
    //ajax call from above
    // ....
    // on success:
    // c3.generate(chart_options)
}

但是,当我这样做时,因为 data.success 在函数内部,并且我从函数外部传递 columns: data.active ,我收到未定义 data[i] 列的 javascript 错误。

谢谢

【问题讨论】:

    标签: javascript jquery ajax d3.js c3.js


    【解决方案1】:
    var chart_options = {bindto: '#chart-data', {data: {type: "area"}} //...}
    function generateChart(param1, param2, param3) {
      //do some stuff
      //ajax call from above
      // ....
      // on success:
      // chart_options.data.colums = data.active;
      // chart_options.data.groups = [data.groups];
      // same for axes…
      // c3.generate(chart_options)
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 2013-02-05
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      相关资源
      最近更新 更多