File: ColorfulHistogram.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (330 lines) | stat: -rw-r--r-- 10,977 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using QuickRoute.BusinessEntities;
using QuickRoute.BusinessEntities.Numeric;
using System.Drawing.Drawing2D;
using System.Drawing.Text;

namespace QuickRoute.BusinessEntities
{
  public class ColorfulHistogram
  {
    private List<double> histogramData = new List<double>(new double[] { 1, 2, 3, 5, 9 });
    private ColorRange colorRange = new ColorRange(new Gradient(Color.Red, Color.Blue), 0, 10);
    private double barSpacing = 0;
    private Pen barBorderPen = new Pen(Color.FromArgb(128, Color.Black), 0.5F);
    private double startValue = 0;
    private double endValue = 10;
    private double binWidth = 2;
    private ScaleCreatorBase xAxisScaleCreator;
    private NumericConverter xAxisNumericConverter;
    private Pen borderPen = new Pen(Color.Black, 1F);
    private Pen gridPen = new Pen(Color.FromArgb(128, Color.Gray), 1F);
    private Color fontColor = Color.Black;
    private Color histogramBackColor = Color.White;
    private string xAxisCaption;
    private string yAxisCaption;
    private bool includeOutOfRangeValues;
    private Font font = new Font("MS Sans Serif", 8.25F);
    private Color backColor = SystemColors.Control;
    private bool cumulative;

    #region Public properties

    ~ColorfulHistogram()
    {
      barBorderPen.Dispose();
      borderPen.Dispose();
      gridPen.Dispose();
      font.Dispose();
    }

    [CategoryAttribute("Data"), DescriptionAttribute("The histogram data expressed as a list of doubles.")]
    public List<double> HistogramData
    {
      get { return histogramData; }
      set { histogramData = value; }
    }

    public ColorRange ColorRange
    {
      get { return colorRange; }
      set { colorRange = value; }
    }

    public double BarSpacing
    {
      get { return barSpacing; }
      set { barSpacing = value; }
    }

    public Pen BarBorderPen
    {
      get { return barBorderPen; }
      set { barBorderPen = value; }
    }

    public double StartValue
    {
      get { return startValue; }
      set { startValue = value; }
    }

    public double EndValue
    {
      get { return endValue; }
      set { endValue = value; }
    }

    public double BinWidth
    {
      get { return binWidth; }
      set { binWidth = value; }
    }

    public Pen BorderPen
    {
      get { return borderPen; }
      set { borderPen = value; }
    }

    public Pen GridPen
    {
      get { return gridPen; }
      set { gridPen = value; }
    }

    public Color FontColor
    {
      get { return fontColor; }
      set { fontColor = value; }
    }

    public Color HistogramBackColor
    {
      get { return histogramBackColor; }
      set { histogramBackColor = value; }
    }

    public string XAxisCaption
    {
      get { return xAxisCaption; }
      set { xAxisCaption = value; }
    }

    public string YAxisCaption
    {
      get { return yAxisCaption; }
      set { yAxisCaption = value; }
    }

    public bool IncludeOutOfRangeValues
    {
      get { return includeOutOfRangeValues; }
      set { includeOutOfRangeValues = value; }
    }

    public ScaleCreatorBase XAxisScaleCreator
    {
      get { return xAxisScaleCreator; }
      set { xAxisScaleCreator = value; }
    }

    public NumericConverter XAxisNumericConverter
    {
      get { return xAxisNumericConverter; }
      set { xAxisNumericConverter = value; }
    }

    public Font Font
    {
      get { return font; }
      set { font = value; }
    }

    public Color BackColor
    {
      get { return backColor; }
      set { backColor = value; }
    }

    public bool Cumulative
    {
      get { return cumulative; }
      set { cumulative = value; }
    }

    #endregion

