Jump to content
Software FX Community

Chart mChart = new Chart() fails


Delphin
 Share

Recommended Posts

private void CreateChartFiles
(string pLogin, string pCanonicalName, CReachabilityInfo pReachabilityInfo,  CUptimeInfo[] pUptimeInfoList, string pWorkingDirectory)
{
  if (pUptimeInfoList.Length == 0)
  {
  return;
  }

  bool bIsReachable = !pReachabilityInfo.IsUnreachable;

  // FAILS HERE ON = new Chart();

  Chart chart = new Chart();

  DataTable dtData = new DataTable();

  dtData.Columns.Add(new DataColumn("Old uptimes", typeof(double)));

  DataRow dtRow = null;

  double maxY = 0;
  double bestOldUptime = 0;
  double currentUptime = -1;

  if (bIsReachable)
  {
  currentUptime = pUptimeInfoList[0].NumberOfDays;
  }

  for (int i = pUptimeInfoList.Length - 1; i > 0; i--)
  {
  dtRow = dtData.NewRow();

  double dayUptime = pUptimeInfoList.NumberOfDays;

  dtRow["Old uptimes"] = dayUptime;

  dtData.Rows.Add(dtRow);

  if (dayUptime > bestOldUptime)
  {
  bestOldUptime = dayUptime;
  maxY = dayUptime;
  }
  }

  if (bIsReachable)
  {
  dtRow = dtData.NewRow();

  if (currentUptime < bestOldUptime)
  {
  dtRow["Old uptimes"] = bestOldUptime;

  dtData.Rows.Add(dtRow);

  dtRow = dtData.NewRow();
  }

  dtRow["Old uptimes"] = currentUptime;

  dtData.Rows.Add(dtRow);

  if (pUptimeInfoList[0].NumberOfDays > maxY)
        {
  maxY = pUptimeInfoList[0].NumberOfDays;
  }
  }

  chart.DataSource = dtData;

  chart.Data.Series = 1;

  if (bIsReachable)
  {
  if (currentUptime < bestOldUptime)
  {
  CustomLegendItem customLegendItem_1 = new CustomLegendItem();
  CustomLegendItem customLegendItem_2 = new CustomLegendItem();

  customLegendItem_1.Color = Color.Red;
  customLegendItem_1.MarkerShape = MarkerShape.Rect;
  customLegendItem_1.Text = "Best uptime";

  customLegendItem_2.Color = Color.Green;
  customLegendItem_2.MarkerShape = MarkerShape.Rect;
  customLegendItem_2.Text = "Current uptime";

  chart.LegendBox.CustomItems.Add(customLegendItem_1);
  chart.LegendBox.CustomItems.Add(customLegendItem_2);

chart.Points[chart.Data.Points - 2].Color = Color.Red;
chart.Points[chart.Data.Points - 1].Color = Color.Green;
  }
  else
  {
  CustomLegendItem customLegendItem = new CustomLegendItem();

  customLegendItem.Color = Color.Green;
  customLegendItem.MarkerShape = MarkerShape.Rect;
  customLegendItem.Text = "Current uptime";

  chart.LegendBox.CustomItems.Add(customLegendItem);

  chart.Points[chart.Data.Points - 1].Color = Color.Green;
  }
  }

  chart.AxisY.Max = maxY + 10;

  chart.AxisX.Visible = false;

  chart.Gallery = Gallery.Bar;

  chart.AxisY.Title.Text = "Days";

  chart.LegendBox.Dock = DockArea.Bottom;
  chart.LegendBox.Border = DockBorder.None;

  CUptimeImageInfo uptimeImageInfo = m_Settings.UserOutputsInfo.UptimeImageInfo;

  chart.Height = uptimeImageInfo.Height;
  chart.Width  = uptimeImageInfo.Width;

  string sTitle = pCanonicalName;

  if (!bIsReachable)
  {
    sTitle += " [unreachable]";
  }

  TitleDockable title = new TitleDockable(sTitle);

  title.Font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Bold);

  chart.Titles.Add(title);

  string sSubTitle = string.Format("{0}, {1}", CUtilities.GetStrCurrentDate(), CUtilities.GetStrCurrentTime());

  TitleDockable subTitle = new TitleDockable(sSubTitle);

  subTitle.Font = new Font(FontFamily.GenericMonospace, 9);
  subTitle.TextColor = Color.Gray;

  chart.Titles.Add(subTitle);

  chart.Border = new SimpleBorder(SimpleBorderType.Color, Color.Black);

  chart.Background = new SolidBackground(Color.White);

  string chartFilePath = Path.Combine(pWorkingDirectory, uptimeImageInfo.FileName);

  CChartFxTools.Save(chart, chartFilePath, eImageFormat.Png);
}

CChartFxTools.Save() declaration:

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

using Net.ServPlex.Service.Constants;

using ChartFX.WinForms;

namespace Net.ServPlex.Service.ChartFxTools
{
  public static class CChartFxTools
  {
  public static void Save(Chart pChart, string pImagePath, eImageFormat pImageFormat)
  {
  string bmpFilePath = Path.ChangeExtension(pImagePath, ".bmp");

  pChart.Export(FileFormat.Bitmap, bmpFilePath);

  Convert(bmpFilePath, pImageFormat);
  }

  private static void Convert(string pBmpFile, eImageFormat pImageFormat)
  {
  if (pBmpFile == null)
  {
  throw new ArgumentNullException("The BmpFile parameter cannot be null");
  }

  if (!File.Exists(pBmpFile))
  {
  throw new FileNotFoundException("Cannot find the " + pBmpFile + " file");
  }

  Bitmap bitmap = null;

  ImageFormat imageFormat = GetImageFormat(pImageFormat);

  string newFilePath = Path.ChangeExtension(pBmpFile, pImageFormat.ToString().ToLower());

  if (File.Exists(newFilePath))
  {
  try
  {
  File.Delete(newFilePath);
  }
  catch(System.Exception x)
  {
  }
  }

  try
  {
  bitmap = new Bitmap(pBmpFile);
  bitmap.Save(newFilePath, imageFormat);
  }
  catch (System.Exception x)
  {
  throw;
  }
  finally
  {
  #region [ Cleaning memory ]

  if (bitmap != null)
  {
  bitmap.Dispose();
  bitmap = null;
  }

  try
  {
  File.Delete(pBmpFile);
  }
  catch (System.Exception x)
  {
  }

  #endregion
  }
  }

  private static ImageFormat GetImageFormat(eImageFormat pImageFormat)
  {
  switch (pImageFormat)
  {
  case eImageFormat.Jpeg:
  return ImageFormat.Jpeg;

  case eImageFormat.Gif:
  return ImageFormat.Gif;

  case eImageFormat.Png:
  return ImageFormat.Png;
  }

  return ImageFormat.Png;
  }
  }
}


CreateChartFiles() method is called in a while(true) with no break + Sleep(). Error occures after ~2 days.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...