Exemple #1
0
        /// <summary>
        /// Draws the graphs and saves them to local files
        /// </summary>
        public void DrawGraphs()
        {
            StringBuilder log = new StringBuilder();
            log.AppendLine("starting DrawGraphs..");

            try
            {
                List<Station> stations = Station.ReadListOfStations(StationXmlFile);
                List<Channel> channels = Channel.ReadFromXml(StationXmlFile);

                DataManager m = DataManager.CreateDefault();
                DateTime end = DateTime.Now.Date.AddHours(DateTime.Now.Hour);
                int interval = ReadIntervalFromConfig();
                DateTime start = end.AddDays(interval * (-1));
                TimeSeries ts = new TimeSeries(start, end);
                TimeSeries curTs;
                List<TimeSeries> tslist = new List<TimeSeries>(4);
                ChartEngine eng = ChartEngine.FromConfigFile(StationXmlFile, GraphDir);
                ChartLabelInfo chi = new ChartLabelInfo();
                Channel curCh;
                chi.UnitsName = "mm";
                chi.Copyright = "Data: KFGG PřF UK Praha";
                chi.ChannelName = "hladina";
                string fileName;

                List<Channel> stch;
                List<Channel> chTypeList;
                List<string> labelList = new List<string>();
                Hashtable stHash = new Hashtable();

                foreach (Station st in stations)
                {
                    stch = Channel.FindByStation(channels, st.Id);
                    chi.StationName = st.Label;

                    //add channels to hashtable
                    //the hashtable now contains lists of multiple channels
                    stHash.Clear();
                    foreach (Channel ch in stch)
                    {
                        if (stHash.ContainsKey(ch.ChType))
                        {
                            ((List<Channel>)stHash[ch.ChType]).Add(ch);
                        }
                        else
                        {
                            stHash.Add(ch.ChType, new List<Channel>(4));
                            ((List<Channel>)stHash[ch.ChType]).Add(ch);
                        }
                    }

                    m.LocalDataDir = LocalDataDir;
                    foreach (DictionaryEntry de in stHash)
                    {
                        chTypeList = (List<Channel>)de.Value; //list of channels of same type
                        if (chTypeList.Count == 1)
                        {
                            curCh = chTypeList[0];
                            fileName = curCh.ChType + "-" + st.Name;
                            chi.ChannelName = curCh.ChLabel;
                            chi.UnitsName = curCh.Unit;

                            m.LoadObservationsFromFile(curCh.StId, curCh, start, end, ts);

                            //m.LoadObservations(curCh.StId, curCh, start, end, ts);
                            eng.CreateChart(ts, chi, curCh.ChType, fileName);
                        }
                        else if (chTypeList.Count > 1)
                        {
                            tslist.Clear();
                            labelList.Clear();
                            foreach (Channel ch in chTypeList)
                            {
                                curTs = new TimeSeries(start, end);
                                m.LoadObservationsFromFile(ch.StId, ch, start, end, curTs);
                                tslist.Add(curTs);
                                labelList.Add(ch.ChLabel);
                            }
                            chi.ChannelName = chTypeList[0].ChLabel;
                            if (chi.ChannelName.IndexOf(" ") >= 0)
                            {
                                chi.ChannelName = chi.ChannelName.Remove(chi.ChannelName.LastIndexOf(" "));
                            }
                            chi.UnitsName = chTypeList[0].Unit;

                            fileName = chTypeList[0].ChType + "-" + st.Name;
                            eng.CreateChart(tslist, chi, chTypeList[0].ChType, labelList, fileName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.AppendLine("DrawGraphs ERROR - " + ex.Source + " " + ex.Message);
            }
            finally
            {
                log.AppendLine("finished DrawGraphs..");
                Logger logger = new Logger(logFile);
                logger.WriteMessage(log.ToString());
            }
        }
        //plot chart titles and other text description, setup the axes etc.
        private void SetupChart(ITimeSeries ts, ChartLabelInfo chartText, string chType, GraphPane pane)
        {
            //Station st = ts.Station;
            //Variable var = ts.Variable;
            DateTime start = ts.Start;
            DateTime end = ts.End;
            DrawTitle(pane, chartText.StationName, chartText.ChannelName, start, end);

            SetupAxis(pane, chType, chartText, start, end);
            SetupGrid(pane, chType);
            SetupLegend(pane);
        }
        /// <summary>
        /// Overloaded version --- draws the chart for multiple time series (max.3)
        /// </summary>
        /// <param name="tsList"></param>
        /// <param name="chList"></param>
        /// <param name="file"></param>
        public void CreateChart(List<TimeSeries> tsList, ChartLabelInfo chli, string chartType,
            List<string> chNames, string file)
        {
            Bitmap bmp;

            GraphPane pane = SetupGraphPane();

            SetupChart(tsList[0], chli, chartType, pane);

            PlotMultiChart(tsList, chNames, pane);

            bmp = ExportGraph(pane);

            string filePath = Path.Combine(_dir, file + ".png");
            bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
            bmp.Dispose();

            //draws the 'thumb' image
            DrawThumb(pane, filePath.Replace(".png", "-m.png"));
        }
        private void SetupAxis(GraphPane myPane, string chartType, ChartLabelInfo chText, 
            DateTime minDate, DateTime maxDate)
        {
            //setup font for axis name and tick marks text
            int scaleFontSize = 16;
            int titleFontSize = 18;

            // X axis
            ZedGraph.Axis myXAxis = myPane.XAxis;
            myXAxis.Type = AxisType.Date;
            myXAxis.Scale.Min = XDate.DateTimeToXLDate(minDate);
            myXAxis.Scale.Max = XDate.DateTimeToXLDate(maxDate);
            myXAxis.Scale.FontSpec.Size = scaleFontSize;
            myXAxis.Scale.Format = "dd.MM";
            myPane.XAxis.Title.FontSpec.Size = titleFontSize;

            // Y axis
            ZedGraph.Axis myYAxis = myPane.YAxis;

            myYAxis.Scale.FontSpec.Size = scaleFontSize;
            myYAxis.Title.Text = chText.ChannelName + " (" + chText.UnitsName + ")";
            myYAxis.Title.FontSpec.Size = titleFontSize;

            // data Copyright
            myPane.XAxis.Title.Text = chText.Copyright;

            // Y axis - setup for water stage
            if (chartType == "hla" || chartType == "rad" || chartType == "sra")
            {
                myYAxis.Scale.Min = 0;
            }

            if (chartType == "vlh")
            {
                myYAxis.Scale.Max = 100;
            }

            // Y2 axis - setup for cumulative precipitation
            if (chartType == "sra")
            {
                myYAxis.Title.Text = "srážky (mm/h)";

                ZedGraph.Axis myY2Axis = myPane.Y2Axis;
                myY2Axis.Scale.Min = 0;
                myY2Axis.Scale.FontSpec.Size = scaleFontSize;

                myY2Axis.Title.Text = "suma srážek (mm)";
                myY2Axis.Title.FontSpec.Size = titleFontSize;
                myY2Axis.IsVisible = true;
            }
        }
        /// <summary>
        /// Creates a chart of given type from the time series
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="labels"></param>
        /// <param name="chartType"></param>
        /// <returns></returns>
        public void CreateChart(ITimeSeries ts, ChartLabelInfo chartText, string chartType, string file)
        {
            Bitmap bmp;

            GraphPane pane = SetupGraphPane();
            SetupChart(ts, chartText, chartType, pane);
            PlotChartForSensor(ts, pane, chartType);

            if (ts.Count == 0 || Math.Abs(ts.MaxValue - ts.MinValue) < 0.001)
            {
                AddText(pane,"čidlo mimo provoz");
            }

            bmp = ExportGraph(pane);

            string filePath = Path.Combine(_dir , file + ".png");
            bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
            //DrawThumb(bmp, file);
            bmp.Dispose();

            //draws the 'thumb' image
            //DrawThumb2(ts, chartType, filePath.Replace(".png", "-m.png"));

            DrawThumb(pane, filePath.Replace(".png", "-m.png"));
        }