Completed
Last Updated: 15 Aug 2017 10:29 by ADMIN
ADMIN
Hristo
Created on: 26 Apr 2017 06:53
Category: ChartView
Type: Bug Report
1
FIX. RadChartView - the exported to an image chart`s rectangle is smaller if the control`s ShowLegend property is set to true
How to reproduce:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.radChartView1.ShowLegend = true;

        LineSeries lineSeries = new LineSeries();
        lineSeries.DataPoints.Add(new CategoricalDataPoint(20, "Jan"));
        lineSeries.DataPoints.Add(new CategoricalDataPoint(22, "Apr"));
        lineSeries.DataPoints.Add(new CategoricalDataPoint(12, "Jul"));
        lineSeries.DataPoints.Add(new CategoricalDataPoint(19, "Oct"));
        this.radChartView1.Series.Add(lineSeries);

        LineSeries lineSeries2 = new LineSeries();
        lineSeries2.DataPoints.Add(new CategoricalDataPoint(18, "Jan"));
        lineSeries2.DataPoints.Add(new CategoricalDataPoint(15, "Apr"));
        lineSeries2.DataPoints.Add(new CategoricalDataPoint(17, "Jul"));
        lineSeries2.DataPoints.Add(new CategoricalDataPoint(22, "Oct"));
        this.radChartView1.Series.Add(lineSeries2);

        ChartPanZoomController panZoomController = new ChartPanZoomController();
        panZoomController.PanZoomMode = ChartPanZoomMode.Horizontal;
        radChartView1.Controllers.Add(panZoomController);
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        this.radChartView1.ExportToImage(@"..\..\image.png", this.radChartView1.Size, System.Drawing.Imaging.ImageFormat.Png);
    }
}

