10

我的任务是在 Windows 窗体中使用 MSChart 制作烛台图。我已经成功地制作了一个没有问题的 3D 条形图。但是在互联网上搜索了很长时间,微软的源代码(WinSamples)和很多令人头疼的问题后,我找不到创建烛台图的正确方法。

可以帮助我的是一个明确的例子,它向图表中添加了一个带有多个 Y 值或更正我的代码的系列(当我运行时,除了图例标签之外,调试没有任何显示)。

一个好处是该示例基于 OleDB(我的值在 Access 数据库中)。

所以我的问题是:如果你有在 Windows 窗体中用 C# 创建烛台图表的经验,你能给我一个提示,或者(甚至更好)你能给我一些 C# 代码吗?

这是我当前(不工作)的代码:

using System.Windows.Forms.DataVisualization.Charting;
public partial class CandleStick : Form
{
    public CandleStick()
    {
        InitializeComponent();
    }

    private void CandleStick_Load(object sender, EventArgs e)
    {
        GrafiekLaden();
    }

    public void GrafiekLaden()
    {

        Koers k = new Koers();
        // This method fills up a list, the data comes from my database
        // it contains Date, High, Low, Open, Close
        k.meerdereOphalen();

        Series price = new Series();
        chart1.Series.Add(price);

        // Set series chart type
        chart1.Series["price"].ChartType = SeriesChartType.Candlestick;

        // Set the style of the open-close marks
        chart1.Series["price"]["OpenCloseStyle"] = "Triangle";

        // Show both open and close marks
        chart1.Series["price"]["ShowOpenClose"] = "Both";

        // Set point width
        chart1.Series["price"]["PointWidth"] = "1.0";

        // Set colors bars
        chart1.Series[0]["PriceUpColor"] = "Green";
        chart1.Series[0]["PriceDownColor"] = "Red";

        for (int i = 0; i < k.Lijst.Count; i++)
        {
            // adding date and high
            chart1.Series["price"].Points.AddXY(DateTime.Parse(k.Lijst[i].Datum), k.Lijst[i].Hoog);
            // adding low
            chart1.Series["price"].Points[i].YValues[1] = k.Lijst[i].Laag;
            //adding open
            chart1.Series["price"].Points[i].YValues[2] = k.Lijst[i].PrijsOpen;
            // adding close
            chart1.Series["price"].Points[i].YValues[3] = k.Lijst[i].PrijsGesloten;
        }
    }
4

1 回答 1

15

您的代码添加了一个Series未命名的“价格”,然后引用两者Series["price"]Series[0]如果其他系列已经存在,这将是不同的东西。我运行了一个稍微修改过的版本(用 a 伪造数据库数据List<>)没有任何问题。您应该验证来自数据库的数据是否正常。

在此处输入图像描述

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    private void CandleStick_Load(object sender, EventArgs e)
    {
        GrafiekLaden();
    }

    public void GrafiekLaden()
    {
        // fake the DB data with a simple list
        List<dbdata> k = new List<dbdata> { 
            new dbdata("1/1/2012", 10f, 8f, 9f, 9.5f),
            new dbdata("2/1/2012", 15F, 10F, 12F, 13F),
            new dbdata("3/1/2012", 5F, 10F, 8F, 6F),
            new dbdata("4/1/2012", 25F, 10F, 18F, 16F)
        };

        Series price = new Series("price"); // <<== make sure to name the series "price"
        chart1.Series.Add(price);

        // Set series chart type
        chart1.Series["price"].ChartType = SeriesChartType.Candlestick;

        // Set the style of the open-close marks
        chart1.Series["price"]["OpenCloseStyle"] = "Triangle";

        // Show both open and close marks
        chart1.Series["price"]["ShowOpenClose"] = "Both";

        // Set point width
        chart1.Series["price"]["PointWidth"] = "1.0";

        // Set colors bars
        chart1.Series["price"]["PriceUpColor"] = "Green"; // <<== use text indexer for series
        chart1.Series["price"]["PriceDownColor"] = "Red"; // <<== use text indexer for series

        for (int i = 0; i < k.Count; i++)
        {
            // adding date and high
            chart1.Series["price"].Points.AddXY(DateTime.Parse(k[i].Datum), k[i].Hoog);
            // adding low
            chart1.Series["price"].Points[i].YValues[1] = k[i].Laag;
            //adding open
            chart1.Series["price"].Points[i].YValues[2] = k[i].PrijsOpen;
            // adding close
            chart1.Series["price"].Points[i].YValues[3] = k[i].PrijsGesloten;
        }
    }
}

class dbdata
{
    public string Datum;
    public float Hoog;
    public float Laag;
    public float PrijsOpen;
    public float PrijsGesloten;
    public dbdata(string d, float h, float l, float o, float c) { Datum = d; Hoog = h; Laag = l; PrijsOpen = o; PrijsGesloten = c; }
}
于 2012-10-24T15:50:01.623 回答