Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem when drawing graph axis

I try to draw a ScatterGraph on a Graphics object. Here's my code:

private void DrawGraphInGraphic(Graphics g)
{
g.Clear(Color.White);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

// Draw the X axis.
NationalInstruments.UI.ComponentDrawArgs cda = new NationalInstruments.UI.ComponentDrawArgs(g, this.graph.XAxes[0].GetBounds());
this.graph.XAxes[0].Draw(cda);

//Draw the Y1 axis.
Rectangle axisRect = this.yAxis1.GetBounds();
cda = new NationalInstruments.UI.ComponentDrawArgs(g, axisRect);
this.yAxis1.Draw(cda, NationalInstruments.UI.YAxisPosition.Left);

// Draw the Y2 axis.
axisRect = this.yAxis2.GetBounds();
cda = new NationalInstruments.UI.ComponentDrawArgs(g, axisRect);
this.yAxis2.Draw(cda, NationalInstruments.UI.YAxisPosition.Right);

// Draw the plot area.
Rectangle r = this.graph.PlotAreaBounds;
g.FillRectangle(Brushes.Black, r);

// Draw the gridlines.
cda = new NationalInstruments.UI.ComponentDrawArgs(g, r);
this.graph.DrawGridLines(cda);

// Draw the plots and everything...
this.graph.DrawPlotAreaComponents(cda);
}

My yAxis1 object is reversed on my displayed graph (minus values at top, positive values going downward). When I use the Draw method, the result is not reversed (minus is at bottom, positive values are going upward). BAD !!!

My yAxis2 object is located (YAxisPosition) at Right. The GetBounds() method returns (Left=0, Top=0, Width=0, Height=0). BAD!!!

Please solve these bugs soon. I really need it to work properly for mid-march.

Thanks!
0 Kudos
Message 1 of 7
(4,877 Views)
ScatterGraph has a Draw method like all the sub-objects you were drawing. To draw the entire ScatterGraph call ScatterGraph.Draw with the bounds you want the ScatterGraph to draw in like:


private void DrawGraph(Graphics g, Rectangle bounds)
{
scatterGraph1.Draw(new ComponentDrawArgs(g, bounds));
}
0 Kudos
Message 2 of 7
(4,872 Views)
Just to clarify, the yAxis1 drawing behavior that you are seeing is in fact a bug. I have filed a bug report to have this fixed. In order to accomplish drawing the entire graph, you can call Draw on the graph itself like breeve pointed out in the earlier post.

The reason yAxis2 bounds has Width and Height of 0 is probably because the axis is set to Visible = false. The Left and Top of the bounds is (0, 0) because you are calling yAxis2.GetBounds() without passing any parameters. When GetBounds() is called without any parameter, the method returns the bounds of the y-axis as though it is located on the left of the plot area of the graph. The YAxis class contains additional overloads of GetBounds that accept YAxisPosition as an argument (similar to the Draw method that you are calling). If you call this overload with YAxisPosition.Right, you will get the correct bounds for the axis. The reason for the additional overloads of GetBounds is that if a user has the Position property set to YAxisPosition.LeftRight, then we could not return a single rectangle that would be correct for both sides of the axis. Instead, we require the user to specify the position in the GetBounds method.
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 3 of 7
(4,859 Views)
Thank you very much. I'll try to solve the yAxis2 problem right away. Your answer is simply logical... where was I? 😞

Please e-mail me when the bug of reversed axis is solved. It's very important for me to know as soon as it's ready.

Thanks again!
0 Kudos
Message 4 of 7
(4,853 Views)
Please could you tell me how you are using the Draw method on the YAxis class. It seems like calling Draw on the graph control would have accomplished what you were trying to do. If you could let me know your use case, I could try to come up with a work around.

Thanks,
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 5 of 7
(4,840 Views)
Simply speaking, I wanted to be able to set the graph's plot area background color according to the kind of device it was intended for. On screen, the backcolor of the graph is black (the plot area). But when printing, I want to be able to set it to white. The axis' background color must always be white, except for the on-screen display, which uses the BackColor property (SystemColors.Control). In the code I sent, the line says "g.FillRectangle(Brushes.Black, r);" and it is hard-coded. But ultimately, I will change it to support any color.

For now, I changed my code to use the ScatterGraph.Draw method because I don't allow to print the graph. I only support "Export" and "Copy to clipboard". But one day, the product manager will ask me to print... and I don't want to print the graph with a black plot area. If I remember, if I change the plot area of the graph at run-time, it flickers... not good for me!

Thank you again! 😉
0 Kudos
Message 6 of 7
(4,836 Views)
If you have button that calls the printing method in your code, then you can set the graph control up exactly the way you would like it to print and not see flicker by calling BeginUpdate before you set the properties (like PlotAreaColor) and EndUpdate after resetting the properties to their original values. For example:


private void printButton_Click(object sender, EventArgs e)
{
waveformGraph.BeginUpdate();
Color graphBackColor = waveformGraph.BackColor;
Color plotAreaColor = waveformGraph.PlotAreaColor;
// PlotAreaBorder is a new property in Measurement Studio 7.1
Border plotAreaBorder = waveformGraph.PlotAreaBorder;
waveformGraph.BackColor = Color.White;
waveformGraph.PlotAreaColor = Color.White;

...
// Code to print the graph
...

waveformGraph.BackColor = graphBackColor;
waveformGraph.PlotAreaColor = plotAreaColor;
waveformGraph.PlotAreaBorder = plotAreaBorder;
waveformGraph.EndUpdate();
}


You also mentioned that you are copying the graph to the clipboard. In Measurement Studio 7.l, we added method on all the user interface controls to copy the control to the clipboard. The method is called ToClipboard. I am not sure if you are already using this or not, but thought I will point it out anyways.
Abhishek Ghuwalewala | Measurement Studio | National Instruments
0 Kudos
Message 7 of 7
(4,830 Views)