There will be data from the com port. And it will be necessary to show these data in the form of a graph, which will change every (say 10 seconds), as new data arrive. Now the question is what is better to use for drawing graphs on wpf? And secondly, I used zedgraph in winforms, is it possible to use it with wpf, or better not, if so, you can use a small example, I can not find anything on this topic on the Internet.
- There was the same problem, I did not find anything - I had to write my own control for this. There are a lot of beautiful libraries for WPF, but they are paid: (And there is nothing standard about it. You can write your own control which will have the "update data" method and use it - Vladimir Paliukhovich
|
2 answers
Why so hard? Quite straight arms and Polyline .
Example: this is the XAML
<Window x:Class="SimpleGraph.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Function Graph" Height="350" Width="525"> <Grid> <Polyline Name="Graph" Stroke="Blue" StrokeThickness="1"/> </Grid> </Window> and code-behind
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); SizeChanged += UpdateGraph; } double func(double x) => // should be injected from VM Math.Sin(x); double minX = -2 * Math.PI; // should be injected from VM double maxX = 2 * Math.PI; // should be injected from VM double minY = -1.5; // should be injected from VM double maxY = 1.5; // should be injected from VM void UpdateGraph(object sender, SizeChangedEventArgs e) { var pixelWidth = Graph.ActualWidth; var pixelHeight = Graph.ActualHeight; PointCollection points = new PointCollection((int)pixelWidth + 1); for (int pixelX = 0; pixelX < pixelWidth; pixelX++) { var x = MapFromPixel(pixelX, pixelWidth, minX, maxX); var y = func(x); var pixelY = pixelHeight - MapToPixel(y, minY, maxY, pixelHeight); points.Add(new Point(pixelX, pixelY)); } Graph.Points = points; } double MapFromPixel(double pixelV, double pixelMax, double minV, double maxV) => minV + (pixelV / pixelMax) * (maxX - minX); double MapToPixel(double v, double minV, double maxV, double pixelMax) => (v - minV) / (maxV - minV) * pixelMax; } gives the following schedule:
- And the grid? Just lines to outline? - MaximK
- @MaximK: Well, yes. And arrows. In any case, third-party controls do the same thing (only to set them up can be more difficult than writing manually). - VladD
- double func (double x) => Math.Sin (x); I do not understand why it gives an error ... - MaximK
- @MaximK: What is your version of Studio? - VladD
- oneAnd, of course, this is the syntax for 2015. For 2013, just write
double func(double x) { return Math.Sin(x); }double func(double x) { return Math.Sin(x); }. - VladD
|
Try using Modern UI Charts
or WPF Toolkit
.
If you delete the update of all styles when using ModernUI, it works very quickly. Toolkit - looks oldish, but it works perfectly!
|
