Custom Data Labels

Technical support for Presentation

Moderator: iceblue support

Custom Data Labels

Postby CPTJ » Thu Apr 06, 2017 2:47 pm

Hi, I am currently evaluating your software and was wondering if it is possible to add custom text to data labels in a chart? For example, instead of showing value or percentage can I insert my own text e.g. "Apple" or "Banana" for instance?

Regards,
Jack
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby Betsy.jiang » Fri Apr 07, 2017 3:49 am

Dear Jack,

Thanks for your inquiry.
Here is sample code for your reference.
Code: Select all
            Presentation PPT = new Presentation(@"F:\sample.pptx", FileFormat.Pptx2010);
            IChart chart = PPT.Slides[0].Shapes[0] as IChart;           
            ChartDataLabel cd1 = chart.Series[0].DataLabels.Add();
            //set the custom text to the datalabel.
            cd1.TextFrame.Paragraphs[0].Text = "Apple";
            PPT.SaveToFile("Result.pptx", FileFormat.Pptx2010);

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1629
Joined: Tue Sep 06, 2016 8:30 am

Re: Custom Data Labels

Postby CPTJ » Tue Apr 11, 2017 9:52 am

Thanks for your example. I have tried it and can change the first label of a series but I am having trouble working out how to change the rest of the labels. I thought incrementing the "Paragraphs[0]" part would do it but that didn't work as the Paragraph Count was only 1. For reference I am using the bar chart from your example code, image attached.

Kind regard,
Jack
You do not have the required permissions to view the files attached to this post.
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby Betsy.jiang » Wed Apr 12, 2017 2:45 am

Dear CPTJ,

Thanks for your feedback.
Please create same numbers of ChartDataLabel as datapoints in one series, and then set the custom label for every label.
Code snippet:
Code: Select all
            ChartDataLabel cd1 = chart.Series[0].DataLabels.Add();
            cd1.TextFrame.Paragraphs[0].Text = "custom1";

            ChartDataLabel cd2 = chart.Series[0].DataLabels.Add();
            cd2.TextFrame.Paragraphs[0].Text = "custom2";

            ChartDataLabel cd3 = chart.Series[0].DataLabels.Add();
            cd3.TextFrame.Paragraphs[0].Text = "custom3";
            ...

Hope this helps. If there is any question, please provide us with the code you were using for investigation.

Sincerely,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1629
Joined: Tue Sep 06, 2016 8:30 am

Re: Custom Data Labels

Postby Betsy.jiang » Thu Apr 13, 2017 7:26 am

Dear CPTJ,

Did you test the code I provided ? Did it help you solve the issue ?

Thanks,
Betsy
E-iceblue support team
User avatar
Betsy.jiang
 
Posts: 1629
Joined: Tue Sep 06, 2016 8:30 am

Re: Custom Data Labels

Postby CPTJ » Thu Apr 13, 2017 11:24 am

Hi Betsy,

Yes, I gave your code a try but all it did was to overwrite the first datapoint label with the new text. I will try and provide my code example when I have time.

thanks.
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby CPTJ » Tue Apr 18, 2017 8:11 am

Here is my code, it is basically your code example for a bar chart.
see https://www.e-iceblue.com/Tutorials/Spire.Presentation/Program-Guide/Insert-chart-in-PPT-document.html
I am able to change the first labels in each series but cannot work out how to change the subsequent labels:

