Free cookie consent management tool by TermsFeed Policy Generator

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

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

Legend implementation updated (#407)

File size: 6.3 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
19    private Color color = Color.Blue;
20    private Font font = new Font("Arial", 8);
21
[1195]22    public LegendShape() {
[1049]23      CreateLegend();
[866]24    }
25
[1342]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
[1388]36    private bool ExistsLegendItems() {
37      return legendItems.Count > 0;
38    }
39   
40    /// <summary>
41    /// paints the legend on the canvas
42    /// </summary>
[1049]43    public void CreateLegend() {
[1388]44      if (ExistsLegendItems()) {
45        ClearShapes();
46        double x = ClippingArea.X1;
47        double y = ClippingArea.Y2;
48
[1665]49        if (row && !top) {
50          y = GetOptimalBottomHeight();
[1342]51        }
[1665]52        int legendItemCounter = 1;
[1388]53        foreach (LegendItem item in legendItems) {
54          if (!row) {
55            CreateColumn(item, y);
[1461]56            y -= Font.Height;
[1388]57          } else {
[1665]58            if (IsNewRow(legendItemCounter)) {
[1388]59              x = ClippingArea.X1;
[1461]60              y -= Font.Height+15;
[1388]61            }
62            CreateRow(item, x, y);
63            x += GetLabelLengthInPixel(item);
[1665]64            legendItemCounter++;
[1388]65          }
66        }
[1342]67      }
[1388]68    }
69
[1665]70    private double GetOptimalBottomHeight() {
[1388]71      int rowsToDraw = 1;
[1665]72      for (int i = 0; i < legendItems.Count; i++) {
73        if (IsNewRow(i + 1)) {
74          rowsToDraw++;
[1342]75        }
[866]76      }
[1665]77      return (Font.Height + 12) * rowsToDraw;
[866]78    }
79
[1665]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
[1342]100    /// <summary>
[1388]101    /// returns the maximum number of items per row to paint
102    /// </summary>
[1665]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;
[1388]114    }
115
116    /// <summary>
117    /// returns the length of the current legenditem in pixel
118    /// </summary>
119    /// <param name="item"></param>
120    /// <returns></returns>
[1461]121    private int GetLabelLengthInPixel(LegendItem item) {
122      int dummy = (int)(item.Label.Length*Font.Size + 20);
[1388]123      if (dummy < LegendItem.WIDTH) {
124        return LegendItem.WIDTH;
125      }
126      return dummy;
127    }
128
129    /// <summary>
[1342]130    /// draws the legend as a row at the top or bottom of the WorldShape
131    /// </summary>
[1346]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>
[1342]135    private void CreateRow(LegendItem item, double x, double y) {
[1461]136      AddShape(new LineShape(x, y - (Font.Height / 2), x + 20, y - (Font.Height / 2), item.Color, item.Thickness, DrawingStyle.Solid));
[1342]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>
[1346]143    /// <param name="item">the legenditem to draw</param>
144    /// <param name="y">y axis to draw the item</param>
[1342]145    private void CreateColumn(LegendItem item, double y) {
[1461]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));
[1342]148    }
149
[1346]150    /// <summary>
151    /// adds a legenditem to the items list
152    /// </summary>
153    /// <param name="item">legenditem to add</param>
[873]154    public void AddLegendItem(LegendItem item) {
155      legendItems.Add(item);
156    }
157
[1346]158    /// <summary>
[1461]159    /// searches the longest label and returns it with factor of the the current font size
[1388]160    /// useful to set the width of the legend
161    /// </summary>
[1461]162    /// <returns>max label length with factor of the current font size</returns>
[1388]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        }
[1461]172        maxLabelLength = (int)(maxLabelLength*Font.Size);
[1388]173      }
174      return maxLabelLength < LegendItem.WIDTH ? LegendItem.WIDTH : maxLabelLength;
175    }
176
177    /// <summary>
[1346]178    /// removes a legenditem from the items list
179    /// </summary>
180    /// <param name="item">legenditem to remove</param>
[873]181    public void RemoveLegendItem(LegendItem item) {
182      legendItems.Remove(item);
183    }
184
[1346]185    /// <summary>
186    /// deletes the legenditem list
187    /// </summary>
[873]188    public void ClearLegendItems() {
189      legendItems.Clear();
190    }
[1337]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
[1346]208    /// <summary>
209    /// updates the font settings of the legend
210    /// </summary>
[1337]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    }
[866]221  }
[1342]222}
Note: See TracBrowser for help on using the repository browser.