2

我有一个 3 列的 excel 表。B 列和 C 列是我的数据,A 列包含每个点的标签。

我必须用它来让我的图表正确

series1Point.XValues = xlWorkSheetDimfract.get_Range("B1:B" + (ip - 1));
series1Point.Values = xlWorkSheetDimfract.get_Range("C1:C" + (ip - 1));

但是即使使用以下内容,A 列的标签也没有显示:

series1Point.ApplyDataLabels();

如何根据 A 列中的内容将标签添加到图表上的每个点,然后将该图表转换为 jpeg?

谢谢

Excels.SeriesCollection seriesCollectionPoint = chartPageDimfract.SeriesCollection();

Excels.Series series1Point = seriesCollectionPoint.NewSeries();
 series1Point.XValues = xlWorkSheetDimfract.get_Range("A1:B" + (ip - 1));
 series1Point.Values = xlWorkSheetDimfract.get_Range("C1:C" + (ip - 1));
     series1Point.ApplyDataLabels(Microsoft.Office.Interop.Excel.XlDataLabelsType.xlDataLabelsShowLabel);

        chartPageDimfract.ChartType = Excels.XlChartType.xlXYScatter;// xlLineMarkers;

        series1Point.MarkerStyle = Excels.XlMarkerStyle.xlMarkerStyleDot;
        series1Point.MarkerSize = 4;


        chartPageDimfract.Export(xlsfic + "temp/dimmensionFractale.jpg", "JPG", misValue);


        xlWorkBookDimfract.SaveAs(xlsfic + "temp/excel_chart_dimmensionFractale.xls",    Excels.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excels.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
   xlWorkBookDimfract.Close(true, misValue, misValue);

'ip' 是我在填写我的 Excel 表后使用的整数。它是最后一项的索引。

4

1 回答 1

2

正如我在评论中提到的,图表类型 XY Scatterplot 本身并不支持您尝试执行的那种标签。它允许您使用 X、Y 或同时使用 X 和 Y 值进行标记,但不能使用某些分类值。

创建一个包含要应用的标签的数组。数组的顺序应该相同,并且包含与系列中的点一样多的标签。

然后,这是我在 VBA 中使用的方法:

 Sub AddXYScatterLabels()

 Dim ptLabels as Variant     '<~~ this is the array that I will populate with the labels to use from Column A.

 Dim i as Long: i=1
 Dim srs as Series           '<~~ This is the series that you will assign the labels to.'
 Dim pt as Point             '<~~ use this to iterate over srs.Points collection.'
 ptLabels = Range("A1:A10")  '<~~ Modify as needed for your Range object.'

 Set srs = ActiveSheet.ChartObjects(1).SeriesCollection(1) 

 For each pt in srs.Points
     pt.DataLabel.Text = ptLabels(i,1)
     i = i+1
 Next 

 End Sub

然后,您应该可以使用该Chart.Export方法创建图表的 JPG。

ActiveChart.Export "c:\chart.jpg"
于 2013-04-18T23:09:07.693 回答