Skip to content

Commit

Permalink
Add BarChart sample 7
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Mar 24, 2022
1 parent 76eb4e3 commit a6a0d49
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 14 deletions.
5 changes: 5 additions & 0 deletions example/lib/bar_chart/bar_chart_page3.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:example/bar_chart/samples/bar_chart_sample6.dart';
import 'package:example/bar_chart/samples/bar_chart_sample7.dart';
import 'package:flutter/material.dart';

import '../bar_chart/samples/bar_chart_sample3.dart';
Expand All @@ -15,6 +16,10 @@ class BarChartPage3 extends StatelessWidget {
body: ListView(
padding: const EdgeInsets.all(24),
children: const <Widget>[
BarChartSample7(),
SizedBox(
height: 18,
),
BarChartSample3(),
SizedBox(
height: 18,
Expand Down
227 changes: 227 additions & 0 deletions example/lib/bar_chart/samples/bar_chart_sample7.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;

class BarChartSample7 extends StatefulWidget {
const BarChartSample7({Key? key}) : super(key: key);

static const shadowColor = Color(0xFFCCCCCC);
static const dataList = [
_BarData(Color(0xFFecb206), 18, 18),
_BarData(Color(0xFFa8bd1a), 17, 8),
_BarData(Color(0xFF17987b), 10, 15),
_BarData(Color(0xFFb87d46), 2.5, 5),
_BarData(Color(0xFF295ab5), 2, 2.5),
_BarData(Color(0xFFea0107), 2, 2),
];

@override
State<BarChartSample7> createState() => _BarChartSample7State();
}

class _BarChartSample7State extends State<BarChartSample7> {
BarChartGroupData generateBarGroup(
int x,
Color color,
double value,
double shadowValue,
) {
return BarChartGroupData(
x: x,
barRods: [
BarChartRodData(
toY: value,
color: color,
width: 6,
),
BarChartRodData(
toY: shadowValue,
color: BarChartSample7.shadowColor,
width: 6,
),
],
showingTooltipIndicators: touchedGroupIndex == x ? [0] : [],
);
}

int touchedGroupIndex = -1;

@override
Widget build(BuildContext context) {
return Card(
color: Colors.white,
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
AspectRatio(
aspectRatio: 1.4,
child: BarChart(
BarChartData(
alignment: BarChartAlignment.spaceBetween,
borderData: FlBorderData(
show: true,
border: const Border.symmetric(
horizontal: BorderSide(
color: Color(0xFFececec),
),
),
),
titlesData: FlTitlesData(
show: true,
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 30,
getTitles: (value, meta) {
return Text(
value.toInt().toString(),
style: const TextStyle(
color: Color(0xFF606060),
),
textAlign: TextAlign.left,
);
},
),
),
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 36,
getTitles: (value, meta) {
final index = value.toInt();
return Padding(
padding: const EdgeInsets.only(top: 8.0),
child: _IconWidget(
color: BarChartSample7.dataList[index].color,
isSelected: touchedGroupIndex == index,
),
);
},
),
),
rightTitles: AxisTitles(),
topTitles: AxisTitles(),
),
gridData: FlGridData(
show: true,
drawVerticalLine: false,
getDrawingHorizontalLine: (value) => FlLine(
color: const Color(0xFFececec),
dashArray: null,
strokeWidth: 1,
),
),
barGroups: BarChartSample7.dataList.asMap().entries.map((e) {
final index = e.key;
final data = e.value;
return generateBarGroup(
index, data.color, data.value, data.shadowValue);
}).toList(),
maxY: 20,
barTouchData: BarTouchData(
enabled: true,
handleBuiltInTouches: false,
touchTooltipData: BarTouchTooltipData(
tooltipBgColor: Colors.transparent,
tooltipMargin: 0,
getTooltipItem: (
BarChartGroupData group,
int groupIndex,
BarChartRodData rod,
int rodIndex,
) {
return BarTooltipItem(
rod.toY.toString(),
TextStyle(
fontWeight: FontWeight.bold,
color: rod.color!,
fontSize: 18,
shadows: const [
Shadow(
color: Colors.black26,
blurRadius: 12,
)
]),
);
}),
touchCallback: (event, response) {
if (event.isInterestedForInteractions &&
response != null &&
response.spot != null) {
setState(() {
touchedGroupIndex =
response.spot!.touchedBarGroupIndex;
});
} else {
setState(() {
touchedGroupIndex = -1;
});
}
},
),
),
),
),
],
),
),
);
}
}

class _BarData {
final Color color;
final double value;
final double shadowValue;

const _BarData(this.color, this.value, this.shadowValue);
}

class _IconWidget extends ImplicitlyAnimatedWidget {
final Color color;
final bool isSelected;

const _IconWidget({
required this.color,
required this.isSelected,
}) : super(duration: const Duration(milliseconds: 300));

@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() =>
_IconWidgetState();
}

