public class ScrollingViewportManager : DefaultViewportManager
{
private bool m_isPlotting;
#region Properties
///
/// User enable auto range of y axis through this property
///
public bool IsEnabledYAxisAutoRange { get; set; } = true;
///
/// User enable auto range of x axis through this property
///
public bool IsEnabledXAxisAutoRange { get; set; } = true;
public bool IsPlotting
{
get { return m_isPlotting; }
set
{
m_isPlotting = value;
ParentSurface.ZoomState = ZoomStates.AtExtents; // Set zoom state to unzoom.
}
}
public double WindowSize { get; set; }
public ISciChartSurface ParentSurface { get; private set; }
#endregion
public ScrollingViewportManager(double windowSize)
{
this.WindowSize = windowSize;
IsEnabledYAxisAutoRange = true; // Default Y axis auto range on
IsEnabledXAxisAutoRange = true; // Default Y axis auto range on
}
public override void AttachSciChartSurface(ISciChartSurface scs)
{
base.AttachSciChartSurface(scs);
this.ParentSurface = scs;
}
protected override IRange OnCalculateNewYRange(IAxis yAxis, RenderPassInfo renderPassInfo)
{
//var baseRange = base.OnCalculateNewYRange(yAxis, renderPassInfo);
// The Current YAxis VisibleRange
var currentVisibleRange = yAxis.VisibleRange.AsDoubleRange();
if (m_isPlotting)
{
if (ParentSurface.ZoomState == ZoomStates.UserZooming) // Don't adjust range if user is zooming
return currentVisibleRange;
if (!IsEnabledYAxisAutoRange) // Don't adjust range if auto range is disabled
return currentVisibleRange;
if (yAxis.AutoRange == AutoRange.Always)
{
// Get max range based on all data of all series */
var maxYRange = yAxis.GetMaximumRange().AsDoubleRange();
if (maxYRange != null && maxYRange.IsDefined)
{
/* Add 5% margin to top and bottom of Y axis */
double newMin = 0.95 * maxYRange.Min;
double newMax = 1.05 * maxYRange.Max;
return new DoubleRange(newMin, newMax);
}
}
}
return currentVisibleRange;
}
protected override IRange OnCalculateNewXRange(IAxis xAxis)
{
// The Current XAxis VisibleRange
var currentVisibleRange = xAxis.VisibleRange.AsDoubleRange();
if (m_isPlotting)
{
if (ParentSurface.ZoomState == ZoomStates.UserZooming) // Don't adjust range if user is zooming
return currentVisibleRange;
if (!IsEnabledXAxisAutoRange) // Don't adjust range if auto range is disabled
return currentVisibleRange;
// Get current data max range
var maxXRange = xAxis.GetMaximumRange().AsDoubleRange();
// Compare data max range with visible max range
double xMax = Math.Max(maxXRange.Max, currentVisibleRange.Max);
if (xMax < WindowSize)
return currentVisibleRange;
// Scroll showing latest window size
return new DoubleRange(xMax - WindowSize, xMax);
}
return currentVisibleRange;
}
}