Question

I am unable to get catch the most basic jqplot click events in my rails 3.1 app. I want to catch any click on my graph. I have created a simple test page that still doesn't work:

    <head>
  <%= javascript_include_tag "jquery"%>
  <%= javascript_include_tag "jquery.jqplot.min.js"%>
  <%= stylesheet_link_tag "jquery.jqplot.min.css" %>
</head>
<body>
<p>Chart test</p>
<div id='chart1'></div>
</body>
<script>
$(document).ready(function(){
  var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]);

  function myClickHandler(ev, gridpos, datapos, neighbor, plot) {
    alert('you have triggered click action');
  }
  $.jqplot.eventListenerHooks.push(['jqplotClick', myClickHandler]);

});
</script>

Nothing happens when I click on the graph. I have tried many different approaches.

I'm using version 3.1.0 of the jquery-rails gem (version 1.11 of jquery) and version 1.0.8r1250 of jqplot.

I'm at the point where I don't even know what else to do to troubleshoot. Any help would be much appreciated.

Was it helpful?

Solution

The solution was to set up the eventListenerHooks before creating the plot:

<script>
$(document).ready(function(){


  function myClickHandler(ev, gridpos, datapos, neighbor, plot) {
    alert('you have triggered click action');
  }
  $.jqplot.eventListenerHooks.push(['jqplotClick', myClickHandler]);

  var plot1 = $.jqplot ('chart1', [[3,7,9,1,4,6,8,2,5]]); //moved


});
</script>

OTHER TIPS

why not use the builtin jQuery event handler? It doesn't require creating an eventListenerHook...

$.jqplot('chart1',...);
$('#chart1').bind('jqplotClick', function(ev, gridpos, datapos, neighbor, plot) {
    //handle jqplotClick
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top