Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1222 was 1194, checked in by mstoeger, 15 years ago

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

File size: 1.6 KB
RevLine 
[1182]1using System;
[987]2using System.Collections.Generic;
3using System.Drawing;
[1194]4using HeuristicLab.Visualization.LabelProvider;
[987]5
[983]6namespace HeuristicLab.Visualization {
[1038]7  public class XAxis : WorldShape {
[1182]8    public const int PixelsPerInterval = 100;
[1194]9    private ILabelProvider labelProvider = new ContinuousLabelProvider("0.####");
[983]10
[1182]11    public ILabelProvider LabelProvider {
12      get { return labelProvider; }
13      set { labelProvider = value; }
[1038]14    }
[987]15
[1182]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      }
[987]32    }
33
34    public override void Draw(Graphics graphics, Rectangle viewport, RectangleD clippingArea) {
[1038]35      shapes.Clear();
[987]36
[1182]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;
[1038]42        shapes.Add(label);
[987]43      }
[1038]44
45      base.Draw(graphics, viewport, clippingArea);
[987]46    }
[983]47  }
48}
Note: See TracBrowser for help on using the repository browser.