C#中一个图表中的多个箱图

时间:2013-12-22 17:04:17

标签: c# charts boxplot microsoft-chart-controls

我正在试图弄清楚如何让c#在图表中放入多个箱图。一旦涉及到C#而不是R或python,在互联网上没有太多帮助。所以我问:我在这段代码中做错了什么或错过了什么?

   private void on_boxplot(object sender, EventArgs e)
    {                  
        List<String> xValue = new List<String> { "Ala (A)", "Arg (R)", "Asn (N)", "Asp (D)", "Cys (C)", "Gln (Q)", "Glu (E)", "Gly (G)", "His (H)", "Ile (I)", "Leu (L)", "Lys (K)", "Met (M)", "Phe (F)", "Pro (P)", "Ser (S)", "Thr (T)", "Trp (W)", "Tyr (Y)", "Val (V)", "Pyl (O)", "Sec (U)" };
        Chart Chart = new Chart();
        Chart.chart_main.Series.Clear();
        Chart.chart_main.Series.Add("BoxPlotSeries");
        for (Int32 i = 0; i < xValue.Count; i++)
        {
            Chart.chart_main.Series.Add(xValue[i]); 
        }

        for (Int32 i = 0; i < DYL; i++)
        {
            if (Data[i, 0] == null) break;
            Chart.chart_main.Series[xValue[0]].Points.AddY(boxplot_helper(i, Dataslots[0]));
            Chart.chart_main.Series[xValue[1]].Points.AddY(boxplot_helper(i, Dataslots[1]));
            Chart.chart_main.Series[xValue[2]].Points.AddY(boxplot_helper(i, Dataslots[2]));
            Chart.chart_main.Series[xValue[3]].Points.AddY(boxplot_helper(i, Dataslots[3]));
            Chart.chart_main.Series[xValue[4]].Points.AddY(boxplot_helper(i, Dataslots[4]));
            Chart.chart_main.Series[xValue[5]].Points.AddY(boxplot_helper(i, Dataslots[5]));
            Chart.chart_main.Series[xValue[6]].Points.AddY(boxplot_helper(i, Dataslots[6]));
            Chart.chart_main.Series[xValue[7]].Points.AddY(boxplot_helper(i, Dataslots[7]));
            Chart.chart_main.Series[xValue[8]].Points.AddY(boxplot_helper(i, Dataslots[8]));
            Chart.chart_main.Series[xValue[9]].Points.AddY(boxplot_helper(i, Dataslots[9]));
            Chart.chart_main.Series[xValue[10]].Points.AddY(boxplot_helper(i, Dataslots[10]));
            Chart.chart_main.Series[xValue[11]].Points.AddY(boxplot_helper(i, Dataslots[11]));
            Chart.chart_main.Series[xValue[12]].Points.AddY(boxplot_helper(i, Dataslots[12]));
            Chart.chart_main.Series[xValue[13]].Points.AddY(boxplot_helper(i, Dataslots[13]));
            Chart.chart_main.Series[xValue[14]].Points.AddY(boxplot_helper(i, Dataslots[14]));
            Chart.chart_main.Series[xValue[15]].Points.AddY(boxplot_helper(i, Dataslots[15]));
            Chart.chart_main.Series[xValue[16]].Points.AddY(boxplot_helper(i, Dataslots[16]));
            Chart.chart_main.Series[xValue[17]].Points.AddY(boxplot_helper(i, Dataslots[17]));
            Chart.chart_main.Series[xValue[18]].Points.AddY(boxplot_helper(i, Dataslots[18]));
            Chart.chart_main.Series[xValue[19]].Points.AddY(boxplot_helper(i, Dataslots[19]));
            Chart.chart_main.Series[xValue[20]].Points.AddY(boxplot_helper(i, Dataslots[20]));
            Chart.chart_main.Series[xValue[21]].Points.AddY(boxplot_helper(i, Dataslots[21]));     
        }

        Chart.chart_main.Series["BoxPlotSeries"].ChartType = SeriesChartType.BoxPlot;
        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotSeries"] = xValue[0];

        Chart.chart_main.ChartAreas.Add("BoxPlot");
        Chart.chart_main.Series["BoxPlotSeries"].ChartArea = "BoxPlot";

        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotWhiskerPercentile"] = "0";
        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotPercentile"] = "25";
        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotShowAverage"] = "true";
        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotShowMedian"] = "true";
        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotShowUnusualValues"] = "true";
        Chart.chart_main.Series["BoxPlotSeries"]["MaxPixelPointWidth"] = "15";
        Chart.chart_main.Series["BoxPlotSeries"].BorderWidth = 2;
        Chart.Show();

    }

    private Double boxplot_helper(Int32 i, Int32 slot)
    {
        String Santas = Data[i, slot].Replace('.', ',').TrimEnd('%').Trim();
        Double LittleHelper = Convert.ToDouble(Santas);
        return LittleHelper;

    }

enter image description here

以上是结果,但我想要22个。任何人都碰巧有任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

您可以创建多个箱图

Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotSeries"] = string.Join(";", xValue);

答案 1 :(得分:2)

有人可能对我所做的事感兴趣:

        List<String> Series = new List<String>();
        for (Int32 i = 0; i < 22; i++)
        {
            Series.Add(xValue[i]);
        }
        String Helper = String.Join("; ", Series);

        Chart.chart_main.Series["BoxPlotSeries"]["BoxPlotSeries"] = Helper;

结果:

enter image description here