forked from ScottPlot/ScottPlot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
349 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
src/ScottPlot5/ScottPlot5 Controls/ScottPlot.Blazor/BlazorPlot.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using ScottPlot.Control; | ||
using SkiaSharp; | ||
|
||
namespace ScottPlot.Blazor | ||
{ | ||
public abstract class BlazorPlot : ComponentBase, IPlotControl | ||
{ | ||
[Parameter] | ||
public string Style { get; set; } = string.Empty; | ||
|
||
public Plot Plot { get; } = new(); | ||
|
||
public BlazorPlot() | ||
{ | ||
HandlerPointerMoved += OnPointerMoved; | ||
HandlerPointerPressed += OnPointerPressed; | ||
HandlerPointerReleased += OnPointerReleased; | ||
HandlerDoubleTapped += OnDoubleTapped; | ||
HandlerPointerWheelChanged += OnPointerWheelChanged; | ||
HandlerKeyDown += OnKeyDown; | ||
HandlerKeyUp += OnKeyUp; | ||
|
||
DisplayScale = DetectDisplayScale(); | ||
Interaction = new(this); | ||
} | ||
|
||
public RenderQueue RenderQueue { get; } = new(); | ||
|
||
public Interaction Interaction { get; private set; } | ||
|
||
public GRContext? GRContext => null; | ||
|
||
public float DisplayScale { get; set; } | ||
|
||
public float DetectDisplayScale() | ||
{ | ||
// TODO: improve support for DPI scale detection | ||
// https://github.com/ScottPlot/ScottPlot/issues/2760 | ||
return 1.0f; | ||
} | ||
|
||
public Coordinates GetCoordinates(Pixel px, IXAxis? xAxis = null, IYAxis? yAxis = null) | ||
{ | ||
return Plot.GetCoordinates(px, xAxis, yAxis); | ||
} | ||
|
||
public virtual void Refresh() { } | ||
|
||
public void Replace(Interaction interaction) | ||
{ | ||
Interaction = interaction; | ||
} | ||
|
||
public void ShowContextMenu(Pixel position) | ||
{ | ||
//throw new NotImplementedException(); | ||
} | ||
|
||
public event EventHandler<PointerEventArgs> HandlerPointerMoved; | ||
|
||
private void OnPointerMoved(object? sender, PointerEventArgs e) | ||
{ | ||
Interaction.OnMouseMove(CoordinateToPixel(e)); | ||
} | ||
|
||
public void OnPointerMoved(PointerEventArgs e) | ||
{ | ||
HandlerPointerMoved.Invoke(this, e); | ||
} | ||
|
||
public event EventHandler<PointerEventArgs> HandlerPointerPressed; | ||
|
||
private void OnPointerPressed(object? sender, PointerEventArgs e) | ||
{ | ||
Interaction.MouseDown(CoordinateToPixel(e), MapToScottMouseButton(e)); | ||
} | ||
|
||
public void OnPointerPressed(PointerEventArgs e) | ||
{ | ||
HandlerPointerPressed.Invoke(this, e); | ||
} | ||
|
||
public event EventHandler<PointerEventArgs> HandlerPointerReleased; | ||
|
||
private void OnPointerReleased(object? sender, PointerEventArgs e) | ||
{ | ||
Interaction.MouseUp(CoordinateToPixel(e), MapToScottMouseButton(e)); | ||
} | ||
|
||
public void OnPointerReleased(PointerEventArgs e) | ||
{ | ||
HandlerPointerReleased.Invoke(this, e); | ||
} | ||
|
||
public event EventHandler<MouseEventArgs> HandlerDoubleTapped; | ||
|
||
private void OnDoubleTapped(object? sender, MouseEventArgs e) | ||
{ | ||
Interaction.DoubleClick(); | ||
} | ||
public void OnDoubleTapped(MouseEventArgs e) | ||
{ | ||
HandlerDoubleTapped.Invoke(this, e); | ||
} | ||
|
||
public event EventHandler<WheelEventArgs> HandlerPointerWheelChanged; | ||
|
||
public void OnPointerWheelChanged(object? sender, WheelEventArgs e) | ||
{ | ||
Interaction.MouseWheelVertical(CoordinateToPixel(e), -(float)e.DeltaY); | ||
} | ||
|
||
public void OnPointerWheelChanged(WheelEventArgs e) | ||
{ | ||
HandlerPointerWheelChanged.Invoke(this, e); | ||
} | ||
|
||
|
||
public event EventHandler<KeyboardEventArgs> HandlerKeyDown; | ||
|
||
public void OnKeyDown(object? sender, KeyboardEventArgs e) | ||
{ | ||
Interaction.KeyDown(MapToScottKey(e.Key)); | ||
|
||
} | ||
|
||
public void OnKeyDown(KeyboardEventArgs e) | ||
{ | ||
HandlerKeyDown.Invoke(this, e); | ||
} | ||
|
||
public event EventHandler<KeyboardEventArgs> HandlerKeyUp; | ||
|
||
public void OnKeyUp(object? sender, KeyboardEventArgs e) | ||
{ | ||
Interaction.KeyUp(MapToScottKey(e.Key)); | ||
} | ||
|
||
public void OnKeyUp(KeyboardEventArgs e) | ||
{ | ||
HandlerKeyUp.Invoke(this, e); | ||
} | ||
|
||
public Pixel CoordinateToPixel(WheelEventArgs args) | ||
{ | ||
return new Pixel((float)args.OffsetX, (float)args.OffsetY); | ||
} | ||
|
||
public Pixel CoordinateToPixel(PointerEventArgs args) | ||
{ | ||
return new Pixel((float)args.OffsetX, (float)args.OffsetY); | ||
} | ||
|
||
public MouseButton MapToScottMouseButton(MouseEventArgs args) | ||
{ | ||
if (args.Button == 0) | ||
{ | ||
return MouseButton.Left; | ||
} | ||
else if (args.Button == 1) | ||
{ | ||
return MouseButton.Middle; | ||
} | ||
else if (args.Button == 2) | ||
{ | ||
return MouseButton.Right; | ||
} | ||
else | ||
{ | ||
return MouseButton.Unknown; | ||
} | ||
} | ||
|
||
public static Key MapToScottKey(string key) | ||
{ | ||
switch (key) | ||
{ | ||
case "Control": | ||
return Key.Ctrl; | ||
case "Alt": | ||
return Key.Alt; | ||
case "Shift": | ||
return Key.Shift; | ||
default: | ||
return Key.Unknown; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/ScottPlot5/ScottPlot5 Controls/ScottPlot.Blazor/autoformat.bat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dotnet format | ||
pause |
31 changes: 31 additions & 0 deletions
31
src/ScottPlot5/ScottPlot5 Sandbox/Sandbox.Blazor.WebAssembly/Pages/BlazorPlotDemo.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@page "/bp" | ||
@using ScottPlot; | ||
@using ScottPlot.Blazor; | ||
|
||
<p>The <strong>BlazorPlot</strong> manipulates pixel data in memory</p> | ||
|
||
<BlazorPlotSkia @ref="BlazorPlot" Style="width: min(100%, 800px); height: 600px;" /> | ||
|
||
@code { | ||
BlazorPlotSkia BlazorPlot = new(); | ||
|
||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
BlazorPlot.Plot.Add.Signal(ScottPlot.Generate.Sin()); | ||
BlazorPlot.Plot.Add.Signal(ScottPlot.Generate.Cos()); | ||
|
||
var crosshair = BlazorPlot.Plot.Add.Crosshair(0, 0); | ||
|
||
BlazorPlot.HandlerPointerMoved += (s, e) => | ||
{ | ||
crosshair.Position = BlazorPlot.GetCoordinates(BlazorPlot.CoordinateToPixel(e)); | ||
BlazorPlot.Refresh(); | ||
}; | ||
} | ||
|
||
base.OnAfterRender(firstRender); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/ScottPlot5/ScottPlot5 Sandbox/Sandbox.Blazor.WebAssembly/Pages/BlazorPlotGLDemo.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@page "/bpgl" | ||
@using ScottPlot; | ||
@using ScottPlot.Blazor; | ||
|
||
<p>The <strong>BlazorPlotGL</strong> uses WebGL for improved performance but may not work in some browsers</p> | ||
|
||
<BlazorPlotGL @ref="BlazorPlot" Style="width: min(100%, 800px); height: 600px;" /> | ||
|
||
@code { | ||
BlazorPlotGL BlazorPlot = new(); | ||
|
||
protected override void OnAfterRender(bool firstRender) | ||
{ | ||
if (firstRender) | ||
{ | ||
BlazorPlot.Plot.Add.Signal(ScottPlot.Generate.Sin()); | ||
BlazorPlot.Plot.Add.Signal(ScottPlot.Generate.Cos()); | ||
|
||
var crosshair = BlazorPlot.Plot.Add.Crosshair(0, 0); | ||
|
||
BlazorPlot.HandlerPointerMoved += (s, e) => | ||
{ | ||
crosshair.Position = BlazorPlot.GetCoordinates(BlazorPlot.CoordinateToPixel(e)); | ||
BlazorPlot.Refresh(); | ||
}; | ||
} | ||
|
||
base.OnAfterRender(firstRender); | ||
} | ||
} | ||
|
Oops, something went wrong.