Text wrap on Legend in a Pie chart using aspose.cells

Last post 05-03-2009, 10:36 AM by kumarvikram. 2 replies.
Sort Posts: Previous Next
  •  05-02-2009, 2:15 PM 177700

    Text wrap on Legend in a Pie chart using aspose.cells .NET

    Hi,

    I am using aspose.Cells to create a pie chart with a width of 250 X 350. I want the text in legend to wrap around onto next line. I know this can be done in aspose.Chart using table layout, could you show me how to implement it in aspose.cells. I am copying the code here.

    Thanks

    private Aspose.Cells.Chart createWorkbookChart()
            {
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
                sheet.Name = "Data";
                Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
                workbook.ChangePalette(Color.Orange, 53);
                workbook.ChangePalette(Color.LightBlue, 54);
                workbook.ChangePalette(Color.LightCoral, 55);
                workbook.ChangePalette(Color.Indigo, 52);
                Color[] colors = workbook.Colors;
                cells["A1"].PutValue("Excelsior lasalle property fund LLC ");
                cells["A2"].PutValue("Excelsior lasalle property fund LLC property fund LLC ");
                cells["A3"].PutValue("Germany");
                cells["A4"].PutValue("England");
                cells["A5"].PutValue("Sweden");
                cells["B1"].PutValue("Sale");
                cells["B2"].PutValue(0.32);
                cells["B3"].PutValue(0.62);
                cells["B4"].PutValue(0.06);
                cells["B5"].PutValue(0.06);
                int sheetIndex = workbook.Worksheets.Add(Aspose.Cells.SheetType.Chart);
                sheet = workbook.Worksheets[sheetIndex];
                sheet.Name = "Chart";
                int chartIndex = 0;
                chartIndex = sheet.Charts.Add(Aspose.Cells.ChartType.Pie, 1, 3, 25, 12);
                Aspose.Cells.Chart c = sheet.Charts[chartIndex];
              
            
                c.ChartObject.Height = 250;
                c.ChartObject.Width = 350;

                c.PlotArea.Width = 2700;
                c.PlotArea.Height = 2700;
                c.PlotArea.X = 0;
                c.PlotArea.Y = 800;
                c.Legend.Border.IsVisible = false;
                c.Legend.AutoScaleFont = true;
              
                c.Legend.AutoScaleFont = false;
                c.Legend.TextFont.Name = "Arial";
                c.Legend.TextFont.Size = 7;
                c.ChartObject.Height = 275;
                c.ChartObject.Width = 450;
              

                //checking to see the length of legend

            

                c.Legend.TextFont.Name = "Arial";
                c.Legend.TextFont.Size = 7;
                c.Legend.Position = Aspose.Cells.LegendPositionType.Right;
          
                c.ChartArea.Background = Aspose.Cells.BackgroundMode.Transparent;
                c.NSeries.Add("Data!B2:B5", true);
                c.NSeries.CategoryData = "Data!A2:A5";
                Aspose.Cells.DataLabels datalabels;
                for (int i = 0; i < c.NSeries.Count; i++)
                {
                    datalabels = c.NSeries[i].DataLabels;
                    //datalabels.IsValueShown = true;
                    datalabels.IsPercentageShown = true;
                    datalabels.NumberFormat = "0%";
                    datalabels.Postion = Aspose.Cells.LabelPositionType.OutsideEnd;
                    datalabels.TextFont.Name = "Arial";
                    datalabels.TextFont.Size = 7;
                }
                   
                return c;
            }
     
  •  05-03-2009, 9:47 AM 177725 in reply to 177700

    Re: Text wrap on Legend in a Pie chart using aspose.cells

    Hi,

    Well, you may use escape sequence character i.e.., '\n' while putting the long text into the source cells to insert line break for legend entries for your need.

    See the following updated code:

    private Aspose.Cells.Chart createWorkbookChart()
            {
                Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
                Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
                sheet.Name = "Data";
                Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
                workbook.ChangePalette(Color.Orange, 53);
                workbook.ChangePalette(Color.LightBlue, 54);
                workbook.ChangePalette(Color.LightCoral, 55);
                workbook.ChangePalette(Color.Indigo, 52);
                Color[] colors = workbook.Colors;
                cells["A1"].PutValue("Excelsior lasalle property fund LLC ");
                cells["A2"].PutValue("Excelsior lasalle property fund\n LLC property fund LLC ");
                cells["A3"].PutValue("Germany");
                cells["A4"].PutValue("England");
                cells["A5"].PutValue("Sweden");
                cells["B1"].PutValue("Sale");
                cells["B2"].PutValue(0.32);
                cells["B3"].PutValue(0.62);
                cells["B4"].PutValue(0.06);
                cells["B5"].PutValue(0.06);
                int sheetIndex = workbook.Worksheets.Add(Aspose.Cells.SheetType.Chart);
                sheet = workbook.Worksheets[sheetIndex];
                sheet.Name = "Chart";
                int chartIndex = 0;
                chartIndex = sheet.Charts.Add(Aspose.Cells.ChartType.Pie, 1, 3, 25, 12);
                Aspose.Cells.Chart c = sheet.Charts[chartIndex];
              
            
                c.ChartObject.Height = 250;
                c.ChartObject.Width = 350;

                c.PlotArea.Width = 2700;
                c.PlotArea.Height = 2700;
                c.PlotArea.X = 0;
                c.PlotArea.Y = 800;
                c.Legend.Border.IsVisible = false;
                c.Legend.AutoScaleFont = true;
              
                c.Legend.AutoScaleFont = false;
                c.Legend.TextFont.Name = "Arial";
                c.Legend.TextFont.Size = 7;
                c.ChartObject.Height = 275;
                c.ChartObject.Width = 450;
              

                //checking to see the length of legend

            

                c.Legend.TextFont.Name = "Arial";
                c.Legend.TextFont.Size = 7;
                c.Legend.Position = Aspose.Cells.LegendPositionType.Right;
          
                c.ChartArea.Background = Aspose.Cells.BackgroundMode.Transparent;
                c.NSeries.Add("Data!B2:B5", true);
                c.NSeries.CategoryData = "Data!A2:A5";
                Aspose.Cells.DataLabels datalabels;
                for (int i = 0; i < c.NSeries.Count; i++)
                {
                    datalabels = c.NSeries[i].DataLabels;
                    //datalabels.IsValueShown = true;
                    datalabels.IsPercentageShown = true;
                    datalabels.NumberFormat = "0%";
                    datalabels.Postion = Aspose.Cells.LabelPositionType.OutsideEnd;
                    datalabels.TextFont.Name = "Arial";
                    datalabels.TextFont.Size = 7;
                }
                   
                return c;
            }

    Thank you.


    Amjad Sahi
    Support Developer,
    Aspose Sialkot Team
    Contact Us
     
  •  05-03-2009, 10:36 AM 177729 in reply to 177725

    Re: Text wrap on Legend in a Pie chart using aspose.cells

    Thanks for the quick response. I think this should be a feature in future releases.

    For those who are in similar situation, I made this function to add line break character. Hope this helps.

    private string fixLongString(string str)
            {
                 int pos;
                for (int i = 0; i < str.Length; i++)
                {
                    pos = str.IndexOf(" ", i);
                    if (pos > 25)
                    {
                        str = str.Insert(pos, "\n");
                        break;
                    }
                    else
                    {
                        i = pos;
                    }
                }
                return str;
            }
     
View as RSS news feed in XML