title: How to create pie & bar chart with Apache POI with XDDFDataSource values from custom List
body: <p>I'm new in Apache-poi, I'm working on a java project using Apache-poi to export data to excel spreadsheet.currently I am able to draw pie chart with fixed values.<br>
  I need to create a pie-chart in java with <code>Apache POI</code>, with custom values for series from list, without reading it from sheet.</p>
  
  <p>Example code I have tried is below.</p>
  
  <pre><code>import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.ss.util.CellRangeAddress;
  import org.apache.poi.xddf.usermodel.chart.LegendPosition;
  import org.apache.poi.xddf.usermodel.chart.XDDFChartData;
  import org.apache.poi.xddf.usermodel.chart.XDDFChartLegend;
  import org.apache.poi.xddf.usermodel.chart.XDDFDataSource;
  import org.apache.poi.xddf.usermodel.chart.XDDFDataSourcesFactory;
  import org.apache.poi.xddf.usermodel.chart.XDDFNumericalDataSource;
  import org.apache.poi.xddf.usermodel.chart.XDDFPieChartData;
  import org.apache.poi.xssf.usermodel.XSSFChart;
  import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
  import org.apache.poi.xssf.usermodel.XSSFDrawing;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  
  public class PieChartSecond {
  
      public static void main(String[] args) throws FileNotFoundException, IOException {
          try (XSSFWorkbook wb = new XSSFWorkbook()) {
  
              XSSFSheet sheet = wb.createSheet("CountryPieChart");
  
              // Create row and put some cells in it. Rows and cells are 0 based.
              Row row = sheet.createRow((short) 0);
  
              Cell cell = row.createCell((short) 0);
              cell.setCellValue("Russia("+17098242+")"
  );
  
              cell = row.createCell((short) 1);
              cell.setCellValue("Canada(" + 9984670 +")"
  );
  
              cell = row.createCell((short) 2);
              cell.setCellValue("USA(" +9826675+")"
  );
  
              row = sheet.createRow((short) 1);
  
              cell = row.createCell((short) 0);
              cell.setCellValue(17098242);
  
              cell = row.createCell((short) 1);
              cell.setCellValue(9984670);
  
              cell = row.createCell((short) 2);
              cell.setCellValue(9826675);
  
              XSSFDrawing drawing = sheet.createDrawingPatriarch();
              XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 20);
  
              XSSFChart chart = drawing.createChart(anchor);
              chart.setTitleText("Countries");
              chart.setTitleOverlay(false);
  
              XDDFChartLegend legend = chart.getOrAddLegend();
              legend.setPosition(LegendPosition.TOP_RIGHT);
  
              XDDFDataSource&lt;String&gt; countries = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                      new CellRangeAddress(0, 0, 0, 2));
  
              XDDFNumericalDataSource&lt;Double&gt; values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
                      new CellRangeAddress(1, 1, 0, 2));
  
  //          XDDFChartData data = chart.createData(ChartTypes.PIE, null, null);
              XDDFChartData data = new XDDFPieChartData(chart.getCTChart().getPlotArea().addNewPieChart());
                data.setVaryColors(true);
  
              data.setVaryColors(true);
              data.addSeries(countries, values);
              chart.plot(data);
  
              // Write output to an excel file
              try (FileOutputStream fileOut = new FileOutputStream("pie-chart-countries.xlsx")) {
                  wb.write(fileOut);
              }
          }
  
      }
  
  }
  </code></pre>
  
  <p>below is my excel snapshot<a href="https://i.stack.imgur.com/slv2j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/slv2j.png" alt="Excel Snapshot of above code"></a></p>
  
  <p>problem is here i'm setting the values to sheet and then fetching it to chart. i don't want to create a table, and i just directly want to insert my values as a list(since it can be changed dynamically)
  i.e<br>
  instead of below lines where i'm fetching values from cell, i need to fetch it from <code>List&lt;String&gt;</code> countries( list of countries) &amp;  <code>List&lt;Double&gt;</code> values and set it to <code>data.addSeries(countries, values)</code></p>
  
  <pre><code>XDDFDataSource&lt;String&gt; countries = XDDFDataSourcesFactory.fromStringCellRange(sheet,
                      new CellRangeAddress(0, 0, 0, 2));
  
              XDDFNumericalDataSource&lt;Double&gt; values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
                      new CellRangeAddress(1, 1, 0, 2));
  </code></pre>
  
  <p>Since Values keep on changing as per changes in Db,is their any way to achieve this.</p>