class _IconWidgetState extends AnimatedWidgetBaseState<_IconWidget> {
Tween<double>? _rotationTween;

@override
Widget build(BuildContext context) {
final rotation = math.pi * 4 * _rotationTween!.evaluate(animation);
final scale = 1 + _rotationTween!.evaluate(animation) * 0.5;
return Transform(
transform: Matrix4.rotationZ(rotation).scaled(scale, scale),
origin: const Offset(14, 14),
child: Icon(
widget.isSelected ? Icons.face_retouching_natural : Icons.face,
color: widget.color,
size: 28,
),
);
}

@override
void forEachTween(TweenVisitor<dynamic> visitor) {
_rotationTween = visitor(
_rotationTween,
widget.isSelected ? 1.0 : 0.0,
(dynamic value) => Tween<double>(
begin: value,
end: widget.isSelected ? 1.0 : 0.0,
),
) as Tween<double>;
}
}
4 changes: 2 additions & 2 deletions lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {

List<GroupBarsPosition>? _groupBarsPosition;

/// Paints [data] into canvas, it is the animating [BarChartData],
/// Paints [dataList] into canvas, it is the animating [BarChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [data] is changing constantly.
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chart/base/axis_chart/axis_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'axis_chart_data.dart';
/// This class is responsible to draw the grid behind all axis base charts.
/// also we have two useful function [getPixelX] and [getPixelY] that used
/// in child classes -> [BarChartPainter], [LineChartPainter]
/// [data] is the currently showing data (it may produced by an animation using lerp function),
/// [dataList] is the currently showing data (it may produced by an animation using lerp function),
/// [targetData] is the target data, that animation is going to show (if animating)
abstract class AxisChartPainter<D extends AxisChartData>
extends BaseChartPainter<D> {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/chart/line_chart/line_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class LineChartBarData with EquatableMixin {
///
/// [show] determines the drawing, if set to false, it draws nothing.
///
/// [colors] determines the color of drawing line, if one color provided it applies a solid color,
/// [mainColors] determines the color of drawing line, if one color provided it applies a solid color,
/// otherwise it gradients between provided colors for drawing the line.
/// Gradient happens using provided [colorStops], [gradientFrom], [gradientTo].
/// if you want it draw normally, don't touch them,
Expand Down Expand Up @@ -722,7 +722,7 @@ bool showAllSpotsBelowLine(FlSpot spot) {
/// It should return a [Color] that needs to be used for drawing target.
typedef GetDotColorCallback = Color Function(FlSpot, double, LineChartBarData);

/// If there is one color in [LineChartBarData.colors], it returns that color,
/// If there is one color in [LineChartBarData.mainColors], it returns that color,
/// otherwise it returns the color along the gradient colors based on the [xPercentage].
Color _defaultGetDotColor(FlSpot _, double xPercentage, LineChartBarData bar) {
if (bar.gradient != null && bar.gradient is LinearGradient) {
Expand All @@ -735,7 +735,7 @@ Color _defaultGetDotColor(FlSpot _, double xPercentage, LineChartBarData bar) {
return bar.gradient?.colors.first ?? bar.color ?? Colors.blueGrey;
}

/// If there is one color in [LineChartBarData.colors], it returns that color in a darker mode,
/// If there is one color in [LineChartBarData.mainColors], it returns that color in a darker mode,
/// otherwise it returns the color along the gradient colors based on the [xPercentage] in a darker mode.
Color _defaultGetDotStrokeColor(
FlSpot spot, double xPercentage, LineChartBarData bar) {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chart/line_chart/line_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class LineChartPainter extends AxisChartPainter<LineChartData> {
_bgTouchTooltipPaint,
_imagePaint;

/// Paints [data] into canvas, it is the animating [LineChartData],
/// Paints [dataList] into canvas, it is the animating [LineChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [data] is changing constantly.
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chart/pie_chart/pie_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import 'pie_chart_data.dart';
class PieChartPainter extends BaseChartPainter<PieChartData> {
late Paint _sectionPaint, _sectionStrokePaint, _centerSpacePaint;

/// Paints [data] into canvas, it is the animating [PieChartData],
/// Paints [dataList] into canvas, it is the animating [PieChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [data] is changing constantly.
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chart/radar_chart/radar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class RadarChartPainter extends BaseChartPainter<RadarChartData> {

List<RadarDataSetsPosition>? dataSetsPosition;

/// Paints [data] into canvas, it is the animating [RadarChartData],
/// Paints [dataList] into canvas, it is the animating [RadarChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [data] is changing constantly.
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chart/scatter_chart/scatter_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class ScatterChartPainter extends AxisChartPainter<ScatterChartData> {
/// [_spotsPaint] is responsible to draw scatter spots
late Paint _spotsPaint, _bgTouchTooltipPaint;

/// Paints [data] into canvas, it is the animating [ScatterChartData],
/// Paints [dataList] into canvas, it is the animating [ScatterChartData],
/// [targetData] is the animation's target and remains the same
/// during animation, then we should use it when we need to show
/// tooltips or something like that, because [data] is changing constantly.
/// tooltips or something like that, because [dataList] is changing constantly.
///
/// [textScale] used for scaling texts inside the chart,
/// parent can use [MediaQuery.textScaleFactor] to respect
Expand Down
4 changes: 4 additions & 0 deletions repo_files/documentations/bar_chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ enum values {`start`, `end`, `center`, `spaceEvenly`, `spaceAround`, `spaceBetwe
##### Sample 6 ([Source Code](/example/lib/bar_chart/samples/bar_chart_sample6.dart))
<img src="https://github.com/imaNNeoFighT/fl_chart/raw/master/repo_files/images/bar_chart/bar_chart_sample_6.png" width="300" >

##### Sample 6 ([Source Code](/example/lib/bar_chart/samples/bar_chart_sample7.dart))
<img src="https://github.com/imaNNeoFighT/fl_chart/raw/master/repo_files/images/bar_chart/bar_chart_sample_7.gif" width="300" >



##### Gist - Toggleable Tooltip ([Source Code](https://gist.github.com/imaNNeoFighT/bce3f0169ff3fd6c3f137cdeb5005c0e))
https://user-images.githubusercontent.com/7009300/156784816-53f95dd9-f387-4600-8a92-d05b1aeea3da.mov
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a6a0d49

Please sign in to comment.