Posted 19 September 2022, 12:26 pm EST
Hi.
I will try to explain my issue without working example, hope it could be understood with provided code chunks.
What I want to achieve is whener new point is added on a chart, scrollbar by default navigated to N last points and show them (N=5, 10,…). Here is main code which triggered when new item is added to code:
private void UpdateChartsControls()
{
//NOTE, I omit some uninteresting stuff where
// Axis X min and max values are calcualted based new data
flexChartMain.AxisX.Max = max;
flexChartMain.AxisX.Min = min;
_aggregator.Source = _fdsi.Positions;
//Debug.WriteLine($"Current max ole dt:{max}");
//setup WHOLE range
rangeSelectorChart.AxisX.Max = max;
rangeSelectorChart.AxisX.Min = _fdsi.Positions.First().Date.ToOADate();
//you can ignore this if
if (Settings.Default.ShowAggChart)
{
ResetRangeSelector();
SetupRangeSelector();
if (Settings.Default.DoRangeTracking)
{
//setup visible(tracked) range
rangeSelectorCtrl.LowerValue = min;
rangeSelectorCtrl.UpperValue = max;
}
rangeSelectorCtrl.Refresh();//??
}
else
{
SetupAxisXSelector();
_horizontalScrollbar.ValueChanged -= ScrollbarAxisXValueChanged;
Debug.WriteLine($"lw = {_horizontalScrollbar.LowerValue},uv = {_horizontalScrollbar.UpperValue}");
_horizontalScrollbar.LowerValue = min;
_horizontalScrollbar.UpperValue = max;
_horizontalScrollbar.ValueChanged += ScrollbarAxisXValueChanged;
_horizontalScrollbar.Tag = new Tuple<double, double>(min, max);
}
}
private void SetupAxisXSelector()
{
if (Settings.Default.ShowAggChart)
{
panel1.Visible = true;
SetupRangeSelector();
}
else
{
panel1.Visible = false;
SetupAxisXScrollingSelector();
}
}
private void SetupAxisXScrollingSelector()
{
if (_horizontalScrollbar != null)
{
return;
}
_horizontalScrollbar = new AxisScrollbar(flexChartMain.AxisX);
_horizontalScrollbar.ValueChanged += ScrollbarAxisXValueChanged;
}
private void ScrollbarAxisXValueChanged(object sender, EventArgs e)
{
if (_fdsi == null)
{
return;
}
Debug.WriteLine($"_horizontalScrollbar.LowerValue={_horizontalScrollbar.LowerValue}");
Debug.WriteLine($"_horizontalScrollbar.UpperValue={_horizontalScrollbar.UpperValue}");
flexChartMain.AxisX.Min = _horizontalScrollbar.LowerValue;
flexChartMain.AxisX.Max = _horizontalScrollbar.UpperValue;
}
The issue I have with this code is that for some reason scrollbar valuechanged event is called after UpdateChartsControls() and it overwrites my upper and lower value and shows scrollbar for it’s whole length instead of just needed length by N last points. In code above I unsubscribe from this event before changing lower and upper values, and then suscribe again, hoping that scrollbar would be rendered correctly but it is rendered for whole length of axis X.
To illustrate further more what I need here is small chunk of code which is doing what I need but make scrollbar completely unusable:
private void flexChartMain_Rendered(object sender, RenderEventArgs e)
{
if (!Settings.Default.ShowAggChart)
{
if (_horizontalScrollbar.Tag is Tuple<double, double> tag)
{
_horizontalScrollbar.LowerValue = tag.Item1;
_horizontalScrollbar.UpperValue = tag.Item2;
}
}
}
What i’m doing wrong? Why _horizontalScrollbar.ValueChanged triggered one more time with lower and upper values reseted by whole range of axis x?
Thanks in advance.
PS: I have to regimes of navigation on chart – additional chart (rangeSelectorChart) + range selector and simple mode with scrolling.