Back to Fl Chart

Bar Chart

repo_files/documentations/bar_chart.md

1.2.013.9 KB
Original Source

<a href="https://www.youtube.com/watch?v=vYe0RY1nCAA&list=PL1-_rCwRcnbNpvodmbt43O81wMUdBv8-a"></a>

How to use

dart
BarChart(
  BarChartData(
    // read about it in the BarChartData section
  ),
  duration: Duration(milliseconds: 150), // Optional
  curve: Curves.linear, // Optional
);

Implicit Animations

When you change the chart's state, it animates to the new state internally (using implicit animations). You can control the animation duration and curve using optional duration and curve properties, respectively.

BarChartData

PropNameDescriptiondefault value
barGroupslist of BarChartGroupData to show the bar lines together, you can provide one item per group to show normal bar chart[]
groupsSpacespace between groups, it applies only when the alignment is BarChartAlignment.start, BarChartAlignment.center or BarChartAlignment.end16
alignmenta BarChartAlignment that determines the alignment of the barGroups, inspired by Flutter MainAxisAlignmentBarChartAlignment.spaceEvenly
titlesDatacheck the FlTitlesDataFlTitlesData()
axisTitleDatacheck the FlAxisTitleDataFlAxisTitleData()
rangeAnnotationsshow range annotations behind the chart, check RangeAnnotationsRangeAnnotations()
backgroundColora background color which is drawn behind the chartnull
barTouchDataBarTouchData holds the touch interactivity detailsBarTouchData()
gridDatacheck the FlGridDataFlGridData()
borderDatacheck the FlBorderDataFlBorderData()
maxYgets maximum y of y axis, if null, value will be read from the input barGroups (But it is more performant if you provide them)null
minYgets minimum y of y axis, if null, value will be read from the input barGroups (But it is more performant if you provide them)null
baselineYdefines the baseline of y-axis0
extraLinesDataallows extra horizontal lines to be drawn on the chart. Vertical lines are ignored when used with BarChartData, please see #1149, check ExtraLinesDataExtraLinesData()
rotationQuarterTurnsRotates the chart 90 degrees (clockwise) in every quarter turns. This feature works like the RotatedBox widget. You can have horizontal BarChart by changing this value to0
errorIndicatorDataHolds data for representing an error indicator (you see the error indicators if you provide the toYErrorRange in the BarChartRodData)ErrorIndicatorData()

BarChartGroupData

PropNameDescriptiondefault value
xx position of the group on horizontal axisnull
barRodslist of BarChartRodData that are a bar line[]
barsSpacethe space between barRods of the group2
showingTooltipIndicatorsindexes of barRods to show the tooltip on top of them, The point is that you need to disable touches to show these tooltips manually[]

BarChartAlignment

enum values {start, end, center, spaceEvenly, spaceAround, spaceBetween}

BarChartRodData

PropNameDescriptiondefault value
fromYPosition that this bar starts from0
toYThis rod is from fromY to toY in the vertical axisnull
colorcolor of the rod bar[Colors.cyan]
gradientYou can use any Gradient here. such as LinearGradient or RadialGradientnull
widthstroke width of the rod bar8
borderRadiusDetermines the edge rounding of the bar corners, see BorderRadius. When null, it defaults to completely round bars.null
borderDashArrayDetermines wether the border stroke is dashed. It is a circular array of dash offsets and lengths. For example, the array [5, 10] would result in dashes 5 pixels long followed by blank spaces 10 pixels long. The array [5, 10, 5] would result in a 5 pixel dash, a 10 pixel gap, a 5 pixel dash, a 5 pixel gap, a 10 pixel dash, etc.null
borderSideDetermines the border stroke around of the bar, see BorderSide. When null, it defaults to draw no stroke.null
backDrawRodDataif provided, draws a rod in the background of the line bar, check the BackgroundBarChartRodDatanull
rodStackItemif you want to have stacked bar chart, provide a list of BarChartRodStackItem, it will draw over your rod.[]
toYErrorRangeIf you want to show an error range on the rod, provide FlErrorRangenull
labela BarChartRodLabel to show a text label on the rodnull

BackgroundBarChartRodData

PropNameDescriptiondefault value
fromYsame as BarChartRodData's fromY0
toYsame as BarChartRodData's y8
showdetermines to show or hide this sectionfalse
colorsame as BarChartRodData's colors[Colors.blueGrey]
gradientsame as BarChartRodData's gradientnull

BarChartRodStackItem

PropNameDescriptiondefault value
fromYdraw stack item from this valuenull
toYdraw stack item to this valuenull
colorcolor of the stack itemnull
gradientgradient of the stack itemnull
labeloptional text label for the stack itemnull
labelStyleoptional TextStyle for the labelnull
borderSidedraw border stroke for each stack itemnull

