Chart Legend

Legend Positioning

In order to set the legend properties. Please follow the steps below:

  • Create an instance of Presentation class.
  • Get the reference of the slide.
  • Adding a chart on slide.
  • Setting the properties of legend.
  • Write the presentation as a PPTX file.

In the example given below, we have set the position and size for Chart legend.

For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = L"../out/legendCustomOptions_out.pptx";
//Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Add chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);
// Set Legend Properties
chart->get_Legend()->set_X ( 50 / chart->get_Width());
chart->get_Legend()->set_Y ( 50 / chart->get_Height());
chart->get_Legend()->set_Width ( 100 / chart->get_Width());
chart->get_Legend()->set_Height ( 100 / chart->get_Height());
// Write the presentation file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Set Font Size of Legend

The Aspose.Slides for C++ lets developers allow to set the font size of the legend. Please follow the steps below: 

  • Instantiate Presentation class.
  • Creating the default chart.
  • Set the Font Size.
  • Set minimum axis value.
  • Set maximum axis value.
  • Write a presentation to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/SettingFontSizeOfLegend_out.pptx";
//Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Add chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);
//Setting legend properties
chart->get_Legend()->get_TextFormat()->get_PortionFormat()->set_FontHeight (20);
chart->get_Axes()->get_VerticalAxis()->set_IsAutomaticMinValue (false);
chart->get_Axes()->get_VerticalAxis()->set_MinValue (-5);
chart->get_Axes()->get_VerticalAxis()->set_IsAutomaticMaxValue (false);
chart->get_Axes()->get_VerticalAxis()->set_MaxValue ( 10);
// Write the presentation file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);

Set Font Size of Individual Legend

The Aspose.Slides for C++ lets developers allow to set the font size of individual legend entries. Please follow the steps below: 

  • Instantiate Presentation class.
  • Creating the default chart.
  • Access legend entry.
  • Set the Font Size.
  • Set minimum axis value.
  • Set maximum axis value.
  • Write a presentation to disk.
For complete examples and data files, please go to https://github.com/aspose-slides/Aspose.Slides-for-C
// The path to the documents directory.
const String outPath = u"../out/SettingFontSizeOfIndividualLegend_out.pptx";
//Instantiate Presentation class that represents PPTX file
SharedPtr<Presentation> pres = MakeObject<Presentation>();
//Access first slide
SharedPtr<ISlide> slide = pres->get_Slides()->idx_get(0);
// Add chart with default data
SharedPtr<IChart> chart = slide->get_Shapes()->AddChart(Aspose::Slides::Charts::ChartType::ClusteredColumn, 0, 0, 500, 500);
//Setting legend properties
SharedPtr<IChartTextFormat> tf = chart->get_Legend()->get_Entries()->idx_get(1)->get_TextFormat();
tf->get_PortionFormat()->set_FontBold (NullableBool::True);
tf->get_PortionFormat()->set_FontHeight(20);
tf->get_PortionFormat()->set_FontItalic(NullableBool::True);
tf->get_PortionFormat()->get_FillFormat()->set_FillType(FillType::Solid) ;
tf->get_PortionFormat()->get_FillFormat()->get_SolidFillColor()->set_Color(System::Drawing::Color::get_Blue());
// Write the presentation file to disk
pres->Save(outPath, Aspose::Slides::Export::SaveFormat::Pptx);