// Simple plot with interpolated difference curve private void CreateGraph_DifferencePlot( ZedGraphControl z1 ) { GraphPane myPane = z1.GraphPane; // Generate the first data set PointPairList list1 = new PointPairList(); for ( int i = 0; i < 13; i++ ) { double x = i + 11.0; double y = 150.0 * ( 1.0 + Math.Sin( i * 0.3 ) ); list1.Add( x, y ); } // Generate a second data set that is unrelated to the first PointPairList list2 = new PointPairList(); for ( int i = 0; i < 15; i++ ) { double x = i * 1.2 + 10.0; double y = 250.0 * ( 1.0 + Math.Sin( x * 0.5 ) ); list2.Add( x, y ); } // Make sure the data are sorted and monotonically increasing list1.Sort(); list2.Sort(); // Get the lower and upper limit of the data // This code can throw an exception if either list is empty double xMin = Math.Min( list1[0].X, list2[0].X ); double xMax = Math.Max( list1[list1.Count - 1].X, list2[list2.Count - 1].X ); // Create a new list that will hold the difference points PointPairList diffList = new PointPairList(); // Select the number of points for the new difference curve // This is completely arbitrary, but more points will make it smoother in the // case of SplineInterpolation const int count = 50; // Loop for each data point to be created in the new PointPairList for ( int i=0; i<count; i++ ) { // Calculated X values are evenly spaced double x = xMin + (double) i * ( xMax - xMin ) / count; // Use spline interpolation to create the Y values for the new curve // Note that this allows extrapolation beyond the actual data available // A tension value of 0.5 is used, but anywhere between 0 and 1 is reasonable //double y = list1.InterpolateX( x ); double y1 = list1.InterpolateX( x ); double y2 = list2.SplineInterpolateX( x, 0.5 ); // Add the new Point to the list taking the difference between the Y values // If either value is Missing, it means that a point was extrapolated beyond // the available data, which is not allowed for SplineInterpolateX() // This won't happen with InterpolateX, since it allows extrapolation if ( y1 == PointPair.Missing || y2 == PointPair.Missing ) diffList.Add( x, PointPair.Missing, PointPair.Missing ); else diffList.Add( x, y1 - y2, (y1-y2) > 0 ? 1 : 0 ); } // Create the three curves -- two datasets, plus a difference curve LineItem diffCurve = myPane.AddCurve( "diff", diffList, Color.Red, SymbolType.None ); LineItem myCurve1 = myPane.AddCurve( "curve", list1, Color.Blue, SymbolType.Diamond ); LineItem myCurve2 = myPane.AddCurve( "curve 2", list2, Color.Green, SymbolType.Circle ); Color[] colors = { Color.Red, Color.Green }; diffCurve.Line.Fill = new Fill( colors, 90 ); diffCurve.Line.Fill.RangeMin = 0; diffCurve.Line.Fill.RangeMax = 1; diffCurve.Line.Fill.Type = FillType.GradientByZ; //diffCurve.Line.GradientFill = new Fill( colors, 90 ); //diffCurve.Line.GradientFill.RangeMin = -100; //diffCurve.Line.GradientFill.RangeMax = 200; //diffCurve.Line.IsOptimizedDraw = true; // Add some "pretty" stuff (optional) myCurve1.Symbol.Fill = new Fill( Color.White ); myCurve2.Symbol.Fill = new Fill( Color.White ); diffCurve.Line.Width = 2.0f; //diffCurve.Symbol.Fill = new Fill( Color.White ); myPane.Title.Text = "Interpolated Data Curve"; myPane.XAxis.Title.Text = "Period"; myPane.YAxis.Title.Text = "Response"; myPane.Legend.FontSpec.Size = 14; myPane.Fill = new Fill( Color.WhiteSmoke, Color.Lavender, 0F ); myPane.Chart.Fill = new Fill( Color.FromArgb( 255, 255, 245 ), Color.FromArgb( 255, 255, 190 ), 90F ); XDate xx = new XDate( 2007, 11, 9 ); XDate x2 = new XDate( 2007, 11, 9 ); XDate x3 = new XDate( 2007, 11, 9, 1, 1, 1 ); object junk = new object(); int i1 = xx.CompareTo( xx ); int i2 = xx.CompareTo( x2 ); int i3 = xx.CompareTo( x3 ); int i4 = x2.CompareTo( xx ); int i5 = x2.CompareTo( x3 ); int i6 = x3.CompareTo( x2 ); int i7 = x3.CompareTo( junk ); z1.IsAntiAlias = true; z1.AxisChange(); }
private void CreateGraph_LineColorGradient2( ZedGraphControl zgc ) { GraphPane myPane = zgc.GraphPane; PointPairList list = new PointPairList(); const int count = 30; for ( int i = 0; i < count; i++ ) { // Use an ordinary sine function to generate the curve double x = i + 1; double y = 5 * Math.Sin( (double) i * Math.PI * 3 / count ) + 5.0; // Set the Z value to be 2.0 if y is between 4 and 6, otherwise, it's 1.0 list.Add( x, y, y > 4 && y < 6 ? 2.0 : 1.0 ); } // Create a curve with symbols only LineItem myCurve = myPane.AddCurve( "Test Curve", list, Color.Red, SymbolType.Diamond ); myCurve.Line.IsVisible = false; myCurve.Symbol.Fill = new Fill( Color.White ); // Create a second curve, with lots of extra points const int count2 = 1000; PointPairList list2 = new PointPairList(); // Points are equal-spaced, across all the X range double dx = ( list[list.Count - 1].X - list[0].X ) / (double) count2; // Calculate the extra points values using linear interpolation for ( int i = 0; i <= count2; i++ ) { double x2 = list[0].X + dx * (double) i; double y2 = list.InterpolateX( x2 ); list2.Add( x2, y2, y2 > 4 && y2 < 6 ? 2.0 : 1.0 ); } // Add the second curve with no symbols LineItem myCurve2 = myPane.AddCurve( "Curve2", list2, Color.Blue, SymbolType.None ); // use a gradient fill to color the each line segment according to its Z value // Color will be blue for Z = 2, and red for Z = 1 Fill fill = new Fill( Color.Red, Color.Blue ); fill.RangeMin = 1; fill.RangeMax = 2; fill.Type = FillType.GradientByZ; myCurve2.Line.GradientFill = fill; // make the line fat myCurve2.Line.Width = 2.0f; // Create a band of green to show the highlighted region BoxObj box = new BoxObj( 0.0, 6.0, 1.0, 2.0, Color.Empty, Color.FromArgb( 150, Color.LightGreen ) ); // Use CoordType.XChartFractionYScale, so that Y values are regular scale values, and // X values are chart fraction, ranging from 0 to 1 box.Location.CoordinateFrame = CoordType.XChartFractionYScale; box.Fill = new Fill( Color.White, Color.FromArgb( 200, Color.LightGreen ), 45.0F ); box.ZOrder = ZOrder.F_BehindGrid; box.IsClippedToChartRect = true; myPane.GraphObjList.Add( box ); // Pretty it up myPane.Title.Text = "Line Segments Colored by Value\nExtra Points Are Interpolated"; myPane.Title.FontSpec.Size = 18; myPane.XAxis.Title.Text = "Time, seconds"; myPane.YAxis.Title.Text = "Potential, volts"; myPane.Legend.IsVisible = false; myPane.Fill = new Fill( Color.WhiteSmoke, Color.Lavender, 0F ); myPane.Chart.Fill = new Fill( Color.FromArgb( 255, 255, 245 ), Color.FromArgb( 255, 255, 190 ), 90F ); zgc.IsAntiAlias = true; zgc.AxisChange(); }
// Basic curve test - Linear Axis private void CreateGraph_BasicLinearSimple( ZedGraphControl z1 ) { z1.IsEnableSelection = true; Selection.Fill = new Fill( Color.Red ); Selection.Line.Color = Color.Red; Selection.Line.DashOn = 1; Selection.Line.DashOff = 1; Selection.Line.Width = 3; GraphPane myPane = z1.GraphPane; PointPairList list1 = new PointPairList(); PointPairList list2 = new PointPairList(); const int count = 10; for ( int i = 0; i < count; i++ ) { double x = i; double x2 = i + 0.4; double y = 300.0 * ( 1.0 + Math.Sin( x * 0.2 ) ); double y2 = 250.0 * ( 1.0 + Math.Sin( x2 * 0.2 ) ); list1.Add( x, y, i / 36.0 ); list2.Add( x2, y2, i / 36.0 ); } LineItem myCurve = myPane.AddCurve( "curve", list1, Color.Blue, SymbolType.Diamond ); LineItem myCurve2 = myPane.AddCurve( "curve 2", list2, Color.Green, SymbolType.Circle ); // Assume that list1 and list2 are existing PointPairLists for which you want the difference // Create a new list that will hold the difference points PointPairList diffList = new PointPairList(); // list1 will be the "Master" list that defines the actual X locations for the new list // list1 also sets the range for the new list foreach ( PointPair pt in list1 ) { // Calculate a Y value for list2 based on linear interpolation at the current X value // Note that you can also use list2.SplineInterpolateX() to use spline fitted data // with a tension value double y2 = list2.InterpolateX(pt.X); // Add the new Point to the list taking the difference between the Y values diffList.Add( pt.X, pt.Y - y2 ); } LineItem diffCurve = myPane.AddCurve( "diff", diffList, Color.Red, SymbolType.Square ); myCurve.Line.IsAntiAlias = true; /* // Just a placeholder for the LineObj that will show the cursor location LineObj cursorLine = new LineObj(Color.Black, 1.0, 0.0, 1.0, 1.0); // Location will be actual X Scale value, but will value will be chart fraction from 0 to 1 cursorLine.Location.CoordinateFrame = CoordType.XScaleYChartFraction; // Start with the cursor line hidden cursorLine.IsVisible = false; // Make the cursorline dashed cursorLine.Line.DashOff = 5; cursorLine.Line.DashOn = 1; cursorLine.Line.Style = DashStyle.Custom; // Add a tag so we can easily find the lineobj in MouseMove handler cursorLine.Tag = "cursorLine"; myPane.GraphObjList.Add(cursorLine); //myPane.XAxis.Scale.MajorStep = 1e-301; */ //BoxObj box = new BoxObj(); //myGraphObj.IsClippedToChartRect = true; z1.AxisChange(); // z1.MouseMoveEvent += new ZedGraphControl.ZedMouseEventHandler(BasicLinear_MouseMoveEvent); }