David PHAM-VAN

Improve PieChart default colors

... ... @@ -83,11 +83,9 @@ class Invoice {
static const _darkColor = PdfColors.blueGrey800;
static const _lightColor = PdfColors.white;
PdfColor get _baseTextColor =>
baseColor.luminance < 0.5 ? _lightColor : _darkColor;
PdfColor get _baseTextColor => baseColor.isLight ? _lightColor : _darkColor;
PdfColor get _accentTextColor =>
baseColor.luminance < 0.5 ? _lightColor : _darkColor;
PdfColor get _accentTextColor => baseColor.isLight ? _lightColor : _darkColor;
double get _total =>
products.map<double>((p) => p.total).reduce((a, b) => a + b);
... ...
... ... @@ -282,17 +282,13 @@ Future<Uint8List> generateReport(
datasets: List<pw.Dataset>.generate(dataTable.length, (index) {
final data = dataTable[index];
final color = chartColors[index % chartColors.length];
final textColor =
color.luminance < 0.2 ? PdfColors.white : PdfColors.black;
final value = (data[2] as num).toDouble();
final pct = (value / expense * 100).round();
return pw.PieDataSet(
legend: '${data[0]}\n$pct%',
value: value,
color: color,
legendStyle: pw.TextStyle(fontSize: 10, color: textColor),
legendStyle: pw.TextStyle(fontSize: 10),
);
}),
),
... ...
... ... @@ -13,6 +13,7 @@
- Passthrough SpanningWidget on SingleChildWidget and StatelessWidget
- Improve TextOverflow support
- Fix Table horizontalInside borders
- Improve PieChart default colors
## 3.2.0
... ...
... ... @@ -180,6 +180,16 @@ class PdfColor {
return math.pow((component + 0.055) / 1.055, 2.4).toDouble();
}
/// Determines whether the given [PdfColor] is light.
bool get isLight => !isDark;
/// Determines whether the given [PdfColor] is dark.
bool get isDark {
final relativeLuminance = luminance;
const kThreshold = 0.15;
return (relativeLuminance + 0.05) * (relativeLuminance + 0.05) > kThreshold;
}
/// Get the luminance
double get luminance {
final R = _linearizeColorComponent(red);
... ... @@ -240,6 +250,12 @@ class PdfColor {
int get hashCode => toInt();
}
class PdfColorGrey extends PdfColor {
/// Create a grey color
const PdfColorGrey(double color, [double alpha = 1.0])
: super(color, color, color, alpha);
}
/// Represents an CMYK color
class PdfColorCmyk extends PdfColor {
/// Creates a CMYK color
... ...
... ... @@ -126,13 +126,14 @@ class PieDataSet extends Dataset {
this.legendAlign,
this.legendPosition = PieLegendPosition.auto,
this.legendLineWidth = 1.0,
this.legendLineColor = PdfColors.black,
PdfColor? legendLineColor,
Widget? legendWidget,
this.legendOffset = 20,
}) : drawBorder = drawBorder ?? borderColor != null && color != borderColor,
assert((drawBorder ?? borderColor != null && color != borderColor) ||
drawSurface),
_legendWidget = legendWidget,
legendLineColor = legendLineColor ?? color,
super(
legend: legend,
color: color,
... ... @@ -201,7 +202,18 @@ class PieDataSet extends Dataset {
_legendWidget ??= legend == null
? null
: Text(legend!, style: legendStyle, textAlign: _legendAlign);
: RichText(
text: TextSpan(
children: [TextSpan(text: legend!, style: legendStyle)],
style: TextStyle(
color: lp == PieLegendPosition.inside
? color!.isLight
? PdfColors.white
: PdfColors.black
: null),
),
textAlign: _legendAlign,
);
if (_legendWidget != null) {
_legendWidget!.layout(context,
... ...