/// <summary> /// Converts movements to a graphical line and polygon /// </summary> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToMovementLineAndPolygon(float[] results, Color color, string label) { // Create the Line ChartPrimitive chartLine = new ChartPrimitive(); chartLine.Color = color; chartLine.Label = label; chartLine.ShowInLegend = true; chartLine.HitTest = true; for (int monthNo = 0; monthNo < results.Length; ++monthNo) { chartLine.AddSmoothHorizontalBar(new Point(monthNo + .5f, results[monthNo])); } // Create the polygon ChartPrimitive polygon = ChartUtilities.ChartLineToBaseLinedPolygon(chartLine); color.A = (byte)(alpha * color.A); polygon.Color = color; polygon.ShowInLegend = false; polygon.HitTest = false; return(new LineAndPolygon(chartLine, polygon)); }
public TestPage() { InitializeComponent(); ChartUtilities.AddTestLines(xyLineChart); xyLineChart.SubNotes = new string[] { "Right Mouse Button To Zoom, Left Mouse Button To Pan, Double-Click To Reset" }; copyToClipboard.CopyToClipboardDelegate = this.CopyChartToClipboard; }
/// <summary> /// Gets ChartLines and ChartPolygons for the population line, and /// the target line. /// </summary> public static LineAndPolygon ConvertResultsToTargetLineAndPolygon(ChartPrimitive populationLine, float[] results, Color color, string label) { // Calculate Target Primitives ChartPrimitive targetLine = new ChartPrimitive(); targetLine.Color = color; targetLine.Dashed = true; targetLine.Label = label + " Target"; targetLine.ShowInLegend = false; targetLine.HitTest = true; if (populationLine.Points.Count == results.Length) { for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { targetLine.AddPoint(new Point(monthNo * .5f, results[monthNo])); targetLine.AddPoint(new Point(monthNo * .5f + 1f, results[monthNo + 1])); } } else { for (int monthNo = 0; monthNo < results.Length; ++monthNo) { targetLine.AddPoint(new Point(monthNo, results[monthNo])); targetLine.AddPoint(new Point(monthNo + 1f, results[monthNo])); } } ChartPrimitive targetPolygon = ChartUtilities.LineDiffToPolygon(populationLine, targetLine); color.A = (byte)(alpha * color.A); targetPolygon.Color = color; targetPolygon.Dashed = true; targetPolygon.ShowInLegend = false; targetLine.HitTest = false; return(new LineAndPolygon(targetLine, targetPolygon)); }
/// <summary> /// Set the chart transform /// </summary> /// <param name="width"></param> /// <param name="height"></param> protected void SetChartTransform(double width, double height) { Rect plotArea = ChartUtilities.GetPlotRectangle(primitiveList, 0.01f, 0.05f); minPoint = plotArea.Location; minPoint.Offset(-plotArea.Width * panZoomCalculator.Pan.X, plotArea.Height * panZoomCalculator.Pan.Y); minPoint.Offset(0.5 * plotArea.Width * (1 - 1 / panZoomCalculator.Zoom.X), 0.5 * plotArea.Height * (1 - 1 / panZoomCalculator.Zoom.Y)); maxPoint = minPoint; maxPoint.Offset(plotArea.Width / panZoomCalculator.Zoom.X, plotArea.Height / panZoomCalculator.Zoom.Y); var plotScale = new Point(); plotScale.X = (width / plotArea.Width) * panZoomCalculator.Zoom.X; plotScale.Y = (height / plotArea.Height) * panZoomCalculator.Zoom.Y; Matrix shapeMatrix = Matrix.Identity; shapeMatrix.Translate(-minPoint.X, -minPoint.Y); shapeMatrix.Scale(plotScale.X, plotScale.Y); shapeTransform.Matrix = shapeMatrix; }
/// <summary> /// Converts population level to a line and polygon /// </summary> /// <param name="results"></param> /// <param name="color"></param> /// <param name="label"></param> /// <returns></returns> public static LineAndPolygon ConvertResultsToPopulationLineAndPolygon(float[] results, Color color, string label) { ChartPrimitive populationLine = new ChartPrimitive(); populationLine.Color = color; populationLine.Label = label; populationLine.ShowInLegend = true; populationLine.HitTest = true; for (int monthNo = 0; monthNo < results.Length; monthNo += 2) { populationLine.AddPoint(new Point(monthNo * .5f, results[monthNo])); populationLine.AddPoint(new Point(monthNo * .5f + 1f, results[monthNo + 1])); } ChartPrimitive populationPolygon = ChartUtilities.ChartLineToBaseLinedPolygon(populationLine); color.A = (byte)(alpha * color.A); populationPolygon.Color = color; populationPolygon.ShowInLegend = false; populationPolygon.HitTest = false; return(new LineAndPolygon(populationLine, populationPolygon)); }
/// <summary> /// Copies the chart to the clipboard /// </summary> /// <param name="element"></param> /// <param name="width"></param> /// <param name="height"></param> protected void CopyChartToClipboard(FrameworkElement element, double width, double height) { ChartUtilities.CopyChartToClipboard(plotToCopy, xyLineChart, width, height); }
/// <summary> /// Draw all the gridlines and labels for the gridlines /// </summary> /// <param name="size"></param> /// <param name="minXY"></param> /// <param name="maxXY"></param> protected void DrawGridlinesAndLabels(Size size, Point minXY, Point maxXY) { // Clear the text canvas textCanvas.Children.Clear(); // Create brush for writing text Brush axisBrush = new SolidColorBrush(gridLineColor); Brush axisScaleBrush = new SolidColorBrush(gridLineLabelColor); // Need to pick appropriate scale increment. // Go for a 2Exx, 5Exx, or 1Exx type scale double scaleX = 0.0; double scaleY = 0.0; // Work out all the limits if (maxXY.X != minXY.X) { scaleX = size.Width / (maxXY.X - minXY.X); } if (maxXY.Y != minXY.Y) { scaleY = size.Height / (maxXY.Y - minXY.Y); } double spacingX; int startXmult, endXmult; if (ScaleXHours.HasValue && scaleX != 0) { int diff = Math.Max(1, Convert.ToInt32(Math.Truncate((MaxDate - MinDate).TotalHours / ScaleXHours.Value))); spacingX = (maxXY.X - minXY.X) / diff; startXmult = 0; endXmult = diff; } else { var optimalSpacingX = optimalGridLineSpacing.X / scaleX; spacingX = ChartUtilities.Closest_1_2_5_Pow10(optimalSpacingX); startXmult = (int)Math.Ceiling(minXY.X / spacingX); endXmult = (int)Math.Floor(maxXY.X / spacingX); } var optimalSpacingY = optimalGridLineSpacing.Y / scaleY; var spacingY = ChartUtilities.Closest_1_2_5_Pow10(optimalSpacingY); var startYmult = (int)Math.Ceiling(minXY.Y / spacingY); var endYmult = (int)Math.Floor(maxXY.Y / spacingY); double maxXLabelHeight = 0; var pathFigure = new PathFigure(); // Draw all the vertical gridlines double lastTextBorder = 0; for (int lineNo = startXmult; lineNo <= endXmult; ++lineNo) { double xValue = lineNo * spacingX; double xPos = (xValue - minXY.X) * scaleX; var startPoint = new Point(xPos, size.Height); var endPoint = new Point(xPos, 0); pathFigure.Segments.Add(new LineSegment(startPoint, false)); pathFigure.Segments.Add(new LineSegment(endPoint, true)); var text = new TextBlock { TextAlignment = TextAlignment.Center, Foreground = axisScaleBrush, //FontSize = 8 }; if (MinDate != DateTime.MaxValue) { DateTime date = MinDate.AddSeconds(xValue); text.Text = (axisDateFormat != null) ? date.ToString(axisDateFormat) : date.ToString(); } else { text.Text = xValue.ToString(); } text.Measure(size); var textX = xPos - text.DesiredSize.Width * .5; if (lastTextBorder == 0 || lastTextBorder <= textX) { if (textX + text.DesiredSize.Width > size.Width) { textX = size.Width - text.DesiredSize.Width; } text.SetValue(Canvas.LeftProperty, textX); text.SetValue(Canvas.TopProperty, size.Height + 1); textCanvas.Children.Add(text); maxXLabelHeight = Math.Max(maxXLabelHeight, text.DesiredSize.Height); lastTextBorder = textX + text.DesiredSize.Width; } } xGridlineLabels.Height = maxXLabelHeight + 2; // Set string format for vertical text double maxYLabelHeight = 0; // Draw all the horizontal gridlines for (int lineNo = startYmult; lineNo <= endYmult; ++lineNo) { double yValue = lineNo * spacingY; double yPos = (-yValue + minXY.Y) * scaleY + size.Height; var startPoint = new Point(0, yPos); var endPoint = new Point(size.Width, yPos); pathFigure.Segments.Add(new LineSegment(startPoint, false)); pathFigure.Segments.Add(new LineSegment(endPoint, true)); var text = new TextBlock(); if (!yTextForDigitalView || Math.Abs(yValue - 1) < 0.00000001 || Math.Abs(yValue) < 0.00000001) { text.Text = yValue.ToString(); } else { text.Text = " "; } text.LayoutTransform = new RotateTransform(-90); text.Measure(size); text.SetValue(Canvas.LeftProperty, -text.DesiredSize.Width - 1); text.SetValue(Canvas.TopProperty, yPos - text.DesiredSize.Height * .5); textCanvas.Children.Add(text); maxYLabelHeight = Math.Max(maxYLabelHeight, text.DesiredSize.Width); } yGridLineLabels.Height = maxYLabelHeight + 2; var path = new Path(); path.Stroke = axisBrush; var pathGeometry = new PathGeometry(new PathFigure[] { pathFigure }); pathGeometry.Transform = (Transform)textCanvas.RenderTransform.Inverse; path.Data = pathGeometry; textCanvas.Children.Add(path); }
/// <summary> /// Render all the plot lines from the collection of Chart Primitives /// </summary> /// <param name="canvas"></param> protected void RenderPlotLines(Canvas canvas) { // Draw the Chart Plot Points // Fill in the background canvas.Children.Clear(); foreach (ChartPrimitive primitive in primitiveList) { if (primitive.Points.Count > 0) { var path = new Path(); var pathGeometry = new PathGeometry { Transform = shapeTransform }; pathGeometry.AddGeometry(primitive.PathGeometry); if (primitive.Filled) { path.Stroke = null; if (primitive.Dashed) { path.Fill = ChartUtilities.CreateHatch50(primitive.Color, new Size(2, 2)); } else { var brush = new SolidColorBrush(primitive.Color) { Opacity = primitive.FillOpacity }; path.Fill = brush; path.Stroke = new SolidColorBrush(primitive.Color); path.StrokeThickness = primitive.LineThickness; } } else { path.Stroke = new SolidColorBrush(primitive.Color); path.StrokeThickness = primitive.LineThickness; path.Fill = null; if (primitive.Dashed) { path.StrokeDashArray = new DoubleCollection(new double[] { 2, 2 }); } } path.Data = pathGeometry; path.Clip = chartClip; canvas.Children.Add(path); if (primitive.ShowIndividualPoints) { path = new Path { Fill = new SolidColorBrush(primitive.Color) { Opacity = primitive.FillOpacity } }; var gm = new GeometryGroup { Transform = shapeTransform }; foreach (var point in primitive.Points) { gm.Children.Add(NewPointGeometry(point, shapeTransform.Matrix)); } path.Data = gm; path.Clip = chartClip; canvas.Children.Add(path); } } } }