We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Chart.toImage - wrong color patern in image when we apply GradientFill with certain angles in chart

Hi,
Please run the sample code below and see the difference in color pattern in actual chart and image generated.

Color pattern applied in 1st and 2nd bar look almost similar in chart and image, BUT we can see major differences in 3rd and 4th bars.
IN the fourth bar, colors are applied in the reverse order

//3rd Bar Color Fill
gf[2] = new GradientFill();
gf[2].setGradientColorType(GradientColorType.TWO_COLORS);
gf[2].getGradientStops().addStop(0, clr1, 0);
gf[2].getGradientStops().addStop(1, clr2, 0);
gf[2].setGradientType(GradientFillType.LINEAR);
gf[2].setGradientFillAngle(45);

//4th Bar Color Fill
gf[3] = new GradientFill();
gf[3].setGradientColorType(GradientColorType.TWO_COLORS);
gf[3].getGradientStops().addStop(0, clr2, 0);
gf[3].getGradientStops().addStop(1, clr1, 0);

gf[3].setGradientType(GradientFillType.LINEAR);
gf[3].setGradientFillAngle(315);

Thanks
Muhammed

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;

import com.aspose.cells.ASeries;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.DisplayUnitType;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.GradientColorType;
import com.aspose.cells.GradientDirectionType;
import com.aspose.cells.GradientFill;
import com.aspose.cells.GradientFillType;
import com.aspose.cells.GradientStyleType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.LegendEntries;
import com.aspose.cells.PatternFill;
import com.aspose.cells.TickLabelPositionType;
import com.aspose.cells.TrendlineType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;

public class GradientFillToImage {

public static void main(String args[]) {

try {
String[] colTitle = new String[] { "Rinse Conditioners"};

//Array of cell data
double[][] data = new double[][] { {17060100},
{20892900},
{20892900},
{14008380}
};

Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().addSheet();

String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

int rowIndex = 0;
for (int i = 0; i < colTitle.length; i++) {
dataSheet.getCells().getCell(rowIndex, i).setValue(colTitle[i]);
}

for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
dataSheet.getCells().getCell(i + 1, j).setValue(data[i][j]);
}
}

Color clr1 = Color.RED;
Color clr2 = Color.GREEN;
GradientFill gf[] = new GradientFill[4];

gf[0] = new GradientFill();
gf[0].setGradientColorType(GradientColorType.TWO_COLORS);
gf[0].getGradientStops().addStop(0, clr1, 0);
gf[0].getGradientStops().addStop(1, clr2, 0);
gf[0].setGradientType(GradientFillType.LINEAR);
gf[0].setGradientFillAngle(0);

gf[1] = new GradientFill();
gf[1].setGradientColorType(GradientColorType.TWO_COLORS);
gf[1].getGradientStops().addStop(0, clr1, 0);
gf[1].getGradientStops().addStop(1, clr2, 0);
gf[1].setGradientType(GradientFillType.LINEAR);
gf[1].setGradientFillAngle(90);

gf[2] = new GradientFill();
gf[2].setGradientColorType(GradientColorType.TWO_COLORS);
gf[2].getGradientStops().addStop(0, clr1, 0);
gf[2].getGradientStops().addStop(1, clr2, 0);
gf[2].setGradientType(GradientFillType.LINEAR);
gf[2].setGradientFillAngle(45);

gf[3] = new GradientFill();
gf[3].setGradientColorType(GradientColorType.TWO_COLORS);
gf[3].getGradientStops().addStop(0, clr2, 0);
gf[3].getGradientStops().addStop(1, clr1, 0);
gf[3].setGradientType(GradientFillType.LINEAR);
gf[3].setGradientFillAngle(315);

//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();

//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 600;
int chartHeight = 400;

int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;

Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 0, 0, chartWidth, chartHeight);

String dataRef = dataSheet.getName() + "!" + CellsHelper.convertCellIndexToName(0, 0) + ":" + CellsHelper.convertCellIndexToName(data.length, colTitle.length - 1);
chart.getNSeries().add(dataRef, false);

int seriesCount = chart.getNSeries().size();
for (int i = 0; i < seriesCount; i++) {
ASeries series = chart.getNSeries().get(i);

if (i < gf.length && gf[i] != null) {
series.getArea().setFill(gf[i]);
series.getBorder().setFill(gf[i]);
}
}

String categoryAreaRef = dataSheet.getName() + "!" + CellsHelper.convertCellIndexToName(0, 0) + ":" + CellsHelper.convertCellIndexToName(0, colTitle.length - 1);
chart.getNSeries().setCategoryData(categoryAreaRef);

chart.getCategoryAxis().setDisplayUnitLabelShown(false);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.NONE);

//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);

chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);

wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);

//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));

System.out.println("successfully completed!!!" + CellsHelper.getReleaseVersion());

}
catch (Exception e) {
e.printStackTrace();
}
}

}

Hi,

We found the color issue of Gradient Bars. We will figure it out soon.

We have logged it in our issue tracking system with an issue id: CELLSJAVA-16150.

Thank you.



The issues you have found earlier (filed as CELLSJAVA-16150) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi,

Colors looks better in the image now. Looks like minor issues related to brightness,

Thanks
Muhammed