Example #1
0
        /// <summary>
        /// Event handler for user request to plot data based on sensor # and time constraints.
        /// </summary>
        /// <param name="sender">Instance of object raising event.</param>
        /// <param name="e">Generic event args.</param>
        private void buttonChart_Click(object sender, EventArgs e)
        {
            TemperatureSensor temperatureSensor = GetSelectedTemperatureSensor();

            //UI Selection Validations.
            if (dateTimePickerStop.Value < dateTimePickerStart.Value)
            {
                MessageBox.Show("Time selection for stop cannot be less than start time.",
                                "Invalid Time Selections",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                return;
            }

            //Attempt to query data.
            if (temperatureSensor != null)
            {
                //Convert timestamps to UTC for MooseBox query.
                DateTime startTimeUTC = dateTimePickerStart.Value.ToUniversalTime();
                DateTime stopTimeUTC  = dateTimePickerStop.Value.ToUniversalTime();

                //Query temperature readings within selected range.
                List <Tuple <DateTime, Single> > readings = AsyncHelper.RunSync(() => { return(temperatureSensor.QueryHistoricalReadings(startTimeUTC, stopTimeUTC)); });

                //Clear the graph so we don't get stale date on redraws.
                Debug.Assert(chartTemperatureHistory.Series.Count > 0);

                chartTemperatureHistory.Series[0].Points.Clear();

                //Re-populate the chart with this batch of historical data.
                Single minFahrenheit = Single.MaxValue;
                Single maxFahrenheit = Single.MinValue;

                foreach (var reading in readings)
                {
                    Single   fahrenheit     = ConvertUnits.Temperature.CelsiusToFahrenheit(reading.Item2);
                    DateTime localTimestamp = reading.Item1.ToLocalTime();

                    //Add plot point.
                    chartTemperatureHistory.Series[0].Points.AddXY(localTimestamp, fahrenheit);

                    //Update minimum value.
                    if (fahrenheit < minFahrenheit)
                    {
                        minFahrenheit = fahrenheit;
                    }

                    //Update maximum value.
                    if (fahrenheit > maxFahrenheit)
                    {
                        maxFahrenheit = fahrenheit;
                    }
                }

                //Update the bounds/scaling so it looks better.
                chartTemperatureHistory.ChartAreas[0].AxisY.Minimum = minFahrenheit - TempPlotPadding;
                chartTemperatureHistory.ChartAreas[0].AxisY.Maximum = maxFahrenheit + TempPlotPadding;

                //Redraw the graph.
                chartTemperatureHistory.Update();
            }
        }