flutter doughnut chart has hugh margin/padding. how to remove? - flutter

After i put the chart there is a high margin/padding.
How to I reduce the space between the chart? or put it to zero?
Center(
child: Container(
child: SfCircularChart(
margin: EdgeInsets.zero,
annotations: <CircularChartAnnotation>[
CircularChartAnnotation(
widget: Container(
child: Text(
rating.toString(),
style: TextStyle(fontSize: 32),
),
)
)
],
series: <CircularSeries>[
DoughnutSeries<ChartData, String>(
dataSource: [
ChartData('Score', rating.toDouble(), Theme.of(context).primaryColor),
ChartData('-Score', (100 - rating).toDouble(), Colors.grey.shade900)
],
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y,
pointColorMapper:(ChartData data, _) => data.color,
radius: '50%',
innerRadius: '80%',
strokeColor: Colors.amber
)
]
)
),
);

I suggest you to add a width and height for the container that hold all the chart widget.
Center(
child: Container(
// add here
width: 100, // for example
height: 100, // for example
child: SfCircularChart(
margin: EdgeInsets.zero,
annotations: <CircularChartAnnotation>[
CircularChartAnnotation(
widget: Container(
child: Text(
rating.toString(),
style: TextStyle(fontSize: 32),
),
)
)
],
series: <CircularSeries>[
DoughnutSeries<ChartData, String>(
dataSource: [
ChartData('Score', rating.toDouble(), Theme.of(context).primaryColor),
ChartData('-Score', (100 - rating).toDouble(), Colors.grey.shade900)
],
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y,
pointColorMapper:(ChartData data, _) => data.color,
radius: '50%',
innerRadius: '80%',
strokeColor: Colors.amber
)
]
)
),
);

Related

How to give color to chart

I want to make a chart with color like below:
But, this is my first time to make a chart, I only know to give a single color, like mine below:
Here I attach the code of my chart:
Padding(
padding: const EdgeInsets.only(
bottom: 28,
left: 40,
right: 40,
),
child: SizedBox(
width: cardWidth,
child: SfCartesianChart(
margin: EdgeInsets.zero,
legend: Legend(
position: LegendPosition.top,
isVisible: true,
textStyle: body1(),
),
tooltipBehavior: TooltipBehavior(enable: true),
primaryXAxis: CategoryAxis(
labelStyle: body2(),
),
primaryYAxis: NumericAxis(labelStyle: body2()),
series: <ChartSeries<dynamic, dynamic>>[
SplineAreaSeries<ChartData, String>(
color: ColorName.brandPrimaryBlue,
dataSource: chartData,
xValueMapper: (ChartData data, _) => data.data,
yValueMapper: (ChartData data, _) =>
data.totalTruck,
name: 'Truck',
markerSettings: const MarkerSettings(
isVisible: false,
color: ColorName.brandPrimaryBlue,
),
),
],
),
),
),
Any advice would be appreciated.
you can use gradient property of SplineAreaSeries like this:
SplineAreaSeries<SalesData, String>(
gradient: const LinearGradient(
colors: <Color>[
Color.fromARGB(80, 9, 59, 167),
Color(0xFF012168),
],
stops: <double>[0.2, 0.7],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
),
dataSource: <SalesData>[
SalesData('Jan', 35),
SalesData('Feb', 28),
SalesData('Mar', 34),
SalesData('Apr', 32),
SalesData('May', 40)
],
xValueMapper: (SalesData sales, _) =>
sales.year,
yValueMapper: (SalesData sales, _) =>
sales.sales,
// Enable data label
dataLabelSettings:
DataLabelSettings(isVisible: true),
markerSettings: const MarkerSettings(
isVisible: false,
color: Colors.red,
),
)
which will give you results like this:
which you can customize by chagning the colors values etc according to your choice.

Flutter: how to makeline chart inside a sized box?

