using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.ComponentModel; using System.Drawing; /* Copyright (c) 2008-2014 DI Zimmermann Stephan (stefan.zimmermann@tele2.at) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ namespace GraphLib { public class DataSource { public delegate String OnDrawXAxisLabelEvent(DataSource src, int idx); public delegate String OnDrawYAxisLabelEvent(DataSource src, float value); public OnDrawXAxisLabelEvent OnRenderXAxisLabel = null; public OnDrawYAxisLabelEvent OnRenderYAxisLabel = null; private PointF[] samples = null; private int length = 0; private String name = String.Empty; private int downSample = 1; private Color color = Color.Black; public float VisibleDataRange_X = 0; public float DY = 0; public float YD0 = -200; public float YD1 = 200; public float Cur_YD0 = -200; public float Cur_YD1 = 200; public float grid_distance_y = 200; // grid distance in units ( draw a horizontal line every 200 units ) public float off_Y = 0; public float grid_off_y = 0; public bool yFlip = true; public bool Active = true; public float XAutoScaleOffset = 100; public float CurGraphHeight = 1.0f; public float CurGraphWidth = 1.0f; private bool autoScaleY = false; private bool autoScaleX = false; public bool AutoScaleY { get { return autoScaleY; } set { autoScaleY = value; } } public bool AutoScaleX { get { return autoScaleX; } set { autoScaleX = value; } } public PointF[] Samples { get { return samples; } set { samples = value; length = samples.Length; } } public float XMin { get { float x_min = float.MaxValue; if (samples != null && samples.Length > 0) { foreach (PointF p in samples) if (p.X < x_min) x_min=p.X; } return x_min; } } public float XMax { get { float x_max = float.MinValue; if (samples != null && samples.Length > 0) { foreach (PointF p in samples) if (p.X > x_max) x_max = p.X; } return x_max; } } public float YMin { get { float y_min = float.MaxValue; if (samples != null && samples.Length > 0) { foreach (PointF p in samples) if (p.Y < y_min) y_min = p.Y; } return y_min; } } public float YMax { get { float y_max = float.MinValue; if (samples.Length > 0) { foreach (PointF p in samples) if (p.Y > y_max) y_max = p.Y; } return y_max; } } public void SetDisplayRangeY(float y_start, float y_end) { YD0 = y_start; YD1 = y_end; } public void SetGridDistanceY(float grid_dist_y_units) { grid_distance_y = grid_dist_y_units; } public void SetGridOriginY(float off_y) { grid_off_y = off_y; } /// /// Load Samples from a file. Returns loaded points count. /// /// File pathname /// Loaded points count public int LoadSamples(string pathName, out float yMin, out float yMax) { yMin = float.MaxValue; yMax = float.MinValue; try { Int64 size = new FileInfo(pathName).Length; if ((size % 8) != 0) return 0; int samplesCount = (int)(size / 8L) - 1; if (samplesCount <= 0) return 0; PointF[] newSamples = new PointF[samplesCount]; using (BinaryReader reader = new BinaryReader(File.Open(pathName, FileMode.Open))) { for (int i = 0; i < samplesCount; i++) { newSamples[i].X = reader.ReadSingle(); newSamples[i].Y = reader.ReadSingle(); } yMin = reader.ReadSingle(); yMax = reader.ReadSingle(); } Samples = newSamples; return samplesCount; } catch (Exception) { return 0; } } [Category("Properties")] // Take this out, and you will soon have problems with serialization; [DefaultValue(typeof(string), "")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public String Name { get { return name; } set { name = value; } } [Category("Properties")] // Take this out, and you will soon have problems with serialization; [DefaultValue(typeof(Color), "")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public Color GraphColor { get { return color; } set { color = value; } } [Category("Properties")] // Take this out, and you will soon have problems with serialization; [DefaultValue(typeof(int), "0")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public int Length { get { return length; } set { length = value; if (length != 0) { samples = new PointF[length]; } else { // length is 0 if (samples != null) { samples = null; } } } } [Category("Properties")] // Take this out, and you will soon have problems with serialization; [DefaultValue(typeof(int), "1")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] public int Downsampling { get { return downSample; } set { downSample = value; } } } }