initialize method
Implementation
void initialize(double widthPX, double heightPX, TextStyle? style) {
_calcScales(heightPX);
//calc axis textpainters, before using
_yTicks = {};
int index = 0;
lines.forEach((chartLine) {
_yTicks[chartLine.unit] = height(chartLine.unit) / 5;
index++;
});
_yAxisTexts = {};
double maxLeft = 0;
double maxRight = 1;
for (int axisIndex = 0; axisIndex < indexToUnit.length; axisIndex++) {
List<TextPainter> painters = [];
_yAxisTexts[axisIndex] = painters;
String? unit = indexToUnit[axisIndex];
for (int c = 0; c <= (stepCount + 1); c++) {
double axisValue = (_minY[unit!]! + _yTicks[unit]! * c);
String axisValueString;
if (_yTicks[unit]! < 1) {
axisValueString = axisValue.toStringAsFixed(2);
if (axisValueString.endsWith('0')) {
axisValueString =
axisValueString.substring(0, axisValueString.length - 1);
}
} else if (_yTicks[unit]! <= 10) {
axisValueString = axisValue.toStringAsFixed(1);
} else {
axisValueString = axisValue.round().toString();
}
TextSpan span = new TextSpan(style: style, text: axisValueString);
TextPainter tp = new TextPainter(
text: span,
textAlign: TextAlign.right,
textDirection: TextDirectionHelper.getDirection());
tp.layout();
if (axisIndex == 0) {
maxLeft = max(tp.width + axisMargin, maxLeft);
} else {
maxRight = max(tp.width + axisMargin, maxRight);
}
painters.add(tp);
}
}
_xAxisOffsetPX = maxLeft;
_xAxisOffsetPXright = maxRight;
_widthStepSize = (widthPX - maxLeft - maxRight) / (stepCount + 1);
_heightStepSize = (heightPX - axisOffsetPX) / (stepCount + 1);
_xScale = (widthPX - xAxisOffsetPX - maxRight) / width;
_xOffset = minX * _xScale!;
if (_xOffset!.isNaN) {
_xOffset = 0;
}
_seriesMap = {};
_pathMap = {};
index = 0;
lines.forEach((chartLine) {
chartLine.points.forEach((p) {
double x = (p.x * xScale!) - xOffset!;
double adjustedY = (p.y * _yScales[chartLine.unit]!) -
(_minY[chartLine.unit]! * _yScales[chartLine.unit]!);
double y = (heightPX - axisOffsetPX) - adjustedY;
//adjust to make room for axis values:
x += xAxisOffsetPX;
if (x.isNaN) x = 0;
if (y.isNaN) y = 0;
if (_seriesMap[index] == null) {
_seriesMap[index] = [];
}
if (p is DateTimeChartPoint) {
_seriesMap[index]!
.add(HighlightPoint(DateTimeChartPoint(x, y, p.dateTime), p.y));
} else {
_seriesMap[index]?.add(HighlightPoint(ChartPoint(x, y), p.y));
}
});
index++;
});
_axisOffSetWithPadding = xAxisOffsetPX - axisMargin;
_xAxisTexts = [];
//Todo: make the axis part generic, to support both string, dates, and numbers
Duration duration = fromTo.max!.difference(fromTo.min!);
double stepInSeconds = duration.inSeconds.toDouble() / (stepCount + 1);
for (int c = 0; c <= (stepCount + 1); c++) {
DateTime tick =
fromTo.min!.add(Duration(seconds: (stepInSeconds * c).round()));
TextSpan span =
new TextSpan(style: style, text: _formatDateTime(tick, duration));
TextPainter tp = new TextPainter(
text: span,
textAlign: TextAlign.right,
textDirection: TextDirectionHelper.getDirection());
tp.layout();
_xAxisTexts!.add(tp);
}
}