圖表中,圖表資料標簽以資料化形式表現圖表中的特定資料,可增強圖表的可讀性,我們可以對圖表添加資料標簽,也可以對已有的資料標簽進行修改或者洗掉,下面將通過C#代碼來實作,
使用工具:Spire.XLS for .NET pack
dll檔案獲取及參考:
方法1:通過官網下載包,下載后,解壓檔案,安裝程式,安裝后,將安裝路徑下Bin檔案夾下的Spire.Xls.dll檔案添加參考至vs專案程式,如下所示:

方法2:可通過Nuget下載,
C# 示例
【添加資料標簽】
using Spire.Xls; using Spire.Xls.Charts; using System.Drawing; namespace AddDataLables { class Program { static void Main(string[] args) { //加載測驗檔案 Workbook workbook = new Workbook(); workbook.LoadFromFile("test.xlsx"); //獲取第一個作業薄以及其中的第一個圖表 Worksheet sheet = workbook.Worksheets[0]; Chart chart = sheet.Charts[0]; //獲取圖表中的指定系列 ChartSerie serie1 = chart.Series[1]; //添加資料標簽,并設定資料標簽樣式 serie1.DataPoints.DefaultDataPoint.DataLabels.HasValue = https://www.cnblogs.com/Yesi/p/true; serie1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.FillType = ShapeFillType.SolidColor; serie1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Fill.ForeColor = Color.White; serie1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Border.Pattern = ChartLinePatternType.Solid; serie1.DataPoints.DefaultDataPoint.DataLabels.FrameFormat.Border.Color = Color.Green; ////使用檔案中其他單元格的資料自定義datalabel //ChartSerie serie2 = chart.Series[2]; //serie2.DataPoints.DefaultDataPoint.DataLabels.ValueFromCell = sheet.Range["B4:E4"]; ////添加資料標注 //serie2.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true; //保存檔案 workbook.SaveToFile("AddDataLable.xlsx"); System.Diagnostics.Process.Start("AddDataLable.xlsx"); } } }
資料標簽效果:

資料標注效果:

【編輯/修改資料標簽】
這里以上面代碼中添加的資料標注為例,進行修改等操作,
using Spire.Xls; using Spire.Xls.Charts; using System.Drawing; namespace ModifyDataLable { class Program { static void Main(string[] args) { //加載檔案 Workbook workbook = new Workbook(); workbook.LoadFromFile("AddDataLable.xlsx"); //獲取第一個作業表以及表里面的第一個圖表 Worksheet sheet = workbook.Worksheets[0]; Chart chart = sheet.Charts[0]; //獲取圖表系列及資料標簽 ChartSerie chartSeries = chart.Series[2]; ChartDataLabels cslabel = chartSeries.DataPoints[1].DataLabels; cslabel.HasLegendKey = true;//設定顯示圖例項標示 cslabel.Position = DataLabelPositionType.Center;//設定資料標簽的位置居中 //自定義資料標簽的位置,默認的原點在圖表左上角; cslabel.IsYMode = true; cslabel.IsXMode = true; cslabel.X = 1100; cslabel.Y = 400; //設定顯示引導線(任意設定一個DataPoint會對整個系列生效,但是只有自定義資料標簽位置之后才會顯示效果) cslabel.ShowLeaderLines = true; //取消楔形標注(任意設定一個DataPoint會對整個系列生效) cslabel.HasWedgeCallout = false; //保存 workbook.SaveToFile("ModifyDatalable.xlsx"); System.Diagnostics.Process.Start("ModifyDatalable.xlsx"); } } }
資料標簽修改效果:

【洗掉資料標簽】
using Spire.Xls; using Spire.Xls.Charts; namespace DeleteDataLable { class Program { static void Main(string[] args) { //加載檔案 Workbook workbook = new Workbook(); workbook.LoadFromFile("AddDataLable.xlsx"); //獲取第一個作業表以及表里面的第一個圖表 Worksheet sheet = workbook.Worksheets[0]; Chart chart = sheet.Charts[0]; //洗掉所有系列的資料標簽 for (int i = 0; i < chart.Series.Count; i++) { ChartSerie chartSeries = chart.Series[i]; chartSeries.DataPoints.Clear(); } //洗掉指定資料系列的資料標簽 //ChartSerie chartSeries = chart.Series[2]; //chartSeries.DataPoints.Clear(); //洗掉指定系列中資料標簽的指定值 //ChartSerie chartSeries = chart.Series[2]; //ChartDataLabels cslabel = chartSeries.DataPoints[0].DataLabels; //cslabel.Text = " "; //cslabel.HasLegendKey = false; //保存檔案 workbook.SaveToFile("RemoveDataLable.xlsx"); System.Diagnostics.Process.Start("RemoveDataLable.xlsx"); } } }
運行程式后可查看資料標簽洗掉效果,
(本文完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/114243.html
標籤:C#
