public DrawGraphParams(DrawGraphParams cloneSource) { canvas = cloneSource.canvas; graphOrigin = cloneSource.graphOrigin; graphLength = cloneSource.graphLength; graphHeight = cloneSource.graphHeight; axis = cloneSource.axis; graphData = cloneSource.graphData; linecolor = cloneSource.linecolor; }
public void DrawGraphs(List <Dictionary <string, decimal> > frameData) { DrawGraphParams graphX = new DrawGraphParams { canvas = Canvas, linecolor = Brushes.Red, graphOrigin = new Point(0, -33), graphLength = (int)Canvas.Width - 40, graphHeight = ((int)Canvas.Height - 100) / 3, axis = "X", graphData = frameData.Select(f => new Dictionary <string, decimal> { { "X", f["X"] }, { "Y", f["Y"] }, { "Z", f["Z"] * -1M }, { "Rotation", f["Rotation"] } }).ToList() }; DrawGraphParams graphY = new DrawGraphParams(graphX) { axis = "Y", linecolor = Brushes.Yellow, graphOrigin = new Point(0, -66 - (((int)Canvas.Height - 100) / 3)) }; DrawGraphParams graphZ = new DrawGraphParams(graphX) { axis = "Z", linecolor = Brushes.Blue, graphOrigin = new Point(0, -100 - 2 * (((int)Canvas.Height - 100) / 3)) }; Canvas.Children.Clear(); new DrawGraph(graphX); new DrawGraph(graphY); new DrawGraph(graphZ); }
public DrawGraph(DrawGraphParams graphParams) { decimal margin = 80; decimal framecount = graphParams.graphData.Count() - 1; decimal horizontalScaler = (graphParams.graphLength - margin) / framecount; decimal axisMax = findMax(graphParams.axis, graphParams.graphData); decimal axisMin = findMin(graphParams.axis, graphParams.graphData); decimal axisSpread = axisMax - axisMin; decimal verticalScaler = axisSpread > 0 ? (graphParams.graphHeight - margin) / axisSpread : 1; decimal verticalOffset = -axisMin; foreach (var point in graphParams.graphData.Select((value, frame) => new { value, frame }).Skip(1)) { var previousPoint = new { value = graphParams.graphData[point.frame - 1], frame = point.frame - 1 }; Line line = new Line { Stroke = graphParams.linecolor, StrokeThickness = 4, X1 = previousPoint.frame * decimal.ToDouble(horizontalScaler) + decimal.ToDouble(graphParams.graphOrigin.x) + decimal.ToDouble(margin), X2 = point.frame * decimal.ToDouble(horizontalScaler) + decimal.ToDouble(graphParams.graphOrigin.x) + decimal.ToDouble(margin), Y1 = (decimal.ToDouble(previousPoint.value[graphParams.axis]) + decimal.ToDouble(verticalOffset)) * decimal.ToDouble(verticalScaler) + decimal.ToDouble(graphParams.graphOrigin.y) + decimal.ToDouble(margin), Y2 = (decimal.ToDouble(point.value[graphParams.axis]) + decimal.ToDouble(verticalOffset)) * decimal.ToDouble(verticalScaler) + decimal.ToDouble(graphParams.graphOrigin.y) + decimal.ToDouble(margin) }; // flip Y coordinates to adjust for the inverted Y axis of a canvas. line.Y1 = line.Y1 * -1 + graphParams.graphHeight; line.Y2 = line.Y2 * -1 + graphParams.graphHeight; graphParams.canvas.Children.Add(line); drawGridline(new DrawGridlineParams { gridlineType = isMultipleOf(point.frame, 10) ? DrawGridlineParams.GridlineType.Main : DrawGridlineParams.GridlineType.sub, canvas = graphParams.canvas, graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, margin = margin, X1 = point.frame * horizontalScaler + graphParams.graphOrigin.x + margin, X2 = point.frame * horizontalScaler + graphParams.graphOrigin.x + margin, Y1 = graphParams.graphOrigin.y + margin - 20, Y2 = graphParams.graphOrigin.y + margin - 10 }); if (isMultipleOf(point.frame, 10)) { drawText(new DrawTextParams { canvas = graphParams.canvas, color = Brushes.Black, text = point.frame.ToString(), graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, X = point.frame * horizontalScaler + graphParams.graphOrigin.x + margin - 10, Y = graphParams.graphOrigin.y + margin - 30 }); } if (previousPoint.frame == 0) { drawGridline(new DrawGridlineParams { gridlineType = DrawGridlineParams.GridlineType.Main, canvas = graphParams.canvas, graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, margin = margin, X1 = previousPoint.frame * horizontalScaler + graphParams.graphOrigin.x + margin, X2 = previousPoint.frame * horizontalScaler + graphParams.graphOrigin.x + margin, Y1 = graphParams.graphOrigin.y + margin - 20, Y2 = graphParams.graphOrigin.y + margin - 10 }); } } (decimal verticalGridlineInterval, decimal verticalMainGridlineInterval) = axisSpread <= 2M ? (0.1M, 0.5M) : axisSpread <= 5M ? (0.25M, 1M) : axisSpread <= 10M ? (0.5M, 1M) : axisSpread <= 20M ? (1M, 5M) : (1M, 10M); for (decimal i = TruncateToNearest(axisMin, verticalGridlineInterval); i - axisMax < verticalGridlineInterval; i += verticalGridlineInterval) { drawGridline(new DrawGridlineParams { gridlineType = isMultipleOf(i, verticalMainGridlineInterval) ? DrawGridlineParams.GridlineType.Main : DrawGridlineParams.GridlineType.sub, canvas = graphParams.canvas, graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, margin = margin, X1 = graphParams.graphOrigin.x + margin - 20, X2 = graphParams.graphOrigin.x + margin - 10, Y1 = (i + verticalOffset) * verticalScaler + graphParams.graphOrigin.y + margin, Y2 = (i + verticalOffset) * verticalScaler + graphParams.graphOrigin.y + margin }); if (isMultipleOf(i, verticalMainGridlineInterval)) { drawText(new DrawTextParams { canvas = graphParams.canvas, color = Brushes.Black, text = graphParams.axis == "Z" ? (-i).ToString() : i.ToString(), graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, X = graphParams.graphOrigin.x + 5, Y = (i + verticalOffset) * verticalScaler + graphParams.graphOrigin.y + margin }); } } string axisinfo = ""; switch (graphParams.axis) { case "X": axisinfo = "+ left - right"; break; case "Y": axisinfo = "+ up - down"; break; case "Z": axisinfo = "+ backward - forward"; break; } drawText(new DrawTextParams { canvas = graphParams.canvas, color = Brushes.Black, text = axisinfo, graphHeight = graphParams.graphHeight, graphLength = graphParams.graphLength, X = graphParams.graphOrigin.x + margin, Y = graphParams.graphOrigin.y + 25 }); }