[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Drawing;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Charting {
|
---|
| 28 | public class Axis : AxisPrimitiveBase {
|
---|
| 29 | public Axis(IChart chart, PointD point, AxisType axisType)
|
---|
| 30 | : base(chart, point, axisType) {
|
---|
| 31 | }
|
---|
| 32 | public Axis(IChart chart, double x, double y, AxisType axisType)
|
---|
| 33 | : this(chart, new PointD(x, y), axisType) {
|
---|
| 34 | }
|
---|
| 35 | public Axis(IChart chart, PointD point, AxisType axisType, Pen pen, Brush brush)
|
---|
| 36 | : base(chart, point, axisType, pen, brush) {
|
---|
| 37 | }
|
---|
| 38 | public Axis(IChart chart, double x, double y, AxisType axisType, Pen pen, Brush brush)
|
---|
| 39 | : this(chart, new PointD(x, y), axisType, pen, brush) {
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public override void Draw(Graphics graphics) {
|
---|
| 43 | int pixelsPerInterval = 100;
|
---|
| 44 | Font axisValuesFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular, GraphicsUnit.Pixel);
|
---|
| 45 | Font axisLabelsFont = new Font(FontFamily.GenericSansSerif, 14, FontStyle.Regular, GraphicsUnit.Pixel);
|
---|
| 46 |
|
---|
| 47 | if ((AxisType & AxisType.Horizontal) == AxisType.Horizontal) {
|
---|
| 48 | int intervals = Chart.SizeInPixels.Width / pixelsPerInterval;
|
---|
| 49 |
|
---|
| 50 | if (intervals > 0) {
|
---|
| 51 | double step = (Chart.UpperRight.X - Chart.LowerLeft.X) / intervals;
|
---|
| 52 | step = Math.Pow(10, Math.Floor(Math.Log10(step)));
|
---|
| 53 | if ((Chart.UpperRight.X - Chart.LowerLeft.X) / (step * 5) > intervals)
|
---|
| 54 | step = step * 5;
|
---|
| 55 | else if ((Chart.UpperRight.X - Chart.LowerLeft.X) / (step * 2) > intervals)
|
---|
| 56 | step = step * 2;
|
---|
| 57 |
|
---|
| 58 | double x = Math.Floor(Chart.LowerLeft.X / step) * step;
|
---|
| 59 | PointD current = new PointD(x, Point.Y);
|
---|
| 60 | while (current.X <= Chart.UpperRight.X) {
|
---|
| 61 | Point p = Chart.TransformWorldToPixel(current);
|
---|
| 62 | if (ShowGrid)
|
---|
| 63 | graphics.DrawLine(Pens.LightGray, p.X, 0, p.X, Chart.SizeInPixels.Height);
|
---|
| 64 | graphics.DrawLine(Pen, p.X, p.Y - 3, p.X, p.Y + 3);
|
---|
| 65 | graphics.DrawString(current.X.ToString(),
|
---|
| 66 | axisValuesFont,
|
---|
| 67 | Brush,
|
---|
| 68 | p.X,
|
---|
| 69 | p.Y + 5,
|
---|
| 70 | StringFormat.GenericDefault);
|
---|
| 71 | current.X = current.X + step;
|
---|
| 72 | }
|
---|
| 73 | if ((HorizontalLabel != null) && (HorizontalLabel != "")) {
|
---|
| 74 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
| 75 | graphics.DrawString(HorizontalLabel,
|
---|
| 76 | axisLabelsFont,
|
---|
| 77 | Brush,
|
---|
| 78 | Chart.SizeInPixels.Width - 20,
|
---|
| 79 | p.Y + 20,
|
---|
| 80 | new StringFormat(StringFormatFlags.DirectionRightToLeft));
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | if ((AxisType & AxisType.Vertical) == AxisType.Vertical) {
|
---|
| 85 | int intervals = Chart.SizeInPixels.Height / pixelsPerInterval;
|
---|
| 86 |
|
---|
| 87 | if (intervals > 0) {
|
---|
| 88 | double step = (Chart.UpperRight.Y - Chart.LowerLeft.Y) / intervals;
|
---|
| 89 | step = Math.Pow(10, Math.Floor(Math.Log10(step)));
|
---|
| 90 | if ((Chart.UpperRight.Y - Chart.LowerLeft.Y) / (step * 5) > intervals)
|
---|
| 91 | step = step * 5;
|
---|
| 92 | else if ((Chart.UpperRight.Y - Chart.LowerLeft.Y) / (step * 2) > intervals)
|
---|
| 93 | step = step * 2;
|
---|
| 94 |
|
---|
| 95 | double y = Math.Floor(Chart.LowerLeft.Y / step) * step;
|
---|
| 96 | PointD current = new PointD(Point.X, y);
|
---|
| 97 | while (current.Y <= Chart.UpperRight.Y) {
|
---|
| 98 | Point p = Chart.TransformWorldToPixel(current);
|
---|
| 99 | if (ShowGrid)
|
---|
| 100 | graphics.DrawLine(Pens.LightGray, 0, p.Y, Chart.SizeInPixels.Width, p.Y);
|
---|
| 101 | graphics.DrawLine(Pen, p.X - 3, p.Y, p.X + 3, p.Y);
|
---|
| 102 | graphics.DrawString(current.Y.ToString(),
|
---|
| 103 | axisValuesFont,
|
---|
| 104 | Brush,
|
---|
| 105 | p.X + 5,
|
---|
| 106 | p.Y,
|
---|
| 107 | StringFormat.GenericDefault);
|
---|
| 108 | current.Y = current.Y + step;
|
---|
| 109 | }
|
---|
| 110 | if ((VerticalLabel != null) && (VerticalLabel != "")) {
|
---|
| 111 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
| 112 | graphics.DrawString(VerticalLabel,
|
---|
| 113 | axisLabelsFont,
|
---|
| 114 | Brush,
|
---|
| 115 | p.X - 20,
|
---|
| 116 | 20,
|
---|
| 117 | new StringFormat(StringFormatFlags.DirectionRightToLeft | StringFormatFlags.DirectionVertical));
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | if ((AxisType & AxisType.Horizontal) == AxisType.Horizontal) {
|
---|
| 122 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
| 123 | graphics.DrawLine(Pen, 0, p.Y, Chart.SizeInPixels.Width, p.Y);
|
---|
| 124 | }
|
---|
| 125 | if ((AxisType & AxisType.Vertical) == AxisType.Vertical) {
|
---|
| 126 | Point p = Chart.TransformWorldToPixel(Point);
|
---|
| 127 | graphics.DrawLine(Pen, p.X, 0, p.X, Chart.SizeInPixels.Height);
|
---|
| 128 | }
|
---|
| 129 | base.Draw(graphics);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|