Workaround: use a custom export to image method
public void ExportChartToImage(string filePath, Size size, ImageFormat imageFormat)
{
    using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
    {
        if (!this.radChartView1.IsLoaded)
        {
            this.radChartView1.LoadElementTree();
        }

        Bitmap bmp = new Bitmap(size.Width, size.Height);
        Graphics graphics = Graphics.FromImage(bmp);
        graphics.Clear(Color.White);

        SizeF titleSize = graphics.MeasureString(this.radChartView1.Title, this.radChartView1.ChartElement.TitleElement.Font, this.Width);

        if (this.radChartView1.ChartElement.TitleElement.TextOrientation == Orientation.Vertical)
        {
            float swap = titleSize.Height;
            titleSize.Height = titleSize.Width;
            titleSize.Width = swap;
        }

        RadRect titleRect = new RadRect(0, 0, titleSize.Width, titleSize.Height);
        RadRect legendRect = new RadRect(0, 0, size.Width, size.Height);
        RadRect chartRect = legendRect;

        switch (this.radChartView1.ChartElement.TitlePosition)
        {
            case TitlePosition.Top:
            case TitlePosition.Bottom:
                titleRect.Width = size.Width;
                break;
            case TitlePosition.Right:
            case TitlePosition.Left:
                titleRect.Height = size.Height;
                break;
        }

        chartRect.X += this.radChartView1.View.Margin.Left;
        chartRect.Y += this.radChartView1.View.Margin.Top;
        chartRect.Width -= this.radChartView1.View.Margin.Horizontal;
        chartRect.Height -= this.radChartView1.View.Margin.Vertical;

        if (this.radChartView1.ShowTitle)
        {
            switch (this.radChartView1.ChartElement.TitlePosition)
            {
                case TitlePosition.Top:
                    legendRect.Y += titleRect.Height;
                    chartRect.Y += titleRect.Height;
                    legendRect.Height -= titleRect.Height;
                    chartRect.Height -= titleRect.Height;
                    break;
                case TitlePosition.Right:
                    titleRect.X = size.Width - this.radChartView1.ChartElement.TitleElement.Size.Width;
                    titleRect.Height = size.Height;
                    legendRect.Width -= titleRect.Width;
                    chartRect.Width -= titleRect.Width;
                    break;
                case TitlePosition.Bottom:
                    titleRect.Y = size.Height - this.radChartView1.ChartElement.TitleElement.Size.Height;
                    titleRect.Width = size.Width;
                    legendRect.Height -= titleRect.Height;
                    chartRect.Height -= titleRect.Height;
                    break;
                case TitlePosition.Left:
                    titleRect.Height = size.Height;
                    legendRect.X += titleRect.Width;
                    chartRect.X += titleRect.Width;
                    legendRect.Width -= titleRect.Width;
                    chartRect.Width -= titleRect.Width;
                    break;
            }
        }

        if (this.radChartView1.ShowLegend)
        {
            switch (this.radChartView1.ChartElement.LegendPosition)
            {
                case LegendPosition.Right:
                    if (this.radChartView1.ChartElement.TitlePosition == TitlePosition.Right)
                    {
                        legendRect.X = titleRect.X - this.radChartView1.ChartElement.LegendElement.Size.Width;
                    }
                    else
                    {
                        legendRect.X = size.Width - this.radChartView1.ChartElement.LegendElement.Size.Width;
                    }

                    legendRect.Width = this.radChartView1.ChartElement.LegendElement.Size.Width;
                    chartRect.Width -= this.radChartView1.View.Margin.Right;
                    break;
                case LegendPosition.Bottom:
                    if (this.radChartView1.ChartElement.TitlePosition == TitlePosition.Bottom)
                    {
                        legendRect.Y = titleRect.Y - this.radChartView1.ChartElement.LegendElement.Size.Height;
                    }
                    else
                    {
                        legendRect.Y = size.Height - this.radChartView1.ChartElement.LegendElement.Size.Height;
                    }

                    legendRect.Height = this.radChartView1.ChartElement.LegendElement.Size.Height;
                    chartRect.Height -= legendRect.Height;
                    break;
                case LegendPosition.Left:
                    legendRect.Width = this.radChartView1.ChartElement.LegendElement.Size.Width;
                    chartRect.X += legendRect.Width + this.radChartView1.View.Margin.Left;
                    chartRect.Width -= legendRect.Width + this.radChartView1.View.Margin.Left;
                    break;
                case LegendPosition.Top:
                    legendRect.Height = this.radChartView1.ChartElement.LegendElement.Size.Height;
                    chartRect.Y += legendRect.Height;
                    chartRect.Height -= legendRect.Height;
                    break;
                case LegendPosition.Float:
                    legendRect.Width = this.radChartView1.ChartElement.LegendElement.Size.Width;
                    legendRect.Height = this.radChartView1.ChartElement.LegendElement.Size.Height;
                    double xRatio = size.Width / this.Size.Width;
                    double yRatio = size.Height / this.Size.Height;
                    legendRect.X = (this.radChartView1.ChartElement.LegendOffset.X * xRatio) + ((this.radChartView1.ChartElement.TitlePosition == TitlePosition.Left) ? titleRect.Right : 0d);
                    legendRect.Y = (this.radChartView1.ChartElement.LegendOffset.Y * yRatio) + ((this.radChartView1.ChartElement.TitlePosition == TitlePosition.Top) ? titleRect.Bottom : 0f);
                    break;
            }
        }

        if (this.radChartView1.ShowLegend)
        {
            float xTransform = (float)legendRect.X - this.radChartView1.ChartElement.LegendElement.ControlBoundingRectangle.X + ((float)legendRect.Width - this.radChartView1.ChartElement.LegendElement.ControlBoundingRectangle.Width) / 2f;
            float yTransform = (float)legendRect.Y - this.radChartView1.ChartElement.LegendElement.ControlBoundingRectangle.Y + ((float)legendRect.Height - this.radChartView1.ChartElement.LegendElement.ControlBoundingRectangle.Height) / 2f;
            graphics.TranslateTransform(xTransform, yTransform);
            this.radChartView1.ChartElement.LegendElement.Paint(new RadGdiGraphics(graphics), this.radChartView1.ChartElement.LegendElement.ControlBoundingRectangle, 0f, new SizeF(1f, 1f), true);
            graphics.ResetTransform();
        }

        RadGdiGraphics radGraphics = new RadGdiGraphics(graphics);

        if (this.radChartView1.ShowTitle)
        {
            object[] miParams = new object[] { ChartRenderer.ToRectangleF(titleRect), titleSize, this.radChartView1.ChartElement.TitleElement.TextAlignment };
            radGraphics.DrawString(this.radChartView1.Title, (RectangleF)typeof(RadChartView).GetMethod("GetTitleDrawRectangle", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(this.radChartView1, miParams), this.radChartView1.ChartElement.TitleElement.Font,
                this.radChartView1.ChartElement.TitleElement.ForeColor, this.radChartView1.ChartElement.TitleElement.TextParams.CreateStringFormat(), this.radChartView1.ChartElement.TitleElement.TextOrientation, this.radChartView1.ChartElement.TitleElement.FlipText);
        }

        this.radChartView1.View.Layout(chartRect);

        IChartRenderer renderer = (IChartRenderer)typeof(ChartArea).GetProperty("Renderer", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(this.radChartView1.Area);//
        renderer.Draw(graphics);

        if (imageFormat == ImageFormat.Emf ||
            imageFormat == ImageFormat.Wmf)
        {
            Metafile metafile = new Metafile(fs, graphics.GetHdc());
            using (Graphics g = Graphics.FromImage(metafile))
            {
                g.DrawImage(bmp, Point.Empty);
            }

            metafile.Dispose();
            graphics.ReleaseHdc();
        }
        else
        {
            bmp.Save(fs, imageFormat);
        }

        this.radChartView1.View.Layout();
    }
0 comments