Ad Hoc Javascript库返回undefined

时间:2016-01-21 19:35:55

标签: javascript data-structures google-visualization

正如标题中所述,我试图从头开始创建一个Javascript库,它必须调整使用Google Chart创建的图表的输出。

要更好地解释,而不是这样做:

var options = {
      title: 'Top Consuming Nations - Thousand barrels daily',

      hAxis: {title: 'Year'},
      width: 1050, height : 400,

      vAxes: [
       {title: 'Top Countries', titleTextStyle: {color: '#FF0000'},  maxValue: max},  // Left axis maxValue: 60000
       {title: 'Total World', titleTextStyle: {color: '#FF0000'},  maxValue: tot}  // Right 
      ],
      series:{
        6: {targetAxisIndex: 1}
      },
      legend: { position: 'top', alignment: 'start' },
    };

    var chart = new google.visualization.LineChart(document.getElementById(chartDiv));

    chart.draw(data, options);

我想用我创建的库(skewedChart)做这样的事情:

 var options = {
      title: 'Top Consuming Nations - Thousand barrels daily',

      hAxis: {title: 'Year'},
      width: 1050, height : 400,

      vAxes: [
       {title: 'Top Countries', titleTextStyle: {color: '#FF0000'},  maxValue: max},  // Left axis maxValue: 60000
       {title: 'Total World', titleTextStyle: {color: '#FF0000'},  maxValue: tot}  // Right 
      ],
      series:{
        6: {targetAxisIndex: 1}
      },
      legend: { position: 'top', alignment: 'start' },
      skew: 1
    };

var chart = skewedChart.visualization.LineChart(document.getElementById(chartDiv)); 

chart.draw(data, options); 

这是我的库的代码,包含在文件skewedChart.js中:

    var skewedChart = {};
    skewedChart.visualization = {};

    skewedChart.visualization.LineChart = function(location) {
        this.chart = new google.visualization.LineChart(location) ;
        this.draw = function(data,option) {
            option.height = this.skewHeight(option.width, option.skew) ;
            option.vAxes[0].maxValue = this.skewMax(option.vAxes[0],option.skew) ;

            this.chart.draw(data,option);
        }
        this.skewHeight= function(width,skewFactor) {

        }
        this.skewMax= function(series, skewFactor) {

        }
    };

问题是图表返回" undefined",为什么?我怎样才能做得更好?

1 个答案:

答案 0 :(得分:1)

如果你没有从函数调用中返回任何内容,它将返回undefined,这就是你所看到的。您需要在函数末尾添加return this;