Tooltip actions on Google Visualization Line Chart

Hello!

I have a Google API line graph with two lines and clickable tooltips. On the click, a hidden div is shown that displays a pie chart with further details about the point on the graph. This is working fine using a switch statement. However, I'm having difficulty getting unique onclick events for both line points. Currently, for the month of Jan 18, it's showing the same pie chart for both lines. Not sure what I can do to fix this and have each bullet display their respective chart.

You can check out my code here: https://jsfiddle.net/Error_423/g4gz279e/35/
I was using this as a reference: https://developers.google.com/chart/interactive/docs/customizing_tooltip_content#tooltip-actions

I've added comments in the JS and HTML to further explain what I'm trying to accomplish.

Any help would be greatly appreciated!
Annie KayAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zephyr_hex (Megan)DeveloperCommented:
One problem is here:
document.getElementById('container').style.display = "block";

Open in new window


You don't have any HTML elements with the id = "container".

I'll poke at it a bit further to see what I think should happen when See Pie Chart is clicked...
zephyr_hex (Megan)DeveloperCommented:
I think you need to determine which line is clicked so that you can display the appropriate pie chart.  You can get that info from the column property (selection[0].column)

column 1 is Sales Index (S-AVI)
column 2 is Employment Index (E-AVI)

I'm not entirely clear why you have your switch statement based on the row values, but I'll assume that's what you want to do.  Inside case 13, look at the column to see which line was clicked.

        chart.setAction({
          id: 'sample',
          text: 'See Pie Chart',
          action: function() {
            selection = chart.getSelection();
			var line = selection[0].column;
            switch (selection[0].row) {
              case 0: document.getElementById('container').style.display = "block"; break;
              case 1: document.getElementById('container').style.display = "block"; break;
              case 2: document.getElementById('container').style.display = "block"; break;
              case 3: document.getElementById('container').style.display = "block"; break;
              case 4: document.getElementById('container').style.display = "block"; break;
              case 5: document.getElementById('container').style.display = "block"; break;
              case 6: document.getElementById('container').style.display = "block"; break;
              case 7: document.getElementById('container').style.display = "block"; break;
              case 8: document.getElementById('container').style.display = "block"; break;
              case 9: document.getElementById('container').style.display = "block"; break;
              case 10: document.getElementById('container').style.display = "block"; break;
              case 11: document.getElementById('container').style.display = "block"; break;
              case 12: document.getElementById('container').style.display = "block"; break;
              case 13: document.getElementById('containerEAVI').style.display = "block"; break;
            }/*Case 13 has the same 'See Pie Chart' for both Jan 2018 points. I need to find a way to specify two unique onClick functions for each colored point during that month. One will show the S-AVI pie chart and the other will show the E-AVI chart  */
			if(line === 1) {
			  //Sales Index (S-AVI)
			}
			else {
                          //Employment Index (E-AVI)
			}
          }
        });

Open in new window


I added an alert to your Fiddle to demonstrate:  https://jsfiddle.net/g4gz279e/46/
NOTE:  This only works when a Jan 2018 "See Pie Chart" is clicked due to the error I mentioned in my previous post.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Annie KayAuthor Commented:
Hi Megan,

Yes, the container items are there as filler :)

Thanks! That's exactly what I was looking for -- so I would remove the switch statement then?

See my changes to the fiddle here: https://jsfiddle.net/Error_423/g4gz279e/51/

I removed the case 13 and added the getElementbyId to the if statement. Should I do this for all the cases?

Thanks!
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

zephyr_hex (Megan)DeveloperCommented:
The switch statement can be taken out.  I believe this is more along the lines of what you're looking for:

https://jsfiddle.net/g4gz279e/53/

          action: function() {
            selection = chart.getSelection();
            if(selection[0].column === 1) {
            document.getElementById('containerSAVI').style.display = "block";
            document.getElementById('containerEAVI').style.display = "none";
            } else {
            document.getElementById('containerEAVI').style.display = "block";
            document.getElementById('containerSAVI').style.display = "none";
            }
          }

Open in new window

Annie KayAuthor Commented:
So the switch statement allowed me to change the pie charts for each month. How would I do that with this if/else?

Thanks!!
zephyr_hex (Megan)DeveloperCommented:
If you are hard-coding this by month, you'll still need a switch statement.  You could keep your previous switch and inside each case, check for the line color, or, check for the line color and have a switch statement inside each clause of the if / else.

Ideally, this would not be hard-coded, but would instead have one pie chart function that accepts the type and month as parameters.  This would reduce the repetition of code and simplify the logic by eliminating the need for conditional statements.
Annie KayAuthor Commented:
Hi Megan,

That makes complete sense - I'll look into that.

Any suggestions on how I can move all the JS into the JS section and just call it from the HTML?
zephyr_hex (Megan)DeveloperCommented:
It looks like you need to use unique variable names in order to move the JS out of the HTML.  I also would recommend that you use jQuery events to trigger your actions, rather than calling JS from HTML.

Here's an example, using the last Fiddle Update I posted above:

https://jsfiddle.net/ufcfc7vg/3/
Annie KayAuthor Commented:
THANK YOU!

Last question -- I've been struggling to figure out how to remove the text See Pie Chart for dots on the line graph that don't have pie charts associated with them. Since I'm setting the text to be See Pie Chart here:  text: 'See Pie Chart', I'm wondering what I could do? I'm okay with making it a manual display none, since I will be manually putting in the numbers as the pie chart data flows in.

Thanks!
Anisah
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
GraphAPI

From novice to tech pro — start learning today.