I have a chart and i am converting it to image.When I convert it to image there is a border around the image.How can i remove this?
Workbook workbook1 = new Workbook();
Worksheet worksheet1 = workbook1.Worksheets[0];
worksheet1.Cells[0, 0].PutValue("Apple");
worksheet1.Cells[0, 1].PutValue(324234234239);
worksheet1.Cells[0, 2].PutValue(3209496298.35049);
worksheet1.Cells[1, 0].PutValue("Orange");
worksheet1.Cells[1, 1].PutValue(12123123);
worksheet1.Cells[1, 2].PutValue(202342342);
worksheet1.Cells[2, 0].PutValue("Mango");
worksheet1.Cells[2, 1].PutValue(12123123);
worksheet1.Cells[2, 2].PutValue(202342342);
int chartIndex1 = worksheet1.Charts.Add(ChartType.Column3DClustered, 5, 0, 15, 5);
Chart chart1 = worksheet1.Charts[chartIndex1];
chart1.Title.Text = "Quantity for period 6 / 2016";
chart1.Title.Font.Size = 6;
chart1.NSeries.CategoryData = "A1:A3";
chart1.CategoryAxis.TickLabels.Font.Size = 6;
chart1.ChartArea.BackgroundMode = BackgroundMode.Transparent;
chart1.NSeries.Add("B1: B3", true);
chart1.NSeries.Add("C1: C3", true);
chart1.NSeries[0].XValues = "A1: A3";
chart1.NSeries[1].XValues = "A1: A3";
chart1.NSeries[0].Name = "Quantity Balance";
chart1.NSeries[1].Name = "Net Available";
chart1.NSeries[0].DataLabels.ShowValue = true;
chart1.NSeries[0].DataLabels.Font.Size = 6;
chart1.NSeries[0].Area.ForegroundColor = Color.Red;
chart1.NSeries[1].DataLabels.ShowValue = true;
chart1.NSeries[1].DataLabels.Font.Size = 6;
chart1.NSeries[1].Area.ForegroundColor = Color.Green;
chart1.Legend.Position = LegendPositionType.Bottom;
chart1.Legend.Font.Size = 6;
chart1.PlotArea.Area.Formatting = FormattingType.None;
chart1.PlotArea.Border.FormattingType = ChartLineFormattingType.None;
ImageOrPrintOptions options1 = new ImageOrPrintOptions()
{
VerticalResolution = 173,
HorizontalResolution = 200,
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
};
Image chartImage1 = chart1.ToImage(options1);
byte[] newImage1 = ConvertImageToByte(chartImage1);
string image1 = Convert.ToBase64String(newImage1);
@anishsm,
Thanks for sample code and details.
Please add the following lines (in bold) to your code segment, it will work for your needs:
e.g
Sample code:
…
chart1.NSeries.CategoryData = “A1:A3”;
chart1.CategoryAxis.TickLabels.Font.Size = 6;
chart1.ChartArea.BackgroundMode = BackgroundMode.Transparent;
chart1.NSeries.Add(“B1: B3”, true);
chart1.NSeries.Add(“C1: C3”, true);
chart1.NSeries[0].XValues = “A1: A3”;
chart1.NSeries[1].XValues = “A1: A3”;
chart1.NSeries[0].Name = “Quantity Balance”;
chart1.NSeries[1].Name = “Net Available”;
chart1.NSeries[0].DataLabels.ShowValue = true;
chart1.NSeries[0].DataLabels.Font.Size = 6;
chart1.NSeries[0].Area.ForegroundColor = Color.Red;
chart1.NSeries[1].DataLabels.ShowValue = true;
chart1.NSeries[1].DataLabels.Font.Size = 6;
chart1.NSeries[1].Area.ForegroundColor = Color.Green;
chart1.Legend.Position = LegendPositionType.Bottom;
chart1.Legend.Font.Size = 6;
chart1.PlotArea.Area.Formatting = FormattingType.None;
chart1.PlotArea.Border.FormattingType = ChartLineFormattingType.None;
chart1.ChartArea.Area.Formatting = FormattingType.None;
chart1.ChartArea.Border.FormattingType = ChartLineFormattingType.None;
ImageOrPrintOptions options1 = new ImageOrPrintOptions()
{
VerticalResolution = 173,
HorizontalResolution = 200,
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
};
…
Hope this helps a bit.
Thank you so much ! worked fine. I have one more doubt.How can we remove the unwanted white space on the sides of chart image ?
@anishsm,
Well, the white space on the sides of the chart is a part of chart area, so you cannot remove it. You may set/minimize the width/height of the chart and its objects though.
@Amjad_Sahi just one question can we get the chart area which is already present on excel sheet and after getting that i just want to remove the border of that chart is their any way to do it.
@asposeLegit,
Could you try to the sample lines of code (Java) to remove or hide borders of the chart area/plot area,
chart.getPlotArea().getBorder().setVisible(false);
cart.getChartArea().getBorder().setVisible(false);
chart.getChartArea().getBorder().setWeight(0);
chart.getPlotArea().getBorder().setTransparency(1);
Hope, this helps a bit.
thank you @Amjad_Sahi, but in my excel sheet i already have chart and i want the range of that chart directly instead of defining it and then i have to remove border of that chart, is their any way to do that.
@asposeLegit,
I could not get you. Please elaborate your issue or requirements and provide more details. Also, share the following resource files.
- Your current Excel file containing the chart in the sheet.
- Your expected Excel file containing the range of chart directly.
- Your sample code you are currently using.
After we have understood your issue/requirements and your provided artifacts (as requested above), we will evaluate and help you through.
@Amjad_Sahi I have chart in different sheet of excel so i have to get the data range of all that chart of all that sheet and remove the border of that chart using the data range which i received so that thing i need sorry but till now i am not able to put this in a code so that’s why i am not sharing anything with you.
@asposeLegit,
You mean you need to remove the data (series) range of the charts, so those charts should become sort of blank charts? Or you want to remove the borders around the charts (maybe your charts (in the sheets) are placed in borders (box))? Moreover, We still request you to kindly provide the following (resource) files.
- Your current Excel file containing the charts in different sheets.
- Your expected Excel file with removed borders for the charts, you may create/update the Excel file as per your requirements into MS Excel manually.
i have created sample file to explain it, in this file i have graph when i am copying this whole in another excel work book it is giving graph and suppose this graph have border or not in the end output file i don’t want that border just i wanna do this and sorry to paste my questions in this thread.1.sample_graph_sheet.xlsx.zip (15.4 KB)
@asposeLegit,
Thanks for the Excel file containing the graph.
We do understand that you need to have borderless graph in the destination sheet. But, do you need to copy the whole sheet (containing the chart) from your source workbook to other workbook or you only want to copy the graph from source workbook to destination workbook? Moreover, you did not provide your expected workbook with your updated/desired chart without borders intact. After your confirmation and getting your expected Excel file, we will devise code segment for you to accomplish your task.
i want whole thing the way it is in another sheet in a same way it is, as in original but now i am just facing issue that in the output file i am getting border to the graph which i don’t want.
@asposeLegit,
See the following sample code using your template Excel file. I copied the worksheet (which contains the chart) to another (empty) workbook, it works fine and as expected. The copied sheet (in the destination workbook) has the chart similar to original in the source workbook. Please find attached the output Excel file for your reference.
files1.zip (13.0 KB)
e.g.
Sample code:
Workbook sourceWorkbook = new Workbook("f:\\files\\1.sample_graph_sheet.xlsx");
Workbook destinationWorkbook = new Workbook();
Worksheet worksheet = destinationWorkbook.getWorksheets().add("MySheet1");
worksheet.copy(sourceWorkbook.getWorksheets().get("Class9"));
destinationWorkbook.save("f:\\files\\out1.xlsx");
Please compare the chart in the attached file with source workbook’s chart and you will notice both are identical.
PS. please note I am using latest version, i.e., Aspose.Cells for Java v22.12, so please try it if you are not already using it.
thank you @Amjad_Sahi so much, but i am able to make the copy of the file which you have given solution to me i just want a function to clear the border of the chart whether it has or not in the input chart
@asposeLegit,
See the updated sample code to accomplish your task. I added a few lines for the chart in the copied sheet to make sure the chart does not have borders for its chart area/plot area:
e.g.
Sample code:
Workbook sourceWorkbook = new Workbook("f:\\files\\1.sample_graph_sheet.xlsx");
Workbook destinationWorkbook = new Workbook();
Worksheet worksheet = destinationWorkbook.getWorksheets().add("MySheet1");
worksheet.copy(sourceWorkbook.getWorksheets().get("Class9"));
//Get the chart of the copied sheet
Chart chart = worksheet.getCharts().get(0);
//Remove/hide the borders or the chart/plot area
chart.getPlotArea().getBorder().setVisible(false);
chart.getChartArea().getBorder().setVisible(false);
destinationWorkbook.save("f:\\files\\out1.xlsx");
If you still have any issue or confusion, give us a sample Excel file containing your expected chart with your desired formatting, we will check it soon.