Home > OS >  How do I create xy plots with WPF APP (.NET Framework)?
How do I create xy plots with WPF APP (.NET Framework)?

Time:10-19

Visual Studio 2019 --- Windows Forms App (.NET Framework) C# ... Toolbox => Data => Charts provides the ability to create xy plots.

This does not exist in WPF APP (.NET Framework).

I've found multiple sites that demo a variety of shapes using .xaml and .cs. But none that demo a combo of both for an xy plot (not predefined shapes), where the .xaml defines the location, and the .cs provides the data.

How do I create xy plots with WPF APP (.NET Framework)?

CodePudding user response:

Using the template on this web site - stackoverflow.com/questions/33756255/create-a-math-plot-in-c-sharp-xam

.xaml

<Canvas x:Name="gCanvasPlot0" 
  HorizontalAlignment="Left" 
  VerticalAlignment="Top" 
  Margin="0,0,0,0" 
  Width="500"
  Height="150" />

.cs

//------------------------------
private void AddPlot()
{
  double dPI = Math.PI;
  Polyline poXY = new Polyline {Stroke = Brushes.Red};
  int iNumOfCycles = 3;
  double dDeltaX = 0.01;
  int iNumOfPoints0 = (int)(iNumOfCycles / dDeltaX);
  for (int ii = 0; ii < iNumOfPoints0; ii  ) {
    double dX = ii * dDeltaX;
    double dY = Math.Sin(2 * dPI * dX);
    poXY.Points.Add(CorrespondingPoint(new Point(dX, dY), iNumOfCycles));
  }
  gCanvasPlot0.Children.Add(poXY);
}//AddPlot
//------------------------------
//------------------------------
private Point CorrespondingPoint(Point pt, int iNumOfCycles)
{
  double dXmin = 0;
  double dXmax = iNumOfCycles;
  double dYmin = -1.1;
  double dYmax = 1.1;
  double dPlotWidth = dXmax - dXmin;
  double dPlotHeight = dYmax - dYmin;

  var poResult = new Point {
    X = (pt.X - dXmin) * gCanvasPlot0.Width / (dPlotWidth),
    Y = gCanvasPlot0.Height - (pt.Y - dYmin) * gCanvasPlot0.Height / 
          (dPlotHeight)
  };
  return poResult;
}//CorrespondingPoint
//------------------------------

This works.

  • Related