i'm trying to make a percent indicator and a line chart inside a sized box like this
but when i add the line chart not compact to the sized box,
it shows error in the sized box with an error with the widht, i'm fairly new to flutter this is my first time using a chart.
Code :
import 'package:flutter/material.dart';
import 'package:percent_indicator/circular_percent_indicator.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:fl_chart/fl_chart.dart';
class Data5 extends StatelessWidget {
const Data5({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 100,
height: 270,
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [Linechart()],
),
),
);
}
}
class Circular extends StatelessWidget {
const Circular({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(8.0),
child: CircularPercentIndicator(
radius: 100,
lineWidth: 15.0,
percent: 1.00,
center: Text(
"TF",
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 40),
),
progressColor: Color.fromRGBO(37, 150, 190, 1),
),
);
}
}
class Linechart extends StatelessWidget {
const Linechart({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
final List<SalesData> chartData = [
SalesData("Jan", 0),
SalesData("Feb", 0),
SalesData("Mar", 0),
SalesData("Apr", 0),
SalesData("May", 3),
SalesData("June", 10),
SalesData("Jul", 85),
SalesData("Aug", 25),
SalesData("Sept", 5),
SalesData("Oct", 2),
SalesData("Nov", 0),
SalesData("Dec", 0)
];
return Padding(
padding: EdgeInsets.all(8.0),
child: Center(
child: Container(
child: SfCartesianChart(
primaryXAxis: DateTimeAxis(),
series: <ChartSeries>[
// Renders line chart
LineSeries<SalesData, String>(
dataSource: chartData,
xValueMapper: (SalesData sales, _) => sales.Month,
yValueMapper: (SalesData sales, _) => sales.sales)
]))));
}
}
class SalesData {
SalesData(this.Month, this.sales);
final String Month;
final int sales;
}
Here is updated class, You can change lots of thing with a little research on your Syncfunction charts.
Container(
height: 200,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
),
padding: const EdgeInsets.all(8.0),
child: Center(
child: Row(
children: [
const CircleAvatar(
radius: 30,
backgroundColor: Colors.green,
child: CircleAvatar(
radius: 22,
backgroundColor: Colors.white,
child: Text('TF'),
)),
Container(
child: SfCartesianChart(
title: ChartTitle(
text: 'TREND EVENT 2022',
alignment: ChartAlignment.near,
textStyle: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 16,
)),
primaryXAxis: CategoryAxis(),
primaryYAxis: NumericAxis(maximum: 100),
tooltipBehavior: TooltipBehavior(enable: true),
series: <ChartSeries>[
LineSeries<SalesData, String>(
markerSettings: const MarkerSettings(isVisible: true),
enableTooltip: true,
dataSource: chartData,
xValueMapper: (SalesData sales, _) => sales.Month,
yValueMapper: (SalesData sales, _) => sales.sales)
])),
],
)))
child: Container(
decoration: const BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(10.0),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(child: Linechart() ) // wrap your line chart with expanded
],
),
),

flutter UI Problem: How to show DoughnutSeries data like this?

