【问题标题】:c3 categories tick formattingc3 类别刻度格式
【发布时间】:2016-06-16 14:13:47
【问题描述】:

我有一个 c3 条形图类别。我想格式化类别标签以便能够将其截断到一定长度。但我想不通。

这是图形定义:

var chart;
chart = c3.generate({
                    padding: {
                        top: 20,
                        bottom: 20

                    },
                    data: {

                        columns: [
                                  ["Listeners",4,2,0],
                                  ["Listens",4,2,0]],
                        type: 'bar',
                        groups:[["Listeners", "Listens"]]
                    },
                    axis: {
                        x: {
                            type: 'category',
                            categories: ["012345678890", "012345678890", "012345678890"],
                            
                        }
                    },
                    bar: {
                        width: {
                            ratio: 0.5 // this makes bar width 50% of length between ticks
                        }
                    },
                    legend: {
                        show: false
                    }
                });
<link href="https://raw.githubusercontent.com/c3js/c3/0.4.11/c3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://raw.githubusercontent.com/c3js/c3/0.4.11/c3.js"></script>

<div id="chart" class="c3" style="max-height: 280px; position: relative;"></div>

我尝试添加它以格式化 x 轴定义内的刻度,但它抱怨图表未定义

tick:{format:function (x) { return chart.categories()[x].substring(0, 10); }}

提前致谢!

【问题讨论】:

    标签: javascript c3.js


    【解决方案1】:

    当然,您只需声明没有值的变量var chart; 是未定义的。 尝试在函数c3.generate() 之前创建图表配置,然后将配置作为参数传递。

    var config = {
            padding : {
                top : 20,
                bottom : 20
            },
            data : {
                columns : [ [ "Listeners", 4, 2, 0 ], [ "Listens", 4, 2, 0 ] ],
                type : 'bar',
                groups : [ [ "Listeners", "Listens" ] ]
            },
            axis : {
                x : {
                    type: 'category',
                    categories: ["012345678890", "012345678890","012345678890"],
                }
            },
            bar : {
                width : {
                    ratio : 0.5
                }
            },
            legend : {
                show : false
            }
        };
    
        config.axis.x.tick = {
            format : function(x) {
                return config.axis.x.categories[x].substring(0,10)
            }
        };
    
        var chart = c3.generate(config);
    

    【讨论】:

      【解决方案2】:

      在您的代码中,您在 x 轴定义中命名了类别,我不确定您为什么不只更改标签?

      ** 示例 **

      var chart;
      chart = c3.generate({
                          padding: {
                              top: 20,
                              bottom: 20
      
                          },
                          data: {
      
                              columns: [
                                        ["Listeners",4,2,0],
                                        ["Listens",4,2,0]],
                              type: 'bar',
                              groups:[["Listeners", "Listens"]]
                          },
                          axis: {
                              x: {
                                  type: 'category',
                                  _comment: "THIS IS WHERE YOU SET YOUR CATEGORY LABELS",
                                  categories: ["Label1", "Label2", "Label3"],
                                  
                              }
                          },
                          bar: {
                              width: {
                                  ratio: 0.5 // this makes bar width 50% of length between ticks
                              }
                          },
                          legend: {
                              show: false
                          }
                      });
      <link href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.css" rel="stylesheet"/>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.js"></script>
      
      <div id="chart" class="c3" style="max-height: 280px; position: relative;"></div>

      【讨论】:

      • 刚试过你的建议:axis: { x: { type: 'category', label: ["012345678890", "012345678890", "012345678890"], } } 但这没有用跨度>
      • 我的问题是我需要保留类别标签,只要它们也用于工具提示,但截断它们以显示在勾选中。所以这个解决方案对我不起作用:-( @Dan
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      相关资源
      最近更新 更多