WPF OxyPlot:如何为用户提供要显示哪个系列的选项?

问题描述 投票:1回答:2

我想向用户列出可供选择的系列列表(可能直接点击图例?)该图表仅显示所选系列

实现这一目标的最佳方法是什么?

wpf oxyplot
2个回答
2
投票

我建议你使用代表那些系列的复选框。当用户选中复选框时,您可以将新LineSeries或任何其他系列添加到PlotModel / PlotView。

checkbox1代表series1。 checkbox2代表series2。等等..

例如:

Plotview pv = new PlotView(); // you can also use plotmodel instead of plotview.
  pv.Width = 480.0;
  pv.Height = 272.0;
  pv.Title = graphname;
private void checkbox4_Checked(object sender, RoutedEventArgs e)
{
  var LineChart = new LineSeries();

        pv.Series.Add(LineChart);

        LineChart.DataFieldX = "X";
        LineChart.DataFieldY = "Y";

        List<Vector> coords = new List<Vector>(); 
        //Add X and Y values to this list. Or you can use any other way you want.

        LineChart.ItemsSource = coords; //Feed it to itemsource
        LineChart.Title = name;
}

0
投票

这是我与同事一起找到的MVVM解决方案,允许用户选择要显示的系列:

Firstable我们在xaml中创建一个PlotView,其中ItemsControl绑定在PlotModel.Series上:

<oxy:PlotView Background="Transparent" Model="{Binding PlotModelCompareChart}" x:Name="CompareChart"/>
        <Border Grid.Column="1" Margin="0,150,0,0" BorderBrush="Gray" BorderThickness="1" Height="80">
            <ItemsControl ItemsSource="{Binding PlotModelCompareChart.Series}" Margin="5" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel IsItemsHost="True">
                        </StackPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type oxy:Series}">

                        <StackPanel Orientation="Horizontal">
                            <CheckBox IsChecked="True" Tag="{Binding ElementName=CompareChart}"  >
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding ElementName=userControl, Path=DataContext.ManageCheckBoxCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=CheckBox}}" ></i:InvokeCommandAction>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{Binding ElementName=userControl, Path=DataContext.ManageCheckBoxCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=CheckBox}}"></i:InvokeCommandAction>
                                    </i:EventTrigger>

                                </i:Interaction.Triggers>
                            </CheckBox>
                            <Rectangle Width="15" Height="3.5"  Margin="5,0,0,0" Fill="{Binding Color, Converter={StaticResource OxyColorToSolidColorBrushConverter}}"/>
                            <TextBlock Text="{Binding Title}" Margin="5,0,0,0" ></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Border>

转换OxyColor接收转换器填充矩形(你必须设置你的系列的颜色,如果你让Oxyplot选择颜色,它不起作用):

public class OxyColorToSolidColorBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (null == value)
        {
            return null;
        }

        if (value is OxyColor)
        {
            var color = (SolidColorBrush)new BrushConverter().ConvertFrom(value.ToString());
            return color;
        }

        Type type = value.GetType();
        throw new InvalidOperationException("Unsupported type [" + type.Name + "]");
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

现在ViewModel中的代码:

--PlotModel Creation -

 public PlotModel PlotModelCompareChart => CreatePlotModelForCompareChart(DataPoints);
 public PlotModel CreatePlotModelForCompareChart(StripLengthReferentialDataPointCollection points)
    {
        var plotModel= new PlotModel();
        plotModel.IsLegendVisible = false;
        var linearAxis = new LinearAxis
        {
            Position = AxisPosition.Bottom,
            MajorGridlineStyle = LineStyle.Solid,
            Title = LabelsAndMessagesRes.StripLengthReferentialChart_Legend_HeadPosition,
            Unit = PhysicalQuantityManager.Instance.LengthAcronym
        };
        var linearAxis2 = new LinearAxis
        {
            Position = AxisPosition.Left,
            Title = LabelsAndMessagesRes.Lexicon_Temperature,
            Unit = PhysicalQuantityManager.Instance.TemperatureAcronym
        };

        var linearAxis3 = new LinearAxis
        {
            Position = AxisPosition.Right,
            Maximum = 70,
            Key = "ValveAxis"
        };


        plotModel.Axes.Add(linearAxis);
        plotModel.Axes.Add(linearAxis2);
        plotModel.Axes.Add(linearAxis3);


        var lineSeries = new LineSeries();
        lineSeries.Title = LabelsAndMessagesRes.StripLengthReferentialChart_Legend_MeasuredCoilingTemperature;
        lineSeries.DataFieldX = "HeadPosition";
        lineSeries.DataFieldY = "MeasuredCoilingTemperature";
        lineSeries.ItemsSource = points;
        lineSeries.Color = OxyColors.Green;

        var lineSeries2 = new LineSeries();
        lineSeries2.Title = "All valves count";
        lineSeries2.DataFieldX = "HeadPosition";
        lineSeries2.DataFieldY = "AllValvesCount";
        lineSeries2.YAxisKey = "ValveAxis";
        lineSeries2.ItemsSource = points;
        lineSeries2.Color = OxyColors.LightSeaGreen;

        plotModel.Series.Add(lineSeries);
        plotModel.Series.Add(lineSeries2);


        return plotModel;

    }

- 复选框事件的命令和方法(未选中和已检查)

  public ICommand ManageCheckBoxCommand { get; private set; }

在Ctor:

 ManageCheckBoxCommand = new GalaSoft.MvvmLight.CommandWpf.RelayCommand<object>(ManageCheckBoxMethod);

方法 :

 private void ManageCheckBoxMethod(object obj)
    {
        if(obj != null)
        {
            var checkbox = obj as CheckBox;
            var datacontext = checkbox.DataContext as Series;
            datacontext.IsVisible = checkbox.IsChecked.Value;
            var plotView = checkbox.Tag as OxyPlot.Wpf.PlotView;
            plotView.Model = null;
            plotView.Model = datacontext.PlotModel;
        }

    }

现在的结果是:Options checked

Options not checked

希望能帮助到你 !

© www.soinside.com 2019 - 2024. All rights reserved.