/// <summary> /// Sets up the polyline and points on the graph /// </summary> /// <param name="graph">The Graph</param> /// <param name="newPoints">The list of points of the polyline on the graph</param> private static void SetUpGraph(PolylineGraph graph, ICollection <Point> newPoints) { //Adding the Polyline Polyline polyline = new Polyline { Points = new PointCollection(newPoints), Stroke = graph.PolylineColor, StrokeThickness = 1 }; Canvas.SetLeft(polyline, graph.AxisThickness - graph.min_x + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0)); Canvas.SetTop(polyline, graph.AxisThickness - graph.min_y + +((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0)); graph.Graph.Children.Add(polyline); //Adding the points with tooltip if (graph.ShowPoints) { foreach (Point p in newPoints) { Ellipse e = new Ellipse { Width = graph.PointSize, Height = graph.PointSize, Fill = graph.PolylineColor, ToolTip = PointToolTip((p.Y / graph.YScaleFactor).ToString()) }; Canvas.SetLeft(e, graph.AxisThickness + p.X - graph.min_x - graph.PointSize / 2 + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0)); Canvas.SetTop(e, graph.AxisThickness + p.Y - graph.min_y - graph.PointSize / 2 + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0)); graph.Graph.Children.Add(e); } } }
/// <summary> /// Redraws the view when the Points collection changes /// </summary> private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PolylineGraph graph = (PolylineGraph)d; if (e.NewValue == null) { return; } graph.Graph.Children.Clear(); graph.Axis.Children.Clear(); graph.max_x = Double.MinValue; graph.max_y = Double.MinValue; graph.min_x = Double.MaxValue; graph.min_y = Double.MaxValue; ICollection <Point> scaledPoints = new Collection <Point>(); foreach (Point p in (ICollection <Point>)e.NewValue) { scaledPoints.Add(new Point(p.X * graph.XScaleFactor, p.Y * graph.YScaleFactor)); } foreach (Point p in scaledPoints) { if (p.X > graph.max_x) { graph.max_x = p.X; } if (p.Y > graph.max_y) { graph.max_y = p.Y; } if (p.X < graph.min_x) { graph.min_x = p.X; } if (p.Y < graph.min_y) { graph.min_y = p.Y; } } Double height = (graph.max_y - graph.min_y); Double width = (graph.max_x - graph.min_x); graph.Graph.Width = width + graph.AxisThickness + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght; graph.Graph.Height = height + graph.AxisThickness + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght; SetUpYAxis(graph, scaledPoints); SetUpXAxis(graph, scaledPoints); SetUpGraph(graph, scaledPoints); }
/// <summary> /// Sets up the Y axis /// </summary> /// <param name="graph">The Graph</param> /// <param name="newPoints">The list of points on the graph</param> private static void SetUpYAxis(PolylineGraph graph, ICollection <Point> newPoints) { //Adding the vertical axis line Polyline verticalLine = new Polyline(); double lineH = graph.max_y - graph.min_y + graph.AxisThickness + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0); verticalLine.Points.Add(new Point(graph.AxisThickness, graph.AxisThickness / 2)); verticalLine.Points.Add(new Point(graph.AxisThickness, lineH + graph.AxisOutlineLenght)); verticalLine.Stroke = graph.AxisColor; verticalLine.StrokeThickness = 0.5; graph.Axis.Children.Add(verticalLine); //Adding the labels and the unit lines if (graph.VerticalSteps > 0) { //Calculatin the steps height Double offset = 0; int n_division = 0; if (graph.VerticalSteps <= 0) { graph.VerticalSteps = 1; } graph.stepHeight = (graph.max_y - graph.min_y); while (graph.stepHeight > 10) { n_division++; graph.stepHeight = graph.stepHeight / 10; } if ((graph.stepHeight - Math.Round(graph.stepHeight)) < 0.5) { graph.stepHeight += 0.5; } graph.stepHeight = Math.Round(graph.stepHeight, MidpointRounding.AwayFromZero); for (int i = 0; i < n_division; i++) { graph.stepHeight = graph.stepHeight * 10; } graph.stepHeight = graph.stepHeight / (graph.VerticalSteps - 1); //Adding the lines for (int i = 0; i < graph.VerticalSteps; i++) { if (graph.AttachGraphToAxis && offset == 0) { i--; offset += graph.stepHeight; continue; } Polyline line = new Polyline(); line.Points.Add(new Point(graph.AxisThickness / 2, graph.AxisThickness + offset + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0))); line.Points.Add(new Point(graph.AxisThickness, graph.AxisThickness + offset + ((!graph.AttachGraphToAxis)?graph.GraphContentPadding:0))); line.Stroke = graph.AxisColor; line.StrokeThickness = 0.5; graph.Axis.Children.Add(line); //Adding the labels if (graph.ShowAxisLabels) { TextBlock label = new TextBlock(); label.Text = Math.Round(((offset + graph.min_y) / graph.YScaleFactor)).ToString(); label.FontSize = 8; label.Foreground = graph.LabelsColor; Canvas.SetBottom(label, graph.AxisThickness + offset + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) - label.FontSize / 2); Canvas.SetLeft(label, -((label.Text.Length * label.FontSize) / 2)); graph.Labels.Children.Add(label); } offset += graph.stepHeight; //Avoiding lines over the vertical axis arrow if (graph.AxisThickness + offset + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) > lineH) { break; } } } //Adding the Y axis name label if (graph.ShowAxisLabels) { Double YPosition = graph.AxisThickness + (graph.max_y - graph.min_y) + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght + 5; Double XPosition = -graph.AxisThickness; Decorator label = Label(graph.YAxisName, XPosition, YPosition, graph.LabelsColor, 10); label.Width = 50; label.Height = 15; graph.Labels.Children.Add(label); graph.Graph.Height += label.Height + 8; } //Adding the arrow on top of the axis line Polyline arrow = VerticalArrow(new Point(graph.AxisThickness, graph.max_y - graph.min_y + graph.AxisThickness + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght), graph.AxisThickness / 2); arrow.Stroke = graph.AxisColor; arrow.StrokeThickness = 1; graph.Axis.Children.Add(arrow); }
/// <summary> /// Sets up the X axis /// </summary> /// <param name="graph">The Graph</param> /// <param name="newPoints">The list of Points on the graph</param> private static void SetUpXAxis(PolylineGraph graph, ICollection <Point> newPoints) { //Adding the horizontal axis line Polyline horizontalLine = new Polyline(); horizontalLine.Points.Add(new Point(graph.AxisThickness / 2, graph.AxisThickness)); horizontalLine.Points.Add(new Point(graph.max_x + graph.AxisThickness - graph.min_x + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght, graph.AxisThickness)); horizontalLine.Stroke = graph.AxisColor; horizontalLine.StrokeThickness = 0.5; graph.Axis.Children.Add(horizontalLine); //Setting the horizontal offset for the axis units and lines Double offset = graph.AxisThickness; if (graph.AttachGraphToAxis is false) { offset += graph.GraphContentPadding; } //Adding the axis unit lines and labels foreach (Point p in newPoints) { if (graph.AttachGraphToAxis && p.X == 0) { continue; } //Adding the lines Polyline line = new Polyline(); line.Points.Add(new Point(offset + p.X - graph.min_x, graph.AxisThickness / 2)); line.Points.Add(new Point(offset + p.X - graph.min_x, graph.AxisThickness)); line.Stroke = graph.AxisColor; line.StrokeThickness = 0.5; graph.Axis.Children.Add(line); //Adding the labels if (graph.ShowAxisLabels) { TextBlock label = new TextBlock { Text = ((p.X) / graph.XScaleFactor).ToString(), FontSize = 8, Foreground = graph.LabelsColor }; Canvas.SetBottom(label, -label.FontSize); Canvas.SetLeft(label, offset + p.X - graph.min_x - ((label.Text.Length * label.FontSize) / 4)); graph.Labels.Children.Add(label); } } //Adding the X axis name label if (graph.ShowAxisLabels) { Double XPosition = graph.AxisThickness + (graph.max_x - graph.min_x) + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght + 5; Double YPosition = graph.AxisThickness / 2; Decorator label = Label(graph.XAxisName, XPosition, YPosition, graph.LabelsColor, 10); label.Width = 50; label.Height = 15; graph.Labels.Children.Add(label); graph.Graph.Width += label.Width + 8; } //Adding the arrow to the end of the axis line Polyline arrow = HorizontalArrow(new Point(graph.max_x - graph.min_x + graph.AxisThickness + ((!graph.AttachGraphToAxis) ? graph.GraphContentPadding : 0) + graph.AxisOutlineLenght, graph.AxisThickness), graph.AxisThickness / 2); arrow.Stroke = graph.AxisColor; arrow.StrokeThickness = 1; graph.Axis.Children.Add(arrow); }