Skip to main content
A newer version of this page is available. .

How to: Format Chart Elements

  • 6 minutes to read

This example demonstrates how to enhance the appearance of an existing chart. For an example on how to create a basic chart in code, refer to the How to: Create a Basic Chart article.

SpreadsheetDocServer_ExcelChartCustomAppearance

Select the action you wish to perform.

Apply the Built-in Chart Style

Chart styles allow you to quickly change chart appearance. The chart style changes the chart’s background fill, specifies the color of the data series, and applies different shape effects and outlines to the chart. To apply one of the predefined styles to the chart, utilize the ChartObject.Style property.

The example below demonstrates how to create a chart and apply one of the predefined styles to it using the ChartObject.Style property.

Worksheet worksheet = workbook.Worksheets["chartTask3"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:D4"]);
chart.TopLeftCell = worksheet.Cells["H2"];
chart.BottomRightCell = worksheet.Cells["N14"];

// Set the chart style.
chart.Style = ChartStyle.Accent1Dark;

Format an Individual Chart Element

The chart style allows you to apply a predefined set of format options. However, you can fine-tune these settings and specify custom formatting for individual chart elements by giving them a different color or outline. A full set of format characteristics available for a chart is provided by the ShapeFormat object. All objects that represent chart elements (Chart, PlotArea, Series, Axis, DataLabel, Legend, ChartTitle, etc.) inherit the ShapeFormat interface, so you can use its formatting properties to set a color and border of the required chart element.

Worksheet worksheet = workbook.Worksheets["chartTask7"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:C8"]);
chart.TopLeftCell = worksheet.Cells["F2"];
chart.BottomRightCell = worksheet.Cells["N17"];

// Add and format the chart title.
chart.Title.SetValue("Сountries with the largest forest area");
chart.Title.Font.Color = Color.FromArgb(0x34, 0x5E, 0x25);

// Set no fill for the plot area.
chart.PlotArea.Fill.SetNoFill();

// Apply the gradient fill to the chart area.
chart.Fill.SetGradientFill(ShapeGradientType.Linear, Color.FromArgb(0xFD, 0xEA, 0xDA), Color.FromArgb(0x77, 0x93, 0x3C));
ShapeGradientFill gradientFill = chart.Fill.GradientFill;
gradientFill.Stops.Add(0.78f, Color.FromArgb(0xB7, 0xDE, 0xE8));
gradientFill.Angle = 90;

// Set the picture fill for the data series.
chart.Series[0].Fill.SetPictureFill("Pictures\\PictureFill.png");

// Customize the axis appearance.
AxisCollection axisCollection = chart.PrimaryAxes;
foreach (Axis axis in axisCollection)
{
    axis.MajorTickMarks = AxisTickMarks.None;
    axis.Outline.SetSolidFill(Color.FromArgb(0x34, 0x5E, 0x25));
    axis.Outline.Width = 1.25;
}
// Change the scale of the value axis.
Axis valueAxis = axisCollection[1];
valueAxis.Scaling.AutoMax = false;
valueAxis.Scaling.Max = 8000000;
valueAxis.Scaling.AutoMin = false;
valueAxis.Scaling.Min = 0;
// Specify display units for the value axis.
valueAxis.DisplayUnits.UnitType = DisplayUnitType.Thousands;
valueAxis.DisplayUnits.ShowLabel = true;

// Hide the legend.
chart.Legend.Visible = false;

Specify the Chart View Options

There are specific settings for different chart types that affect chart appearance. Use the properties of the ChartView and View3DOptions objects to specify chart options for 2-D and 3-D charts. For example, you can adjust the distance between column clusters in the column chart by using the ChartView.GapWidth property, or apply a different color to each data marker in the series by setting the ChartView.VaryColors property to true.

The example below demonstrates how to create a clustered column chart and automatically apply a different color to each data marker representing a data point on the chart. To vary colors of the same-series data markers point by point, set the ChartView.VaryColors property to true.

Worksheet worksheet = workbook.Worksheets["chartTask5"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:C8"]);
chart.TopLeftCell = worksheet.Cells["F2"];
chart.BottomRightCell = worksheet.Cells["L15"];

// Specify that each data point in the series has a different color.
chart.Views[0].VaryColors = true;
// Hide the legend.
chart.Legend.Visible = false;
See Also