I implement morris graph and also working on click function but i want when i clicked on bar then data of that bar will alert.so how can get data.
<!DOCTYPE html>
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://cdn.oesmith.co.uk/morris-0.4.1.min.js"></script>
<meta charset=utf-8 />
<title>Morris.js Bar Chart Example</title>
</head>
<body>
<div id="bar-example"></div>
</body>
<script>
Morris.Bar({
element: 'bar-example',
data: [
{ y: '2006', a: 100, b: 90 },
{ y: '2007', a: 75, b: 65 },
{ y: '2008', a: 50, b: 40 },
{ y: '2009', a: 75, b: 65 },
{ y: '2010', a: 50, b: 40 },
{ y: '2011', a: 75, b: 65 },
{ y: '2012', a: 100, b: 90 }
],
xkey: 'y',
ykeys: ['a', 'b'],
labels: ['Series A', 'Series B']
});
$( "#bar-example svg rect" ).on('click', function(data) {
alert( data);//show [object.Object] when alert popup
});
</script>
</html>
I am not very proud of the following javascript function, but to retrieve the index of the rectangle on which we just click, I use it successfully. Then I call a C# method to retrieve the values.
The "rect" element is not in my line graph, so I just removed it. Thanks so much for this, it gave me exactly what I needed.
First, thank you to let me discover this cool graph library. ;)
This is an EDIT
After a couple tries...
I ended up in a real simplification of my solution.
So for clarity, I removed all previous edit. You can see them in edit history anyway. ;).
So now, based on this new example provided in comments below:
We are getting the needed infos in the summary below the graph:
thisDate
andthisData
hold nothing (empty) onload.But they hold the last clicked bar values on click.
These variables are usable in other subsequent scripts, since declared outside of the
click()
handler.See in this new CodePen