Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/XAxis.cs @ 1192

Last change on this file since 1192 was 1182, checked in by mstoeger, 16 years ago

Implemented X/Y-Axes and a Grid. (#433)

File size: 1.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.