1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Visualization {
|
---|
6 | public class XAxis : WorldShape {
|
---|
7 | public const int PixelsPerInterval = 100;
|
---|
8 | private ILabelProvider labelProvider = new DefaultLabelProvider("0.##");
|
---|
9 |
|
---|
10 | public ILabelProvider LabelProvider {
|
---|
11 | get { return labelProvider; }
|
---|
12 | set { labelProvider = value; }
|
---|
13 | }
|
---|
14 |
|
---|
15 | public static IEnumerable<double> GetTicks(int pixelsPerInterval, int screenSize, double worldSize, double worldStart) {
|
---|
16 | int intervals = screenSize/pixelsPerInterval;
|
---|
17 |
|
---|
18 | if (intervals > 0) {
|
---|
19 | double step = worldSize/intervals;
|
---|
20 | step = Math.Pow(10, Math.Floor(Math.Log10(step)));
|
---|
21 | if (worldSize/(step*5) > intervals)
|
---|
22 | step = step*5;
|
---|
23 | else if (worldSize/(step*2) > intervals)
|
---|
24 | step = step*2;
|
---|
25 |
|
---|
26 | for (double x = Math.Floor(worldStart/step)*step;
|
---|
27 | x <= worldStart + worldSize;
|
---|
28 | x += step)
|
---|
29 | yield return x;
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public override void Draw(Graphics graphics, Rectangle viewport, RectangleD clippingArea) {
|
---|
34 | shapes.Clear();
|
---|
35 |
|
---|
36 | foreach (double x in GetTicks(PixelsPerInterval, viewport.Width, ClippingArea.Width, ClippingArea.X1)) {
|
---|
37 | TextShape label = new TextShape(x, ClippingArea.Height - 3,
|
---|
38 | labelProvider.GetLabel(x));
|
---|
39 | label.AnchorPositionX = AnchorPositionX.Middle;
|
---|
40 | label.AnchorPositionY = AnchorPositionY.Top;
|
---|
41 | shapes.Add(label);
|
---|
42 | }
|
---|
43 |
|
---|
44 | base.Draw(graphics, viewport, clippingArea);
|
---|
45 | }
|
---|
46 | }
|
---|
47 | } |
---|