Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Want to print grid lines along with axis and scatterplot

I am using Measurement Studio v 8.1 inside Visual Studio 2005.  I reviewed online help as well as the example code for printing found in the Measurement Studio help folders for Visual Studio 2005 and have managed to get a printout of my scattergraph data and both the x-axis and y-axis.  I have not yet been able to figure out how to get a print out of the gridlines for the x-axis and y-axis.  Below is the relevant code.  A button is pressed (btnPrint) and the plot in the form is printed to the default printer and sized according to the printer settings.  Any help would be greatly appreciated.

Regards,

Peter

private void btnPrint_Click(object sender, EventArgs e)
        {
            using (PrintDocument document = new PrintDocument())
            {
                    document.PrintPage += GetPrintPageEventHandler();
                    document.Print();
            }
        }

        private PrintPageEventHandler GetPrintPageEventHandler()
        {
            PrintPageEventHandler handler = new PrintPageEventHandler(PrintPageCalibrationData);

            return handler;
        }

        private void PrintPageCalibrationData(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;

            Rectangle plotBounds, xAxisBounds, yAxisBounds;
            GetElementBounds(e, out plotBounds, out xAxisBounds, out yAxisBounds);

            xAxis1.Draw(new ComponentDrawArgs(g, xAxisBounds), XAxisPosition.Bottom);
            yAxis1.Draw(new ComponentDrawArgs(g, yAxisBounds), YAxisPosition.Left);
            // We need to draw the division lines here.
           
            // Draw plot outline
            g.DrawRectangle(new Pen(Brushes.Black), plotBounds);
           
            scatterPlot1.Draw(new ComponentDrawArgs(g, plotBounds));
        }

        private void GetElementBounds(PrintPageEventArgs e, out Rectangle plotBounds, out Rectangle xAxisBounds, out Rectangle yAxisBounds)
        {
            Graphics g = e.Graphics;
            // Find page dimensions and resize bounding rectangle accordingly
            Rectangle bounds = Rectangle.Inflate(e.MarginBounds, 0, -2 * e.PageSettings.Margins.Top);

            Rectangle originalXAxisBounds = xAxis1.GetBounds();
            Rectangle originalYAxisBounds = yAxis1.GetBounds();

            plotBounds = new Rectangle(bounds.X + originalYAxisBounds.Width, bounds.Y + originalXAxisBounds.Height, bounds.Width - originalYAxisBounds.Width, bounds.Height - (originalXAxisBounds.Height * 2));

            xAxisBounds = xAxis1.GetBounds(g, plotBounds);
            yAxisBounds = yAxis1.GetBounds(g, plotBounds);
        }
0 Kudos
Message 1 of 13
(6,048 Views)
Hi Peter,
 
Adding printing to the example: C:\Program Files\National Instruments\MeasurementStudioVS2005\DotNET\Examples\Application\SimpleGraph\cs\SimpleGraph.2005.sln
 
prints the grids.  So I'm assuming the problem is getting the grids on the graph.
 
Here is an example of programmatically setting horizontal grids for the major divisions:

sampleWaveformGraph.YAxes[0].MajorDivisions.GridVisible =

true;

Cheers,

David Goldberg
National Instruments
Software R&D
0 Kudos
Message 2 of 13
(6,020 Views)
I will look at that example.  But no, I am having no trouble with the placing either major or minor gridlines or making them visible on the graph.  It is only in printing that they do not appear.  

I tried to add my printing methods to the simpleGraph application you mentioned, changing xAxis1 and yAxis1 to xAxis and yAxis and scatterPlot1 to scrollingSineWave.  The same thing that happens in my program happens here.  You said you were able to print the gridlines with no problem.  So either your code is different or something more sinister is going on with my printer settings or something I don't understand.  This really shouldn't be difficult, I'm just not understanding something.

Thanks Again,
Peter
0 Kudos
Message 3 of 13
(6,017 Views)
Hi Peter,

You need to call the Graph.DrawGridLines method.  For example, you could say:

xAxis.MajorDivisions.GridColor = Color.Red;
xAxis.MajorDivisions.GridLineStyle = LineStyle.Solid;
xAxis.MajorDivisions.GridVisible = true;

xAxis.Draw(new ComponentDrawArgs(g, xAxisBounds), XAxisPosition.Bottom);
sineWavePlot.Draw(new ComponentDrawArgs(g, plotBounds));

sampleWaveformGraph.DrawGridLines(new ComponentDrawArgs(g, plotBounds));

The code above is applicable to the Printing shipping example.

Hope this helps!
 
Best Regards,

Message Edited by Jonathan N on 05-11-2007 04:15 PM

Jonathan N.
National Instruments
0 Kudos
Message 4 of 13
(6,014 Views)
Thanks a lot. Graph.DrawGridLines solved all of my problems

Regards,

Peter
0 Kudos
Message 5 of 13
(5,994 Views)
Hi there!

Thanks for all these instructions and examples, printing works very well.

BUT I'm having problems with the grid lines as well.

Everything prints fine as long as first and last major divisions are at the range minimum/maximum,
but if this is not the case, the grid lines appear at the wrong positions (they do not line up with the axis ticks, but are off by some amount).

I'm using NI Measurement Studio 8 with the same code as posted in this thread.

Yours sincerely

Wilfried Nesensohn

Message Edited by wnesensohn on 08-30-2007 08:58 AM

0 Kudos
Message 6 of 13
(5,745 Views)
Hi Wilfried,

Could you post your program (or at least part of it) so I can reproduce the issue on my machine?

Regards,
0 Kudos
Message 7 of 13
(5,730 Views)
Hi James,
thanks for your reply.

It happens when I modify the printing example (in DotNet\Examples\UI\WindowsForms\Graph\Printing\cs) as follows:

Set GridVisible of yAxis.MajorDivisions to true;

Modify PrintPageOnlySineWave to read:

....
xAxis.Draw(...)
yAxis.Draw(...)

sampleWaveformGraph.DrawGridLines(new ComponentDrawArgs(g, plotBounds));

//g.FillRectangle(Brushes.Black, plotBounds); <-- otherwise you won't see the grid lines
sineWavePlot.Draw(...)
....

Now if you zoom the graph or pan (with Shift / Ctrl + Mouse), the grid lines will be messed up (in this case only if you print the sine wave). The grid lines are perfectly ok if you don't zoom or pan, so I suspect it's not because of wrong drawing bounds.

Regards,
Wilfried
0 Kudos
Message 8 of 13
(5,718 Views)
Hi Wilfried,

What do you mean when you say the gridlines will be messed up? I just tried doing what you said (in version 8.1.1) and my printouts show the gridlines correctly. You said you are you using version 8, does that mean 8.0, 8.1, 8.1.1, etc?

Regards,
0 Kudos
Message 9 of 13
(5,690 Views)
I'm using Measurement Studio 8.1.1

I've made a picture of the preview which shows what I mean with "messed up"


0 Kudos
Message 10 of 13
(5,684 Views)