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