How do I implement charts in WPF?

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

.xaml

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

.cs

C#:
//------------------------------
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...

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
I don't think WPF has any built in charting library. You'll need to either use a free open source one, or pay for one of the big guys like Telerik or DevExpress.

If all you need to do is plot some sine waves (based on your other thread), it shouldn't be that hard to use WPF's drawing directly.

 

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
It's surprising that Chart is not standard for WPF. It is trivial to use in Forms.

Is there sample code for using WPF's drawing directly for an xy plot?
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
The Charts in WinForms essentially just used the MS Chart Controls which are GDI based. WPF uses DirectDraw. They would have had to port code over into the WPF way of doing things.
 

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
I've been unable to find a WPF DirectDraw that creates an xy plot. Not sure where to start in xaml. DataGrid provides a grid, not xy area.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
See the link I provided. It has both declarative XAML, and programmatic C# ways of drawing a line. You are going to have to use the programmatic way.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
I've been there. As far as I can tell, those are predefined shapes. Where do I find a way to plot a sequence of numbers?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
As points? Or as lines between the points?
 

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
I have tried this:

for (int ii = 1; ii < 99; ii++) { myLine.X1 = (double)ii; myLine.Y1 = (double)ii; myLine.X2 = (double)ii + 1; myLine.Y2 = (double)ii + 1; gCVPlot.Children.Add(myLine); }

When ii = 2, I get a Exception.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Add a PolyLineSegment to the Path.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
I've been there. As far as I can tell, those are predefined shapes. Where do I find a way to plot a sequence of numbers?
See link that you said you've already visited. Go about 2/3rd of the way down the page where a PolyLineSegment is demonstrated.
 

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
That demo results in

C#:
pathFigure2    {M10,100L50,100 50,150Q200,200,300,100}  
polyLinePointArray    {System.Windows.Point[2]}
    [0]    {50,100}    System.Windows.Point
    [1]    {50,150}    System.Windows.Point
myPolyLineSegment.Points    {50,100 50,150}    System.Windows.Media.PointCollection
myPathGeometry    {M10,100L50,100 50,150Q200,200,300,100}

Which does not provide enough info to me to have a sequence of xy numbers. Am I missing some details?
 
Last edited by a moderator:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
That is the stream example. See the non-stream example above that.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Or read the documentation of the PolyLineSegment.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,762
Location
Chesapeake, VA
Programming Experience
10+
Specifically PolyLineSegment.Points:
Definition
Gets or sets the collection of Point structures that defines this PolyLineSegment object.
:
Remarks
A line is drawn from the end point of the previous segment or, if this is the first segment, from the StartPoint of the PathFigure to the first point in this collection. Lines are then drawn between each subsequent point specified in this collection.
 

bhs67

Active member
Joined
Oct 11, 2021
Messages
33
Programming Experience
10+
Every example I view is a predefined image. Is there an example that uses C# code (not xaml code) to create an xy plot?
 
Top Bottom