javascript - click event on morris bar - Stack Overflow

I implement morris graph and also working on click function but i want when i clicked on bar then data

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
Add a ment  | 

4 Answers 4

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745293568a4621006.html

相关推荐

  • javascript - click event on morris bar - Stack Overflow

    I implement morris graph and also working on click function but i want when i clicked on bar then data

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信