Document Solutions for Excel, Java Edition | Document Solutions
Features / Chart / Customize Chart Objects / Legends
In This Topic
    Legends
    In This Topic

    Legends are the visual charting elements (keys associated with the data plotted on a chart) that automatically appear in spreadsheets when a user finishes the process of adding a chart.

    Generally, legends help in quick interpretation of the charted data and are located at the right side of the chart. Also, they allow end users to figure out the series and series points representing different groups of data in a worksheet.

    Further, legends depict series names by listing and identifying the data points that belong to a particular series. Corresponding to the data, each legend entry appearing on the worksheet can be shown with the help of a legend marker along with the legend text that identifies it.

    In DsExcel Java, you can customize the legend text, configure the position and layout of the legend, reset the font style for the legend entries, delete legend and its entries as and when you want using the methods of the ILegend interface, ILegendEntries interface and the IChart interface. 

    In order to configure some useful legend settings in your chart, refer to the following example code.

    Java
    Copy Code
    IShape shape = worksheet.getShapes().addChart(ChartType.Column3D, 200, 30, 300, 300);
    Object[][] data = new Object[][] { { null, "S1", "S2", "S3" }, { "Item1", 10, 25, 25 },
            { "Item2", -51, -36, 27 }, { "Item3", 52, -85, -30 }, { "Item4", 22, 65, 65 },
            { "Item5", 23, 69, 69 } };
    worksheet.getRange("A1:D6").setValue(data);
    shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    
    shape.getChart().setHasLegend(true);
    ILegend legend = shape.getChart().getLegend();
            
    // position.
    legend.setPosition(LegendPosition.Left);
            
    // font.
    legend.getFont().getColor().setRGB(Color.GetRed());
    legend.getFont().setItalic(true);
            
    // format.
    legend.getFormat().getFill().getColor().setRGB(Color.GetPink());
    legend.getFormat().getLine().getColor().setRGB(Color.GetBlue());
            
    // Config legend entry font style.
    ILegendEntry legendentry = legend.getLegendEntries().get(0);
    legendentry.getFont().getColor().setRGB(Color.GetRed());
    legendentry.getFont().setSize(15);

    In case you want to delete the legend or a specific legend entry from your chart, refer to the following example code.

    Java
    Copy Code
    // Delete legend.
    IShape shape1 = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    shape1.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    shape1.getChart().setHasLegend(true);
    ILegend legend1 = shape1.getChart().getLegend();
    legend1.delete();
            
    // Delete lengend entry.
    IShape shape2 = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    shape2.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    shape2.getChart().setHasLegend(true);
    ILegend legend2 = shape.getChart().getLegend();
    ILegendEntry legendentry2 = legend.getLegendEntries().get(0);
    legendentry2.delete();