2017-09-05 67 views
1

我使用我们的图表/图表(使用chart.js)创建了一些功能,这将允许用户选择他们想要查看的时间缩放比例。我创造了一些可行的东西;但由于我们有4个不同的图表,因此我不想通过重复使用此代码来创建巨大而低效的文档,然后找到代替已更改的部分的文档。相反,我想创建一次创建的模板对象,然后用该图形自己的属性替换模板/通用变量名称。创建模板并使用变量替换JS

这里是我创建的代码的精简版:

 $("#engagementDaily").hide(); 
     $("#engagementWeekly").hide(); 
     $("#engagementMonthly").hide(); 
     $("#engagementYearly").hide(); 
     $("#engagementDay").click(function(){ 
      engGraph.options.scales.xAxes[0].time.unit = 'day' 
      $(this).addClass('active'); 
      nullXAxes(); 
      $("#engagementDaily").fadeIn(); 
      $("#engagementWeekly").hide(); 
      $("#engagementMonthly").hide(); 
      $("#engagementYearly").hide(); 
      engGraph.update(); 
     }); 
     $("#engagementWeek").click(function(){ 
      engGraph.options.scales.xAxes[0].time.unit = 'week' 
      $(this).addClass('active'); 
      nullXAxes(); 
      $("#engagementDaily").hide(); 
      $("#engagementWeekly").fadeIn(); 
      $("#engagementMonthly").hide(); 
      $("#engagementYearly").hide(); 
      engGraph.update(); 
     }); 
     $("#engagementMonth").click(function(){ 
      engGraph.options.scales.xAxes[0].time.unit = 'month' 
      $(this).addClass('active'); 
      nullXAxes(); 
      $("#engagementDaily").hide(); 
      $("#engagementWeekly").hide(); 
      $("#engagementMonthly").fadeIn(); 
      $("#engagementYearly").hide();     
      engGraph.update(); 
     }); 
     $("#engagementYear").click(function(){ 
      engGraph.options.scales.xAxes[0].time.unit = 'year' 
      $(this).addClass('active'); 
      nullXAxes(); 
      $("#engagementDaily").hide(); 
      $("#engagementWeekly").hide(); 
      $("#engagementMonthly").hide(); 
      $("#engagementYearly").fadeIn(); 
      engGraph.update(); 
     }); 
     $("#engagementAutoDay").click(function(){ 
      engGraph.options.scales.xAxes[0].time.min = null; 
      engGraph.options.scales.xAxes[0].time.max = null; 
      $(this).addClass('active'); 
      engGraph.update(); 
     }); 
     $("#engagementAutoWeek").click(function(){ 
      engGraph.options.scales.xAxes[0].time.min = null; 
      engGraph.options.scales.xAxes[0].time.max = null; 
      $(this).addClass('active'); 
      engGraph.update(); 
     }); 
     $("#engagementAutoMonth").click(function(){ 
      engGraph.options.scales.xAxes[0].time.min = null; 
      engGraph.options.scales.xAxes[0].time.max = null; 
      $(this).addClass('active'); 
      engGraph.update(); 
     });  
     $("#engagementAutoYear").click(function(){ 
      engGraph.options.scales.xAxes[0].time.min = null; 
      engGraph.options.scales.xAxes[0].time.max = null; 
      $(this).addClass('active'); 
      engGraph.update(); 
     });      
     $("#engagement7Day").click(function(){ 
      engGraph.options.scales.xAxes[0].time.min = sevenDays; 
      engGraph.options.scales.xAxes[0].time.max = latestDate; 
      $(this).addClass('active'); 
      engGraph.update(); 
     }); 
     //Et Cetera, et cetera 

理想情况下,我想这变成可在脚本的根范围内生活在一个对象的模板,我可以代替重复与图表/图形的名称元素 - 是这样的:

var graphTemplate = function(currentGraph, currentGraphShort) 

 $("#" + currentGraph + "Daily").hide(); 
     ///// 
     [currentGraphShort].options.scales.xAxes[0].time.unit = 'day' 

但我的实验没有奏效。我已经看过使用模板文字;但这些似乎更多地用于字符串(我可以说,以及从Iv'e使用的)。我试图创建一个对象,如[currentGraphShort]["options"]["scales"]["xAxes[0]"]["time"]["unit"] = 'day',这也不起作用...

也许解决方案是非常简单的东西,我俯瞰,但我会喜欢与这一个手!

谢谢=)

编辑:

我尝试添加我跑在全球根范围的功能确切的代码,和图形的功能中运行它;但之后它无法找到我的图形(engGraph) - 说它是未定义的。也许这也是我失踪的范围的一个因素?

任何帮助,非常感谢!

回答

0

最后,我创建了看起来是这样的一个全局函数:

