ZedGraph 规模一团糟

发布于 2024-12-27 04:49:10 字数 1915 浏览 0 评论 0原文

使用 ZedGraph 显示一段时间内的价格。显然我在设置图表时做错了什么。有什么想法吗?

这是我正在做的代码

private void Form1_Load(object sender, EventArgs e)
{
    myPane = zgc.GraphPane;

    // Set the Titles
    myPane.Title.Text = "Forex";
    myPane.XAxis.Title.Text = "Date/Time";
    myPane.YAxis.Title.Text = "Price";

    myPane.XAxis.Type = AxisType.Date;
    myPane.XAxis.Scale.MajorUnit = DateUnit.Minute;
    myPane.XAxis.Scale.Format = "T";
}

private void QueryDB(ref PointPairList lst)
{
    if (connection == null)
        connection = new MySql.Data.MySqlClient.MySqlConnection(connStr);

    try
    {
        if (connection.State == ConnectionState.Closed)
            connection.Open();

        string pair = cboPair.Text;
        string sql = "SELECT bid, ask, price_datetime FROM forex.prices WHERE pair='" + pair + "' and price_datetime > (NOW() - INTERVAL 1 MINUTE) ORDER BY price_datetime desc;";
        MySqlCommand cmd = new MySqlCommand(sql, connection);
        MySqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            DateTime tm = rdr.GetDateTime("price_datetime");
            double bid = rdr.GetDouble("bid");
            lst.Add(tm.ToOADate(), bid);
        }
        rdr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
    }
}

private void cmdRefresh_Click(object sender, EventArgs e)
{
    PointPairList bidPrice = new PointPairList();

    QueryDB(ref bidPrice);

    LineItem myCurve = myPane.AddCurve("Bid", bidPrice, Color.Red);

    myPane.YAxis.Scale.MinAuto = true;
    myPane.YAxis.Scale.MaxAuto = true;

    myPane.XAxis.Scale.MinAuto = true;
    myPane.XAxis.Scale.MaxAuto = true;

    zgc.AxisChange();

    // just points not lines. bigger points and colored based on
    // buy or sell. red for sell, green for buy
    //PointPairList tradeEntries = new PointPairList();
}

Using ZedGraph to show a price over time. Clearly I'm doing something wrong when setting up my graph. Any ideas?

Here is the code that I'm doing

private void Form1_Load(object sender, EventArgs e)
{
    myPane = zgc.GraphPane;

    // Set the Titles
    myPane.Title.Text = "Forex";
    myPane.XAxis.Title.Text = "Date/Time";
    myPane.YAxis.Title.Text = "Price";

    myPane.XAxis.Type = AxisType.Date;
    myPane.XAxis.Scale.MajorUnit = DateUnit.Minute;
    myPane.XAxis.Scale.Format = "T";
}

private void QueryDB(ref PointPairList lst)
{
    if (connection == null)
        connection = new MySql.Data.MySqlClient.MySqlConnection(connStr);

    try
    {
        if (connection.State == ConnectionState.Closed)
            connection.Open();

        string pair = cboPair.Text;
        string sql = "SELECT bid, ask, price_datetime FROM forex.prices WHERE pair='" + pair + "' and price_datetime > (NOW() - INTERVAL 1 MINUTE) ORDER BY price_datetime desc;";
        MySqlCommand cmd = new MySqlCommand(sql, connection);
        MySqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
            DateTime tm = rdr.GetDateTime("price_datetime");
            double bid = rdr.GetDouble("bid");
            lst.Add(tm.ToOADate(), bid);
        }
        rdr.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
    }
}

private void cmdRefresh_Click(object sender, EventArgs e)
{
    PointPairList bidPrice = new PointPairList();

    QueryDB(ref bidPrice);

    LineItem myCurve = myPane.AddCurve("Bid", bidPrice, Color.Red);

    myPane.YAxis.Scale.MinAuto = true;
    myPane.YAxis.Scale.MaxAuto = true;

    myPane.XAxis.Scale.MinAuto = true;
    myPane.XAxis.Scale.MaxAuto = true;

    zgc.AxisChange();

    // just points not lines. bigger points and colored based on
    // buy or sell. red for sell, green for buy
    //PointPairList tradeEntries = new PointPairList();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2025-01-03 04:49:10

阅读你的代码后,我发现一段代码看起来不适合 zedgraph 的含义。

lst.Add(tm.ToOADate(), bid);

您需要将日期解析为 OADate 吗?如果你把它设为 XDate,你可以将它用作 double

XDate tm = rdr.GetDateTime("price_datetime");
double bid = rdr.GetDouble("bid");
lst.Add((double)tm, bid);

after reading your code i found a piece of code that didnt look right for zedgraph meaning.

lst.Add(tm.ToOADate(), bid);

do you need to parse the date to OADate? if you make it an XDate you can use it as double

XDate tm = rdr.GetDateTime("price_datetime");
double bid = rdr.GetDouble("bid");
lst.Add((double)tm, bid);
君勿笑 2025-01-03 04:49:10

一种选择是使用不同的轴类型:
myPane.XAxis.Type = AxisType.DateAsOrdinal ->而不是 AxisType.Date

这意味着图表上的所有点将在 X 轴上均匀分布。
ZedGraph 不会使用日期/时间来确定点的 X 值,
但您仍然可以看到它(工具提示,也许也在轴标签上)

除此之外,也许您需要将 myPane.XAxis.Scale.MinorUnit 设置为 DateUnit.Second,我不知道

祝你好运

One option is to use a different axis type:
myPane.XAxis.Type = AxisType.DateAsOrdinal -> instead of AxisType.Date

This means that all points on the graph will be evenly spaced on the X axis.
ZedGraph will not use the date/time for determining the X values of the point,
but you will still be able to see it (tooltip, maybe on the axis labels also)

Besides from that, maybe you need to set the myPane.XAxis.Scale.MinorUnit to DateUnit.Second, I don't know

good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文