如何在c3.js折线图或详细图表中为区域添加标签?

问题描述 投票:1回答:1

我使用c3.js绘制线图与区域引用qazxsw poi。

我需要显示区域的标签。我搜索了很多但是不喜欢任何解决方案。

现有代码:

this link
var chart = c3.generate({
    bindto: '#detail_chart',
    data: {
        columns: [
            ['data1', 30, 200, 100, 400, 150, 250, 400],
            ['data2', 830, 1200, 1100, 1400, 1150, 1250, 1500],
        ],
        axes: {
            data2: 'y2'
        }
    },
    axis: {
        y2: {
            show: true
        }
    },
    regions: [
        {axis: 'x', end: 1, class: 'regionX'},
        {axis: 'x', start: 2, end: 4, class: 'regionX'},
        {axis: 'x', start: 5, class: 'regionX'},
        {axis: 'y', end: 50, class: 'regionY'},
        {axis: 'y', start: 80, end: 140, class: 'regionY'},
        {axis: 'y', start: 400, class: 'regionY'},
        {axis: 'y2', end: 900, class: 'regionY2'},
        {axis: 'y2', start: 1150, end: 1250, class: 'regionY2'},
        {axis: 'y2', start: 1300, class: 'regionY2'},
    ]
});
.c3-region.regionY {
  fill: blue;
}
.c3-region.regionY2 {
  fill: yellow;
}

我的要求是:

<!-- Load c3.css -->
<link href=" https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.7/c3.css
" rel="stylesheet">

<!-- Load d3.js and c3.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.6.7/c3.min.js"></script>
 <div id="detail_chart"></div>

预期结果:regions: [ {axis: 'x', end: 1, class: 'regionX',label:'label1'}, {axis: 'x', start: 2, end: 4, class: 'regionZ',label:'label3'}, {axis: 'x', start: 5, class: 'regionY',label:'label2'}, ] 如何使用c3.js或d3.js实现它。如果有人有想法与我分享。提前致谢。

javascript d3.js label c3.js region
1个回答
2
投票

这是使用d3执行此操作的方法。您需要根据自己的喜好改变字体大小。请参阅代码中的注释。

enter image description here
var chart = c3.generate({
    bindto: '#detail_chart',
    data: {
        columns: [
            ['data1', 30, 200, 100, 400, 150, 250, 400],
            ['data2', 830, 1200, 1100, 1400, 1150, 1250, 1500],
        ],
        axes: {
            data2: 'y2'
        }
    },
    axis: {
        y2: {
            show: true
        }
    },
    regions: [
        {axis: 'x', end: 1, class: 'regionX'},
        {axis: 'x', start: 2, end: 4, class: 'regionX'},
        {axis: 'x', start: 5, class: 'regionX'},
        {axis: 'y', end: 50, class: 'regionY'},
        {axis: 'y', start: 80, end: 140, class: 'regionY'},
        {axis: 'y', start: 400, class: 'regionY'},
        {axis: 'y2', end: 900, class: 'regionY2'},
        {axis: 'y2', start: 1150, end: 1250, class: 'regionY2'},
        {axis: 'y2', start: 1300, class: 'regionY2'},
    ]
});

// set your labels here
var labels = {
  '_1': 'label for region _1',
  '2_4': 'label for region 2-4',
  '5_': 'label for 5_'
};

// select all regionX rectangles in the chart
d3.selectAll('#detail_chart rect.regionX').each( function(r){
  var region = this;
  // attach a text element to the parent node
  d3.select(this.parentNode)
    .append('text')
    // x offset is current region's x value + half its width
    .attr('x', function() {
      return region.width.baseVal.value/2 + region.x.baseVal.value;
    })
    .attr('y', 20) // change this to your liking
    .attr('text-anchor', 'middle')
    .attr('class', 'region-label')
    .text( function(){
      // this corresponds to the values you set when configuring the axis
      // it is in the form <start>_<end>
      var id = (r.start ? r.start : '') + '_' + (r.end ? r.end : '');
      return labels[ id ]
    });
});
.c3-region.regionY {
  fill: blue;
}
.c3-region.regionY2 {
  fill: yellow;
}
© www.soinside.com 2019 - 2024. All rights reserved.