Issue in creating combo line and column charts in java using aspose.cells

Hi Aspose team, I am facing an issue in creating combo line and column charts using aspose cells in java, I am using maven and java version 1.8.351
Which chart type should I use for this?

@sm350
Please check document

@simon.zhao I want to create the following type of chart
image.png (7.3 KB)

@sm350
Could you upload a sample Excel file with your excepted chart? We will check it soon.

@sm350
You can first create a Column Chart and then change the type of another Series. Please refer to the attachment (16.2 KB).

The sample code as follows:

// Create the workbook
Workbook workbook = new Workbook(filePath + "combo.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Add a Column chart
int columnIndex = worksheet.getCharts().add(ChartType.COLUMN, 15, 0, 34, 12);
// Retrieve the Chart object
Chart chart = worksheet.getCharts().get(columnIndex);
// Set the legend can be showed
chart.setShowLegend(true);
// Set the chart title name 
chart.getTitle().setText("Combo Chart");
// Set the Legend at the bottom of the chart area
chart.getLegend().setPosition(LegendPositionType.BOTTOM);
// Set data range
chart.setChartDataRange("A1:C12", true);
// Set category data 
chart.getNSeries().setCategoryData("A2:A12");

//get the first Series
Series firstSeries = chart.getNSeries().get(0);
// Set the chart type
firstSeries.setType(ChartType.LINE);
// Set style for the border of first series
firstSeries.getBorder().setStyle(LineType.SOLID);
// Set Color for the first series
firstSeries.getBorder().setColor(Color.getDarkBlue());
// Fill the PlotArea area with nothing 
chart.getPlotArea().getArea().setFormatting(FormattingType.NONE);
// Save the Excel file
workbook.save(filePath + "out_java.xlsx");

Hope helps a bit.

excel sheet with chart.zip (11.7 KB)

@simon.zhao

image.png (7.4 KB)

@John.He how do I make the chart for this data? Please advice

@John.He I am still getting column type charts after using your code
image.png (8.2 KB)

@sm350
You can use the following sample code to achieve your goals. Please refer to the attachment (14.1 KB).

The java sample code as follows:

// Create the workbook
Workbook workbook = new Workbook(filePath + "New Microsoft Excel Worksheet.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Add a Column chart
int columnIndex = worksheet.getCharts().add(ChartType.COLUMN, 18, 5, 34, 12);
// Retrieve the Chart object
Chart chart = worksheet.getCharts().get(columnIndex);
// Set the legend can be showed
chart.setShowLegend(true);
// Set the chart title name 
chart.getTitle().setText("Combo Chart");
// Set the Legend at the bottom of the chart area
chart.getLegend().setPosition(LegendPositionType.BOTTOM);
// Set data range
chart.setChartDataRange("A2:E11", true);
// Set category data 
chart.getNSeries().setCategoryData("A2:A11");

//get the first Series
Series firstSeries = chart.getNSeries().get(0);
// Set the chart type
firstSeries.setType(ChartType.LINE);
// Set style for the border of first series
firstSeries.getBorder().setStyle(LineType.SOLID);
// Set Color for the first series
firstSeries.getBorder().setColor(Color.fromArgb(165, 165, 165));

//get the second Series
Series secondSeries = chart.getNSeries().get(1);
// Set the chart type
secondSeries.setType(ChartType.LINE);
// Set style for the border of first series
secondSeries.getBorder().setStyle(LineType.SOLID);
// Set Color for the first series
secondSeries.getBorder().setColor(Color.fromArgb(255, 192, 0));

//get the third Series
Series thirdSeries = chart.getNSeries().get(2);
thirdSeries.getArea().setForegroundColor(Color.fromArgb(91, 155, 213));

//get the fourth Series
Series fourthSeries = chart.getNSeries().get(3);
fourthSeries.getArea().setForegroundColor(Color.fromArgb(237, 125, 49));

chart.getPlotArea().getArea().setFormatting(FormattingType.NONE);
chart.getPlotArea().getBorder().setVisible(false);
chart.getSeriesAxis().getAxisLine().setVisible(false);
chart.getValueAxis().getAxisLine().setVisible(false);
chart.getCategoryAxis().getAxisLine().setVisible(false);
chart.getChartArea().getBorder().setColor(Color.fromArgb(217, 217, 217));
chart.getValueAxis().getMajorGridLines().setColor(Color.fromArgb(217, 217, 217));

// Save the Excel file
workbook.save(filePath + "out_java.xlsx");

Hope helps a bit.

Hi @John.He I ran your code, it is giving the output
image.png (7.6 KB)

@sm350
Did you directly run the sample code I provided? If so, please provide your project. You can use compression tools to compress it into zip format, and the project only contains running Java files. We will check it soon.
Also, which version are you using? We are using the latest version 23.10 for testing.

I directly ran the code sent by you
I am using java version “1.8.0_381”
Java™ SE Runtime Environment (build 1.8.0_381-b09)
Java HotSpot™ 64-Bit Server VM (build 25.381-b09, mixed mode)


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedHashMap;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartCollection;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.FormattingType;

//import com.aspose.cells.Workbook;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.LegendPositionType;
import com.aspose.cells.License;
import com.aspose.cells.LineType;
import com.aspose.cells.Picture;
import com.aspose.cells.PictureCollection;
import com.aspose.cells.Series;
import com.aspose.cells.SeriesCollection;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.cells.WorksheetCollection;
import com.aspose.slides.FillType;
import com.aspose.slides.IChart;
//import com.aspose.chart.ChartImage;
import com.aspose.slides.IPPImage;
import com.aspose.slides.IPictureFrame;
import com.aspose.slides.IShape;
import com.aspose.slides.IShapeCollection;
import com.aspose.slides.ISlide;
import com.aspose.slides.ITable;
import com.aspose.slides.ITextFrame;
//import com.aspose.slides.ITextFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.PresentationFactory;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ShapeType;
//import com.aspose.slides.SlideUtil;
import com.aspose.slides.SlideUtil;

public class Test1 {
	public static void main(String[] args) throws Exception {
		// Load the first Excel file with the first series
        Workbook workbook1 = new Workbook("C:\\Users\\ehastim\\Downloads\\New Microsoft Excel Worksheet.xlsx"); // Replace with the actual file path
        Worksheet worksheet1 = workbook1.getWorksheets().get(0); // Index of the worksheet, change as needed

        int columnIndex = worksheet1.getCharts().add(ChartType.COLUMN, 18, 5, 34, 12);
	     // Retrieve the Chart object
	     Chart chart = worksheet1.getCharts().get(columnIndex);
	     // Set the legend can be showed
	     chart.setShowLegend(true);
	     // Set the chart title name 
	     chart.getTitle().setText("Combo Chart");
	     // Set the Legend at the bottom of the chart area
	     chart.getLegend().setPosition(LegendPositionType.BOTTOM);
	     // Set data range
	     chart.setChartDataRange("A2:E11", true);
	     // Set category data 
	     chart.getNSeries().setCategoryData("A2:A11");
	
	     //get the first Series
	     Series firstSeries = chart.getNSeries().get(0);
	     // Set the chart type
	     firstSeries.setType(ChartType.LINE);
	     // Set style for the border of first series
	     firstSeries.getBorder().setStyle(LineType.MEDIUM_GRAY);
	     // Set Color for the first series
	     firstSeries.getBorder().setColor(Color.fromArgb(165, 165, 165));
	
	     //get the second Series
	     Series secondSeries = chart.getNSeries().get(1);
	     // Set the chart type
	     secondSeries.setType(ChartType.LINE);
	     // Set style for the border of first series
	     secondSeries.getBorder().setStyle(LineType.SOLID);
	     // Set Color for the first series
	     secondSeries.getBorder().setColor(Color.fromArgb(255, 192, 0));
	
	     //get the third Series
	     Series thirdSeries = chart.getNSeries().get(2);
	     thirdSeries.getArea().setForegroundColor(Color.fromArgb(91, 155, 213));
	
	     //get the fourth Series
	     Series fourthSeries = chart.getNSeries().get(3);
	     fourthSeries.getArea().setForegroundColor(Color.fromArgb(237, 125, 49));
	
	     chart.getPlotArea().getArea().setFormatting(FormattingType.NONE);
	     chart.getPlotArea().getBorder().setVisible(false);
	     chart.getSeriesAxis().getAxisLine().setVisible(false);
	     chart.getValueAxis().getAxisLine().setVisible(false);
	     chart.getCategoryAxis().getAxisLine().setVisible(false);
	     chart.getChartArea().getBorder().setColor(Color.fromArgb(217, 217, 217));
	     chart.getValueAxis().getMajorGridLines().setColor(Color.fromArgb(217, 217, 217));

		
		 // Saving the Excel file
		 workbook1.save("C:\\Users\\ehastim\\Downloads\\HTCCustomChart_out.xlsx");
		
		 // Print message
		 System.out.println("Customized chart is successfully created.");
		}

}

@sm350
This is the result I generated using your code and Aspose.Cells for Java 23.10. Please check it.
HTCCustomChart_out.zip (14.2 KB)

Please print your version using the following code.

System.out.println(CellsHelper.getVersion());

My version is 18.2.0

@sm350 ,

Since you are using an older version of the API, so we cannot evaluate it. Please try using our latest version/fix, it will create your desired chart.

@amjad.sahi how do I download the latest version?

@sm350,

You may browse individual API (Java) release pages on the main releases page. Click on Versions link and then click/select your desired version (on top) of the API. Now click “Package Explorer” link (which has appeared now). Here, you may download the API jars directly there.

Let us know if you still could not download it.

@amjad.sahi @John.He
I am facing some issue in license, is it possible to make some changes so that the code works in version 18.2?

@sm350
I’m afraid we cannot use the old version to evaluate the issue, and the old version cannot include fixes or enhancements. All enhancements and fixes are based solely on the latest API set.

In short, please try upgrading to the latest version and use it. If you still find any issues, please let us know. We will check it soon.

Hi @John.He, will this code, converted to python, work with similar results?