Anilkumar Patel
Anilkumar Patel

Reputation: 2409

Flutter : How to set fix width of Bar chart Column with scrolling x axis

flutter_charts: ^0.1.10

https://pub.dev/packages/charts_flutter

https://pub.dev/documentation/charts_flutter/latest/flutter/BarChart-class.html

i Want set fix width of bar chart and need to scroll horizontal x axis.so it possible?

import 'package:charts_flutter/flutter.dart' as charts;

final data = [
      new OrdinalSales('2014', random.nextInt(100)),
      new OrdinalSales('2015', random.nextInt(100)),
      new OrdinalSales('2016', random.nextInt(100)),
      new OrdinalSales('2017', random.nextInt(100)),
      new OrdinalSales('2018', random.nextInt(100)),
      new OrdinalSales('2019', random.nextInt(100)),
      new OrdinalSales('2020', random.nextInt(100)),
      new OrdinalSales('2021', random.nextInt(100)),
      new OrdinalSales('2022', random.nextInt(100)),
      new OrdinalSales('2023', random.nextInt(100)),
    ];
...
@override
  Widget build(BuildContext context) {
    return new charts.BarChart(
      seriesList,
      animate: animate,
    );
  }
...

enter image description here

Upvotes: 4

Views: 7522

Answers (2)

Samuel Huff
Samuel Huff

Reputation: 698

You can do this by setting an initial viewport and adding the SlidingViewport and PanBehavior to your chart:

Widget build(BuildContext context) {

    final barsToDisplay = 3;

    return new charts.BarChart(
      seriesList,
      animate: animate,
      domainAxis: charts.DateTimeAxisSpec(
        viewport: charts.DateTimeExtents(
          start: DateTime(data.first.year),
          end: DateTime(data[barsToDisplay - 1].year),
        ),
      ),
      behaviors: [
        // Adding this behavior will allow tapping a bar to center it in the viewport
        charts.SlidingViewport(
          charts.SelectionModelType.action,
        ),
        charts.PanBehavior(),
      ],
    );
  }

Upvotes: 4

Ovidiu
Ovidiu

Reputation: 8714

Why not use your desired column width to calculate what the parent width should be? Wrap the chart with a SizedBox, wrapped with a horizontal SingleChildScrollView. Set the width of the SizedBox to eg. 1.5 * desiredColumnWidth * columnCount. I don't know the exact padding between the columns, but it visually seems to be about half a column width.

Upvotes: 5

Related Questions