본문 바로가기
C#/DevExpress

[C# DevExpress] ChartControl - PieChart

by 샤나엘 2018. 4. 9.
반응형



C# DevExpress ChartControl - PieChart 사용예제입니다.

 

아래와 같은 Chart의 형태가 PieChart입니다.

 

아래 표시된 PieChart는 Grid Data의 Product별 누적값을 집계해서 PieChart로 표시하는 예제입니다.

 

 

 

Series의 ViewType을 Pie로 설정합니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private void SetPieChartData(ChartControl chart, DataTable dt)
        {
            ViewType viewType = ViewType.Pie;
 
            Dictionary<stringdouble> totalInfo = new Dictionary<stringdouble>();
 
            //TotalQty 집계
            foreach (DataRow row in dt.Rows)
            {
                string product = row["PRODUCT"].ToString();
                string year = row["YEAR"].ToString();
                int qty = (int)row["QTY"];
 
                if (totalInfo.ContainsKey(product) == false)
                    totalInfo.Add(product, 0);
 
                totalInfo[product] += qty;
            }
 
            Series series = new Series("Total", ViewType.Pie);
            chart.Series.Add(series);
 
            foreach (KeyValuePair<stringdouble> info in totalInfo)
            {
                string product = info.Key;
                double qty = info.Value;
 
                SeriesPoint point = new SeriesPoint(product, qty);
                series.Points.Add(point);
            }
 
            //ChartTitle 생성
            ChartTitle title = new ChartTitle();
            title.Text = viewType.ToString();
            chart.Titles.Add(title);
        }

 

위 소스로 별다른 설정을 하지 않고 표시했더니 

ChartLable가 기본은 %로 표시가 되고, Legend값도 %값으로 표시되네요.

 

이 부분을 변경해보겠습니다.

 

=================================================================

 

[Label의 값을 데이터의 값 그대로 나오도록 설정 방법]

 

아래 소스에서 빨간색으로 표시된 부분을 추가해줍니다.

(series.Label.PointOptions as PiePointOptions).PercentOptions.ValueAsPercent = false;
series.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.General;

 

[소스코드]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        private void SetPieChartData(ChartControl chart, DataTable dt)
        {
            ViewType viewType = ViewType.Pie;
 
            Dictionary<stringdouble> totalInfo = new Dictionary<stringdouble>();
 
            //TotalQty 집계
            foreach (DataRow row in dt.Rows)
            {
                string product = row["PRODUCT"].ToString();
                string year = row["YEAR"].ToString();
                int qty = (int)row["QTY"];
 
                if (totalInfo.ContainsKey(product) == false)
                    totalInfo.Add(product, 0);
 
                totalInfo[product] += qty;
            }
 
            Series series = new Series("Total", ViewType.Pie);
            chart.Series.Add(series);
 
            //Label값을 데이터의 값 그대로 나오도록 설정
            (series.Label.PointOptions as PiePointOptions).PercentOptions.ValueAsPercent = false;
            series.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.General;
 
            foreach (KeyValuePair<stringdouble> info in totalInfo)
            {
                string product = info.Key;
                double qty = info.Value;
 
                SeriesPoint point = new SeriesPoint(product, qty);
                series.Points.Add(point);
            }
 
            //ChartTitle 생성
            ChartTitle title = new ChartTitle();
            title.Text = viewType.ToString();
            chart.Titles.Add(title);
        }

 

[실행화면]

 

데이터이 값 그대로 Label에 표시되는 것을 확인할 수 있습니다.

 

 


=================================================================

 

[Legend 표시부분 변경]

 

이번에는 Legend부분을 Product 값 ( APPLE, BANANA, ORANGE ) 로 나오도록 설정해보겠습니다.

 

Series의 LegendTextPattern 의 값을 "{A}" 로 변경시켜줍니다.

아래 소스에서 빨간색으로 표시된 부분을 참조하시면 됩니다.

 

[소스코드]

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private void SetPieChartData(ChartControl chart, DataTable dt)
        {
            ViewType viewType = ViewType.Pie;
 
            Dictionary<stringdouble> totalInfo = new Dictionary<stringdouble>();
 
            //TotalQty 집계
            foreach (DataRow row in dt.Rows)
            {
                string product = row["PRODUCT"].ToString();
                string year = row["YEAR"].ToString();
                int qty = (int)row["QTY"];
 
                if (totalInfo.ContainsKey(product) == false)
                    totalInfo.Add(product, 0);
 
                totalInfo[product] += qty;
            }
 
            Series series = new Series("Total", ViewType.Pie);
            chart.Series.Add(series);
 
            //Label값을 데이터의 값 그대로 나오도록 설정
            (series.Label.PointOptions as PiePointOptions).PercentOptions.ValueAsPercent = false;
            series.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.General;
 
            //Legend 표시부분 변경
            series.LegendTextPattern = "{A}";
 
            foreach (KeyValuePair<stringdouble> info in totalInfo)
            {
                string product = info.Key;
                double qty = info.Value;
 
                SeriesPoint point = new SeriesPoint(product, qty);
                series.Points.Add(point);
            }
 
            //ChartTitle 생성
            ChartTitle title = new ChartTitle();
            title.Text = viewType.ToString();
            chart.Titles.Add(title);
        }

 

[실행화면]

 

아래처럼 PRODUCT 명으로 Legend가 표시되는 것을 확인 할 수 있습니다.

 

 

LegnedTextPattern에 대한 Pattern의 종류는 아래를 참조하시면 됩니다.

 

Pattern Description
{A} Displays a series point argument.
{V} Displays series point values.
{VP} Displays series point values as percentages (for a Pie (Donut) series and Full-Stacked series).
{S} Displays the name of the series.
{G} Displays the name of a stacked group.
{W} Displays the weight (for a Bubble series).
{V1} Displays the first value (for range series).
{V2} Displays the second value (for range series).
{VD} Displays the duration between the first and second data point values (for range series).
{HV} Displays the high value (for a Financial series).
{LV} Displays the low value (for a Financial series).
{OV} Displays the open value (for a Financial series).
{CV} Displays the close value (for a Financial series).

 

※ 출처 : https://documentation.devexpress.com/CoreLibraries/DevExpress.XtraCharts.SeriesBase.LegendTextPattern.property

 

 

=================================================================

 

[PieChart에서 특정 값에 대해 강조하기]

 

아래처럼 값이 가장 큰 데이터에 대해서 강조하는 부분을 설정하는 방법입니다.

 

 

 

PieSeriesView의 ExplodeMode의 값을 PieExplodeMode.MaxValue로 설정하시면 됩니다.

소스에서 빨간색으로 표시된 부분을 참조하시면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
private void SetPieChartData(ChartControl chart, DataTable dt)
        {
            ViewType viewType = ViewType.Pie;
 
            Dictionary<stringdouble> totalInfo = new Dictionary<stringdouble>();
 
            //TotalQty 집계
            foreach (DataRow row in dt.Rows)
            {
                string product = row["PRODUCT"].ToString();
                string year = row["YEAR"].ToString();
                int qty = (int)row["QTY"];
 
                if (totalInfo.ContainsKey(product) == false)
                    totalInfo.Add(product, 0);
 
                totalInfo[product] += qty;
            }
 
            Series series = new Series("Total", ViewType.Pie);
            chart.Series.Add(series);
 
            //Label값을 데이터의 값 그대로 나오도록 설정
            (series.Label.PointOptions as PiePointOptions).PercentOptions.ValueAsPercent = false;
            series.Label.PointOptions.ValueNumericOptions.Format = NumericFormat.General;
 
            //Legend 표시부분 변경
            series.LegendTextPattern = "{A}";
 
            //ExplodeMode 설정
            (series.View as PieSeriesView).ExplodeMode = PieExplodeMode.MaxValue;
 
            foreach (KeyValuePair<stringdouble> info in totalInfo)
            {
                string product = info.Key;
                double qty = info.Value;
 
                SeriesPoint point = new SeriesPoint(product, qty);
                series.Points.Add(point);
            }
 
            //ChartTitle 생성
            ChartTitle title = new ChartTitle();
            title.Text = viewType.ToString();
            chart.Titles.Add(title);
        }

 

MaxValue외에도 아래처럼 다른 속성들도 있으니 사용용도에 맞게 설정해서 사용하면 될 것 같습니다.

1
2
3
4
5
6
7
8
9
10
11
    [ResourceFinder(typeof(ResFinder), "PropertyNamesRes")]
    public enum PieExplodeMode
    {
        None = 0,
        All = 1,
        MinValue = 2,
        MaxValue = 3,
        UsePoints = 4,
        UseFilters = 5,
        Others = 6,
    }

 


반응형

댓글