Skip to content

Commit

Permalink
Fix smooth scrolling when doing live recording.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettchris committed Apr 20, 2021
1 parent d363342 commit 724c227
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 110 deletions.
78 changes: 66 additions & 12 deletions LogViewer/LogViewer/Controls/SimpleLineChart.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ internal void Add(double x, double y)
minY = Math.Min(minY, y);
maxY = Math.Max(maxY, y);
}

public override string ToString()
{
return string.Format("X: {0}, {1}, Y: {2}, {3}", minX, maxX, minY, maxY);
}
}

public static class SimpleLineChartCommands
Expand Down Expand Up @@ -133,14 +138,18 @@ public bool LiveScrolling
get { return liveScrolling; }
set
{
liveScrolling = value;
if (value)
{
StartUpdateTimer();
}
else
if (value != liveScrolling)
{
StopUpdateTimer();
liveScrolling = value;
if (value)
{
TrimToLiveScroll();
StartUpdateTimer();
}
else
{
StopUpdateTimer();
}
}
}
}
Expand Down Expand Up @@ -219,6 +228,10 @@ public void DelayedUpdate()

internal void ZoomTo(double x, double width)
{
if (this.liveScrolling)
{
return;
}
int i = GetIndexFromX(x);
if (i == -1)
{
Expand All @@ -227,7 +240,7 @@ internal void ZoomTo(double x, double width)
}
this.visibleStartIndex = i;
this.visibleEndIndex = GetIndexFromX(x + width);

this.dirty = true;
var info = ComputeScaleSelf();
ApplyScale(info);

Expand All @@ -236,10 +249,17 @@ internal void ZoomTo(double x, double width)

internal void ResetZoom()
{
if (this.liveScrolling)
{
return;
}
this.dirty = true;
this.visibleStartIndex = 0;
this.visibleEndIndex = -1;
this.smoothScrollScaleIndex = 0;
this.scaleSelf = new ChartScaleInfo();
var info = ComputeScaleSelf();
ApplyScale(info);
InvalidateArrange();
}

Expand Down Expand Up @@ -295,7 +315,7 @@ public ChartScaleInfo ComputeScaleSelf()
int startIndex = this.visibleStartIndex;
int endIndex = this.visibleEndIndex;

if (!dirty && scaleSelf != null)
if (!dirty && scaleSelf != null && !this.liveScrolling)
{
return scaleSelf;
}
Expand Down Expand Up @@ -332,6 +352,7 @@ public ChartScaleInfo ComputeScaleSelf()
scaleSelf.Add(x, y);
}
this.smoothScrollScaleIndex = endIndex;

return scaleSelf;
}

Expand Down Expand Up @@ -425,10 +446,9 @@ private void SmoothScroll(DataValue newValue)
this.series.Values.Add(copy);

PathGeometry g = Graph.Data as PathGeometry;
if (g == null)
if (g == null || this.dirty)
{
UpdateChart();
return;
}
PathFigure f = g.Figures[0];

Expand Down Expand Up @@ -462,6 +482,40 @@ private void SmoothScroll(DataValue newValue)
}
}

private void TrimToLiveScroll()
{
double scaleX = this.liveScrollingXScale;

this.visibleEndIndex = series.Values.Count;
this.visibleStartIndex = this.visibleEndIndex;
var width = this.ActualWidth;

// find the data value that starts the chart on the left side of the scrolling window
// and make this the visibleStartIndex from which smooth scrolling will resume.
if (series.Values.Count > 0)
{
// walk back until the scaled values fill one screen width.
this.visibleStartIndex = this.visibleEndIndex - 1;
var dv = series.Values[this.visibleStartIndex];
double endX = dv.X * scaleX;

while (--this.visibleStartIndex > 0)
{
dv = series.Values[this.visibleStartIndex];
double startX = dv.X * scaleX;

if (endX - startX > width)
{
break;
}
}
}

this.scaleSelf = null;
this.dirty = true;
}


Size layoutSize;

void UpdateChart()
Expand All @@ -471,6 +525,7 @@ void UpdateChart()
this.series = new DataSeries() { Name = "", Values = new List<DataValue>() };
}

