Question

I am using the morris.js library and I would like to customize my graphs so to properly format the date-time present in their legend. At this time, when I pass values as-like 2013-07-26T03:34:41+02:00 (ISO8601) to the ykey then the generated legend content is the following:

The morris legend

I would like to generate the content of legend so to display the 2013-07-26T03:34:41+02:00 in a user-friendly way, maybe something like 03:34:41 (2013-07-26) or just now / 19 seconds ago.

Is it possible? If so, how can I make that?

Was it helpful?

Solution 2

Use below function and change the body according to your need.

   hoverCallback: function (index, options, content) {
   var row = options.data[index];
   //here row has x and y values
   }

OTHER TIPS

If anyone came here looking for an actual legend rather than formatting the hover details on a data point, one of the Morris team made a fiddle showing how to do it with some JS and CSS.

From here.

<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>
<style>
    #legend {
        height: 50px;
        background: rgba(127, 127, 127, 0.5)
    }
    #legend span {
        display: inline-block;
        padding: 15px 30px;
        position: relative;
    }
    #legend span:after {
        padding-left: 4px;
        content: '\00a0\00a0\00a0\00a0\00a0\00a0';
    text-decoration: line-through;
    }
</style>
<body>
  <div id="area-example">

  </div>
    <div id="legend"></div>
</body>

then add this below your JS:

chart.options.labels.forEach(function(label, i){
    var legendItem = $('<span></span>').text(label).css('color', chart.options.lineColors[i])
    $('#legend').append(legendItem)
})

If it's only for the date format of the x label that you are aiming, then you should use the dateFormat option :

dateFormat: function (x) {
   return moment(x).calendar();
}

It won't change the default hoverCallback and just impact the way the x label will be displayed inside the hover bubble.

In my example I used the moment.js lib to make the date look better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top