Introduction
In our previous topics, we have given a brief introduction of the types of charts and charting objects offered by Aspose.Cells to create any kind of chart. In this topic, we will discuss about customizing the appearance of charts by setting their properties.
Setting Chart Area
There are different kinds of areas in a chart and Aspose.Cells provides the flexibility of modifying the appearance of each area. Developers can apply different formatting settings on an area by changing its foreground color, background color and fill format etc.
In the example given below, we have applied different formatting settings on different kinds of areas of a chart. These areas include:
- Plot Area
- Chart Area
- NSeries Area
- Area of a Single Point in an NSeries
Example:
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
//Adding a sample value to "A1" cell
worksheet.Cells["A1"].PutValue(50);
//Adding a sample value to "A2" cell
worksheet.Cells["A2"].PutValue(100);
//Adding a sample value to "A3" cell
worksheet.Cells["A3"].PutValue(150);
//Adding a sample value to "B1" cell
worksheet.Cells["B1"].PutValue(60);
//Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(32);
//Adding a sample value to "B3" cell
worksheet.Cells["B3"].PutValue(50);
//Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(ChartType.Column,5,0,15,5);
//Accessing the instance of the newly added chart
Chart chart = worksheet.Charts[chartIndex];
//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3",true);
//Setting the foreground color of the plot area
chart.PlotArea.Area.ForegroundColor = Color.Blue;
//Setting the foreground color of the chart area
chart.ChartArea.Area.ForegroundColor = Color.Yellow;
//Setting the foreground color of the 1st NSeries area
chart.NSeries[0].Area.ForegroundColor = Color.Red;
//Setting the foreground color of the area of the 1st NSeries point
chart.NSeries[0].Points[0].Area.ForegroundColor = Color.Cyan;
//Filling the area of the 2nd NSeries with a gradient
chart.NSeries[1].Area.FillFormat.SetOneColorGradient(Color.Lime, 1, GradientStyleType.Horizontal, 1);
//Saving the Excel file
workbook.Save(saveFileDialog1.FileName,FileFormatType.Default);
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Adding a new worksheet to the Workbook object
Dim sheetIndex As Integer = workbook.Worksheets.Add()
'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)
'Adding a sample value to "A1" cell
worksheet.Cells("A1").PutValue(50)
'Adding a sample value to "A2" cell
worksheet.Cells("A2").PutValue(100)
'Adding a sample value to "A3" cell
worksheet.Cells("A3").PutValue(150)
'Adding a sample value to "B1" cell
worksheet.Cells("B1").PutValue(60)
'Adding a sample value to "B2" cell
worksheet.Cells("B2").PutValue(32)
'Adding a sample value to "B3" cell
worksheet.Cells("B3").PutValue(50)
'Adding a chart to the worksheet
Dim chartIndex As Integer = worksheet.Charts.Add(ChartType.Column,5,0,15,5)
'Accessing the instance of the newly added chart
Dim chart As Chart = worksheet.Charts(chartIndex)
'Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3",True)
'Setting the foreground color of the plot area
chart.PlotArea.Area.ForegroundColor = Color.Blue
'Setting the foreground color of the chart area
chart.ChartArea.Area.ForegroundColor = Color.Yellow
'Setting the foreground color of the 1st NSeries area
chart.NSeries(0).Area.ForegroundColor = Color.Red
'Setting the foreground color of the area of the 1st NSeries point
chart.NSeries(0).Points(0).Area.ForegroundColor = Color.Cyan
'Filling the area of the 2nd NSeries with a gradient
chart.NSeries(1).Area.FillFormat.SetOneColorGradient(Color.Lime, 1, GradientStyleType.Horizontal, 1)
'Saving the Excel file
workbook.Save(saveFileDialog1.FileName,FileFormatType.Default)
[JAVA]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheets worksheets = workbook.getWorksheets();
//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet= worksheets.getSheet(0);
Cells cells = worksheet.getCells();
//Adding a sample value to "A1" cell
cells.getCell("A1").setValue(50);
//Adding a sample value to "A2" cell
cells.getCell("A2").setValue(100);
//Adding a sample value to "A3" cell
cells.getCell("A3").setValue(150);
//Adding a sample value to "B1" cell
cells.getCell("B1").setValue(60);
//Adding a sample value to "B2" cell
cells.getCell("B2").setValue(32);
//Adding a sample value to "B3" cell
cells.getCell("B3").setValue(50);
//Adding a chart to the worksheet
Charts charts = worksheet.getCharts();
int chartIndex= charts.add(ChartType.COLUMN_CLUSTERED,5,0,15,5);
//Accessing the instance of the newly added chart
Chart chart= charts.getChart(chartIndex);
//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B4"
NSeries nSeries = chart.getNSeries();
nSeries.add("A1:B3",true);
//Setting the foreground color of the plot area
PlotArea plotArea = chart.getPlotArea();
Area area = plotArea.getArea();
area.setForegroundColor(Color.BLUE);
//Setting the foreground color of the chart area
ChartArea chartArea = chart.getChartArea();
area = chartArea.getArea();
area.setForegroundColor(Color.YELLOW);
//Setting the foreground color of the 1st NSeries area
ASeries aSeries = nSeries.get(0);
area = aSeries.getArea();
area.setForegroundColor(Color.RED);
//Setting the foreground color of the area of the 1st NSeries point
ChartPoints chartPoints =aSeries.getChartPoints();
ChartPoint point = chartPoints.getChartPoint(0);
point.getArea().setForegroundColor(Color.CYAN);
workbook.save("book1.xls");
After executing the above example code, a column chart will be added to the worksheet as shown below:
|
Figure: Column chart with filled areas
|
Setting Chart Lines
Developers can also apply different kinds of styles on the lines or data markers of the NSeries as shown below in the example.
Example:
[C#]
//Applying a dotted line style on the lines of an NSeries
chart.NSeries[0].Line.Style = LineType.Dot;
//Applying a triangular marker style on the data markers of an NSeries
chart.NSeries[0].MarkerStyle = ChartMarkerType.Triangle;
//Setting the weight of all lines in an NSeries to medium
chart.NSeries[1].Line.Weight = WeightType.MediumLine;
[VB.NET]
'Applying a dotted line style on the lines of an NSeries
chart.NSeries(0).Line.Style = LineType.Dot
'Applying a triangular marker style on the data markers of an NSeries
chart.NSeries(0).MarkerStyle = ChartMarkerType.Triangle
'Setting the weight of all lines in an NSeries to medium
chart.NSeries(1).Line.Weight = WeightType.MediumLine
[JAVA]
//Applying a dotted line style on the lines of an NSeries
ASeries aSeries = nSeries.get(0);
Line line = aSeries.getBorder();
line.setStyle(LineType.DOT);
//Applying a triangular marker style on the data markers of an NSeries
aSeries.setMarkerStyle(ChartMarkerType.TRIANGLE);
//Setting the weight of all lines in an NSeries to medium
aSeries = nSeries.get(1);
line = aSeries.getBorder();
line.setWeight(WeightType.MEDIUME_LINE);
After executing the above example code, a column chart will be added to the worksheet as shown below:
|
Figure: Column chart after applying line styles
|
Setting the Titles of Charts or Axes
You can use Microsoft Excel to set the titles of a chart and its axes in a WYSIWYG environment as shown below
|
Figure: Setting titles of a chart & its axes using Microsoft Excel
|
Aspose.Cells also allows developers to set the titles of a chart and its axes at runtime. All charts and their axes contain a Title property that can be used to set their titles as shown below in an example.
Example:
[C#]
//Setting the title of a chart
chart.Title.Text = "Title";
//Setting the font color of the chart title to blue
chart.Title.TextFont.Color = Color.Blue;
//Setting the title of category axis of the chart
chart.CategoryAxis.Title.Text = "Category";
//Setting the title of value axis of the chart
chart.ValueAxis.Title.Text = "Value";
[VB.NET]
'Setting the title of a chart
chart.Title.Text = "Title"
'Setting the font color of the chart title to blue
chart.Title.TextFont.Color = Color.Blue
'Setting the title of category axis of the chart
chart.CategoryAxis.Title.Text = "Category"
'Setting the title of value axis of the chart
chart.ValueAxis.Title.Text = "Value"
[JAVA]
//Setting the title of a chart
Title title = chart.getTitle();
title.setText("Title");
//Setting the font color of the chart title to blue
Font font = title.getFont();
font.setColor(Color.BLUE);
//Setting the title of category axis of the chart
CategoryAxis categoryAxis = chart.getCategoryAxis();
title = categoryAxis.getTitle();
title.setText("Category");
//Setting the title of value axis of the chart
ValueAxis valueAxis = chart.getValueAxis();
title = valueAxis.getTitle();