Chart image error

Dear,

I used Aspose.Cells.ChartShape.Chart.ToImage() to get chart’s image, but it had error:

1. Data input:


a s d f
e 0.3654 0.3344 0.6455 0.2233
r 0.4235 0.5453 0.7345 0.3276
t 0.4347 0.8452 0.7673 0.4156

I get an image as in file attach, image above: Error

2. Data input:

a s d f
e 0.36 0.33 0.64 0.22
r 0.42 0.54 0.73 0.32
t 0.43 0.84 0.76 0.41

I get an image as in fileattach, image under: Correct

If data has more than 2digits after decimal seperator, it would error.

Can you help me to solve this proplem.
Thanks.
KhaiTD

Hi,

Well, I implement your desired chart and convert the chart to image and I don't find the problem you have mentioned. Which version you are using. Attached Files.zip contains input excel file, output chart image and Aspose.Cells version 4.4.1.16 (kindly try it).

Here is my sample code and the output chart image (attached) is fine:

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
workbook.Open(@"d:\test\new_Book1.xls");
Aspose.Cells.Chart chart = workbook.Worksheets[0].Charts[0];
Bitmap bitmap = chart.ToImage();
bitmap.Save(@"d:\Test_chrt.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

If you still find the problem, post your template file, output chart image and sample code here, we will check it soon.

Thank you.

Hi,

That error happen in version 4.3.
I use Aspose version 4.4.1.16, it run OK. Thank you very much.

I have some question:
1. How to change the background color of the chart image?
2. How can I set the Category Axis label?

Best regard.
KhaiTD.

Hi KhaiTD,

1. How to change the background color of the chart image?
2. How can I set the Category Axis label?

Well, you have to change the background / fill color in the designer chart in the template file using Aspose.Cells APIs. Similarly you may set the category axis labels and other objects related chart in the workbook before taking image of the final chart (shaped chart).

For reference, please check the source code of our online featured chart demos (The demos solutions are automatically installed on the system using Aspose.Cells msi installer, so you may open the solutions into VS.NET and check the source codes):

Here I write a sample code for your reference, I create a chart from the scratch and set plot area background color, set category axis and other valus, specify different other chart objects too and finally take the image of the chart (attached):

Workbook workbook = new Workbook();
workbook.ChangePalette(Color.Orange,55);

//Set default font
Style style = workbook.DefaultStyle;
style.Font.Name = "Tahoma";
workbook.DefaultStyle = style;
Cells cells = workbook.Worksheets[0].Cells;

//Put a string into a cell
cells["A1"].PutValue("Region");
cells["A2"].PutValue("France");
cells["A3"].PutValue("Germany");
cells["A4"].PutValue("England");

cells["B1"].PutValue("Marketing Costs");
cells["B2"].PutValue(70000);
cells["B3"].PutValue(55000);
cells["B4"].PutValue(30000);

Worksheet sheet = workbook.Worksheets[0];
//Set the name of the worksheet
sheet.Name = "Clustered Column";
sheet.IsGridlinesVisible = false;

//Create chart
int chartIndex = sheet.Charts.Add(ChartType.Column, 5, 1, 29, 10);
Chart chart = sheet.Charts[chartIndex];

//Add the nseries collection to a chart
chart.NSeries.Add("B2:B4", true);
//Get or set the range of category axis values
chart.NSeries.CategoryData = "A2:A4";
chart.NSeries.IsColorVaried = true;


for ( int i = 0; i < chart.NSeries.Count ; i ++ )
{
chart.NSeries[i].DataLabels.IsValueShown = true;
chart.NSeries[i].Area.ForegroundColor = Color.Orange;
}
//chart.IsDataTableShown = true;

//Set properties of chart
chart.PlotArea.Area.ForegroundColor = Color.Coral;
chart.PlotArea.Area.FillFormat.SetTwoColorGradient(Color.Yellow,Color.White,GradientStyleType.Vertical,2);
chart.PlotArea.Border.IsVisible = false;

//Set the legend position type
chart.Legend.Position = LegendPositionType.Top;
chart.GapWidth = int.Parse("100");
chart.MajorGridLines.IsVisible = false;

//Set properties of chart title
chart.Title.Text = "Marketing Costs by Region";
chart.Title.TextFont.IsBold = true;
chart.Title.TextFont.Color = Color.Black;
chart.Title.TextFont.Size = 12;

//Set properties of categoryaxis title
chart.CategoryAxis.Title.Text = "Region";
chart.CategoryAxis.Title.TextFont.Color = Color.Black;
chart.CategoryAxis.TickLabels.AutoScaleFont = true;
chart.CategoryAxis.TickLabels.Rotation = 40;
chart.CategoryAxis.TickLabels.Font.Size =7;

chart.CategoryAxis.Title.TextFont.IsBold = true;
chart.CategoryAxis.Title.TextFont.Size = 10;

//Set properties of valueaxis title
chart.ValueAxis.Title.Text = "In Thousands";
chart.ValueAxis.Title.TextFont.Name = "Arial";
chart.ValueAxis.Title.TextFont.Color = Color.Black;
chart.ValueAxis.Title.TextFont.IsBold = true;
chart.ValueAxis.Title.TextFont.Size = 10;
chart.ValueAxis.Title.Rotation = 90;
chart.ValueAxis.MajorUnit = double.Parse("20000");
chart.ValueAxis.MaxValue = double.Parse("80000");
chart.ValueAxis.MinorUnit = double.Parse("5000");
chart.ValueAxis.MinValue = double.Parse("0");

chart = workbook.Worksheets[0].Charts[0];
Bitmap bitmap = chart.ToImage();
bitmap.Save(@"d:\Testchrt.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

//workbook.Save("d:\\test\\out1_colchart_1.xls");

Thank you.

Hi,

That’s OK.

Thank you very much.
KhaiTD