public void ReloadData() { if (DataSource == null) { return; } CALayer parentLayer = _barChartView.Layer; int barsCount = DataSource.NumberOfBarsOnChart(this); for (int i = 0; i < xAxisText.Length; i++) { xAxisText [i].String = DataSource.TimeIntervalAtIndex(i); } _barChartView.UserInteractionEnabled = false; float barHeight = (Bounds.Height - topBarMargin - yAxisMargin) / Convert.ToSingle(barsCount); const float padding = 1.0f; float initialY = barHeight / 2 + topBarMargin; for (int i = 0; i < barsCount; i++) { BarLayer oneLayer = CreateBarLayer(barHeight - padding); parentLayer.AddSublayer(oneLayer); oneLayer.Position = new PointF(0, initialY + i * barHeight); oneLayer.TimeValue = DataSource.ValueForBarAtIndex(this, i); oneLayer.MoneyValue = DataSource.ValueForSecondaryBarAtIndex(this, i); var timeTextLayer = (CATextLayer)oneLayer.Sublayers [BarLayer.TimeTextIndex]; timeTextLayer.String = DataSource.TimeForBarAtIndex(i); timeTextLayer.Hidden = (string.Compare(timeTextLayer.String, "0.00", StringComparison.Ordinal) == 0); timeTextLayer.Position = new PointF(timeTextLayer.Position.X, oneLayer.Bounds.Height / 2); timeTextLayer.FontSize = (barsCount > 12) ? 9.0f : 10.0f; var nsString = new NSString(DataSource.TextForBarAtIndex(this, i)); SizeF size = nsString.GetSizeUsingAttributes(new UIStringAttributes() { Font = LabelFont }); var textLayer = (CATextLayer)oneLayer.Sublayers [BarLayer.DateTextIndex]; textLayer.String = DataSource.TextForBarAtIndex(this, i); textLayer.FontSize = (barsCount > 12) ? 9.0f : 10.0f; textLayer.Bounds = new RectangleF(0, 0, size.Width, size.Height); textLayer.Position = new PointF(0.0f, oneLayer.Bounds.Height / 2); if (barsCount > 12) { timeTextLayer.Opacity = 0.0f; textLayer.Opacity = (i % 3 == 0) ? 1.0f : 0.0f; } else { timeTextLayer.Opacity = (string.Compare(timeTextLayer.String, "0.00", StringComparison.Ordinal) == 0) ? 0.0f : 1.0f; } } _barChartView.UserInteractionEnabled = true; }
private BarLayer CreateBarLayer(float barHeight) { var barLayer = new BarLayer() { ZPosition = 0, BorderColor = UIColor.White.CGColor, AnchorPoint = new PointF(0.0f, 0.5f), Frame = new RectangleF(0, 0, Bounds.Width, barHeight) }; var mainBar = new CALayer(); mainBar.AnchorPoint = new PointF(0.0f, 0.0f); mainBar.Bounds = new RectangleF(0, 0, Bounds.Width - xAxisMargin - xTextMargin, barHeight); mainBar.Position = new PointF(xAxisMargin, 0); mainBar.BackgroundColor = MainBarColor.CGColor; mainBar.SetValueForKeyPath(new NSNumber(minBarScale), new NSString("transform.scale.x")); barLayer.AddSublayer(mainBar); var secondaryBar = new CALayer(); secondaryBar.AnchorPoint = new PointF(0.0f, 0.0f); secondaryBar.Bounds = new RectangleF(0, 0, Bounds.Width - xAxisMargin - xTextMargin, barHeight); secondaryBar.Position = new PointF(xAxisMargin, 0); secondaryBar.BackgroundColor = SecondaryBarColor.CGColor; secondaryBar.SetValueForKeyPath(new NSNumber(minBarScale), new NSString("transform.scale.x")); barLayer.AddSublayer(secondaryBar); var emptyBar = new CALayer(); emptyBar.AnchorPoint = new PointF(0.0f, 0.0f); emptyBar.Bounds = new RectangleF(0, 0, 2.0f, barHeight); emptyBar.Position = new PointF(xAxisMargin, 0); emptyBar.BackgroundColor = UIColor.Gray.CGColor; barLayer.AddSublayer(emptyBar); CGFont font = CGFont.CreateWithFontName(LabelFont.Name); SizeF size = ((NSString)"00.00:00").StringSize(LabelFont); var textLayer = new CATextLayer() { ContentsScale = UIScreen.MainScreen.Scale, FontSize = 10.0f, AnchorPoint = new PointF(0.0f, 0.5f), AlignmentMode = CATextLayer.AlignmentLeft, BackgroundColor = UIColor.Clear.CGColor, ForegroundColor = LabelColor.CGColor }; barLayer.AddSublayer(textLayer); var timeTextLayer = new CATextLayer() { ContentsScale = UIScreen.MainScreen.Scale, FontSize = 10.0f, AnchorPoint = new PointF(0.0f, 0.5f), AlignmentMode = CATextLayer.AlignmentLeft, BackgroundColor = UIColor.Clear.CGColor, ForegroundColor = SecondaryBarColor.CGColor }; barLayer.AddSublayer(timeTextLayer); if (font != null) { textLayer.SetFont(font); timeTextLayer.SetFont(font); font.Dispose(); } CATransaction.DisableActions = true; textLayer.Bounds = new RectangleF(new PointF(0, 0), size); textLayer.Position = mainBar.Position; timeTextLayer.Bounds = new RectangleF(new PointF(0, 0), size); timeTextLayer.Position = new PointF(mainBar.Position.X + 5.0f, mainBar.Position.Y); CATransaction.DisableActions = false; return(barLayer); }
private BarLayer CreateBarLayer ( nfloat barHeight) { var barLayer = new BarLayer () { ZPosition = 0, BorderColor = UIColor.White.CGColor, AnchorPoint = new CGPoint ( 0.0f, 0.5f), Frame = new CGRect ( 0, 0, Bounds.Width, barHeight) }; var mainBar = new CALayer (); mainBar.AnchorPoint = new CGPoint (0.0f, 0.0f); mainBar.Bounds = new CGRect ( 0, 0, Bounds.Width - xAxisMargin - xTextMargin, barHeight); mainBar.Position = new CGPoint ( xAxisMargin, 0); mainBar.BackgroundColor = MainBarColor.CGColor; mainBar.SetValueForKeyPath ( new NSNumber ( minBarScale), new NSString ( "transform.scale.x")); barLayer.AddSublayer (mainBar); var secondaryBar = new CALayer (); secondaryBar.AnchorPoint = new CGPoint (0.0f, 0.0f); secondaryBar.Bounds = new CGRect ( 0, 0, Bounds.Width - xAxisMargin - xTextMargin, barHeight); secondaryBar.Position = new CGPoint ( xAxisMargin, 0); secondaryBar.BackgroundColor = SecondaryBarColor.CGColor; secondaryBar.SetValueForKeyPath ( new NSNumber ( minBarScale), new NSString ( "transform.scale.x")); barLayer.AddSublayer (secondaryBar); var emptyBar = new CALayer (); emptyBar.AnchorPoint = new CGPoint (0.0f, 0.0f); emptyBar.Bounds = new CGRect ( 0, 0, 2.0f, barHeight); emptyBar.Position = new CGPoint ( xAxisMargin, 0); emptyBar.BackgroundColor = UIColor.Gray.CGColor; barLayer.AddSublayer (emptyBar); CGFont font = CGFont.CreateWithFontName (LabelFont.Name); CGSize size = ((NSString)"00.00:00").StringSize (LabelFont); var textLayer = new CATextLayer () { ContentsScale = UIScreen.MainScreen.Scale, FontSize = 10.0f, AnchorPoint = new CGPoint (0.0f, 0.5f), AlignmentMode = CATextLayer.AlignmentLeft, BackgroundColor = UIColor.Clear.CGColor, ForegroundColor = LabelColor.CGColor }; barLayer.AddSublayer (textLayer); var timeTextLayer = new CATextLayer () { ContentsScale = UIScreen.MainScreen.Scale, FontSize = 10.0f, AnchorPoint = new CGPoint (0.0f, 0.5f), AlignmentMode = CATextLayer.AlignmentLeft, BackgroundColor = UIColor.Clear.CGColor, ForegroundColor = SecondaryBarColor.CGColor }; barLayer.AddSublayer (timeTextLayer); if (font != null) { textLayer.SetFont (font); timeTextLayer.SetFont (font); font.Dispose (); } CATransaction.DisableActions = true; textLayer.Bounds = new CGRect (new CGPoint (0, 0), size); textLayer.Position = mainBar.Position; timeTextLayer.Bounds = new CGRect (new CGPoint (0, 0), size); timeTextLayer.Position = new CGPoint ( mainBar.Position.X + 5.0f, mainBar.Position.Y); CATransaction.DisableActions = false; return barLayer; }