chart_flutter标签/ x轴上的文本彼此重叠

时间:2019-01-30 09:40:47

标签: charts dart flutter google-visualization flutter-dependencies

我在flutter中使用Charts_flutter包绘制了条形图。但是x轴上的域标签相互重叠。有办法解决吗?有没有办法使文本倾斜或增加图表的宽度,使其可以水平滚动?

barchart

我尝试搜索任何解决方案,例如labelspecs,但找不到解决方案。这是我的代码-

double maxheight = .80 * MediaQuery.of(context).size.height;
        var series = [
          new charts.Series(
            domainFn: (BarChartConfig barchartconfig, _) => barchartconfig.name,
            measureFn: (BarChartConfig barchartconfig, _) => barchartconfig.rowcount,
            colorFn: (BarChartConfig barchartconfig, _) => barchartconfig.color,
            id: 'Count',
            data: datacharts,
          )
        ]; 
        var chart = new charts.BarChart(
          series,
          animate: true,
          animationDuration: Duration(seconds: 2),
        );
        return SizedBox(
          height: maxheight,
          child: chart,
        );

3 个答案:

答案 0 :(得分:4)

@ aswani-prakash的回答很好,

如果希望标签可见,则只需旋转x轴标签即可。

BarChart(
    ...
    domainAxis: charts.OrdinalAxisSpec(
              renderSpec: charts.SmallTickRendererSpec(labelRotation: 60),
       ),
 ),

答案 1 :(得分:0)

您可以发布图表小部件的构建功能吗?

下面是通过domainAxis-> renderSpec-> labelStyle-> fontSize:

调整字体大小的示例。

https://google.github.io/charts/flutter/example/axes/custom_font_size_and_color

此外,您可以在renderSpec对象中设置minimumPaddingBetweenLabelsPx: 0来调整标签之间的填充。整个构建功能可能如下所示:

  @override
  Widget build(BuildContext context) {
    return new charts.BarChart(
      seriesList,
      animate: animate,

      /// Assign a custom style for the domain axis.
      ///
      /// This is an OrdinalAxisSpec to match up with BarChart's default
      /// ordinal domain axis (use NumericAxisSpec or DateTimeAxisSpec for
      /// other charts).
      domainAxis: new charts.OrdinalAxisSpec(
          renderSpec: new charts.SmallTickRendererSpec(
              minimumPaddingBetweenLabelsPx: 0
              // Tick and Label styling here.
              labelStyle: new charts.TextStyleSpec(
                  fontSize: 18, // size in Pts.
                  color: charts.MaterialPalette.black),

              // Change the line colors to match text color.
              lineStyle: new charts.LineStyleSpec(
                  color: charts.MaterialPalette.black))),

      /// Assign a custom style for the measure axis.
      primaryMeasureAxis: new charts.NumericAxisSpec(
          renderSpec: new charts.GridlineRendererSpec(

              // Tick and Label styling here.
              labelStyle: new charts.TextStyleSpec(
                  fontSize: 18, // size in Pts.
                  color: charts.MaterialPalette.black),

              // Change the line colors to match text color.
              lineStyle: new charts.LineStyleSpec(
                  color: charts.MaterialPalette.black))),
    );
  }

答案 2 :(得分:0)

设置sliding viewportordinal viewport

          var chart = charts.BarChart(
                series,
                animate: true,
                domainAxis: new charts.OrdinalAxisSpec(
                    viewport: new charts.OrdinalViewport('AePS', 3),
                ),

                behaviors: [
                    new charts.SeriesLegend(),
                    new charts.SlidingViewport(),
                    new charts.PanAndZoomBehavior(),

                ],
            )