function graphInit(graphName, currentGraph, latestDate) { 
    //Date formatting 
    var sevenDays = moment(latestDate).subtract(7, 'days').format("YYYY-MM-DD HH:mm") 
    var thirtyDays = moment(latestDate).subtract(30, 'days').format("YYYY-MM-DD HH:mm") 
    var oneWeek = moment(latestDate).subtract(1, 'weeks').format("YYYY-MM-DD HH:mm") 
    var twoWeeks = moment(latestDate).subtract(2, 'weeks').format("YYYY-MM-DD HH:mm") 
    var fourWeeks = moment(latestDate).subtract(4, 'weeks').format("YYYY-MM-DD HH:mm") 
    var threeMonths = moment(latestDate).subtract(3, 'months').format("YYYY-MM-DD HH:mm") 
    var sixMonths = moment(latestDate).subtract(6, 'months').format("YYYY-MM-DD HH:mm") 
    var oneYear = moment(latestDate).subtract(1, 'years').format("YYYY-MM-DD HH:mm") 
    var twoYears = moment(latestDate).subtract(2, 'years').format("YYYY-MM-DD HH:mm") 
     //nullify the xAxes scale (Used to default to auto when switching scales) 
    function nullXAxes() { 
     currentGraph.options.scales.xAxes[0].time.min = null; 
     currentGraph.options.scales.xAxes[0].time.max = null; 
     $(this).addClass('active'); 
     currentGraph.update(); 
    } 
    //reset the xAxes scale to specified min/max 
    function xAxesScale(min, max) { 
     currentGraph.options.scales.xAxes[0].time.min = min; 
     currentGraph.options.scales.xAxes[0].time.max = max; 
     $(this).addClass('active'); 
     currentGraph.update(); 
    } 

    //init scaling 
    $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide(); 
    //left-side click parameters 
    $("#" + graphName + "Auto").click(function() { 
     currentGraph.options.scales.xAxes[0].time.unit = null; 
     $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide(); 
     nullXAxes(); 
    }); 
    $("#" + graphName + "Day").click(function() { 
     currentGraph.options.scales.xAxes[0].time.unit = 'day' 
     $("#" + graphName + "Weekly,#" + graphName + "Monthly,#" + graphName + "Yearly").hide() 
     $("#" + graphName + "Daily").fadeIn(); 
     nullXAxes(); 
    }); 
    $("#" + graphName + "Week").click(function() { 
     currentGraph.options.scales.xAxes[0].time.unit = 'week' 
     $("#" + graphName + "Daily,#" + graphName + "Monthly,#" + graphName + "Yearly").hide() 
     $("#" + graphName + "Weekly").fadeIn(); 
     nullXAxes(); 
    }); 
    $("#" + graphName + "Month").click(function() { 
     currentGraph.options.scales.xAxes[0].time.unit = 'month' 
     $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Yearly").hide() 
     $("#" + graphName + "Monthly").fadeIn(); 
     nullXAxes(); 
    }); 
    $("#" + graphName + "Year").click(function() { 
     currentGraph.options.scales.xAxes[0].time.unit = 'year' 
     $("#" + graphName + "Daily,#" + graphName + "Weekly,#" + graphName + "Monthly").hide() 
     $("#" + graphName + "Yearly").fadeIn(); 
     nullXAxes(); 
    }); 
    //right-side click parameters 
    $("#" + graphName + "AutoDay,#" + graphName + "AutoWeek,#" + graphName + "AutoMonth,#" + graphName + "AutoYear").click(function() { 
     nullXAxes() 
    }); 
    $("#" + graphName + "7Day").click(function() { 
     xAxesScale(sevenDays, latestDate) 
    }); 
    $("#" + graphName + "30Day").click(function() { 
     xAxesScale(thirtyDays, latestDate) 
    }); 
    $("#" + graphName + "1Week").click(function() { 
     xAxesScale(oneWeek, latestDate) 
    }); 
    $("#" + graphName + "2Week").click(function() { 
     xAxesScale(twoWeeks, latestDate) 
    }); 
    $("#" + graphName + "4Week").click(function() { 
     xAxesScale(fourWeeks, latestDate) 
    }); 
    $("#" + graphName + "3Month").click(function() { 
     xAxesScale(threeMonths, latestDate) 
    }); 
    $("#" + graphName + "6Month").click(function() { 
     xAxesScale(sixMonths, latestDate) 
    }); 
    $("#" + graphName + "1Year").click(function() { 
     xAxesScale(oneYear, latestDate) 
    }); 
    $("#" + graphName + "2Year").click(function() { 
     xAxesScale(twoYears, latestDate) 
    }); 
}; 

,并确保调用graphInit(whateverGraph,currentGraph,latestDate)的功能,我们已经创建了图形之后,并分配它是一个变量(currentGraph)。这工作,至少保存了1/4的代码,并使其更加模块化。