admin管理员组文章数量:1434913
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 badges3 Answers
Reset to default 3If 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.
本文标签: javaHow to customize the legend of a Morris graphStack Overflow
版权声明:本文标题:java - How to customize the legend of a Morris graph? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745642141a2667922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论