Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1195 was 1194, checked in by mstoeger, 16 years ago

Implemented a continuous-, discrete- and string- label provider for the X/Y axis labels. (#433)

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