GUI/PlotPanel.cs
author moel.mich
Sun, 09 Jun 2013 16:53:16 +0000
changeset 398 ac9b3b647906
parent 397 243b6d5afa7c
child 400 f4e2e3e69651
permissions -rw-r--r--
Small correction to the plot with multiple axes.
moel@158
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@395
     7
  Copyright (C) 2009-2013 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@1
    12
using System.Collections.Generic;
moel@1
    13
using System.Drawing;
moel@395
    14
using System.Linq;
moel@1
    15
using System.Windows.Forms;
moel@1
    16
using OpenHardwareMonitor.Hardware;
moel@395
    17
using OxyPlot;
moel@395
    18
using OxyPlot.Axes;
moel@395
    19
using OxyPlot.WindowsForms;
moel@395
    20
using OxyPlot.Series;
moel@395
    21
using OpenHardwareMonitor.Collections;
moel@1
    22
moel@1
    23
namespace OpenHardwareMonitor.GUI {
moel@158
    24
  public class PlotPanel : UserControl {
moel@1
    25
moel@326
    26
    private PersistentSettings settings;
moel@326
    27
moel@395
    28
    private readonly Plot plot;
moel@395
    29
    private readonly PlotModel model;
moel@395
    30
    private readonly TimeSpanAxis timeAxis = new TimeSpanAxis();
moel@395
    31
    private readonly SortedDictionary<SensorType, LinearAxis> axes =
moel@395
    32
      new SortedDictionary<SensorType, LinearAxis>();
moel@395
    33
moel@397
    34
    private UserOption stackedAxes;
moel@397
    35
moel@1
    36
    private DateTime now;
moel@326
    37
moel@326
    38
    public PlotPanel(PersistentSettings settings) {
moel@326
    39
      this.settings = settings;
moel@395
    40
      this.model = CreatePlotModel();
moel@326
    41
moel@395
    42
      this.plot = new Plot();
moel@395
    43
      this.plot.Dock = DockStyle.Fill;
moel@395
    44
      this.plot.Model = model;
moel@395
    45
      this.plot.BackColor = Color.White;
moel@397
    46
      this.plot.ContextMenu = CreateMenu();
moel@397
    47
moel@397
    48
      UpdateAxesPosition();
moel@1
    49
moel@395
    50
      this.SuspendLayout();
moel@395
    51
      this.Controls.Add(plot);
moel@395
    52
      this.ResumeLayout(true);
moel@1
    53
    }
moel@1
    54
moel@395
    55
    public void SetCurrentSettings() {
moel@395
    56
      settings.SetValue("plotPanel.MinTimeSpan", (float)timeAxis.ViewMinimum);
moel@395
    57
      settings.SetValue("plotPanel.MaxTimeSpan", (float)timeAxis.ViewMaximum);
moel@395
    58
moel@395
    59
      foreach (var axis in axes.Values) {
moel@395
    60
        settings.SetValue("plotPanel.Min" + axis.Key, (float)axis.ViewMinimum);
moel@395
    61
        settings.SetValue("plotPanel.Max" + axis.Key, (float)axis.ViewMaximum);
moel@395
    62
      }
moel@395
    63
    }
moel@395
    64
moel@397
    65
    private ContextMenu CreateMenu() {
moel@397
    66
      ContextMenu menu = new ContextMenu();
moel@397
    67
moel@397
    68
      MenuItem stackedAxesMenuItem = new MenuItem("Stacked Axes");
moel@397
    69
      stackedAxes = new UserOption("stackedAxes", true,
moel@397
    70
        stackedAxesMenuItem, settings);
moel@397
    71
      stackedAxes.Changed += (sender, e) => {
moel@397
    72
        UpdateAxesPosition();
moel@397
    73
        InvalidatePlot();
moel@397
    74
      };
moel@397
    75
      menu.MenuItems.Add(stackedAxesMenuItem);
moel@397
    76
moel@395
    77
      MenuItem timeWindow = new MenuItem("Time Window");
moel@395
    78
      MenuItem[] timeWindowMenuItems =
moel@395
    79
        { new MenuItem("Auto", 
moel@395
    80
            (s, e) => { timeAxis.Zoom(0, double.NaN); InvalidatePlot(); }),
moel@395
    81
          new MenuItem("5 min", 
moel@395
    82
            (s, e) => { timeAxis.Zoom(0, 5 * 60); InvalidatePlot(); }),
moel@395
    83
          new MenuItem("10 min", 
moel@395
    84
            (s, e) => { timeAxis.Zoom(0, 10 * 60); InvalidatePlot(); }),
moel@395
    85
          new MenuItem("20 min", 
moel@395
    86
            (s, e) => { timeAxis.Zoom(0, 20 * 60); InvalidatePlot(); }),
moel@395
    87
          new MenuItem("30 min", 
moel@395
    88
            (s, e) => { timeAxis.Zoom(0, 30 * 60); InvalidatePlot(); }),
moel@395
    89
          new MenuItem("45 min", 
moel@395
    90
            (s, e) => { timeAxis.Zoom(0, 45 * 60); InvalidatePlot(); }),
moel@395
    91
          new MenuItem("1 h", 
moel@395
    92
            (s, e) => { timeAxis.Zoom(0, 60 * 60); InvalidatePlot(); }),
moel@395
    93
          new MenuItem("1.5 h", 
moel@395
    94
            (s, e) => { timeAxis.Zoom(0, 1.5 * 60 * 60); InvalidatePlot(); }),
moel@395
    95
          new MenuItem("2 h", 
moel@395
    96
            (s, e) => { timeAxis.Zoom(0, 2 * 60 * 60); InvalidatePlot(); }),
moel@395
    97
          new MenuItem("3 h", 
moel@395
    98
            (s, e) => { timeAxis.Zoom(0, 3 * 60 * 60); InvalidatePlot(); }),
moel@395
    99
          new MenuItem("6 h", 
moel@395
   100
            (s, e) => { timeAxis.Zoom(0, 6 * 60 * 60); InvalidatePlot(); }),
moel@395
   101
          new MenuItem("12 h", 
moel@395
   102
            (s, e) => { timeAxis.Zoom(0, 12 * 60 * 60); InvalidatePlot(); }),
moel@395
   103
          new MenuItem("24 h", 
moel@395
   104
            (s, e) => { timeAxis.Zoom(0, 24 * 60 * 60); InvalidatePlot(); }) };
moel@326
   105
      foreach (MenuItem mi in timeWindowMenuItems)
moel@326
   106
        timeWindow.MenuItems.Add(mi);
moel@397
   107
      menu.MenuItems.Add(timeWindow);
moel@326
   108
moel@397
   109
      return menu;
moel@326
   110
    }
moel@326
   111
moel@395
   112
    private PlotModel CreatePlotModel() {
moel@1
   113
moel@395
   114
      timeAxis.Position = AxisPosition.Bottom;
moel@395
   115
      timeAxis.MajorGridlineStyle = LineStyle.Solid;
moel@395
   116
      timeAxis.MajorGridlineThickness = 1;
moel@395
   117
      timeAxis.MajorGridlineColor = OxyColor.FromRgb(192, 192, 192);
moel@395
   118
      timeAxis.MinorGridlineStyle = LineStyle.Solid;
moel@395
   119
      timeAxis.MinorGridlineThickness = 1;
moel@395
   120
      timeAxis.MinorGridlineColor = OxyColor.FromRgb(232, 232, 232);
moel@395
   121
      timeAxis.StartPosition = 1;
moel@395
   122
      timeAxis.EndPosition = 0;
moel@395
   123
      timeAxis.MinimumPadding = 0;
moel@395
   124
      timeAxis.MaximumPadding = 0;
moel@395
   125
      timeAxis.AbsoluteMinimum = 0;
moel@395
   126
      timeAxis.Minimum = 0;
moel@395
   127
      timeAxis.AbsoluteMaximum = 24 * 60 * 60;
moel@395
   128
      timeAxis.Zoom(
moel@395
   129
        settings.GetValue("plotPanel.MinTimeSpan", 0.0f),
moel@395
   130
        settings.GetValue("plotPanel.MaxTimeSpan", 10.0f * 60));
moel@395
   131
      timeAxis.StringFormat = "h:mm";
moel@395
   132
moel@395
   133
      var units = new Dictionary<SensorType, string>();
moel@395
   134
      units.Add(SensorType.Voltage, "V");
moel@395
   135
      units.Add(SensorType.Clock, "MHz");
moel@395
   136
      units.Add(SensorType.Temperature, "°C");
moel@395
   137
      units.Add(SensorType.Load, "%");
moel@395
   138
      units.Add(SensorType.Fan, "RPM");
moel@395
   139
      units.Add(SensorType.Flow, "L/h");
moel@395
   140
      units.Add(SensorType.Control, "%");
moel@395
   141
      units.Add(SensorType.Level, "%");
moel@395
   142
      units.Add(SensorType.Factor, "1");
moel@395
   143
      units.Add(SensorType.Power, "W");
moel@395
   144
      units.Add(SensorType.Data, "GB");
moel@395
   145
moel@395
   146
      foreach (SensorType type in Enum.GetValues(typeof(SensorType))) {
moel@395
   147
        var axis = new LinearAxis();
moel@395
   148
        axis.Position = AxisPosition.Left;
moel@395
   149
        axis.MajorGridlineStyle = LineStyle.Solid;
moel@395
   150
        axis.MajorGridlineThickness = 1;
moel@395
   151
        axis.MajorGridlineColor = timeAxis.MajorGridlineColor;
moel@395
   152
        axis.MinorGridlineStyle = LineStyle.Solid;
moel@395
   153
        axis.MinorGridlineThickness = 1;
moel@395
   154
        axis.MinorGridlineColor = timeAxis.MinorGridlineColor;
moel@397
   155
        axis.AxislineStyle = LineStyle.Solid;
moel@395
   156
        axis.Title = type.ToString();
moel@395
   157
        axis.Key = type.ToString();
moel@395
   158
moel@395
   159
        axis.Zoom(
moel@395
   160
          settings.GetValue("plotPanel.Min" + axis.Key, float.NaN),
moel@395
   161
          settings.GetValue("plotPanel.Max" + axis.Key, float.NaN));
moel@395
   162
moel@395
   163
        if (units.ContainsKey(type))
moel@395
   164
          axis.Unit = units[type];
moel@395
   165
        axes.Add(type, axis);
moel@1
   166
      }
moel@1
   167
moel@395
   168
      var model = new PlotModel();
moel@395
   169
      model.Axes.Add(timeAxis);
moel@395
   170
      foreach (var axis in axes.Values)
moel@395
   171
        model.Axes.Add(axis);
moel@395
   172
      model.PlotMargins = new OxyThickness(0);
moel@395
   173
      model.IsLegendVisible = false;
moel@158
   174
moel@395
   175
      return model;
moel@1
   176
    }
moel@1
   177
moel@158
   178
    public void SetSensors(List<ISensor> sensors,
moel@158
   179
      IDictionary<ISensor, Color> colors) {
moel@395
   180
      this.model.Series.Clear();
moel@395
   181
moel@395
   182
      ListSet<SensorType> types = new ListSet<SensorType>();
moel@395
   183
moel@395
   184
      foreach (ISensor sensor in sensors) {
moel@395
   185
        var series = new LineSeries();
moel@395
   186
        series.ItemsSource = sensor.Values.Select(value => new DataPoint {
moel@395
   187
          X = (now - value.Time).TotalSeconds, Y = value.Value
moel@395
   188
        });
moel@395
   189
        series.Color = colors[sensor].ToOxyColor();
moel@395
   190
        series.StrokeThickness = 1;
moel@395
   191
        series.YAxisKey = axes[sensor.SensorType].Key;
moel@395
   192
        series.Title = sensor.Hardware.Name + " " + sensor.Name;
moel@395
   193
        this.model.Series.Add(series);
moel@395
   194
moel@395
   195
        types.Add(sensor.SensorType);
moel@395
   196
      }
moel@395
   197
moel@395
   198
      foreach (var pair in axes.Reverse()) {
moel@395
   199
        var axis = pair.Value;
moel@395
   200
        var type = pair.Key;
moel@397
   201
        axis.IsAxisVisible = types.Contains(type);     
moel@397
   202
      } 
moel@397
   203
moel@397
   204
      UpdateAxesPosition();
moel@397
   205
      InvalidatePlot();
moel@397
   206
    }
moel@397
   207
moel@397
   208
    private void UpdateAxesPosition() {
moel@397
   209
      if (stackedAxes.Value) {
moel@397
   210
        var count = axes.Values.Count(axis => axis.IsAxisVisible);
moel@397
   211
        var start = 0.0;
moel@397
   212
        foreach (var pair in axes.Reverse()) {
moel@397
   213
          var axis = pair.Value;
moel@397
   214
          var type = pair.Key;
moel@397
   215
          axis.StartPosition = start;
moel@397
   216
          var delta = axis.IsAxisVisible ? 1.0 / count : 0;
moel@397
   217
          start += delta;
moel@397
   218
          axis.EndPosition = start;
moel@397
   219
          axis.PositionTier = 0;
moel@397
   220
          axis.MajorGridlineStyle = LineStyle.Solid;
moel@397
   221
          axis.MinorGridlineStyle = LineStyle.Solid;   
moel@397
   222
        }
moel@397
   223
      } else {
moel@397
   224
        var tier = 0;
moel@397
   225
        foreach (var pair in axes.Reverse()) {
moel@397
   226
          var axis = pair.Value;
moel@397
   227
          var type = pair.Key;
moel@397
   228
          axis.StartPosition = 0;
moel@397
   229
          axis.EndPosition = 1;
moel@398
   230
          axis.PositionTier = axis.IsAxisVisible ? tier : 0;
moel@397
   231
          if (axis.IsAxisVisible)
moel@397
   232
            tier++;
moel@397
   233
          axis.MajorGridlineStyle = LineStyle.None;
moel@397
   234
          axis.MinorGridlineStyle = LineStyle.None;          
moel@397
   235
        }
moel@395
   236
      }
moel@398
   237
moel@395
   238
    }
moel@395
   239
moel@395
   240
    public void InvalidatePlot() {
moel@395
   241
      this.now = DateTime.UtcNow;
moel@395
   242
      this.plot.InvalidatePlot(true);
moel@1
   243
    }
moel@1
   244
moel@1
   245
  }
moel@1
   246
}