사용자 지정 범례 항목(차트 컨트롤)
기본적으로 범례 항목은 각각 하나의 계열에 바인딩되며 모든 계열에 대해 속성이 동일합니다. 계열에 바인딩되지 않고 모든 셀의 모양, 위치 및 여백을 조정할 수 있는 사용자 지정 범례 항목을 사용할 수 있습니다. 이렇게 하려면 Legend.CustomItems 컬렉션 속성(LegendItemCollection 개체)을 사용하여 사용자 지정 범례 항목(LegendItem 개체)을 범례에 추가합니다. 이 컬렉션의 범례 항목은 범례에 있는 다른 범례 항목의 끝에 항상 연결됩니다.
Legend.CustomItems 컬렉션의 범례 항목을 사용하여 다음을 사용자 지정할 수 있습니다.
LegendItem.ImageStyle 속성을 사용하여 기호를 지정합니다. 사각형, 선, 표식 중에서 선택합니다.
LegendItem.Image 또는 LegendItem.MarkerImage 속성을 사용하여 이미지를 기호로 사용합니다.
범례에 셀을 추가합니다.
각 셀의 모양, 위치 및 여백을 조정합니다.
사용자 지정 범례 항목에서 셀 사용
범례 셀(LegendCell 개체)을 사용자 지정 범례 항목에 추가하려면 LegendItem.Cells 컬렉션 속성(LegendCellCollection 개체)을 사용합니다.
LegendCell.CellType 속성에서 셀 유형을 지정합니다. 이 속성을 LegendCellType.SeriesSymbol로 설정한 경우 범례 셀에서 컨테이너 범례 항목에 사용되는 기호를 사용합니다.
예를 들어 더 긴 문자열을 수용할 수 있도록 두 인접 셀을 하나로 병합하려면 LegendCell.CellSpan 속성을 사용합니다.
범례 항목에 하나 이상의 범례 셀이 있는 경우 범례 항목의 모양 속성이 적용되지 않습니다.
다음 코드에서는 런타임에 범례 셀이 있는 사용자 지정 범례 항목을 사용하여 통계 평균이 가장 높은 영역을 표시합니다.
Dim avgWA As Double = Chart1.DataManipulator.Statistics.Mean("WA")
Dim avgOR As Double = Chart1.DataManipulator.Statistics.Mean("OR")
Dim avgCA As Double = Chart1.DataManipulator.Statistics.Mean("CA")
Dim top As String = (If(avgWA >= avgOR, "Washington", "Oregon"))
If avgCA >= avgWA AndAlso avgCA >= avgOR Then
top = "California"
End If
Dim newItem As New LegendItem()
newItem.ImageStyle = LegendImageStyle.Marker
newItem.MarkerStyle = MarkerStyle.Diamond
newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter)
newItem.Cells.Add(LegendCellType.Text, "State Average =", ContentAlignment.MiddleCenter)
newItem.Cells(1).CellSpan = 2
newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleCenter)
newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleCenter)
Chart1.Legends(0).CustomItems.Add(newItem)
double avgWA = Chart1.DataManipulator.Statistics.Mean("WA");
double avgOR = Chart1.DataManipulator.Statistics.Mean("OR");
double avgCA = Chart1.DataManipulator.Statistics.Mean("CA");
String top = (avgWA >= avgOR ? "Washington":"Oregon");
if (avgCA >= avgWA && avgCA >= avgOR)
top = "California";
LegendItem newItem = new LegendItem();
newItem.ImageStyle = LegendImageStyle.Marker;
newItem.MarkerStyle = MarkerStyle.Diamond;
newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, "State Average =", ContentAlignment.MiddleCenter);
newItem.Cells[1].CellSpan = 2;
newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleCenter);
newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleCenter);
Chart1.Legends[0].CustomItems.Add(newItem);
참고 항목
참조
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting