Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/21/09 13:28:46 (15 years ago)
Author:
shofstad
Message:

Legend implementation updated with position setting (#407)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Visualization/Legend/LegendShape.cs

    r1346 r1388  
    1717    private bool top;
    1818
    19 
    2019    private Color color = Color.Blue;
    2120    private Font font = new Font("Arial", 8);
     
    3534    }
    3635
     36    private bool ExistsLegendItems() {
     37      return legendItems.Count > 0;
     38    }
     39   
     40    /// <summary>
     41    /// paints the legend on the canvas
     42    /// </summary>
    3743    public void CreateLegend() {
    38       ClearShapes();
    39       double x = ClippingArea.X1;
    40       double y = ClippingArea.Y2;
    41       int numberOfItemsPerRow = 0;
    42       int rowCounter = 0;
     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 -= 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        }
     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;
    4382      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);
     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 {
    5089          rowsToDraw = value + rest;
    5190        }
    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       }
     91      }
     92      return 25*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 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;
    69117    }
    70118
     
    76124    /// <param name="y">y axis to draw the item</param>
    77125    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));
     126      AddShape(new LineShape(x, y - 5, x + 20, y - 5, item.Color, item.Thickness, DrawingStyle.Solid));
    79127      AddShape(new TextShape(x + 25, y, item.Label, Font, Color));
    80128    }
     
    99147
    100148    /// <summary>
     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>
    101168    /// removes a legenditem from the items list
    102169    /// </summary>
Note: See TracChangeset for help on using the changeset viewer.