Код: Выделить всё
chart1.Series.Clear();
chart1.ChartAreas.Clear();
chart1.ChartAreas.Add(new ChartArea("MainArea"));
// Set background color to white
chart1.BackColor = System.Drawing.Color.White;
chart1.ChartAreas[0].BackColor = System.Drawing.Color.White;
Series weightSeries = new Series("Weight");
weightSeries.ChartType = SeriesChartType.Line;
weightSeries.XValueType = ChartValueType.Date;
weightSeries.BorderWidth = 3;
List weightPoints = new List();
foreach (DataRow row in weightData.Rows)
{
DateTime date = Convert.ToDateTime(row["date"]);
if (row["weight"] != DBNull.Value)
{
double weight = Convert.ToDouble(row["weight"]);
DataPoint dp = new DataPoint(date.ToOADate(), weight);
dp.MarkerStyle = MarkerStyle.Circle;
dp.MarkerSize = 7;
dp.Label = weight.ToString();
weightPoints.Add(dp);
}
}
weightPoints = weightPoints.OrderBy(dp => dp.XValue).ToList(); // Ensure the points are ordered by date
foreach (DataPoint dp in weightPoints)
{
weightSeries.Points.Add(dp);
}
chart1.Series.Add(weightSeries);
// Set x-axis minimum and maximum values based on data points
chart1.ChartAreas[0].AxisX.Minimum = weightPoints.Min(dp => dp.XValue);
chart1.ChartAreas[0].AxisX.Maximum = weightPoints.Max(dp => dp.XValue);
// Customize X-axis to show only the relevant dates
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45; // Rotate labels for better readability
chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd"; // Format the dates as "yyyy-MM-dd"
chart1.ChartAreas[0].AxisX.Title = "Date";
chart1.ChartAreas[0].AxisY.Title = "Weight (kg)";
chart1.ChartAreas[0].AxisY.Minimum = weightPoints.Min(dp => dp.YValues[0]) - 10;
chart1.ChartAreas[0].AxisY.Maximum = weightPoints.Max(dp => dp.YValues[0]) + 10;
// Configure X-axis as a date axis
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Auto; // Adjust interval type automatically
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = System.Drawing.Color.LightGray;
chart1.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true; // Ensure the last label is visible
// Refresh the chart to display the updated data
chart1.Invalidate();
2024-09-12
2023-09-06
2024-09-01
17 июля 2025 г.
12 марта 2024 г.
это мои даты
и это моя диаграмма

Подробнее здесь: https://stackoverflow.com/questions/786 ... wing-dates