Hello,
We have updated a .NET solution from version 2.0 (and spire xls dll version 9.5.6.6020) to .NET 4.6.1 (and spire xls dll version 13.1.1.0)
In the new version, spire change the data label position of a columnstacked chart. The problem is that with the new version the labels are hidden behind the stacks or strikethrough.
With the followig code, with .NET 2.0 we get the attached file old_version_grafica_28022023_1133.bmp:
Private Function GraficaPruebaDatalabels() As Integer
Try
Dim rutaImagen As String = "c:\temp\grafica_" & Now.ToShortDateString.Replace("/", "") & "_" & Now.ToShortTimeString.Replace(":", "") & ".bmp"
Dim libro As New Spire.Xls.Workbook
libro.DefaultFontName = "Calibri Light"
libro.CreateEmptySheet(1)
Dim hoja As Spire.Xls.Worksheet = libro.Worksheets(0)
hoja.Name = "ConsVolXTipoAct"
'Vuelco los datos a la hoja:
hoja.Range(1, 1).Value = "Grupo"
hoja.Range(1, 2).Value = "Valor"
hoja.Range(2, 1).Value = "RV EURO"
hoja.Range(2, 2).Value = "0,157704566876736"
hoja.Range(3, 1).Value = "DP NO EURO"
hoja.Range(3, 2).Value = "0"
hoja.Range(4, 1).Value = "DIVERSIFICACION"
hoja.Range(4, 2).Value = "-0,0630980817958831"
hoja.Range(5, 1).Value = "DIVISAS"
hoja.Range(5, 2).Value = "0,0158817699531128"
'Creo el gráfico:
Dim grafico As Spire.Xls.Chart = hoja.Charts.Add()
grafico.ChartType = Spire.Xls.ExcelChartType.ColumnStacked
grafico.LeftColumn = 5
grafico.TopRow = 2
grafico.RightColumn = 31
grafico.BottomRow = 29
grafico.ChartTitle = ""
grafico.DataRange = hoja.Range("B2:B5")
grafico.SeriesDataFromRange = False
grafico.PlotArea.Fill.Visible = False
grafico.PrimaryCategoryAxis.CategoryLabels = hoja.Range("A2:A5")
grafico.PrimaryValueAxis.MajorGridLines.LineProperties.Color = colorLineasRejilla
Dim cs1 As Spire.Xls.Charts.ChartSerie = grafico.Series(0)
cs1.Values = hoja.Range("B2:B5")
cs1.DataPoints.Clear()
cs1.Format.Fill.FillType = Spire.Xls.ShapeFillType.SolidColor
cs1.DataFormat.Fill.ForeColor = Color.Black
cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
cs1.DataPoints.DefaultDataPoint.DataLabels.IsYMode = True
cs1.DataPoints.DefaultDataPoint.DataLabels.Size = CInt(17)
cs1.DataPoints.DefaultDataPoint.DataLabels.IsAutoMode = True
hoja.Range("B1:B5").NumberFormat = "0,00%"
hoja.Range("A1:A5").Style.Font.Size = 12
grafico.PrimaryCategoryAxis.TextRotationAngle = -45
grafico.PrimaryCategoryAxis.TickLabelPosition = Spire.Xls.TickLabelPositionType.TickLabelPositionLow
grafico.PrimaryCategoryAxis.Font.Size = CInt(17)
grafico.PrimaryValueAxis.Font.Size = CInt(17)
grafico.Legend.Delete()
'Pongo un color distinto a las barras con valor negativo:
With grafico
For i As Integer = 0 To cs1.Values.Count - 1
If cs1.Values.Cells(i).Value < 0 Then
cs1.DataPoints().Item(i).DataFormat.Fill.ForeColor = Color.Gray
End If
Next
cs1.DataPoints.DefaultDataPoint.DataLabels.Position = Spire.Xls.DataLabelPositionType.Above
End With
Dim img As Image = libro.SaveChartAsImage(hoja, 0)
'Para borrar el borde:
Dim w As Single = 0.2
Using g As Graphics = Graphics.FromImage(img)
Using pen As New Pen(Color.White, w)
g.DrawRectangle(pen, New Rectangle(0, 0, img.Width - 1, img.Height - 1))
g.Dispose()
End Using
End Using
'Guardo la imagen:
img.Save(rutaImagen, Imaging.ImageFormat.Bmp)
Return 0
Catch ex As Exception
Return -1
End Try
End Function
And with the followig code, with .NET 4.6.1 we get the attached file new_version_grafica_28022023_1144.bmp:
Private Function GraficaPruebaDatalabels() As Integer
Try
Dim rutaImagen As String = "c:\temp\grafica_" & Now.ToShortDateString.Replace("/", "") & "_" & Now.ToShortTimeString.Replace(":", "") & ".bmp"
Dim libro As New Spire.Xls.Workbook
libro.DefaultFontName = "Calibri Light"
libro.CreateEmptySheet(1)
Dim hoja As Spire.Xls.Worksheet = libro.Worksheets(0)
hoja.Name = "ConsVolXTipoAct"
'Vuelco los datos a la hoja:
hoja.Range(1, 1).Value = "Grupo"
hoja.Range(1, 2).Value = "Valor"
hoja.Range(2, 1).Value = "RV EURO"
hoja.Range(2, 2).Value = "0,157704566876736"
hoja.Range(3, 1).Value = "DP NO EURO"
hoja.Range(3, 2).Value = "0"
hoja.Range(4, 1).Value = "DIVERSIFICACION"
hoja.Range(4, 2).Value = "-0,0630980817958831"
hoja.Range(5, 1).Value = "DIVISAS"
hoja.Range(5, 2).Value = "0,0158817699531128"
'Creo el gráfico:
Dim grafico As Spire.Xls.Chart = hoja.Charts.Add()
grafico.ChartType = Spire.Xls.ExcelChartType.ColumnStacked
grafico.LeftColumn = 5
grafico.TopRow = 2
grafico.RightColumn = 31
grafico.BottomRow = 29
grafico.ChartTitle = ""
grafico.DataRange = hoja.Range("B2:B5")
grafico.SeriesDataFromRange = False
grafico.PlotArea.Fill.Visible = False
grafico.PrimaryCategoryAxis.CategoryLabels = hoja.Range("A2:A5")
grafico.PrimaryValueAxis.MajorGridLines.LineProperties.Color = colorLineasRejilla
Dim cs1 As Spire.Xls.Charts.ChartSerie = grafico.Series(0)
cs1.Values = hoja.Range("B2:B5")
cs1.DataPoints.Clear()
cs1.Format.Fill.FillType = Spire.Xls.ShapeFillType.SolidColor
cs1.DataFormat.Fill.ForeColor = Color.Black
cs1.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
cs1.DataPoints.DefaultDataPoint.DataLabels.IsYMode = True
cs1.DataPoints.DefaultDataPoint.DataLabels.Size = CInt(17)
'cs1.DataPoints.DefaultDataPoint.DataLabels.IsAutoMode = True 'NO COMPATIBLE CON NUEVA VERSION
hoja.Range("B1:B5").NumberFormat = "0,00%"
hoja.Range("A1:A5").Style.Font.Size = 12
grafico.PrimaryCategoryAxis.TextRotationAngle = -45
grafico.PrimaryCategoryAxis.TickLabelPosition = Spire.Xls.TickLabelPositionType.TickLabelPositionLow
grafico.PrimaryCategoryAxis.Font.Size = CInt(17)
grafico.PrimaryValueAxis.Font.Size = CInt(17)
grafico.Legend.Delete()
'Pongo un color distinto a las barras con valor negativo:
With grafico
For i As Integer = 0 To cs1.Values.Count - 1
If cs1.Values.Cells(i).Value < 0 Then
cs1.DataPoints().Item(i).DataFormat.Fill.ForeColor = Color.Gray
End If
Next
cs1.DataPoints.DefaultDataPoint.DataLabels.Position = Spire.Xls.DataLabelPositionType.Above
End With
Dim img As Image = libro.SaveChartAsImage(hoja, 0)
'Para borrar el borde:
Dim w As Single = 0.2
Using g As Graphics = Graphics.FromImage(img)
Using pen As New Pen(Color.White, w)
g.DrawRectangle(pen, New Rectangle(0, 0, img.Width - 1, img.Height - 1))
g.Dispose()
End Using
End Using
'Guardo la imagen:
img.Save(rutaImagen, Imaging.ImageFormat.Bmp)
Return 0
Catch ex As Exception
Return -1
End Try
End Function
Please, note that with the new version, the line cs1.DataPoints.DefaultDataPoint.DataLabels.IsAutoMode = True is commented to prevent an error when trying to set a readonly property.
Is possible to get the same result with the new version?
Thanks.