我正在使用jQuery图,并尝试根据我的喜好配置我的工具提示。
我正在创建的图表是一个堆叠图,它有两个重叠图表,每个折线图上有12个点或标记。
每个点表示每个图表的一个月和一年前。

可能更容易用图片来描述。如图例所示,浅蓝色是前12个月,而黄色是前12个月。
无论如何,我在plot实例中初始化自己的自定义变量yearrange,如下所示
$.plot($(".flot-line"), [{
label: "Previous Year",
yearrange: "Jul-2017 - Jun-2018",
data: previousyear,
color: "rgb(237,194,64)"
}, {
label: "Current Year",
yearrange: "Jul-2018 - Jun-2019",
data: currentyear,
color: "rgb(175,216,248)"
}]然后使用此工具提示函数在每个点上覆盖工具提示
$.fn.UseTooltip = function () {
$(this).bind("plothover", function (event, pos, item) {
if (item) {
if (previousPoint != item.dataIndex) {
previousPoint = item.dataIndex;
$("#tooltip").remove();
var x = item.datapoint[0];
var y = item.datapoint[1];
var currmonth = months[x- 1];
var yearrange = item.series.yearrange;
showTooltip(item.pageX, item.pageY,
"<strong> " + y + "</strong> (" + item.series.label + ")");
}
}
else {
$("#tooltip").remove();
previousPoint = null;
}
});
};所以--
我有两个变量,我希望在这个工具提示函数中对它们进行操作,以获得所需的输出。
var currmonth = months[x- 1];它输出我悬停在其上的X轴标签(例如,Nov)
var yearrange = item.series.yearrange;在flot中插入自定义变量的输出(例如,Jul-2017 - Jun-2018)
同样,例如,在当前年份,var currmonth = Jul和var yearrange = Jul-2018 - Jun-2019
我的问题是,如何创建一个变量输出给定我正在处理的变量的唯一可能的月和年组合。
假设我的两个变量是Nov和Jul-2017 - Jun-2018。我希望得到的输出是在这些月份范围内唯一可能的11月-- November 2017。
同样,如果我有当前月份第一年的变量Jul和变量Jul-2018 - Jun-2019--唯一适合此跨度的7月将是July 2018的输出。
发布于 2019-06-26 13:20:03
你可以从这个开始-我更新了它,这样你就不需要使用moment.js来获取完整的月份名称:
const months = ",January,February,March,April,May,June,July,August,September,October,November,December".split(",")
const monthArr = months.map((month) => month.substring(0,3));
const pad = (num) => ("0"+num).slice(-2);
let getMonthYear = (curMonth,range) => {
// Note: all string manipulation
const curMonthStr = pad(monthArr.indexOf(curMonth));
const [start,end] = range.split(" - ").map(
d => d.replace(/(\w+)-(\d+)/,(m,a,b) => b+pad(monthArr.indexOf(a)))
);
const curYearMonthStart = start.slice(0,-2)+curMonthStr;
const curYearMonthEnd = end.slice(0, -2)+curMonthStr;
let curYearMonth = curYearMonthStart;
if (curYearMonth<start) curYearMonth = curYearMonthEnd; // which one do we take?
return curYearMonth >= start && curYearMonth <=end ? months[monthArr.indexOf(curMonth)] + " " + curYearMonth.slice(0,-2) : "";
}
const range = "Jun-2018 - Jun-2019";
let curMonth = "Nov";
console.log(
getMonthYear(curMonth,range)
)
curMonth = "Jul";
console.log(
getMonthYear(curMonth,range)
)
https://stackoverflow.com/questions/56765506
复制相似问题