// MyPlot.xaml.cs
public partial class MyPlot : UserControl
{
public static readonly DependencyProperty ScattersProperty = DependencyProperty.Register(
nameof(Scatters), typeof(ICollection<Scatter>), typeof(MyPlot), new FrameworkPropertyMetadata(OnScattersChanged));
public ICollection<Scatter>? Scatters
{
get => GetValue(ScattersProperty) as ICollection<Scatter>;
set => SetValue(ScattersProperty, value);
}
public MyPlot()
{
InitializeComponent();
}
private static void OnScattersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not MyPlot myPlot)
return;
if (e.NewValue is not ICollection<Scatter> scatters)
return;
myPlot.PlotView.Plot.Clear();
foreach (var scatter in scatters)
{
myPlot.PlotView.Plot.AddScatter(scatter.XDatas, scatter.YDatas);
}
myPlot.PlotView.Refresh();
}
}
<!-- MyPlot를 사용하려는 곳에서 -->
...
<local:MyPlot Scatters="{Binding Scatters}"/>
...
// MyPlot를 사용하려는 곳의 ViewModel
...
// 만약 속성이 변할 수 있으면 ICollection 대신 ObservableCollection이나 BindingList
public ICollection<Scatter> Scatters { get; set; }
...
여기서 나온 Scatter는 제가 임시로 만든 자료구조인데 사용환경에 맞게 작성하면 될 것 같아요.
파일을 압축해서 올리려 했는데 업로드 제한에 걸리네요…
public readonly struct Scatter
{
public double[] XDatas { get; }
public double[] YDatas { get; }
public Scatter(double[] xDatas, double[] yDatas)
{
XDatas = xDatas;
YDatas = yDatas;
}
}