How to set font color of legend for a particular series?

Hi,

I am facing difficulty in changing font color of legend for a particular series in any of the charts.

For example,

Series1

Series2

Series3

Series4

Four series are there in a legend. I want to set series1 legend color as blue.

I tried the following code:(VB.net)

chart.nseries(0).legendentry.font.color=color.blue

But, its not working.

when i tried,

chart.legend.font.color=color.blue

It set blue color to all the series in the legend.

But i want to set color for particular series in legend. Please give me solution for this asap

Thanks in advance.

Hi,


Please try our latest fix/version: Aspose.Cells for .NET v7.4.2.3 I have tested your scenario using the following sample code, it works fine, please see the attache output file here.

Sample code:

Workbook workbook = new Workbook();
int sheetIndex = workbook.Worksheets.Add();
Worksheet worksheet = workbook.Worksheets[sheetIndex];

Style styl = new Style();
styl.Number = 15;

worksheet.Cells[“A3”].PutValue(“Series 1”);
worksheet.Cells[“A4”].PutValue(“Series 2”);
worksheet.Cells[“A5”].PutValue(“Series 3”);
worksheet.Cells[“A6”].PutValue(“Series 4”);

worksheet.Cells[“B3”].PutValue(1.44);
worksheet.Cells[“C3”].PutValue(0.2);
worksheet.Cells[“D3”].PutValue(3.6);
worksheet.Cells[“E3”].PutValue(4.5);
worksheet.Cells[“F3”].PutValue(5.17);

worksheet.Cells[“B4”].PutValue(1.1);
worksheet.Cells[“C4”].PutValue(1.2);
worksheet.Cells[“D4”].PutValue(3.5);
worksheet.Cells[“E4”].PutValue(4.5);
worksheet.Cells[“F4”].PutValue(5.7);

worksheet.Cells[“B5”].PutValue(1.4);
worksheet.Cells[“C5”].PutValue(1.32);
worksheet.Cells[“D5”].PutValue(5.0);
worksheet.Cells[“E5”].PutValue(4.9);
worksheet.Cells[“F5”].PutValue(3.7);

worksheet.Cells[“B6”].PutValue(1.4);
worksheet.Cells[“C6”].PutValue(1.2);
worksheet.Cells[“D6”].PutValue(3);
worksheet.Cells[“E6”].PutValue(4.5);
worksheet.Cells[“F6”].PutValue(5.7);

int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 10, 10, 20, 20);
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

chart.NSeries.Add(“B3:F6”, false);

chart.NSeries[0].Name = “=A3”;
chart.NSeries[0].LegendEntry.Font.Color = Color.Blue;

chart.NSeries[1].Name = “=A4”;

chart.NSeries[2].Name = “=A5”;
chart.NSeries[3].Name = “=A6”;
chart.PlotArea.Area.ForegroundColor = Color.White;


workbook.Save(“e:\test2\out__legendblue2.xlsx”);


If you still find the issue, kindly paste your sample runnable code and attach your output file here, we will check your issue soon.

Thank you.

Hi,

I tried with the new latest version of Aspose.cells. But still, it not setting font color for a series in legend. i attached Project file for your reference. Please check it and give me solution asap

Hi,

Thanks for the sample project.

After an initial test, I can notice the issue with the output image of the chart by Aspose.Cells API. The custom font color of the legend entry for a particular series is not rendered in the output image file. I did add / update a few lines to your sample code, I found, it works fine if we save the Excel file (the chart is fine with custom font color for the legend entry for the series). But the output image is not the same as per the original chart in MS Excel.

Sample code:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'Dim lic2 As Aspose.Cells.License = New Aspose.Cells.License()
'lic2.SetLicense("D:\\Aspose Data\\Aspose.Total.lic")

Dim lic As Aspose.Slides.License = New Aspose.Slides.License()

' lic.SetLicense("D:\\Aspose Data\\Aspose.Total.lic")

'Step - 1: Create an excel chart using Aspose.Cells

