【问题标题】:How to populate a Chart ( Using System.Web.Helpers.Chart ) in LinqPad?如何在 LinqPad 中填充图表(使用 System.Web.Helpers.Chart)?
【发布时间】:2012-04-02 11:02:09
【问题描述】:

我有下面的代码,在 LinqPad 4.40.03 + Sql Server 2008 R2 + NorthWind 中运行。

LinqPad 返回异常:“ArgumentNullException:值不能为空。参数名称:httpContext”。

请给我最终固定代码,我希望它在 linqpad 输出屏幕中填充图表(使用 System.Web.Helpers.Chart)。

void Main()
{
    var q = from p in Products
            let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
            orderby s
            select new { ProductName = p.ProductName, Sales = s};

    var basicChart = new System.Web.Helpers.Chart(width: 600, height: 400)
        .AddTitle("Product Sales")
        .DataBindTable(dataSource: q, xField: "ProductName")
        .Write();

    basicChart.Dump();
}

【问题讨论】:

    标签: linqpad


    【解决方案1】:

    ASP.NET 图表控件依赖于 HttpContext.Current,它仅在运行 ASP.NET 应用程序时存在。

    您可以尝试mocking an HttpContext,或者改用System.Windows.Forms.DataVisualization.Charting 中的图表控件:

    var q = from p in Products
        let s = p.OrderDetails.Sum(o => o.Quantity) * p.UnitPrice
        orderby s
        select new { ProductName = p.ProductName, Sales = s};
    
    var chart = new Chart();
    
    var ca = new ChartArea();
    ca.AxisX.LabelStyle.Interval = 1;
    chart.ChartAreas.Add (ca);
    
    var series = new Series { ChartType = SeriesChartType.Bar};
    series.Points.DataBind (q.Take(20), "ProductName", "Sales", null);
    chart.Series.Add (series);
    chart.Dump ("Chart");
    

    【讨论】:

    • 感谢伟大的乔!图表很漂亮。 LinqPad 非常强大。
    • 嗨乔,我模拟了一个 HttpContext(使用 Moq),然后花了很多时间尝试使用 System.Web.Helpers.Chart 在 LinqPad 中填充聊天,但仍然无法使其工作。可以给我上面的最终代码吗,谢谢!
    猜你喜欢
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多