none
C# chart - X Axis in hours, Data provided in seconds RRS feed

  • Question

  • Hi there,

    I hope somebody can help me.

    I'm trying to create a chart in c#.

    My Data is provided in timestamps in seconds and other data.

    My Problem is to create the x axis with hour 0 - 23h.

    My Code so far:

     double[] timestamps = { 0, 337, 675, 1012, 1350, 1687, 2025, 2362, 2700, 3037, 3375, 3712, 4050, 4387, 4725, 5062, 5400, 5737, 6075, 6412, 6750, 7087, 7425, 7762, 8100, 8437, 8775, 9112, 9450, 9787, 10125, 10462, 10800, 11137, 11475, 11812, 12150, 12487, 12825, 13162, 13500, 13837, 14175, 14512, 14850, 15187, 15525, 15862, 16200, 16537, 16875, 17212, 17550, 17887, 18225, 18562, 18900, 19237, 19575, 19912, 20250, 20587, 20925, 21262, 21600, 21937, 22275, 22612, 22950, 23287, 23625, 23962, 24300, 24637, 24975, 25312, 25650, 25987, 26325, 26662, 27000, 27337, 27675, 28012, 28350, 28687, 29025, 29362, 29700, 30037, 30375, 30712, 31050, 31387, 31725, 32062, 32400, 32737, 33075, 33412, 33750, 34087, 34425, 34762, 35100, 35437, 35775, 36112, 36450, 36787, 37125, 37462, 37800, 38137, 38475, 38812, 39150, 39487, 39825, 40162, 40500, 40837, 41175, 41512, 41850, 42187, 42525, 42862};
           double[] peaks = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 666, 2443, 3998, 6072, 8293, 10663, 12662, 15254, 17920, 20067, 22807, 25621, 27917, 30805, 33619, 36433, 38728, 41468, 44134, 46281, 48799, 51243, 53094, 55241, 57315, 58796, 60499, 62054, 63461, 64424, 65460, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65312, 64868, 64350, 63831, 63387, 62795, 62202, 61684, 61091, 60425, 59833, 59166, 58426, 57611, 57019, 56130, 55241, 54501, 53538, 52576, 51687, 50576, 49465, 48207, 47244, 45911, 44504, 43319, 41838, 40283, 39024, 37321, 35618, 34211, 32434, 30582, 28731, 27250, 25251, 23326, 21696, 19697, 17624, 15994, 13921, 11848, 9700, 7997, 5850, 3702, 1999 };
                  
    
                Series peak_white = new Series("White");
               
    
                for (int i = 0; i < timestamps.Length; i++)
                {
    
                    peak_white.Points.AddXY(timestamps[i]/3600, peaks[i]);
    
                }
    
                peak_white.Name = "White";
                peak_white.Color = Color.Yellow;
                peak_white.ChartType = SeriesChartType.Spline;
                            
                peak_white.ToolTip = "#SERIESNAME : X=#VALX, Y=#VALY";
                peak_white.MarkerSize = 5;
                peak_white.MarkerStyle = MarkerStyle.Square;
            
             
                chart1.ChartAreas[0].AxisY.Minimum = 0;
                chart1.ChartAreas[0].AxisY.Maximum = 65535;
                chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;
                chart1.ChartAreas[0].AxisX.Minimum = 0;
                chart1.ChartAreas[0].AxisX.Maximum = 23;
                chart1.ChartAreas[0].AxisX.Interval = 1;
                chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm";
    
    
    
                chart1.Series.Add(peak_white);
    
            }
    
    
        }
    }
    
    

    Kind regards,

    Andreas

    Saturday, February 2, 2019 9:30 PM

All replies

  • Hi,

    how is you output currently? Can you show us a screenshot?

    Saturday, February 2, 2019 9:46 PM
  • Unfortunately not at the moment. My account needs to be verified???
    Saturday, February 2, 2019 10:01 PM
  • Hi AndiAA,

    Since the type of your time is double, you can directly add the X-axis data without setting its display type to "hour".

    The modified code is shown as follows:

        // Code omitted
        // ...
        // Code omitted
        //chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;
        chart1.ChartAreas[0].AxisX.Minimum = 0;
        chart1.ChartAreas[0].AxisX.Maximum = 23;
        chart1.ChartAreas[0].AxisX.Interval = 1;
        //chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm";
        chart1.Series.Add(peak_white);

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Wednesday, February 13, 2019 2:26 AM
    Monday, February 4, 2019 2:34 AM