Completed
Last Updated: 26 May 2021 11:01 by ADMIN
Release R2 2021 SP1
Sven
Created on: 11 Mar 2021 06:46
Category: ChartView
Type: Bug Report
0
RadChartView: GridLineAnnotations are not precisely aligned with the ticks on the vertical axis

            radChartView1.AreaType = ChartAreaType.Cartesian;
            var scatterSeries = new ScatterSeries();
            scatterSeries.DataPoints.Add(new ScatterDataPoint(15, 19));
            scatterSeries.DataPoints.Add(new ScatterDataPoint(18, 10));
            scatterSeries.DataPoints.Add(new ScatterDataPoint(13, 15));
            scatterSeries.DataPoints.Add(new ScatterDataPoint(10, 8));
            scatterSeries.DataPoints.Add(new ScatterDataPoint(5, 12));
            radChartView1.Series.Add(scatterSeries);

            var verticalAxis = radChartView1.Axes.Get<LinearAxis>(1);
            verticalAxis.Minimum = 0.0d;
            verticalAxis.Maximum = 5.0d;
            verticalAxis.MajorStep = 0.5d;
            verticalAxis.LabelFormat = "{0:0.00} m";

            for (var i = verticalAxis.Minimum; i <= verticalAxis.Maximum; i += verticalAxis.MajorStep)
            {
                var annotation1 = new CartesianGridLineAnnotation();
                annotation1.Axis = this.radChartView1.Axes[1] as CartesianAxis;
                annotation1.BorderColor = Color.LightGray;
                annotation1.BorderDashStyle = DashStyle.Solid;
                annotation1.BorderWidth = 1;
                annotation1.Value = i;
                radChartView1.Annotations.Add(annotation1);
            }

Workaround:

        public RadForm1()
        {
            InitializeComponent();

            this.radChartView1.CreateRenderer += radChartView1_CreateRenderer; 
            InitChart();  
        }

        private void radChartView1_CreateRenderer(object sender, ChartViewCreateRendererEventArgs e)
        {
            e.Renderer = new CustomRenderer(e.Area as CartesianArea);
        }

        public class CustomRenderer : CartesianRenderer
        {
            public CustomRenderer(CartesianArea area) : base(area)
            {
            }

            protected override void InitializeAnnotations(AnnotationDrawMode drawMode)
            {
                for (int i = 0; i < this.Area.Annotations.Count; i++)
                {
                    if (this.Area.Annotations[i] is CartesianGridLineAnnotation && this.Area.Annotations[i].DrawMode == drawMode)
                    {
                        this.DrawParts.Add(new CustomCartesianGridLineAnnotationDrawPart(this.Area.Annotations[i] as CartesianGridLineAnnotation, this));
                    }
                }
            }
        }

        public class CustomCartesianGridLineAnnotationDrawPart : CartesianGridLineAnnotationDrawPart
        {
            public CustomCartesianGridLineAnnotationDrawPart(CartesianGridLineAnnotation element, CartesianRenderer renderer) : base(element, renderer)
            {
            }

            public override void Draw()
            {
                FieldInfo fi = typeof(CartesianGridLineAnnotation).GetField("model", BindingFlags.Instance | BindingFlags.NonPublic);
                ChartAnnotationModel model = fi.GetValue(this.Element) as ChartAnnotationModel;
                RectangleF rect = ChartRenderer.ToRectangleF(model.LayoutSlot);
                rect.Offset(this.ViewportOffsetX, this.ViewportOffsetY);
                rect.Offset(new PointF(0,1));
                Graphics graphics = this.Renderer.Surface as Graphics;
                RadGdiGraphics radGraphics = new RadGdiGraphics(graphics);

                Rectangle clipRect = ChartRenderer.ToRectangle(this.Element.View.GetArea<CartesianArea>().AreaModel.PlotArea.LayoutSlot);
                clipRect.Offset((int)this.ViewportOffsetX, (int)this.ViewportOffsetY);
                graphics.SetClip(clipRect);

                GraphicsPath path = new GraphicsPath();
                path.AddLine(rect.Location, new PointF(rect.Right, rect.Bottom));

                BorderPrimitiveImpl border = new BorderPrimitiveImpl(this.Element, null);
                border.PaintBorder(radGraphics, null, path, rect);

                rect.Size = graphics.MeasureString(this.Element.Label, this.Element.Font);
                rect.Offset(this.Element.PositonOffset.Width + 1, this.Element.PositonOffset.Height + 1);

                TextParams tp = new TextParams(); 
                tp.font = this.Element.Font;
                tp.foreColor = this.Element.ForeColor;
                tp.paintingRectangle = new RectangleF(rect.X, rect.Y, rect.Height, rect.Width);
                tp.text = this.Element.Label;

                FillPrimitiveImpl fill = new FillPrimitiveImpl(this.Element, null);
                fill.PaintFill(radGraphics, null, rect);

                radGraphics.DrawString(tp, new SizeF(rect.Height, rect.Width));
            }
        }

0 comments