this.dirty = false;
layoutSize = new Size(this.ActualWidth, this.ActualHeight);
Canvas.SetLeft(Graph, 0);
visibleCount = 0;
Expand Down Expand Up @@ -575,7 +630,6 @@ private void AddScaledValues(PathFigure figure, int start, int end)
visibleCount++;
if (!started)
{
Debug.WriteLine(pt.ToString() + ",");
figure.StartPoint = pt;
started = true;
previousX = (int)rx;
Expand Down
2 changes: 1 addition & 1 deletion LogViewer/LogViewer/LogViewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<SuiteName>PX4 Log Viewer</SuiteName>
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>publish.htm</WebPage>
<ApplicationRevision>61</ApplicationRevision>
<ApplicationRevision>62</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down
22 changes: 14 additions & 8 deletions LogViewer/LogViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ private void ShowSchema()
{
schema = currentFlightLog.Schema;
}

foreach (var log in this.logs)
{
var s = log.Schema;
Expand All @@ -657,13 +658,13 @@ private void ShowSchema()
schema.Combine(s);
}
}
if (schema == null || schema.ChildItems == null || schema.ChildItems.Count == 0)
if (schema == null || !schema.HasChildren)
{
CategoryList.ItemsSource = null;
}
else
{
List<LogItemSchema> list = new List<Model.LogItemSchema>(schema.ChildItems);
List<LogItemSchema> list = schema.CopyChildren();
list.Sort((a, b) => { return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase); });
CategoryList.ItemsSource = list;
}
Expand All @@ -683,7 +684,7 @@ private void OnListItemSelected(object sender, SelectionChangedEventArgs e)
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
LogItemSchema item = (LogItemSchema)e.AddedItems[0];
if (sender == CategoryList && item.ChildItems != null)
if (sender == CategoryList && item.HasChildren)
{
ListViewItem listItem = CategoryList.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;
if (listItem != null)
Expand All @@ -693,7 +694,7 @@ private void OnListItemSelected(object sender, SelectionChangedEventArgs e)
expander.IsExpanded = true;
}
}
else if (item.ChildItems == null || item.ChildItems.Count == 0)
else if (!item.HasChildren)
{
if (item.Parent == null)
{
Expand All @@ -705,7 +706,7 @@ private void OnListItemSelected(object sender, SelectionChangedEventArgs e)
if (e.RemovedItems != null && e.RemovedItems.Count > 0)
{
LogItemSchema item = (LogItemSchema)e.RemovedItems[0];
if (sender == CategoryList && item.ChildItems != null)
if (sender == CategoryList && item.HasChildren)
{
ListViewItem listItem = CategoryList.ItemContainerGenerator.ContainerFromItem(item) as ListViewItem;
if (listItem != null)
Expand Down Expand Up @@ -1459,7 +1460,7 @@ private void OnFlightSelected(object sender, SelectionChangedEventArgs e)
// todo: show sensor data pruned to this flight time...
foreach (LogItemSchema item in CategoryList.SelectedItems)
{
if (item.ChildItems == null || item.ChildItems.Count == 0)
if (!item.HasChildren)
{
GraphItem(item);
}
Expand All @@ -1480,10 +1481,15 @@ private void OnItemExpanded(object sender, RoutedEventArgs e)
{
Expander expander = (Expander)sender;
ListView childView = expander.Content as ListView;
if (childView != null)
if (childView != null && childView.ItemsSource == null)
{
LogItemSchema item = (LogItemSchema)expander.DataContext;
childView.ItemsSource = item.ChildItems;
List<LogItemSchema> list = item.CopyChildren();
if (!item.IsArray)
{
list.Sort((a, b) => { return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase); });
}
childView.ItemsSource = list;
}
}

Expand Down
18 changes: 8 additions & 10 deletions LogViewer/LogViewer/Model/CsvDataLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ await Task.Run(() =>
{
// create the schema
this.schema = new LogItemSchema() { Name = "CsvLog", Type = "Root" };
List<LogItemSchema> children = new List<Model.LogItemSchema>();
LogItemSchema row = null;

foreach (String name in reader.ColumnNames)
Expand All @@ -93,31 +92,30 @@ await Task.Run(() =>
LogItemSchema group = null;
if (!map.ContainsKey(key))
{
group = new LogItemSchema() { Name = key, Parent = this.schema, Type = key, ChildItems = new List<LogItemSchema>() };
children.Add(group);
group = new LogItemSchema() { Name = key, Type = key };
this.schema.AddChild(group);
map[key] = group;
}
else
{
group = map[key];
}
var leaf = new LogItemSchema() { Name = field, Parent = group, Type = "Double" };
group.ChildItems.Add(leaf);
var leaf = new LogItemSchema() { Name = field, Type = "Double" };
group.AddChild(leaf);
map[name] = leaf;
}
else
{
if (row == null)
{
row = new LogItemSchema() { Name = "Other", Parent = this.schema, Type = "Other", ChildItems = new List<LogItemSchema>() };
children.Add(row);
row = new LogItemSchema() { Name = "Other", Type = "Other" };
this.schema.AddChild(row);
}
var leaf = new LogItemSchema() { Name = name, Parent = row, Type = "Double" };
row.ChildItems.Add(leaf);
var leaf = new LogItemSchema() { Name = name, Type = "Double" };
row.AddChild(leaf);
map[name] = leaf;
}
}
this.schema.ChildItems = children;
}

if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "row")
Expand Down
13 changes: 7 additions & 6 deletions LogViewer/LogViewer/Model/KmlDataLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,23 @@ private void Schematize(LogItemSchema schema, IEnumerable<LogField> items)
foreach (var item in items)
{
// is this a new item we haven't seen before?
if (!(from c in schema.ChildItems where c.Name == item.Name select c).Any())

if (!schema.HasChild(item.Name))
{
var typeName = item.Name;
if (item is LogEntry)
{
// recurrse down the structure
var s = new LogItemSchema() { Name = item.Name, Type = typeName, ChildItems = new List<Model.LogItemSchema>(), Parent = schema };
schema.ChildItems.Add(s);
var s = new LogItemSchema() { Name = item.Name, Type = typeName };
schema.AddChild(s);
Schematize(s, ((LogEntry)item).GetFields());
}
else
{
// leaf node
typeName = item.Value.GetType().Name;
var leaf = new LogItemSchema() { Name = item.Name, Type = typeName, Parent = schema };
schema.ChildItems.Add(leaf);
var leaf = new LogItemSchema() { Name = item.Name, Type = typeName };
schema.AddChild(leaf);
}
}
}
Expand All @@ -185,7 +186,7 @@ public LogItemSchema Schema
get {
if (this.schema == null)
{
this.schema = new LogItemSchema() { Name = "KmlLog", Type = "Root", ChildItems = new List<Model.LogItemSchema>() };
this.schema = new LogItemSchema() { Name = "KmlLog", Type = "Root" };
Schematize(this.schema, this.log);
}
return this.schema;
Expand Down
Loading

0 comments on commit 724c227

Please sign in to comment.