Code: Select all
            //  Insert chart, set title and style of the chart.
            RectangleF rect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 200, 100, 400, 400);
            Spire.Presentation.Charts.IChart chart = presentation.Slides[totalSlides].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.Cylinder3DClustered, rect);
            chart.ChartTitle.TextProperties.Text = "Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            // Prepare data for the chart. Load a simple DataTable from XML file via LoadData() method.
            DataTable dataTable = LoadData();

            // Attach the data to chart. We can use chart.ChartData[rowIndex, columnIndex] to get/set the value of the specified
            // cell in the data table of chart. Using the property chart.ChartData[rowIndex, columnIndex].Text to get/set the text value.
            // Using the property chart.ChartData[rowIndex, columnIndex].Value to get/set numeric value. In this article we define a method
            // InitChartData to attach the Data of the chart.
            // load data from datatable to chart
            InitChartData(chart, dataTable);

            // Set the Series label and Category label.
            chart.Series.SeriesLabel = chart.ChartData["B1", "D1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A9"];

            // Assign data to each Series and set each Series' fill color.
            chart.Series[0].Values = chart.ChartData["B2", "B9"];
            chart.Series[0].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].Fill.SolidColor.KnownColor = KnownColors.Brown;
            chart.Series[1].Values = chart.ChartData["C2", "C9"];
            chart.Series[1].Fill.FillType = FillFormatType.Solid;
            chart.Series[1].Fill.SolidColor.KnownColor = KnownColors.Green;
            chart.Series[2].Values = chart.ChartData["D2", "D9"];
            chart.Series[2].Fill.FillType = FillFormatType.Solid;
            chart.Series[2].Fill.SolidColor.KnownColor = KnownColors.Orange;

            // try changing the labels to custom text

            Spire.Presentation.Charts.ChartDataLabel cd1 = chart.Series[0].DataLabels.Add();
            Spire.Presentation.Charts.ChartDataLabel cd2 = chart.Series[1].DataLabels.Add();
            Spire.Presentation.Charts.ChartDataLabel cd3 = chart.Series[2].DataLabels.Add();


            cd1.TextFrame.Paragraphs[0].Text = "Apple";
            cd2.TextFrame.Paragraphs[0].Text = "Pear";
            cd3.TextFrame.Paragraphs[0].Text = "Banana";
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby Betsy.jiang » Tue Apr 18, 2017 8:48 am

Dear CPTJ,

Many thanks for the information.
Kindly note that there are three series in that tutorial you mentioned, and every series has six datapoints. So you need to create six datalables for one series, eighteen datalables in total . Then set the custom lable for every lable.
Code: Select all
                //Series 1
                ChartDataLabel cd11 = chart.Series[0].DataLabels.Add();
                cd11.TextFrame.Paragraphs[0].Text = "series1-custom1";
                ChartDataLabel cd12 = chart.Series[0].DataLabels.Add();
                cd12.TextFrame.Paragraphs[0].Text = "series1-custom2";
                ChartDataLabel cd13 = chart.Series[0].DataLabels.Add();
                cd13.TextFrame.Paragraphs[0].Text = "series1-custom3";
                ...

                //Series 2
                ChartDataLabel cd21 = chart.Series[1].DataLabels.Add();
                cd21.TextFrame.Paragraphs[0].Text = "series2-custom1";
                ChartDataLabel cd22 = chart.Series[1].DataLabels.Add();
                cd22.TextFrame.Paragraphs[0].Text = "series2-custom2";
                ChartDataLabel cd23 = chart.Series[1].DataLabels.Add();
                cd23.TextFrame.Paragraphs[0].Text = "series2-custom3";
                ...

                //Series 3
                ChartDataLabel cd31 = chart.Series[2].DataLabels.Add();
                cd31.TextFrame.Paragraphs[0].Text = "series3-custom1";
                ChartDataLabel cd32 = chart.Series[2].DataLabels.Add();
                cd32.TextFrame.Paragraphs[0].Text = "series3-custom2";
                ChartDataLabel cd33 = chart.Series[2].DataLabels.Add();
                cd33.TextFrame.Paragraphs[0].Text = "series3-custom3";
                ...



If there is still issue, please let me know.

Sincerely,
Betsy
E-iceblue support team
Last edited by Betsy.jiang on Wed Apr 19, 2017 2:24 am, edited 1 time in total.
User avatar
Betsy.jiang
 
Posts: 1629
Joined: Tue Sep 06, 2016 8:30 am

Re: Custom Data Labels

Postby CPTJ » Tue Apr 18, 2017 9:55 am

Hi, I have tried your code but as I have mentioned before the first label is the only one that gets updated so the first label becomes "series1-custom6" and the others remain unchanged. Here is my code:

