public static List <PiePartGeometry> GetPieChart(ICanvasResourceCreator resourceCreator, float[] valueList, ChartTextFormat textFormat, float radius, bool isShowData) { var centerPoint = new Vector2(radius, radius); var geoList = new List <PiePartGeometry>(); var sum = valueList.Sum(); var startAngle = 0f; var percentList = valueList.Select(o => o / sum); foreach (var item in percentList) { var radian = item * 2 * Math.PI; var builder = new CanvasPathBuilder(resourceCreator); builder.BeginFigure(centerPoint); builder.AddArc(centerPoint, radius, radius, startAngle, (float)radian); builder.EndFigure(CanvasFigureLoop.Closed); if (isShowData) { var partCenter = new Point( (radius - textFormat.Thickness.Top) * Math.Cos(startAngle + radian / 2) + centerPoint.X, (radius - textFormat.Thickness.Top) * Math.Sin(startAngle + radian / 2) + centerPoint.Y) .ToVector2(); var textLayout = ChartDrawHelper.CreateCanvasText(resourceCreator, $"{string.Format("{0:F1}", item * 100)}%", textFormat); var textRect = textLayout.DrawBounds; var textPos = new Point(partCenter.X - textRect.Width / 2, partCenter.Y - textRect.Height / 2); var part = new PiePartGeometry { ShapeGeometry = CanvasGeometry.CreatePath(builder), TextGeometry = CanvasGeometry.CreateText(textLayout), DataRect = new Rect(textPos, new Size(textRect.Width, textRect.Height)) }; geoList.Add(part); builder.Dispose(); textLayout.Dispose(); } else { var part = new PiePartGeometry { ShapeGeometry = CanvasGeometry.CreatePath(builder) }; geoList.Add(part); builder.Dispose(); } startAngle += (float)radian; } return(geoList); }
public static ICanvasImage CreateValueMarker(ICanvasResourceCreator resourceCreator, ChartTextFormat markerFormat, List <Vector2> positions, List <string> labels, MarkerPosition markerPosition) { if (positions?.Count > 0 && positions.Count == labels?.Count) { var commandList = new CanvasCommandList(resourceCreator); using (var drawSession = commandList.CreateDrawingSession()) { var cancasTextArray = labels.Select(o => CreateCanvasText(resourceCreator, o, markerFormat)).ToArray(); for (int i = 0; i < positions.Count; i++) { var textRect = cancasTextArray[i].DrawBounds; double x = positions[i].X, y = positions[i].Y; switch (markerPosition) { case MarkerPosition.Top: x -= textRect.Width / 2 + textRect.X; y -= textRect.Height + textRect.Y + markerFormat.Thickness.Bottom; break; case MarkerPosition.Right: x += markerFormat.Thickness.Left - textRect.X; y -= textRect.Height / 2 + textRect.Y; break; case MarkerPosition.Bottom: x -= textRect.Width / 2 + textRect.X; y += -textRect.Y + markerFormat.Thickness.Top; break; case MarkerPosition.Left: x -= textRect.Width + markerFormat.Thickness.Right - textRect.X; y -= textRect.Height / 2 + textRect.Y; break; } drawSession.DrawTextLayout(cancasTextArray[i], (float)x, (float)y, ColorConverter.ConvertHexToColor(markerFormat.Foreground)); cancasTextArray[i].Dispose(); } } return(commandList); } return(null); }
public static CanvasTextLayout CreateCanvasText(ICanvasResourceCreator resourceCreator, string text, ChartTextFormat textFormat, float requestedWidth = 0, float requestedHeight = 0) { var textLayout = new CanvasTextLayout(resourceCreator, text, new CanvasTextFormat { FontWeight = new Windows.UI.Text.FontWeight { Weight = textFormat.IsBold?(ushort)700: (ushort)400 }, FontFamily = textFormat.FontFamily, FontSize = textFormat.FontSize, WordWrapping = CanvasWordWrapping.NoWrap, TrimmingSign = CanvasTrimmingSign.Ellipsis }, requestedWidth, requestedHeight); return(textLayout); }
public static List <PolyLineGeometry> GetLineChart(ICanvasResourceCreator resourceCreator, LineChartStyle style, List <float[]> valuesList, float height, float width, ChartTextFormat dataLabelFormat, List <string> colorSpace) { var geoList = new List <PolyLineGeometry>(); var max = valuesList.Select(o => o.Max()).Max(); var pointNum = valuesList[0].Length; var pointGap = width / (pointNum - 1); for (int i = 0; i < valuesList.Count; i++) { var list = valuesList[i]; var markerPoints = new List <Vector2>(); var markerStrs = new List <string>(); var dotList = new List <CanvasGeometry>(); var builder = new CanvasPathBuilder(resourceCreator); var heightList = list.Select(o => (float)(o / max * height)).ToList(); var offset = 0f; var first = heightList.First(); var startPoint = new Vector2(offset, -first); builder.BeginFigure(startPoint); dotList.Add(GetDotGeometry(resourceCreator, style.DotShape, startPoint, style.DotSizeWidth, style.DotSizeHeight)); markerPoints.Add(startPoint); markerStrs.Add($"{list[0]}"); heightList.Remove(first); for (int j = 0; j < heightList.Count; j++) { offset += pointGap; var point = new Vector2(offset, -heightList[j]); builder.AddLine(point); dotList.Add(GetDotGeometry(resourceCreator, style.DotShape, point, style.DotSizeWidth, style.DotSizeHeight)); markerPoints.Add(point); markerStrs.Add($"{list[j + 1]}"); } builder.EndFigure(CanvasFigureLoop.Open); var format = dataLabelFormat.Clone(); //format.Foreground = colorSpace[i % colorSpace.Count]; format.Thickness = new ChartThickness(0, format.Thickness.Top + style.DotSizeHeight / 2, 0, 0); var polyLineGeometry = new PolyLineGeometry { LineGeometry = CanvasGeometry.CreatePath(builder), DotGeometries = dotList, DataLabels = ChartDrawHelper.CreateValueMarker(resourceCreator, format, markerPoints, markerStrs, MarkerPosition.Bottom), }; geoList.Add(polyLineGeometry); } return(geoList); }
public static List <BarGroupGeometry> GetBarChart(ICanvasResourceCreator resourceCreator, List <float[]> valuesList, float height, float barWidth, float barBorderWidth, float barGap, float groupGap, ChartTextFormat dataLabelFormat) { var geoList = new List <BarGroupGeometry>(); var max = valuesList.Select(o => o.Max()).Max(); var barNum = valuesList.Count; var totalGroupGap = (barWidth + barBorderWidth + barGap) * barNum + groupGap - barGap; var offset = 0f; foreach (var list in valuesList) { var builder = new CanvasPathBuilder(resourceCreator); var heightList = list.Select(o => (float)(o / max * height)).ToArray(); var startPoint = new Vector2(); var markerPoints = new List <Vector2>(); var markerStrs = new List <string>(); for (int i = 0; i < heightList.Count(); i++) { builder.BeginFigure(startPoint); builder.AddLine(new Vector2(startPoint.X, startPoint.Y - heightList[i])); builder.AddLine(new Vector2(startPoint.X + barWidth, startPoint.Y - heightList[i])); builder.AddLine(new Vector2(startPoint.X + barWidth, startPoint.Y)); builder.EndFigure(CanvasFigureLoop.Open); var markerPoint = new Vector2(startPoint.X + barWidth / 2, startPoint.Y - heightList[i]); markerPoints.Add(markerPoint); markerStrs.Add($"{list[i]}"); startPoint = new Vector2(startPoint.X + totalGroupGap, startPoint.Y); } geoList.Add(new BarGroupGeometry { ShapeGeometry = CanvasGeometry.CreatePath(builder), DataLabels = ChartDrawHelper.CreateValueMarker(resourceCreator, dataLabelFormat, markerPoints, markerStrs, MarkerPosition.Bottom), Offset = offset }); builder.Dispose(); offset += barGap + barWidth + barBorderWidth; } return(geoList); }