Microsoft C# Chart Control displays false data
I'm trying to display visits to different websites over time, and it works fine. But when I try to add another website which I start tracking after I started tracking the other sites, the chart displays it as if I started it together with the other sites. For example when I have those arrays:
{
dates[] { "8/1", "8/2", "8/3" }
visits[] { 100, 74, 96 }
}
{
dates[] { "8/1", "8/2", "8/3" }
visits[] { 52, 86, 23 }
}
It works fine.. But when I have those arrays:
{
dates[] { "8/1", "8/2", "8/3" }
visits[] { 100, 74, 96 }
}
{
dates[] { "8/3" }
visits[] { 52 }
}
It shows me the second as it had 52 visits on 8/1 and not on 8/3
Here's my code:
public void drawChart(List<object[]> data)
{
chart1.S开发者_Python百科eries.Clear();
for (int i = 0; i < data.Count; i = i + 3)
{
chart1.Series.Add(data[i+2][0].ToString());
chart1.Series[i / 3].Points.DataBindXY(data[i], data[i+1]);
chart1.Series[i / 3].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
chart1.Series[i / 3].IsValueShownAsLabel = true;
chart1.Series[i / 3].BorderWidth = 3;
}
}
The list contains the arrays, first array contains the dates, second one contains the visits and the third contains only one object which is the series name.
see Microsoft Chart Controls and X-Axis time scale format
Passing strings as X values turn them into dumb labels I think. You will need convert them to Date for the axis to be able to position them correctly then use label formatting to display what you want.
this.chart.ChartAreas["Default"].AxisX.LabelStyle.Format = "DD/MM";
Why not bind directly to DateTime?
IList<DateTime> listx = new List<DateTime>();
IList<double> listy = new List<double>();
//iterate and add your values to the two lists: listx and listy
//when you're done, bind the lists to the points collection
chart1.Series[i / 3].Points.DataBindXY(listx, listy);
Hope this helps :)
精彩评论