简体   繁体   English

如何在C3饼图中更改图例的文本

[英]How can i change text of legend in c3 pie chart

How can I change the Text of legend of pie chart. 如何更改饼图图例的文本。 I am using c3 charts in my php page. 我在我的PHP页面中使用c3图表。 I have already read the documentation of c3 charts but no luck. 我已经阅读了c3图表的文档,但是没有运气。

Currently i am using this code it show legend for true but I not able to change the text I have tried this. 目前,我正在使用此代码,它显示图例为true但我无法更改尝试过的文本。

var chart = c3.generate({
        bindto: '#container',
        padding: {
                  top: 10,
                  right: 0,
                  bottom: 10,
                  left: 0,
            },
        data: {
            columns: [<?php echo $pieChartDataString; ?>],
            type : 'pie',
            labels: true
        },
        legend: {
                show: true,
                position: 'upper-center'
                format: {
                        title: function () { return "Legend title"; },
                        name : function () { return "Legend name"; },
                        value: function () { return "Legend value";}
                        }
                }

   //But these legend values or not showing 

    });

It's not showing my legend values its always shows only columns as legend. 它没有显示我的图例值,它始终仅将列显示为图例。

Is there any way that I can change the legend values. 有什么方法可以更改图例值。

You haven't provided the data that gets outputted from your php, so it's hard to say. 您尚未提供从php输出的数据,因此很难说。

But the first item in each of the columns array determines the name that goes in the legend. 但是,每个columns数组中的第一项决定图例中使用的名称。 So: 所以:

columns: [
    ['this is legend 1', 30],
    ['put your value here', 120], 
]

would result in the legend labels being "this is legend 1" and "put your value here". 将导致图例标签为“这是图例1”和“在此处输入您的值”。

Here's a fiddle: http://jsfiddle.net/jrdsxvys/9/ 这是一个小提琴: http : //jsfiddle.net/jrdsxvys/9/

Edit... Another option is to use the names property, as done here: http://jsfiddle.net/jrdsxvys/40/ 编辑...另一个选择是使用names属性,如下所示: http : //jsfiddle.net/jrdsxvys/40/

data: {
    columns: [
      ['d1', 30],
      ['d2', 120]
    ],
    type: 'pie',
    labels: true,
    names: {
      d1: 'some name here',
      d2: 'another name'
    }
}

@agpt Yes. @agpt是的。 The names property is a good way to go generally because the first property of the columns data array eg 'd1' above is used when doing things like having multiple types on charts. names属性通常是个不错的选择,因为在进行诸如在图表上具有多种类型之类的操作时,会使用列数据数组的第一个属性,例如上面的“ d1”。 eg for a bar and line combination using types instead of type: 'pie' : 例如,对于使用types而不是type: 'pie'的条和线组合type: 'pie'

columns: [
   ['bar_1', 3, 8, 6],
   ['bar_2', 4, 0, 7],
   ['bar_3', 2, 3, 0] 
],

types: {
  bar_1: 'bar',
  bar_2: 'line',
  bar_3: 'bar'
},

names : {
      bar_1: 'Initial',
      bar_2: '3 month',
      bar_3: '6 month'                
}

So, using the names property allows you to use more 'dynamic' property names and be consistent throughout the config. 因此,使用names属性可以使您使用更多的“动态”属性名称,并且在整个配置中保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM