How to create chart in excel c#


#1

Hi,
How to i can create chart as image follow :
chart.PNG (61.7 KB)


#2

@LongNgo,

Thanks for the screenshot.

Please create your desired chart in MS Excel manually to save the file and provide us the template file, we will check on how to create the chart via Aspose.Cells APIs.


#3

Hi Amjad_Sahi,
Thanks you feedback, i send template file as image follow :
template.PNG (5.8 KB)


#4

@LongNgo,

Thanks for the new screenshot.

We asked you to provide Excel file. We recommend you to kindly see the document on how to create different types of charts for your reference and create your desired chart(s) based on your data source accordingly:
https://docs.aspose.com/display/cellsnet/Creating+and+Customizing+Charts

Hope, this helps a bit.


#5

Hi Amjad_Sahi,
Thanks you feedback, i send excel file
I want to create chart column
template.PNG (5.8 KB)
from file excel attach in code behind.
excel_file.zip (6.1 KB)
Please, help me!!!


#6

@LongNgo

We have checked your excel_file.xlsx and did not find any chart inside it. Please create a chart in your Excel file manually using Microsoft Excel and submit to us. We will then create replica of your chart using Aspose APIs and provide you sample code.


#7

Hi shakeel.faiz,
I created a chart in my excel file manually using Microsoft Excel :
excel_file.zip (11.3 KB)


#8

@LongNgo,

Thanks for the template Excel file (containing the chart).

Please see the following sample code to generate your desired chart for your reference. I have used your template file and used the data in the worksheet to mimic your desired chart:
e.g
Sample code:

    Workbook workbook = new Workbook("e:\\test2\\excel_file.xlsx");
    
    int chartIndex = workbook.Worksheets[0].Charts.Add(ChartType.Column, 23, 15, 39, 24);

    Chart chart = workbook.Worksheets[0].Charts[chartIndex];
    chart.NSeries.Add("='Sheet1'!$A$3:$L$23", true);

    chart.NSeries.CategoryData = "='Sheet1'!$A$3:$B$23";
    chart.CategoryAxis.Title.RotationAngle = 90;
    chart.NSeries.IsColorVaried = true;

    for (int i = 0; i < chart.NSeries.Count; i++)
    {
        Series aseries = chart.NSeries[i];
        aseries.Overlap = 100;
        aseries.GapWidth = 0;

    }

    chart.Legend.Position = LegendPositionType.Bottom;

    workbook.Save("e:\\test2\\out1.xlsx");

Hope, this helps a bit.


#9

Thank you, this solution works fine for me!!!
I have a problem : how to set width for chart and how to hide name series as image follow :
template.PNG (6.7 KB)


#10

@LongNgo,

Thanks for the screenshot.

  1. Well, you may specify the parameters value (in ChartCollection.Add() method) accordingly when adding the chart to the worksheet accordingly:
    upper left row, upper left column, lower right row and lower right column.
    Moreover, you may also try to extend the width and height of the added chart as following:
    e.g
    Sample code:


    Aspose.Cells.Charts.Chart chart = workbook.Worksheets[0].Charts[i];
    chart.ChartObject.Height = 700;
    chart.ChartObject.Width = 600;

  2. See the line of code:
    e.g
    Sample code:


    chart.CategoryAxis.IsVisible = false;

    Well, you may either show all the ticket labels (series names in the category axis) or hide all of them but you cannot hide a part of it. If you could accomplish the task in MS Excel, we can do that. In that case, provide the Excel file containing the chart with part of the series tick labels hidden only, we will check it soon.


#11

Hi Amjad_Sahi,
Thanks you so much,
I have a problem : how to set text direction in code behind as image follow :
image.png (6.3 KB)


#12

@LongNgo,

Thanks for the screenshot.

By default, the tick labels are rendered horizontally, so you do not need to do anything. You may try to use RotationAngle attribute to specify the rotation angle if you want. See the sample line of code:
e.g
Sample code:


//Set the ticket labels direction horizontally.
chart.CategoryAxis.Title.RotationAngle = 0;


#13

Hi Amjad_Sahi,
How to i can set tick labels are rendered to “rotate all text 270”,thanks!!!


#14

Hi Amjad_Sahi,
I used sample code:
chart.CategoryAxis.TickLabels.RotationAngle = 90;
and everything is correct.
Thank You Very Much.
Have a nice day :slight_smile:


#15

@LongNgo

Thanks for your feedback and using Aspose.Cells.

It is good to know that your issue is resolved now. Let us know if you encounter any other issue, we will be glad to look into it and help you further.


#16

Hi shakeel.faiz
How i can hide all value zero in chart type pie3d code behind as image follow
charttypepie3d.PNG (13.0 KB)


#17

@LongNgo,

Thanks for the screenshot.

Well, you may hide or delete the specific data labels for the data point of the series. You need to first find out the point (in the series) whose data label should not be shown on the chart. See some lines of code, you may pick one for your needs:
e.g
Sample code:

.............
chart.NSeries[0].Points[i].DataLabels.IsDeleted = true; 
chart.NSeries[0].Points[i].DataLabels.ShowPercentage = false;
............

Hope, this helps a bit.


#18

Hi Amjad_Sahi,
How to i can get value point in series is 0 in code behind


#19

@LongNgo,

You may try to use DataLabels.Text attribute in if condition or if it works for you.


#20

Hi Amjad_Sahi,
i used DataLabels.Text but value of it is null