1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Visualization {
|
---|
26 | public class Axis : AxisPrimitiveBase {
|
---|
27 | protected const int PixelsPerInterval = 100;
|
---|
28 |
|
---|
29 | public Axis(IChart chart, PointD point, AxisType axisType)
|
---|
30 | : base(chart, point, axisType) {
|
---|
31 | }
|
---|
32 | public Axis(IChart chart, PointD point, AxisType axisType, Pen pen, Brush brush)
|
---|
33 | : base(chart, point, axisType, pen, brush) {
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override void Draw(Graphics graphics) {
|
---|
37 | var axisValuesFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Pixel);
|
---|
38 | var axisLabelsFont = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Regular, GraphicsUnit.Pixel);
|
---|
39 |
|
---|
40 | if ((AxisType & AxisType.Horizontal) == AxisType.Horizontal) {
|
---|
41 | int intervals = Chart.SizeInPixels.Width / PixelsPerInterval;
|
---|
42 |
|
---|
43 | if (intervals > 0) {
|
---|
44 | double step = (Chart.UpperRight.X - Chart.LowerLeft.X) / intervals;
|
---|
45 | step = Math.Pow(10, Math.Floor(Math.Log10(step)));
|
---|
46 | if ((Chart.UpperRight.X - Chart.LowerLeft.X) / (step * 5) > intervals)
|
---|
47 | step = step * 5;
|
---|
48 | else if ((Chart.UpperRight.X - Chart.LowerLeft.X) / (step * 2) > intervals)
|
---|
49 | step = step * 2;
|
---|
50 |
|
---|
51 | double x = Math.Floor(Chart.LowerLeft.X / step) * step;
|
---|
52 | var current = new PointD(x, Point.Y);
|
---|
53 | while (current.X <= Chart.UpperRight.X) {
|
---|
54 | Point p = Chart.TransformWorldToPixel(current);
|
---|
55 | if (ShowGrid)
|
---|
56 | graphics.DrawLine(Pens.LightGray, p.X, 0, p.X, Chart.SizeInPixels.Height);
|
---|
57 | graphics.DrawLine(Pen, p.X, p.Y - 3, p.X, p.Y + 3);
|
---|
58 | graphics.DrawString(current.X.ToString(),
|
---|
59 | axisValuesFont,
|
---|
60 | Brush,
|
---|
61 | p.X,
|
---|
62 | p.Y + 5,
|
---|
63 | StringFormat.GenericDefault);
|
---|
64 | current.X = current.X + step;
|
---|
65 | }
|
---|
66 | if ((HorizontalLabel != null) && (HorizontalLabel != "")) {
|
---|
67 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
68 | graphics.DrawString(HorizontalLabel,
|
---|
69 | axisLabelsFont,
|
---|
70 | Brush,
|
---|
71 | Chart.SizeInPixels.Width - 20,
|
---|
72 | p.Y + 20,
|
---|
73 | new StringFormat(StringFormatFlags.DirectionRightToLeft));
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | if ((AxisType & AxisType.Vertical) == AxisType.Vertical) {
|
---|
78 | int intervals = Chart.SizeInPixels.Height / PixelsPerInterval;
|
---|
79 |
|
---|
80 | if (intervals > 0) {
|
---|
81 | double step = (Chart.UpperRight.Y - Chart.LowerLeft.Y) / intervals;
|
---|
82 | step = Math.Pow(10, Math.Floor(Math.Log10(step)));
|
---|
83 | if ((Chart.UpperRight.Y - Chart.LowerLeft.Y) / (step * 5) > intervals)
|
---|
84 | step = step * 5;
|
---|
85 | else if ((Chart.UpperRight.Y - Chart.LowerLeft.Y) / (step * 2) > intervals)
|
---|
86 | step = step * 2;
|
---|
87 |
|
---|
88 | double y = Math.Floor(Chart.LowerLeft.Y / step) * step;
|
---|
89 | var current = new PointD(Point.X, y);
|
---|
90 | while (current.Y <= Chart.UpperRight.Y) {
|
---|
91 | Point p = Chart.TransformWorldToPixel(current);
|
---|
92 | if (ShowGrid)
|
---|
93 | graphics.DrawLine(Pens.LightGray, 0, p.Y, Chart.SizeInPixels.Width, p.Y);
|
---|
94 | graphics.DrawLine(Pen, p.X - 3, p.Y, p.X + 3, p.Y);
|
---|
95 | graphics.DrawString(current.Y.ToString(),
|
---|
96 | axisValuesFont,
|
---|
97 | Brush,
|
---|
98 | p.X + 5,
|
---|
99 | p.Y,
|
---|
100 | StringFormat.GenericDefault);
|
---|
101 | current.Y = current.Y + step;
|
---|
102 | }
|
---|
103 | if ((VerticalLabel != null) && (VerticalLabel != "")) {
|
---|
104 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
105 | graphics.DrawString(VerticalLabel,
|
---|
106 | axisLabelsFont,
|
---|
107 | Brush,
|
---|
108 | p.X - 20,
|
---|
109 | 20,
|
---|
110 | new StringFormat(StringFormatFlags.DirectionRightToLeft | StringFormatFlags.DirectionVertical));
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if ((AxisType & AxisType.Horizontal) == AxisType.Horizontal) {
|
---|
115 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
116 | graphics.DrawLine(Pen, 0, p.Y, Chart.SizeInPixels.Width, p.Y);
|
---|
117 | }
|
---|
118 | if ((AxisType & AxisType.Vertical) == AxisType.Vertical) {
|
---|
119 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
120 | graphics.DrawLine(Pen, p.X, 0, p.X, Chart.SizeInPixels.Height);
|
---|
121 | }
|
---|
122 | base.Draw(graphics);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|