Найти максимальные / минимальные значения текущего видового экрана
Вопрос: Как найти максимальное и минимальное значение оси X текущего окна просмотра RadCartesianChart, используя DateTimeCategoricalAxis
?
Я могу легко найти максимальные / минимальные значения набора данных и, если бы я использовал DateTimeContinousAxis
(к сожалению, не вариант), я мог бы просто использовать ActualRange
а также ActualVisibleRange
, Однако используя DateTimeCategoricalAxis
это совершенно другая история, так как я не могу точно найти точки данных в текущем окне просмотра. Я даже пытался использовать PlotAreaClip
но безрезультатно. Не говоря уже о, PlotAreaClip
не учитывает размер зума, что становится проблемой.
Любая помощь будет принята с благодарностью!
Вот упрощенная версия моего кода:
XAML:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<telerik:RadCartesianChart ZoomChanged="Zooming">
<telerik:RadCartesianChart.Behaviors>
<telerik:ChartPanAndZoomBehavior DragMode="Pan"
PanMode="Both"
ZoomMode="None" />
</telerik:RadCartesianChart.Behaviors>
<telerik:RadCartesianChart.Series>
<telerik:OhlcSeries ItemsSource="{Binding Bars}"
OpenBinding="Open"
HighBinding="High"
LowBinding="Low"
CloseBinding="Close"
CategoryBinding="Date">
</telerik:OhlcSeries>
</telerik:RadCartesianChart.Series>
<telerik:RadCartesianChart.HorizontalAxis>
<telerik:DateTimeCategoricalAxis DateTimeComponent="Ticks"
ShowLabels="False"
PlotMode="OnTicks"
MajorTickInterval="100">
</telerik:DateTimeCategoricalAxis>
</telerik:RadCartesianChart.HorizontalAxis>
<telerik:RadCartesianChart.VerticalAxis>
<telerik:LinearAxis HorizontalLocation="Right"
RangeExtendDirection="Both"
Minimum="{Binding PriceMinimum, Mode=OneWay}"
Maximum="{Binding PriceMaximum, Mode=OneWay}"
MajorStep="{Binding PriceMajorStep, Mode=OneWay}" />
</telerik:RadCartesianChart.VerticalAxis>
<telerik:RadCartesianChart.Grid>
<telerik:CartesianChartGrid MajorXLinesRenderMode="All"
MajorLinesVisibility="XY" />
</telerik:RadCartesianChart.Grid>
</telerik:RadCartesianChart>
</Grid>
</UserControl>
КОД позади:
public void Zooming(object sender, ChartZoomChangedEventArgs e)
{
RadCartesianChart chart = sender as RadCartesianChart;
if (chart != null)
{
//PlotAreaClip.Location.X (PlotAreaClip.X also works) to get left side of clip
//PlotAreaClip.Right to get right side of clip
DataTuple dtMin = chart.ConvertPointToData(new Point(chart.PlotAreaClip.Location.X - Math.Abs(chart.PanOffset), chart.PlotAreaClip.Y), chart.HorizontalAxis, chart.VerticalAxis);
DataTuple dtMax = chart.ConvertPointToData(new Point(chart.PlotAreaClip.Right - Math.Abs(chart.PanOffset), chart.PlotAreaClip.Y), chart.HorizontalAxis, chart.VerticalAxis);
object xMin = dtMin.FirstValue;
object xMax = dtMax.FirstValue;
//these numbers are VERY inconsistent, especially when you zoom in!
}
}
Упомянутый элемент управления Telerik можно найти здесь.
Если у кого-то есть какие-либо предложения, я был бы очень признателен! Спасибо!
1 ответ
После некоторых копаний я нашел эту тему со следующим решением:
private DataPoint[] FindFirstLastVisiblePoints(CategoricalSeries series)
{
DataPoint firstPoint = null;
DataPoint lastPoint = null;
RadRect plotArea = this.radChart1.PlotAreaClip;
foreach (DataPoint point in series.DataPoints)
{
if (point.LayoutSlot.IntersectsWith(plotArea))
{
if (firstPoint == null)
{
firstPoint = point;
}
lastPoint = point;
}
}
return new DataPoint[] { firstPoint, lastPoint };
}
В основном, этот метод принимает текущий PlotAreaClip
и возвращает первую и последнюю точку данных в текущем ViewPort. Я действительно надеюсь, что это поможет другим в будущем!