Setting Charts Appearance

Skip to end of metadata
Go to start of metadata
In Introduction to Charts and Chart Types and How to Create a Chart, we gave a brief introduction of the types of charts and charting objects offered by Aspose.Cells, and described how to create one.

In this article, we discuss how to customize the appearance of charts by setting a number of different 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
  • SeriesCollection area
  • The area of a single point in an SeriesCollection

Code Example

[Java]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();

//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet= worksheets.get(0);
Cells cells = worksheet.getCells();
//Adding a sample value to "A1" cell
cells.get("A1").setValue(50);

//Adding a sample value to "A2" cell
cells.get("A2").setValue(100);

//Adding a sample value to "A3" cell
cells.get("A3").setValue(150);

//Adding a sample value to "B1" cell
cells.get("B1").setValue(60);

//Adding a sample value to "B2" cell
cells.get("B2").setValue(32);

//Adding a sample value to "B3" cell
cells.get("B3").setValue(50);

//Adding a chart to the worksheet
ChartCollection charts = worksheet.getCharts();

//Accessing the instance of the newly added chart
int chartIndex = charts.add(ChartType.COLUMN,5,0,15,5);
Chart chart = charts.get(chartIndex);

//Adding NSeries (chart data source) to the chart ranging from "A1" cell
SeriesCollection nSeries = chart.getNSeries();
nSeries.add("A1:B3",true);

//Setting the foreground color of the plot area
ChartFrame plotArea = chart.getPlotArea();
Area area = plotArea.getArea();
area.setForegroundColor(Color.getBlue());

//Setting the foreground color of the chart area
ChartArea chartArea = chart.getChartArea();
area = chartArea.getArea();
area.setForegroundColor(Color.getYellow());

//Setting the foreground color of the 1st NSeries area
Series aSeries = nSeries.get(0);
area = aSeries.getArea();
area.setForegroundColor(Color.getRed());

//Setting the foreground color of the area of the 1st NSeries point
ChartPointCollection chartPoints =aSeries.getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());

//Save the workbook
workbook.save("book1.xls");
 

After executing the above example code, a column chart will be added to the worksheet as shown below:


Column chart with filled areas

Setting Chart Lines

Developers can also apply different kinds of styles on the lines or data markers of the SeriesCollection as shown below in the example.

Code Examples

[Java]
//Applying a dotted line style on the lines of an NSeries
Series aSeries = nSeries.get(0);
Line line = aSeries.getLine();
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.getLine();
line.setWeight(WeightType.MEDIUM_LINE);
 

After executing the above example code, a column chart will be added to the worksheet as shown below:


Column chart after applying line styles

Applying Microsoft Excel 2007/2010 Themes to Charts

Developers can apply different Microsoft Excel themes and colors to the SeriesCollection or other chart objects as shown in the example below.

Code Example

[JAVA]
//Specify the Excel file path
String filePath = "e:\\test2\\SourceBook1.xlsx";

//Instantiate the workbook to open the file that contains a chart
Workbook workbook = new Workbook(filePath);

//Get the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

//Get the first chart in the sheet
Chart chart = worksheet.getCharts().get(0);

//Specify the FilFormat's type to Solid Fill of the first series
chart.getNSeries().get(0).getArea().getFillFormat().setType(FillType.SOLID);   

//Get the CellsColor of SolidFill 
CellsColor cc = chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().getCellsColor();
           
//Create a theme in Accent style
cc.setThemeColor(new ThemeColor(ThemeColorType.ACCENT_6, 0.6));

//Apply the them to the series
chart.getNSeries().get(0).getArea().getFillFormat().getSolidFill().setCellsColor(cc);

//Save the Excel file
workbook.save(filePath + ".out.xlsx");

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.


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.setText method that can be used to set their titles as shown below in an example.

Code Example

[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.getTextFont();
font.setColor(Color.getBlue());

//Setting the title of category axis of the chart
Axis categoryAxis = chart.getCategoryAxis();
title = categoryAxis.getTitle();
title.setText("Category");

//Setting the title of value axis of the chart
Axis valueAxis = chart.getValueAxis();
title = valueAxis.getTitle();
title.setText("Value");
 

After executing the above example code, a column chart will be added to the worksheet as shown below:


Column chart after setting titles

Setting Major Gridlines

Hiding Major Gridlines

Developers can control the visibility of major gridlines by using the setVisible method of the MajorGridlines object.

Code Example
[Java]
//Hiding the major gridlines of value axis
Axis valueAxis = chart.getValueAxis();
Line majorGridLines = valueAxis.getMajorGridLines();
majorGridLines.setVisible(false);
 

After hiding the major gridlines, a column chart added to the worksheet will have the following appearance:


Column chart with hidden major gridlines

Changing Major Gridlines Settings

Developers cannot only control the visibility of major gridlines but also other properties including its color etc.

Code Example
[Java]
//Setting the color of major gridlines of value axis to silver
Axis categoryAxis = chart.getCategoryAxis();
categoryAxis.getMajorGridLines().setColor(Color.getSilver());
 

After setting the color of major gridlines, a column chart added to the worksheet will have the following appearance:


Column chart with colored major gridlines

Setting Borders for Back and Side Walls

Since the release of MS Excel 2007, the walls of a 3D chart have been divided into two parts: side wall and back wall, so we have to use two Wall objects to represent them separately and you can access them by using Chart.getBackWall() and Chart.getSideWall().

In the example given below, you may find how to set the border of the side wall by using different attributes.

Code Example

[Java]
//Get the side wall border line
Line sideLine =  chart.getSideWall().getBorder();
//Make it visible
sideLine.setVisible(true);
//Set the solid line
sideLine.setStyle(LineType.SOLID);
//Set the line width
sideLine.setWeight(10);
//Set the color
sideLine.setColor(Color.getBlack());
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.