1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 |
|
---|
5 | namespace 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 | int numberOfItemsPerRow = 1;
|
---|
50 | if (row) {
|
---|
51 | numberOfItemsPerRow = GetNrOfItemsPerRow();
|
---|
52 | if (!top) {
|
---|
53 | y = GetHeight4Rows();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | int rowCounter = 0;
|
---|
57 | foreach (LegendItem item in legendItems) {
|
---|
58 | if (!row) {
|
---|
59 | CreateColumn(item, y);
|
---|
60 | y -= Font.Height;
|
---|
61 | } else {
|
---|
62 | if (rowCounter >= numberOfItemsPerRow) {
|
---|
63 | x = ClippingArea.X1;
|
---|
64 | y -= Font.Height+15;
|
---|
65 | rowCounter = 0;
|
---|
66 | }
|
---|
67 | CreateRow(item, x, y);
|
---|
68 | x += GetLabelLengthInPixel(item);
|
---|
69 | rowCounter++;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
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;
|
---|
88 | } else {
|
---|
89 | rowsToDraw = value + rest;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return (Font.Height+12)*rowsToDraw;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
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 int GetLabelLengthInPixel(LegendItem item) {
|
---|
112 | int dummy = (int)(item.Label.Length*Font.Size + 20);
|
---|
113 | if (dummy < LegendItem.WIDTH) {
|
---|
114 | return LegendItem.WIDTH;
|
---|
115 | }
|
---|
116 | return dummy;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <summary>
|
---|
120 | /// draws the legend as a row at the top or bottom of the WorldShape
|
---|
121 | /// </summary>
|
---|
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>
|
---|
125 | private void CreateRow(LegendItem item, double x, double y) {
|
---|
126 | AddShape(new LineShape(x, y - (Font.Height / 2), x + 20, y - (Font.Height / 2), item.Color, item.Thickness, DrawingStyle.Solid));
|
---|
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>
|
---|
133 | /// <param name="item">the legenditem to draw</param>
|
---|
134 | /// <param name="y">y axis to draw the item</param>
|
---|
135 | private void CreateColumn(LegendItem item, double y) {
|
---|
136 | AddShape(new LineShape(10, y - (Font.Height / 2), 30, y - (Font.Height / 2), item.Color, item.Thickness, DrawingStyle.Solid));
|
---|
137 | AddShape(new TextShape(Font.Height+12, y, item.Label, Font, Color));
|
---|
138 | }
|
---|
139 |
|
---|
140 | /// <summary>
|
---|
141 | /// adds a legenditem to the items list
|
---|
142 | /// </summary>
|
---|
143 | /// <param name="item">legenditem to add</param>
|
---|
144 | public void AddLegendItem(LegendItem item) {
|
---|
145 | legendItems.Add(item);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /// <summary>
|
---|
149 | /// searches the longest label and returns it with factor of the the current font size
|
---|
150 | /// useful to set the width of the legend
|
---|
151 | /// </summary>
|
---|
152 | /// <returns>max label length with factor of the current font size</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 = (int)(maxLabelLength*Font.Size);
|
---|
163 | }
|
---|
164 | return maxLabelLength < LegendItem.WIDTH ? LegendItem.WIDTH : maxLabelLength;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /// <summary>
|
---|
168 | /// removes a legenditem from the items list
|
---|
169 | /// </summary>
|
---|
170 | /// <param name="item">legenditem to remove</param>
|
---|
171 | public void RemoveLegendItem(LegendItem item) {
|
---|
172 | legendItems.Remove(item);
|
---|
173 | }
|
---|
174 |
|
---|
175 | /// <summary>
|
---|
176 | /// deletes the legenditem list
|
---|
177 | /// </summary>
|
---|
178 | public void ClearLegendItems() {
|
---|
179 | legendItems.Clear();
|
---|
180 | }
|
---|
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 |
|
---|
198 | /// <summary>
|
---|
199 | /// updates the font settings of the legend
|
---|
200 | /// </summary>
|
---|
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 | }
|
---|
211 | }
|
---|
212 | }
|
---|