Question

I need to add a graph in my project and I tried one free graph (link is given below) I need to add an onclick handler to that pie chart. I think need to add the onclick function inside

snap.svg.js

(in path tag) I tried a lot but didn't get

http://www.jqueryscript.net/demo/Responsive-Pie-Chart-Plugin-wit-jQuery-Snap-SVG-Pizza

Was it helpful?

Solution

You can use jquery

If you want to add a click event to a slice:

  // this adds a click event to the first slice
  $("path[data-id='s0']").click(function() {
     alert('you clicked slice one');
  });

  // if you have multiple pie's you can be more specific
  $("#svg svg path[data-id='s0']").click(function(){
     alert('you just clicked pie with id #svg');
  });

if you want to add a click event to the whole pie:

  $('#svg').click(function(e){
      alert('you just clicked the whole pie!');
  });

OTHER TIPS

.onclick should be set to a function instead of a string. Try

elemm.onclick = function() { alert('blah'); };

In your pizza.js file you could add an onclick listener following Snap.svg

   pie : function (legend) {
      // pie chart concept from JavaScript the 
      // Definitive Guide 6th edition by David Flanagan
      ...

        path.node.setAttribute('data-id', 's' + i);

        //============= You could add your listener here
        path.click(alert("My click function"));
        //===============================================


        this.animate(path, cx, cy, settings);

        // The next wedge begins where this one ends
        start_angle = end_angle;
      }

  return [legend, svg.node];
},

Another option is trying to add new events through the events {} @ pizza.js

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top