问题
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>
回答1:
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:
var thisDate,thisData;
$( "#bar-example svg rect" ).on('click', function() {
// Find data and date in the actual morris diply below the graph.
thisDate = $(".morris-hover-row-label").html();
thisDataHtml = $(".morris-hover-point").html().split(":");
thisData = thisDataHtml[1].trim();
// alert !!
alert( "Data: "+thisData+"\nDate: "+thisDate );
});
thisDate
and thisData
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
回答2:
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.
var thisDate,thisData;
$( "#morrisLine svg" ).on('click', function() {
// Find data and date in the actual morris diply below the graph.
thisDate = $(".morris-hover-row-label").html();
thisDataHtml = $(".morris-hover-point").html().split(":");
thisData = thisDataHtml[1].trim();
// alert !!
alert( "Data: "+thisData+"\nDate: "+thisDate );
});
回答3:
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.
$('#m_Top10Applications svg rect').on('click', function (event) { window.location.replace("MyPage.aspx?Top10Index=" + GetMorrisBarClickedBarIndex($this));});
function GetMorrisBarClickedBarIndex(par$ThisRect)
{
var xRect = par$ThisRect[0].x.baseVal.value;
var TheParent = par$ThisRect.parent();
var Rectangles = TheParent.find("rect");
var Rect0 = Rectangles[0];
var Rect1 = Rectangles[1];
var X0 = Rect0.x.baseVal.value;
var X1 = Rect1.x.baseVal.value;
var RectWidth = X1 - X0;
var xPos = xRect - X0;
var Index = Math.floor(xPos / RectWidth, 0);
return Index;
}
来源:https://stackoverflow.com/questions/38662260/click-event-on-morris-bar