이번에는 ChartFX X축 특정 Label 색상 변경 하는 방법을 알아보도록 하겠습니다.
프로젝트 진행 중 X축 상에 출력된 Label 중 특정한 위치의 Label만 색상을 변경해 달라는 요청이 접수 되었습니다. Label의 속성을 직접 변경할 수 있지 않을까 생각해 보았는데 그렇지는 않았습니다.
저는 section을 이용하여 Label 색상을 변경하였습니다. section은 원래 차트의 영역을 나누어 그 부분에만 속성을 변경하는데 사용합니다. 제가 제시한 방법보다 더 나은 방법이 있을 수 있습니다.
일단 소스를 보겠습니다.
SolidBackground solidBackground = new SolidBackground();
solidBackground.Color = System.Drawing.Color.White;
var chart = String.Empty;
var swChart = new StringWriter();
var hwChart = new HtmlTextWriter(swChart);
var crtChart = new Chart
{
Width = 560,
Height = 300,
Gallery = Gallery.Lines,
RenderFormat = "Image",
Background = solidBackground,
Border = new ChartFX.WebForms.Adornments.ImageBorder(ImageBorderType.Rounded)
};
crtChart.DataSourceSettings.Style = DataSourceStyles.Transpose;
crtChart.Border.Color = Color.Transparent;
crtChart.AxisY.Style |= AxisStyles.HideText;
crtChart.AxisY.Style |= AxisStyles.Hide;
crtChart.AxisX.Grids.Major.Visible = false;
crtChart.AxisX.Grids.Minor.Visible = false;
crtChart.AxisX.Sections.Add(new AxisSection(4, 4, Color.Transparent));
crtChart.AxisX.Sections[0].TextColor = ColorTranslator.FromHtml("#ff0000");
crtChart.AxisX.Sections[0].Grids.Major.Visible = false;
crtChart.DataSource = datasource;
crtChart.DataBind();
crtChart.RenderControl(hwChart);
chart = swChart.ToString();
litChart.Text = chart;
강조된 된 27-29 행 부분을 보시면 X축에 Section을 추가하여 글자의 색상을 빨간색으로 변경하였습니다.
출력 결과는 다음과 같습니다.
2013년이 빨간색으로 변경 되었습니다.
2010년 Label의 색상을 변경하려면
crtChart.AxisX.Sections.Add(new AxisSection(4, 4, Color.Transparent));
부분을
crtChart.AxisX.Sections.Add(new AxisSection(1, 1, Color.Transparent));
로 변경하시면 됩니다. AxisSection에 대한 자세한 내용은 Reference를 참조하시면 됩니다.
