asyncInitWithOutAnimal method
Implementation
Map<int, LineInfo> asyncInitWithOutAnimal(ChartsState state) {
ChartDimensionCoordinateState layout = state.layout as ChartDimensionCoordinateState;
chartState.children = [];
int index = 0;
//offset.dx 滚动偏移 (src.zoom - 1) * (src.size.width / 2) 缩放
double left = layout.left;
double right = layout.right;
double top = layout.top;
double bottom = layout.bottom;
Map<int, LineInfo> pathMap = {};
ChartItemLayoutState? lastShape;
num? lastXValue;
//遍历数据 处理数据信息
for (T value in data) {
ChartLineLayoutState currentPointLayout = ChartLineLayoutState();
currentPointLayout.layout = layout;
currentPointLayout.xAxis = layout.xAxis;
currentPointLayout.yAxis = layout.yAxis;
currentPointLayout.yAxisPosition = yAxisPosition;
chartState.children.add(currentPointLayout);
//获取原数据
num? xValue = _instance.position.call(value);
if (lastXValue != null) {
assert(lastXValue < xValue, '$xValue 必须大于 $lastXValue,(虽然可以支持逆序,但是为了防止数据顺序混乱,还是强制要求必须是正序的数组)');
}
List<num>? yValues = currentPointLayout.yValues;
yValues ??= _instance.values.call(value);
//保存数据
currentPointLayout.index = index;
currentPointLayout.xValue = xValue;
currentPointLayout.yValues = yValues;
assert(_instance.colors.length >= yValues.length, '颜色配置跟数据源不匹配');
assert(_instance.shaders == null || _instance.shaders!.length >= yValues.length, '颜色配置跟数据源不匹配');
//计算x轴和y轴的物理位置
double xPos = xValue * layout.xAxis.density;
//一组数据下可能多条线
for (int valueIndex = 0; valueIndex < yValues.length; valueIndex++) {
//每条线用map存放下,以便后面统一绘制
LineInfo? lineInfo = pathMap[valueIndex];
if (lineInfo == null) {
lineInfo = LineInfo(_instance.isCurve);
pathMap[valueIndex] = lineInfo;
}
//计算点的位置
num yValue = yValues[valueIndex];
//y轴位置
double yPos = bottom - layout.yAxis[yAxisPosition].getHeight(yValue);
Offset currentPoint = Offset(xPos, yPos);
lineInfo.startPoint ??= currentPoint;
//点的信息
ChartLineLayoutState childLayoutState;
if (valueIndex < currentPointLayout.children.length) {
childLayoutState = currentPointLayout.children[valueIndex] as ChartLineLayoutState;
} else {
childLayoutState = ChartLineLayoutState();
currentPointLayout.children.add(childLayoutState);
}
childLayoutState.setOriginRect(Rect.fromCenter(center: currentPoint, width: _instance.dotRadius, height: _instance.dotRadius));
childLayoutState.index = index;
childLayoutState.layout = layout;
childLayoutState.xAxis = layout.xAxis;
childLayoutState.yAxis = layout.yAxis;
childLayoutState.yAxisPosition = yAxisPosition;
childLayoutState.xValue = xValue;
childLayoutState.yValue = yValue;
//存放点的位置
lineInfo.appendPoint(childLayoutState);
}
Rect currentRect = Rect.fromLTRB(xPos - _instance.dotRadius, top, xPos + _instance.dotRadius, bottom);
currentPointLayout.setOriginRect(currentRect);
currentPointLayout.left = left;
currentPointLayout.right = right;
//这里用链表解决查找附近节点的问题
currentPointLayout.preShapeState = lastShape;
lastShape?.nextShapeState = currentPointLayout;
lastShape = currentPointLayout;
//放到最后
index++;
lastXValue = xValue;
}
return pathMap;
}