Set offset of the filled image could make your chart background pictures zoom and show proportional as you wish. This article shows how to set the offset of image via Spire.XLS.
Here are the steps:
Step 1: Create an instance of Spire.XLS.Workbook.
Workbook book = new Workbook();
Step 2: Load data and create a contrast sheet.
workbook.LoadFromFile("test.xlsx",ExcelVersion.Version2013); Worksheet sheet = workbook.Worksheets[0]; Worksheet sheet1 = workbook.Worksheets.Add("Contrast");
Step 3: Add chart1 and background image to sheet1 as comparision.
Chart chart1 = sheet1.Charts.Add(ExcelChartType.ColumnClustered); chart1.DataRange = sheet.Range["D1:E9"]; chart1.SeriesDataFromRange = false; chart1.LeftColumn = 1; chart1.TopRow = 11; chart1.RightColumn = 8; chart1.BottomRow = 33; chart1.ChartArea.Fill.CustomPicture(Image.FromFile("2.jpg"), "None");
Step 4: Add same chart and background image then set offset of image by transforming image in sheet[0] as form of XlsShapeFill. Then set the property of PicStretch of each direction with percentage and Tile property.
Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered); chart.DataRange = sheet.Range["D1:E9"]; chart.SeriesDataFromRange = false; chart.LeftColumn = 1; chart.TopRow = 11; chart.RightColumn = 8; chart.BottomRow = 33; chart.ChartArea.Fill.CustomPicture(Image.FromFile("2.jpg"), "None"); IChart ichart = sheet.Charts[0]; (ichart.ChartArea.Fill as XlsShapeFill).Tile = false; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Left = 10; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Top = 20; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Right = 10; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Bottom = 5;
Step 5: Save and review.
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013); System.Diagnostics.Process.Start("result.xlsx");
Screenshot:
Full code:
using Spire.Xls; using Spire.Xls.Core; using Spire.Xls.Core.Spreadsheet.Shapes; using System.Drawing; namespace SetOffsetofImage { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2013); Worksheet sheet = workbook.Worksheets[0]; Worksheet sheet1 = workbook.Worksheets.Add("Contrast"); //add contrast Chart chart1 = sheet1.Charts.Add(ExcelChartType.ColumnClustered); chart1.DataRange = sheet.Range["D1:E9"]; chart1.SeriesDataFromRange = false; //Chart Position chart1.LeftColumn = 1; chart1.TopRow = 11; chart1.RightColumn = 8; chart1.BottomRow = 33; chart1.ChartArea.Fill.CustomPicture(Image.FromFile("2.jpg"), "None"); //add original Chart chart = sheet.Charts.Add(ExcelChartType.ColumnClustered); chart.DataRange = sheet.Range["D1:E9"]; chart.SeriesDataFromRange = false; //Chart Position chart.LeftColumn = 1; chart.TopRow = 11; chart.RightColumn = 8; chart.BottomRow = 33; chart.ChartArea.Fill.CustomPicture(Image.FromFile("2.jpg"), "None"); IChart ichart = sheet.Charts[0]; (ichart.ChartArea.Fill as XlsShapeFill).Tile = false; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Left = 10; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Top = 20; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Right = 10; (ichart.ChartArea.Fill as XlsShapeFill).PicStretch.Bottom = 5; workbook.SaveToFile("result.xlsx", ExcelVersion.Version2013); System.Diagnostics.Process.Start("result.xlsx"); } } }