Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Thu Apr 09, 2020 8:44 pm

Hello,
I’m creating a piechart on PowerPoint with 5 rows of data but, when the PowerPoint is created, only the first 4 appear.

Code: Select all

String [] names = new String []{“name1”,”name2”, “name3”, “name4”, “name5”};
int [] values ={1,2,3,4,5};

pieChart.getChartData().get(0,0).setText(“Names”);
pieChart.getChartData().get(0,1).setText(“Values”);

for (int i=0;i<names.length();i ++){
  pieChart.getChartData().get(i+1,0).setText(names[i]);
  pieChart.getChartData().get(i+1,1).setText(values[i]);
}



The 5 rows are present in the excel but are not plotted on the graph.
If I only add 2 values, 2 other “dummy” values are added. It seems there’s a problem which fixes the number of values to 4.
I have read in another post that I can solve this by adding “false” to the IChart constructor but, when I do that, the PieChart appears empty in the presentation.

How can I solve this ?
Can you help?

lvicinan
 
Posts: 1
Joined: Thu Apr 09, 2020 7:20 pm

Fri Apr 10, 2020 3:01 am

Hello,

Thanks for your inquiry.
Below is the sample code for your reference. If there is any question, please get back to us.
Code: Select all
        //create a PPT document
        Presentation presentation = new Presentation();

        //insert a Pie chart to the first slide and set the chart title
        Rectangle2D rect1 = new Rectangle2D.Double(40, 100, 550, 320);
        IChart chart = presentation.getSlides().get(0).getShapes().appendChart(ChartType.PIE, rect1, false);
        chart.getChartTitle().getTextProperties().setText("Sales by Quarter");
        chart.getChartTitle().getTextProperties().isCentered(true);
        chart.getChartTitle().setHeight(30);
        chart.hasTitle(true);

        //define some data
        String[] names = new String[] { "name1", "name2", "name3", "name4", "name5" };
        int[] values = { 1, 2, 3, 4, 5 };

        //append data to ChartData, which represents a data table where the chart data is stored
        chart.getChartData().get(0, 0).setText("names");
        chart.getChartData().get(0, 1).setText("values");
        for (int i = 0; i < names.length; ++i)
        {
            chart.getChartData().get(i + 1, 0).setValue(names[i]);
            chart.getChartData().get(i + 1, 1).setValue(values[i]);
        }
        //set category labels, series label and series data
        chart.getSeries().setSeriesLabel(chart.getChartData().get("B1", "B1"));
        //!!!********************************************************************************************
        chart.getCategories().setCategoryLabels(chart.getChartData().get("A2", "A6"));
        chart.getSeries().get(0).setValues(chart.getChartData().get("B2", "B6"));
        //!!!********************************************************************************************

        //add data points to series and fill each data point with different color
        for (int i = 0; i < chart.getSeries().get(0).getValues().getCount(); i++)
        {
            ChartDataPoint cdp = new ChartDataPoint(chart.getSeries().get(0));
            cdp.setIndex(i);
            chart.getSeries().get(0).getDataPoints().add(cdp);
        }
        chart.getSeries().get(0).getDataPoints().get(0).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(0).getFill().getSolidColor().setColor(Color.GREEN);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(1).getFill().getSolidColor().setColor(Color.BLUE);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(2).getFill().getSolidColor().setColor(Color.PINK);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(3).getFill().getSolidColor().setColor(Color.YELLOW);
        chart.getSeries().get(0).getDataPoints().get(4).getFill().setFillType( FillFormatType.SOLID);
        chart.getSeries().get(0).getDataPoints().get(4).getFill().getSolidColor().setColor(Color.ORANGE);

        //set the data labels to display label value and percentage value
        chart.getSeries().get(0).getDataLabels().setLabelValueVisible(true);
        chart.getSeries().get(0).getDataLabels().setPercentValueVisible(true);

        //save to file
        presentation.saveToFile("createPieChart.pptx", FileFormat.PPTX_2013);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Apr 24, 2020 10:12 am

Hello,

Greetings from E-iceblue!
Has your issue been resolved? Thanks in advance for your feedback.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Presentation

cron