這是我在這里的第一篇文章,我的英語不是很好,但我會嘗試。
我需要做一個這樣的圖表
我試圖重新創建的圖表
基于由 SQL 查詢填充的資料表,其結構如下:
我需要繪制圖表的查詢結果
現在我想出了這個運作良好的想法,但我正在努力解決它的某些方面:
我想出了什么
我只需要顯示查詢結果中存在的 x 值,但是由于它們是日期時間值,當我將每個點添加到圖形中時,圖表的 x 軸向我顯示一天中每個時間的線性時間刻度,而不是只有我添加到圖表的點。這是主要的問題,
另一個問題是,當顯示太多值時,我需要能夠使用滑鼠滾輪水平滾動圖表以查看所有其他值。
我提前感謝你們,希望我已經寫了所有的資訊,無論如何,如果你需要他們,我可以給你們更多
氣喘吁吁
我現在附上與該主題相關的所有代碼:(設計師)
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 請求 Query 使用我的 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;
}
}
uj5u.com熱心網友回復:
我已經解決了這個問題,為了每個資料列都有一個 X 值點,我需要使用設定(對于每個系列)索引兩個系列中的所有值:
series10.IsXValueIndexed = true;
有關 Microsoft 幫助的更多資訊:
https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.datavisualization.charting.series.isxvalueindexed?view=netframework-4.8
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/357995.html
上一篇:訊息框圖示->變數?
