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<DataPoint> weightPoints = new List<DataPoint>();
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();
I have this code that I am trying to show the wieght of a person per each date recorded the issue is that the dates are not showing on the chart, i have tried diffrent ways but the issue still happen as there is a huge gape between dates in the chart. What i want is to show only those dates that are stored in tha databse on that chart with thier weight
2024-09-12
2023-09-06
2024-09-01
2025-07-17
2024-03-12
these are my dates