    public void Draw(Graphics g, RectangleF drawingRectangle)
    {
      if (NoOfBins <= 0) return;
      int[] bins = CreateBins();
      double maxBarValue = 0;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.TextRenderingHint = TextRenderingHint.AntiAlias;

      // get max bar height
      for (int i = 0; i < NoOfBins; i++)
      {
        maxBarValue = Math.Max(maxBarValue, bins[i]);
      }
      if (maxBarValue == 0) return;
      // allow some extra space at top
      double spaceFactor = 1.1;
      double maxPercentage = Math.Min(1, spaceFactor * maxBarValue / histogramData.Count);
      DoubleScaleCreator yAxisScaleCreator = new DoubleScaleCreator(0, maxPercentage, 5, true);
      maxPercentage = yAxisScaleCreator.ScaleEndValue;

      // calculate max x axis label size
      SizeF maxXAxisLabelSize = SizeF.Empty;
      for (int i = 0; i < xAxisScaleCreator.NoOfMarkers; i++)
      {
        double value = xAxisScaleCreator.MarkerValue(i);
        string label = xAxisNumericConverter.ToString(value);
        SizeF labelSize = g.MeasureString(label, font);
        maxXAxisLabelSize.Width = Math.Max(maxXAxisLabelSize.Width, labelSize.Width);
        maxXAxisLabelSize.Height = Math.Max(maxXAxisLabelSize.Height, labelSize.Height);
      }

      // calculate max y axis label size
      SizeF maxYAxisLabelSize = SizeF.Empty;
      for (int i = 0; i < yAxisScaleCreator.NoOfMarkers; i++)
      {
        double value = yAxisScaleCreator.MarkerValue(i);
        string label = String.Format("{0:P0}", value);
        SizeF labelSize = g.MeasureString(label, font);
        maxYAxisLabelSize.Width = Math.Max(maxYAxisLabelSize.Width, labelSize.Width);
        maxYAxisLabelSize.Height = Math.Max(maxYAxisLabelSize.Height, labelSize.Height);
      }

      // calculate the graph rectangle
      SizeF xAxisCaptionSize = g.MeasureString(xAxisCaption, font);
      SizeF yAxisCaptionSize = g.MeasureString(yAxisCaption, font);
      float leftMargin = Math.Max(yAxisCaptionSize.Height + maxYAxisLabelSize.Width, maxXAxisLabelSize.Width / 2);
      RectangleF histogramRectangle = new RectangleF(
        (int)(drawingRectangle.Left + leftMargin),
        (int)(drawingRectangle.Top + maxYAxisLabelSize.Height / 2),
        Math.Max(1, (int)(drawingRectangle.Width - leftMargin - maxXAxisLabelSize.Width / 2 - 1)),
        Math.Max(1, (int)(drawingRectangle.Height - maxYAxisLabelSize.Height / 2 - maxXAxisLabelSize.Height - xAxisCaptionSize.Height - 1)));
      float barWidth = histogramRectangle.Width / (float)((endValue - startValue) / binWidth);


      // clear graphics
      g.Clear(BackColor);

      // draw the histogram background
      Brush b = new SolidBrush(histogramBackColor);
      g.FillRectangle(b, histogramRectangle);
      b.Dispose();

      // draw the axis labels
      // x axis
      Brush fontBrush = new SolidBrush(fontColor);
      for (int i = 0; i < xAxisScaleCreator.NoOfMarkers; i++)
      {
        double value = xAxisScaleCreator.MarkerValue(i);
        int x = (int)histogramRectangle.Left + (int)((float)(value - startValue) / (float)(endValue - startValue) * histogramRectangle.Width);
        string label = xAxisNumericConverter.ToString(value);
        SizeF labelSize = g.MeasureString(label, font);
        g.DrawString(label, font, fontBrush, new PointF(x - labelSize.Width / 2, histogramRectangle.Bottom));
        g.DrawLine(gridPen, new Point(x, (int)histogramRectangle.Top), new Point(x, (int)histogramRectangle.Bottom));
      }
      // y axis
      for (int i = 0; i < yAxisScaleCreator.NoOfMarkers; i++)
      {
        double value = yAxisScaleCreator.MarkerValue(i);
        int y = (int)histogramRectangle.Bottom - (int)((float)(value / maxPercentage) * histogramRectangle.Height);
        string label = String.Format("{0:P0}", value);
        SizeF labelSize = g.MeasureString(label, font);
        g.DrawString(label, font, fontBrush, new PointF(histogramRectangle.Left - labelSize.Width, y - maxYAxisLabelSize.Height / 2));
        g.DrawLine(gridPen, new Point((int)histogramRectangle.Left, y), new Point((int)histogramRectangle.Right, y));
      }



      // draw the axis captions
      // x axis
      PointF xAxisCaptionLocation = new PointF(
        histogramRectangle.Left + (histogramRectangle.Width - xAxisCaptionSize.Width) / 2,
        histogramRectangle.Bottom + xAxisCaptionSize.Height);
      g.DrawString(xAxisCaption, Font, fontBrush, xAxisCaptionLocation);
      // y axis, rotated 90 degrees
      PointF yAxisCaptionLocation = new PointF(
        histogramRectangle.Left - 3 * yAxisCaptionSize.Height,
        histogramRectangle.Bottom - (histogramRectangle.Height - yAxisCaptionSize.Width) / 2);
      g.ResetTransform();
      g.TranslateTransform(-yAxisCaptionLocation.X, -yAxisCaptionLocation.Y, MatrixOrder.Append);
      g.RotateTransform(-90F, MatrixOrder.Append);
      g.TranslateTransform(yAxisCaptionLocation.X, yAxisCaptionLocation.Y, MatrixOrder.Append);
      g.DrawString(yAxisCaption, Font, fontBrush, yAxisCaptionLocation);
      g.ResetTransform();


      // draw the bars
      g.SetClip(histogramRectangle);
      for (int i = 0; i < NoOfBins; i++)
      {
        float barHeight = bins[i] / (float)histogramData.Count / (float)maxPercentage * histogramRectangle.Height;
        RectangleF barRectangle = new RectangleF(
          histogramRectangle.Left + i * barWidth,
          histogramRectangle.Top + histogramRectangle.Height - barHeight,
          barWidth,
          barHeight
          );
        Color barColor = colorRange.GetColor(startValue + (i + 0.5) * binWidth);
        b = new SolidBrush(barColor);
        g.FillRectangle(b, barRectangle);
        b.Dispose();
        if (barBorderPen != null) g.DrawRectangle(barBorderPen, barRectangle.X, barRectangle.Y, barRectangle.Width, barRectangle.Height);
      }
      g.ResetClip();

      // draw the histogram border
      g.DrawRectangle(borderPen, histogramRectangle.X, histogramRectangle.Y, histogramRectangle.Width, histogramRectangle.Height);

      fontBrush.Dispose();
    }



    private int NoOfBins
    {
      get { return (int)Math.Ceiling((endValue - startValue) / binWidth); }
    }

    private int[] CreateBins()
    {
      int[] bins = new int[NoOfBins];

      foreach (double value in histogramData)
      {
        int binIndex = (int)((value - startValue) / binWidth);
        if (includeOutOfRangeValues || cumulative)
        {
          binIndex = Math.Max(Math.Min(binIndex, NoOfBins - 1), 0);
        }
        if (binIndex >= 0 && binIndex < NoOfBins)
        {
          bins[binIndex] += 1;
        }
      }

      if (cumulative)
      {
        int sum = 0;
        for (int i = 0; i < NoOfBins - 1; i++)
        {
          sum += bins[i];
          bins[i] = sum;
        }
      }

      return bins;
    }

  }
}