public void ProcessRequest(HttpContext context) { //set this to TRUE if files are to be saved on the disk bool saveToDisk = false; //declaration of local variables string path = context.Request.Path.ToLower(); System.Collections.Specialized.NameValueCollection queryParams = context.Request.QueryString; // process the query string parameters string imageName = path; int slashIndex = path.LastIndexOf("/"); if (slashIndex >= 0) { imageName = path.Remove(0, slashIndex + 1); } string Name = string.Empty; System.Drawing.Bitmap bmp; ChartEngine engine = new ChartEngine(); if (saveToDisk) { string physicalPath = HttpContext.Current.Request.PhysicalApplicationPath; string parent = physicalPath.Substring(0, physicalPath.LastIndexOf("\\")); string TempDir = (Directory.GetParent(physicalPath)).FullName + "\\charts\\"; string ImagePath = System.IO.Path.Combine(TempDir, imageName + ".png"); if (File.Exists(ImagePath)) { // is the image already exists, return the existing file bmp = new System.Drawing.Bitmap(ImagePath); } else { bmp = engine.GenerateImage(imageName); SaveImageToDisk(bmp, ImagePath); } } else { bmp = engine.GenerateImage(imageName); } //ProcessBitmap will send the bitmap as a http binary stream //so that it can be displayed as an image by the user's browser ProcessBitmap(bmp, context); }
/// <summary> /// Generates a new 'chart' image to be written to http output stream. /// Also saves the newly generated image to file on disk. /// </summary> /// <param name="ImagePath">Http request path of the chart image</param> public Bitmap GenerateImage(string ImagePath) { Bitmap bmp; ChartEngine ChartGen = new ChartEngine(); Width = 600; Height = 275; ShowText = true; QueryStringValidator QVal = new QueryStringValidator(ImagePath); QVal.Validate(); //the language and culture is also set in the validator if (QVal.IsValid) { // get query string parameters string lang = QVal.Culture; int StId = QVal.StationId; Variable curVar = QVal.Variable; DateTime startTime = QVal.StartDate; DateTime endTime = QVal.EndDate; // TODO: for longer intervals change curVar to [precip_day] // search for the channel corresponding to station/variable combination ChannelSearchResult found = ChannelSearchEngine.FindByStationIdAndVariable( StId, curVar); if (!found.HasMatchingStations) //no matching station { return ChartGen.CreateErrorChart(found.ErrorMessage); } if (found.HasMatchingChannels) //correct results found { Channel ch = found.Channels[0]; ITimeSeries ts; TimeInterval interval = new TimeInterval(startTime, endTime); MessageWriter writer = new MessageWriter(ch.Station.Name, ch.Variable.Name); // for precipitation, if it's hourly and a longer period, // always select daily precipitation to display a readable graph // otherwise, hourly precipitation will be displayed. int maxHourPrecipPeriod = 10; VariableEnum var = ch.Variable.VarEnum; if (var == VariableEnum.PrecipHour && interval.Length.TotalDays > maxHourPrecipPeriod) { var = VariableEnum.Precip; } //here we retrieve the time series ts = TimeSeriesManager.GetTimeSeries(ch, interval); if (ts.Count == 0) //this also means 'no data for time series' { bmp = ChartGen.CreateErrorChart (writer.WriteErrorMessage(MessageType.NoDataForPeriod)); } bmp = ChartGen.CreateChart(ch, ts); } else { // incorrect input - create a 'chart' with an error message bmp = ChartGen.CreateErrorChart(found.ErrorMessage); } } else { // incorrect input - create a 'chart' with an error message bmp = ChartGen.CreateErrorChart(QVal.ErrorMessage); } return bmp; }