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.
Related
I have SfCartesianChart and I want to make a bottom line dashed. How to make it?
This is my code:
SfCartesianChart(
plotAreaBorderWidth: 0,
plotAreaBorderColor: Colors.white24,
primaryXAxis: DateTimeAxis(
majorGridLines: const MajorGridLines(
width: 0.5,
color: Colors.transparent,
),
),
primaryYAxis: NumericAxis(
majorGridLines: const MajorGridLines(width: 1, color: Colors.white24, dashArray: <double>[5, 5]),
minimum: 0,
maximum: 100
),
)
Have you tried playing with the dashArray property on the AxisLine? I.e.:
primaryXAxis: DateTimeAxis(
majorGridLines: const MajorGridLines(
width: 0.5,
color: Colors.transparent,
),
axisLine: AxisLine(
color: Colors.red,
dashArray: <double>[2.0, 1.0],
),
),
I have implemented a linear graph using the below code. I use charts flutter package.. the first image shows the graph I created. the second image shows what I need to be implemented I want to add another two lines for the same graph with different data values. how can I do this? appreciate your help on this. I use hardcoded values for the graph.
developer_series.dart
import 'package:charts_flutter/flutter.dart' as charts;
class DeveloperSeries {
final int day;
final int calories;
final charts.Color barColor;
DeveloperSeries(
{
required this.day,
required this.calories,
required this.barColor
}
);
}
developer_chart.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'developer_series.dart';
class DeveloperChart extends StatelessWidget {
final List<DeveloperSeries> data;
DeveloperChart({required this.data});
#override
Widget build(BuildContext context) {
List<charts.Series<DeveloperSeries,int>> series = [
charts.Series(
id: "calories",
data: data,
domainFn: (DeveloperSeries series, _) => series.day,
measureFn: (DeveloperSeries series, _) => series.calories,
colorFn: (DeveloperSeries series, _) => series.barColor
)
];
return Container(
height: 700,
// padding: EdgeInsets.all(25),
// decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(40),
//
// ),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30.0),
),
color: Colors.grey.withOpacity(0.4),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only( top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
height: 30,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
gradient: LinearGradient(
colors: [
Color.fromRGBO(125, 158, 205, 1.0),
Color.fromRGBO(158, 125, 243, 0.7490196078431373),
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
child: TextButton(onPressed:() {},
child: const Text('Daily', style: TextStyle(color: Colors.white),),
),
),
Container(
height: 30,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
gradient: LinearGradient(
colors: [
Color.fromRGBO(125, 158, 205, 1.0),
Color.fromRGBO(158, 125, 243, 0.7490196078431373),
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
child: TextButton(onPressed:() {},
child: const Text('Week', style: TextStyle(color: Colors.white),),
),
),
Container(
height: 30,
width: 80,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
gradient: LinearGradient(
colors: [
Color.fromRGBO(125, 158, 205, 1.0),
Color.fromRGBO(158, 125, 243, 0.7490196078431373),
],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
child: TextButton(
onPressed:() {},
child: const Text('Month', style: TextStyle(color: Colors.white),),
),
),
],
),
),
Expanded(
child: charts.LineChart(series,
animate: true,
domainAxis: const charts.NumericAxisSpec(
renderSpec: charts.GridlineRendererSpec(
labelStyle: charts.TextStyleSpec(
fontSize: 10,
color: charts.MaterialPalette.white,
),
lineStyle: charts.LineStyleSpec(
color: charts.MaterialPalette.transparent,
)),
tickProviderSpec:
charts.BasicNumericTickProviderSpec(zeroBound: false),
// viewport: charts.NumericExtents(2016.0, 2022.0),
),
primaryMeasureAxis: new charts.NumericAxisSpec(
renderSpec: charts.GridlineRendererSpec(
// labelOffsetFromAxisPx: -20,
// labelAnchor: charts.TickLabelAnchor.after,
lineStyle: charts.LineStyleSpec(
color: charts.MaterialPalette.white,
thickness: 1,
)
),
),
),
),
],
),
),
),
);
}
}
graph_calories.dart
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;
import 'developer_chart.dart';
import 'developer_series.dart';
class GraphDisplayCalories extends StatefulWidget {
#override
State<GraphDisplayCalories> createState() => _GraphDisplayCaloriesState();
}
class _GraphDisplayCaloriesState extends State<GraphDisplayCalories> {
final List<DeveloperSeries> data = [
DeveloperSeries(
day: 2017,
calories: 40000,
barColor: charts.ColorUtil.fromDartColor(Colors.purple),
),
DeveloperSeries(
day: 2018,
calories: 10000,
barColor: charts.ColorUtil.fromDartColor(Colors.purple),
),
DeveloperSeries(
day:2019,
calories: 20000,
barColor: charts.ColorUtil.fromDartColor(Colors.purple),
),
DeveloperSeries(
day: 2020,
calories: 35000,
barColor: charts.ColorUtil.fromDartColor(Colors.purple),
),
DeveloperSeries(
day: 2021,
calories: 20000,
barColor: charts.ColorUtil.fromDartColor(Colors.purple),
),
];
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.transparent,
body: Center(
child: DeveloperChart(
data: data,
)
),
);
}
}
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
)
]
)
),
);
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 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
),
);
}