MultiPlot.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Text;
  6. namespace ScottPlot
  7. {
  8. [Obsolete("This class will be deleted in a future version. See ScottPlot FAQ for details.")]
  9. public class MultiPlot
  10. {
  11. public readonly Plot[] subplots;
  12. public readonly int rows, cols;
  13. public readonly int width, height;
  14. private readonly Bitmap bmp;
  15. private readonly Graphics gfx;
  16. public int subplotCount { get { return rows * cols; } }
  17. public int subplotWidth { get { return width / cols; } }
  18. public int subplotHeight { get { return height / rows; } }
  19. public MultiPlot(int width = 800, int height = 600, int rows = 1, int cols = 1)
  20. {
  21. if (rows < 1 || cols < 1)
  22. throw new ArgumentException("must have at least 1 row and column");
  23. this.width = width;
  24. this.height = height;
  25. this.rows = rows;
  26. this.cols = cols;
  27. bmp = new Bitmap(width, height);
  28. gfx = Graphics.FromImage(bmp);
  29. subplots = new Plot[subplotCount];
  30. for (int i = 0; i < subplotCount; i++)
  31. subplots[i] = new Plot(subplotWidth, subplotHeight);
  32. }
  33. private void Render()
  34. {
  35. gfx.Clear(Color.White);
  36. int subplotIndex = 0;
  37. for (int row = 0; row < rows; row++)
  38. {
  39. for (int col = 0; col < cols; col++)
  40. {
  41. Bitmap subplotBmp = subplots[subplotIndex++].Render(subplotWidth, subplotHeight, false);
  42. Point pt = new Point(col * subplotWidth, row * subplotHeight);
  43. gfx.DrawImage(subplotBmp, pt);
  44. }
  45. }
  46. }
  47. public Bitmap GetBitmap()
  48. {
  49. Render();
  50. return bmp;
  51. }
  52. public void SaveFig(string filePath)
  53. {
  54. filePath = System.IO.Path.GetFullPath(filePath);
  55. string fileFolder = System.IO.Path.GetDirectoryName(filePath);
  56. if (!System.IO.Directory.Exists(fileFolder))
  57. throw new Exception($"ERROR: folder does not exist: {fileFolder}");
  58. ImageFormat imageFormat;
  59. string extension = System.IO.Path.GetExtension(filePath).ToUpper();
  60. if (extension == ".JPG" || extension == ".JPEG")
  61. imageFormat = ImageFormat.Jpeg; // TODO: use jpgEncoder to set custom compression level
  62. else if (extension == ".PNG")
  63. imageFormat = ImageFormat.Png;
  64. else if (extension == ".TIF" || extension == ".TIFF")
  65. imageFormat = ImageFormat.Tiff;
  66. else if (extension == ".BMP")
  67. imageFormat = ImageFormat.Bmp;
  68. else
  69. throw new NotImplementedException("Extension not supported: " + extension);
  70. Render();
  71. bmp.Save(filePath, imageFormat);
  72. }
  73. public Plot GetSubplot(int rowIndex, int columnIndex)
  74. {
  75. if (rowIndex < 0 || rowIndex >= rows)
  76. throw new ArgumentException("invalid row index");
  77. if (columnIndex < 0 || columnIndex >= cols)
  78. throw new ArgumentException("invalid column index");
  79. int subplotIndex = rowIndex * cols + columnIndex;
  80. return subplots[subplotIndex];
  81. }
  82. }
  83. }