FlexCel .NET: Dynamically Changing Chart Data Label Font Size at Runtime

I need to change the font size of data labels in a chart at runtime with FlexCel .NET. The number of data labels is dynamic, and I need to reduce the font size when there are many labels (e.g., more than 20) to prevent them from overlapping.

My process is:

  1. Load a template with a chart.
  2. Populate the data.
  3. I need to modify the data label font size of the rendered chart.

I have tried some code that APIMate creates for my result file (with modified Data Labels), but runtume it does not affect my result file

here is example of my manually modified chart and APIMate code
APIMate_code.txt (206.2 KB)

In the code some lines are very specific because of my font size,

for example

LabelTextOptions = new TChartTextOptions(new TFlxChartFont("Calibri", 700, TExcelColor.FromArgb(0x00, 0x00, 0x00), TFlxFontStyles.Bold, TFlxUnderline.None, TFontScheme.None), THFlxAlignment.center, TVFlxAlignment.center, TBackgroundMode.Transparent, TextFillOptions);
...

LabelTextOptions = new TChartTextOptions(new TFlxChartFont("Calibri", 1000, TExcelColor.FromArgb(0x00, 0x00, 0x00), TFlxFontStyles.Bold, TFlxUnderline.None, TFontScheme.None), THFlxAlignment.center, TVFlxAlignment.center, TBackgroundMode.Transparent, TextFillOptions);
..

LabelTextOptions = new TChartTextOptions(new TFlxChartFont("Calibri", 900, TExcelColor.FromArgb(0x00, 0x00, 0x00), TFlxFontStyles.Bold, TFlxUnderline.None, TFontScheme.None), THFlxAlignment.center, TVFlxAlignment.center, TBackgroundMode.Transparent, TextFillOptions);

but my test run with similar changes inmy code does not change final excel result.

If someone has expirience with changes like that- please let me know how tro proceed it properly....

*Update:

it starts to work somehow....

private void RenderDataLabels(Fiplan.ReportGenerator.ExcelReporter.ExcelReporter xls)
{

    for (int i = 1; i <= xls.SheetCount; i++)
    {
        xls.ActiveSheet = i;

        int a = xls.FXLS.ObjectCount;
        FlexCel.Core.ExcelChart chart1 = xls.FXLS.GetChart(1, String.Empty);

        if (chart1 == null)
            continue;

        FlexCel.Core.TDataLabel[] dataLabels = chart1.GetDataLabels();

        FlexCel.Core.ChartSeries chartSeries = chart1.GetSeries(1, true, true, true);

        if (chartSeries == null)
            continue;

        int newFontSize;

        var legendOptions = chartSeries.LegendOptions;

        if (chartSeries.DataValues.Count() < 10)
        {
            newFontSize = 14; // default
        }
        else if (chartSeries.DataValues.Count() < 20)
        {
            newFontSize = 12;
        }
        else if (chartSeries.DataValues.Count() < 30)
        {
            newFontSize = 10;
        }
        else
        {
            newFontSize = 9;
        }


        TChartTextOptions originalLabelTextOptions = null;

        for (int ii=0; ii< dataLabels.Count(); ii++)
        {
            originalLabelTextOptions = dataLabels[ii].TextOptions;
            
            TChartTextOptions modifiedLabelTextOptions = null;
            modifiedLabelTextOptions = originalLabelTextOptions.Clone() as TChartTextOptions;

            TFlxChartFont modifiedFont = modifiedLabelTextOptions.Font;

            modifiedFont.Font = new TFlxFont(modifiedFont.Font.Name, newFontSize * 20);

            originalLabelTextOptions.Font = modifiedFont;

            dataLabels[ii].TextOptions = modifiedLabelTextOptions;

            TDataLabelOptions LabelOptions = new TDataLabelOptions();
            LabelOptions.ShowValues = true;
            LabelOptions.DataType = TLabelDataValue.SeriesInfo;
            dataLabels[ii].LabelOptions = LabelOptions;

        }

        chart1.SetDataLabels(dataLabels);

    }
    
}

Hi,
Sorry for the late reply, we just released 7.25 yesterday and now we are fighting with the backlog of support questions that had to be delayed to after the release.

If I understood right, this problem is solved then? The normal reason why modifications might not apply to the file is because in FlexCel, all(*) methods return classes that are independent from the original object, and you might forget to apply the class back. That is, in OLE automation you would do something in the style:

font = cell.getfont;
font.Size = 20;  <-this will modify the cell font.

But in FlexCel, it is something like:

font = cell.getfont;
font.Size = 20;  <- //this will modify the font object, but that is not linked to the cell. So the cell font will remain the same
cell.setfont(font)  <- // Only here the cell will be modified. 

(*)There is an exception to this rule, and that is GetChart(). This will return an object linked to the original file, and any modification you do to the chart object will apply to the linked file too, without needing to do a SetChart. This is because the ChartObject is too complex, and copying everything to give you an unlinked chart and then copying it again to apply the unlinked chart back to the file would be too slow. But that is the only exception, for every other thing, you need to do a Get/Modify/Set procedure.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.