GUI/PlotPanel.cs
changeset 1 361e324a0ed4
child 158 119693c3b7d1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/PlotPanel.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,258 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.ComponentModel;
    1.44 +using System.Drawing;
    1.45 +using System.Drawing.Drawing2D;
    1.46 +using System.Windows.Forms;
    1.47 +using OpenHardwareMonitor.Hardware;
    1.48 +
    1.49 +namespace OpenHardwareMonitor.GUI {
    1.50 +  public partial class PlotPanel : UserControl {
    1.51 +
    1.52 +    private DateTime now;
    1.53 +    private List<ISensor> clocks = new List<ISensor>();
    1.54 +    private List<ISensor> temperatures = new List<ISensor>();
    1.55 +    private List<ISensor> fans = new List<ISensor>();
    1.56 +    private IDictionary<ISensor, Color> colors;
    1.57 +
    1.58 +    private StringFormat centerlower;
    1.59 +    private StringFormat centerleft;
    1.60 +    private StringFormat lowerleft;
    1.61 +    private Brush lightBrush;
    1.62 +    private Pen lightPen;
    1.63 +
    1.64 +    public PlotPanel() {
    1.65 +      this.SetStyle(ControlStyles.DoubleBuffer |
    1.66 +        ControlStyles.UserPaint |
    1.67 +        ControlStyles.AllPaintingInWmPaint | 
    1.68 +        ControlStyles.ResizeRedraw, true);
    1.69 +      this.UpdateStyles();
    1.70 +
    1.71 +      InitializeComponent();
    1.72 +
    1.73 +      centerlower = new StringFormat();
    1.74 +      centerlower.Alignment = StringAlignment.Center;
    1.75 +      centerlower.LineAlignment = StringAlignment.Near;
    1.76 +
    1.77 +      centerleft = new StringFormat();
    1.78 +      centerleft.Alignment = StringAlignment.Far;
    1.79 +      centerleft.LineAlignment = StringAlignment.Center;
    1.80 +
    1.81 +      lowerleft = new StringFormat();
    1.82 +      lowerleft.Alignment = StringAlignment.Far;
    1.83 +      lowerleft.LineAlignment = StringAlignment.Near;
    1.84 +
    1.85 +      lightBrush = new SolidBrush(Color.FromArgb(245, 245, 245));
    1.86 +      lightPen = new Pen(Color.FromArgb(200, 200, 200));
    1.87 +    }
    1.88 +
    1.89 +    private List<float> GetTemperatureGrid() {
    1.90 +
    1.91 +      float? minTempNullable = null;
    1.92 +      float? maxTempNullable = null;
    1.93 +      foreach (ISensor sensor in temperatures) {
    1.94 +        IEnumerable<ISensorEntry> graph = sensor.Plot;
    1.95 +        foreach (ISensorEntry entry in graph) {
    1.96 +          if (!minTempNullable.HasValue || minTempNullable > entry.Value)
    1.97 +            minTempNullable = entry.Value;
    1.98 +          if (!maxTempNullable.HasValue || maxTempNullable < entry.Value)
    1.99 +            maxTempNullable = entry.Value;
   1.100 +        }
   1.101 +      }
   1.102 +      if (!minTempNullable.HasValue) {
   1.103 +        minTempNullable = 20;
   1.104 +        maxTempNullable = 30;
   1.105 +      }
   1.106 +
   1.107 +      float maxTemp = (float)Math.Ceiling(maxTempNullable.Value / 10) * 10;
   1.108 +      float minTemp = (float)Math.Floor(minTempNullable.Value / 10) * 10;
   1.109 +      if (maxTemp == minTemp) 
   1.110 +        maxTemp += 10;   
   1.111 +     
   1.112 +      int countTempMax = 4;
   1.113 +      float deltaTemp = maxTemp - minTemp;
   1.114 +      int countTemp = (int)Math.Round(deltaTemp / 2);
   1.115 +      if (countTemp > countTempMax)
   1.116 +        countTemp = (int)Math.Round(deltaTemp / 5);
   1.117 +      if (countTemp > countTempMax)
   1.118 +        countTemp = (int)Math.Round(deltaTemp / 10);
   1.119 +      if (countTemp > countTempMax)
   1.120 +        countTemp = (int)Math.Round(deltaTemp / 20);
   1.121 +
   1.122 +      List<float> grid = new List<float>(countTemp + 1);
   1.123 +      for (int i = 0; i <= countTemp; i++) {
   1.124 +        grid.Add(minTemp + i * deltaTemp / countTemp);
   1.125 +      }
   1.126 +      return grid;
   1.127 +    }
   1.128 +
   1.129 +    private List<float> GetTimeGrid() {
   1.130 +
   1.131 +      float maxTime = 5;      
   1.132 +      if (temperatures.Count > 0) {
   1.133 +        IEnumerator<ISensorEntry> enumerator = 
   1.134 +          temperatures[0].Plot.GetEnumerator();
   1.135 +        if (enumerator.MoveNext()) {
   1.136 +          maxTime = (float)(now - enumerator.Current.Time).TotalMinutes;
   1.137 +        }
   1.138 +      }
   1.139 +
   1.140 +      int countTime = 10;
   1.141 +      float deltaTime = 5;
   1.142 +      while (deltaTime + 1 < maxTime && deltaTime < 10)
   1.143 +        deltaTime += 1;
   1.144 +      while (deltaTime + 2 < maxTime && deltaTime < 30)
   1.145 +        deltaTime += 2;
   1.146 +      while (deltaTime + 5 < maxTime && deltaTime < 100)
   1.147 +        deltaTime += 5;
   1.148 +
   1.149 +      List<float> grid = new List<float>(countTime + 1);
   1.150 +      for (int i = 0; i <= countTime; i++) {
   1.151 +        grid.Add(i * deltaTime / countTime);
   1.152 +      }
   1.153 +      return grid;
   1.154 +    }
   1.155 +
   1.156 +    protected override void OnPaint(PaintEventArgs e) {
   1.157 +
   1.158 +      now = DateTime.Now - new TimeSpan(0, 0, 4);
   1.159 +
   1.160 +      List<float> timeGrid = GetTimeGrid();
   1.161 +      List<float> tempGrid = GetTemperatureGrid();
   1.162 +
   1.163 +      Graphics g = e.Graphics;
   1.164 +
   1.165 +      RectangleF r =
   1.166 +        new RectangleF(0, 0, Bounds.Width, Bounds.Height);
   1.167 +
   1.168 +      float ml = 40;
   1.169 +      float mr = 15;
   1.170 +      float x0 = r.X + ml;
   1.171 +      float w = r.Width - ml - mr;
   1.172 +
   1.173 +      float mt = 15;
   1.174 +      float mb = 28;
   1.175 +      float y0 = r.Y + mt;
   1.176 +      float h = r.Height - mt - mb;
   1.177 +
   1.178 +      float leftScaleSpace = 5;
   1.179 +      float bottomScaleSpace = 5;
   1.180 +      
   1.181 +      g.Clear(Color.White);
   1.182 +
   1.183 +      if (w > 0 && h > 0) {
   1.184 +        g.FillRectangle(lightBrush, x0, y0, w, h);
   1.185 +
   1.186 +        g.SmoothingMode = SmoothingMode.HighQuality;
   1.187 +        for (int i = 0; i < timeGrid.Count; i++) {
   1.188 +          float x = x0 + i * w / (timeGrid.Count - 1);
   1.189 +          g.DrawLine(lightPen, x, y0, x, y0 + h);
   1.190 +        }
   1.191 +
   1.192 +        for (int i = 0; i < tempGrid.Count; i++) {
   1.193 +          float y = y0 + i * h / (tempGrid.Count - 1);
   1.194 +          g.DrawLine(lightPen, x0, y, x0 + w, y);
   1.195 +        }
   1.196 +
   1.197 +        float deltaTemp = tempGrid[tempGrid.Count - 1] - tempGrid[0];
   1.198 +        float deltaTime = timeGrid[timeGrid.Count - 1];
   1.199 +        foreach (ISensor sensor in temperatures) {
   1.200 +          using (Pen pen = new Pen(colors[sensor])) {
   1.201 +            IEnumerable<ISensorEntry> graph = sensor.Plot;
   1.202 +            PointF last = new PointF();
   1.203 +            bool first = true;
   1.204 +            foreach (ISensorEntry entry in graph) {
   1.205 +              PointF point = new PointF(
   1.206 +                  x0 + w - w * (float)(now - entry.Time).TotalMinutes / deltaTime,
   1.207 +                  y0 + h - h * (entry.Value - tempGrid[0]) / deltaTemp);
   1.208 +              if (!first)
   1.209 +                g.DrawLine(pen, last, point);
   1.210 +              last = point;
   1.211 +              first = false;
   1.212 +            }
   1.213 +          }
   1.214 +        }
   1.215 +
   1.216 +        g.SmoothingMode = SmoothingMode.None;
   1.217 +        g.FillRectangle(Brushes.White, 0, 0, x0, r.Height);
   1.218 +        g.FillRectangle(Brushes.White, x0 + w + 1, 0, r.Width - x0 - w, 
   1.219 +          r.Height);
   1.220 +
   1.221 +        for (int i = 1; i < timeGrid.Count; i++) {
   1.222 +          float x = x0 + (timeGrid.Count - 1 - i) * w / (timeGrid.Count - 1);  
   1.223 +          g.DrawString(timeGrid[i].ToString(), Font, Brushes.Black, x, 
   1.224 +            y0 + h + bottomScaleSpace, centerlower);
   1.225 +        }
   1.226 +
   1.227 +        for (int i = 0; i < tempGrid.Count - 1; i++) {
   1.228 +          float y = y0 + (tempGrid.Count - 1 - i) * h / (tempGrid.Count - 1);
   1.229 +          g.DrawString(tempGrid[i].ToString(), Font, Brushes.Black, 
   1.230 +            x0 - leftScaleSpace, y, centerleft);
   1.231 +        }
   1.232 +
   1.233 +        g.SmoothingMode = SmoothingMode.HighQuality;
   1.234 +        g.DrawString("[°C]", Font, Brushes.Black, x0 - leftScaleSpace, y0,
   1.235 +          lowerleft);        
   1.236 +        g.DrawString("[min]", Font, Brushes.Black, x0 + w,
   1.237 +          y0 + h + bottomScaleSpace, lowerleft);        
   1.238 +      }
   1.239 +    }
   1.240 +
   1.241 +    public void SetSensors(List<ISensor> sensors, 
   1.242 +      IDictionary<ISensor, Color> colors) 
   1.243 +    {
   1.244 +      this.colors = colors;
   1.245 +      List<ISensor> clocks = new List<ISensor>();
   1.246 +      List<ISensor> temperatures = new List<ISensor>();
   1.247 +      List<ISensor> fans = new List<ISensor>();
   1.248 +      foreach (ISensor sensor in sensors)
   1.249 +        switch (sensor.SensorType) {
   1.250 +          case SensorType.Clock: clocks.Add(sensor); break;
   1.251 +          case SensorType.Temperature: temperatures.Add(sensor); break;
   1.252 +          case SensorType.Fan: fans.Add(sensor); break;
   1.253 +        }
   1.254 +      this.clocks = clocks;
   1.255 +      this.temperatures = temperatures;
   1.256 +      this.fans = fans;
   1.257 +      Invalidate();
   1.258 +    }
   1.259 +
   1.260 +  }
   1.261 +}