How to get a range / sub graph of a line chart on runtime?

Bar Code for WPF Forum

Posted 1 year ago by Nerd22 - Germany
Version: 22.1.4
Avatar

€: Oops, I accidently wrote this in the wrong forum. I'm sorry.

Hello there,

I'm using C#, .NET Framework 4.8 (WPF) and I would like to know how to get a sub graph or range of a given graph?

Let's say, I build a graph of 20 points, where:

X = {0,1,...,19}

Y = random numbers in a range of (-20,20)

The graph would start on {0,-12}, and continues with {1,13}, {2,9}, ..., {19,0}.

I would like to get a sub graph in the range between 5 and 15, where {5,2} might be the new start point and ends on {15,-6}.

That is my idea:

public List<Coordinates> GetSubGraph(int startPos, int endPos) {
	List<Coordinates> subGraph = new List<Coordinates>();

	for(int i = startPos; i < endPos; i++) {
		Coordinates c = LineGraph.ElementAt(i);
		subGraph.Add(c);
	}

	return subGraph;
}

followed by:

// clear old graph
chartX.Series.Clear();

// this shall create the new graph
chartX.Series.Add(new LineSeries() {
	ItemsSource = ViewModel.GetSubGraph(5, 15)
});

//  refreshing
chartX.Refresh();

There're no errors on runtime, however, the graph won't be displayed as expexted.

I only see a "graph" from 5 to 15, where each Y point is fixed on 1.

What do I miss?

Thank you (:

[Modified 1 year ago]

Comments (1)

Answer - Posted 1 year ago by Nerd22 - Germany
Avatar

I guess, I found the solution randomly.

private void HandleAreas(AreaSection section) {
	//chartX.Series.Clear();                     //not needed
	List<Coordinates> subGraph = ViewModel.GetSubGraph(5, 15);

	//chartX = new XYChart();                    //nope
	chartX.Series.Add(new LineSeries() {
		ItemsSource = subGraph,
		XPath = "X",
		YPath = "Y",
		LineKind = XYSeriesLineKind.Spline
	});

	chartX.Refresh();
}

Now I'm able to display the expected sub graph. :o)

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.