I am trying to create a pie chart template so that i can write data source dynamically through my java program and template will create the chart once after the data is added. Please refer the example data source,

Subject                Count
ComputerScience          2
History                  3

In the above example the number of subjects may vary(it may have 6 subjects next time), so my chart template should be able to create chart for the number of rows exists in the sheet. I tried to select the data source as some 30 rows(assuming the maximum number of subject is 30), but the chart was not proper and it has created 30 different legends for that. Is there any way to achieve this?

Kindly let me know if i am not clear with my requirements. Thank you.


I'll assume that you're copying and pasting your data into the Excel workbook. If you're importing through a data connection let me know and I'll rewrite the answer accordingly.

  1. Insert a data table. On the ribbon, you do this using Insert > Table.

    Make sure you tick My table has headers.

    enter image description here

  2. Paste your data, including headers into cell A1.

    enter image description here

  3. Select a cell in your table. On the ribbon, click Insert > PivotTable. In the resulting PivotTable, tick the Subject box so that it appears in row labels, then tick the Count box so that it appears in values (make sure it says Sum of Count and not Count of Count. To change it right-click Count of Count and select Summarise Values by > Sum ). You should see a table very like the first table you created.

    enter image description here

  4. Select a cell in your pivot table and insert a chart. Select pie chart. This is a PivotChart, meaning that any changes you make in your PivotTable will also be made to your chart.

    enter image description here

  5. To update your chart, simply paste your new data into your data table, then right-click the PivotTable and click Refresh to update your chart.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.