圖例 (Chart 控制項)
圖例用來區別圖表圖片中的數列和資料點。圖例會當做 Legend 物件儲存在 Chart.Legends 集合屬性中。
根據預設,圖例不會停駐於圖表區域,而是顯示於所有圖表區域外部。您可以使用 Legend 物件的 DockedToChartArea 屬性,將圖例停駐於圖表區域。使用 Series 物件的 Legend 屬性,將每個數列個別指派至圖例。
在大部分圖表類型中,每個圖例項目表示繪製數列。在圓形圖、環圈圖、漏斗圖或金字塔圖中,每個圖例項目表示數列中的資料點。
根據預設,Chart 控制項會在圖例中自動建立兩個資料行:一個表示繪製區域的色彩,一個顯示圖例文字。您可以在 Series.LegendText 屬性或 DataPoint.LegendText 屬性中指定圖例文字。
在執行階段加入圖例
若要在執行階段將圖例加入至圖表中,請確定圖例停駐於正確圖表區域、指派至資料數列,而且資料數列的 IsVisibleInLegend 屬性是設為 true。下列程式碼可示範這項處理。
' Create a new legend called "Legend2".
Chart1.Legends.Add(New Legend("Legend2"))
' Set Docking chart of the legend to the Default chart area.
Chart1.Legends("Legend2").DockToChartArea = "Default"
' Assign the legend to Series1.
Chart1.Series("Series1").Legend = "Legend2"
Chart1.Series("Series1").IsVisibleInLegend = true
// Create a new legend called "Legend2".
Chart1.Legends.Add(new Legend("Legend2"));
// Set Docking of the Legend chart to the Default Chart Area.
chart1.Legends["Legend2"].DockToChartArea = "Default";
// Assign the legend to Series1.
Chart1.Series["Series1"].Legend = "Legend2";
Chart1.Series["Series1"].IsVisibleInLegend = true;
使用 Legend 物件中的屬性來自訂圖例外觀,例如 Docking、Alignment、IsDockedInsideChartArea、Position、TableStyle、LegendStyle、MaximumAutoSize、Font 和 InterlacedRows。
使用標題、標頭和資料行
下圖顯示各種圖例功能以及 Legend 物件中的對應屬性。
注意
您無法調整 Chart.Legends 集合中的個別圖例項目和資料格。若要調整它們,請使用自訂圖例項目。
在使用 Legend.Title 屬性指定圖例標題時,使用 \n 表示新行字元。您也可以使用關鍵字,讓文字可以依據資料數列而變更。如需詳細資訊,請參閱關鍵字。
若要自訂圖例中的資料行數目,請使用 Legend.CellColumns 集合屬性。這個屬性中的資料行適用於每個自動產生的圖例項目。
下列程式碼示範 Legend.CellColumns 屬性以及關鍵字的用法。它會加入三個資料行:一個用於數列名稱、一個用於數列符號、一個用於每個數列中的資料平均值。
Chart1.Legends["Legend2"].CellColumns.Add(New LegendCellColumn("Name", LegendCellColumnType.Text, "#LEGENDTEXT"))
Chart1.Legends["Legend2"].CellColumns.Add(New LegendCellColumn("Sym", LegendCellColumnType.SeriesSymbol, ""))
Chart1.Legends["Legend2"].CellColumns.Add(New LegendCellColumn("Avg", LegendCellColumnType.Text, "#AVG{N2}"))
Chart1.Legends["Legend2"].CellColumns.Add(new LegendCellColumn("Name", LegendCellColumnType.Text, "#LEGENDTEXT"));
Chart1.Legends["Legend2"].CellColumns.Add(new LegendCellColumn("Sym", LegendCellColumnType.SeriesSymbol,""));
Chart1.Legends["Legend2"].CellColumns.Add(new LegendCellColumn("Avg", LegendCellColumnType.Text, "#AVG{N2}"));
您可以使用 LegendCellColumn 物件的屬性,例如 MinimumWidth、MaximumWidth、Alignmnent、Margins, HeaderText 和 ItemColumnsSeparator,調整每個資料行的外觀及操作。