linear_grid.dart
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ignore_for_file: omit_local_variable_types
part of widget;
class LinearGrid extends ChartGrid {
LinearGrid({
@required this.xAxis,
@required this.yAxis,
this.xMargin = 10,
this.yMargin = 2,
this.textStyle,
this.lineWidth = 1,
this.color = PdfColors.black,
this.separatorLineWidth = .5,
this.separatorColor = PdfColors.grey,
}) : assert(_isSortedAscending(xAxis)),
assert(_isSortedAscending(yAxis));
final List<double> xAxis;
final List<double> yAxis;
final double xMargin;
final double yMargin;
final TextStyle textStyle;
final double lineWidth;
final PdfColor color;
final double separatorLineWidth;
final PdfColor separatorColor;
TextStyle style;
PdfFont font;
PdfFontMetrics xAxisFontMetric;
PdfFontMetrics yAxisFontMetric;
PdfRect gridBox;
double xOffset;
double xTotal;
double yOffset;
double yTotal;
static bool _isSortedAscending(List<double> list) {
double prev = list.first;
for (double elem in list) {
if (prev > elem) {
return false;
}
prev = elem;
}
return true;
}
@override
PdfPoint tochart(PdfPoint p) {
return PdfPoint(
gridBox.left + gridBox.width * (p.x - xOffset) / xTotal,
gridBox.bottom + gridBox.height * (p.y - yOffset) / yTotal,
);
}
@override
void layout(Context context, PdfPoint size) {
style = Theme.of(context).defaultTextStyle.merge(textStyle);
font = style.font.getFont(context);
xAxisFontMetric =
font.stringMetrics(xAxis.reduce(math.max).toStringAsFixed(1)) *
(style.fontSize);
yAxisFontMetric =
font.stringMetrics(yAxis.reduce(math.max).toStringAsFixed(1)) *
(style.fontSize);
gridBox = PdfRect.fromLTRB(
yAxisFontMetric.width + xMargin,
xAxisFontMetric.height + yMargin,
size.x - xAxisFontMetric.width / 2,
size.y - yAxisFontMetric.height / 2);
xOffset = xAxis.reduce(math.min);
yOffset = yAxis.reduce(math.min);
xTotal = xAxis.reduce(math.max) - xOffset;
yTotal = yAxis.reduce(math.max) - yOffset;
}
@override
void paintBackground(Context context, PdfPoint size) {
xAxis.asMap().forEach((int i, double x) {
context.canvas
..setColor(style.color)
..drawString(
style.font.getFont(context),
style.fontSize,
x.toStringAsFixed(1),
gridBox.left +
gridBox.width * i / (xAxis.length - 1) -
xAxisFontMetric.width / 2,
0,
);
});
for (double y in yAxis.where((double y) => y != yAxis.first)) {
final double textWidth =
(font.stringMetrics(y.toStringAsFixed(1)) * (style.fontSize)).width;
final double yPos = gridBox.bottom + gridBox.height * y / yAxis.last;
context.canvas
..setColor(style.color)
..drawString(
style.font.getFont(context),
style.fontSize,
y.toStringAsFixed(1),
xAxisFontMetric.width / 2 - textWidth / 2,
yPos - font.ascent,
);
context.canvas.drawLine(
gridBox.left,
yPos + font.descent + font.ascent - separatorLineWidth / 2,
gridBox.right,
yPos + font.descent + font.ascent - separatorLineWidth / 2);
}
context.canvas
..setStrokeColor(separatorColor)
..setLineWidth(separatorLineWidth)
..strokePath();
context.canvas
..setStrokeColor(color)
..setLineWidth(lineWidth)
..drawLine(gridBox.left, gridBox.bottom, gridBox.right, gridBox.bottom)
..drawLine(gridBox.left, gridBox.bottom, gridBox.left, gridBox.top)
..strokePath();
}
@override
void paintForeground(Context context, PdfPoint size) {}
}