syneffort 2 years ago
parent a2ac0de8f6
commit 2f47c6b1e6
  1. 15
      PacticeSolution/LiveChartPractice/MainWindow.xaml
  2. 45
      PacticeSolution/LiveChartPractice/MainWindow.xaml.cs
  3. 11
      PacticeSolution/LiveChartPractice/SampleData.cs

@ -18,7 +18,7 @@
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<lvc:CartesianChart Grid.Row="0" Series="{Binding SeriesCollection}" LegendLocation="Right">
<lvc:CartesianChart Grid.Row="0" Series="{Binding SeriesCollection}" LegendLocation="Right" Zoom="Xy">
<lvc:CartesianChart.AxisY>
<lvc:Axis Title="Sales" LabelFormatter="{Binding YFormatter}"/>
</lvc:CartesianChart.AxisY>
@ -29,12 +29,19 @@
<Button x:Name="btnSeriesAdd" Grid.Row="1" Content="Add" Click="btnSeriesAdd_Click"/>
</Grid>
</TabItem>
<TabItem Header="Columns">
<TabItem Header="RT">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="20"/>
</Grid.RowDefinitions>
<lvc:CartesianChart Grid.Row="0" DisableAnimations="True" Series="{Binding SingleSeries}"/>
<Button x:Name="btnRT" Grid.Row="1" Content="Add" Click="btnRT_Click"/>
</Grid>
</TabItem>
<TabItem Header="StackedColumns">
<TabItem Header="Material Design">
</TabItem>
</TabControl>

@ -14,6 +14,7 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Timers;
namespace LiveChartPractice
{
@ -22,27 +23,57 @@ namespace LiveChartPractice
/// </summary>
public partial class MainWindow : Window
{
private Timer _timer;
private Random _rand;
private SampleData _data;
public MainWindow()
{
InitializeComponent();
InitInstance();
}
private void InitInstance()
{
_data = this.DataContext as SampleData;
_timer = new Timer();
_timer.Elapsed -= _timer_Elapsed;
_timer.Elapsed += _timer_Elapsed;
_timer.Interval = 100;
_timer.Stop();
_rand = new Random();
}
private void btnSeriesAdd_Click(object sender, RoutedEventArgs e)
{
SampleData data = this.DataContext as SampleData;
if (data == null)
if (_data == null)
return;
Random rand = new Random();
data.SeriesCollection.Add(new LineSeries
_data.SeriesCollection.Add(new LineSeries
{
Title = $"Series {data.SeriesCollection.Count + 1}",
Values = new ChartValues<double> { rand.Next(1, 8), rand.Next(1, 8), rand.Next(1, 8), rand.Next(1, 8), rand.Next(1, 8) },
LineSmoothness = rand.Next(0, 2), //0: straight lines, 1: really smooth lines
Title = $"Series {_data.SeriesCollection.Count + 1}",
Values = new ChartValues<double> { _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8), _rand.Next(1, 8) },
LineSmoothness = _rand.Next(0, 2), //0: straight lines, 1: really smooth lines
//PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
//PointGeometrySize = 50,
//PointForeground = Brushes.Gray
});
}
private void btnRT_Click(object sender, RoutedEventArgs e)
{
if (_data == null)
return;
_timer.Start();
}
private void _timer_Elapsed(object? sender, ElapsedEventArgs e)
{
_data.SingleSeries[0].Values.Add(_rand.NextDouble() * 10.0);
}
}
}

@ -13,6 +13,7 @@ namespace LiveChartPractice
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
public Func<double, string> YFormatter { get; set; }
public SeriesCollection SingleSeries { get; set; }
public SampleData()
{
@ -40,6 +41,16 @@ namespace LiveChartPractice
this.Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };
this.YFormatter = value => value.ToString("C");
this.SingleSeries = new SeriesCollection()
{
new LineSeries()
{
Title = "Series",
Values = new ChartValues<double>(),
LineSmoothness = 0,
}
};
}
}
}

Loading…
Cancel
Save