its my first post here, im not very good in english but i will try.
i need to make a graph like this one
The chart that im trying to recreate
based on a datatable filled by a SQL query that is structured like this:
Result of The query that i need to chart
now i have come up with this idea that is working quite well, but i am struggling with some aspect of it:
i need to show only the x value that are present in the query result, but since that they are datetime value when i add to the graph every point the x axes of the chart show me a linear time scale of every time of the day not only the point that i add to the chart. that is the main problem,
the other problem is that i need, when there are too much value showed, to be able to scroll horizontally the chart to see all the other values using the mouse scrolling wheel.
i thank y'all guys in advance hope that i have wrote all the information, in any case i can give you guys more if you need them
tysm
i now attach all the code insterested in the topic: (The designer)
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;
}
}
(now my Sql request Query with the use of my Drawchart function)
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");
}
(and now my function)
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;
}
}
CodePudding user response:
i have solved that, in order to have one X value point for every data column i needed to index all value from the two series with the setting (for each series):
series10.IsXValueIndexed = true;
more info about that on Microsoft help: