Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/Legend/LegendShape.cs @ 1368

Last change on this file since 1368 was 1346, checked in by shofstad, 15 years ago

added code comments (#406)

File size: 3.9 KB
RevLine 
[1342]1using System;
[873]2using System.Collections.Generic;
[1337]3using System.Drawing;
[866]4
[1233]5namespace HeuristicLab.Visualization.Legend {
[1342]6  public enum LegendPosition {
7    Top,
8    Bottom,
9    Left,
10    Right
11  }
12
[1195]13  public class LegendShape : WorldShape {
[873]14    private readonly IList<LegendItem> legendItems = new List<LegendItem>();
[1342]15    // legend draw default value: column
16    private bool row;
17    private bool top;
[1337]18
[1342]19
[1337]20    private Color color = Color.Blue;
21    private Font font = new Font("Arial", 8);
22
[1195]23    public LegendShape() {
[1049]24      CreateLegend();
[866]25    }
26
[1342]27    public bool Row {
28      set { row = value; }
29      get { return row; }
30    }
31
32    public bool Top {
33      set { top = value; }
34      get { return top; }
35    }
36
[1049]37    public void CreateLegend() {
[1240]38      ClearShapes();
[1342]39      double x = ClippingArea.X1;
[1232]40      double y = ClippingArea.Y2;
[1342]41      int numberOfItemsPerRow = 0;
42      int rowCounter = 0;
43      int rowsToDraw = 1;
44      if (row) {
45        numberOfItemsPerRow = (int)ClippingArea.Width/LegendItem.WIDTH;
46        int nrOfItems = legendItems.Count;
47        if (nrOfItems > numberOfItemsPerRow) {
48          int rest;
49          int value = Math.DivRem(nrOfItems, numberOfItemsPerRow, out rest);
50          rowsToDraw = value + rest;
51        }
52        if (!top)
53          y = 25*rowsToDraw;
54      }
[1049]55      foreach (LegendItem item in legendItems) {
[1342]56        if (!row) {
57          CreateColumn(item, y);
58          y -= 15;
59        } else {
60          if (rowCounter >= numberOfItemsPerRow) {
61            x = ClippingArea.X1;
62            y -= 25;
63          }
64          CreateRow(item, x, y);
65          x += LegendItem.WIDTH;
66          rowCounter++;
67        }
[866]68      }
69    }
70
[1342]71    /// <summary>
72    /// draws the legend as a row at the top or bottom of the WorldShape
73    /// </summary>
[1346]74    /// <param name="item">the legenditem to draw</param>
75    /// <param name="x">x axis to draw the item</param>
76    /// <param name="y">y axis to draw the item</param>
[1342]77    private void CreateRow(LegendItem item, double x, double y) {
78      AddShape(new LineShape(x, y - 10, x + 20, y - 10, item.Color, item.Thickness, DrawingStyle.Solid));
79      AddShape(new TextShape(x + 25, y, item.Label, Font, Color));
80    }
81
82    /// <summary>
83    /// draws the legend as a column on the right or left side of the WorldShape
84    /// </summary>
[1346]85    /// <param name="item">the legenditem to draw</param>
86    /// <param name="y">y axis to draw the item</param>
[1342]87    private void CreateColumn(LegendItem item, double y) {
88      AddShape(new LineShape(10, y - 10, 30, y - 10, item.Color, item.Thickness, DrawingStyle.Solid));
89      AddShape(new TextShape(35, y, item.Label, Font, Color));
90    }
91
[1346]92    /// <summary>
93    /// adds a legenditem to the items list
94    /// </summary>
95    /// <param name="item">legenditem to add</param>
[873]96    public void AddLegendItem(LegendItem item) {
97      legendItems.Add(item);
98    }
99
[1346]100    /// <summary>
101    /// removes a legenditem from the items list
102    /// </summary>
103    /// <param name="item">legenditem to remove</param>
[873]104    public void RemoveLegendItem(LegendItem item) {
105      legendItems.Remove(item);
106    }
107
[1346]108    /// <summary>
109    /// deletes the legenditem list
110    /// </summary>
[873]111    public void ClearLegendItems() {
112      legendItems.Clear();
113    }
[1337]114
115    public Color Color {
116      get { return color; }
117      set {
118        color = value;
119        UpdateTextShapes();
120      }
121    }
122
123    public Font Font {
124      get { return font; }
125      set {
126        font = value;
127        UpdateTextShapes();
128      }
129    }
130
[1346]131    /// <summary>
132    /// updates the font settings of the legend
133    /// </summary>
[1337]134    private void UpdateTextShapes() {
135      foreach (IShape shape in shapes) {
136        TextShape textShape = shape as TextShape;
137
138        if (textShape != null) {
139          textShape.Font = font;
140          textShape.Color = color;
141        }
142      }
143    }
[866]144  }
[1342]145}
Note: See TracBrowser for help on using the repository browser.