使用 C# WinForms 图表。我想要上面的图表,我想将 Y 值范围设置为 2000、2001、2002...直至 3000。 我们如何在图表中设置范围以及如何在 Y 值范围中定义步骤。
从 SQL Server 动态访问数据。
private void LineChart()
{ //yet not working, steps are not settings yet
DateTime date = DateTime.Now;
string Date1 = date.ToShortDateString();
Date1 = DateTime.Now.ToString("yyyy-MM-dd");
//Fetch the Statistical data from database.
string query = "SELECT * FROM ( Select TOP 500
SrNo,Machine,ConeWeight,Date,DateTime FROM [DB_SProd].[dbo].
[TableProduction] Where Date='"+Date1+"' order by SrNo DESC ) as
Machines\r\n order by SrNo;\r\n ";
DataTable dt = dbConn.GetData(query);
//Get the DISTINCT Countries.
List<string> Machine1 = (from p in dt.AsEnumerable()
select p.Field<string>("Machine")).ToList();
List<int> Weight1 = (from p in dt.AsEnumerable()
select p.Field<int>("ConeWeight")).ToList();
int[] countCones = { };
//Remove the Default Series.
if (chart3.Series.Count() == 1)
{
chart3.Series.Remove(chart3.Series[0]);
}
//Loop through the Countries.
foreach (string macine in Machine1)
{
//get each machine name dynamitcally
string[] x = (from p in dt.AsEnumerable()
where p.Field<string>("Machine") == macine
orderby p.Field<string>("Machine") ascending
select p.Field<string>("Machine")).ToArray();
//Get the Total of Orders for each Country.
int[] y = (from p in dt.AsEnumerable()
where p.Field<string>("Machine") == macine
orderby p.Field<string>("Machine") ascending
select p.Field<int>("ConeWeight")).ToArray();
countCones = new int[y.Length];
for (int i = 0; i < y.Length; i++)
{
countCones[i] = i;
}
//Add Series to the Chart.
if (chart3.Series.IsUniqueName(macine))
{
//chart1.Series.Add(macine);
chart3.Series.Add(new Series(macine));
chart3.Series[macine].IsValueShownAsLabel = false;
chart3.Series[macine].BorderWidth = 2;
chart3.Series[macine].ChartType = SeriesChartType.Line;
chart3.Series[macine].Points.DataBindXY(countCones,y);
}
}
chart3.Legends[0].Enabled = true;
}
手动:
chart3.ChartAreas[0].AxisY.Minimum = 2001;
chart3.ChartAreas[0].AxisY.Maximum = 3000;
或自动
chart3.ChartAreas[0].AxisY.IsStartedFromZero = false;
chart3.ChartAreas[0].AxisY.IsMarginVisible = false;
设置步骤是:
chart3.ChartAreas[0].AxisY.Interval = 1;
if (chart3.Series.IsUniqueName(机器)) {
chart3.ChartAreas.Clear();
chart3.ChartAreas.Add("ChartArea1");
chart3.Series.Add(new Series(macine));
chart3.Series[macine].IsValueShownAsLabel = false;
chart3.Series[macine].BorderWidth = 2;
chart3.Series[macine].ChartType = SeriesChartType.Line;
chart3.DataBindings.Clear();
chart3.Series[macine].Points.DataBindXY(countCones,y);
// chart3.ChartAreas[0].AxisY.Minimum = 2001;
chart3.ChartAreas[0].AxisY.IsStartedFromZero = false;
chart3.ChartAreas[0].AxisY.IsMarginVisible = false;
chart3.ChartAreas[0].AxisX.Minimum = 0;
chart3.Update();
} Main Issue Resolved, but it doesn't refresh the chart as timer refresh this function with new data but chart remain the same