Working with Charts through Shape.Chart Object

Contents
[ ]

Once the chart was inserted and filled with data, you are able to change its appearance. Shape::Chart property contains all chart related options available through the public API.

For example, let’s change the Chart title or legend behavior:

The code generates the followings results:

line-chart-aspose-words-cpp


FAQ

  1. Q: How can I change the chart title programmatically?
    A: Use the Chart::Title property. Call ChartTitle::set_Text with the desired string and optionally adjust formatting via ChartTitle::get_Font. Example:

    auto chartShape = System::ExplicitCast<Shape>(doc->GetChild(NodeType::Shape, 0, true));
    auto chart = chartShape->get_Chart();
    chart->get_Title()->set_Text(u"Quarterly Sales");
    chart->get_Title()->get_Font()->set_Size(14);
    
  2. Q: How do I hide or show the chart legend?
    A: The legend is controlled by Chart::get_Legend(). To hide it, set its position to LegendPosition::None; to show it, set another position. Example:

    chart->get_Legend()->set_Position(LegendPosition::None); // hides the legend
    
  3. Q: Can I change the chart type after it has been created?
    A: Aspose.Words has no API to change the type of an existing chart. Insert a new chart of the desired type with DocumentBuilder::InsertChart (for instance, ChartType::Bar) and remove the old chart shape.

  4. Q: How can I modify the data series values of an existing chart?
    A: Access the Chart::Series collection, retrieve the desired ChartSeries, clear its values with ClearValues, and add new values with Add. Example:

    auto series = chart->get_Series()->idx_get(0);
    series->ClearValues();
    series->Add(ChartXValue::FromString(u"A"), ChartYValue::FromDouble(150));
    series->Add(ChartXValue::FromString(u"B"), ChartYValue::FromDouble(200));
    series->Add(ChartXValue::FromString(u"C"), ChartYValue::FromDouble(250));
    
  5. Q: Is it possible to format the axis labels (e.g., number format, font)?
    A: Yes. Use Chart::AxisX and Chart::AxisY to adjust label formatting. Example:

    chart->get_AxisX()->get_NumberFormat()->set_FormatCode(u"#,##0");
    chart->get_AxisY()->get_TickLabels()->get_Font()->set_Bold(true);