用图表绘制调查数据

本文关键字:数据 调查 绘制 用图表 | 更新日期: 2023-09-27 18:11:14

我想用asp.net和c#做一个调查。

很明显,我能做这件事。但是对于结果,我想用饼状图和直方图来展示。

问题只有两个答案"是"answers"否"。在饼状图中,"是"的百分比和"否"的百分比,如%55"是",%45"否"。

在直方图中,我想显示每个问题,它分为两部分("是"answers"否")。

为了做这些(饼图和直方图),我应该使用组件像Telerik吗?或者我可以用。net中的绘图库做到这一点吗?

用图表绘制调查数据

您可以使用Asp.net Chart控件在饼图......中表示数据这也取决于数据绑定。

如果你正在使用datatable从数据库中获取数据,那么asp.net chart控件会更好…

请查看此链接以获取更多信息....

https://web.archive.org/web/20211020111731/https://www.4guysfromrolla.com/articles/120804 - 1. - aspx

编辑

这个函数从数据库中获取数据。

     public  DataTable GetVisits(System.DateTime startdate , System.DateTime enddate)
     { 
         const string sql  = @"SELECT CONCAT(UPPER(SUBSTRING(visit_Status, 1, 1)), SUBSTRING(visit_Status FROM 2))  as Status, COUNT('x') AS Visits
                              FROM visits
                              WHERE visit_Date BETWEEN @startdate AND @enddate
                              GROUP BY visit_Status";
         return sqlexecution(startdate, enddate, sql);
     }

我用堆栈柱状图表示这些数据。

如果想用饼状图表示,可以在代码

中更改
     public void DrawMembersvisits(Chart targetchartcontrol, DateTime startdate, DateTime enddate)
    {
        chart1 = targetchartcontrol;
        Series series = null;
        Title title;
        string area = null;
        chart1.ChartAreas.Clear();
        chart1.Series.Clear();
        chart1.Titles.Clear();
        DataTable membervisits = null;
        area = "Visits";
        chart1.ChartAreas.Add(area);
        series = chart1.Series.Add(area);
        series.ChartArea = area;
        title = chart1.Titles.Add("Member Visits");

       title.Alignment = ContentAlignment.MiddleCenter;
       title.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);

        title.DockedToChartArea = area;
        chart1.Titles.Add("").DockedToChartArea = area;
        foreach (Title titles in chart1.Titles)
        {
            titles.IsDockedInsideChartArea = false;
        }
        foreach (ChartArea areas in chart1.ChartAreas)
        {
            areas.Area3DStyle.Enable3D = true;
            areas.AxisX.LabelStyle.IsEndLabelVisible = false;                                                                                                               areas.AxisX.LabelStyle.Angle = -90;
            areas.AxisX.LabelStyle.IsEndLabelVisible = true;
            areas.AxisX.LabelStyle.Enabled = true;

        }
        foreach (Legend legends in chart1.Legends)
        {
            legends.Enabled = false;
        }
        foreach (Series serie in chart1.Series)
        {
            serie.ChartType = SeriesChartType.StackedColumn; 
           // change here to get the pie charts
              // charttypes.ChartType = SeriesChartType.Pie;
              // charttypes["LabelStyle"] = "Outside";
              // charttypes["DoughnutRadius"] = "30";
             //  charttypes["PieDrawingStyle"] = "SoftEdge";
             //  charttypes.BackGradientStyle = GradientStyle.DiagonalLeft;

                serie["LabelStyle"] = "Outside";
               serie["ColumnDrawingStyle"] = "SoftEdge";
            serie["LabelStyle"] = "Top";
            serie.IsValueShownAsLabel = true;
            serie.BackGradientStyle = GradientStyle.DiagonalLeft;
        }

        membervisits = visistsdataf.GetVisits(startdate, enddate);
        chart1.Series[0].Points.DataBindXY(membervisits.Rows, "Status", membervisits.Rows, "Visits");
        foreach (Series chartSeries in chart1.Series)
        {
            foreach (DataPoint point in chartSeries.Points)
            {
                switch (point.AxisLabel)
                {
                    case "Accepted": point.Color = Color.Green; break;
                    case "Refused": point.Color = Color.Red; break;
                }
                point.Label = string.Format("{0:0} - {1}", point.YValues[0], point.AxisLabel);
            }
        }

    }