Skip to content

Commit 023a29d

Browse files
committed
Fixed issues with rendering graph starts, and skew angles on text labels.
1 parent a26a0ad commit 023a29d

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

lib/layered_chart.dart

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ class LayeredChartState extends State<LayeredChart> {
2929
List<Path> paths;
3030
List<Path> capPaths;
3131
List<double> maxValues;
32+
double theta;
33+
double graphHeight ;
3234
List<TextPainter> labelPainter;
3335
List<TextPainter> milestonePainter;
3436
Size lastSize = null;
3537

36-
void buildPaths(Size size, List<DataSeries> dataToPlot, List<WeekLabel> milestones, int numPoints, double graphHeight, double graphGap, double margin, double theta, double capTheta, double capSize) {
38+
void buildPaths(Size size, List<DataSeries> dataToPlot, List<WeekLabel> milestones, int numPoints, double graphGap, double margin, double capTheta, double capSize) {
39+
double screenRatio = size.width / size.height;
40+
double degrees = MathUtils.clampedMap(screenRatio, 0.5, 2.5, 50, 5);
41+
print("Ratio: ${screenRatio}, Degrees: ${degrees}");
42+
theta = pi*degrees/180;
43+
graphHeight = MathUtils.clampedMap(screenRatio, 0.5, 2.5, 50, 150);
44+
3745
int m = dataToPlot.length;
3846
paths = new List<Path>(m);
3947
capPaths = new List<Path>(m);
@@ -55,12 +63,8 @@ class LayeredChartState extends State<LayeredChart> {
5563
double startY = size.height;
5664
double endY = startY - (endX - startX) * tan(theta);
5765
double xWidth = (endX - startX) / numPoints;
58-
double capX = cos(capTheta + pi / 2) * capSize;
59-
double capY = -sin(capTheta + pi / 2) * capSize;
60-
double capRange = capSize * cos(capTheta);
61-
double capFix = 1 - (capRange % xWidth) / capRange;
62-
capX *= capFix;
63-
capY *= capFix;
66+
double capRangeX = capSize * cos(capTheta);
67+
double capFix = 1 - (capRangeX % xWidth) / capRangeX;
6468
double tanCapTheta = tan(capTheta);
6569
List<double> curvePoints = new List<double>(numPoints);
6670
for (int i = 0; i < m; i++) {
@@ -86,13 +90,12 @@ class LayeredChartState extends State<LayeredChart> {
8690
capPaths[i] = new Path();
8791
paths[i].moveTo(startX, startY);
8892
capPaths[i].moveTo(startX, startY);
89-
capPaths[i].lineTo(startX + capX, startY + capY);
9093
for (int j = 0; j < numPoints; j++) {
9194
double v = curvePoints[j];
9295
int k = j + 1;
9396
double xDist = xWidth;
9497
double capV = v;
95-
while (k < numPoints && xDist <= capRange) {
98+
while (k < numPoints && xDist <= capRangeX) {
9699
double cy = curvePoints[k] + xDist * tanCapTheta;
97100
capV = max(capV, cy);
98101
k++;
@@ -103,6 +106,12 @@ class LayeredChartState extends State<LayeredChart> {
103106
double y = baseY - v;
104107
double cY = baseY - capV;
105108
paths[i].lineTo(x, y);
109+
if (j == 0) {
110+
int k = capRangeX ~/ xWidth;
111+
double mx = MathUtils.map(-k.toDouble(), 0, (numPoints - 1).toDouble(), startX, endX);
112+
double my = MathUtils.map(-k.toDouble(), 0, (numPoints - 1).toDouble(), startY, endY) - capV;
113+
capPaths[i].lineTo(mx, my);
114+
}
106115
capPaths[i].lineTo(x, cY);
107116
}
108117
paths[i].lineTo(endX, endY);
@@ -135,7 +144,7 @@ class LayeredChartState extends State<LayeredChart> {
135144
Widget build(BuildContext context) {
136145
return new Container(
137146
color: Constants.backgroundColor,
138-
child: new CustomPaint(foregroundPainter: new ChartPainter(this, widget.dataToPlot, widget.milestones, 80, 150, 50, 5, 50, 10, 500, widget.animationValue), child: new Container()));
147+
child: new CustomPaint(foregroundPainter: new ChartPainter(this, widget.dataToPlot, widget.milestones, 80, 50, 50, 12, 500, widget.animationValue), child: new Container()));
139148
}
140149
}
141150

@@ -145,9 +154,7 @@ class ChartPainter extends CustomPainter {
145154
List<WeekLabel> milestones;
146155

147156
double margin;
148-
double graphHeight;
149157
double graphGap;
150-
double theta;
151158
double capTheta;
152159
double capSize;
153160
int numPoints;
@@ -162,8 +169,7 @@ class ChartPainter extends CustomPainter {
162169

163170
LayeredChartState state;
164171

165-
ChartPainter(this.state, this.dataToPlot, this.milestones, this.margin, this.graphHeight, this.graphGap, double degrees, double capDegrees, this.capSize, this.numPoints, this.amount) {
166-
this.theta = pi * degrees / 180;
172+
ChartPainter(this.state, this.dataToPlot, this.milestones, this.margin, this.graphGap, double capDegrees, this.capSize, this.numPoints, this.amount) {
167173
this.capTheta = pi * capDegrees / 180;
168174
pathPaint = new Paint();
169175
pathPaint.style = PaintingStyle.fill;
@@ -190,17 +196,9 @@ class ChartPainter extends CustomPainter {
190196
return;
191197
}
192198

193-
// Adjust angles and heights based on current screen width ratio
194-
double screenRatio = size.width / size.height;
195-
var degrees = MathUtils.clampedMap(screenRatio, 0.5, 2.5, 50, 5);
196-
// print("Ratio: ${screenRatio}, Degrees: ${degrees}");
197-
theta = pi*degrees/180;
198-
graphHeight = MathUtils.clampedMap(screenRatio, 0.5, 2.5, 50, 150);
199-
// print("Height: ${graphHeight}");
200-
201199
if (state.lastSize == null || size.width != state.lastSize.width || size.height != state.lastSize.height) {
202200
print("Building paths, lastsize = ${state.lastSize}");
203-
state.buildPaths(size, dataToPlot, milestones, numPoints, graphHeight, graphGap, margin, theta, capTheta, capSize);
201+
state.buildPaths(size, dataToPlot, milestones, numPoints, graphGap, margin, capTheta, capSize);
204202
}
205203
// Using the 900 version of the Material color for the main color, and the 500 version for the cap
206204
List<Color> colors = [
@@ -228,7 +226,7 @@ class ChartPainter extends CustomPainter {
228226
double startX = margin + xIndent;
229227
double endX = size.width - margin;
230228
double startY = size.height;
231-
double endY = startY - (endX - startX) * tan(theta);
229+
double endY = startY - (endX - startX) * tan(state.theta);
232230
// TextStyle textStyle = new TextStyle();
233231
// ParagraphBuilder paragraphBuilder = new ParagraphBuilder(new ParagraphStyle(fontSize: 10));
234232

@@ -253,7 +251,7 @@ class ChartPainter extends CustomPainter {
253251
canvas.save();
254252
TextPainter tp = state.milestonePainter[i];
255253
canvas.translate(textX, textY);
256-
canvas.skew(capTheta * 1.0, -theta);
254+
canvas.skew(tan(capTheta * 1.0), -tan(state.theta));
257255
canvas.translate(-tp.width / 2, 0);
258256
tp.paint(canvas, new Offset(0, 0));
259257
canvas.restore();
@@ -280,8 +278,11 @@ class ChartPainter extends CustomPainter {
280278
// canvas.skew(capTheta * 1.0, -theta);
281279
// Vertical approach
282280
// canvas.translate(startX + 25, startY - 2);
283-
canvas.skew(0 * pi / 180, -theta);
284281
TextPainter tp = state.labelPainter[i];
282+
// canvas.translate(-tp.width / 2, tp.height / 2);
283+
// canvas.skew(0, -state.theta + 2 * pi);
284+
canvas.skew(0, -tan(state.theta));
285+
// canvas.rotate(-state.theta);
285286
canvas.drawRect(new Rect.fromLTWH(-1, -1, tp.width + 2, tp.height + 2), fillPaint);
286287
tp.paint(canvas, new Offset(0, 0));
287288
canvas.restore();
@@ -293,8 +294,8 @@ class ChartPainter extends CustomPainter {
293294
Path clipPath = new Path();
294295
clipPath.moveTo(startX - capSize, startY + 11);
295296
clipPath.lineTo(endX, endY + 1);
296-
clipPath.lineTo(endX, endY - graphHeight - capSize);
297-
clipPath.lineTo(startX - capSize, startY - graphHeight - capSize);
297+
clipPath.lineTo(endX, endY - state.graphHeight - capSize);
298+
clipPath.lineTo(startX - capSize, startY - state.graphHeight - capSize);
298299
clipPath.close();
299300
// canvas.drawPath(clipPath, testPaint);
300301
canvas.clipPath(clipPath);
@@ -306,6 +307,7 @@ class ChartPainter extends CustomPainter {
306307
canvas.translate(offsetX - startX, offsetY - startY);
307308
canvas.drawPath(state.capPaths[i], capPaint);
308309
canvas.drawPath(state.paths[i], pathPaint);
310+
309311
canvas.restore();
310312
}
311313
}

0 commit comments

Comments
 (0)