每个工作班次 C# .NET WindowsForm 客户端控件的生产摘要图表

Posted

技术标签:

【中文标题】每个工作班次 C# .NET WindowsForm 客户端控件的生产摘要图表【英文标题】:Chart With Production Summary for each work-Shift C# .NET WindowsForm Client Control 【发布时间】:2021-12-19 16:09:31 【问题描述】:

这是我在这里的第一篇文章,我的英语不太好,但我会尝试。

我需要做一个这样的图表

The chart that im trying to recreate

基于由如下结构的 SQL 查询填充的数据表:

Result of The query that i need to chart

现在我想出了这个非常有效的想法,但我正在努力解决它的某些方面:

What i have come up with

我只需要显示查询结果中存在的 x 值,但是由于它们是日期时间值,因此当我将每个点添加到图表中时,图表的 x 轴会向我显示每次这一天不仅是我添加到图表中的点。 这是主要问题,

另一个问题是,当显示的值过多时,我需要能够使用鼠标滚轮水平滚动图表以查看所有其他值。

提前感谢大家希望我已经写了所有信息,无论如何,如果您需要,我可以给你们更多信息

tysm

我现在附上主题中感兴趣的所有代码: (设计师)

namespace GRAPHICOBJECTS

    partial class GRAFTREND
    
        /// <summary> 
        /// Variabile di progettazione necessaria.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Pulire le risorse in uso.
        /// </summary>
        /// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
        protected override void Dispose(bool disposing)
        
            if (disposing && (components != null))
            
                components.Dispose();
            
            base.Dispose(disposing);
        

        #region Codice generato da Progettazione componenti

        /// <summary> 
        /// Metodo necessario per il supporto della finestra di progettazione. Non modificare 
        /// il contenuto del metodo con l'editor di codice.
        /// </summary>
        private void InitializeComponent()
        
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
            this.CHART = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.CHART)).BeginInit();
            this.SuspendLayout();
            // 
            // CHART
            // 
            this.CHART.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(175)))), ((int)(((byte)(175)))), ((int)(((byte)(175)))));
            chartArea1.AxisX.Enabled = System.Windows.Forms.DataVisualization.Charting.AxisEnabled.True;
            chartArea1.AxisX.Interval = 1D;
            chartArea1.AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
            chartArea1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
            chartArea1.AxisX.IsLabelAutoFit = false;
            chartArea1.AxisX.LabelStyle.Angle = 90;
            chartArea1.AxisX.LabelStyle.Format = "dd/MM HH:mm";
            chartArea1.AxisX.LabelStyle.Interval = 0D;
            chartArea1.AxisX.LabelStyle.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
            chartArea1.AxisX2.Interval = 1D;
            chartArea1.AxisX2.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
            chartArea1.AxisX2.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
            chartArea1.AxisX2.IsLabelAutoFit = false;
            chartArea1.AxisX2.LabelStyle.Angle = 90;
            chartArea1.AxisX2.LabelStyle.Format = "dd/MM HH:mm";
            chartArea1.AxisX2.LabelStyle.Interval = 0D;
            chartArea1.AxisX2.LabelStyle.IntervalType = System.Windows.Forms.DataVisualization.Charting.DateTimeIntervalType.Hours;
            chartArea1.Name = "ChartArea1";
            this.CHART.ChartAreas.Add(chartArea1);
            this.CHART.Location = new System.Drawing.Point(1, 0);
            this.CHART.Name = "CHART";
            this.CHART.RightToLeft = System.Windows.Forms.RightToLeft.No;
            series1.ChartArea = "ChartArea1";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
            series1.IsVisibleInLegend = false;
            series1.Name = "S2";
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
            series2.ChartArea = "ChartArea1";
            series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
            series2.IsVisibleInLegend = false;
            series2.Name = "S1";
            series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.DateTime;
            this.CHART.Series.Add(series1);
            this.CHART.Series.Add(series2);
            this.CHART.Size = new System.Drawing.Size(1012, 350);
            this.CHART.TabIndex = 1;
            this.CHART.Text = "CHART";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(3, 352);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(58, 43);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // GRAFTREND
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.button1);
            this.Controls.Add(this.CHART);
            this.Name = "GRAFTREND";
            this.Size = new System.Drawing.Size(1015, 398);
            this.Load += new System.EventHandler(this.UserControl2_Load);
            ((System.ComponentModel.ISupportInitialize)(this.CHART)).EndInit();
            this.ResumeLayout(false);

        

        #endregion

        private System.Windows.Forms.DataVisualization.Charting.Chart CHART;
        private System.Windows.Forms.Button button1;
    

