Search Icon

Adding a custom chart to a report

In a previous posting ( http://o-calcpro.com/wiki/2015/12/02/creating-a-real-report/ ) we saw how to create a simple report. In that case it was the Applied Load report. In this article we are going to extend that report by adding a chart of the heights.

O-Calc includes a graphing API that is used to generate all of the graphs that appear on the “Charts” tab of the main interface. To keep a consistent look and feel you can take advantage of that same API when defining your reports.

The following code would be added to the bottom of the “AddPole” (immediately after the FinalizeTable(); cDocument.LastSection.AddParagraph(“\n”); code) method of the report defined in the previous article (see link at the top of this article) and will result in the generation of the chart and its inclusion in the resulting report. See inline documentation for an understanding of the code.

  1. using (ZedGraphControl zedGraph = new ZedGraphControl ( ) )
  2.             {
  3.                 //create the report
  4.  
  5.                 GraphPane graphPane = zedGraph . GraphPane ;
  6.                 graphPane . CurveList . Clear ( ) ;
  7.  
  8.                 // Set the titles and axis labels and get data
  9.                 graphPane . Title . Text = "Load Heights" ;
  10.                 graphPane . XAxis . Title . Text = " " ;
  11.                 if (PPLGridModel . Attribute . UnitsConvention == PPLGridModel . Attribute . UNITS_CONVENTION . METRIC )
  12.                 {
  13.                     graphPane . YAxis . Title . Text = "Height Meters" ;
  14.                 }
  15.                 else
  16.                 {
  17.                     graphPane . YAxis . Title . Text = "Height Feet" ;
  18.                 }
  19.  
  20.  
  21.                 zedGraph . RestoreScale (graphPane ) ;
  22.                 graphPane . YAxis . Type = AxisType . Linear ;
  23.                 graphPane . YAxis . Scale . Mag = 0 ;
  24.                 graphPane . YAxis . Scale . MagAuto = false ;
  25.                 graphPane . YAxis . Scale . IsUseTenPower = false ;
  26.                 graphPane . XAxis . Type = AxisType . Linear ;
  27.                 graphPane . XAxis . Scale . Mag = 0 ;
  28.                 graphPane . XAxis . Scale . MagAuto = false ;
  29.                 graphPane . XAxis . Scale . IsUseTenPower = false ;
  30.                 graphPane . XAxis . MinorGrid . IsVisible = false ;
  31.                 graphPane . YAxis . MinorGrid . IsVisible = false ;
  32.  
  33.                 PointPairList points = new PointPairList ( ) ;
  34.  
  35.                 foreach (ForceSummary fs in loads )
  36.                 {
  37.                     PointPair pp ;
  38.                     if (PPLGridModel . Attribute . UnitsConvention == PPLGridModel . Attribute . UNITS_CONVENTION . METRIC )
  39.                     {
  40.                         pp = new PointPair ( 0, PPLGridModel . Attribute . InchesToMeters (fs . cHeightOfForceApplicationAboveGLinInches ) ) ;
  41.                     }
  42.                     else
  43.                     {
  44.                         pp = new PointPair ( 0, PPLGridModel . Attribute . InchesToFeet (fs . cHeightOfForceApplicationAboveGLinInches ) ) ;
  45.                     }
  46.                     points . Add (pp ) ;
  47.                 }
  48.                 //add the groundline
  49.                 points . Add ( 0, 0 ) ;
  50.  
  51.                 LineItem curve = graphPane . AddCurve ( "Attachment Points", points, Color . Blue, SymbolType . Star ) ;
  52.                 curve . Line . Width = 2 ;
  53.  
  54.  
  55.                 //Add the chart to the report
  56.                 zedGraph . Width = 640 ;
  57.                 zedGraph . Height = 500 ;
  58.                 // signal we are done and rendering can begin
  59.                 zedGraph . AxisChange ( ) ;
  60.  
  61.                 //get the resulting chart as a bitmap
  62.                 Image img = zedGraph . ImageRender ( ) ;
  63.  
  64.                 //save the bitmap to a temp file
  65.                 String fname = System.IO . Path . GetTempFileName ( ) + ".jpg" ;
  66.                 img . Save (fname ) ;
  67.  
  68.                 try
  69.                 {
  70.                     int imgWidth = 0 ;
  71.                     int imgHeight = 0 ;
  72.  
  73.                     using ( System.Drawing . Image bimage = PPL_CorvusImageViewer . CorvusBitmap . FromFile (fname ) )
  74.                     {
  75.                         imgWidth = bimage . Width ;
  76.                         imgHeight = bimage . Height ;
  77.                         bimage . Dispose ( ) ;
  78.                     }
  79.  
  80.                     MigraDoc . DocumentObjectModel . Section section = cDocument . LastSection ;
  81.                     MigraDoc . DocumentObjectModel . Paragraph paragraph = section . LastParagraph ;
  82.  
  83.                     double printableAreaWidth = ReportFont . cPageWidth ;
  84.                     double printableAreaHeight = printableAreaWidth * 3.0 / 4.0 ;
  85.  
  86.                     MigraDoc . DocumentObjectModel . Shapes . Image image = paragraph . AddImage (fname ) ;
  87.                     paragraph . Format . Alignment = MigraDoc . DocumentObjectModel . ParagraphAlignment . Center ;
  88.                     double scaleWidth = printableAreaWidth / ( ( double )imgWidth ) ;
  89.                     double scaleHeight = printableAreaHeight / ( ( double )imgHeight ) ;
  90.                     image . Height = new MigraDoc . DocumentObjectModel . Unit (printableAreaHeight, MigraDoc . DocumentObjectModel . UnitType . Inch ) ;
  91.                 }
  92.                 catch { }
  93.             }

Contact your local Osmose professional.