fl_chart icon indicating copy to clipboard operation
fl_chart copied to clipboard

Bar Chart overflows Container widget

Open clemens- opened this issue 1 year ago • 4 comments

Describe the bug When creating a bar graph, the graph is larger than the container surrounding it. I would expect the graph to stay within the container size I defined.

To Reproduce

Widget _buildBarGraph(BuildContext context, List<int> allValues, bool opState) {
  final int barGraphLength = 5;
  List<int> values = List.filled(barGraphLength, 0);
  for (var i = 0; i < barGraphLength; i++) {
    if (i < allValues.length) {
      values[i] = allValues[i];
    }
  }

  List<BarChartGroupData> barChartGroupData = [
    BarChartGroupData(
      x: 0,
      barRods: [
        BarChartRodData(
          toY: values[0].toDouble(),
          color: (opState ? FlutterFlowTheme.of(context).alternate : Colors.transparent),
          borderSide: BorderSide(color: FlutterFlowTheme.of(context).alternate, width: 4),
          width: 36,
          borderRadius: BorderRadius.circular(6),
        ),
      ],
      showingTooltipIndicators: [0],
    ),
  ];

  for (var i = 1; i < barGraphLength; i++) {
    barChartGroupData.add(
      BarChartGroupData(
        x: i,
        barRods: [
          BarChartRodData(
            toY: values[i].toDouble(),
            color: FlutterFlowTheme.of(context).primary,
            borderSide: BorderSide(color: FlutterFlowTheme.of(context).primary, width: 4),
            width: 36,
            borderRadius: BorderRadius.circular(6),
          ),
        ],
        showingTooltipIndicators: [0],
      ),
    );
  }

  return Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height * 0.2,
      decoration: BoxDecoration(
        color: FlutterFlowTheme.of(context).pacificCyan,
      ),
      child: BarChart(
            BarChartData(
              titlesData: FlTitlesData(show: false),
              minY: 0,
              maxY: 100,
              gridData: FlGridData(show: false),
              borderData: FlBorderData(show: false),
              barGroups: barChartGroupData,
              barTouchData: BarTouchData(
                  enabled: false,
                  touchTooltipData: BarTouchTooltipData(
                    tooltipBgColor: Colors.transparent,
                    tooltipMargin: 6,
                    getTooltipItem: (
                      BarChartGroupData group,
                      int groupIndex,
                      BarChartRodData rod,
                      int rodIndex,
                    ) {
                      return BarTooltipItem(
                        rod.toY.round().toString(),
                        FlutterFlowTheme.of(context).bodyMedium,
                      );
                    },
                  )),
            ),
          ));
}

Screenshots image The container is painted in cyan. Note that the bottom lines overflow on all bars as well as on the top of the column to the left.

Versions Flutter version 3.19.0 fl_chart: 0.66.2

clemens- avatar Feb 19 '24 14:02 clemens-

Please provide a full reproducible code (a main.dart file content)

imaNNeo avatar Feb 28 '24 23:02 imaNNeo

are you solve

RashkaDahir avatar Apr 01 '24 08:04 RashkaDahir

@RashkaDahir I found a workaround: I just limit the maximum height of my data to a suitable value. This works until it does not and is probably not the best option. But I did not find any other solution.

  List<BarChartGroupData> barChartGroupData = [
    BarChartGroupData(
      x: 0,
      barRods: [
        BarChartRodData(
          toY: values[0] <= 100 ? values[0].toDouble() : 100,
          color: (opState ? FlutterFlowTheme.of(context).alternate : Colors.transparent),
          borderSide: BorderSide(color: FlutterFlowTheme.of(context).alternate, width: 3),
          width: 36,
          borderRadius: BorderRadius.circular(6),
        ),
      ],
      showingTooltipIndicators: [0],
    ),
  ];

@imaNNeo I cannot post my complete code.

clemens- avatar Apr 01 '24 13:04 clemens-

You can attach a file (preferably a main.dart file)

imaNNeo avatar May 09 '24 00:05 imaNNeo