Code: Select all
//  Insert chart, set title and style of the chart.
            RectangleF rect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 200, 100, 400, 400);
            Spire.Presentation.Charts.IChart chart = presentation.Slides[totalSlides].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.Cylinder3DClustered, rect);
            chart.ChartTitle.TextProperties.Text = "Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            // Prepare data for the chart. Load a simple DataTable from XML file via LoadData() method.
            DataTable dataTable = LoadData();

            // Attach the data to chart. We can use chart.ChartData[rowIndex, columnIndex] to get/set the value of the specified
            // cell in the data table of chart. Using the property chart.ChartData[rowIndex, columnIndex].Text to get/set the text value.
            // Using the property chart.ChartData[rowIndex, columnIndex].Value to get/set numeric value. In this article we define a method
            // InitChartData to attach the Data of the chart.
            // load data from datatable to chart
            InitChartData(chart, dataTable);

            // Set the Series label and Category label.
            chart.Series.SeriesLabel = chart.ChartData["B1", "D1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];

            // Assign data to each Series and set each Series' fill color.
            chart.Series[0].Values = chart.ChartData["B2", "B7"];
            chart.Series[0].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].Fill.SolidColor.KnownColor = KnownColors.Brown;
            chart.Series[1].Values = chart.ChartData["C2", "C7"];
            chart.Series[1].Fill.FillType = FillFormatType.Solid;
            chart.Series[1].Fill.SolidColor.KnownColor = KnownColors.Green;
            chart.Series[2].Values = chart.ChartData["D2", "D7"];
            chart.Series[2].Fill.FillType = FillFormatType.Solid;
            chart.Series[2].Fill.SolidColor.KnownColor = KnownColors.Orange;

            // try changing the labels to text
            //Series 1
            Spire.Presentation.Charts.ChartDataLabel cd11 = chart.Series[0].DataLabels.Add();
            cd11.TextFrame.Paragraphs[0].Text = "series1-custom1";
            Spire.Presentation.Charts.ChartDataLabel cd12 = chart.Series[0].DataLabels.Add();
            cd12.TextFrame.Paragraphs[0].Text = "series1-custom2";
            Spire.Presentation.Charts.ChartDataLabel cd13 = chart.Series[0].DataLabels.Add();
            cd13.TextFrame.Paragraphs[0].Text = "series1-custom3";
            Spire.Presentation.Charts.ChartDataLabel cd14 = chart.Series[0].DataLabels.Add();
            cd14.TextFrame.Paragraphs[0].Text = "series1-custom4";
            Spire.Presentation.Charts.ChartDataLabel cd15 = chart.Series[0].DataLabels.Add();
            cd15.TextFrame.Paragraphs[0].Text = "series1-custom5";
            Spire.Presentation.Charts.ChartDataLabel cd16 = chart.Series[0].DataLabels.Add();
            cd16.TextFrame.Paragraphs[0].Text = "series1-custom6";

            //Series 2
            Spire.Presentation.Charts.ChartDataLabel cd21 = chart.Series[1].DataLabels.Add();
            cd21.TextFrame.Paragraphs[0].Text = "series2-custom1";
            Spire.Presentation.Charts.ChartDataLabel cd22 = chart.Series[1].DataLabels.Add();
            cd22.TextFrame.Paragraphs[0].Text = "series2-custom2";
            Spire.Presentation.Charts.ChartDataLabel cd23 = chart.Series[1].DataLabels.Add();
            cd23.TextFrame.Paragraphs[0].Text = "series2-custom3";
            Spire.Presentation.Charts.ChartDataLabel cd24 = chart.Series[1].DataLabels.Add();
            cd24.TextFrame.Paragraphs[0].Text = "series2-custom4";
            Spire.Presentation.Charts.ChartDataLabel cd25 = chart.Series[1].DataLabels.Add();
            cd25.TextFrame.Paragraphs[0].Text = "series2-custom5";
            Spire.Presentation.Charts.ChartDataLabel cd26 = chart.Series[1].DataLabels.Add();
            cd26.TextFrame.Paragraphs[0].Text = "series2-custom6";

            //Series 3
            Spire.Presentation.Charts.ChartDataLabel cd31 = chart.Series[2].DataLabels.Add();
            cd31.TextFrame.Paragraphs[0].Text = "series3-custom1";
            Spire.Presentation.Charts.ChartDataLabel cd32 = chart.Series[2].DataLabels.Add();
            cd32.TextFrame.Paragraphs[0].Text = "series3-custom2";
            Spire.Presentation.Charts.ChartDataLabel cd33 = chart.Series[2].DataLabels.Add();
            cd33.TextFrame.Paragraphs[0].Text = "series3-custom3";
            Spire.Presentation.Charts.ChartDataLabel cd34 = chart.Series[2].DataLabels.Add();
            cd34.TextFrame.Paragraphs[0].Text = "series3-custom4";
            Spire.Presentation.Charts.ChartDataLabel cd35 = chart.Series[2].DataLabels.Add();
            cd35.TextFrame.Paragraphs[0].Text = "series3-custom5";
            Spire.Presentation.Charts.ChartDataLabel cd36 = chart.Series[2].DataLabels.Add();
            cd36.TextFrame.Paragraphs[0].Text = "series3-custom6";
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby Betsy.jiang » Tue Apr 18, 2017 10:43 am

