Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Visualization/3.2/Legend/LegendShape.cs @ 6004

Last change on this file since 6004 was 1665, checked in by shofstad, 16 years ago

Legend implementation updated (#407)

File size: 6.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4
5namespace HeuristicLab.Visualization.Legend {
6  public enum LegendPosition {
7    Top,
8    Bottom,
9    Left,
10    Right
11  }
12
13  public class LegendShape : WorldShape {
14    private readonly IList<LegendItem> legendItems = new List<LegendItem>();
15    // legend draw default value: column
16    private bool row;
17    private bool top;
18
19    private Color color = Color.Blue;
20    private Font font = new Font("Arial", 8);
21
22    public LegendShape() {
23      CreateLegend();
24    }
25
26    public bool Row {
27      set { row = value; }
28      get { return row; }
29    }
30
31    public bool Top {
32      set { top = value; }
33      get { return top; }
34    }
35
36    private bool ExistsLegendItems() {
37      return legendItems.Count > 0;
38    }
39   
40    /// <summary>
41    /// paints the legend on the canvas
42    /// </summary>
43    public void CreateLegend() {
44      if (ExistsLegendItems()) {
45        ClearShapes();
46        double x = ClippingArea.X1;
47        double y = ClippingArea.Y2;
48
49        if (row && !top) {
50          y = GetOptimalBottomHeight();
51        }
52        int legendItemCounter = 1;
53        foreach (LegendItem item in legendItems) {
54          if (!row) {
55            CreateColumn(item, y);
56            y -= Font.Height;
57          } else {
58            if (IsNewRow(legendItemCounter)) {
59              x = ClippingArea.X1;
60              y -= Font.Height+15;
61            }
62            CreateRow(item, x, y);
63            x += GetLabelLengthInPixel(item);
64            legendItemCounter++;
65          }
66        }
67      }
68    }
69
70    private double GetOptimalBottomHeight() {
71      int rowsToDraw = 1;
72      for (int i = 0; i < legendItems.Count; i++) {
73        if (IsNewRow(i + 1)) {
74          rowsToDraw++;
75        }
76      }
77      return (Font.Height + 12) * rowsToDraw;
78    }
79
80    ///// <summary>
81    ///// returns the maximum number of items per row to paint
82    ///// </summary>
83    ///// <returns>number of items per row</returns>
84    //public int GetNrOfItemsPerRow() {
85    //  double sum = 0;
86    //  double caw = ClippingArea.Width;
87    //  int items = 0;
88    //  foreach (var item in legendItems) {
89    //    sum += GetLabelLengthInPixel(item);
90    //    if (sum < caw) {
91    //      items++;
92    //    }
93    //  }
94    //  if (items == 0) {
95    //    items++;
96    //  }
97    //  return items;
98    //}
99
100    /// <summary>
101    /// returns the maximum number of items per row to paint
102    /// </summary>
103    /// <returns>number of items per row</returns>
104    private bool IsNewRow(int toCurrentItem) {
105      double sum = 0;
106      double caw = ClippingArea.Width;
107      for (int i = 0; i < toCurrentItem; i++) {
108        sum += GetLabelLengthInPixel(legendItems[i]);
109        if (sum > caw) {
110          return true;
111        }
112      }
113      return false;
114    }
115
116    /// <summary>
117    /// returns the length of the current legenditem in pixel
118    /// </summary>
119    /// <param name="item"></param>
120    /// <returns></returns>
121    private int GetLabelLengthInPixel(LegendItem item) {
122      int dummy = (int)(item.Label.Length*Font.Size + 20);
123      if (dummy < LegendItem.WIDTH) {
124        return LegendItem.WIDTH;
125      }
126      return dummy;
127    }
128
129    /// <summary>
130    /// draws the legend as a row at the top or bottom of the WorldShape
131    /// </summary>
132    /// <param name="item">the legenditem to draw</param>
133    /// <param name="x">x axis to draw the item</param>
134    /// <param name="y">y axis to draw the item</param>
135    private void CreateRow(LegendItem item, double x, double y) {
136      AddShape(new LineShape(x, y - (Font.Height / 2), x + 20, y - (Font.Height / 2), item.Color, item.Thickness, DrawingStyle.Solid));
137      AddShape(new TextShape(x + 25, y, item.Label, Font, Color));
138    }
139
140    /// <summary>
141    /// draws the legend as a column on the right or left side of the WorldShape
142    /// </summary>
143    /// <param name="item">the legenditem to draw</param>
144    /// <param name="y">y axis to draw the item</param>
145    private void CreateColumn(LegendItem item, double y) {
146      AddShape(new LineShape(10, y - (Font.Height / 2), 30, y - (Font.Height / 2), item.Color, item.Thickness, DrawingStyle.Solid));
147      AddShape(new TextShape(Font.Height+12, y, item.Label, Font, Color));
148    }
149
150    /// <summary>
151    /// adds a legenditem to the items list
152    /// </summary>
153    /// <param name="item">legenditem to add</param>
154    public void AddLegendItem(LegendItem item) {
155      legendItems.Add(item);
156    }
157
158    /// <summary>
159    /// searches the longest label and returns it with factor of the the current font size
160    /// useful to set the width of the legend
161    /// </summary>
162    /// <returns>max label length with factor of the current font size</returns>
163    public int GetMaxLabelLength() {
164      int maxLabelLength = 0;
165      if (ExistsLegendItems()) {
166        maxLabelLength = legendItems[0].Label.Length;
167        foreach (var item in legendItems) {
168          if (item.Label.Length > maxLabelLength) {
169            maxLabelLength = item.Label.Length;
170          }
171        }
172        maxLabelLength = (int)(maxLabelLength*Font.Size);
173      }
174      return maxLabelLength < LegendItem.WIDTH ? LegendItem.WIDTH : maxLabelLength;
175    }
176
177    /// <summary>
178    /// removes a legenditem from the items list
179    /// </summary>
180    /// <param name="item">legenditem to remove</param>
181    public void RemoveLegendItem(LegendItem item) {
182      legendItems.Remove(item);
183    }
184
185    /// <summary>
186    /// deletes the legenditem list
187    /// </summary>
188    public void ClearLegendItems() {
189      legendItems.Clear();
190    }
191
192    public Color Color {
193      get { return color; }
194      set {
195        color = value;
196        UpdateTextShapes();
197      }
198    }
199
200    public Font Font {
201      get { return font; }
202      set {
203        font = value;
204        UpdateTextShapes();
205      }
206    }
207
208    /// <summary>
209    /// updates the font settings of the legend
210    /// </summary>
211    private void UpdateTextShapes() {
212      foreach (IShape shape in shapes) {
213        TextShape textShape = shape as TextShape;
214
215        if (textShape != null) {
216          textShape.Font = font;
217          textShape.Color = color;
218        }
219      }
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.