Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1434 was 1388, checked in by shofstad, 16 years ago

Legend implementation updated with position setting (#407)

File size: 6.0 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
49        int numberOfItemsPerRow = 1;
50        if (row) {
51          numberOfItemsPerRow = GetNrOfItemsPerRow();
52          if (!top) {
53            y = GetHeight4Rows();
54          }
[1342]55        }
[1388]56        int rowCounter = 0;
57        foreach (LegendItem item in legendItems) {
58          if (!row) {
59            CreateColumn(item, y);
60            y -= 15;
61          } else {
62            if (rowCounter >= numberOfItemsPerRow) {
63              x = ClippingArea.X1;
64              y -= 25;
65              rowCounter = 0;
66            }
67            CreateRow(item, x, y);
68            x += GetLabelLengthInPixel(item);
69            rowCounter++;
70          }
71        }
[1342]72      }
[1388]73    }
74
75    /// <summary>
76    /// returns die optimal height for the top or bottom legend
77    /// </summary>
78    /// <returns></returns>
79    private int GetHeight4Rows() {
80      int numberOfItemsPerRow = GetNrOfItemsPerRow();
81      int nrOfItems = legendItems.Count;
82      int rowsToDraw = 1;
83      if (nrOfItems > numberOfItemsPerRow) {
84        int rest;
85        int value = Math.DivRem(nrOfItems, numberOfItemsPerRow, out rest);
86        if (rest > 1) {
87          rowsToDraw = rest;
[1342]88        } else {
[1388]89          rowsToDraw = value + rest;
[1342]90        }
[866]91      }
[1388]92      return 25*rowsToDraw;
[866]93    }
94
[1342]95    /// <summary>
[1388]96    /// returns the maximum number of items per row to paint
97    /// </summary>
98    /// <returns></returns>
99    private int GetNrOfItemsPerRow() {
100      int numberOfItemsPerRow = (int)ClippingArea.Width / GetMaxLabelLength();
101      // if the width of the clippingarea < maxLabelLength
102      if (numberOfItemsPerRow == 0) numberOfItemsPerRow = 1;
103      return numberOfItemsPerRow;
104    }
105
106    /// <summary>
107    /// returns the length of the current legenditem in pixel
108    /// </summary>
109    /// <param name="item"></param>
110    /// <returns></returns>
111    private static int GetLabelLengthInPixel(LegendItem item) {
112      int dummy = item.Label.Length*6 + 20;
113      if (dummy < LegendItem.WIDTH) {
114        return LegendItem.WIDTH;
115      }
116      return dummy;
117    }
118
119    /// <summary>
[1342]120    /// draws the legend as a row at the top or bottom of the WorldShape
121    /// </summary>
[1346]122    /// <param name="item">the legenditem to draw</param>
123    /// <param name="x">x axis to draw the item</param>
124    /// <param name="y">y axis to draw the item</param>
[1342]125    private void CreateRow(LegendItem item, double x, double y) {
[1388]126      AddShape(new LineShape(x, y - 5, x + 20, y - 5, item.Color, item.Thickness, DrawingStyle.Solid));
[1342]127      AddShape(new TextShape(x + 25, y, item.Label, Font, Color));
128    }
129
130    /// <summary>
131    /// draws the legend as a column on the right or left side of the WorldShape
132    /// </summary>
[1346]133    /// <param name="item">the legenditem to draw</param>
134    /// <param name="y">y axis to draw the item</param>
[1342]135    private void CreateColumn(LegendItem item, double y) {
136      AddShape(new LineShape(10, y - 10, 30, y - 10, item.Color, item.Thickness, DrawingStyle.Solid));
137      AddShape(new TextShape(35, y, item.Label, Font, Color));
138    }
139
[1346]140    /// <summary>
141    /// adds a legenditem to the items list
142    /// </summary>
143    /// <param name="item">legenditem to add</param>
[873]144    public void AddLegendItem(LegendItem item) {
145      legendItems.Add(item);
146    }
147
[1346]148    /// <summary>
[1388]149    /// searches the longest label and returns it with the factor 6
150    /// useful to set the width of the legend
151    /// </summary>
152    /// <returns>max label length with factor 6</returns>
153    public int GetMaxLabelLength() {
154      int maxLabelLength = 0;
155      if (ExistsLegendItems()) {
156        maxLabelLength = legendItems[0].Label.Length;
157        foreach (var item in legendItems) {
158          if (item.Label.Length > maxLabelLength) {
159            maxLabelLength = item.Label.Length;
160          }
161        }
162        maxLabelLength = maxLabelLength*6;
163      }
164      return maxLabelLength < LegendItem.WIDTH ? LegendItem.WIDTH : maxLabelLength;
165    }
166
167    /// <summary>
[1346]168    /// removes a legenditem from the items list
169    /// </summary>
170    /// <param name="item">legenditem to remove</param>
[873]171    public void RemoveLegendItem(LegendItem item) {
172      legendItems.Remove(item);
173    }
174
[1346]175    /// <summary>
176    /// deletes the legenditem list
177    /// </summary>
[873]178    public void ClearLegendItems() {
179      legendItems.Clear();
180    }
[1337]181
182    public Color Color {
183      get { return color; }
184      set {
185        color = value;
186        UpdateTextShapes();
187      }
188    }
189
190    public Font Font {
191      get { return font; }
192      set {
193        font = value;
194        UpdateTextShapes();
195      }
196    }
197
[1346]198    /// <summary>
199    /// updates the font settings of the legend
200    /// </summary>
[1337]201    private void UpdateTextShapes() {
202      foreach (IShape shape in shapes) {
203        TextShape textShape = shape as TextShape;
204
205        if (textShape != null) {
206          textShape.Font = font;
207          textShape.Color = color;
208        }
209      }
210    }
[866]211  }
[1342]212}
Note: See TracBrowser for help on using the repository browser.