BarChartRodLabel

Extends FlLabel.

PropNameDescriptiondefault value
showdetermines whether to show or hide the label (inherited from FlLabel)true
textthe text content of the label (inherited from FlLabel)''
styleTextStyle of the label. When null or when inherit is true, the style is merged with the ambient DefaultTextStyle and respects the platform's bold-text accessibility setting (inherited from FlLabel)null
anglerotation angle of the label in degrees (inherited from FlLabel)0
textDirectionTextDirection of the label text (inherited from FlLabel)TextDirection.ltr
offsetOffset from the rod tip to position the label. dx shifts horizontally, dy shifts verticallyOffset(0, 8)

BarTouchData (read about touch handling)

PropNameDescriptiondefault value
enableddetermines to enable or disable touch behaviorstrue
mouseCursorResolveryou can change the mouse cursor based on the provided FlTouchEvent and BarTouchResponseMouseCursor.defer
touchTooltipDataa BarTouchTooltipData, that determines how show the tooltip on top of touched spots (appearance of the showing tooltip bubble)BarTouchTooltipData()
touchExtraThresholdan EdgeInsets class to hold a bounding threshold of touch accuracyEdgeInsets.all(4)
allowTouchBarBackDrawif sets true, touch works on backdraw bar linefalse
handleBuiltInTouchesset this true if you want the built in touch handling (show a tooltip bubble and an indicator on touched spots)true
touchCallbacklisten to this callback to retrieve touch/pointer events and responses, it gives you a FlTouchEvent and BarTouchResponsenull
longPressDurationallows to customize the duration of the longPress gesture. If null, the duration of the longPressGesture is kLongPressTimeoutnull

BarTouchTooltipData

PropNameDescriptiondefault value
tooltipBorderborder of the tooltip bubbleBorderSide.none
tooltipBorderRadiusbackground corner radius of the tooltip bubbleBorderRadius.circular(4)
tooltipPaddingpadding of the tooltipEdgeInsets.symmetric(horizontal: 16, vertical: 8)
tooltipMarginmargin between the tooltip and the touched spot16
tooltipHorizontalAlignmenthorizontal alginment of tooltip relative to the barFLHorizontalAlignment.center
tooltipHorizontalOffsethorizontal offset of tooltip0
maxContentWidthmaximum width of the tooltip (if a text row is wider than this, then the text breaks to a new line120
getTooltipItemsa callback that retrieve BarTooltipItem by the given BarChartGroupData, groupIndex, BarChartRodData and rodIndexdefaultBarTooltipItem
fitInsideHorizontallyforces tooltip to horizontally shift inside the chart's bounding boxfalse
fitInsideVerticallyforces tooltip to vertically shift inside the chart's bounding boxfalse
directionControls showing tooltip on top or bottom, default is auto.auto
getTooltipColora callback that retrieves the Color for each rod separately from the given BarChartGroupData to set the background color of the tooltip bubbleColors.blueGrey.darken(15)

BarTooltipItem

PropNameDescriptiondefault value
texttext string of each row in the tooltip bubblenull
textStyleTextStyle of the showing text rownull
textAlignTextAlign of the showing text rowTextAlign.center
textDirectionTextDirection of the showing text rowTextDirection.ltr
childrenList<TextSpan> pass additional InlineSpan children for a more advance tooltipnull

BarTouchResponse

PropNameDescriptiondefault value
touchLocationthe location of the touch event in the device pixels coordinatesrequired
touchChartCoordinatethe location of the touch event in the chart coordinatesrequired
spota BarTouchedSpot class to hold data about touched spotnull

BarTouchedSpot

PropNameDescriptiondefault value
touchedBarGroupthe BarChartGroupData that user touched its rod's spotnull
touchedBarGroupIndexindex of touched barGroupnull
touchedRodDatathe BarChartRodData that user touched its spotnull
touchedRodDataIndexindex of touchedRodnull
touchedStackItemBarChartRodStackItem is the touched stack (if you have stacked bar chart)null
touchedStackItemIndexindex of barChartRodStackItem, -1 if nothing found-1

Some Samples


Sample 1 (Source Code)
Sample 2 (Source Code)
Sample 3 (Source Code)
Sample 4 (Source Code)
Sample 5 (Source Code)
Sample 6 (Source Code)
Sample 7 (Source Code)
Gist - Toggleable Tooltip (Source Code)

https://user-images.githubusercontent.com/7009300/156784816-53f95dd9-f387-4600-8a92-d05b1aeea3da.mov