Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:15:31+00:00 2026-06-13T05:15:31+00:00

I am wondering whether there is a way to resize a chart using Apache

  • 0

I am wondering whether there is a way to resize a chart using Apache POI (XSSF). Currently I am using an Excel template, which has a chart that changes when more data is inserted using namedRanges.

Everything works fine, the only troubles I’m facing are:

  • The chart always stays the same size, so if there are more entries, it gets cluttered making the chart kind of useless.
  • I am using dates, but I am not able to represent the date as day/month(17/10) on the chart. Basically instead of 04/01/2001, it writes 36982.

The purpose of the workbook is to list several jobs and check whether they took longer on a given date, the graph is for helping to identify the ocurrences of longer elapsed times. The jobs runtime might range from seconds to hours.

This is the code I am using:

package le_package.poi_tests.xssflibrary;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class POIReadFile {
    public static void main(String[] args) {
        try
        {           
            String jobName = "I am a job"; 
            String jobParent = "I am your father, Job.";        
            int rowNum = 40;
            int deface = 4;

            //Open Excel as OOXML
            XSSFWorkbook currentWorkbook = new XSSFWorkbook( OPCPackage.open("include/excelTemplate.xlsx"));

            //Get sheet in position 0
            Sheet currentSheet = currentWorkbook.getSheetAt(0);

            //Get sheet name for processing
            String sheetName = currentSheet.getSheetName();

            //Set values for headers
            currentSheet.getRow(1).getCell(0).setCellValue(jobName);            
            currentSheet.getRow(1).getCell(1).setCellValue(jobParent);              

            for (int i=0; i<rowNum; i++)
            {
                //Create row in a given position
                Row newRow = currentSheet.createRow(i+deface);

                //Create cell within row
                Cell newCell0 = newRow.createCell(0);
                Cell newCell1 = newRow.createCell(1);
                Cell newCell2 = newRow.createCell(2);
                String cellDate = "";

                /* Set CellType
                 *  0 - Numeric | 1 - String | 2 - Formula | 3 - Blank | 4 - Boolean | 5 - Error */
                newCell0.setCellType(0);
                cellDate = "3/"+(i+1)+"/2001 00:00:00";

                //Convert text into date
                Date currentCellDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(cellDate);
                //System.out.println(currentCellDate.toString()+"--"+cellDate);

                //Set CellValue
                newCell0.setCellValue(currentCellDate);


                cellDate = "4/"+(i+1)+"/2001 00:00:00"; 
                currentCellDate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse(cellDate);
                //System.out.println(currentCellDate.toString()+"--"+cellDate);             
                newCell1.setCellType(0);
                newCell1.setCellValue(currentCellDate);

                //setCellFormula sets the formula to be evaluated by excel, it doesn't need to start with '=' 
                newCell2.setCellFormula("A" + (i+deface+1) + "-B" + (i+deface+1));              
            }           

            //Search for named range
            Name rangeCell = currentWorkbook.getName("startRange");         
            //Set new range for named range 
            String reference = sheetName + "!$A$" + ( deface+1 ) + ":$A$" + ( rowNum+deface );          
            //Assigns range value to named range
            rangeCell.setRefersToFormula(reference);

            rangeCell = currentWorkbook.getName("endRange");            
            reference = sheetName + "!$B$"+(deface+1) + ":$B$" + (rowNum+deface);
            rangeCell.setRefersToFormula(reference);            

            rangeCell = currentWorkbook.getName("elapsedTime");
            reference = sheetName + "!$C$"+(deface+1) + ":$C$" + (rowNum+deface);
            rangeCell.setRefersToFormula(reference);

            //Create a fileStream to write into a file
            FileOutputStream newExcelFile = new FileOutputStream(jobName+".xlsx");

            //Write Stream
            currentWorkbook.write(newExcelFile);

            //Close New Excel File
            newExcelFile.close();           
        }
        catch (Exception e)
        {
            System.out.println("AAAAARGH, I was wounded by the following exception!:");
            e.printStackTrace();
            System.out.println("Sorry, your program is dead :(");
        }
    }
}

Is it possible to do what I need?

Thanks.

*Note: I am not asking to create a chart from the scratch, I only need to resize the one the template has, and change some cells to date instead of the number that is written.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:15:32+00:00Added an answer on June 13, 2026 at 5:15 am

    After researching how xlsx works, I was able to find how to get it done.

    //Call the partiarch to start drawing
    XSSFDrawing drawing = ((XSSFSheet)currentSheet).createDrawingPatriarch();
    //Create CTMarket for anchor
    CTMarker chartEndCoords = CTMarker.Factory.newInstance();
    //The coordinates are set in columns and rows, not pixels.
    chartEndCoords.setCol(column);
    //Set Column offset
    chartEndCoords.setColOff(0);
    chartEndCoords.setRow(row);
    chartEndCoords.setRowOff(0);
    //drawing.getCTDrawing().getTwoCellAnchorArray(0).setFrom(chartStartCoords);
    drawing.getCTDrawing().getTwoCellAnchorArray(0).setTo(chartEndCoords);
    
    /*
        This line of code allows to resize the chart:
            The Patriarch is what allows to get control over the drawings, since
            a chart is considered a graph in xlsx you can access it with getCTDrawing.
            Each graph is stored in the tag getTwoCellAnchorArray, where the array position
            is the chart you have; for example getTwoCellAnchorArray(3) would refer to the
            forth graph within the sheet.
    
            Each getTwoCellAnchorArray has several properties as FROM and TO, which define
            where the existing graph starts and ends.   
    */
    

    If you have any comments, let me know.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using TFS 2010 and am wondering whether there is an easy way, either
I was wondering whether there's a way of using multiple selectors in jQuery and
I am currently using SQL server 2008, and I was wondering whether there was
I'm wondering whether there is a way to poll a socket in c# when
I was wondering whether there is a way to check when the database was
Just wondering is there any way I can check whether the url links to
I was wondering whether there is a way to detect a touch in an
Hey, I was wondering whether there is a way to use FlexScroll (JavaScript custom
I was wondering whether there's a way to trigger the parent page. For example,
I was wondering whether there is any way to create custom Orders statuses in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.