Presently I am attempting to display a chart using windows forms that shows monthly data on the X axis and an integer value on the Y axis; however, I am not setting the range properly for the X Axis, where MonthYear is a DateTime:
var pnChart = new System.Windows.Forms.Panel(); pnChart.Controls.Clear(); DataTable dtChartData = myDatabaseLayer.BuildDataTable("SELECT Added, Modified FROM tblStatistics WHERE ApplicationID = " + intApplicationID + " ORDER BY MonthYear"); Chart chart = GenerateChart(dtChartData, pnChart.Width, pnChart.Height, "ActiveBorder", 6); chart.Series[0].XValueType = ChartValueType.DateTime; chart.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd"; chart.ChartAreas[0].AxisX.Interval = 1; chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months; chart.ChartAreas[0].AxisX.IntervalOffset = 1; pnChart.Controls.Add(chart);
The problem is, when the chart is displayed, the X axis has the datetime "1900-01-01" so my question is, how do I set the date range to start at 2013-01-01?
Please note that I have searched the internet and tried the following settings, but they do not give me the correct range:
chart.ChartAreas[0].AxisX.Maximum = DateTime.Now.Ticks;
Or,
chart.ChartAreas[0].AxisX.Crossing = DateTime.Now.Ticks;
Or,
chart.ChartAreas[0].AxisX.Minimum = DateTime.Now.Ticks;
TIA.
UPDATE: Please note that I found how to set the range properly using this:
chart.Series[0].XValueType = ChartValueType.DateTime; DateTime minDate = new DateTime(2013, 01, 01); DateTime maxDate = DateTime.Now; chart.ChartAreas[0].AxisX.Minimum = minDate.ToOADate(); chart.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate();
The above code sets the proper range now for the X axis; however, now chart itself is blank.
UPDATE 2:
Yes, thank you DasKrumelmonster--that fixed it! I was using code from http://www.codeproject.com/Articles/168056/Windows-Charting-Application, and simply should have looked more closely at the author's protected internal Chart GenerateChart(DataTable dtChartDataSource, int width, int height, string bgColor, int intType) function. To correct the issue, I changed these lines:
foreach (DataRow dr in dtChartDataSource.Rows) { double dataPoint = 0; double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint); DataPoint objDataPoint = new DataPoint() { AxisLabel = "series", YValues = new double[] { dataPoint } }; chart.Series[series].Points.Add(dataPoint); }
To this:
DateTime dtStart = new DateTime(2013, 01, 01); int intMonthCounter = 0; //Add data points to the series foreach (DataRow dr in dtChartDataSource.Rows) { double dataPoint = 0; double.TryParse(dr[dc.ColumnName].ToString(), out dataPoint); DataPoint objDataPoint = new DataPoint() { AxisLabel = "series", YValues = new double[] { dataPoint } }; chart.Series[series].Points.AddXY(dtStart.AddMonths(intMonthCounter), dataPoint); intMonthCounter++; }
Thank you!
Cannot reproduce. I tried this code:
private void button1_Click(object sender, EventArgs e) { var s = new Series(); s.ChartType = SeriesChartType.Line; var d = new DateTime(2013, 04, 01); s.Points.AddXY(d, 3); s.Points.AddXY(d.AddMonths(-1), 2); s.Points.AddXY(d.AddMonths(-2), 1); s.Points.AddXY(d.AddMonths(-3), 4); chart1.Series.Clear(); chart1.Series.Add(s); chart1.Series[0].XValueType = ChartValueType.DateTime; chart1.ChartAreas.Add(new ChartArea()); // In some cases the winforms designer adds this already chart1.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd"; chart1.ChartAreas[0].AxisX.Interval = 1; chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Months; chart1.ChartAreas[0].AxisX.IntervalOffset = 1; chart1.Series[0].XValueType = ChartValueType.DateTime; DateTime minDate = new DateTime(2013, 01, 01).AddSeconds(-1); DateTime maxDate = new DateTime(2013, 05, 01); // or DateTime.Now; chart1.ChartAreas[0].AxisX.Minimum = minDate.ToOADate(); chart1.ChartAreas[0].AxisX.Maximum = maxDate.ToOADate(); }
Maybe I fixed your error on the way.
It works as expected: One line with four data points, all x-axes labels are visible and so is the graph itself. If there is still an issue, please provide full testing code along with a description of what should happen vs. what actually happens.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With