'--------------------------------------------------
'Create a workbook
Dim wb As Workbook = New Workbook()
'Add an excel chart
Dim chartRows As Integer = 55
Dim chartCols As Integer = 25
Dim cellsName As String() = { _
"A1", "A2", "A3", "A4", _
"B1", "B2", "B3", "B4", _
"C1", "C2", "C3", "C4", _
"D1", "D2", "D3", "D4", _
"E1", "E2", "E3", "E4" _
}
'Array of cell data
Dim cellsValue As Integer() = { _
67, 86, 68, 91, _
44, 64, 89, 48, _
46, 97, 78, 60, _
43, 29, 69, 26, _
24, 40, 38, 25 _
}
'Add a new worksheet to populate cells with data
Dim dataSheetIdx As Integer = wb.Worksheets.Add()
Dim dataSheet As Worksheet = wb.Worksheets(dataSheetIdx)
Dim sheetName As String = "DataSheet"
dataSheet.Name = sheetName
'Populate DataSheet with data
For i As Integer = 0 To cellsName.Length - 1
Dim cellName As String = cellsName(i)
Dim cellValue As Integer = cellsValue(i)
dataSheet.Cells(cellName).PutValue(cellValue)
Next
'Add a chart sheet
Dim chartSheetIdx As Integer = wb.Worksheets.Add(SheetType.Chart)
Dim chartSheet As Worksheet = wb.Worksheets(chartSheetIdx)
chartSheet.Name = "ChartSheet"
'Add a chart in ChartSheet with data series from DataSheet
Dim chartIdx As Integer = chartSheet.Charts.Add(ChartType.Column, 0, chartRows, 0, chartCols)
Dim _chart As Chart = chartSheet.Charts(chartIdx)
_chart.NSeries.Add(sheetName + "!A1:E1", False)


_chart.NSeries.Add(sheetName + "!A2:E2", False)
_chart.NSeries.Add(sheetName + "!A3:E3", False)
_chart.NSeries.Add(sheetName + "!A4:E4", False)
_chart.Legend.Font.Size = 8


_chart.CategoryAxis.TickLabels.Font.Size = 8
_chart.ValueAxis.TickLabels.Font.Size = 8
For i = 0 To _chart.NSeries.Count - 1
_chart.NSeries(i).DataLabels.Font.Size = 8
Next i
'Set ChartSheet an active sheet
_chart.SizeWithWindow = True



' _chart.ChartObject.Width = CInt(Fix((5472 / 576.0F) * 96.0F * 0.98F))
' _chart.ChartObject.Height = CInt(Fix((2304 / 576.0F) * 96.0F * 0.98F))

_chart.NSeries(0).LegendEntry.Font.Color = Color.Blue
wb.Settings.WindowWidthInch = 5472 / 576.0F
wb.Settings.WindowHeightInch = 2304 / 576.0F + 0.52
wb.Worksheets.ActiveSheetIndex = chartSheetIdx

'Dim chartSheetIndex As Integer = AddExcelChartInWorkbook(wb, chartRows, chartCols)
'Step - 2: Set the OLE size of the chart. using Aspose.Cells
'-----------------------------------------------------------
Dim r As Integer = dataSheet.Cells.GetRowHeightInch(0) * 576
Dim c As Integer = dataSheet.Cells.GetColumnWidthInch(0) * 5 * 576


'wb.Worksheets.SetOleSize(0, 5472 / 576, 0, 2304 / 576)

' wb.Worksheets.SetOleSize(0, r, 0, c)
'Step - 3: Get the image of the chart with Aspose.Cells
'-----------------------------------------------------------
'wb.Worksheets(chartSheetIdx).Charts(0).ChartArea.Width = 700


Dim imgChart As Bitmap = wb.Worksheets(chartSheetIdx).Charts(0).ToImage()
imgChart.Save("..\..\..\TestOleChart1.bmp")


'Save the workbook to stream
Dim wbStream As MemoryStream = wb.SaveToStream()

wb.Save("..\..\..\TestOleChart1.xls")

'Step - 4 AND 5
'-----------------------------------------------------------
'Step - 4: Embed the chart as an OLE object inside .ppt presentation using Aspose.Slides
'-----------------------------------------------------------
'Step - 5: Replace the object changed image with the image obtained in step 3 to cater Object Changed Issue
'-----------------------------------------------------------
'Create a presentation
Dim pres As Presentation = New Presentation()
Dim sld As Slide = pres.GetSlideByPosition(1)
'Add the workbook on slide
AddExcelChartInPresentation(pres, sld, wbStream, imgChart)
'Step - 6: Write the output presentation on disk
'-----------------------------------------------------------
pres.Write("..\..\..\TestOleChart1.ppt")
End Sub

I have logged a ticket with an id "CELLSNET-41548" for your issue. We will look into your issue soon.

Once we have any update on it or we sort it out, we will let you know here.

Thank you.

Hi,

We have fixed the issue. Please download and try this fix: Aspose.Cells for .NET v7.4.2.5 and let us know your feedback. We have supported setting legend item’s color for Chart to Image feature.

Thank you.

The issues you have found earlier (filed as CELLSNET-41548) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.