[英]Set background color in flutter chart
I am using charts_flutter to build a bar chart, but having a hard time setting the background color.我正在使用 charts_flutter 构建条形图,但很难设置背景颜色。 The char is working fine, but I cannot seem to find the option to set the background color, which now for some reason is black (nowhere in the code the word "black" appears) Here's what I have so far:
char 工作正常,但我似乎找不到设置背景颜色的选项,现在由于某种原因是黑色(代码中没有“黑色”一词出现)这是我到目前为止的内容:
/// Bar chart example
// EXCLUDE_FROM_GALLERY_DOCS_START
import 'dart:math';
// EXCLUDE_FROM_GALLERY_DOCS_END
import 'package:charts_flutter/flutter.dart' as charts;
import 'package:flutter/material.dart';
class SimpleBarChart extends StatelessWidget {
final List<charts.Series<dynamic, String>> seriesList;
final bool animate;
SimpleBarChart(this.seriesList, {this.animate = false});
// EXCLUDE_FROM_GALLERY_DOCS_START
// This section is excluded from being copied to the gallery.
// It is used for creating random series data to demonstrate animation in
// the example app only.
factory SimpleBarChart.withRandomData() {
return new SimpleBarChart(_createRandomData());
}
/// Create random data.
static List<charts.Series<OrdinalSales, String>> _createRandomData() {
final random = new Random();
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)),
];
return [
new charts.Series<OrdinalSales, String>(
id: 'Sales',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (OrdinalSales sales, _) => sales.year,
measureFn: (OrdinalSales sales, _) => sales.sales,
data: data,
)
];
}
// EXCLUDE_FROM_GALLERY_DOCS_END
@override
Widget build(BuildContext context) {
return new charts.BarChart(
seriesList,
animate: animate,
);
}
}
/// Sample ordinal data type.
class OrdinalSales {
final String year;
final int sales;
OrdinalSales(this.year, this.sales);
}
Any advice?有什么建议吗?
Wrap your BarChart
with a Container
and give it a color.用一个
Container
包裹你的BarChart
并给它一个颜色。
return Container(
color: Colors.white,
child: new charts.BarChart(
seriesList,
animate: animate,
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.