从月份变量和 Month-Year 范围返回月份和年份

Returning Month and Year from Month Variable and Month-Year Range

我正在使用 jQuery 图表并尝试根据自己的喜好配置我的工具提示。

我正在创建的图表是一个堆叠图表,有两个重叠图表,每个折线图上有 12 个点或标记。

对于每个图表,每个点表示一年前的一个月。

可能在图片中更容易描述。如图例中所述,浅蓝色是前 12 个月,而黄色是之前的 12 个月。

无论如何,我正在像这样在绘图实例中启动我自己的自定义变量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-Axis 标签(例如,Nov

var yearrange = item.series.yearrange;

我在 flot 中的自定义变量插入输出(例如,Jul-2017 - Jun-2018

同样,例如,在当年,var currmonth = Julvar yearrange = Jul-2018 - Jun-2019

我的问题是如何创建一个变量,输出唯一可能的月和年组合,给定我正在使用的变量。

假设我的两个变量是 NovJul-2017 - Jun-2018。我希望获得的输出是那些月份范围内唯一可能的 11 月 -- November 2017.

同样,如果我在当前月份的第一年有一些变量 Jul 和变量 Jul-2018 - Jun-2019-- 唯一适合此跨度的 7 月将是 [= 的输出25=].

你可以从这个开始 - 我更新了它所以你不必使用 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)
)