Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Fri Feb 28, 2020 6:53 pm

I am using Spire.XLS Chart in Excel in C #

I need to create a chart like the one attached, someone who can help me know how I can generate it, I think it would be the Donut

I hope you can help me
Attachments
grafica3.png
grafica3.png (20.05 KiB) Viewed 1669 times

AnaMacias
 
Posts: 8
Joined: Fri Feb 28, 2020 6:33 pm

Mon Mar 02, 2020 8:01 am

Hi,

Thanks for your inquiry.
Please refer to following code:
Code: Select all
            //Create a workbook
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //Insert data
            sheet.Range["A1"].Value = "67";
            sheet.Range["A2"].Value = "164";


            //Add a new chart, set chart type as doughnut
            Chart chart = sheet.Charts.Add();
            chart.ChartType = ExcelChartType.Doughnut;
            chart.DataRange = sheet.Range["A1:A2"];
            chart.SeriesDataFromRange = false;

            //Set position of chart
            chart.LeftColumn = 4;
            chart.TopRow = 2;
            chart.RightColumn = 12;
            chart.BottomRow = 22;

            //Chart title
            chart.ChartTitle = "Chart title";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 12;

            chart.Series[0].DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
            chart.Series[0].Format.Options.DoughnutHoleSize = 45;

            //Create a font
            ExcelFont font = workbook.CreateFont();
            font.Size = 13.0;
            //Set font
            chart.Series[0].DataPoints[0].DataLabels.TextArea.SetFont(font);
           
            ChartDataLabels format = chart.Series[0].DataPoints[0].DataLabels;
            format.HasPercentage = true;
            format.Delimiter = "\n";
            //Change the position of data label
            format.IsXMode = true;
            format.IsYMode = true;
            format.X = 1700;
            format.Y = 2000;

            chart.Series[0].DataPoints[1].DataLabels.HasValue = false;
            chart.Series[0].DataPoints[1].DataFormat.Fill.FillType = ShapeFillType.SolidColor;
            chart.Series[0].DataPoints[1].DataFormat.Fill.ForeColor = Color.LightGray;

            //Save the document
            string output = "CreateDoughnutChart20756.xlsx";
            workbook.SaveToFile(output, ExcelVersion.Version2013);

If this doesn't meet your requirement, please share your input file(if any) and the desired Excel file instead of screenshot for further investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Mar 02, 2020 11:16 pm

Thank you very much for your support, the code of the example you proposed is perfect, I only have two doubts:

1.- How the segment that is in blue color is assigned is 29%, since I do not see it as a variable.

2.- How can I assign three images in one?

AnaMacias
 
Posts: 8
Joined: Fri Feb 28, 2020 6:33 pm

Tue Mar 03, 2020 6:17 am

Hi,

Thanks for your feedback.
1) The 29% is generated by the chart automatically, it is the percentage of the value 67 for the chart. The below code could control if the percentage would be showed.
Code: Select all
ChartDataLabels.HasPercentage = true;


2) The code I provided only creates the first chart. If you want other two charts, you only could change the corresponding values for the range "A1" and "A2" . Sorry that I am not quit clear about your meaning. If you still have the issues, please provide detailed information along with the desired Excel file.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Mar 03, 2020 6:43 pm

Hi,
E-iceblue support team

Thank you very much for your code example and your comments, they were very helpful, see you later.

Sincerely,
Ana Rosa

AnaMacias
 
Posts: 8
Joined: Fri Feb 28, 2020 6:33 pm

Wed Mar 04, 2020 1:48 am

Hi,

Thanks for your feedback.
Any question, please feel free to contact us.
Have a nice day :D

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.XLS