Live Razor Page Samples
Simple Range Image with Chart
This sample shows how to create an image of a range that includes a chart and stream the image directly to the browser.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Website.Pages.Support.Samples.RazorPages.Imaging
{
public partial class SimpleRangeImageWithChartModel : PageModel
{
public FileResult OnGet()
{
// Create a new workbook.
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
SpreadsheetGear.IRange cells = worksheet.Cells;
// Load some sample data.
SpreadsheetGear.IRange dataCells = cells["B2:E5"];
dataCells.Value = new string[,] {
{ "$3,723", "$5,954", "$3,522", "$7,701" },
{ "$2,681", "$8,665", "$5,836", "$7,851" },
{ "$3,739", "$2,107", "$3,298", "$8,711" },
{ "$1,459", "$7,385", "$2,125", "$9,070" }, };
// Create row headings for four quarters
SpreadsheetGear.IRange quarterCells = cells["A2:A5"];
quarterCells.Value = new string[,] { { "Q1" }, { "Q2" }, { "Q3" }, { "Q4" } };
// Create column headings for four regions.
SpreadsheetGear.IRange regionCells = cells["B1:E1"];
regionCells.Value = new string[,] { { "North", "South", "East", "West" } };
// Add formulas which use the SUM worksheet function to total sales by region.
SpreadsheetGear.IRange totalCells = cells["B6:E6"];
totalCells.Formula = "=SUM(B2:B5)";
totalCells.NumberFormat = "$#,##0_);($#,##0)";
// Add a chart to the worksheet's shape collection.
// NOTE: Calculate the coordinates of the chart by converting row and column
// coordinates to points. Use fractional row and colum values to get
// coordinates anywhere in between row and column boundaries.
double left = 5;
double top = windowInfo.RowToPoints(6.0) + 5;
double right = windowInfo.ColumnToPoints(5.0) - 5;
double bottom = windowInfo.RowToPoints(16.0) - 5;
SpreadsheetGear.Charts.IChart chart = worksheet.Shapes.AddChart(left, top, right - left, bottom - top).Chart;
// Set the chart's source data range, plotting series in rows.
chart.SetSourceData(totalCells, SpreadsheetGear.Charts.RowCol.Rows);
// Set the chart type to a pie chart.
chart.ChartType = SpreadsheetGear.Charts.ChartType.Pie;
// Increase the first pie slice angle.
chart.ChartGroups[0].FirstSliceAngle = 30;
// Get a reference to the first and only series.
SpreadsheetGear.Charts.ISeries series = chart.SeriesCollection[0];
// Link category labels to the region cells.
series.XValues = regionCells;
// Add series data labels and change to show percentage only.
series.HasDataLabels = true;
series.DataLabels.ShowPercentage = true;
series.DataLabels.ShowValue = false;
// Explode the first pie slice (North Region).
series.Points[0].Explosion = 25;
// Add a chart title and change the font size.
chart.HasTitle = true;
chart.ChartTitle.Text = "Percentage of Sales by Region";
chart.ChartTitle.Font.Size = 12;
// Create the image class from a specified range.
SpreadsheetGear.Drawing.Image image =new SpreadsheetGear.Drawing.Image(cells["A1:E16"]);
// Get a new bitmap image of the represented range.
using (System.Drawing.Bitmap bitmap = image.GetBitmap())
{
// Stream the image to the client in PNG format.
System.IO.MemoryStream imageStream = new System.IO.MemoryStream();
bitmap.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
// Reset position to the beginning of the stream.
imageStream.Seek(0, System.IO.SeekOrigin.Begin);
// Stream the image to the client.
return File(imageStream, "image/png");
}
}
}
}