■ Axis 클래스의 SetMinMax 메소드를 사용해 등각 축을 설정하는 방법을 보여준다.
▶ MainForm.cs
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System.Windows.Forms; using Steema.TeeChart; using Steema.TeeChart.Drawing; using Steema.TeeChart.Styles; using Steema.TeeChart.Themes; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 장치 정보 구하기 - GetDeviceCaps(deviceContextHandle, index) /// <summary> /// 장치 정보 구하기 /// </summary> /// <param name="deviceContextHandle">디바이스 컨텍스트 핸들</param> /// <param name="index">인덱스</param> /// <returns>장치 정보</returns> [DllImport("gdi32")] private static extern int GetDeviceCaps(IntPtr deviceContextHandle, int index); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 수평 크기 (밀리미터 단위) /// </summary> private const short HORZSIZE = 4; /// <summary> /// 수직 크기 (밀리미터 단위) /// </summary> private const short VERTSIZE = 6; /// <summary> /// 라인 /// </summary> private Line line; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Text = "Axis 클래스 : SetMinMax 메소드를 사용해 등각 축 설정하기"; Theme.ApplyChartTheme(typeof(ReportTheme), this.tChart.Chart); this.tChart.Panel.Pen = new ChartPen(Color.Black); this.tChart.Axes.Left.Grid.Style = DashStyle.Solid; this.tChart.Axes.Left.Grid.Color = Color.Black; this.tChart.Axes.Left.Increment = 50.0; this.tChart.Axes.Left.Labels.Separation = 0; this.tChart.Axes.Bottom.AxisPen.Visible = false; this.tChart.Axes.Bottom.Grid.Style = DashStyle.Solid; this.tChart.Axes.Bottom.Grid.Color = Color.Black; this.tChart.Axes.Bottom.Increment = 50.0; this.tChart.Axes.Bottom.Labels.Separation = 0; this.tChart.Axes.Right.Grid.Style = DashStyle.Solid; this.tChart.Axes.Right.Grid.Color = Color.Black; this.tChart.Axes.Right.Increment = 50.0; this.tChart.Axes.Right.Labels.Separation = 0; this.tChart.Axes.Right.Visible = true; this.tChart.Axes.Top.Visible = false; this.tChart.Legend.Visible = false; this.line = new Line(this.tChart.Chart); this.line.Pointer.Color = Color.White; this.line.Pointer.Visible = true; this.setIsometricAxisButton.Click += setIsometricAxisButton_Click; this.tChart.Zoomed += tChart_Zoomed; this.tChart.UndoneZoom += tChart_UndoneZoom; Random random = new Random(); for(int i = 1; i <= 100; i++) { this.line.Add(i, random.Next(1, 100)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 등각 축 설정하기 버튼 클릭시 처리하기 - setIsometricAxisButton_Click(sender, e) /// <summary> /// 등각 축 설정하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void setIsometricAxisButton_Click(object sender, EventArgs e) { SetIsometricAxis(this.line); } #endregion #region TChart 확대시 처리하기 - tChart_Zoomed(sender, e) /// <summary> /// TChart 확대시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tChart_Zoomed(object sender, EventArgs e) { SetIsometricAxis(this.line); } #endregion #region TChart 확대 취소시 처리하기 - tChart_UndoneZoom(sender, e) /// <summary> /// TChart 확대 취소시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tChart_UndoneZoom(object sender, EventArgs e) { SetIsometricAxis(this.line); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 등각 축 설정하기 - SetIsometricAxis(horizontalAxis, verticalAxis) /// <summary> /// 등각 축 설정하기 /// </summary> /// <param name="horizontalAxis">수평 축</param> /// <param name="verticalAxis">수직 축</param> private void SetIsometricAxis(Axis horizontalAxis, Axis verticalAxis) { if((verticalAxis.Chart.Width > 0) && (verticalAxis.Chart.Height > 0)) { double xRange = horizontalAxis.Maximum - horizontalAxis.Minimum; double yRange = verticalAxis.Maximum - verticalAxis.Minimum; double temporaryX = xRange / (double)verticalAxis.Chart.Width; double temporaryY = yRange / (double)verticalAxis.Chart.Height; Screen screen = Screen.FromControl(this); Graphics graphics = Graphics.FromHwnd(Handle); IntPtr deviceContextHandle = graphics.GetHdc(); double xyscreen = GetDeviceCaps(deviceContextHandle, HORZSIZE) * screen.Bounds.Height; xyscreen /= (double)GetDeviceCaps(deviceContextHandle, VERTSIZE) * screen.Bounds.Width; graphics.ReleaseHdc(deviceContextHandle); graphics.Dispose(); temporaryY *= xyscreen; double offset; if(temporaryX > temporaryY) { if(temporaryY != 0.0) { offset = 0.5 * (yRange * temporaryX / temporaryY - yRange); verticalAxis.SetMinMax(verticalAxis.Minimum - offset, verticalAxis.Maximum + offset); } } else if(temporaryX < temporaryY) { if(temporaryX != 0.0) { offset = 0.5 * (xRange * temporaryY / temporaryX - xRange); horizontalAxis.SetMinMax(horizontalAxis.Minimum - offset, horizontalAxis.Maximum+offset); } } } } #endregion #region 등각 축 설정하기 - SetIsometricAxis(series) /// <summary> /// 등각 축 설정하기 /// </summary> /// <param name="series">시리즈</param> private void SetIsometricAxis(Series series) { SetIsometricAxis(series.GetHorizAxis, series.GetVertAxis); } #endregion } } |