Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Jul 10, 2023 12:13 pm

In my ASP.NET MVC Framework (4.6) project, I've programmatically created a presentation. For the most part, it's working splendidly. But there a couple of quirks I need to get cleaned up if possible:

(1) When I try to set the Chart Legend font color, it's not being honored. I need the text to be white against a black background.
(2) I'm not sure how to set the the minimum and maximum axis bounds. I messed around with it a bit but like the Chart Legend font color, the values I entered didn't seem to be honored so I removed it.

Any help would be appreciated! Here's my code below:
Code: Select all
        private void AddSlideWithChartData<T>(string title, Scorecard<T> scorecard, Indicator indicator) where T : IConvertible
        {
            if (scorecard == null) return;

            ISlide slide = Presentation.Slides.Append();
            slide.SlideBackground.Type = BackgroundType.Custom;
            slide.SlideBackground.Fill.FillType = FillFormatType.Solid;
            slide.SlideBackground.Fill.SolidColor.Color = Color.Black;

            IChart chart = slide.Shapes.AppendChart(ChartType.LineMarkers, new RectangleF(25, 25, Presentation.SlideSize.Size.Width - 50, Presentation.SlideSize.Size.Height - 75));

            chart.HasTitle = true;
            chart.ChartTitle.TextProperties.Text = title;
            chart.ChartTitle.TextProperties.IsCentered = true;
            chart.ChartLegend.Position = ChartLegendPositionType.Top;


            chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            chart.ChartTitle.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

            //NOTE: Here are the two lines I assumed would set the chart legend font color but aren't...
            chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

            chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

            chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            chart.PrimaryCategoryAxis.TextProperties.Paragraphs[0]. DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

            chart.PrimaryValueAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
            chart.PrimaryValueAxis.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

            chart.ChartData[0, 0].Text = "Month";
            chart.ChartData[0, 2].Text = "Dollars";
            chart.ChartData[0, 3].Text = "Goal";
            chart.ChartData[0, 4].Text = Store.FullName;
            chart.ChartData[0, 5].Text = "Baseline";

            int rowIndex = 1;
            foreach (var record in scorecard.Records)
            {
                chart.ChartData[rowIndex, 0].Value = record.Month.ToString();
                chart.ChartData[rowIndex, 2].Value = double.Parse(record.Metrics[ReportMetric.ActualT12].ToString());
                chart.ChartData[rowIndex, 3].Value = double.Parse(record.Metrics[ReportMetric.Goal].ToString());
                chart.ChartData[rowIndex, 4].Value = double.Parse(record.Metrics[ReportMetric.Benchmark].ToString());
                chart.ChartData[rowIndex, 5].Value = double.Parse(record.Metrics[ReportMetric.Baseline].ToString());

                rowIndex++;
            }

            chart.Series.SeriesLabel = chart.ChartData["C1", "F1"];
            chart.Categories.CategoryLabels = chart.ChartData["A2", $"A{rowIndex}"];
            var lineColors = new List<Color>() { Color.Blue, Color.Red, Color.HotPink, Color.Green };

            for (int i = 0; i < 4; i++)
            {
                chart.Series[i].Values = chart.ChartData[$"{char.ConvertFromUtf32(i + 67)}2", $"{char.ConvertFromUtf32(i + 67)}{rowIndex}"];
                chart.Series[i].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.KnownColor = KnownColors.White;
                chart.Series[i].DataLabels.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
                chart.Series[i].MarkerStyle = ChartMarkerType.Circle;
            }


            string imagePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"img\good-indicators\{indicator}.png");
            IEmbedImage image = slide.Shapes.AppendEmbedImage(ShapeType.Rectangle, imagePath, new RectangleF(25, 65, 97.5F, 33));
            image.Line.FillType = FillFormatType.None;
        }

dhughes01
 
Posts: 2
Joined: Mon Jul 10, 2023 11:07 am

Tue Jul 11, 2023 3:41 am

Hi,

Thanks for your inquiry.