Dear CPTJ,

Thanks for your prompt response.
I have tested the code with the latest Spire.Presentation Pack Hotfix Version:2.7.51, all datalables can be changed correctly. Please try to use this version. Here is my entire code.
Code: Select all
            Presentation presentation = new Presentation();
            //  Insert chart, set title and style of the chart.
            RectangleF rect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 200, 100, 400, 400);
            Spire.Presentation.Charts.IChart chart = presentation.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.Cylinder3DClustered, rect);
            chart.ChartTitle.TextProperties.Text = "Report";
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartTitle.Height = 30;
            chart.HasTitle = true;

            // Prepare data for the chart. Load a simple DataTable from XML file via LoadData() method.
            DataTable dataTable = LoadData();

            // Attach the data to chart. We can use chart.ChartData[rowIndex, columnIndex] to get/set the value of the specified
            // cell in the data table of chart. Using the property chart.ChartData[rowIndex, columnIndex].Text to get/set the text value.
            // Using the property chart.ChartData[rowIndex, columnIndex].Value to get/set numeric value. In this article we define a method
            // InitChartData to attach the Data of the chart.
            // load data from datatable to chart
            InitChartData(chart, dataTable);

            // Set the Series label and Category label.
            chart.Series.SeriesLabel = chart.ChartData["B1", "D1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A9"];

            // Assign data to each Series and set each Series' fill color.
            chart.Series[0].Values = chart.ChartData["B2", "B9"];
            chart.Series[0].Fill.FillType = FillFormatType.Solid;
            chart.Series[0].Fill.SolidColor.KnownColor = KnownColors.Brown;
            chart.Series[1].Values = chart.ChartData["C2", "C9"];
            chart.Series[1].Fill.FillType = FillFormatType.Solid;
            chart.Series[1].Fill.SolidColor.KnownColor = KnownColors.Green;
            chart.Series[2].Values = chart.ChartData["D2", "D9"];
            chart.Series[2].Fill.FillType = FillFormatType.Solid;
            chart.Series[2].Fill.SolidColor.KnownColor = KnownColors.Orange;

            // try changing the labels to text
            //Series 1
            Spire.Presentation.Charts.ChartDataLabel cd11 = chart.Series[0].DataLabels.Add();
            cd11.TextFrame.Paragraphs[0].Text = "1";
            Spire.Presentation.Charts.ChartDataLabel cd12 = chart.Series[0].DataLabels.Add();
            cd12.TextFrame.Paragraphs[0].Text = "2";
            Spire.Presentation.Charts.ChartDataLabel cd13 = chart.Series[0].DataLabels.Add();
            cd13.TextFrame.Paragraphs[0].Text = "3";
            Spire.Presentation.Charts.ChartDataLabel cd14 = chart.Series[0].DataLabels.Add();
            cd14.TextFrame.Paragraphs[0].Text = "4";
            Spire.Presentation.Charts.ChartDataLabel cd15 = chart.Series[0].DataLabels.Add();
            cd15.TextFrame.Paragraphs[0].Text = "5";
            Spire.Presentation.Charts.ChartDataLabel cd16 = chart.Series[0].DataLabels.Add();
            cd16.TextFrame.Paragraphs[0].Text = "6";

            //Series 2
            Spire.Presentation.Charts.ChartDataLabel cd21 = chart.Series[1].DataLabels.Add();
            cd21.TextFrame.Paragraphs[0].Text = "21";
            Spire.Presentation.Charts.ChartDataLabel cd22 = chart.Series[1].DataLabels.Add();
            cd22.TextFrame.Paragraphs[0].Text = "22";
            Spire.Presentation.Charts.ChartDataLabel cd23 = chart.Series[1].DataLabels.Add();
            cd23.TextFrame.Paragraphs[0].Text = "23";
            Spire.Presentation.Charts.ChartDataLabel cd24 = chart.Series[1].DataLabels.Add();
            cd24.TextFrame.Paragraphs[0].Text = "24";
            Spire.Presentation.Charts.ChartDataLabel cd25 = chart.Series[1].DataLabels.Add();
            cd25.TextFrame.Paragraphs[0].Text = "25";
            Spire.Presentation.Charts.ChartDataLabel cd26 = chart.Series[1].DataLabels.Add();
            cd26.TextFrame.Paragraphs[0].Text = "26";

            //Series 3
            Spire.Presentation.Charts.ChartDataLabel cd31 = chart.Series[2].DataLabels.Add();
            cd31.TextFrame.Paragraphs[0].Text = "31";
            Spire.Presentation.Charts.ChartDataLabel cd32 = chart.Series[2].DataLabels.Add();
            cd32.TextFrame.Paragraphs[0].Text = "32";
            Spire.Presentation.Charts.ChartDataLabel cd33 = chart.Series[2].DataLabels.Add();
            cd33.TextFrame.Paragraphs[0].Text = "33";
            Spire.Presentation.Charts.ChartDataLabel cd34 = chart.Series[2].DataLabels.Add();
            cd34.TextFrame.Paragraphs[0].Text = "34";
            Spire.Presentation.Charts.ChartDataLabel cd35 = chart.Series[2].DataLabels.Add();
            cd35.TextFrame.Paragraphs[0].Text = "35";
            Spire.Presentation.Charts.ChartDataLabel cd36 = chart.Series[2].DataLabels.Add();
            cd36.TextFrame.Paragraphs[0].Text = "36";

            presentation.SaveToFile("result418.pptx", FileFormat.Pptx2010);
        }

        //function to load data from XML file to DataTable
        private DataTable LoadData()
        {
            DataSet ds = new DataSet();
            ds.ReadXmlSchema("data-schema.xml");
            ds.ReadXml("data.xml");
            return ds.Tables[0];
        }

        //function to load data from DataTable to IChart
        private void InitChartData(IChart chart, DataTable dataTable)
        {
            for (int c = 0; c < dataTable.Columns.Count; c++)
            {
                chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
            }
            for (int r = 0; r < dataTable.Rows.Count; r++)
            {
                object[] data = dataTable.Rows[r].ItemArray;
                for (int c = 0; c < data.Length; c++)
                {
                    chart.ChartData[r + 1, c].Value = data[c];
                }
            }
        }

And I have attached my result file for checking. If you still have issue, please provide us with input files and entire code.

Sincerely,
Betsy
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar
Betsy.jiang
 
Posts: 1629
Joined: Tue Sep 06, 2016 8:30 am

Re: Custom Data Labels

Postby CPTJ » Fri Apr 21, 2017 10:17 am

Hi Betsy,

I tried your code with the latest hotfix and it all worked fine (I was previously using the free version).
Thanks for all your help with this issue.

Kind regards,
Jack
CPTJ
 
Posts: 6
Joined: Wed Apr 05, 2017 3:00 pm

Re: Custom Data Labels

Postby Jane.Bai » Mon Apr 24, 2017 1:55 am

Hello,

Thanks for your feedback.
Please do not hesitate to contact us if you need any help.

Sincerely,
Jane
E-iceblue support team
User avatar
Jane.Bai
 
Posts: 528
Joined: Tue Nov 29, 2016 1:47 am


Return to Spire.Presentation

Who is online

Users browsing this forum: No registered users and 0 guests