ShapeCollection.AddShapeInChartByScale

AddShapeInChartByScale(MsoDrawingType, PlacementType, double, double, double, double)

Add a shape to chart. All unit is percent scale of chart area.

public Shape AddShapeInChartByScale(MsoDrawingType type, PlacementType placement, double left, 
    double top, double right, double bottom)
ParameterTypeDescription
typeMsoDrawingTypeThe drawing type.
placementPlacementTypethe placement type.
leftDoubleUnit is percent scale of chart area width.
topDoubleUnit is percent scale of chart area height.
rightDoubleUnit is percent scale of chart area width.
bottomDoubleUnit is percent scale of chart area height.

Examples

using System;
using Aspose.Cells;
using Aspose.Cells.Drawing;
using System.Drawing;

namespace AsposeCellsExamples
{
    public class ShapeCollectionMethodAddShapeInChartByScaleWithMsoDrawingTypePlacementTypeDouDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Add sample data for the chart
            worksheet.Cells["A1"].PutValue("Category");
            worksheet.Cells["A2"].PutValue("A");
            worksheet.Cells["A3"].PutValue("B");
            worksheet.Cells["A4"].PutValue("C");
            worksheet.Cells["B1"].PutValue("Value");
            worksheet.Cells["B2"].PutValue(10);
            worksheet.Cells["B3"].PutValue(20);
            worksheet.Cells["B4"].PutValue(30);

            // Add a chart
            int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 5, 1, 20, 10);
            Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
            chart.SetChartDataRange("A1:B4", true);

            // Calculate chart
            chart.Calculate();

            // Add a rectangle shape to the chart using scale coordinates
            Shape rectangle = chart.Shapes.AddShapeInChartByScale(
                MsoDrawingType.Rectangle, 
                PlacementType.Move,
                0.2,  // x1 (20% from left)
                0.2,  // y1 (20% from top)
                0.4,  // x2 (40% from left)
                0.4); // y2 (40% from top)
            rectangle.Fill.SolidFill.Color = Color.Blue;
            rectangle.Line.SolidFill.Color = Color.Black;

            // Add an oval shape to the chart using scale coordinates
            Shape oval = chart.Shapes.AddShapeInChartByScale(
                MsoDrawingType.Oval,
                PlacementType.Move,
                0.6,  // x1 (60% from left)
                0.6,  // y1 (60% from top)
                0.8,  // x2 (80% from left)
                0.8); // y2 (80% from top)
            oval.Fill.SolidFill.Color = Color.Red;
            oval.Line.SolidFill.Color = Color.Black;

            // Save the workbook
            workbook.Save("AddShapeInChartByScaleWithMsoDrawingType.xlsx");
        }
    }
}

See Also


AddShapeInChartByScale(MsoDrawingType, PlacementType, double, double, double, double, byte[])

Add a shape to chart .All unit is 1/4000 of chart area.

public Shape AddShapeInChartByScale(MsoDrawingType type, PlacementType placement, double left, 
    double top, double right, double bottom, byte[] imageData)
ParameterTypeDescription
typeMsoDrawingTypeThe drawing type.
placementPlacementTypethe placement type.
leftDoubleUnit is percent scale of chart area width.
topDoubleUnit is percent scale of chart area height.
rightDoubleUnit is percent scale of chart area width.
bottomDoubleUnit is percent scale of chart area height.
imageDataByte[]If the shape is not a picture or ole object,imageData should be null.

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Charts;
    using Aspose.Cells.Drawing;
    using System;
    using System.IO;

    public class ShapeCollectionMethodAddShapeInChartByScaleWithMsoDrawingTypePlacementTypeDouDemo2
    {
        public static void Run()
        {
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            // Add sample data for chart
            worksheet.Cells["A1"].PutValue("Category");
            worksheet.Cells["A2"].PutValue("A");
            worksheet.Cells["A3"].PutValue("B");
            worksheet.Cells["A4"].PutValue("C");
            worksheet.Cells["B1"].PutValue("Value");
            worksheet.Cells["B2"].PutValue(10);
            worksheet.Cells["B3"].PutValue(20);
            worksheet.Cells["B4"].PutValue(30);

            // Add a chart
            int chartIndex = worksheet.Charts.Add(ChartType.Column, 5, 1, 20, 10);
            Chart chart = worksheet.Charts[chartIndex];
            chart.NSeries.Add("B2:B4", true);
            chart.NSeries.CategoryData = "A2:A4";

            // Prepare image data
            byte[] imageData = File.ReadAllBytes("example.jpg");

            try
            {
                // Call AddShapeInChartByScale method
                Shape shape = chart.Shapes.AddShapeInChartByScale(
                    MsoDrawingType.Rectangle,    // Type
                    PlacementType.Move,           // Placement
                    0.1,                          // Left (10% of chart width)
                    0.1,                         // Top (10% of chart height)
                    0.3,                          // Right (30% of chart width)
                    0.3,                         // Bottom (30% of chart height)
                    imageData);                   // Image data

                // Set shape properties
                shape.Name = "ChartRectangle";
                shape.Text = "Sample Shape in Chart";
                shape.Fill.Transparency = 0.5;

                Console.WriteLine("Shape added successfully to chart");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error adding shape to chart: {ex.Message}");
            }

            workbook.Save("AddShapeInChartByScaleWithMsoDrawingType2.xlsx");
        }
    }
}

See Also