For the first issue, you can use the following code to set the text color of the chart legend:
Code: Select all
chart.ChartLegend.LegendEntries[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.LegendEntries[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;

Please iterate through the LegendEntries collection to check if the specific entry exists. If it does, you can apply the desired effect; otherwise, there is no need to make any changes.
Regarding the second issue, we recommend referring to the tutorial available at the following link: https://www.e-iceblue.com/Tutorials/Spire.Presentation/Spire.Presentation-Program-Guide/Chart/How-to-Format-Axis-of-Chart-in-C-VB.NET.html

We hope this information helps resolve your concerns. If you have any further questions or need further assistance, please do not hesitate to reach out to us.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Wed Jul 12, 2023 5:28 pm

Thanks for the quick reply. The link you provided was useful to help me understand how to change the y-axis. I haven't had any success with changing the font color for the chart legend, though. I've tried four different approaches (see below). Am I overlooking something? Note that I am currently using a free version (FreeSpire.Presentation 7.8.0).

Code: Select all
//Attempt #1: Original Code
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

//Attempt #2: Modified Code
chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

//Attempt #3: Combination of Original & Modified (just in case both snippets were required)
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;
chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;

//Attempt #4: Just in case I needed to loop through all values instead of assuming an index of [0]:
foreach (var entryObject in chart.ChartLegend.LegendEntrys)
{
    var entry = entryObject as ChartLegend;
    if (entry == null) continue;
    foreach (var paragraphObj in entry.TextProperties.Paragraphs)
    {
        var paragraph = paragraphObj as TextParagraph;
        if (paragraph == null) continue;
        paragraph.DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
        paragraph.DefaultCharacterProperties.Fill.SolidColor.Color = Color.White;
    }
}

dhughes01
 
Posts: 2
Joined: Mon Jul 10, 2023 11:07 am

Thu Jul 13, 2023 2:01 am

Hi,

Thanks for your reply.
I created a simple demo for your reference, using the free version (FreeSpire.Presentation_7.8.0), the color of the chart legend was changed successfully. See attached screenshot. Please use the code snippet I shared and have a test.
test.png


Code: Select all
//Create a PPT file
Presentation presentation = new Presentation();

//Add line markers chart
RectangleF rect1 = new RectangleF(90, 100, 550, 320);
IChart chart = presentation.Slides[0].Shapes.AppendChart(ChartType.LineMarkers, rect1, false);

//Chart title
chart.ChartTitle.TextProperties.Text = "Line Makers Chart";
chart.ChartTitle.TextProperties.IsCentered = true;
chart.ChartTitle.Height = 30;
chart.HasTitle = true;

//Data for series
Double[] Series1 = new Double[] { 7.7, 8.9, 1.0, 2.4 };
Double[] Series2 = new Double[] { 15.2, 5.3, 6.7, 8 };

//Set series text
chart.ChartData[0, 1].Text = "Series1";
chart.ChartData[0, 2].Text = "Series2";

//Set category text
chart.ChartData[1, 0].Text = "Category 1";
chart.ChartData[2, 0].Text = "Category 2";
chart.ChartData[3, 0].Text = "Category 3";
chart.ChartData[4, 0].Text = "Category 4";

//Fill data for chart
for (int i = 0; i < Series1.Length; ++i)
{
    chart.ChartData[i + 1, 1].Value = Series1[i];
    chart.ChartData[i + 1, 2].Value = Series2[i];

}

//Set series label
chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];
//Set category label
chart.Categories.CategoryLabels = chart.ChartData["A2", "A5"];

//Set values for series
chart.Series[0].Values = chart.ChartData["B2", "B5"];
chart.Series[1].Values = chart.ChartData["C2", "C5"];

chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.FillType = FillFormatType.Solid;
chart.ChartLegend.LegendEntrys[0].TextProperties.Paragraphs[0].DefaultCharacterProperties.Fill.SolidColor.Color = Color.Red;


presentation.SaveToFile("test.pptx", FileFormat.Pptx2013);




Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Presentation

cron