How to Work with Charts in .NET
This guide shows how to insert a native Word chart and control its appearance with Aspose.Words FOSS for .NET — hiding the legend and axes, fixing the value-axis scale, then choosing a different chart type and formatting data labels and individual points. Aspose.Words FOSS for .NET is not yet published as a NuGet package, so reference it by building from source first.
Step-by-Step Guide
Step 1: Get the Library
Clone the repository and build Aspose.Words.sln in Release configuration, then add a project reference to Aspose.Words.csproj from your own .NET project, as described in the Installation guide.
Step 2: Insert a Chart and Simplify Its Appearance
DocumentBuilder.InsertChart(chartType, width, height) inserts a chart shape at the cursor and returns the containing Shape; its Chart property is the Chart object you configure, with AxisX, AxisY, Legend, and Title as its main sub-objects. Charting types live in the Aspose.Words.Drawing.Charts namespace alongside the core Aspose.Words namespace. A minimal, presentation-ready ChartType.Bar chart on a new Document often means setting Legend.Position to LegendPosition.None, setting Hidden and HasMajorGridlines on AxisY and AxisX, setting Title.Show to false, clearing the placeholder ChartSeriesCollection with Series.Clear() before calling Series.Add(seriesName, categories, values), pinning AxisY.Scaling.Maximum to a fixed AxisBound instead of letting it auto-scale, then writing the result with Document.Save. The full example — Document, DocumentBuilder, Chart, AxisX, AxisY, Legend, Title, ChartSeriesCollection, AxisBound, and Document.Save — looks like this:
using Aspose.Words;
using Aspose.Words.Drawing.Charts;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertChart(ChartType.Bar, 432, 252);
Chart chart = shape.Chart;
chart.Legend.Position = LegendPosition.None;
ChartSeriesCollection seriesColl = chart.Series;
chart.AxisY.Hidden = true;
chart.AxisY.HasMajorGridlines = false;
chart.AxisX.Hidden = true;
chart.Title.Show = false;
seriesColl.Clear();
chart.AxisY.Scaling.Maximum = new AxisBound(100);
string[] categories = new string[] { "AW Category 1" };
seriesColl.Add("AW Series 1", categories, new double[] { 30 });
doc.Save("chart.docx");This builds a Document, inserts a ChartType.Bar chart, sets Chart.Legend.Position to LegendPosition.None to hide the legend, sets Hidden = true and HasMajorGridlines = false on chart.AxisY and Hidden = true on chart.AxisX, sets chart.Title.Show = false, clears the placeholder series with seriesColl.Clear(), fixes the value axis to a 100 maximum via chart.AxisY.Scaling.Maximum = new AxisBound(100), adds one real series with seriesColl.Add(seriesName, categories, values), and writes the result with Document.Save.
Step 3: Choose a Different Chart Type
ChartType covers the standard Word chart families — Line, Bar, Column (plus Stacked, PercentStacked, and 3D variants), Pie, PieOfPie, Doughnut, Area, Scatter, Bubble, Radar, Stock, Surface, and the Word 2016 types Treemap, Sunburst, Histogram, Pareto, BoxAndWhisker, and Waterfall. Pass the value you want as the chartType argument to InsertChart — there’s no member that changes an existing chart’s type after insertion, so pick the right one up front or replace the shape.
Step 4: Add and Format Data Labels
Set series.HasDataLabels = true before configuring series.DataLabels (a ChartDataLabelCollection) — its display toggles have no visible effect while labels are off. ShowValue, ShowCategoryName, and ShowPercentage control which pieces of information appear, and Position places them relative to the data point.
Step 5: Format an Individual Data Point
Index into a series’s DataPoints (a ChartDataPointCollection) to reach one ChartDataPoint and set its Format, Marker, Explosion, or InvertIfNegative independently of the rest of the series. This is how you highlight a single bar, slice, or marker without restyling the whole series.
Common Issues and Fixes
New chart shows unexpected demo data.
InsertChart seeds the chart with placeholder series. Call chart.Series.Clear() before adding your own series.
Hiding AxisY doesn’t remove the gridlines.
Hidden hides the axis line and labels; gridlines are a separate toggle. Set HasMajorGridlines = false (and HasMinorGridlines = false if minor gridlines are also visible).
Data labels don’t appear after setting ShowValue = true.
HasDataLabels wasn’t set on the series first. Set series.HasDataLabels = true, then configure series.DataLabels.
Scaling.Maximum doesn’t seem to apply.
Confirm you’re setting it on the correct axis object (chart.AxisY for the value axis in most chart types) and that no earlier code path resets Scaling back to its default AxisBound().
Frequently Asked Questions
How do I hide a chart’s legend?
Set chart.Legend.Position = LegendPosition.None.
How do I fix an axis to a specific range instead of auto-scaling?
Set AxisScaling.Minimum and/or AxisScaling.Maximum (both AxisBound values) on chart.AxisX.Scaling or chart.AxisY.Scaling.
How do I hide the chart title without deleting it?
Set chart.Title.Show = false. The ChartTitle object and its Text remain; only the visible caption is suppressed.
Which chart types are available?
ChartType lists the classic families (Line, Bar, Column, Pie, Area, Scatter, Bubble, Radar, Stock, Surface, plus stacked and 3D variants) alongside the Word 2016 types (Treemap, Sunburst, Histogram, Pareto, BoxAndWhisker, Waterfall).
Can I format one data point differently from the rest of its series?
Yes — index into ChartSeries.DataPoints and set that ChartDataPoint’s Format, Marker, or Explosion independently.