如何使用查询在ASP.NET中创建高级图表?

时间:2017-09-13 11:09:43

标签: asp.net charts

这是我从数据库中获取数据并以表格形式显示的代码。这工作正常,现在我想要一个图表/条形图来显示这个值。我不知道该怎么做。请帮忙。

- (void)addShadowWithRadius:(CGFloat)shadowRadius withShadowOpacity:(CGFloat)shadowOpacity withShadowOffset:(CGSize)shadowOffset withShadowColor:(UIColor *)shadowColor withCornerRadius:(CGFloat)cornerRadius withBorderColor:(UIColor *)borderColor withBorderWidth:(CGFloat)borderWidth forView:(UIView *)view{

    // drop shadow
    [view.layer setShadowRadius:shadowRadius];
    [view.layer setShadowOpacity:shadowOpacity];
    [view.layer setShadowOffset:shadowOffset];
    [view.layer setShadowColor:shadowColor.CGColor];

    // border radius
    [view.layer setCornerRadius:cornerRadius];

    // border
    [view.layer setBorderColor:borderColor.CGColor];
    [view.layer setBorderWidth:borderWidth];
}

1 个答案:

答案 0 :(得分:1)

我使用命名空间 System.Windows.Forms.DataVisualization.Charting; 来创建我的图表并将其保存为.jpg或我想要的任何内容。以下是我使用它的代码示例:

   public static class ChartService
            {
                public static async Task CreateChart(Dictionary<DateTime, int> model)
                {
                    DataSet dataSet = new DataSet();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("Name", typeof(string));
                    dt.Columns.Add("Counter", typeof(int));

                    CultureInfo deDe = new CultureInfo("de-De");

                    foreach (var item in model)
                    {              
                        DataRow r = dt.NewRow();
                        r[0] = item.Key.ToString("MMMM yyyy", deDe);
                        r[1] = item.Value;
                        dt.Rows.Add(r);
                    }
                    dataSet.Tables.Add(dt);

                    //prepare chart control...
                    Chart chart = new Chart();
                    chart.DataSource = dataSet.Tables[0];
                    chart.Width = 780;
                    chart.Height = 250;            
                    //create serie...
                    Series serie1 = new Series();
                    serie1.Name = "Serie1";
                    serie1.Color = Color.FromArgb(255, 127, 102);
                    serie1.BorderColor = Color.FromArgb(255, 127, 102);
                    serie1.ChartType = SeriesChartType.Line;
                    serie1.BorderDashStyle = ChartDashStyle.Solid;
                    serie1.BorderWidth = 3;
                    serie1.ShadowColor = Color.FromArgb(255, 127, 102);
                    serie1.ShadowOffset = 0;            
                    //serie1.IsValueShownAsLabel = true;
                    serie1.XValueMember = "Name";
                    serie1.YValueMembers = "Counter";
                    serie1.Font = new Font("Tahoma", 8.0f);
                    serie1.BackSecondaryColor = Color.FromArgb(255, 127, 102);
                    serie1.LabelForeColor = Color.FromArgb(81, 80, 84);
                    chart.Series.Add(serie1);
                    //create chartareas...
                    ChartArea ca = new ChartArea();
                    ca.Name = "ChartArea1";
                    ca.BackColor = Color.White;
                    ca.BorderWidth = 1;
                    ca.BorderColor = Color.FromArgb(200, 200, 200);           
                    ca.BorderDashStyle = ChartDashStyle.Solid;           
                    ca.AxisX = new Axis();
                    ca.AxisY = new Axis();
                    ca.AxisX.LineColor = Color.FromArgb(171, 171, 171);
                    ca.AxisX.MajorGrid.LineWidth = 0;
                    ca.AxisY.LineColor = Color.FromArgb(171, 171, 171);  
                    ca.AxisY.MajorGrid.LineColor = Color.FromArgb(171, 171, 171);
                    ca.AxisY.LabelStyle.ForeColor = Color.FromArgb(82, 82, 82);
                    ca.AxisX.LabelStyle.ForeColor = Color.FromArgb(82, 82, 82);
                    chart.ChartAreas.Add(ca);
                    //databind...
                    chart.DataBind();
                    //save result...
                    chart.SaveImage("example\\temp\\img\\chart.jpg", ChartImageFormat.Jpeg);
                }
            }

输出是这样的:

chart

希望,这就是你要找的东西。 chears