最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

java - How to customize the legend of a Morris graph? - Stack Overflow

matteradmin6PV0评论

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:

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?

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:

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?

Share Improve this question edited Jul 26, 2013 at 2:19 user502052 asked Jul 26, 2013 at 2:10 user502052user502052 15.3k31 gold badges113 silver badges194 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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./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>
<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)
})

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
   }

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.

Post a comment

comment list (0)

  1. No comments so far