1
votes

I'm trying to initialize the axes of a chart in C# with the following parameters:

chart1.ChartAreas[0].AxisX.Maximum = 20;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisX.Interval = 5;

System.DateTime x1 = new System.DateTime(2016, 6, 8, 12, 00, 00);
System.DateTime x2 = new System.DateTime(2016, 6, 8, 23, 00, 00);

chart1.ChartAreas[0].AxisY.Maximum = x2.ToOADate();
chart1.ChartAreas[0].AxisY.Minimum = x1.ToOADate();

After running the above code I'm trying to add some data to the graph using:

 DateTime x = DateTime.Now;
 chart1.Series[0].Points.AddXY(i,x.ToOADate());

(where i gets incremented on subsequent data addition)

But, as soon as I initialize both the axis, the chart does not show any data.

If I don't set Y axis min and max, then it shows the following plot successfully.

demo plot

How can i initialize both the axes and continue to plot data in the chart?

Thanks!

----- UPDATE 1 -----

It's working now. Changes made: In designer view, for the properties of chart, Charting/Series/YValueType was changed to DateTime. Before it was only Time.

Thanks!

1
In AddXY(), make sure you're not mixing up x and y coordinates.jsanalytics
I tried your code and is working fine for me.sowjanya attaluri
@jstreet - It's looks okay. When the axes ranges are not put, it plots fine.chiro3110
@sowjanyaattaluri - that's strange. is it possible you can share the method? no change from the above code at all? P.S. I saw your question, already in stackoverflow about datetime on axis, but i not not that advanced in coding, could not understand it all. thanks!chiro3110

1 Answers

0
votes

I tried the below code, I placed chart control,button and timer in form.

DateTime minValue, maxValue;
    int i = 0;
    public Form6()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        chart1.ChartAreas[0].AxisY.LabelStyle.Format = "HH:mm:ss";

        minValue = DateTime.Now;       
        maxValue = minValue.AddSeconds(60);

        chart1.ChartAreas[0].AxisX.Maximum = 20;
        chart1.ChartAreas[0].AxisX.Minimum = 0;
        chart1.ChartAreas[0].AxisX.Interval = 5;

        System.DateTime x1 = new System.DateTime(2016, 6, 8, 12, 00, 00);
        System.DateTime x2 = new System.DateTime(2016, 6, 8, 23, 00, 00);

        chart1.ChartAreas[0].AxisY.Maximum = x2.ToOADate();
        chart1.ChartAreas[0].AxisY.Minimum = x1.ToOADate();

        timer1.Start();

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        DateTime x = DateTime.Now;
        chart1.Series[0].Points.AddXY(i, x.ToOADate());
        i++;
    }