Here I want My UI like Second Image , I want to show DoughnutSeries data like below DoughnutSeries circle in second Image.
This is my syncfusion DoughnutSeries data ui code.
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import '../../../../Constants/constants.dart';
class FSSectorAllocationPage extends StatefulWidget {
const FSSectorAllocationPage({Key? key}) : super(key: key);
#override
_FSSectorAllocationPageState createState() => _FSSectorAllocationPageState();
}
class _FSSectorAllocationPageState extends State<FSSectorAllocationPage> {
final List<ChartData> chartData = [
ChartData('Healthcare', 25.5, Color.fromRGBO(9, 0, 136, 1)),
ChartData('Education', 38, Color.fromRGBO(147, 0, 119, 1)),
ChartData('Power', 34, Color.fromRGBO(228, 0, 124, 1)),
ChartData('Manufacture', 52, greyColor),
ChartData('Agriculture', 52, greenColor),
ChartData('Services', 52, orangeColor),
ChartData('Others', 52, green2Color),
];
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
child: SfCircularChart(
legend: Legend(
isVisible: true,
// width: "130",
position: LegendPosition.bottom,
overflowMode: LegendItemOverflowMode.wrap,
legendItemBuilder: (String name, dynamic series,
dynamic point, int index) {
return Container(color: Colors.red,
width: double.infinity,
child: Padding(
padding: const EdgeInsets.only(left: 20.0,right: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: point.color),
height: 15,
width: 15,
),
Padding(
padding:
const EdgeInsets.fromLTRB(9.0, 8, 15, 9),
child: Text(
point.x.toString(),
style: TextStyle(
color: blackColor,
fontSize: tSize13,
fontWeight: FontWeight.w500),
textAlign: TextAlign.left,
),
),
],
),
Padding(
padding: const EdgeInsets.fromLTRB(9.0, 8, 15, 9),
child: Row(
children: [
Text(
point.y.toString(),
style: TextStyle(
color: blackColor,
fontSize: tSize13,
fontWeight: FontWeight.w500),
),
Text(
"%",
style: TextStyle(
color: blackColor,
fontSize: tSize13,
fontWeight: FontWeight.w500),
),
],
),
),
],
),
),
);
}),
series: <CircularSeries>[
DoughnutSeries<ChartData, String>(
dataSource: chartData,
pointColorMapper: (ChartData data, _) => data.color,
xValueMapper: (ChartData data, _) => data.x,
yValueMapper: (ChartData data, _) => data.y,
innerRadius: '60%'),
],
annotations: <CircularChartAnnotation>[
CircularChartAnnotation(
widget: Container(
child: Text(
'Sectors',
style: TextStyle(
fontSize: tSize16, fontWeight: FontWeight.w500),
)),
)
])),
);
}
}
class ChartData {
ChartData(this.x, this.y, [this.color]);
final String x;
final double y;
final Color? color;
}
This is my ui which I created
Here my DoughnutSeries data height is not increasing ,I am not understanding how to increase that height.
Here My Design , I want to make like this UI.
I bet you are looking for something like Pie & Donut Charts,
Column(
children: [
SizedBox(
width: 250, // Preferred width
height: 250, // Preferred height
child: FittedBox(
fit: BoxFit.scaleDown,
DChartPie(...),
),
),
Expanded(
child: ListView(
children: [...],
),
),
],
),

i was reload chart SfRangeSelector start with wrong in flutter

first time was working normal.i was reload chart SfRangeSelector start with wrong.i will check the values with help of debugger the values also correct.help me solve this bug .i add error screenshot in below.i have added code snippet for reference.
SfRangeSelectorTheme(
data: SfRangeSelectorThemeData(),
child:SfRangeSelector(
min: _min0,
max: _max0,
interval:15,
showLabels: true,
showTicks: false,
enableDeferredUpdate: false,
deferredUpdateDelay: 1000,
enableIntervalSelection: true,
dateFormat:DateFormat.Hm(),
dateIntervalType:DateIntervalType.minutes,
dragMode: SliderDragMode.both,
startThumbIcon: const Icon(
Icons.arrow_back_ios,
color: Colors.blue,
size: 20.0),
endThumbIcon: const Icon(
Icons.arrow_forward_ios,
color: Colors.blue,
size: 20.0),
// inactiveColor: Color.fromARGB(255,5, 90, 194),
activeColor: Color.fromARGB(255, 126, 184, 253),
labelPlacement: LabelPlacement.betweenTicks,
initialValues: _values0,
controller: _rangeController0,
onChanged: (SfRangeValues values) {
setState(() { });
},
child: Container(
color: Colors.blue[200],
height: 40,
padding: EdgeInsets.zero,
margin: EdgeInsets.zero,
child: SfCartesianChart(
primaryXAxis: DateTimeAxis(
minimum: _min0,
maximum: _max0,
intervalType:DateTimeIntervalType.minutes,
isVisible: false,
),
primaryYAxis: NumericAxis(
isVisible: false),
plotAreaBorderWidth: 0,
series: <SplineSeries<Data, DateTime>>[
SplineSeries<Data, DateTime>(
// borderColor: const Color.fromRGBO(0, 193, 187, 1),
color: Colors.blue,
dataSource:chartData,
xValueMapper: (Data sales, _) => sales.x ,
yValueMapper: (Data sales, _) => sales.y)
],
),
),
)

How to apply linear gradient in flutter charts?

How to apply linear gradient in the flutter charts?
I have tried with colors class but i am able to apply only a solid color and unable to find a way for using gradient in the graph bars.
chart.Series<Person,String>(
id:"Person3",
colorFn:(_,__)=>chart.MaterialPalette.red.shadeDefault,
domainFn: (Person dataPoint, _)=>dataPoint.name,
measureFn: (Person dataPoint, _)=>dataPoint.no,
data:data2,
)
Provide me some way to apply linear gradient
Apparently there is no such functionality yet. I managed to get it working with the following code:
Widget _buildChart() {
return Container(
height: 300,
child: ShaderMask(
child: charts.BarChart(
_createRandomData(),
animate: true,
primaryMeasureAxis: new charts.NumericAxisSpec(
renderSpec: new charts.NoneRenderSpec(), showAxisLine: false),
domainAxis: new charts.OrdinalAxisSpec(
showAxisLine: false, renderSpec: new charts.NoneRenderSpec()),
layoutConfig: new charts.LayoutConfig(
leftMarginSpec: new charts.MarginSpec.fixedPixel(0),
topMarginSpec: new charts.MarginSpec.fixedPixel(0),
rightMarginSpec: new charts.MarginSpec.fixedPixel(0),
bottomMarginSpec: new charts.MarginSpec.fixedPixel(0)),
defaultRenderer: new charts.BarRendererConfig(
cornerStrategy: const charts.ConstCornerStrategy(30)),
),
shaderCallback: (Rect bounds) {
return LinearGradient(
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
colors: [Color(0xFF51DE93), Color(0xFFFFB540), Color(0xFFFA4169)],
stops: [
0.0,
0.5,
1.0,
],
).createShader(bounds);
},
blendMode: BlendMode.srcATop,
)
);
}
Result:
An Example of AreaSeries chart
#override
Widget build(BuildContext context) {
final List<Color> color = <Color>[];
color.add(Colors.blue[50]);
color.add(Colors.blue[100]);
color.add(Colors.blue);
final List<double> stops = <double>[];
stops.add(0.0);
stops.add(0.5);
stops.add(1.0);
final LinearGradient gradientColors =
LinearGradient(colors: color, stops: stops);
return Container(
width: double.infinity,
height: 250,
child: SfCartesianChart(
primaryXAxis: CategoryAxis(),
series: <AreaSeries<SalesData, String>>[
AreaSeries<SalesData, String>(
dataSource: <SalesData>[
SalesData('Jan', 85),
SalesData('Feb', 58),
SalesData('Mar', 64),
SalesData('Apr', 62),
SalesData('May', 78),
SalesData('Jun', 92),
SalesData('Jul', 90),
],
xValueMapper: (SalesData sales, _) => sales.year,
yValueMapper: (SalesData sales, _) => sales.sales,
gradient: gradientColors,
),
],
),
);
}
Use the property BoxDecoration of Container can implement it , like this :
#override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
height: 370,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors(0XFFA573FF),
blurRadius: 10,
offset: Offset(2, 3)),
],
borderRadius: BorderRadius.all(Radius.circular(18)),
gradient: LinearGradient(colors: [
color: Colors(0XFFA573FF),
color: Colors(0XFF645AFF),
], stops: [
0.35,
1
], begin: Alignment.topLeft, end: Alignment.bottomRight)),
child:
SizedBox(
height: 280,
width: double.infinity,
child: StackedAreaCustomColorLineChart(
StackedAreaCustomColorLineChart._createSampleData())
// replace child with your chart here
),
);
}