Exemple #1
0
        private void updateLabelForLayer(SliceLayer sliceLayer, nfloat value)
        {
            var textLayer = (CATextLayer)sliceLayer.Sublayers [1];

            textLayer.Hidden = !ShowLabel;
            if (!ShowLabel)
            {
                return;
            }

            String label    = ShowPercentage ? sliceLayer.Percentage.ToString("P1") : sliceLayer.Value.ToString("0.00");
            var    nsString = new NSString(label);
            CGSize size     = nsString.GetSizeUsingAttributes(new UIStringAttributes()
            {
                Font = LabelFont
            });

            if (Math.PI * 2 * LabelRadius * sliceLayer.Percentage < Math.Max(size.Width, size.Height) || value <= 0)
            {
                textLayer.String = "";
            }
            else
            {
                textLayer.String = label;
                textLayer.Bounds = new CGRect(0, 0, size.Width, size.Height);
            }
        }
Exemple #2
0
        private int containsLayer(List <SliceLayer> list, SliceLayer layer)
        {
            int result = -1;

            for (int i = 0; i < list.Count; i++)
            {
                var item = list [i];
                if (layer.StartAngle.CompareTo(item.StartAngle) == 0 &&
                    layer.EndAngle.CompareTo(item.EndAngle) == 0)
                {
                    result = i;
                }
            }
            return(result);
        }
Exemple #3
0
        private SliceLayer createSliceLayer()
        {
            var pieLayer = new SliceLayer();

            pieLayer.Frame = _pieView.Frame;
            var arcLayer = new CAShapeLayer();

            arcLayer.ZPosition   = 0;
            arcLayer.StrokeColor = null;
            pieLayer.AddSublayer(arcLayer);

            var textLayer = new CATextLayer();

            textLayer.ContentsScale = UIScreen.MainScreen.Scale;
            CGFont font = CGFont.CreateWithFontName(LabelFont.Name);

            if (font != null)
            {
                textLayer.SetFont(font);
                font.Dispose();
            }

            textLayer.FontSize        = LabelFont.PointSize;
            textLayer.AnchorPoint     = new CGPoint(0.5f, 0.5f);
            textLayer.AlignmentMode   = CATextLayer.AlignmentCenter;
            textLayer.BackgroundColor = UIColor.Clear.CGColor;
            textLayer.ForegroundColor = LabelColor.CGColor;

            if (LabelShadowColor != null)
            {
                textLayer.ShadowColor   = LabelShadowColor.CGColor;
                textLayer.ShadowOffset  = CGSize.Empty;
                textLayer.ShadowOpacity = 1.0f;
                textLayer.ShadowRadius  = 2.0f;
            }

            CGSize size = ((NSString)"0").StringSize(LabelFont);

            textLayer.Frame    = new CGRect(new CGPoint(0, 0), size);
            textLayer.Position = new CGPoint(_pieCenter.X + LabelRadius * (nfloat)Math.Cos(0), _pieCenter.Y + LabelRadius * (nfloat)Math.Sin(0));
            pieLayer.AddSublayer(textLayer);
            layerPool.Add(pieLayer);
            return(pieLayer);
        }
        private void updateLabelForLayer (SliceLayer sliceLayer, nfloat value)
        {
            var textLayer = (CATextLayer)sliceLayer.Sublayers [1];
            textLayer.Hidden = !ShowLabel;
            if (!ShowLabel) {
                return;
            }

            String label = ShowPercentage ? sliceLayer.Percentage.ToString ("P1") : sliceLayer.Value.ToString ("0.00");
            var nsString = new NSString (label);
            CGSize size = nsString.GetSizeUsingAttributes (new UIStringAttributes () { Font = LabelFont });

            if (Math.PI * 2 * LabelRadius * sliceLayer.Percentage < Math.Max (size.Width, size.Height) || value <= 0) {
                textLayer.String = "";
            } else {
                textLayer.String = label;
                textLayer.Bounds = new CGRect (0, 0, size.Width, size.Height);
            }
        }
        private SliceLayer createSliceLayer ()
        {
            var pieLayer = new SliceLayer ();
            pieLayer.Frame = _pieView.Frame;
            var arcLayer = new CAShapeLayer ();
            arcLayer.ZPosition = 0;
            arcLayer.StrokeColor = null;
            pieLayer.AddSublayer (arcLayer);

            var textLayer = new CATextLayer ();
            textLayer.ContentsScale = UIScreen.MainScreen.Scale;
            CGFont font = CGFont.CreateWithFontName (LabelFont.Name);

            if (font != null) {
                textLayer.SetFont (font);
                font.Dispose ();
            }

            textLayer.FontSize = LabelFont.PointSize;
            textLayer.AnchorPoint = new CGPoint (0.5f, 0.5f);
            textLayer.AlignmentMode = CATextLayer.AlignmentCenter;
            textLayer.BackgroundColor = UIColor.Clear.CGColor;
            textLayer.ForegroundColor = LabelColor.CGColor;

            if (LabelShadowColor != null) {
                textLayer.ShadowColor = LabelShadowColor.CGColor;
                textLayer.ShadowOffset = CGSize.Empty;
                textLayer.ShadowOpacity = 1.0f;
                textLayer.ShadowRadius = 2.0f;
            }

            CGSize size = ((NSString)"0").StringSize (LabelFont);

            textLayer.Frame = new CGRect (new CGPoint (0, 0), size);
            textLayer.Position = new CGPoint (_pieCenter.X + LabelRadius * (nfloat)Math.Cos (0), _pieCenter.Y + LabelRadius * (nfloat)Math.Sin (0));
            pieLayer.AddSublayer (textLayer);
            layerPool.Add (pieLayer);
            return pieLayer;
        }
 private int containsLayer ( List<SliceLayer> list, SliceLayer layer)
 {
     int result = -1;
     for (int i = 0; i < list.Count; i++) {
         var item = list [i];
         if (layer.StartAngle.CompareTo ( item.StartAngle) == 0 &&
                 layer.EndAngle.CompareTo ( item.EndAngle) == 0) {
             result = i;
         }
     }
     return result;
 }
