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

javascript - C3.js Bar Graphs: setting X axis labels - Stack Overflow

programmeradmin1浏览0评论

I have to draw a bar graph with two data sets and I want to label each set (in X axis) with a text. Below code produces:

As can be seen, labels are auto generated, i.e. 0 and 1 (highlighted in yellow color). But I want to change 0 and 1 to a two text values, say, P1 and P2.

var chart = c3.generate({
  bindto: '#bar',
  data: {
    columns: [
      ['A1', 30, 200],
      ['A2', 130, 100],
      ['A3', 12, 59],
      ['A4', 42, 25],
      ['A5', 76, 84],
      ['A6', 83, 14],
      ['A7', 54, 87],
      ['A8', 110, 40],
      ['A9', 34, 48]
    ],
    type: 'bar'
  },
  bar: {
    width: {
      ratio: 0.5
    }
  }
});
<script src=".4.11/d3.min.js"></script>
<script src=".3.0/c3.min.js"></script>
<link href=".3.0/c3.css" rel="stylesheet"/>

<div id="bar">
</div>

I have to draw a bar graph with two data sets and I want to label each set (in X axis) with a text. Below code produces:

As can be seen, labels are auto generated, i.e. 0 and 1 (highlighted in yellow color). But I want to change 0 and 1 to a two text values, say, P1 and P2.

var chart = c3.generate({
  bindto: '#bar',
  data: {
    columns: [
      ['A1', 30, 200],
      ['A2', 130, 100],
      ['A3', 12, 59],
      ['A4', 42, 25],
      ['A5', 76, 84],
      ['A6', 83, 14],
      ['A7', 54, 87],
      ['A8', 110, 40],
      ['A9', 34, 48]
    ],
    type: 'bar'
  },
  bar: {
    width: {
      ratio: 0.5
    }
  }
});
<script src="http://cdnjs.cloudflare./ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="http://cdnjs.cloudflare./ajax/libs/c3/0.3.0/c3.min.js"></script>
<link href="http://cdnjs.cloudflare./ajax/libs/c3/0.3.0/c3.css" rel="stylesheet"/>

<div id="bar">
</div>

I couldn't find a way to assign X-axis labels so far...

Share Improve this question edited Oct 2, 2020 at 17:08 Ruslan López 4,4772 gold badges30 silver badges40 bronze badges asked Mar 31, 2018 at 3:40 Lakmal PremaratneLakmal Premaratne 1,2297 gold badges19 silver badges36 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

add axis option to the code

var chart = c3.generate({
  bindto: '#bar',
  data: {
    columns: [
      ['A1', 30, 200],
      ['A2', 130, 100],
      ['A3', 12, 59],
      ['A4', 42, 25],
      ['A5', 76, 84],
      ['A6', 83, 14],
      ['A7', 54, 87],
      ['A8', 110, 40],
      ['A9', 34, 48]
    ],
    type: 'bar'
  },
  bar: {
    width: {
      ratio: 0.5
    }
  },
  axis: {
    x: {
      type: 'category',
      categories: ['cat1', 'cat2']
    }
  }
});
<script src="http://cdnjs.cloudflare./ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="http://cdnjs.cloudflare./ajax/libs/c3/0.3.0/c3.min.js"></script>
<link href="http://cdnjs.cloudflare./ajax/libs/c3/0.3.0/c3.css" rel="stylesheet" />

<div id="bar">
</div>

发布评论

评论列表(0)

  1. 暂无评论