Class Chart
Namespace: Aspose.Cells.Charts
Assembly: Aspose.Cells.dll (25.2.0)
Encapsulates the object that represents a single Excel chart.
public class Chart
Inheritance
Inherited Members
object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()
Examples
The following codes show how to create a chart with .Net codes.
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
Cells cells = sheet.Cells;
cells[0,1].PutValue("Income");
cells[1,0].PutValue("Company A");
cells[2,0].PutValue("Company B");
cells[3,0].PutValue("Company C");
cells[1,1].PutValue(10000);
cells[2,1].PutValue(20000);
cells[3,1].PutValue(30000);
int chartIndex = sheet.Charts.Add(ChartType.Column, 9, 9, 21, 15);
Chart chart = sheet.Charts[chartIndex];
chart.SetChartDataRange("A1:B4", true);
chart.ShowLegend = true;
chart.Title.Text = "Income Analysis";
Dim workbook as Workbook = new Workbook()
Dim sheet as Worksheet = workbook.Worksheets(0)
Dim cells as Cells = sheet.Cells
cells(0,1).PutValue("Income")
cells(1,0).PutValue("Company A")
cells(2,0).PutValue("Company B")
cells(3,0).PutValue("Company C")
cells(1,1).PutValue(10000)
cells(2,1).PutValue(20000)
cells(3,1).PutValue(30000)
Dim chartIndex as Integer = sheet.Charts.Add(ChartType.Column, 9, 9, 21, 15)
Dim chart as Chart = sheet.Charts(chartIndex)
chart.SetChartDataRange("A1:B4", True);
chart.ShowLegend = True
chart.Title.Text = "Income Analysis"
Properties
ActualChartSize
Gets actual size of chart in unit of pixels.
[Obsolete("Use Chart.GetActualSize() method instead.")]
public Size ActualChartSize { get; }
Property Value
Remarks
NOTE: This member is now obsolete. Instead, please use Chart.GetActualSize() method. This property will be removed 12 months later since July 2022. Aspose apologizes for any inconvenience you may have experienced.
AutoScaling
True if Microsoft Excel scales a 3-D chart so that it’s closer in size to the equivalent 2-D chart. The RightAngleAxes property must be True.
public bool AutoScaling { get; set; }
Property Value
BackWall
Returns a Aspose.Cells.Charts.Chart.Walls object that represents the back wall of a 3-D chart.
public Walls BackWall { get; }
Property Value
CategoryAxis
Gets the chart’s X axis.
public Axis CategoryAxis { get; }
Property Value
ChartArea
Gets the chart area in the worksheet.
public ChartArea ChartArea { get; }
Property Value
ChartDataTable
Represents the chart data table.
public ChartDataTable ChartDataTable { get; }
Property Value
ChartObject
Represents the chartShape;
public ChartShape ChartObject { get; }
Property Value
DepthPercent
Represents the depth of a 3-D chart as a percentage of the chart width (between 20 and 2000 percent).
public int DepthPercent { get; set; }
Property Value
DisplayNaAsBlank
Indicates whether displaying #N/A as blank value.
public bool DisplayNaAsBlank { get; set; }
Property Value
Elevation
Represents the elevation of the 3-D chart view, in degrees.
public int Elevation { get; set; }
Property Value
Remarks
The chart elevation is the height at which you view the chart, in degrees. The default is 15 for most chart types. The value of this property must be between -90 and 90, except for 3-D bar charts, where it must be between 0 and 44.
FilteredNSeries
Gets a Aspose.Cells.Charts.SeriesCollection collection representing the data series that are filtered in the chart.
public SeriesCollection FilteredNSeries { get; }
Property Value
FirstSliceAngle
Gets or sets the angle of the first pie-chart or doughnut-chart slice, in degrees (clockwise from vertical). Applies only to pie, 3-D pie, and doughnut charts, 0 to 360.
public int FirstSliceAngle { get; set; }
Property Value
Floor
Returns a Aspose.Cells.Charts.Chart.Floor object that represents the walls of a 3-D chart.
public Floor Floor { get; }
Property Value
Remarks
This property doesn’t apply to 3-D pie charts.
GapDepth
Gets or sets the distance between the data series in a 3-D chart, as a percentage of the marker width. The value of this property must be between 0 and 500.
public int GapDepth { get; set; }
Property Value
GapWidth
Returns or sets the space between bar or column clusters, as a percentage of the bar or column width. The value of this property must be between 0 and 500.
public int GapWidth { get; set; }
Property Value
HeightPercent
Returns or sets the height of a 3-D chart as a percentage of the chart width (between 5 and 500 percent).
public int HeightPercent { get; set; }
Property Value
HidePivotFieldButtons
Indicates whether hide the pivot chart field buttons only when the chart is PivotChart.
public bool HidePivotFieldButtons { get; set; }
Property Value
Is3D
Indicates whether the chart is a 3d chart.
public bool Is3D { get; }
Property Value
IsRectangularCornered
Gets or sets a value indicating whether the chart area is rectangular cornered. Default is true.
public bool IsRectangularCornered { get; set; }
Property Value
Legend
Gets the chart legend.
public Legend Legend { get; }
Property Value
Line
Gets the line.
public Line Line { get; }
Property Value
NSeries
Gets a Aspose.Cells.Charts.SeriesCollection collection representing the data series in the chart.
public SeriesCollection NSeries { get; }
Property Value
Name
Gets and sets the name of the chart.
public string Name { get; set; }
Property Value
PageSetup
Represents the page setup description in this chart.
public PageSetup PageSetup { get; }
Property Value
Perspective
Returns or sets the perspective for the 3-D chart view. Must be between 0 and 100. This property is ignored if the RightAngleAxes property is True.
public short Perspective { get; set; }
Property Value
PivotOptions
Specifies the pivot controls that appear on the chart
public PivotOptions PivotOptions { get; }
Property Value
PivotSource
The source is the data of the pivotTable. If PivotSource is not empty ,the chart is PivotChart.
public string PivotSource { get; set; }
Property Value
Remarks
If the pivot table “PivotTable1” in the Worksheet “Sheet1” in the file “Book1.xls”. The pivotSource could be “[Book1.xls]Sheet1!PivotTable1” if the chart and the PivotTable is not in the same workbook. If you set this property ,the previous data source setting will be lost.
Placement
Represents the way the chart is attached to the cells below it.
public PlacementType Placement { get; set; }
Property Value
PlotArea
Gets the chart’s plot area which includes axis tick labels.
public PlotArea PlotArea { get; }
Property Value
PlotBy
Gets and sets whether plot by row or column.
public PlotDataByType PlotBy { get; }
Property Value
PlotEmptyCellsType
Gets and sets how to plot the empty cells.
public PlotEmptyCellsType PlotEmptyCellsType { get; set; }
Property Value
PlotVisibleCellsOnly
Indicates whether plot visible cells only.
public bool PlotVisibleCellsOnly { get; set; }
Property Value
PrintSize
Gets and sets the printed chart size.
public PrintSizeType PrintSize { get; set; }
Property Value
RightAngleAxes
True if the chart axes are at right angles. Applies only for 3-D charts(except Column3D and 3-D Pie Charts).
public bool RightAngleAxes { get; set; }
Property Value
Remarks
If this property is True, the Perspective property is ignored.
RotationAngle
Represents the rotation of the 3-D chart view (the rotation of the plot area around the z-axis, in degrees).
public int RotationAngle { get; set; }
Property Value
Remarks
The value of this property must be from 0 to 360, except for 3-D bar charts, where the value must be from 0 to 44. The default value is 20. Applies only to 3-D charts.
SecondCategoryAxis
Gets the chart’s second X axis.
public Axis SecondCategoryAxis { get; }
Property Value
SecondValueAxis
Gets the chart’s second Y axis.
public Axis SecondValueAxis { get; }
Property Value
SeriesAxis
Gets the chart’s series axis.
public Axis SeriesAxis { get; }
Property Value
Shapes
Returns all drawing shapes in this chart.
public ShapeCollection Shapes { get; }
Property Value
ShowDataTable
Gets or sets a value indicating whether the chart displays a data table.
public bool ShowDataTable { get; set; }
Property Value
ShowLegend
Gets or sets a value indicating whether the chart legend will be displayed. Default is true.
public bool ShowLegend { get; set; }
Property Value
SideWall
Returns a Aspose.Cells.Charts.Chart.Walls object that represents the side wall of a 3-D chart.
public Walls SideWall { get; }
Property Value
SizeWithWindow
True if Microsoft Excel resizes the chart to match the size of the chart sheet window.
public bool SizeWithWindow { get; set; }
Property Value
Style
Gets and sets the builtin style.
public int Style { get; set; }
Property Value
Remarks
It should be between 1 and 48. Return -1 if it’s not be set.
SubTitle
Gets the chart’s sub-title. Only for ODS format file.
public Title SubTitle { get; }
Property Value
Title
Gets the chart’s title.
public Title Title { get; }
Property Value
Type
Gets or sets a chart’s type.
public ChartType Type { get; set; }
Property Value
ValueAxis
Gets the chart’s Y axis.
public Axis ValueAxis { get; }
Property Value
Walls
Returns a Aspose.Cells.Charts.Chart.Walls object that represents the walls of a 3-D chart.
public Walls Walls { get; }
Property Value
Remarks
This property doesn’t apply to 3-D pie charts.
WallsAndGridlines2D
True if gridlines are drawn two-dimensionally on a 3-D chart.
public bool WallsAndGridlines2D { get; set; }
Property Value
Worksheet
Gets the worksheet which contains this chart.
public Worksheet Worksheet { get; }
Property Value
Methods
Calculate()
Calculates the custom position of plot area, axes if the position of them are auto assigned.
public void Calculate()
Calculate(ChartCalculateOptions)
Calculates the custom position of plot area, axes if the position of them are auto assigned, with Chart Calculate Options.
public void Calculate(ChartCalculateOptions calculateOptions)
Parameters
calculateOptions
ChartCalculateOptions
ChangeTemplate(byte[])
Change chart type with preset template.
public void ChangeTemplate(byte[] data)
Parameters
data
byte[]
The data of chart template file(.crtx).
GetActualSize()
Gets actual size of chart in unit of pixels.
public int[] GetActualSize()
Returns
int[]
Actual size in an array(width and height). [0] is width; [1] is height.
GetChartDataRange()
Gets the data source range of the chart.
public string GetChartDataRange()
Returns
The data source.
Remarks
Only supports range.
HasAxis(AxisType, bool)
Returns which axes exist on the chart.
public bool HasAxis(AxisType aixsType, bool isPrimary)
Parameters
aixsType
AxisType
isPrimary
bool
Returns
Remarks
Normally, Pie, PieExploded, PiePie,PieBar, Pie3D, Pie3DExploded,Doughnut, DoughnutExploded is no axis.
IsCellReferedByChart(int, int, int)
Returns whether the cell refered by the chart.
public bool IsCellReferedByChart(int sheetIndex, int rowIndex, int columnIndex)
Parameters
sheetIndex
int
The sheet Index.-1 means the worksheet which contains current chart.
rowIndex
int
The row index
columnIndex
int
The column index
Returns
IsChartDataChanged()
Detects if a chart’s data source has changed.
public bool IsChartDataChanged()
Returns
Returns true if the chart has changed otherwise returns false
Remarks
The method detects the changes in the chart’s data source before rendering the chart to image format. At first Chart.toImage call, the chart source data (e.g. XValuesParseData, ValuesParseData) will be recorded. Before calling the Chart.toImage method again, call IsChartDataChanged method to check if Chart needs re-rendering.
Move(int, int, int, int)
Moves the chart to a specified location.
public void Move(int upperLeftRow, int upperLeftColumn, int lowerRightRow, int lowerRightColumn)
Parameters
upperLeftRow
int
Upper left row index.
upperLeftColumn
int
Upper left column index.
lowerRightRow
int
Lower right row index
lowerRightColumn
int
Lower right column index
RefreshPivotData()
Refreshes chart’s data from pivot table.
public void RefreshPivotData()
Remarks
We will gather data from pivot data source to the pivot table report. This method is only used to gather all data to a pivot chart.
SetChartDataRange(string, bool)
Specifies data range for a chart.
public void SetChartDataRange(string area, bool isVertical)
Parameters
area
string
Specifies values from which to plot the data series
isVertical
bool
Specifies whether to plot the series from a range of cell values by row or by column.
SwitchRowColumn()
Switches row/column.
public bool SwitchRowColumn()
Returns
False means switching row/column fails.
ToImage(string)
Creates the chart image and saves it to a file. The extension of the file name determines the format of the image.
public void ToImage(string imageFile)
Parameters
imageFile
string
The image file name with full path.
Remarks
The format of the image is specified by using the extension of the file name. For example, if you specify "myfile.png", then the image will be saved in the PNG format. The following file extensions are recognized: .bmp, .gif, .png, .jpg, .jpeg, .tiff, .tif, .emf.
If the width or height is zero or the chart is not supported according to Supported Charts List, this method will do nothing.ToImage(string, ImageType)
Creates the chart image and saves it to a file in the specified image type.
public void ToImage(string imageFile, ImageType imageType)
Parameters
imageFile
string
The image file name with full path.
imageType
ImageType
The image type in which to save the image.
Remarks
The type of the image is specified by using imageType
.
The following types are supported:
ImageType.Bmp, ImageType.Gif, ImageType.Png, ImageType.Jpeg, ImageType.Tiff, ImageType.Emf.
ToImage(string, long)
Creates the chart image and saves it to a file in the Jpeg format.
public void ToImage(string imageFile, long jpegQuality)
Parameters
imageFile
string
The image file name with full path.
jpegQuality
long
Jpeg quality.
Remarks
If the width or height is zero or the chart is not supported according to Supported Charts List, this method will do nothing.
ToImage(Stream, long)
Creates the chart image and saves it to a stream in the Jpeg format.
public void ToImage(Stream stream, long jpegQuality)
Parameters
stream
Stream
The output stream.
jpegQuality
long
Jpeg quality.
Remarks
If the width or height is zero or the chart is not supported according to Supported Charts List, this method will do nothing.
ToImage(Stream, ImageType)
Creates the chart image and saves it to a stream in the specified format.
public void ToImage(Stream stream, ImageType imageType)
Parameters
stream
Stream
The output stream.
imageType
ImageType
The image type in which to save the image.
Remarks
The type of the image is specified by using imageType
.
The following types are supported:
ImageType.Bmp, ImageType.Gif, ImageType.Png, ImageType.Jpeg, ImageType.Tiff, ImageType.Emf.
ToImage(string, ImageOrPrintOptions)
Creates the chart image and saves it to a file. The extension of the file name determines the format of the image.
public void ToImage(string imageFile, ImageOrPrintOptions options)
Parameters
imageFile
string
The image file name with full path.
options
ImageOrPrintOptions
Additional image creation options
Examples
Saves to Tiff with 300 dpi and CCITT4 compression.
csharp
[C#]
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.HorizontalResolution = 300;
options.VerticalResolution = 300;
options.TiffCompression = TiffCompression.CompressionCCITT4;
Workbook book = new Workbook(@"test.xls");
book.Worksheets[0].Charts[0].ToImage(@"chart.Tiff", options);
[VB]
Dim options As ImageOrPrintOptions = New ImageOrPrintOptions()
options.HorizontalResolution = 300
options.VerticalResolution = 300
options.TiffCompression = TiffCompression.CompressionCCITT4
Dim book As Workbook = New Workbook("test.xls")
book.Worksheets(0).Charts(0).ToImage("chart.Tiff", options)
Saves to Jpeg with 300 dpi and 80 image quality.
csharp
[C#]
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.HorizontalResolution = 300;
options.VerticalResolution = 300;
options.Quality = 80;
Workbook book = new Workbook(@"test.xls");
book.Worksheets[0].Charts[0].ToImage(@"chart.Jpeg", options);
[VB]
Dim options As ImageOrPrintOptions = New ImageOrPrintOptions()
options.HorizontalResolution = 300
options.VerticalResolution = 300
options.Quality = 80
Dim book As Workbook = New Workbook("test.xls")
book.Worksheets(0).Charts(0).ToImage("chart.Jpeg", options)
Remarks
The format of the image is specified by using the extension of the file name. For example, if you specify "myfile.png", then the image will be saved in the PNG format. The following file extensions are recognized: .bmp, .gif, .png, .jpg, .jpeg, .tiff, .tif, .emf.
If the width or height is zero or the chart is not supported according to Supported Charts List, this method will do nothing. Please refer to Supported Charts List for more details.ToImage(Stream, ImageOrPrintOptions)
Creates the chart image and saves it to a stream in the specified format.
public void ToImage(Stream stream, ImageOrPrintOptions options)
Parameters
stream
Stream
The output stream.
options
ImageOrPrintOptions
Additional image creation options
Remarks
The type of the image is specified by using options.ImageType
.
The following formats are supported:
ImageType.Bmp, ImageType.Gif, ImageType.Png, ImageType.Jpeg, ImageType.Tiff, ImageType.Emf.
ToPdf(string)
Saves the chart to a pdf file.
public void ToPdf(string fileName)
Parameters
fileName
string
the pdf file name with full path
ToPdf(string, float, float, PageLayoutAlignmentType, PageLayoutAlignmentType)
Saves the chart to a pdf file.
public void ToPdf(string fileName, float desiredPageWidth, float desiredPageHeight, PageLayoutAlignmentType hAlignmentType, PageLayoutAlignmentType vAlignmentType)
Parameters
fileName
string
the pdf file name with full path
desiredPageWidth
float
The desired page width in inches.
desiredPageHeight
float
The desired page height in inches.
hAlignmentType
PageLayoutAlignmentType
The chart horizontal alignment type in the output page.
vAlignmentType
PageLayoutAlignmentType
The chart vertical alignment type in the output page.
ToPdf(Stream)
Creates the chart pdf and saves it to a stream.
public void ToPdf(Stream stream)
Parameters
stream
Stream
The output stream.
ToPdf(Stream, float, float, PageLayoutAlignmentType, PageLayoutAlignmentType)
Creates the chart pdf and saves it to a stream.
public void ToPdf(Stream stream, float desiredPageWidth, float desiredPageHeight, PageLayoutAlignmentType hAlignmentType, PageLayoutAlignmentType vAlignmentType)
Parameters
stream
Stream
The output stream.
desiredPageWidth
float
The desired page width in inches.
desiredPageHeight
float
The desired page height in inches.
hAlignmentType
PageLayoutAlignmentType
The chart horizontal alignment type in the output page.
vAlignmentType
PageLayoutAlignmentType
The chart vertical alignment type in the output page.