/// <summary> /// Transform AxisData from linear axis to logarithmic axis /// </summary> /// <param name="ad">linear AxisData</param> /// <param name="auto">logarithmic autoscaled axisdata</param> /// <returns>logarithmic AxisData</returns> public static AxisData TransformLog(AxisData ad, AxisData auto) { //NOTE!!: ad uses linear axis, auto uses logarithmic axis //This tries to convert from linear axisdata to logarithmic axisdata //It should be noted that it primarily uses the ad axisdata for //the conversion. The auto axisdata is used to limit the ad axisdata in case of illegal values //If original(ad) is smaller than auto.Min or zero //it probably is an illegal value, therefore set it to the autoscaled one if (ad.Min <= 0 || ad.Min < Math.Pow(10, auto.Min)) { ad.Min = auto.Min; } else { ad.Min = Power(ad.Min); } //See above, removes illegal values from ad if (ad.Max <= 0 || ad.Max > Math.Pow(10, auto.Max)) { ad.Max = auto.Max; } else { ad.Max = Power(ad.Max) + 1; } ad.Step = 1; return(ad); }
/// <summary> /// Get AxisData (Min, Max, Step) from double[] array but do not autoscale axisdata /// </summary> /// <param name="f">double[] array</param> /// <returns>AxisData</returns> public static AxisData TransformNoScale(double[] f) { AxisData ad = new AxisData(double.MaxValue, double.MinValue, double.MinValue); for (int i = 0; i < f.Length; i++) { if (!double.IsInfinity(f[i]) && !double.IsNaN(f[i])) { if (f[i] < ad.Min) { ad.Min = f[i]; } if (f[i] > ad.Max) { ad.Max = f[i]; } } } if (ad.Max == double.MaxValue) { ad.Max = 10; } if (ad.Min == double.MinValue) { ad.Min = ad.Max - 10; } ad.Step = Math.Abs(ad.Max - ad.Min) / 5; return(ad); }
public static AxisData GetMaxMinLog(double[] f) { //Init axisdata AxisData ad = new AxisData(double.MaxValue, double.MinValue, double.MinValue); //Get max and min from data for (int i = 0; i < f.Length; i++) { if (!double.IsInfinity(f[i]) && !double.IsNaN(f[i]) && f[i] > 0) { if (f[i] < ad.Min) { ad.Min = f[i]; } if (f[i] > ad.Max) { ad.Max = f[i]; } } } //If max is not found set to 10 if (ad.Max == double.MinValue) { ad.Max = 10; } //if max is not found set to 1 if (ad.Min == double.MaxValue) { ad.Min = 1; } return(ad); }
/// <summary> /// Create a GraphMath object. Used to transform from "real" values to /// control points /// </summary> /// <param name="Size">Size of graphics object to draw on</param> /// <param name="X">AxisData for X axis</param> /// <param name="Y">AxisData for Y axis</param> public GraphMath(Size Size, AxisData X, AxisData Y) { this.Size = Size; this.Xd = X; this.Yd = Y; //Calculate scalefactors to convert from real points to screen points this.xScaleFactor = ((double)this.Size.Width) / (Xd.Max - Xd.Min); this.yScaleFactor = ((double)this.Size.Height) / (Yd.Max - Yd.Min); }
private void btnApply_Click(object sender, System.EventArgs e) { if (this.cbAuto.Checked) { cc.ChartDataList.RestoreOriginalAxis(); } else { AxisData ax = new AxisData(Double.Parse(tbXMin.Text), Double.Parse(tbXMax.Text), 1); AxisData ay = new AxisData(Double.Parse(tbYMin.Text), Double.Parse(tbYMax.Text), 1); cc.AxisRangeX = new AxisData(ax.Min, ax.Max, Math.Abs(ax.Max - ax.Min) / 5); cc.AxisRangeY = new AxisData(ay.Min, ay.Max, Math.Abs(ay.Max - ay.Min) / 5); } cc.Invalidate(); }
/// <summary> /// Get sensible max, min and step for logarithmic Y axis /// </summary> /// <param name="f"></param> /// <returns></returns> public static AxisData TransformLogY(double[][] f) { AxisData ad = GetMaxMinLog(f); return(TransformLogY(ad.Min, ad.Max)); }
/// <summary> /// Returns sensible max, min and step value for a certain max and min input /// </summary> /// <param name="ad">AxisData</param> /// <returns>AxisData</returns> public static AxisData AxisScale(AxisData ad) { //Don't do anything if the values are fubar if (double.IsNaN(ad.Max) || double.IsInfinity(ad.Max) || double.IsNaN(ad.Min) || double.IsInfinity(ad.Min)) { return(ad); } //Make sure that Max != Min if (ad.Max == ad.Min) { if (ad.Max == 0) { ad.Max += 0.5; ad.Min -= 0.5; } else { ad.Max += Math.Pow(10, Power(ad.Max)) * 0.5; ad.Min -= Math.Pow(10, Power(ad.Max)) * 0.5; } } //Get maximum power int t = MaxPower(ad.Max, ad.Min); //Get the 1eMaxPower double pow = Math.Pow(10, t); //Scale maximum up to closest mantisse double temp_min = pow * Math.Floor(ad.Min / pow); //Scale minimum down to closest mantisse double temp_max = pow * Math.Ceiling(ad.Max / pow); //If min and max is equal add 1 to max if (temp_min == temp_max) { temp_max += 1; } //Calculate the range of mantisse's double tmp = Math.Abs(temp_max / pow - temp_min / pow); //Try with step 1eMaxPower double temp_step = 1 * pow; //Scale step according to the range of mantisse's if (tmp < 2) { temp_step = 0.2 * pow; } else if (tmp < 5) { temp_step = 0.5 * pow; } else if (tmp > 10) { temp_step = 2 * pow; } //Set max, min, step and return ad.Max = temp_max; ad.Min = temp_min; ad.Step = temp_step; return(ad); }
public void Rescale() { //Get max and min value and compare too current; AxisData tarx; AxisData tary; arx = new AxisData(double.MaxValue, double.MinValue, 1); ary = new AxisData(double.MaxValue, double.MinValue, 1); oarx = new AxisData(double.MaxValue, double.MinValue, 1); oary = new AxisData(double.MaxValue, double.MinValue, 1); ChartData d; for (int i = 0; i < this.Length; i++) { d = this[i]; if (d.AxisRangeX.Max != 1e-31 && (d.AxisRangeX.Max != d.AxisRangeX.Min) && !AutoScale) { tarx = d.AxisRangeX; } else { switch (AxisTypeX) { case AxisType.LOG: tarx = GraphMath.GetMaxMinLog(d.X); break; case AxisType.LIN: tarx = GraphMath.TransformNoScale(d.X); break; default: tarx = GraphMath.TransformNoScale(d.X); break; } } if (d.AxisRangeY.Max != 1e-31 && (d.AxisRangeY.Max != d.AxisRangeY.Min) && !AutoScale) { tary = d.AxisRangeY; } else { switch (AxisTypeY) { case AxisType.LOG: tary = GraphMath.GetMaxMinLog(d.Y); break; case AxisType.LIN: tary = GraphMath.TransformNoScale(d.Y); break; default: tary = GraphMath.TransformNoScale(d.Y); break; } } if (arx.Max < tarx.Max) { arx.Max = tarx.Max; } if (arx.Min > tarx.Min) { arx.Min = tarx.Min; } if (ary.Max < tary.Max) { ary.Max = tary.Max; } if (ary.Min > tary.Min) { ary.Min = tary.Min; } } if (arx.Max == double.MinValue) { arx.Max = 10; } if (arx.Min == double.MaxValue) { arx.Min = arx.Max - 10; } if (ary.Max == double.MinValue) { ary.Max = 10; } if (ary.Min == double.MaxValue) { ary.Min = ary.Max - 10; } arx.Step = Math.Abs(arx.Max - arx.Min) / 5; ary.Step = Math.Abs(ary.Max - ary.Min) / 5; oarx = arx; oary = ary; }
public void RestoreOriginalAxis() { arx = oarx; ary = oary; }