Skip to content

Instantly share code, notes, and snippets.

@KWodarczyk
Created March 17, 2017 22:59
Show Gist options
  • Save KWodarczyk/06306ed4b74046c5ac57799be91a51d2 to your computer and use it in GitHub Desktop.
Save KWodarczyk/06306ed4b74046c5ac57799be91a51d2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LiveCharts;
using LiveCharts.Wpf;
using System.Globalization;
namespace CurrencyPlayer.Controls
{
/// <summary>
/// Interaction logic for CurrencyChart.xaml
/// </summary>
public partial class CurrencyChart : UserControl, INotifyPropertyChanged
{
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public Func<double, string> YFormatter { get; set; }
public List<Currency> CurrencyCollection { get; set; }
private double _to;
private double _from;
private readonly int size = 200;
public CurrencyChart()
{
InitializeComponent();
From = 0;
To = size;
CurrencyChartCtrl.Zoom = ZoomingOptions.X;
CurrencyChartCtrl.AxisX.Add(new Axis
{
Separator = new LiveCharts.Wpf.Separator
{
Step = 0.00001,
IsEnabled = false
}
});
CurrencyCollection = new List<Currency>
{
new Currency
{
Rate = 4.1,
Date = DateTime.Now.ToLongTimeString(),
Action = CurrencyAction.None
},
new Currency
{
Rate = 4.1,
Date = DateTime.Now.AddMinutes(1).ToLongTimeString(),
Action = CurrencyAction.Bought
},
new Currency
{
Rate = 4.1,
Date = DateTime.Now.AddMinutes(2).ToLongTimeString(),
Action = CurrencyAction.Sold
},
new Currency
{
Rate = 4.1,
Date = DateTime.Now.AddMinutes(3).ToLongTimeString(),
Action = CurrencyAction.None
}
};
//CurrencyCollection = CurrencyCollection.Take<Currency>(400).ToList();
ChartValues<double> values = new ChartValues<double>();
SectionsCollection MySections = new SectionsCollection();
VisualElementsCollection vElements = new VisualElementsCollection();
int x = 0;
foreach (Currency c in CurrencyCollection)
{
values.Add(c.Rate);
if (c.Action != CurrencyAction.None)
{
if (c.Action == CurrencyAction.Bought)
{
AxisSection section = new AxisSection
{
Value = c.Rate,
Label = c.Date,
StrokeThickness = 3,
Visibility = Visibility.Visible,
Stroke = c.Action == CurrencyAction.Bought ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 255, 0))
};
VisualElement vElement = new VisualElement
{
X = x,
Y = c.Rate,
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Visibility = Visibility.Visible,
UIElement = new Line
{
Width = 2,
Visibility = Visibility.Visible,
Stroke = c.Action == CurrencyAction.Bought ? new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0)) : new SolidColorBrush(System.Windows.Media.Color.FromRgb(0, 255, 0))
}
};
MySections.Add(section);
vElements.Add(vElement);
}
}
x++;
}
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Currency Rate",
Values = values
}
};
CurrencyChartCtrl.VisualElements.Add(new VisualElement
{
X = 4,
Y = 4,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Top,
UIElement = new TextBlock //notice this property must be a wpf control
{
Text = "Warning!",
FontWeight = FontWeights.Bold,
FontSize = 16,
Opacity = 0.6
}
});
CurrencyChartCtrl.VisualElements = vElements;
CurrencyChartCtrl.AxisX.Add(new Axis
{
Sections = MySections,
IsEnabled = true,
Visibility = Visibility.Visible
});
Labels = CurrencyCollection.Select(c => c.Date).ToArray();
YFormatter = value => value.ToString();
DataContext = this;
}
public double From
{
get { return _from; }
set
{
_from = value;
OnPropertyChanged("From");
}
}
public double To
{
get { return _to; }
set
{
_to = value;
OnPropertyChanged("To");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void NextOnClick(object sender, RoutedEventArgs e)
{
From += size;
To += size;
}
private void PrevOnClick(object sender, RoutedEventArgs e)
{
From -= size;
To -= size;
}
private void ManualZoom(object sender, RoutedEventArgs e)
{
//you only need to change the axis limits to zoom in/out any axis.
From = 5;
To = 10;
}
public void LoadData()
{
}
private void ClearZoom()
{
//to clear the current zoom/pan just set the axis limits to double.NaN
CurrencyChartCtrl.AxisX[0].MinValue = double.NaN;
CurrencyChartCtrl.AxisX[0].MaxValue = double.NaN;
CurrencyChartCtrl.AxisY[0].MinValue = double.NaN;
CurrencyChartCtrl.AxisY[0].MaxValue = double.NaN;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment