Skip to content

Commit

Permalink
Add .csv export menu item on charts.
Browse files Browse the repository at this point in the history
  • Loading branch information
lovettchris committed Mar 30, 2017
1 parent 68efe37 commit ded6645
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LogViewer/LogViewer/Controls/ChartGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LogViewer.Controls
public class ChartGroup : Grid
{
private bool scaleIndependently;
private int scaleIndex;
private int scaleIndex = 0;

public bool ScaleIndependently
{
Expand Down
6 changes: 6 additions & 0 deletions LogViewer/LogViewer/Controls/ChartStack.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ private void ZoomTo(double x, double width)
{
chart.ZoomTo(x, width);
}
if (ZoomChanged != null)
{
ZoomChanged(this, EventArgs.Empty);
}
}

public event EventHandler ZoomChanged;

protected override void OnMouseMove(MouseEventArgs e)
{
Point pos = e.GetPosition(this);
Expand Down
2 changes: 2 additions & 0 deletions LogViewer/LogViewer/Controls/SimpleLineChart.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<Separator/>
<MenuItem Header="Clear Adornments" Click="OnClearAdornments"/>
<MenuItem Header="Clear All Adornments" Click="OnClearAllAdornments"/>
<Separator/>
<MenuItem Header="Export .csv ..." Click="OnExportCsv"/>
</ContextMenu>
</Grid.ContextMenu>
<Grid.RowDefinitions>
Expand Down
65 changes: 59 additions & 6 deletions LogViewer/LogViewer/Controls/SimpleLineChart.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public sealed partial class SimpleLineChart : UserControl
bool liveScrolling;
double liveScrollingXScale = 1;
static bool anyContextMenuOpen;
double visibleCount;

/// <summary>
/// Set this property to add the chart to a group of charts. The group will share the same "scale" information across the
Expand All @@ -78,6 +79,11 @@ public SimpleLineChart()
this.ContextMenuClosing += ContextMenu_ContextMenuClosing;
}

internal double GetVisibleCount()
{
return visibleCount;
}

private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
anyContextMenuOpen = true;
Expand Down Expand Up @@ -447,6 +453,7 @@ void UpdateChart()
Canvas.SetLeft(Graph, 0);
scaleIndex = 0;
updateIndex = 0;
visibleCount = 0;

ComputeScale();

Expand Down Expand Up @@ -512,8 +519,9 @@ private void AddScaledValues(PathFigure figure, int start, int end)
double x = point.X;

double rx = x + offset;
if (rx > 0)
if (rx > 0 && rx < width)
{
visibleCount++;
Point pt = new Point(x, y);
if (!started)
{
Expand Down Expand Up @@ -812,8 +820,12 @@ private void OnLockTooltip(object sender, RoutedEventArgs e)
<TextBlock x:Name="PointerLabel" Foreground="{StaticResource TooltipForeground}"/>
</Border>
*/
Path ptr = new Path() {
Fill = Pointer.Fill, Data = Pointer.Data.Clone(), RenderTransform = Pointer.RenderTransform.Clone() };
Path ptr = new Path()
{
Fill = Pointer.Fill,
Data = Pointer.Data.Clone(),
RenderTransform = Pointer.RenderTransform.Clone()
};
AdornerCanvas.Children.Add(ptr);

PointerBorder ptrBorder = new PointerBorder()
Expand All @@ -833,6 +845,24 @@ private void OnLockTooltip(object sender, RoutedEventArgs e)
AdornerCanvas.Children.Add(ptrBorder);
}

private IEnumerable<DataValue> GetVisibleDataValues()
{
double w = this.ActualWidth;
if (this.series != null)
{
foreach (DataValue d in this.series.Values)
{
Point point = scaleTransform.Transform(new Point(d.X, d.Y));
point = zoomTransform.Transform(point);
if (point.X >= 0 && point.X <= w)
{
// then it is a visible point.
yield return d;
}
}
}
}

private void OnAddTrendLine(object sender, RoutedEventArgs e)
{
if (this.series.Values.Count == 0)
Expand All @@ -844,12 +874,13 @@ private void OnAddTrendLine(object sender, RoutedEventArgs e)

DataValue first = null;
DataValue last = null;
double w = this.ActualWidth;

foreach (DataValue d in this.series.Values)
{
Point point = scaleTransform.Transform(new Point(d.X, d.Y));
point = zoomTransform.Transform(point);
if (point.X >= 0 && point.X <= this.ActualWidth)
if (point.X >= 0 && point.X <= w)
{
// then it is a visible point.
if (first == null) first = d;
Expand Down Expand Up @@ -994,12 +1025,12 @@ private void OnAddMeanLine(object sender, RoutedEventArgs e)

DataValue first = null;
DataValue last = null;

double w = this.ActualWidth;
foreach (DataValue d in this.series.Values)
{
Point point = scaleTransform.Transform(new Point(d.X, d.Y));
point = zoomTransform.Transform(point);
if (point.X >= 0 && point.X <= this.ActualWidth)
if (point.X >= 0 && point.X <= w)
{
// then it is a visible point.
if (first == null) first = d;
Expand Down Expand Up @@ -1063,6 +1094,28 @@ private void OnAddMeanLine(object sender, RoutedEventArgs e)

AdornerCanvas.Children.Add(endlabel);
}

private void OnExportCsv(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog fo = new Microsoft.Win32.SaveFileDialog();
fo.Filter = "CSV Files (*.csv)|*.csv";
fo.CheckPathExists = true;
if (fo.ShowDialog() == true)
{
using (var stream = fo.OpenFile())
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
{
if (this.series != null) {
foreach (var d in this.GetVisibleDataValues())
{
writer.WriteLine(d.X + "\t" + d.Y);
}
}
}
}
}
}
}

class PointerBorder : Border
Expand Down
18 changes: 16 additions & 2 deletions LogViewer/LogViewer/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,24 @@ public MainWindow()
this.SizeChanged += OnWindowSizeChanged;
this.LocationChanged += OnWindowLocationChanged;
ChartStack.Visibility = Visibility.Collapsed;
ChartStack.ZoomChanged += OnZoomChanged;
initialAttitude = ModelViewer.ModelAttitude;
CameraPanel.Visibility = Visibility.Collapsed;
SystemConsole.Visibility = Visibility.Collapsed;
}

private void OnZoomChanged(object sender, EventArgs e)
{
double total = 0;
double count = 0;
foreach (var chart in ChartStack.FindCharts())
{
total += chart.GetVisibleCount();
count++;
}
ShowStatus(string.Format("zoom shwowing {0} data values", (int)(total / count)));
}

private void OnWindowLocationChanged(object sender, EventArgs e)
{
delayedActions.StartDelayedAction("SaveWindowLocation", SavePosition, TimeSpan.FromMilliseconds(1000));
Expand Down Expand Up @@ -470,8 +483,9 @@ private void LoadFlights(IDataLog data)
Flight entireLog = new Flight()
{
Name = "Log " + logs.Count,
StartTime = data.StartTime,
Duration = data.Duration
StartTime = DateTime.MinValue,
Duration = TimeSpan.MaxValue,
Log = data
};
allFlights.Add(entireLog);

Expand Down

0 comments on commit ded6645

Please sign in to comment.