示例#1
0
        public SKPaint GetTextBrush(GraphText graphText)
        {
            SKPaint brush;
            string  key = $"{graphText.Color}{graphText.PointSize}{graphText.Alignment}{graphText.Bold}";

            if (textBrushes.ContainsKey(key))
            {
                brush = textBrushes[key];
            }
            else
            {
                SKTextAlign alignment = SKTextAlign.Left;
                switch (graphText.Alignment)
                {
                case TextAlignment.End:
                    alignment = SKTextAlign.Right;
                    break;

                case TextAlignment.Center:
                    alignment = SKTextAlign.Center;
                    break;
                }
                brush = new SKPaint
                {
                    Color        = graphText.Color,
                    TextSize     = graphText.PointSize,
                    TextAlign    = alignment,
                    FakeBoldText = graphText.Bold
                };
                textBrushes[key] = brush;
            }
            return(brush);
        }
        protected virtual void DrawLeftLabel(List <IGraphItem> graphItems, float graphHeight, float scale, float yPos)
        {
            if (ViewModel.LeftLabel == null || string.IsNullOrWhiteSpace(ViewModel.LeftLabel.Text))
            {
                return;
            }

            SKPaint leftLabelBrush = CreateLeftLabelBrush(scale); // Needed to determine graph width, taking into account Y-Label max width.
            var     graphText      = new GraphText
            {
                Rotation  = 270,
                Alignment = ViewModel.LeftLabel.TextAlignment,
                Color     = ViewModel.LeftLabel.Color,
                PointSize = ViewModel.LeftLabel.PointSize * scale,
                Text      = ViewModel.LeftLabel.Text,
                XPos      = ViewModel.LeftLabel.Height,
                Bold      = ViewModel.LeftLabel.Bold
            };

            switch (ViewModel.LeftLabel.TextAlignment)
            {
            case TextAlignment.Start:
                graphText.YPos = yPos + graphHeight;
                break;

            case TextAlignment.Center:
                graphText.YPos = yPos + graphHeight / 2;
                break;

            case TextAlignment.End:
                graphText.YPos = yPos;
                break;
            }
            graphItems.Add(graphText);
        }
示例#3
0
        public void Draw(SKSurface surface, List <IGraphItem> graphItems)
        {
            SKCanvas canvas = surface.Canvas;

            foreach (var item in graphItems)
            {
                string type = item.GetType().ToString();
                int    pos  = type.LastIndexOf('.');
                type = type.Substring(pos + 1);
                switch (type)
                {
                case "GraphClear":
                    GraphClear graphClear = item as GraphClear;
                    canvas.Clear(graphClear.Color);
                    break;

                case "GraphLine":
                    GraphLine graphLine = item as GraphLine;
                    canvas.DrawLine(graphLine.XPosStart, graphLine.YPosStart, graphLine.XPosEnd, graphLine.YPosEnd, GetLineBrush(graphLine));
                    break;

                case "GraphRectangle":
                    GraphRectangle graphRectangle = item as GraphRectangle;
                    var            rect           = SKRect.Create(graphRectangle.XPos, graphRectangle.YPos, graphRectangle.Width, graphRectangle.Height);
                    canvas.DrawRect(rect, GetRectangleBrush(graphRectangle));
                    break;

                case "GraphText":
                    GraphText graphText = item as GraphText;
                    if (graphText.Rotation == 0)
                    {
                        canvas.DrawText(graphText.Text, graphText.XPos, graphText.YPos, GetTextBrush(graphText));
                    }
                    else
                    {
                        using (new SKAutoCanvasRestore(canvas))     // https://stackoverflow.com/questions/41908497/draw-rotated-text-in-skiasharp
                        {
                            // do any transformations
                            canvas.RotateDegrees(graphText.Rotation, graphText.XPos, graphText.YPos);
                            // do serious work
                            canvas.DrawText(graphText.Text, graphText.XPos, graphText.YPos, GetTextBrush(graphText));
                            // auto restore, even on exceptions or errors
                        }
                    }
                    break;

                case "GraphCircle":
                    GraphCircle graphCircle = item as GraphCircle;
                    canvas.DrawCircle(graphCircle.XPos, graphCircle.YPos, graphCircle.Radius, GetCircleBrush(graphCircle));
                    break;
                }
            }
        }
 public override void Begin()
 {
     Temp = new GraphText();
 }