【问题标题】:C3 Chart bar with percentage带有百分比的 C3 图表栏
【发布时间】:2018-11-08 12:29:39
【问题描述】:

我正在使用带有 React 的 C3 图表。并且图表的创建没有问题。但是我需要将条形图 [1, 5, 1, 5, 2, 2, 1, 3] 中的数据显示在 % 旁边。

例如 1%、5%、1%、2%...

这是我的代码

 const failureTopBar = {
                data: {
                    columns: [
                        ["Probability", 1, 5, 1, 5, 2, 2, 1, 3]
                    ],
                    type: 'bar',
                    labels: true,
                    colors: {
                        data1: '#4DA7EF',
                    }
                },
                bar: {
                    width: 10
                },
                axis: {
                    rotated: true,
                    y: { 
                        show: false,
                    },       
                    x:{
                        type: 'category',
                        categories: ['failure 1','failure 2','failure 2','failure 3','failure 4','failure 5','failure 6','failure 7']
                    }  
                },
                legend: {
                    show: false
                },
                grid: {
                    lines: {
                      front: false
                  }
                },
                tooltip: {
                    show: false
                }
            }
<C3Chart 
      data={failureTopBar.data} 
      bar={failureTopBar.bar} 
      axis={failureTopBar.axis} 
      legend={failureTopBar.legend} 
      grid={failureTopBar.grid}
      tooltip={failureTopBar.tooltip}
      style={{svg: {width: '100%'}}}
/>

有什么想法吗? 感谢您的帮助!

【问题讨论】:

    标签: javascript reactjs bar-chart c3.js react-chartjs


    【解决方案1】:

    为图表添加标签

    labels: {
       format:function (v, id, i, j) { return v + '%'; }
    }
    

    更新代码

    const failureTopBar = {
                    data: {
                        columns: [
                            ["Probability", 1, 5, 1, 5, 2, 2, 1, 3]
                        ],
                        type: 'bar',
                        labels: {
                          format:function (v, id, i, j) { return v + '%'; }
                        },
                        colors: {
                            data1: '#4DA7EF',
                        }
                    },
                    bar: {
                        width: 10
                    },
                    axis: {
                        rotated: true,
                        y: { 
                            show: false,
                        },       
                        x:{
                            type: 'category',
                            categories: ['failure 1','failure 2','failure 2','failure 3','failure 4','failure 5','failure 6','failure 7']
                        }  
                    },
                    legend: {
                        show: false
                    },
                    grid: {
                        lines: {
                          front: false
                      }
                    },
                    tooltip: {
                        show: false
                    }
                }
    <C3Chart 
          data={failureTopBar.data} 
          bar={failureTopBar.bar} 
          axis={failureTopBar.axis} 
          legend={failureTopBar.legend} 
          grid={failureTopBar.grid}
          tooltip={failureTopBar.tooltip}
          style={{svg: {width: '100%'}}}
    />
    

    【讨论】:

    • 你成就了我的一天!这正是我所需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2019-03-12
    • 1970-01-01
    • 2022-01-21
    • 2020-10-17
    相关资源
    最近更新 更多