KeiStory

728x90
반응형

TChart Legend 클릭 시 선택한 Series 강조하기

 

TeeChart 에서 Legend 의 특정 Series 를 마우스로 클릭한 경우 클릭한 Series 를 강조하는 코드입니다.

using Steema.TeeChart.Styles;

namespace Win.TeeChartLegendClickTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // Set SampleData
            for (int i = 0; i < 10; i++)
            {
                Points points = new Points(this.tChart1.Chart);

                points.FillSampleValues(20);
            }

            this.tChart1.ClickLegend += TChart1_ClickLegend;
            this.tChart1.MouseClick += TChart1_MouseClick;
        }

        private void TChart1_ClickLegend(object? sender, MouseEventArgs e)
        {
            int index = this.tChart1.Legend.Clicked(e.X, e.Y);

            if (index > -1)
            {
                for (int i = 0; i < this.tChart1.Series.Count; i++)
                {
                    // 선택된 Legend 강조
                    this.tChart1[i].Transparency = 0;

                    if (i != index)
                    {
                        // 선택되지 않은 항목 반?투명하게 처리
                        this.tChart1[i].Transparency = 70;
                    }
                }
            }
        }

        private void TChart1_MouseClick(object? sender, MouseEventArgs e)
        {
            // Legend 영역이 아닌 경우 모두 원상태로 돌리기
            int index = this.tChart1.Legend.Clicked(e.X, e.Y);

            if (index > -1)
            {
                return;
            }
            else
            {
                for (int i = 0; i < this.tChart1.Series.Count; i++)
                {
                    this.tChart1[i].Transparency = 0;
                }
            }
        }
    }
}

 

ClickLegend 이벤트에서 Chart.Legend.Clicked(e.X, e.Y) 로 Series 의 Index 를 얻고

Index 에 해당하는 Series 를 제외한 나머지 Series 를 불투명 하게 하는 코드입니다.

Transparency 값을 조정하여 붙투명도를 조정하면됩니다.

 

그런데 .Net Framework 는 위 코드가 잘 동작합니다.

하지만 .NET6  에서는 버그가 있습니다.

.NET6 에서는 Legend 가 5개 이상 되는 경우 아래쪽에 있는 Series 를 클릭할 때

엉뚱한 Index 를 반환하여 다른 Series 를 강조하게되는 버그가 있습니다.

이 부분은 해결 방법을 찾았고 다음 포스팅에서 다뤄보겠습니다.

 

아래 gif 이미지를 보면 잘 되다가 아래로 가면 갈수록 엉뚱한걸 강조합니다.

TChart Legend 클릭 시 선택한 Series 강조하기 및 .NET6 버그

 

728x90
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band