admin管理员组文章数量:1418708
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=".1.0/raphael-min.js"></script>
<script src=".8.2.min.js"></script>
<script src=".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 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./ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="http://code.jquery./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>
Share
Improve this question
asked Jul 29, 2016 at 15:18
User101User101
551 silver badge10 bronze badges
4 Answers
Reset to default 4First, 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 ments 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
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 );
});
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;
}
Above examples were of line chart rather than bar charts with multiple bars of different colors.
Based on above solution below is my code in case you have three bars in your morris chart:
$('#bar-sixmonthstats svg rect').on('click', function () {
_$month = $('.morris-hover-row-label').text();
//debugger;
barColor = $(this).attr('fill');
var barType = '';
switch (barColor) {
case '#64a900': barType = 'PO'; break;
case '#7acc00': barType = 'SO'; break;
case '#4d8000': barType = 'WO'; break;
}
console.log("Order Type : " + barType + "\nDate : " + _$month);
});
本文标签: javascriptclick event on morris barStack Overflow
版权声明:本文标题:javascript - click event on morris bar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745293567a2651942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论