我正在设置一个条形图,最终会导出到PowerPoint幻灯片中。我已经把它完美地导出了,但是已经要求在每个条形图的顶部将该条的值放在它的正上方。我看起来很高很低,很遗憾无法知道如何做到这一点。
答案 0 :(得分:1)
我在Aspose担任社交媒体开发人员。检查以下示例代码,将自定义标签设置为值并设置标签的位置。
'Instantiate Presentation class that represents PPTX file
Dim pres As New Presentation()
'Access first slide
Dim sld As ISlide = pres.Slides(0)
' Add chart with default data
Dim chart As IChart = sld.Shapes.AddChart(ChartType.ClusteredBar, 0, 0, 500, 500)
'Setting chart Title
'chart.ChartTitle.TextFrameForOverriding.Text = "Sample Title";
chart.ChartTitle.AddTextFrameForOverriding("Sample Title")
chart.ChartTitle.TextFrameForOverriding.TextFrameFormat.CenterText = NullableBool.True
chart.ChartTitle.Height = 20
chart.HasTitle = True
'Set first series to Show Values
chart.ChartData.Series(0).Labels.DefaultDataLabelFormat.ShowValue = True
'Setting the index of chart data sheet
Dim defaultWorksheetIndex As Integer = 0
'Getting the chart data worksheet
Dim fact As IChartDataWorkbook = chart.ChartData.ChartDataWorkbook
'Delete default generated series and categories
chart.ChartData.Series.Clear()
chart.ChartData.Categories.Clear()
Dim s As Integer = chart.ChartData.Series.Count
s = chart.ChartData.Categories.Count
'Adding new series
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.Type)
chart.ChartData.Series.Add(fact.GetCell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.Type)
'Adding new categories
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"))
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"))
chart.ChartData.Categories.Add(fact.GetCell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"))
'Take first chart series
Dim series As IChartSeries = chart.ChartData.Series(0)
'Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 1, 20))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 1, 50))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 1, 30))
'Setting fill color for series
series.Format.Fill.FillType = FillType.Solid
series.Format.Fill.SolidFillColor.Color = System.Drawing.Color.Red
'Set Data Labels for first series
For Each point As IChartDataPoint In series.DataPoints
Dim lbl As IDataLabel = point.Label
lbl.DataLabelFormat.ShowValue = True
lbl.DataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd
Next point
'Take second chart series
series = chart.ChartData.Series(1)
'Now populating series data
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 1, 2, 30))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 2, 2, 10))
series.DataPoints.AddDataPointForBarSeries(fact.GetCell(defaultWorksheetIndex, 3, 2, 60))
'Setting fill color for series
series.Format.Fill.FillType = FillType.Solid
series.Format.Fill.SolidFillColor.Color = System.Drawing.Color.Green
'Set Data Labels for first series
For Each point As IChartDataPoint In series.DataPoints
Dim lbl As IDataLabel = point.Label
lbl.DataLabelFormat.ShowValue = True
lbl.DataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd
Next point
'Save presentation with chart
pres.Save("c:\data\AsposeChart.pptx", Aspose.Slides.Export.SaveFormat.Pptx)
以下是上述代码中的示例,以便了解如何设置标签。
'Set Data Labels for first series
For Each point As IChartDataPoint In series.DataPoints
Dim lbl As IDataLabel = point.Label
lbl.DataLabelFormat.ShowValue = True
lbl.DataLabelFormat.Position = LegendDataLabelPosition.OutsideEnd
Next point