WFA中如何给BoxPlot添加数据?

How to add data to BoxPlot in WFA?

我正在手动创建箱线图。我有 4 个 double[] 数组,其中包含一些我想在图表上显示的计算结果。我不知道如何将我的数组与图表系列正确连接。 这是我的图表:

        Chart chart = new Chart();
        chart.Series.Add("S1");
        chart.Series.Add("S2");
        chart.Series.Add("S3");
        chart.Series.Add("S4");
        chart.ChartAreas.Add("ChartArea1");
        chart.ChartAreas[0].Visible = true;
        chart.ChartAreas[0].Position.Auto = true;
        chart.Series[0].ChartType = SeriesChartType.BoxPlot;
        chart.Series[1].ChartType = SeriesChartType.BoxPlot;
        chart.Series[2].ChartType = SeriesChartType.BoxPlot;
        chart.Series[3].ChartType = SeriesChartType.BoxPlot;

        chart.Parent = this;
        chart.Visible = true;
        double[] yValues = { 2, 3, 4, 5, 4, 5, 5, 2, 1, 9, 20, 4 };//example values
        chart.Series["S1"].Points.DataBindY(yValues);

这是我得到的:

结果我想要这样的东西:

您已尝试将数据绑定到 BoxPlot 系列。但这只导致绑定了第一个 Y 值,这意味着你创建了一组 'lower whiskers'。所有其他 5 个 Y 值都是空的,即 0。因此,您看到的图形很少..

MSDN: The values for a box are typically calculated values from data that is present in another series. One box symbol (DataPoint object) is associated with one data series.

The data for a Box Plot series may still be populated using data binding, or by employing the Series.Points member (a DataPointCollection object).

让我们看看所有这些选项:

  1. 使用常规数据绑定。这是您尝试过的方法,但语法错误。

  2. 您也可以用AddYAddXY将箱形图系列本身的DataPoints一一添加,提供所有6个Y-Values,即输入统计分析的结果。这里只使用了一个数组,它包含六个 y 值。

  3. 或者您可以使用一个或多个数据系列,让图表在每个系列的一个框中汇总这些数据。该系列非常正常,读取可能是 PointLine 或其他任何内容。它们甚至可以是不可见的,当然您可以为它们使用数据绑定。-一旦一些数据系列就位,您可以将 BoxPlot 系列和 'bind' 定义为数据系列,方法是将其 ["BoxPlotSeries"] 特殊 属性 设置为将系列名称连接到其中的字符串...

选项 1。使用常规数据绑定来提供您拥有的统计信息。

这是您尝试过的方法。不过,正确的方法有点令人惊讶;您的数据需要这样排序:

外层数组(或IEnumerable)必须有六个y值;六个内部数组应该包含一个值,对应于您要在框中显示的每个数据集。让我们看一个包含三个伪造统计数据集的示例:

double[][] yValues = {
            new[]{ 15.6, 24.4, 36.1 },  // Lower whiskers
            new[]{ 46.2, 52.2, 91.9 },  // Upper whiskers
            new[]{ 22.3, 27.2, 55.9 },  // Lower boxes
            new[]{ 33.2, 44.4, 77.9 },  // Upper boxes
            new[]{ 25.2, 38.4, 68.5 },  // Averages and means
            new[]{ 27.4, 32.4, 66.9 }   // Medians
        };

这是绑定到 BoxPlot 系列后的样子 S1

S1.Points.DataBindY(yValues);

您还可以创建个人系列;请参阅底部的更新!

选项 2:自己输入 BoxPlot 数据

让我们看一下第一种方式的例子:这里我们需要准备好统计数据。首先我创建一个随机数据结构;它是 double arraysList,每个元素有 6 个元素:

Random R = new Random(23);
List<double[]> yValues = new List<double[]>();
for (int i = 0; i < 8; i++)
{
    { R.Next(5),      R.Next(5) + 20, R.Next(5) + 3, 
      R.Next(5) + 10, R.Next(5) +  5, R.Next(5) + 7 });
}

现在我们将这些虚假统计数据添加到 BoxPlot 系列中:

S1.ChartType = SeriesChartType.BoxPlot;
S1.Points.Clear();
for (int i = 0; i < yValues.Count; i++ ) S1.Points.Add(new DataPoint(i, yValues[i]));

请注意,每个 DataPoint 都是从 6 个双精度数组创建的!

结果如下:

图表现在显示 8 个数据集的统计数据,全部是伪造的 ;-)

选项 3a:将一些数据系列与 BoxPlot 相关联并让它进行数学运算

另一个用法是让图表成为数学:它可以计算你拥有的任意数量的数据系列的统计数据,并创建一个[=每个 152=] 框。

我们将使用与之前相同的数据集,但现在它们用于创建 6 个数据系列,每个系列有 8 个点:

for (int i = 0; i < 6; i++)
{
    Series ds = chart.Series.Add("D" + (i+1));  // set a name D1, D2..
    dx.ChartType = SeriesChartType.Line;
    dx.Points.DataBindY(yValues.Select(x => x[i]).ToArray());
}

它们绑定到与上面相同的数字,但它们现在具有不同的含义。因此,结果将会而且应该 而不是 看起来一样!!

事实上,您可以查看方框和图表,看看它们如何完美契合。

注意Names我给出的数据系列;我们现在将需要它们,当我们 'bind' 它们进入 BoxPlot 系列时:

S1.ChartType = SeriesChartType.BoxPlot;
S1["BoxPlotSeries"] = "D1;D2;D3;D4;D5;D6";  // box plot binding!
S1.LegendText = "BoxPlot";

我对 'binding' 使用引号,因为它不是 真正的 数据绑定;相反,数据系列仅 与具有特殊 属性 字符串 "BoxPlotSeries".

BoxPlot 系列相关联

您的示例有多个 BoxPlot 系列;这里适用相同的规则。

一定要看看 ,它显示了 BoxPlot 的另一种用法,包括设置单独的颜色..

选项 3b:将一些数据系列与您添加到 BoxPlot 系列的数据点相关联;在这里它也会为我们计算

虽然选项 3a 看起来相当简单,但我找不到给方框着色的方法。以下是我们如何做到这一点:

首先,我们强制图表将默认颜色复制到系列中。在我们这样做的同时,让我们也隐藏 Legend 项:

S1.Color = Color.Transparent;
S1.LegendText = " ";
chart.ApplyPaletteColors()

然后我们为每个数据系列创建一个DataPoint in out BoxPlot系列S1;不是我的系列是如何排序的:0=箱线图,1-6 一些数据系列!您可能需要对此进行调整!

for (int i = 1; i < chart.Series.Count; i++)
{
    DataPoint dp = new DataPoint();
    S1.Points.Add(dp);
    dp["BoxPlotSeries"] =  "D" + i;  // names D1-D6
    dp.Color = chart.Series[i].Color;
}

结果如下:

更新:

您的示例实际上显示了三个箱线图系列;因此是颜色,最值得注意的是:集群(即无缝)显示。

这将是将上述数据(来自选项 1)绑定到三个单独的框的方法:

for (int i = 0; i < 3; i++)
{
    Series bps = chart.Series.Add("BoxPlotSeries" + i);
    bps.ChartType = SeriesChartType.BoxPlot;
    var yValOne = yValues.Select(x => new[] { x[i] }).ToArray();
    bps.Points.DataBindY(yValOne);
}

最后说明:您的示例代码包含一个包含 12 个双精度数和 4 个箱线图系列的数组。这是没有意义的。不过,您可以将这 12 个值添加到一个正常系列,并使用选项 3 将其与一个箱线图系列相关联..