(现在我的 Sql 请求使用我的 Drawchart 函数查询)

private void button1_Click(object sender, EventArgs e)

    SqlCommand cmd = con.CreateCommand();

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "GetStatisticalData_ShiftGraph";
    cmd.Parameters.AddWithValue("@DataStartSH", "2021-09-01 06:00:00.000");
    cmd.Parameters.AddWithValue("@DataEndSH", "2021-09-02 06:00:00.000");
    cmd.Parameters.AddWithValue("@RecipeName", "TEST");
    cmd.Parameters.AddWithValue("@LineN", 1);

    cmd.ExecuteNonQuery();
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    NRecord = da.Fill(dt);
    GraphicFunctions.DrawChart(this.CHART, dt, "TimeShift", "Rejected", Color.Yellow, "TimeShift", "Good", Color.LightGreen);

    this.CHART.Series["S1"].IsValueShownAsLabel = true;
    this.CHART.Series["S2"].IsValueShownAsLabel = true;
    this.CHART.DataManipulator.InsertEmptyPoints(1, IntervalType.Days, "S1");
    this.CHART.DataManipulator.InsertEmptyPoints(1, IntervalType.Days, "S2");

(现在是我的功能)

public static void DrawChart(Chart ObjChart,DataTable DTable, string xColumnS1, string yColumnS1,Color ColorS1, string xColumnS2, string yColumnS2, Color ColorS2)

    double PointsCount;
    try
    
        ObjChart.DataSource = DTable;
        ObjChart.Series["S1"].XValueMember = xColumnS1;
        ObjChart.Series["S1"].YValueMembers = yColumnS1;
        ObjChart.Series["S2"].Enabled = true;
        ObjChart.Series["S2"].XValueMember = xColumnS2;
        ObjChart.Series["S2"].YValueMembers = yColumnS2;
        ObjChart.DataBind();
        PointsCount = ObjChart.Series["S1"].Points.Count;
        for (int i = 0; i <= PointsCount - 1; i++) ObjChart.Series["S1"].Points[i].Color = ColorS1;
        PointsCount = ObjChart.Series["S2"].Points.Count;
        for (int i = 0; i <= PointsCount - 1; i++) ObjChart.Series["S2"].Points[i].Color = ColorS2;
    
    catch (System.Exception ex)
    
        throw ex;
    

【问题讨论】:

我们需要看一下设置图表和填写数据的相关代码。 好的,现在我已经添加了所有信息 【参考方案1】:

我已经解决了这个问题,为了让每个数据列都有一个 X 值点,我需要使用设置(对于每个系列)对两个系列中的所有值进行索引:

series10.IsXValueIndexed = true;

有关 Microsoft 帮助的更多信息:

https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.datavisualization.charting.series.isxvalueindexed?view=netframework-4.8

【讨论】:

以上是关于每个工作班次 C# .NET WindowsForm 客户端控件的生产摘要图表的主要内容,如果未能解决你的问题,请参考以下文章

在 SQL 中计算连续班次和天数

算法:根据周数获取明年日期工作班次类型

程序设计题目---实现简单票务系统

程序设计题目---实现简单票务系统

Python计算旋转工作班次的天数

思迅软件-易超的工作日记