Advertisement
Guest User

ProgressGraph.cs

a guest
Jun 1st, 2012
556
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Controls;
  6. using System.Windows.Media;
  7. using System.Windows.Shapes;
  8. using System.Windows.Controls.Primitives;
  9.  
  10. namespace Progress
  11. {
  12.     public class ProgressGraph : ContentControl
  13.     {
  14.         private Canvas canvas;
  15.         private Axis axis;
  16.         private Brush brush;
  17.         private Rectangle rectangle;
  18.         private Line line;
  19.  
  20.         /// <summary>
  21.         /// Initializes a new instance of the <see cref="ProgressGraph"/> class.
  22.         /// </summary>
  23.         public ProgressGraph()
  24.         {
  25.             // Set a default colour for the progress graph
  26.             Color color = Colors.LightGreen;
  27.             color.A = 150;
  28.  
  29.             this.brush = new SolidColorBrush(color);
  30.             this.axis = new Axis();
  31.             this.canvas = new Canvas();
  32.             this.canvas.ClipToBounds = true;
  33.             this.Content = this.canvas;
  34.  
  35.             Refresh();
  36.         }
  37.  
  38.         #region Properties
  39.  
  40.        
  41.  
  42.         #endregion
  43.  
  44.         /// <inheritdoc />
  45.         protected override void OnRenderSizeChanged(System.Windows.SizeChangedInfo sizeInfo)
  46.         {
  47.             base.OnRenderSizeChanged(sizeInfo);
  48.  
  49.             this.axis.Height = this.Height;
  50.             this.axis.Width = this.Width;
  51.  
  52.             this.Refresh();
  53.         }
  54.  
  55.         private void Refresh()
  56.         {
  57.             try
  58.             {
  59.                 this.canvas.Children.Clear();
  60.                 this.canvas.Children.Add(this.axis);
  61.  
  62.                 this.rectangle = new Rectangle();
  63.                 this.rectangle.Height = this.Height;
  64.                 this.rectangle.Width = this.Width;
  65.                 this.rectangle.Fill = this.brush;
  66.                 this.canvas.Children.Add(this.rectangle);
  67.  
  68.                 this.line = new Line();
  69.                 this.line.X1 = 0;
  70.                 this.line.X2 = this.Width;
  71.                 this.line.Y1 = 10;
  72.                 this.line.Y2 = 10;
  73.                 this.line.StrokeThickness = 1;
  74.                 this.line.SnapsToDevicePixels = true;
  75.                 this.line.Stroke = Brushes.Black;
  76.                 this.canvas.Children.Add(this.line);
  77.             }
  78.             catch (Exception) { }
  79.         }
  80.  
  81.         public Double Value
  82.         {
  83.             get
  84.             {
  85.                 return this.value;
  86.             }
  87.             set
  88.             {
  89. #warning Need to stop when we hit 100%
  90.                 this.value = value;
  91.  
  92.                 this.rectangle.Dispatcher.Invoke(new Action(() =>
  93.                 {
  94.                     this.rectangle.Width = (this.Width / 100) * value;
  95.                 }));
  96.             }
  97.         }
  98.         private Double value;
  99.  
  100.         public Double Rate
  101.         {
  102.             get { return this.rate; }
  103.             set
  104.             {
  105.                 this.rate = value;
  106.                 if (this.rate > this.maxRate)
  107.                     this.maxRate = this.rate;
  108.  
  109.                 this.Dispatcher.Invoke(new Action(() =>
  110.                 {
  111. #warning This doesn't correctly max out at 80% which is required to print some text. We'll also need to re-scale the graph :/
  112.                     Double rateValue = ((this.Height * 0.8) / this.maxRate) * this.rate;
  113.                     this.line.Y1 = rateValue;
  114.                     this.line.Y2 = rateValue;
  115.                 }));
  116.             }
  117.         }
  118.         private Double rate;
  119.         private Double maxRate;
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement