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 |
|
---|
20 | private Color color = Color.Blue;
|
---|
21 | private Font font = new Font("Arial", 8);
|
---|
22 |
|
---|
23 | public LegendShape() {
|
---|
24 | CreateLegend();
|
---|
25 | }
|
---|
26 |
|
---|
27 | public bool Row {
|
---|
28 | set { row = value; }
|
---|
29 | get { return row; }
|
---|
30 | }
|
---|
31 |
|
---|
32 | public bool Top {
|
---|
33 | set { top = value; }
|
---|
34 | get { return top; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void CreateLegend() {
|
---|
38 | ClearShapes();
|
---|
39 | double x = ClippingArea.X1;
|
---|
40 | double y = ClippingArea.Y2;
|
---|
41 | int numberOfItemsPerRow = 0;
|
---|
42 | int rowCounter = 0;
|
---|
43 | int rowsToDraw = 1;
|
---|
44 | if (row) {
|
---|
45 | numberOfItemsPerRow = (int)ClippingArea.Width/LegendItem.WIDTH;
|
---|
46 | int nrOfItems = legendItems.Count;
|
---|
47 | if (nrOfItems > numberOfItemsPerRow) {
|
---|
48 | int rest;
|
---|
49 | int value = Math.DivRem(nrOfItems, numberOfItemsPerRow, out rest);
|
---|
50 | rowsToDraw = value + rest;
|
---|
51 | }
|
---|
52 | if (!top)
|
---|
53 | y = 25*rowsToDraw;
|
---|
54 | }
|
---|
55 | foreach (LegendItem item in legendItems) {
|
---|
56 | if (!row) {
|
---|
57 | CreateColumn(item, y);
|
---|
58 | y -= 15;
|
---|
59 | } else {
|
---|
60 | if (rowCounter >= numberOfItemsPerRow) {
|
---|
61 | x = ClippingArea.X1;
|
---|
62 | y -= 25;
|
---|
63 | }
|
---|
64 | CreateRow(item, x, y);
|
---|
65 | x += LegendItem.WIDTH;
|
---|
66 | rowCounter++;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// draws the legend as a row at the top or bottom of the WorldShape
|
---|
73 | /// </summary>
|
---|
74 | /// <param name="item"></param>
|
---|
75 | /// <param name="x"></param>
|
---|
76 | /// <param name="y"></param>
|
---|
77 | private void CreateRow(LegendItem item, double x, double y) {
|
---|
78 | AddShape(new LineShape(x, y - 10, x + 20, y - 10, item.Color, item.Thickness, DrawingStyle.Solid));
|
---|
79 | AddShape(new TextShape(x + 25, y, item.Label, Font, Color));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /// <summary>
|
---|
83 | /// draws the legend as a column on the right or left side of the WorldShape
|
---|
84 | /// </summary>
|
---|
85 | /// <param name="item"></param>
|
---|
86 | /// <param name="y"></param>
|
---|
87 | private void CreateColumn(LegendItem item, double y) {
|
---|
88 | AddShape(new LineShape(10, y - 10, 30, y - 10, item.Color, item.Thickness, DrawingStyle.Solid));
|
---|
89 | AddShape(new TextShape(35, y, item.Label, Font, Color));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public void AddLegendItem(LegendItem item) {
|
---|
93 | legendItems.Add(item);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void RemoveLegendItem(LegendItem item) {
|
---|
97 | legendItems.Remove(item);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void ClearLegendItems() {
|
---|
101 | legendItems.Clear();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public Color Color {
|
---|
105 | get { return color; }
|
---|
106 | set {
|
---|
107 | color = value;
|
---|
108 | UpdateTextShapes();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | public Font Font {
|
---|
113 | get { return font; }
|
---|
114 | set {
|
---|
115 | font = value;
|
---|
116 | UpdateTextShapes();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | private void UpdateTextShapes() {
|
---|
121 | foreach (IShape shape in shapes) {
|
---|
122 | TextShape textShape = shape as TextShape;
|
---|
123 |
|
---|
124 | if (textShape != null) {
|
---|
125 | textShape.Font = font;
|
---|
126 | textShape.Color = color;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|