KeiStory

728x90
반응형

TChart Legend 클릭 시 잘못된 Series Index 반환하는 버그 수정

 

이전 시간에 TChart Legend 클릭 시 선택한 Series 강조하는 방법에 대해서 알아봤습니다.

2024.02.15 - [코딩/C#] - TChart Legend 클릭 시 선택한 Series 강조하기

그런데 .NET6 버전부터는 버그가 있어 아래처럼 Legend 클릭에 클릭한 Series 가 아닌

엉뚱한 Series Index 를 반환하는 버그가 있었습니다.

아래쪽 Series 를 선택하면 엉뚱한 Series 가 강조되는 버그

그런데 이를 코딩으로 차트의 Legend 높이가 Series Count 등을 계산하여

클릭한 Series 가 강조되도록 하였습니다.

아래처럼 코딩하여 처리하면 됩니다.

// .NET6 버그 처리코드 주석 부분

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)
        {
            // .NET6 버그 처리코드
            double seriesHeight = (double)this.tChart.Legend.Height / (double)this.tChart.Series.Count;

            var result = this.tChart.Legend.Height - (this.tChart.Legend.Height - (e.Y - this.tChart.Legend.Top));

            int index = (int)((double)result / seriesHeight);

            // .NET Framework 인 경우
            //int index = this.tChart.Legend.Clicked(e.X, e.Y);

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

                    if (i != index)
                    {
                        // 선택되지 않은 항목 반?투명하게 처리
                        this.tChart[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;
                }
            }
        }
    }
}

이제 아래의 Series 를 선택해도 정상적으로 강조되는 걸 확인할 수 있습니다.

 

728x90

공유하기

facebook twitter kakaoTalk kakaostory naver band