How to add a label on pie chart from flutter?

喜欢而已 提交于 2019-12-24 00:30:05

问题


I wanna users see more information in pie chart (example percent of data, shown in the chart below).

My code is used show pie chart, i tried some function like displayName, labelAccessorFn in charts.Series but seem label still down show for me

  @override
  Widget build(BuildContext context) {
    return new charts.PieChart(seriesList,
        animate: animate,
        defaultRenderer: new charts.ArcRendererConfig(arcWidth: 60));
  }

  new charts.Series<LinearSales, int>(
    id: 'Sales',
    domainFn: (LinearSales sales, _) => sales.year,
    measureFn: (LinearSales sales, _) => sales.sales,
    data: data,
  )

回答1:


I think you're missing this:

  new charts.Series<LinearSales, int>(
    id: 'Sales',
    domainFn: (LinearSales sales, _) => sales.year,
    measureFn: (LinearSales sales, _) => sales.sales,
    data: data,
    labelAccessorFn: (LinearSales row, _) => '${row.year}: ${row.sales}', //Add this
)



回答2:


For other people that come to this Question:

to add labels to the chart, you need to add the arcRendererDecorators to the ArcRendererConfig like this:

  @override
  Widget build(BuildContext context) {
    return charts.PieChart(
        seriesList,
        animate: animate,
        defaultRenderer: new charts.ArcRendererConfig(
            arcWidth: 120,
            arcRendererDecorators: [  // <-- add this to the code 
              charts.ArcLabelDecorator() // <-- and this of course
            ]
        ));
  }
}

don't forget also the labelAccessorFn



来源:https://stackoverflow.com/questions/51445485/how-to-add-a-label-on-pie-chart-from-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!