4

I'm using jQuery Flot Chart and trying to configure my tool tips per my liking.

The chart I am creating is a stacked chart and has two overlaying charts with 12 points or markers on each line graph.

Each point indicates a month going back a year for each graph.

enter image description here

Probably easier to describe in picture. As noted in the legend light Blue is the previous 12 months, where as Yellow is the 12 months before that.

Anyways, I am initiating my own custom variable yearrange in the plot instance like so

$.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)"
        }]

And then using this tooltip function to overlay a tooltip on each point

$.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;
        }
    });
};

So--

I have two variables that I'm looking manipulate within this tooltip function to give me a desired output.

var currmonth = months[x-  1];

Which outputs the X-Axis label over which I am hovering (for example, Nov)

var yearrange = item.series.yearrange;

Which outputs from my custom variable insertion in flot (for example, Jul-2017 - Jun-2018)

Likewise, for example, on Current Year, var currmonth = Jul and var yearrange = Jul-2018 - Jun-2019

My question is how can I create a variable outputs the only Month & Year combination possible given the variables I'm working with.

Say my two variables are Nov and Jul-2017 - Jun-2018. The output I'm looking to get is the only November that is possible within those month ranges -- November 2017.

Likewise if I have something for the first year in the current month, variable Jul and variable Jul-2018 - Jun-2019-- the only July that will fit in this span would be an output of July 2018.

Brian Bruman
  • 883
  • 12
  • 28
  • 1
    What if you have Nov and Jul-2017 - Jun-2019 – mplungjan Jun 26 '19 at 05:15
  • 1
    That doesn't happen. doing a `console.log(item)` inside my tooltip function the data brought back is only relevant to which line I am interacting with. Therefore I will only ever get back the Month (as shown in the X axis), and either `"Jul-2017 - Jun-2018"` OR `"Jul-2018 - Jun-2019"`... never both or a combination of – Brian Bruman Jun 26 '19 at 05:20
  • 1
    I meant a range of 2 years – mplungjan Jun 26 '19 at 05:22
  • 1
    Well, right, that would be more complicated. I can see your point if I wish to extend it further, but for all intents and purposes of this endeavor it's a 12-month span with only one month fit for the two variables – Brian Bruman Jun 26 '19 at 05:28

1 Answers1

3

You can start with this - I updated it so you do not have to use moment.js to get the full monthname:

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)
)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 1
    Excellent, thank you! Output works as I'd hoped. Only change I made was to take out the `"-"` in the return and used `moment.js` to convert it to the full month and year - `var getmonthyear = getMonthYear(currmonth, yearrange);`, and then `var monthyear = moment(getmonthyear, 'MMM Y').format('MMMM Y')`. Brings back full Months July 2017 to June 2019 depending on which point I'm hovering on the graph. Many thanks. – Brian Bruman Jun 26 '19 at 11:58
  • 2
    Updated to NOT to have to use moment – mplungjan Jun 26 '19 at 12:04
  • Just tested - thank you! I was more or less using moment as quick-fix as I work though this, really appreciate the update. – Brian Bruman Jun 26 '19 at 15:27