C# Visual Studio Chart XValue 中的日期处理问题

问题描述 投票:0回答:1

我在 Visual Studio 上的 Microsoft Forms 中的图表无法显示格式正确的日期。当您输入整数时,它可以正常工作,但是格式化为日期的数字则不起作用。

这是我在 SQLite 中的表,其中 pd_data 作为整数。

CREATE TABLE producao
(
    pd_pr_codigo INTEGER,
    pd_data INTEGER,
    pd_codigo INTEGER,
    pd_qtd_produzida REAL,

    PRIMARY KEY (pd_pr_codigo, pd_codigo, pd_data)
    FOREIGN KEY (pd_pr_codigo) REFERENCES produtos(pr_codigo)
)

这是我的代码:

private void BtProdBlocos_Click(object sender, EventArgs e)
{
        try
        {
            string sql = "SELECT pd_data, pd_qtd_produzida FROM producao";
            SqliteParameter[] emptyparameter = new SqliteParameter[]{ };
            chart1.DataSource = Funcoes.Cursor(sql, emptyparameter);

            chart1.Series["Producao"].XValueMember = "pd_data"; 
            chart1.Series["Producao"].YValueMembers = "pd_qtd_produzida";

            chart1.DataBind();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}

这是图表,X个整数中的值对应于日期2024/10/22和2024/10/23。

现在使用 pd_data 作为文本并将日期(如 dd/mm/yyyy)放入 SQL 中:

CREATE TABLE producao
(
    pd_pr_codigo INTEGER,
    pd_data TEXT,
    pd_codigo INTEGER,
    pd_qtd_produzida REAL,

    PRIMARY KEY (pd_pr_codigo, pd_codigo, pd_data)
    FOREIGN KEY (pd_pr_codigo) REFERENCES produtos(pr_codigo)
)

具有相同的日期和相同的代码,只出现 1 个日期,无论我做什么和尝试。我,不。实现了。安排。

显示日期的表格列就在那里。

我调试了代码并得出结论,所有日期都已到达,只是由于某种原因没有正确显示在图表中。另外,

Funcoes.cursor
是与数据库的连接。我通过这个函数发送 SQL 查询,它返回所有值。

c# visual-studio sqlite charts microsoft-forms
1个回答
0
投票

我找到了解决方案。在 C# 中,X 的值必须是

datetime
类型才能正确理解。由于 SQLite 没有原生
datetime
类型,解决方案是将这两个值传输到 DataTable,并在其中将
pd_data
pd_qtd_quantidade
值分别转换为
datetime
和 double。

在代码中,我还将 X 轴格式化为以 45 度角显示,以提高可读性,并且它只会显示第一个和最后一个值作为可选。我唯一无法更改的是,如果日期之间存在差距,程序会填写它(例如:从 10/11/2024 到 10/13/2024,图表将添加 10/12/ 2024 年之间,即使没有 Y 值),我个人不喜欢并希望更改,尽管我还没有找到解决方案。我希望这也能帮助别人!

这是代码:

private void BtProdBlocos_Click(object sender, EventArgs e) 
{
    string comandosql = "SELECT pd_data, pd_qtd_produzida FROM producao";
    SqliteParameter[] parametrovazio = new SqliteParameter[] { };
    DataTable dataTable = Funcoes.Cursor(comandosql, parametrovazio);

    // Check if the chart has the correct series
    if (chart1.Series.Count == 0)
    {
        chart1.Series.Add("Producao");
    }

    // Configure the series
    chart1.Series["Producao"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
    chart1.Series["Producao"].YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;

    // Clear previous data (if any)
    chart1.Series["Producao"].Points.Clear();

    // Add points to the chart
    foreach (DataRow row in dataTable.Rows)
    {
        DateTime data = Convert.ToDateTime(row["pd_data"]);
        double quantidadeProduzida = Convert.ToDouble(row["pd_qtd_produzida"]);

        // Add the point (date, quantity) to the series
        chart1.Series["Producao"].Points.AddXY(data, quantidadeProduzida);
    }

    // Set the X axis label format to display only day and month
    chart1.ChartAreas[0].AxisX.LabelStyle.Format = "dd/MM/yyyy";

    // Set the angle of the labels on the X axis
    chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;

    // Set the interval for the X axis labels (here set to 1 day)
    chart1.ChartAreas[0].AxisX.Interval = 1;
    chart1.ChartAreas[0].AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Days;

    // Optional: Force the X axis boundary to start and end at the exact dates
    chart1.ChartAreas[0].AxisX.Minimum = chart1.Series["Producao"].Points[0].XValue;
    chart1.ChartAreas[0].AxisX.Maximum = chart1.Series["Producao"].Points[chart1.Series["Producao"].Points.Count - 1].XValue;

    // Refresh the chart
    chart1.Invalidate();
}
© www.soinside.com 2019 - 2024. All rights reserved.