FastReport

Форк
0
199 строк · 7.3 Кб
1
using System;
2
using System.Drawing;
3
using System.Drawing.Drawing2D;
4
using System.ComponentModel;
5
using FastReport.Utils;
6

7
namespace FastReport.Gauge.Linear
8
{
9
    /// <summary>
10
    /// Represents a linear scale.
11
    /// </summary>
12
#if !DEBUG
13
    [DesignTimeVisible(false)]
14
#endif
15
    public class LinearScale : GaugeScale
16
    {
17
        #region Fields
18

19
        private float left;
20
        private float top;
21
        private float height;
22
        private float width;
23
        private int majorTicksNum;
24

25
        #endregion // Fields
26

27
        #region Properties
28

29

30

31
        #endregion // Properties
32

33
        #region Constructors
34

35
        /// <summary>
36
        /// Initializes a new instance of the <see cref="LinearScale"/> class.
37
        /// </summary>
38
        /// <param name="parent">The parent gauge object.</param>
39
        public LinearScale(GaugeObject parent) : base(parent)
40
        {
41
            MajorTicks = new ScaleTicks(10, 2, Color.Black);
42
            MinorTicks = new ScaleTicks(6, 1, Color.Black);
43
            majorTicksNum = 6;
44
        }
45

46
        #endregion // Constructors
47

48
        #region Private Methods
49

50
        private void DrawMajorTicksHorz(FRPaintEventArgs e)
51
        {
52
            IGraphics g = e.Graphics;
53
            Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
54
            Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
55
                Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
56
            float x = left;
57
            float y1 = top;
58
            float y2 = top + height;
59
            float step = width / (majorTicksNum - 1);
60
            int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
61
            Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
62
            string text = Parent.Minimum.ToString();
63
            float y3 = y1 - 0.4f * Units.Centimeters * e.ScaleY;
64
            if ((Parent as LinearGauge).Inverted)
65
            {
66
                y3 = y2 - g.MeasureString(text, Font).Height + 0.4f * Units.Centimeters * e.ScaleY;
67
            }
68
            for (int i = 0; i < majorTicksNum; i++)
69
            {
70
                g.DrawLine(pen, x, y1, x, y2);
71
                SizeF strSize = g.MeasureString(text, Font);
72
                g.DrawString(text, font, brush, x - strSize.Width / 2 * e.ScaleX / (DrawUtils.ScreenDpi / 96f), y3);
73
                text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
74
                x += step;
75
            }
76
            brush.Dispose();
77
        }
78

79
        private void DrawMinorTicksHorz(FRPaintEventArgs e)
80
        {
81
            IGraphics g = e.Graphics;
82
            Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
83
            float x = left;
84
            float y1 = top + height * 0.2f;
85
            float y2 = top + height - height * 0.2f;
86
            float step = width / (majorTicksNum - 1) / 4;
87
            for (int i = 0; i < majorTicksNum - 1; i++)
88
            {
89
                x += step;
90
                for (int j = 0; j < 3; j++)
91
                {
92
                    g.DrawLine(pen, x, y1, x, y2);
93
                    x += step;
94
                }
95
            }
96
        }
97

98
        private void DrawMajorTicksVert(FRPaintEventArgs e)
99
        {
100
            IGraphics g = e.Graphics;
101
            Pen pen = e.Cache.GetPen(MajorTicks.Color, MajorTicks.Width * e.ScaleX, DashStyle.Solid);
102
            Brush brush = TextFill.CreateBrush(new RectangleF(Parent.AbsLeft * e.ScaleX, Parent.AbsTop * e.ScaleY,
103
     Parent.Width * e.ScaleX, Parent.Height * e.ScaleY), e.ScaleX, e.ScaleY);
104
            float y = top + height;
105
            float x1 = left;
106
            float x2 = left + width;
107
            float step = height / (majorTicksNum - 1);
108
            int textStep = (int)((Parent.Maximum - Parent.Minimum) / (majorTicksNum - 1));
109
            Font font = e.Cache.GetFont(Font.FontFamily, Parent.IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi, Font.Style);
110
            string text = Parent.Minimum.ToString();
111
            for (int i = 0; i < majorTicksNum; i++)
112
            {
113
                g.DrawLine(pen, x1, y, x2, y);
114
                SizeF strSize = g.MeasureString(text, Font);
115
                float x3 = x1 - strSize.Width * e.ScaleX / (DrawUtils.ScreenDpi / 96f) - 0.04f * Units.Centimeters * e.ScaleX;
116
                if ((Parent as LinearGauge).Inverted)
117
                {
118
                    x3 = x2 + 0.04f * Units.Centimeters * e.ScaleX;
119
                }
120
                g.DrawString(text, font, brush, x3, y - strSize.Height / 2 * e.ScaleY / (DrawUtils.ScreenDpi / 96f));
121
                text = Convert.ToString(textStep * (i + 1) + Parent.Minimum);
122
                y -= step;
123
            }
124
            brush.Dispose();
125
        }
126

127
        private void DrawMinorTicksVert(FRPaintEventArgs e)
128
        {
129
            IGraphics g = e.Graphics;
130
            Pen pen = e.Cache.GetPen(MinorTicks.Color, MinorTicks.Width * e.ScaleX, DashStyle.Solid);
131
            float y = top + height;
132
            float x1 = left + width * 0.2f;
133
            float x2 = left + width - width * 0.2f;
134
            float step = height / (majorTicksNum - 1) / 4;
135
            for (int i = 0; i < majorTicksNum - 1; i++)
136
            {
137
                y -= step;
138
                for (int j = 0; j < 3; j++)
139
                {
140
                    g.DrawLine(pen, x1, y, x2, y);
141
                    y -= step;
142
                }
143
            }
144
        }
145

146
        #endregion // Private Methods
147

148
        #region Public Methods
149

150
        /// <inheritdoc/>
151
        public override void Assign(GaugeScale src)
152
        {
153
            base.Assign(src);
154

155
            LinearScale s = src as LinearScale;
156
            MajorTicks.Assign(s.MajorTicks);
157
            MinorTicks.Assign(s.MinorTicks);
158
        }
159

160
        /// <inheritdoc/>
161
        public override void Draw(FRPaintEventArgs e)
162
        {
163
            base.Draw(e);
164

165
            if (Parent.Vertical)
166
            {
167
                left = (Parent.AbsLeft + 0.7f * Units.Centimeters) * e.ScaleX;
168
                top = (Parent.AbsTop + 0.5f * Units.Centimeters) * e.ScaleY;
169
                height = (Parent.Height - 1.0f * Units.Centimeters) * e.ScaleY;
170
                width = (Parent.Width - 1.4f * Units.Centimeters) * e.ScaleX;
171

172
                DrawMajorTicksVert(e);
173
                DrawMinorTicksVert(e);
174
            }
175
            else
176
            {
177
                left = (Parent.AbsLeft + 0.5f * Units.Centimeters) * e.ScaleX;
178
                top = (Parent.AbsTop + 0.6f * Units.Centimeters) * e.ScaleY;
179
                height = (Parent.Height - 1.2f * Units.Centimeters) * e.ScaleY;
180
                width = (Parent.Width - 1.0f * Units.Centimeters) * e.ScaleX;
181

182
                DrawMajorTicksHorz(e);
183
                DrawMinorTicksHorz(e);
184
            }
185
        }
186

187
        /// <inheritdoc/>
188
        public override void Serialize(FRWriter writer, string prefix, GaugeScale diff)
189
        {
190
            base.Serialize(writer, prefix, diff);
191

192
            LinearScale dc = diff as LinearScale;
193
            MajorTicks.Serialize(writer, prefix + ".MajorTicks", dc.MajorTicks);
194
            MinorTicks.Serialize(writer, prefix + ".MinorTicks", dc.MinorTicks);
195
        }
196

197
        #endregion // Public Methods
198
    }
199
}
200

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.