Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axes improvements #1196

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions samples/AvaloniaSample/General/RealTime/View.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="AvaloniaSample.General.RealTime.View"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Avalonia"
xmlns:vms="using:ViewModelsSamples.General.RealTime">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart
SyncContext="{Binding Sync}"
Series="{Binding Series}"
XAxes="{Binding XAxes}">
</lvc:CartesianChart>
</UserControl>
17 changes: 17 additions & 0 deletions samples/AvaloniaSample/General/RealTime/View.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace AvaloniaSample.General.RealTime;

public class View : UserControl
{
public View()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
13 changes: 13 additions & 0 deletions samples/BlazorSample/Pages/General/RealTime.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@page "/General/RealTime"
@using LiveChartsCore.SkiaSharpView.Blazor
@using ViewModelsSamples.General.RealTime

<CartesianChart
Series="ViewModel.Series"
SyncContext="ViewModel.Sync"
XAxes="ViewModel.XAxes">
</CartesianChart>

@code {
public ViewModel ViewModel { get; set; } = new();
}
24 changes: 24 additions & 0 deletions samples/EtoFormsSample/General/RealTime/View.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Eto.Forms;
using LiveChartsCore.SkiaSharpView.Eto;
using ViewModelsSamples.General.RealTime;

namespace EtoFormsSample.General.RealTime;

public class View : Panel
{
private readonly CartesianChart cartesianChart;

public View()
{
var viewModel = new ViewModel();

cartesianChart = new CartesianChart
{
SyncContext = viewModel.Sync,
Series = viewModel.Series,
XAxes = viewModel.XAxes
};

Content = cartesianChart;
}
}
20 changes: 20 additions & 0 deletions samples/MauiSample/General/RealTime/View.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiSample.General.RealTime.View"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.Maui;assembly=LiveChartsCore.SkiaSharpView.Maui"
xmlns:vms="clr-namespace:ViewModelsSamples.General.RealTime;assembly=ViewModelsSamples"
>
<ContentPage.BindingContext>
<vms:ViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>

<lvc:CartesianChart
SyncContext="{Binding Sync}"
Series="{Binding Series}"
XAxes="{Binding XAxes}">
</lvc:CartesianChart>

</ContentPage.Content>
</ContentPage>
10 changes: 10 additions & 0 deletions samples/MauiSample/General/RealTime/View.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MauiSample.General.RealTime;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class View : ContentPage
{
public View()
{
InitializeComponent();
}
}
19 changes: 19 additions & 0 deletions samples/UWPSample/General/RealTime/View.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<UserControl
x:Class="UWPSample.General.RealTime.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.Uno"
xmlns:vms="using:ViewModelsSamples.General.RealTime"
mc:Ignorable="d">

<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart
SyncContext="{Binding Sync}"
Series="{Binding Series}"
XAxes="{Binding XAxes}">
</lvc:CartesianChart>
</UserControl>
12 changes: 12 additions & 0 deletions samples/UWPSample/General/RealTime/View.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Windows.UI.Xaml.Controls;

namespace UWPSample.General.RealTime
{
public sealed partial class View : UserControl
{
public View()
{
InitializeComponent();
}
}
}
7 changes: 7 additions & 0 deletions samples/UWPSample/UWPSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
<Compile Include="General\Legends\View.xaml.cs">
<DependentUpon>View.xaml</DependentUpon>
</Compile>
<Compile Include="General\RealTime\View.xaml.cs">
<DependentUpon>View.xaml</DependentUpon>
</Compile>
<Compile Include="General\MultiThreading2\View.xaml.cs">
<DependentUpon>View.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -571,6 +574,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="General\RealTime\View.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="General\MultiThreading2\View.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<UserControl
x:Class="UnoWinUISample.General.RealTime.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:lvc="using:LiveChartsCore.SkiaSharpView.WinUI"
xmlns:vms="using:ViewModelsSamples.General.RealTime"
mc:Ignorable="d">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart
SyncContext="{Binding Sync}"
Series="{Binding Series}"
XAxes="{Binding XAxes}">
</lvc:CartesianChart>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.UI.Xaml.Controls;

namespace UnoWinUISample.General.RealTime;

public sealed partial class View : UserControl
{
public View()
{
InitializeComponent();
}
}
99 changes: 99 additions & 0 deletions samples/ViewModelsSamples/General/RealTime/ViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;

namespace ViewModelsSamples.General.RealTime;

public partial class ViewModel : ObservableObject
{
private readonly Random _random = new();
private readonly List<DateTimePoint> _values = new();
private readonly DateTimeAxis _customAxis;

public ViewModel()
{
Series = new ObservableCollection<ISeries>
{
new LineSeries<DateTimePoint>
{
Values = _values,
Fill = null,
GeometryFill = null,
GeometryStroke = null
}
};

_customAxis = new DateTimeAxis(TimeSpan.FromSeconds(1), Formatter)
{
CustomSeparators = GetSeparators(),
AnimationsSpeed = TimeSpan.FromMilliseconds(0),
SeparatorsPaint = new SolidColorPaint(SKColors.Black.WithAlpha(100))
};

XAxes = new Axis[] { _customAxis };

_ = ReadData();
}

public ObservableCollection<ISeries> Series { get; set; }

public Axis[] XAxes { get; set; }

public object Sync { get; } = new object();

public bool IsReading { get; set; } = true;

private async Task ReadData()
{
// to keep this sample simple, we run the next infinite loop // mark
// in a real application you should stop the loop/task when the view is disposed // mark

while (IsReading)
{
await Task.Delay(100);

// Because we are updating the chart from a different thread // mark
// we need to use a lock to access the chart data. // mark
// this is not necessary if your changes are made in the UI thread. // mark
lock (Sync)
{
_values.Add(new DateTimePoint(DateTime.Now, _random.Next(0, 10)));
if (_values.Count > 250) _values.RemoveAt(0);

// we need to update the separators every time we add a new point // mark
_customAxis.CustomSeparators = GetSeparators();
}
}
}

private double[] GetSeparators()
{
var now = DateTime.Now;

return new double[]
{
now.AddSeconds(-25).Ticks,
now.AddSeconds(-20).Ticks,
now.AddSeconds(-15).Ticks,
now.AddSeconds(-10).Ticks,
now.AddSeconds(-5).Ticks,
now.Ticks
};
}

private static string Formatter(DateTime date)
{
var secsAgo = (DateTime.Now - date).TotalSeconds;

return secsAgo < 1
? "now"
: $"{secsAgo:N0}s ago";
}
}
1 change: 1 addition & 0 deletions samples/ViewModelsSamples/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static class Index
"Events/Pie",
"Events/Polar",

"General/RealTime",
"General/Scrollable",
"General/Sections",
"General/Sections2",
Expand Down
14 changes: 14 additions & 0 deletions samples/WPFSample/General/RealTime/View.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<UserControl x:Class="WPFSample.General.RealTime.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
xmlns:vms="clr-namespace:ViewModelsSamples.General.RealTime;assembly=ViewModelsSamples">
<UserControl.DataContext>
<vms:ViewModel/>
</UserControl.DataContext>
<lvc:CartesianChart
SyncContext="{Binding Sync}"
Series="{Binding Series}"
XAxes="{Binding XAxes}">
</lvc:CartesianChart>
</UserControl>
14 changes: 14 additions & 0 deletions samples/WPFSample/General/RealTime/View.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Windows.Controls;

namespace WPFSample.General.RealTime;

/// <summary>
/// Interaction logic for View.xaml
/// </summary>
public partial class View : UserControl
{
public View()
{
InitializeComponent();
}
}
46 changes: 46 additions & 0 deletions samples/WinFormsSample/General/RealTime/View.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.