使用SQL查询绘制图表

时间:2013-10-23 11:05:41

标签: c# sql wpf reporting-services charts

我正在尝试在C#WPF应用程序中创建折线图。 我已经广泛搜索了我的问题的答案。我知道这是一个很长的镜头但是 我很绝望。 SQL查询如下:

SELECT stock_symbol, stock_date, stock_price_adj_close 
FROM MoneyBMine.dbo.NYSE 
WHERE stock_symbol IN ('AA', 'AIT') 
AND stock_date BETWEEN '2000-01-03' AND '2000-02-04'

OR:

SELECT stock_symbol, stock_date, stock_price_adj_close 
FROM MoneyBMine.dbo.NYSE 
WHERE stock_symbol = 'AA' 
AND stock_date BETWEEN '2000-01-03' AND '2000-02-04'

我只是想让我的任何值显示在我的应用程序内的图表上。 我也尝试过ReportViewer并在“Microsoft SQL Server Report Builder”中生成报告,但是再一次尝试将这些报告带入我的C#WPF应用程序中没有任何效果。所以我要问的是在C#WPF应用程序中在某种图表/报表上可视化SQL数据的方法。

1 个答案:

答案 0 :(得分:0)

我正在使用OxyPlot,你可以得到它here ...它比WPF自己的解决方案更好,更容易使用(他们提供的图表dll没有文档)。

一旦拥有它,您可以在代码中执行以下操作:

// Create plot
var plot_model = new PlotModel()  { Title = title};

// Create points
var points = from_a_method_linq_Sql_whatever();
var line_series = new LineSeries();

// Add plot to serie
foreach (var point in points)
    line_series.Points.Add(point);

// Add them to the plot
plot_model.Series.Add(line_series);

我想将这个图作为属性,然后简单地从XAML绑定到它:

<oxy:Plot Model="{Binding OutputChart}" />

(确保你的xaml上有这个:

 xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf"

您还可以将您的积分作为属性公开,并通过xaml上的绑定完成所有操作。你的选择。

添加轴和图例很简单,只需快速浏览一下他们的文档/论坛。

修改
我在我写的一个应用程序中使用了类似的东西:

// Running on a collection of objects that have dates and levels, and other things
foreach (var obj in lvl_reps)
{
    points.Add(new ExercisePoint
    {
        DateTime = exercise.Date,
        Lvl = obj.Level,
        X = DateTimeAxis.ToDouble(exercise.Date.AddHours(-12)),
        Y = obj.Reps.Sum(),
        Exercise = exercise.Exercise
    });
}

不要让ExercisePoint吓到你,这是它的实现:

/// <summary>
/// Extends the normal point used by OxyPlot to include the level and the date.
/// The Idea is to use them on the tool tip of the exercise
/// </summary>
class ExercisePoint : IDataPoint
{
    public double X { get; set; }
    public double Y { get; set; }
    public int Lvl { get; set; }
    public DateTime DateTime { get; set; }
    public string Exercise { get; set; }
}

因此,这就是您可以将自定义字段绑定到点的方法。