Exemple #7
0
        public void ReloadData()
        {
            if (DataSource == null)
            {
                return;
            }

            CALayer parentLayer = _pieView.Layer;
            var     sliceLayers = parentLayer.Sublayers ?? new CALayer[0];

            _selectedSliceIndex = -1;
            for (int i = 0; i < sliceLayers.Length; i++)
            {
                var layer = (SliceLayer)sliceLayers [i];
                if (layer.IsSelected)
                {
                    SetSliceDeselectedAtIndex(i);
                    layer.Opacity = 1;
                }
            }

            double startToAngle = 0.0f;
            double endToAngle   = startToAngle;

            nint   sliceCount = DataSource.NumberOfSlicesInPieChart(this);
            nfloat sum        = 0.0f;
            var    values     = new nfloat[sliceCount];

            for (nint index = 0; index < sliceCount; index++)
            {
                values [index] = DataSource.ValueForSliceAtIndex(this, index);
                sum           += values [index];
            }

            var angles = new nfloat[sliceCount];

            for (nint index = 0; index < sliceCount; index++)
            {
                nfloat div;
                if (sum == 0.0f)
                {
                    div = 0;
                }
                else
                {
                    div = values [index] / sum;
                }
                angles [index] = (nfloat)Math.PI * 2 * div;
            }

            _pieView.UserInteractionEnabled = false;

            var  layersToRemove = new List <SliceLayer> ();
            bool isOnStart      = (sliceLayers.Length == 0 && sliceCount > 0);
            nint diff           = sliceCount - sliceLayers.Length;

            for (int i = 0; i < sliceLayers.Length; i++)
            {
                layersToRemove.Add((SliceLayer)sliceLayers [i]);
            }

            bool isOnEnd = (sliceLayers.Length > 0) && (sliceCount == 0 || sum <= 0);

            if (isOnEnd)
            {
                foreach (var item in sliceLayers)
                {
                    var layer = (SliceLayer)item;
                    updateLabelForLayer(layer, 0.0f);
                    layer.CreateArcAnimationForKey("startAngle", StartPieAngle, StartPieAngle, _animationDelegate);
                    layer.CreateArcAnimationForKey("endAngle", StartPieAngle, StartPieAngle, _animationDelegate);
                }
                return;
            }

            for (int index = 0; index < sliceCount; index++)
            {
                SliceLayer layer = null;
                double     angle = angles [index];
                endToAngle += angle;
                double startFromAngle = StartPieAngle + startToAngle;
                double endFromAngle   = StartPieAngle + endToAngle;

                if (index >= sliceLayers.Length)
                {
                    layer = createSliceLayer();
                    if (isOnStart)
                    {
                        startFromAngle = endFromAngle = StartPieAngle;
                    }
                    parentLayer.AddSublayer(layer);
                    diff--;
                }
                else
                {
                    var onelayer = (SliceLayer)sliceLayers [index];
                    if (diff == 0 || onelayer.Value == values [index])
                    {
                        layer = onelayer;
                        layersToRemove.Remove(layer);
                    }
                    else if (diff > 0)
                    {
                        layer = createSliceLayer();
                        parentLayer.InsertSublayer(layer, index);
                        diff--;
                    }
                    else if (diff < 0)
                    {
                        while (diff < 0)
                        {
                            onelayer.RemoveFromSuperLayer();
                            parentLayer.AddSublayer(onelayer);  // TODO: check removing this code with the new Xamarin compiler
                            diff++;
                            onelayer = (SliceLayer)sliceLayers [index];
                            if (onelayer.Value == values [index] || diff == 0)
                            {
                                layer = onelayer;
                                layersToRemove.Remove(layer);
                                break;
                            }
                        }
                    }
                }

                layer.Value      = values [index];
                layer.Percentage = (sum > 0) ? layer.Value / sum : 0;
                UIColor color;

                if (DataSource.ColorForSliceAtIndex(this, index) != null)
                {
                    color = DataSource.ColorForSliceAtIndex(this, index);
                }
                else
                {
                    color = UIColor.FromHSBA((nfloat)(index / 8f % 20.0f / 20.0 + 0.02f), (nfloat)((index % 8 + 3) / 10.0), (nfloat)(91 / 100.0), 1);
                }

                layer.ChangeToColor(color);

                if (!String.IsNullOrEmpty(DataSource.TextForSliceAtIndex(this, index)))
                {
                    layer.Text = DataSource.TextForSliceAtIndex(this, index);
                }

                updateLabelForLayer(layer, values [index]);
                layer.CreateArcAnimationForKey("startAngle", startFromAngle, startToAngle + StartPieAngle, _animationDelegate);
                layer.CreateArcAnimationForKey("endAngle", endFromAngle, endToAngle + StartPieAngle, _animationDelegate);
                startToAngle = endToAngle;
            }

            foreach (var layer in layersToRemove)
            {
                var shapeLayer = (CAShapeLayer)layer.Sublayers [0];
                layerPool.Remove(layer);
                shapeLayer.FillColor = PieBackgroundColor.CGColor;
                shapeLayer.Path.Dispose();
                layer.Delegate             = null;
                layer.ZPosition            = 0;
                layer.Sublayers [1].Hidden = true;
                layer.RemoveFromSuperLayer();
            }
            layersToRemove.Clear();

            foreach (var layer in sliceLayers)
            {
                layer.ZPosition = defaultSliceZOrder;
            }

            _pieView.UserInteractionEnabled = true;
        }