javascript - 如何使用 ASP.net、C# 或 Javascript 创建箱线图

标签 javascript c# asp.net .net charts

有人知道如何使用 ASP.net、C# 或 JavaScript 创建箱线图吗?

这是我在网上找到的,但我不确定如何将其应用到我的案例中:

代码:

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;

如果有人能帮助我,谢谢!

最佳答案

这是一个 ASP.NET 示例:

    <asp:Chart ID="Chart1" runat="server" Width="600px">
        <Series>
            <asp:Series Name="Series1" ChartType="BoxPlot" YValuesPerPoint="6">
                <Points>
                    <asp:DataPoint XValue="1" YValues="10,60,20,50,30,40" />
                    <asp:DataPoint XValue="2" YValues="40,90,50,80,60,70" />
                    <asp:DataPoint XValue="3" YValues="20,70,30,60,40,50" />
                </Points>
            </asp:Series>
        </Series>
        <ChartAreas>
            <asp:ChartArea Name="ChartArea1">
                <AxisY>
                    <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                    <MajorTickMark LineColor="DarkGray" LineDashStyle="Dot" />
                </AxisY>
                <AxisX>
                    <MajorGrid LineColor="DarkGray" LineDashStyle="Dot" />
                    <MajorTickMark LineColor="DarkGray" LineDashStyle="Dot" />
                </AxisX>
            </asp:ChartArea>
        </ChartAreas>
    </asp:Chart>

enter image description here

您可以在此处查找详细信息:MSDN - Box Plot Chart

编辑: 您可以像这样动态绑定(bind)数据:

    protected void Page_Load(object sender, EventArgs e)
    {
        MyDataCollection data = new MyDataCollection();
        Chart1.Series[0].Points.DataBind(data, "XValue", "LowWhisker,UpWhisker,LowWBox,UpBox,Average,Median", null);
    }

public class MyDataCollection : List<MyData>
{
    public MyDataCollection()
    {
        Add(new MyData { XValue = 1, LowWhisker = 10, UpWhisker = 60, LowWBox = 20, UpBox = 50, Average = 30, Median = 40 });
        Add(new MyData { XValue = 2, LowWhisker = 40, UpWhisker = 90, LowWBox = 50, UpBox = 80, Average = 60, Median = 70 });
        Add(new MyData { XValue = 3, LowWhisker = 20, UpWhisker = 70, LowWBox = 30, UpBox = 60, Average = 40, Median = 50 });
    }
}

public class MyData
{
    public double XValue { get; set; }
    public double LowWhisker { get; set; }
    public double UpWhisker { get; set; }
    public double LowWBox { get; set; }
    public double UpBox { get; set; }
    public double Average { get; set; }
    public double Median { get; set; }
}

每当您向集合中添加/删除数据时,您都需要调用 DataBind()

编辑2:您可以通过相应地修改XValuestring来使MyData成为MyDataCollection:

enter image description here

编辑 3:使用 DataTable

    protected void Page_Load(object sender, EventArgs e)
    {
        MyDataTable dt = new MyDataTable();

        foreach (DataRow row in dt.Rows)
            Chart1.Series[0].Points.AddXY(row["Status"], new object[] { row["Min"], row["Max"], row["Avg"], row["Percentile25"], row["Percentile50"], row["Percentile75"] });
    }

public class MyDataTable : DataTable
{
    public MyDataTable()
    {
        Columns.Add("Title", typeof(string));
        Columns.Add("Status", typeof(string));
        Columns.Add("Min", typeof(double));
        Columns.Add("Max", typeof(double));
        Columns.Add("Avg", typeof(double));
        Columns.Add("Percentile25", typeof(double));
        Columns.Add("Percentile50", typeof(double));
        Columns.Add("Percentile75", typeof(double));

        DataRow row = NewRow();
        row["Status"] = "Status 1";
        row["Min"] = 10;
        row["Max"] = 60;
        row["Avg"] = 20;
        row["Percentile25"] = 50;
        row["Percentile50"] = 30;
        row["Percentile75"] = 40;
        Rows.Add(row);

        row = NewRow();
        row["Status"] = "Status 2";
        row["Min"] = 40;
        row["Max"] = 90;
        row["Avg"] = 50;
        row["Percentile25"] = 80;
        row["Percentile50"] = 60;
        row["Percentile75"] = 70;
        Rows.Add(row);

        row = NewRow();
        row["Status"] = "Status 3";
        row["Min"] = 20;
        row["Max"] = 70;
        row["Avg"] = 30;
        row["Percentile25"] = 60;
        row["Percentile50"] = 40;
        row["Percentile75"] = 50;
        Rows.Add(row);
    }
}

enter image description here

关于javascript - 如何使用 ASP.net、C# 或 Javascript 创建箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32733807/

相关文章:

javascript - 将 javascript 中的数组数组传递给字段

c# - 如何删除变量的参数约束定义,以便断言在 Rhino.Mocks 中变得不那么拥挤?

c# - 使用 MultipartFormDataContent 生成错误的 Content-Type header

c# - 如何从 View asp.net-mvc-3 返回数组

c# - 日期格式问题,String 未被识别为有效的 DateTime

javascript - 使用新值从 JavaScript 调用 ASP.NET 查看功能

asp.net - 在asp.net中使用href值在gridview列中显示图像图标

javascript - 使用 jQuery .click 从一些 CSS 类更改为其他 CSS 类而不进行切换?

javascript - 手动向 mvc 表单添加不显眼的验证

javascript - 在 JavaScript 警报框中调用我的函数不起作用