User101
User101

Reputation: 55

click event on morris bar

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>

Upvotes: 2

Views: 5496

Answers (4)

sanpat
sanpat

Reputation: 76

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);
});

Upvotes: 0

bruno leclerc
bruno leclerc

Reputation: 342

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;
}

Upvotes: 0

Carlos
Carlos

Reputation: 160

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 );
    });

Upvotes: 0

Louys Patrice Bessette
Louys Patrice Bessette

Reputation: 33933

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

Upvotes: 4

Related Questions