You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello. I want to add a real time graph to my WinForms project. Also, it should be possible to add series to the chart.
Here's what the non-working option looks like at the moment:
`public ChartForm()
{
InitializeComponent();
In the constructor of the form class, the chart class is initialized. The chart element is already present on the form and is named cartesianChart1.
A series dictionary has been added to the graph class, containing the series themselves with their points. 3) The constructor specifies a collection containing the series, as well as instructions for the horizontal XAxes axis and running the ReadData method in a new thread.
`public partial class RealTimeChartForChartForm : ObservableObject
{
private readonly Dictionary<string, List> _values = new();//ObservableCollection
private readonly DateTimeAxis _customAxis;
public RealTimeChartForChartForm()
{
Series = new ObservableCollection<ISeries>();
_customAxis = new DateTimeAxis(TimeSpan.FromSeconds(1), Formatter)
{
CustomSeparators = GetSeparators(),
AnimationsSpeed = TimeSpan.FromMilliseconds(0),
SeparatorsPaint = new SolidColorPaint(SKColors.Black.WithAlpha(100))
};
XAxes = new Axis[] { _customAxis };
_ = ReadData();
}
public void AddSeries(string seriesName)
{
if (!_values.ContainsKey(seriesName))
{
var observableValues = new List();
_values[seriesName] = observableValues;
Series.Add(new LineSeries
{
Values = observableValues,
Fill = null,
GeometryFill = null,
GeometryStroke = null,
Name = seriesName
});
}
}
public ObservableCollection Series { get; set; }
public Axis[] XAxes { get; set; }
public object Sync { get; } = new object();
public bool IsReading { get; set; } = true;
private async Task ReadData()
{
while (IsReading)
{
await Task.Delay(1000);
As you can see, the ReadData method iterates through the dictionary of series and adds a dot to each of them.
The result is that the graph is not updated😭😭😭 as expected when adding a series.
I have suspicions:
4)
a) Incorrect reference to an existing element cartesianChart1
b) The problem is the correct creation of series or points on a series
c) Something too light and inconspicuous. For example, incorrect values for points (large or small, or zero, beyond the limits) but it seems like I just set it to 10 and it doesn’t work either
I have placed markers from 1 to 4 so that my mistake can be easily pointed out. I believe all markers will be used :(
Please tell me where is the problem in my code? What is better to use in this situation? Thank you very much in advance!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello. I want to add a real time graph to my WinForms project. Also, it should be possible to add series to the chart.
Here's what the non-working option looks like at the moment:
`public ChartForm()
{
InitializeComponent();
}`
In the constructor of the form class, the chart class is initialized. The chart element is already present on the form and is named cartesianChart1.
A series dictionary has been added to the graph class, containing the series themselves with their points. 3) The constructor specifies a collection containing the series, as well as instructions for the horizontal XAxes axis and running the ReadData method in a new thread.
`public partial class RealTimeChartForChartForm : ObservableObject
{
private readonly Dictionary<string, List> _values = new();//ObservableCollection
private readonly DateTimeAxis _customAxis;
public RealTimeChartForChartForm()
{
}
public void AddSeries(string seriesName)
{
if (!_values.ContainsKey(seriesName))
{
var observableValues = new List();
_values[seriesName] = observableValues;
Series.Add(new LineSeries
{
Values = observableValues,
Fill = null,
GeometryFill = null,
GeometryStroke = null,
Name = seriesName
});
}
}
public ObservableCollection Series { get; set; }
public Axis[] XAxes { get; set; }
public object Sync { get; } = new object();
public bool IsReading { get; set; } = true;
private async Task ReadData()
{
while (IsReading)
{
await Task.Delay(1000);
}
private double[] GetSeparators()
{
var now = DateTime.Now;
}
private static string Formatter(DateTime date)
{
var secsAgo = (DateTime.Now - date).TotalSeconds;
}
}`
As you can see, the ReadData method iterates through the dictionary of series and adds a dot to each of them.
The result is that the graph is not updated😭😭😭 as expected when adding a series.
I have suspicions:
4)
a) Incorrect reference to an existing element cartesianChart1
b) The problem is the correct creation of series or points on a series
c) Something too light and inconspicuous. For example, incorrect values for points (large or small, or zero, beyond the limits) but it seems like I just set it to 10 and it doesn’t work either
I have placed markers from 1 to 4 so that my mistake can be easily pointed out. I believe all markers will be used :(
Please tell me where is the problem in my code? What is better to use in this situation? Thank you very much in advance!
Beta Was this translation helpful? Give feedback.
All reactions