是否可以匹配来自 c# winforms 中两个不同数据集的图表中的两个系列数据

2024-01-11

我正在开发一个应用程序,根据工单编号绘制已检查的电路板和有缺陷的电路板。在查看图表并比较实际数据后,我意识到该系列与工单编号不匹配。我不知道如何将两个系列与工单编号相匹配以获得正确的图表。

这是我首先提取的数据拉取 Boards_Inspected 的代码

public DataSet Get_Boards_Inspected(string startDate, string endDate, int location)
    {
        DataSet ds = new DataSet();

        NpgsqlConnection conn = DatabaseConnection.getConnectionString();
        try
        {
            conn.Open();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter("select new_table.top_or_bottom, new_table.board_wo_number, count(new_table.board_serial_number) from" +
            " (select distinct master_board.board_serial_number, board_wo.board_wo_number,board_wo.board_part_number, board_time.top_or_bottom from master_board" +
            " inner join board_time on board_time.board_time_id = master_board.id" +
            " inner join board_wo on board_wo.board_wo_number = master_board.board_wo_number" +
            " where time_in between '" + startDate + "' and '" + endDate + "'" +
            " and board_time.location_id = '" + location + "')" +
            " as new_table" +
            " group by new_table.top_or_bottom, new_table.board_wo_number" +
            " order by top_or_bottom;", conn);
            ds.Reset();
            da.Fill(ds);
            da.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show("An unexpected error has occured with the application. \n" +
            "An email has been sent to the software developer for analysis. \n" +
            "this program will now close.", "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            eMessageBody = ex.ToString();
            Mail sendMessage = new Mail();
            sendMessage.SendSMTP(eMessageBody, eMessageSubject);
            Application.Exit();
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }

有缺陷的板

 public DataSet Get_Boards_With_Issue(string startDate, string endDate, int location)
    {
        DataSet ds = new DataSet();
        NpgsqlConnection conn = DatabaseConnection.getConnectionString();
        try
        {
            conn.Open();
            NpgsqlDataAdapter da = new NpgsqlDataAdapter("select new_table.top_or_bottom, new_table.board_wo_number, count(new_table.defect_id) from" +
            " (select distinct defect_id, top_or_bottom, board_wo_number from defect" +
            " inner join master_board on defect.defect_id = master_board.id" +
            " where defect_time between '" + startDate + "' and '" + endDate + " 23:59:59'" +
            " and location_id = '" + location + "')" +
            " as new_table" +
            " group by new_table.top_or_bottom, new_table.board_wo_number" +
            " order by top_or_bottom;", conn);

            ds.Reset();
            da.Fill(ds);
            da.Dispose();
        }
        catch(Exception ex)
        {
            MessageBox.Show("An unexpected error has occured with the application. \n" +
            "An email has been sent to the software developer for analysis. \n" +
            "this program will now close.", "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            eMessageBody = ex.ToString();
            Mail sendMessage = new Mail();
            sendMessage.SendSMTP(eMessageBody, eMessageSubject);
            Application.Exit();
        }
        finally
        {
            conn.Close();
        }
        return ds;
    }

这是我的图表系列作业代码

private void Boards_Without_Issue_Chart(string starDate, string endDate, int location)
    {
        try
        {
            #region Chart Setup
            chart1.Series.Clear();
            chart1.Series.Add("Boards Inspected");
            chart1.Series.Add("Boards Without Issue");
            chart1.Series.Add("Boards With Issue");

            chart1.Series["Boards Inspected"].Points.Clear();
            chart1.Series["Boards Without Issue"].Points.Clear();
            chart1.Series["Boards With Issue"].Points.Clear();

            chart1.Series["Boards Inspected"]["LabelStyle"] = "Top";
            chart1.Series["Boards Without Issue"]["LabelStyle"] = "Top";
            chart1.Series["Boards With Issue"]["LabelStyle"] = "Top";

            chart1.ChartAreas[0].AxisX.Interval = 1;
            chart1.Series["Boards Inspected"].ChartType = SeriesChartType.Column;
            chart1.Series["Boards Without Issue"].ChartType = SeriesChartType.Column;
            chart1.Series["Boards With Issue"].ChartType = SeriesChartType.Column;

            chart1.Series["Boards Inspected"]["DrawingStyle"] = "LightToDark";
            chart1.Series["Boards Without Issue"]["DrawingStyle"] = "LightToDark";
            chart1.Series["Boards With Issue"]["DrawingStyle"] = "LightToDark";


            if (chart1.Titles.Contains(t1))
            {
                t1.Font = new System.Drawing.Font("Microsoft Sans serif", 14, FontStyle.Bold);
                t1.Text = titleText + " - Boards Without Issue - (" + startDate + " - " +endDate+ ")";
            }
            else
            {
                t1.Name = "tTitle1";
                t1.Font = new System.Drawing.Font("Microsoft Sans serif", 14, FontStyle.Bold);
                t1.Text = titleText + " - Boards Without Issue - (" + startDate + " - " + endDate + ")";
                chart1.Titles.Add(t1);
            }

            chart1.ChartAreas[0].BorderDashStyle = ChartDashStyle.Solid;
            chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
            chart1.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Microsoft Sans serif", 14, FontStyle.Bold);
            chart1.ChartAreas[0].AxisY.Title = "Amount of Boards";
            chart1.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Microsoft Sans serif", 14, FontStyle.Bold);
            chart1.ChartAreas[0].AxisX.Title = "Work Order";
            chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            chart1.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Microsoft Sans serif", 12, FontStyle.Regular);
            chart1.Series["Boards Inspected"].IsValueShownAsLabel = true;
            chart1.Series["Boards With Issue"].IsValueShownAsLabel = true;

            #endregion

            #region Chart Data Assignment
            DataAccess DA = new DataAccess();
            DataAccess DA2 = new DataAccess();
            DataSet mda = new DataSet();
            if ((DA.Get_Boards_Inspected(startDate, endDate, location).Tables[0].Rows.Equals(0)) && 
                (DA2.Get_Boards_With_Issue(startDate, endDate, location).Tables[0].Rows.Equals(0)))
                lblError.Text = "No information Availiable";
            else
            {
                foreach (DataTable tb in DA.Get_Boards_Inspected(startDate, endDate, location).Tables)
                {
                    foreach (DataRow dr in tb.Rows)
                    {
                        object tpn = dr["top_or_bottom"];
                        var ct = (dr["count"].ToString());
                        var wo = (dr["board_wo_number"].ToString());

                        if (tpn == DBNull.Value)
                            chart1.Series["Boards Inspected"].Points.AddXY(wo, ct);
                        else
                            chart1.Series["Boards Inspected"].Points.AddXY(wo + " - " + tpn, ct);
                    }
                }
                DA.Get_Boards_Inspected(startDate, endDate, location).Clear();
                DA.Get_Boards_Inspected(startDate, endDate, location).Dispose();

                foreach (DataTable tb2 in DA2.Get_Boards_With_Issue(startDate, endDate, location).Tables)
                {
                    foreach (DataRow dr2 in tb2.Rows)
                    {
                        object tpn2 = dr2["top_or_bottom"];
                        var ct = (dr2["count"].ToString());
                        var wo = (dr2["board_wo_number"].ToString());

                        if (tpn2 == DBNull.Value)
                            chart1.Series["Boards With Issue"].Points.AddXY(wo, ct);
                        else
                            chart1.Series["Boards With Issue"].Points.AddXY(wo + " - " + tpn2, ct);
                    }
                }
                DA2.Get_Boards_With_Issue(startDate, endDate, location).Clear();
                DA2.Get_Boards_With_Issue(startDate, endDate, location).Dispose();
            }

            #endregion
        }
        catch(Exception ex)
        {
            MessageBox.Show("An unexpected error has occured with the application. \n" +
            "An email has been sent to the software developer for analysis. \n" +
            "this program will now close.", "Unexpected Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            eMessageBody = ex.ToString();
            Mail sendMessage = new Mail();
            sendMessage.SendSMTP(eMessageBody, eMessageSubject);
            Application.Exit();
            MessageBox.Show(ex.ToString());
        }
    }

编辑:这是图表数据的代码

 DataAccess DA = new DataAccess();
            DataAccess DA2 = new DataAccess();
            DataSet mda = new DataSet();
            if ((DA.Get_Boards_Inspected(startDate, endDate, location).Tables[0].Rows.Equals(0)) && 
                (DA2.Get_Boards_With_Issue(startDate, endDate, location).Tables[0].Rows.Equals(0)))
                lblError.Text = "No information Availiable";
            else
            {
                foreach (DataTable tb in DA.Get_Boards_Inspected(startDate, endDate, location).Tables)
                {
                    foreach (DataRow dr in tb.Rows)
                    {
                        object tpn = dr["top_or_bottom"];
                        var ct = (dr["count"].ToString());
                        var wo = (dr["board_wo_number"].ToString());

                        if (tpn == DBNull.Value)
                            chart1.Series["Boards Inspected"].Points.AddXY(wo, ct);
                        else
                            chart1.Series["Boards Inspected"].Points.AddXY(wo + " - " + tpn, ct);
                    }
                }
                DA.Get_Boards_Inspected(startDate, endDate, location).Clear();
                DA.Get_Boards_Inspected(startDate, endDate, location).Dispose();

                foreach (DataTable tb2 in DA2.Get_Boards_With_Issue(startDate, endDate, location).Tables)
                {
                    foreach (DataRow dr2 in tb2.Rows)
                    {
                        object tpn2 = dr2["top_or_bottom"];
                        var ct = (dr2["count"].ToString());
                        var wo = (dr2["board_wo_number"].ToString());

                        if (tpn2 == DBNull.Value)
                            chart1.Series["Boards With Issue"].Points.AddXY(wo, ct);
                        else
                            chart1.Series["Boards With Issue"].Points.AddXY(wo + " - " + tpn2, ct);
                    }
                }
                DA2.Get_Boards_With_Issue(startDate, endDate, location).Clear();
                DA2.Get_Boards_With_Issue(startDate, endDate, location).Dispose();

编辑:链接

两个数据集,试图将它们排列在图表中 https://i.stack.imgur.com/kQ9aX.png

最近的图表 https://i.stack.imgur.com/o7QRk.png


这是使用图表控件时的一个典型错误。

他们似乎自动地正确地完成了所有事情,直到你走近一点并发现问题。

规则如下:

所有值(X 值和所有 Y 值)在内部存储为双精度值。

如果你输入数字,一切都会正常。如果您输入任何其他数据类型,您就会在未来的某个地方遇到麻烦。

起初,一切似乎都有效,您输入的内容很好地显示在标签中。

但是当你想使用任何高级能力时Chart, 很可能是行不通的..

这可以是简单的事情格式化标签,仅适用于字符串格式。如果您想使用数字格式,您需要输入数字!

你遇到的问题是matching不同系列的上升点。

您已将 X 值作为字符串输入,因此一旦数据点需要超出您添加它们的顺序进行匹配,问题就会开始。

你可以按照精确的顺序喂它们,但你需要了解会发生什么。

让我们看看幕后花絮...:如果您使用调试器来查看系列数据点的 x 值,您会惊讶地发现它们都是0!您输入的字符串已进入标签,但 X 值都是转换为 double 失败的结果,导致0。这意味着所有数据点都具有相同的 X 值!

您有两个选择:

  • 要么同步添加它们,所有序列都以相同的顺序和相同的点数。

  • 使用数字作为 X 值。

您的工单looks像一个数字;如果是,您可以使用它(如果您愿意),但它会根据这些数字分散数据。可能不是一个好主意。相反,您可以为每个工作订单分配一个索引并使用它。

要创建漂亮的标签,请使用AxisLabel每个数据点的属性!

我认为创建一个是最简单的DataPoint首先使用值和轴标签,也许还有工具提示和颜色等等,然后将其添加到点集合中。


现在为了让大家明白这一点,请看一下这张图表:

这是创建它的代码:

private void Form1_Load(object sender, EventArgs e)
{
    Random R = new Random(1);
    List<Tuple<Series, int>> misses = new List<Tuple<Series, int>>();
    chart1.Series.Clear();

    for (int i = 0; i < 3; i++ )
    {
        Series s = new Series("S" + (i + 1));
        s.ChartType = SeriesChartType.Column;
        chart1.Series.Add(s);
    }

    chart1.ChartAreas[0].AxisX.Interval = 1;

    foreach(Series s in chart1.Series)
    {
        for (int i = 0; i < 30; i+=3)
        {
            if (R.Next(3) > 0) s.Points.AddXY(i, i+1);
            else misses.Add(new Tuple<Series, int>(s, i));
        }
    }

    foreach (Tuple<Series, int> m in misses)
    {
        if (m.Item1.Name == "S1") m.Item1.Points.AddXY(m.Item2 + "X", m.Item2 + 5);
        else m.Item1.Points.AddXY(m.Item2, m.Item2 + 5);
    }

    for (int i = 0; i < chart1.Series[0].Points.Count - 1; i++)
    {
        chart1.Series[0].Points[i].AxisLabel = chart1.Series[0].Points[i].XValue + "%";
    }
}

让我们看看发生的事情:

我首先创建三个系列,然后在其中填充一些要点。但是,我randomly留几个空位。

我将它们存储在元组列表中,以便稍后添加它们出故障了.

你可以看到他们都匹配,除了蓝色系列“S1”。

你能明白为什么吗?

我总是为 X 值使用一个很好的数字,但不会为蓝调系列中遗漏的点使用,我在数字上附加了“X”。

现在这些点也被添加了,但所有的 X 值都是 0,所以它们都位于位置 0。

Note: 注意上面的是not代码你可以use。这是代码study了解数据类型Chart值以及添加字符串作为 X 值的后果!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以匹配来自 c# winforms 中两个不同数据集的图表中的两个系列数据 的相关文章

  • 更改 Visual Studio 2015 扩展中项目内的文件 ProjectItem 的内容?

    如何更改文件的内容 ProjectItem在给定的范围内Project 我想用字符串替换它的所有内容 这个问题有解决办法吗 我想做一些改变ProjectItem CS 文件 通过使用 VSIX 包 以及我现在看到的唯一一种执行此操作的方法
  • 在静态断言和运行时错误之间自动选择

    我有一个执行除法并检查对齐的宏 define BYTES TO WORDS x CHECK ALIGNMENT x 2 x 2 我想实施CHECK ALIGNMENT作为一个总是返回 1 的宏 并且如果满足以下条件则触发错误x不除以 2 宏
  • 如何在函数中将结构成员作为指针传递?

    问题是我有一个结构是另一个 主要 结构的成员 我编写了一个函数来清除第一个结构 它需要一个指向结构的指针 我想使用该函数来清除主要结构内的结构 但我不确切知道哪种方法是正确的 为了更好地解释它 这里有一些代码 我有一个结构 定义为 type
  • C++ 中的字符串到 LPCWSTR

    我正在尝试从字符串转换为 LPCWSTR 我使用多位 1 例如 LPCWSTR ToLPCWSTR string text LPCWSTR sw LPCWSTR text c str return sw 2 返回中文字符 LPCWSTR T
  • 实体框架 5 不清除导航属性

    我在 Entity Framework 5 中遇到了这个奇怪的问题 我在其中一个实体中有一个导航属性 我想将其设置为null 但由于某种原因 该属性只有在我第二次调用该属性时才会被清除 using var db new Entities v
  • 将 gcov 与 CMake/CDash 结合使用的详细指南?

    我在我的项目中使用 CMake 并设置了 cdash 服务器以进行连续 夜间构建 一切运行良好 通过设置 crontab 我们可以将每小时 每晚的构建 测试结果自动上传到我们的 cdash 服务器 我的下一步是将测试覆盖率报告添加到构建中
  • 如何从c++调用python

    我是Python新手 我尝试像这样从 C 调用 python 脚本 在 Raspberry Pi 中 std string pythonCommand python Callee py a b int res system pythonCo
  • 接口中的私有成员

    是否可以在 NET 接口中创建私有成员 我听说现在可以了 但我的 IDE 拒绝了 public interface IAnimal void SetDefaultName string name ChangeName name privat
  • 如何在单独的类库中管理客户端上下文对象?

    我正在尝试创建一个库 类库 对于共享点 它将拥有所有共享点 dll 来与共享点服务器交互上传文件 文档并创建文档库和文档集 现在这个库可以被使用客户端 例如 Web 应用程序 asp net webform 或 mvc 或控制台应用程序或
  • MSBuild 将动态生成的文件复制为项目依赖项的一部分

    我有一个自定义 msbuild 任务 它正在生成一些输出文件到 ProjectA 的输出目录 TargetDir 当前的代码是这样的
  • 当应用程序未聚焦时监听按键

    我有一个应用程序 C 4 0 WPF 它是隐藏的 可以通过单击系统托盘图标或我创建的其他框架 停靠在左侧和最上面的小框架 来显示 My customer wants to add a new way to display the appli
  • 为什么 C# 编译的正则表达式比等效的字符串方法更快?

    每次我必须对字符串执行简单的包含或替换操作 其中我正在搜索的术语是固定值 时 我发现如果我获取示例输入并对其进行一些分析 则使用编译的正则表达式是几乎 总是比使用 String 类中的等效方法更快 我尝试过比较多种方法 hs是要搜索的 干草
  • Cuda:最小二乘求解,速度较差

    最近 我使用Cuda编写了一个名为 正交匹配追踪 的算法 在我丑陋的 Cuda 代码中 整个迭代需要 60 秒 而 Eigen lib 只需 3 秒 在我的代码中 矩阵 A 是 640 1024 y 是 640 1 在每一步中 我从 A 中
  • 是否可以从.NET Core中间件检索控制器的操作结果?

    public class UsersController APIControllerBase public UsersController public Client Get return new Client ClientID 1 Las
  • 第一个随机数始终小于其余随机数

    我碰巧注意到 在 C 中 使用 std rand 方法调用的第一个随机数大多数时候都明显小于第二个随机数 关于 Qt 实现 第一个几乎总是小几个数量级 qsrand QTime currentTime msec qDebug lt lt q
  • STL 向量、迭代器和插入 (C++)

    我有一个将向量的迭代器传递到的方法 在这个方法中 我想向向量中添加一些元素 但我不确定当只有迭代器时这是否可行 void GUIComponentText AddAttributes vector
  • TypeScript 中 C# 类虚拟成员的等效项

    因此 在 C 中 当我创建模型类和延迟加载内容时 我会执行以下操作 public int User ID get set public int Dept ID get set 然后在我的班级稍远一点的地方 我像这样弹出我的虚拟 public
  • MsBuild 在 Visual Studio Online 上找不到恢复的 NuGet 包

    我尝试构建一个存储在 Visual Studio Online 上的外部 GIT 存储库中的解决方案 它有以下步骤 1 Git 恢复 有效 2 NuGet 恢复 有效 3 构建 不起作用 查看日志时我的第一个猜测是 MsBuild 没有查找
  • 构建 OpenCV 时出错 :: MonitorFromRect 未在此范围内声明

    我试图建立OpenCV version 2 4 8与它一起使用CodeBlocks and MinGw 我按照以下指示进行操作here http kevinhughes ca tutorials opencv install on wind
  • AddressAccessDeniedException :无需 netsh 即可解决它?

    我遇到了异常AddressAccessDeniedException因为我的processus没有注册URL的权限 我首先以管理员身份运行我的程序 好的 它成功了 但我现在想要分发我的应用程序 并且我希望每个用户都能够运行它 而不必成为管理

随机推荐