简体   繁体   中英

C# chart and datetime as X Axis

I use this code to draw a chart in C# 4

Series current = chart1.Series.Add("current");
    current.AxisLabel = "current";
    current.ChartType = SeriesChartType.Line;
    string[] xaxis = ClsPath.dt.AsEnumerable()
     .Select(row => row.Field<string>("Time"))
     .ToArray();

    string[] yaxis1 = ClsPath.dt.AsEnumerable()
   .Select(row => row.Field<string>("M_1_CURRENT"))
   .ToArray();
    int i = 0;
    foreach (string s in xaxis)
    {
        chart1.Series["current"].Points.AddXY(Convert.ToDateTime(s), yaxis1[i++]);
    }
    chart1.Series["current"].XValueType = ChartValueType.DateTime;
       chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
 chart1.ChartAreas[0].AxisX.Interval = 1;

chart is drown without xaxis. I clear this lines

Series current = chart1.Series.Add("current");
current.AxisLabel = "current";
current.ChartType = SeriesChartType.Line;
string[] xaxis = ClsPath.dt.AsEnumerable()
 .Select(row => row.Field<string>("Time"))
 .ToArray();

string[] yaxis1 = ClsPath.dt.AsEnumerable()
   .Select(row => row.Field<string>("M_1_CURRENT"))
   .ToArray();
    int i = 0;
    foreach (string s in xaxis)
    {
        chart1.Series["current"].Points.AddXY(s, yaxis1[i++]);
    }

now it show only two xaxis value. I use databindxy and it raise an error that Y values cannot bind to string object. is there anyway to show all x values in chart?

PS : I use DataBindCrossTable (chart1.DataBindCrossTable(dv, "Time", "Time", "M_1_CURRENT", "");) and result is 在此处输入图片说明

Have you tried something like this? http://msdn.microsoft.com/en-us/library/vstudio/dd488514(v=vs.100).aspx

current.DataBindCrossTable(dv, "series", "Time", "M_1_CURRENT", "");

Hope this helps!

from msdn page

public void DataBindCrossTable(
IEnumerable dataSource,
string seriesGroupByField,
string xField,
string yFields,
string otherFields)

the second parameter should be a value that is the same for all data of the line like the same data here

+--------+------------+-------------+
| series |    Time    | M_1_CURRENT |
+--------+------------+-------------+
| serie1 | 2013-11-12 | 1           |
| serie1 | 2013-11-13 | 1.5         |
| serie1 | 2013-11-14 | 1.9         |
| serie1 | 2013-11-15 | 1.2         |
+--------+------------+-------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM