Exemplo n.º 1
0
        protected override void drawChart(CanvasDrawingSession canvas)
        {
            if (chartData == null)
            {
                return;
            }

            int transitionAlpha = 255;

            //canvas.save();
            if (transitionMode == TRANSITION_MODE_CHILD)
            {
                transitionAlpha  = (int)(transitionParams.progress * transitionParams.progress * 255);
                canvas.Transform = Matrix3x2.CreateScale(
                    new Vector2(transitionParams.progress, transitionParams.progress),
                    new Vector2(chartArea.centerX(), chartArea.centerY())
                    );
            }

            if (isEmpty)
            {
                if (emptyDataAlpha != 0)
                {
                    emptyDataAlpha -= 0.12f;
                    if (emptyDataAlpha < 0)
                    {
                        emptyDataAlpha = 0;
                    }
                    invalidate();
                }
            }
            else
            {
                if (emptyDataAlpha != 1f)
                {
                    emptyDataAlpha += 0.12f;
                    if (emptyDataAlpha > 1f)
                    {
                        emptyDataAlpha = 1f;
                    }
                    invalidate();
                }
            }

            transitionAlpha = (int)(transitionAlpha * emptyDataAlpha);
            float sc = 0.4f + emptyDataAlpha * 0.6f;

            canvas.Transform = Matrix3x2.CreateScale(
                new Vector2(sc, sc),
                new Vector2(chartArea.centerX(), chartArea.centerY())
                );

            int radius = (int)((chartArea.Width > chartArea.Height ? chartArea.Height : chartArea.Width) * 0.45f);

            rectF = createRect(
                chartArea.centerX() - radius,
                chartArea.centerY() + 16 - radius,
                chartArea.centerX() + radius,
                chartArea.centerY() + 16 + radius
                );


            float a = -90f;
            float rText;

            int n = lines.Count;

            float localSum = 0f;

            for (int i = 0; i < n; i++)
            {
                float v = lines[i].drawingPart * lines[i].alpha;
                localSum += v;
            }
            if (localSum == 0)
            {
                //canvas.restore();
                return;
            }
            for (int i = 0; i < n; i++)
            {
                if (lines[i].alpha <= 0 && !lines[i].enabled)
                {
                    continue;
                }
                lines[i].paint.A = (byte)transitionAlpha;

                float currentPercent = lines[i].drawingPart / localSum * lines[i].alpha;
                darawingValuesPercentage[i] = currentPercent;

                if (currentPercent == 0)
                {
                    continue;
                }

                //canvas.save();

                double textAngle = a + (currentPercent / 2f) * 360f;

                if (lines[i].selectionA > 0f)
                {
                    float ai = INTERPOLATOR.getInterpolation(lines[i].selectionA);
                    canvas.Transform = Matrix3x2.CreateTranslation(
                        (float)(Math.Cos(MathEx.ToRadians(textAngle)) * 8 * ai),
                        (float)(Math.Sin(MathEx.ToRadians(textAngle)) * 8 * ai)
                        );
                }

                //lines[i].paint.setStyle(Paint.Style.FILL_AND_STROKE);
                lines[i].paint.StrokeWidth = 1;
                //lines[i].paint.setAntiAlias(!USE_LINES);

                //canvas.drawArc(
                //        rectF,
                //        a,
                //        (currentPercent) * 360f,
                //        true,
                //        lines[i].paint);

                var b = a + (currentPercent) * 360f;

                var builder = new CanvasPathBuilder(canvas);
                var center  = new Vector2((float)rectF.X + (float)rectF.Width / 2, (float)rectF.Y + (float)rectF.Height / 2);
                builder.BeginFigure(center);
                builder.AddLine(
                    new Vector2(
                        (float)(center.X + Math.Sin(a * Math.PI / 180) * (float)rectF.Width / 2),
                        (float)(center.Y - Math.Cos(a * Math.PI / 180) * (float)rectF.Height / 2)));

                builder.AddArc(
                    new Vector2(
                        (float)(center.X + Math.Sin(b * Math.PI / 180) * (float)rectF.Width / 2),
                        (float)(center.Y - Math.Cos(b * Math.PI / 180) * (float)rectF.Height / 2)),
                    (float)rectF.Width / 2,
                    (float)rectF.Height / 2,
                    0, CanvasSweepDirection.Clockwise,
                    (b - a) >= 180.0 ? CanvasArcSize.Large : CanvasArcSize.Small);

                builder.EndFigure(CanvasFigureLoop.Closed);
                canvas.FillGeometry(CanvasGeometry.CreatePath(builder), lines[i].paint.Color);

                //lines[i].paint.setStyle(Paint.Style.STROKE);

                //canvas.restore();
                canvas.Transform = Matrix3x2.Identity;

                lines[i].paint.A = 255;
                a += currentPercent * 360f;
            }
            a = -90f;

            for (int i = 0; i < n; i++)
            {
                if (lines[i].alpha <= 0 && !lines[i].enabled)
                {
                    continue;
                }
                float currentPercent = (lines[i].drawingPart * lines[i].alpha / localSum);
                //canvas.save();

                double textAngle = a + (currentPercent / 2f) * 360f;

                if (lines[i].selectionA > 0f)
                {
                    float ai = INTERPOLATOR.getInterpolation(lines[i].selectionA);
                    canvas.Transform = Matrix3x2.CreateTranslation(
                        (float)(Math.Cos(MathEx.ToRadians(textAngle)) * 8 * ai),
                        (float)(Math.Sin(MathEx.ToRadians(textAngle)) * 8 * ai)
                        );
                }

                int percent = (int)(100f * currentPercent);
                if (currentPercent >= 0.02f && percent > 0 && percent <= 100)
                {
                    rText = (float)(rectF.Width * 0.42f * Math.Sqrt(1f - currentPercent));
                    //textPaint.setTextSize(MIN_TEXT_SIZE + currentPercent * MAX_TEXT_SIZE);
                    //textPaint.setAlpha((int)(transitionAlpha * lines[i].alpha));
                    //canvas.DrawText(
                    //        lookupTable[percent],
                    //        (float)(rectF.centerX() + rText * Math.Cos(MathEx.ToRadians(textAngle))),
                    //        (float)(rectF.centerY() + rText * Math.Sin(MathEx.ToRadians(textAngle))) - ((textPaint.descent() + textPaint.ascent()) / 2),
                    //        textPaint);
                }

                //canvas.restore();
                canvas.Transform = Matrix3x2.Identity;

                lines[i].paint.A = 255;
                a += currentPercent * 360f;
            }

            //canvas.restore();
            canvas.Transform = Matrix3x2.Identity;
        }