private void DrawFigure(SharpDXFigure figure) { SharpDX.Direct2D1.PathGeometry geometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory); SharpDX.Direct2D1.GeometrySink sink = geometry.Open(); sink.BeginFigure(figure.Points[0], new SharpDX.Direct2D1.FigureBegin()); for (int i = 0; i < figure.Points.Length; i++) { sink.AddLine(figure.Points[i]); } sink.AddLine(figure.Points[0]); sink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed); sink.Close(); RenderTarget.FillGeometry(geometry, dxmBrushes[figure.Color].DxBrush); geometry.Dispose(); sink.Dispose(); }
private void DrawRegionBetweenSeries(ChartScale chartScale, Series <double> firstSeries, Series <double> secondSeries, string upperColor, string lowerColor, int displacement) { string BrushName = String.Empty; List <SharpDX.Vector2> SeriesAPoints = new List <SharpDX.Vector2>(); List <SharpDX.Vector2> SeriesBPoints = new List <SharpDX.Vector2>(); List <SharpDX.Vector2> tmpPoints = new List <SharpDX.Vector2>(); List <SharpDXFigure> SharpDXFigures = new List <SharpDXFigure>(); // Convert SeriesA and SeriesB to points int start = ChartBars.FromIndex - displacement > 0 ? ChartBars.FromIndex - displacement : 0; int end = ChartBars.ToIndex; for (int barIndex = start; barIndex <= end; barIndex++) { if (firstSeries.IsValidDataPointAt(barIndex)) { SeriesAPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(firstSeries.GetValueAt(barIndex)))); SeriesBPoints.Add(new SharpDX.Vector2((float)ChartControl.GetXByBarIndex(ChartBars, barIndex + displacement), (float)chartScale.GetYByValue(secondSeries.GetValueAt(barIndex)))); } } int lastCross = 0; bool isTouching = false; bool colorNeeded = true; for (int i = 0; i < SeriesAPoints.Count; i++) { if (colorNeeded) { colorNeeded = false; // Set initial color or wait until we need to start a shape if (SeriesAPoints[i].Y < SeriesBPoints[i].Y) { BrushName = upperColor; } else if (SeriesAPoints[i].Y > SeriesBPoints[i].Y) { BrushName = lowerColor; } else { colorNeeded = true; lastCross = i; } if (!colorNeeded) { tmpPoints.Add(SeriesAPoints[i]); } continue; } // Check if SeriesA and SeriesB meet or have crossed to loop back and close figure if ((SeriesAPoints[i].Y == SeriesBPoints[i].Y && isTouching == false) || (SeriesAPoints[i].Y > SeriesBPoints[i].Y && SeriesAPoints[i - 1].Y < SeriesBPoints[i - 1].Y) || (SeriesBPoints[i].Y > SeriesAPoints[i].Y && SeriesBPoints[i - 1].Y < SeriesAPoints[i - 1].Y)) { // reset isTouching isTouching = false; // Set the endpoint SharpDX.Vector2 endpoint = (SeriesAPoints[i].Y != SeriesBPoints[i].Y) ? FindIntersection(SeriesAPoints[i - 1], SeriesAPoints[i], SeriesBPoints[i - 1], SeriesBPoints[i]) : SeriesAPoints[i]; tmpPoints.Add(endpoint); // Loop back and add SeriesBPoints for (int j = i - 1; j >= lastCross; j--) { tmpPoints.Add(SeriesBPoints[j]); } // Create figure SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (SeriesAPoints[i - 1].Y < SeriesBPoints[i - 1].Y) ? upperColor : lowerColor); SharpDXFigures.Add(figure); // Clear Points tmpPoints.Clear(); // Start new figure if we crossed, otherwise we will wait until we need a new figure if (SeriesAPoints[i].Y != SeriesBPoints[i].Y) { tmpPoints.Add(SeriesBPoints[i]); tmpPoints.Add(endpoint); tmpPoints.Add(SeriesAPoints[i]); } else { isTouching = true; } // Set last cross lastCross = i; } // Check if we are at the end of our rendering pass to loop back to loop back and close figure else if (i == SeriesAPoints.Count - 1) { tmpPoints.Add(SeriesAPoints[i]); // Loop back and add SeriesBPoints for (int j = i; j >= lastCross; j--) { tmpPoints.Add(SeriesBPoints[j]); } // Create figure SharpDXFigure figure = new SharpDXFigure(tmpPoints.ToArray(), (SeriesAPoints[i].Y < SeriesBPoints[i].Y) ? upperColor : lowerColor); SharpDXFigures.Add(figure); // Clear Points tmpPoints.Clear(); break; } // Figure does not need to be closed. Add more points or open a new figure if we were touching else if (SeriesAPoints[i].Y != SeriesBPoints[i].Y) { if (isTouching == true) { tmpPoints.Add(SeriesAPoints[i - 1]); lastCross = i - 1; } tmpPoints.Add(SeriesAPoints[i]); isTouching = false; } } // Draw figures foreach (SharpDXFigure figure in SharpDXFigures) { DrawFigure(figure); } }