草庐IT

c# - 烛台多个 Y 值

coder 2024-06-02 原文

我的任务是在 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;
        }
    }

最佳答案

您的代码添加了一个 Series未命名为“价格”,则同时引用 Series["price"]Series[0]如果其他系列已经存在,这将不是一回事。我运行了一个稍微修改过的版本(用 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; }
}

关于c# - 烛台多个 Y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12983199/

有关c# - 烛台多个 Y 值的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  5. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  6. c# - 如何在 ruby​​ 中调用 C# dll? - 2

    如何在ruby​​中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL

  7. C# 到 Ruby sha1 base64 编码 - 2

    我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha

  8. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  9. ruby-on-rails - before_filter 运行多个方法 - 2

    是否有可能:before_filter:authenticate_user!||:authenticate_admin! 最佳答案 before_filter:do_authenticationdefdo_authenticationauthenticate_user!||authenticate_admin!end 关于ruby-on-rails-before_filter运行多个方法,我们在StackOverflow上找到一个类似的问题: https://

  10. ruby-on-rails - Rails 3.1 中具有相同形式的多个模型? - 2

    我正在使用Rails3.1并在一个论坛上工作。我有一个名为Topic的模型,每个模型都有许多Post。当用户创建新主题时,他们也应该创建第一个Post。但是,我不确定如何以相同的形式执行此操作。这是我的代码:classTopic:destroyaccepts_nested_attributes_for:postsvalidates_presence_of:titleendclassPost...但这似乎不起作用。有什么想法吗?谢谢! 最佳答案 @Pablo的回答似乎有你需要的一切。但更具体地说...首先改变你View中的这一行对此#

随机推荐