最后,我创建了一个全局函数,看起来像这样:
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)
});
};