C#-在Oxyplot中创建箱线图

C#-在Oxyplot中创建箱线图,c#,data-binding,boxplot,oxyplot,C#,Data Binding,Boxplot,Oxyplot,我试图习惯Oxyplot,在我的C#应用程序中创建一些图表。所以我想用OxyPlot创建一个箱线图,但是我不能得到任何结果。我尝试了“入门”示例,没有遇到任何问题,但现在我不知道如何将数据绑定到BoxplotSeries。我阅读了GitHub上关于BoxPlotSeries的文档,但是没有列出太多的信息,示例对我也没有帮助。有人能给我举个例子,如何使用BoxPlotSeries并将数据绑定到它吗 非常感谢。我不知道您是否希望在WPF/XAML中执行此操作,如果答案是肯定的,则您目前无法执行此操作

我试图习惯Oxyplot,在我的C#应用程序中创建一些图表。所以我想用OxyPlot创建一个箱线图,但是我不能得到任何结果。我尝试了“入门”示例,没有遇到任何问题,但现在我不知道如何将数据绑定到
BoxplotSeries
。我阅读了GitHub上关于
BoxPlotSeries
的文档,但是没有列出太多的信息,示例对我也没有帮助。有人能给我举个例子,如何使用
BoxPlotSeries
并将数据绑定到它吗


非常感谢。

我不知道您是否希望在WPF/XAML中执行此操作,如果答案是肯定的,则您目前无法执行此操作,因为BoxPlot系列没有WPF包装

我自己尝试移植它,但遇到了一些问题,如果您想提供帮助,请在GitHub上查看以下问题:


编辑:我刚刚提交了一个拉取请求,您应该能够使用WPF中的箱线图

因此我找到了一种使用OxyPlot创建箱线图的方法。只需使用OxyPlot的类BoxPlotSeries,一个包含Boxplots和一点数学知识的列表对象。我使用x轴的类别来区分箱线图

因此,对于未来,如果有人遇到同样的问题,下面是代码

 public class Item
{
    #region Public Properties

    public string Label { get; set; }

    #endregion Public Properties
}

public class BoxPlotSeriesExample
{
    #region Public Constructors

    public BoxPlotSeriesExample()
    {
    }

    #endregion Public Constructors

    #region Public Methods

    public PlotModel createBoxPlot()
    {
        const int boxes = 16;
        var plot = new PlotModel();
        var items = new Collection<Item>();

        for (int i = 1; i < boxes + 1; i++)
        {
            items.Add(new Item { Label = i.ToString() });
        }

        plot.Axes.Add(new LinearAxis
        {
            Position = AxisPosition.Left,
            MajorStep = 1,
            MinorStep = 0.25,
            TickStyle = TickStyle.Crossing,
            AbsoluteMaximum = 5.25,
            AbsoluteMinimum = -0.25
        });

        plot.Axes.Add(new CategoryAxis
        {
            Position = AxisPosition.Bottom,
            ItemsSource = items,
            LabelField = "Label",
            IsTickCentered = true,
            TickStyle = TickStyle.None,
            AbsoluteMinimum = -1,
            AbsoluteMaximum = 17,
            IsZoomEnabled = false
        });

        var lineAnnotation = new LineAnnotation
        {
            Type = LineAnnotationType.Horizontal,
            Y = 5,
            LineStyle = LineStyle.Dash,
            StrokeThickness = 2,
            Color = OxyColor.FromArgb(50, 0, 0, 0)
        };
        plot.Annotations.Add(lineAnnotation);

        lineAnnotation = new LineAnnotation
        {
            Type = LineAnnotationType.Horizontal,
            Y = 1,
            LineStyle = LineStyle.Dash,
            StrokeThickness = 1.5,
            Color = OxyColor.FromArgb(50, 0, 0, 0)
        };
        plot.Annotations.Add(lineAnnotation);

        lineAnnotation = new LineAnnotation
        {
            Type = LineAnnotationType.Horizontal,
            Y = 4,
            LineStyle = LineStyle.Solid,
            StrokeThickness = 1.5,
            Color = OxyColor.FromArgb(50, 0, 0, 0)
        };
        plot.Annotations.Add(lineAnnotation);

        lineAnnotation = new LineAnnotation
        {
            Type = LineAnnotationType.Horizontal,
            Y = 2,
            LineStyle = LineStyle.Solid,
            StrokeThickness = 1.5,
            Color = OxyColor.FromArgb(50, 0, 0, 0)
        };
        plot.Annotations.Add(lineAnnotation);

        var s1 = new BoxPlotSeries();
        s1.Fill = OxyColor.FromRgb(0x1e, 0xb4, 0xda);
        s1.StrokeThickness = 1.1;
        s1.WhiskerWidth = 1;
        var random = new Random();
        for (int i = 0; i < boxes; i++)
        {
            double x = i;
            var points = 5 + random.Next(15);
            var values = new List<double>();
            for (int j = 0; j < points; j++)
            {
                values.Add((random.NextDouble()) * 5);
            }

            values.Sort();
            var median = getMedian(values);
            int r = values.Count % 2;
            double firstQuartil = getMedian(values.Take((values.Count + r) / 2)); // 25%-Quartil
            double thirdQuartil = getMedian(values.Skip((values.Count - r) / 2)); // 75%-Quartil

            var iqr = thirdQuartil - firstQuartil; // Quartilabstand
            var step = 1.5 * iqr;
            var upperWhisker = thirdQuartil + step;
            upperWhisker = values.Where(v => v <= upperWhisker).Max();
            var lowerWhisker = firstQuartil - step;
            lowerWhisker = values.Where(v => v >= lowerWhisker).Min();
            var outliers = values.Where(v => v > upperWhisker || v < lowerWhisker).ToList();

            s1.Items.Add(new BoxPlotItem(x, lowerWhisker, firstQuartil, median, thirdQuartil, upperWhisker, outliers));
        }

        plot.Series.Add(s1);
        return plot;
    }

    #endregion Public Methods

    #region Private Methods

    private static double getMedian(IEnumerable<double> values)
    {
        var sortedInterval = new List<double>(values);
        sortedInterval.Sort();
        var count = sortedInterval.Count;
        if (count % 2 == 1)
        {
            return sortedInterval[(count - 1) / 2];
        }

        return 0.5 * sortedInterval[count / 2] + 0.5 * sortedInterval[(count / 2) - 1];
    }

    #endregion Private Methods
}
公共类项目
{
#区域公共财产
公共字符串标签{get;set;}
#端域公共属性
}
公共类BoxPlot系列示例
{
#地区公共建设者
public BoxPlotSeriesExample()
{
}
#端域公共构造函数
#区域公共方法
公共PlotModel createBoxPlot()
{
常数整型框=16;
变量绘图=新的绘图模型();
var items=新集合();
对于(int i=1;iv>=lowerWhisker).Min();
var异常值=值。其中(v=>v>upperWhisker | | v