最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding coloured box as legends to morris graph - Stack Overflow

programmeradmin3浏览0评论

I am trying to add colored box as legends to the morris graph. The below picture shows the C3 graph legend. But I am unable to find a way to add in the Morris the same way.

Can anyone help ?

Currently , The Morris legends appears as below with the code:

The code for Morris - jQuery

var chart=  Morris.Line({
    element: 'status-req',
    data: <?=json_encode($jsonDataArray)?>,     
    xkey: 'y',
    ykeys: <?=json_encode($deliveryAspect['statusKeys'])?>,
    labels:  <?=json_encode($deliveryAspect['statusLabels'])?>,
    hideHover: 'auto',
    resize: true,      
    smooth:true       
});

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

I am trying to add colored box as legends to the morris graph. The below picture shows the C3 graph legend. But I am unable to find a way to add in the Morris the same way.

Can anyone help ?

Currently , The Morris legends appears as below with the code:

The code for Morris - jQuery

var chart=  Morris.Line({
    element: 'status-req',
    data: <?=json_encode($jsonDataArray)?>,     
    xkey: 'y',
    ykeys: <?=json_encode($deliveryAspect['statusKeys'])?>,
    labels:  <?=json_encode($deliveryAspect['statusLabels'])?>,
    hideHover: 'auto',
    resize: true,      
    smooth:true       
});

chart.options.labels.forEach(function(label, i) {
    var legendItem = $('<span align="center"></span> ').text(label).css('color', chart.options.lineColors[i])
    $('#legend').append(legendItem);
});
Share Improve this question edited Dec 30, 2020 at 13:41 Tyler2P 2,37030 gold badges25 silver badges33 bronze badges asked Mar 17, 2016 at 6:11 Wings2flyWings2fly 9371 gold badge13 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The problem is solved using the below code. CSS

.mbox {   
    display: inline-block;
    width: 10px;
    height: 10px;
    margin: 10px 55px 10px 25px;
    padding-left: 4px;
}

jQuery

chart.options.labels.forEach(function(label, i){
    var legendlabel=$('<span style="display: inline-block;">'+label+'</span>')
    var legendItem = $('<div class="mbox"></div>').css('background-color', chart.options.lineColors[i]).append(legendlabel)
    $('#legend').append(legendItem)   
})
发布评论

评论列表(0)

  1. 暂无评论