Have a GraphWidget fill the entire View in AndroidPlot - androidplot

Is there a way to have the graph of an AndroidPlot XYPlot fill the entire view without padding?
I am specifically talking about the space on the left side that I marked red in the image:
My goal is to overlay the graph over an image and have it flush with the parent views borders. I haved removed all the labels in my subclass of XYPlot, by setting their Paint to null, the space they would take up remains though.
Here is how I set up the plot:
public void setup() {
this.setBackgroundPaint(null);
setPlotMarginLeft(0);
setRangeBoundaries(-2, 2, BoundaryMode.FIXED);
setDomainBoundaries(0, HISTORY_SIZE, BoundaryMode.FIXED);
XYGraphWidget g = getGraphWidget();
g.setDomainLabelPaint(null);
g.setRangeLabelPaint(null);
g.setDomainOriginLabelPaint(null);
g.setRangeOriginLabelPaint(null);
g.setGridBackgroundPaint(null);
g.setGridPaddingLeft(0);
g.setGridPaddingRight(0);
g.setMarginLeft(0);
g.setBackgroundPaint(null);
g.position(-0.5f, XLayoutStyle.RELATIVE_TO_RIGHT,
-0.5f, YLayoutStyle.RELATIVE_TO_BOTTOM,
AnchorPosition.CENTER);
g.setSize(new SizeMetrics(
0, SizeLayoutType.FILL,
0, SizeLayoutType.FILL));
LayoutManager l = getLayoutManager();
l.remove(this.getDomainLabelWidget());
l.remove(this.getRangeLabelWidget());
l.remove(getLegendWidget());
mSeries = new SimpleXYSeries("Azimuth");
mSeries.useImplicitXVals();
addSeries(mSeries, new LineAndPointFormatter(Color.BLACK, null, null, null));
}

Got it, here is the code I am using to set up a Plot without any margins, padding or labels:
XYGraphWidget g = getGraphWidget();
g.setDomainLabelPaint(null);
g.setRangeLabelPaint(null);
g.setDomainOriginLabelPaint(null);
g.setRangeOriginLabelPaint(null);
g.setGridBackgroundPaint(null);
g.setGridPaddingLeft(0);
g.setGridPaddingRight(0);
g.setMarginLeft(0);
g.setBackgroundPaint(null);
g.position(-0.5f, XLayoutStyle.RELATIVE_TO_RIGHT,
-0.5f, YLayoutStyle.RELATIVE_TO_BOTTOM,
AnchorPosition.CENTER);
g.setSize(new SizeMetrics(
0, SizeLayoutType.FILL,
0, SizeLayoutType.FILL));
LayoutManager l = getLayoutManager();
l.remove(this.getDomainLabelWidget());
l.remove(this.getRangeLabelWidget());
l.remove(getLegendWidget());
g.setRangeLabelWidth(0);
g.setDomainLabelWidth(0);
g.setPadding(0, 0, 0, 0);
g.setMargins(0, 0, 0, 0);
g.setGridPadding(0, 0, 0, 0);
setPlotMargins(0, 0, 0, 0);
setPlotPadding(0, 0, 0, 0);

Related

How do I draw 2D fonts in processing P3D mode?

I'm running a sketch with an array of points in 3D space (P3D). I'd like to add an interface to it by drawing text as if it were "onscreen"/2D, only using "X, Y" parameters.
When I tried just adding "text("!##$%", width/2, height/2);" it rendered in 3D space.
Is it possible? I tried "textMode(SCREEN) but doesnt exist in processing 2 anymore.
Here is what I found, I guess on the Processing Forum
You can use:
a PMatrix3D for your 3D content
and code your 2D stuff the plain old way
I wish it helps
PMatrix3D baseMat;
float alpha =0;
void setup() {
size(400, 400, P3D);
// Remember the start model view matrix values
baseMat = getMatrix(baseMat);
}
void draw() {
background(40);
pushMatrix();
camera(0, 0, 400, 0, 0, 0, 0, 1, 0);
directionalLight(255, 255, 255, -100, 150, -100);
ambientLight(40, 40, 40);
// 3D drawing stuff here
rotateY(alpha);
box(100);
alpha += 0.05;
popMatrix();
// Restore the base matrix and lighting ready for 2D
this.setMatrix(baseMat);
ambientLight(255, 255, 255);
// draw 2D stuff here
rect(10, 10, 50, 10);
textSize(25);
text("voila", mouseX, mouseY);
}
A workaround that comes to mind is to create a 2D PGraphic that has the same width/height as your sketch, give it a transparent background, draw your text where you want on it, and then draw the PGraphic onto your real sketch like you would if you were copying over image source data.

Androidplot: Remove space to the left of chart

I am using androidplot-core-0.6.1.jar and I'm modifying the SimpleXYPlotActivity example from the quickstart tutorial. I have removed some items and margin/padding, but what I want to do and can not find a way how, is to remove the space to the left of the chart. I have marked the space I want to remove with red in the image below. Can this be done?
How it looks
Red shows what I want to remove
Code from the activity:
public class MyXYPlotActivity extends Activity {
private XYPlot plot;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.simple_xy_plot_example);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Foo"); // Set the display title of the series
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getApplicationContext(),
R.xml.line_point_formatter_with_plf1);
plot.addSeries(series1, series1Format);
plot.setDomainValueFormat(new DecimalFormat("#"));
plot.setRangeValueFormat(new DecimalFormat("#"));
plot.setTitle("Title");
plot.setRangeBoundaries(0,10, BoundaryMode.FIXED);
int length = 30;
plot.setDomainBoundaries(1,length, BoundaryMode.FIXED);
plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
plot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, 1);
plot.setMarkupEnabled(true);
plot.setTicksPerRangeLabel(1);
plot.setTicksPerDomainLabel(1);
plot.getGraphWidget().setDomainLabelOrientation(90);
plot.getLayoutManager().remove(plot.getLegendWidget());
plot.getLayoutManager().remove(plot.getRangeLabelWidget());
plot.getLayoutManager().remove(plot.getDomainLabelWidget());
plot.setPlotMargins(0, 0, 0, 0);
plot.setPlotPadding(0, 0, 0, 0);
plot.setBorderStyle(Plot.BorderStyle.SQUARE, null, null);
plot.getRangeLabelWidget().setHeight(0);
plot.getRangeLabelWidget().setWidth(0);
plot.getRangeLabelWidget().setPadding(0, 0, 0, 0);
plot.getRangeLabelWidget().setMargins(0, 0, 0, 0);
plot.getDomainLabelWidget().setHeight(0);
plot.getDomainLabelWidget().setWidth(0);
plot.getDomainLabelWidget().setPadding(0, 0, 0, 0);
plot.getDomainLabelWidget().setMargins(0, 0, 0, 0);
}
}
EDIT: I found the solution:
XYGraphWidget g = plot.getGraphWidget();
g.setRangeLabelWidth(25);
g.setDomainLabelWidth(25);
This above works.
Looks like you want to reduce/remove the graphWidget's margins and/or padding. Try adding these to your code to see if it is having the desired effect...you'll obviously need to use nonzero values suited for your application:
plot.getGraphWidget().setMarginLeft(0);
plot.getGraphWidget().setPaddingLeft(0);
Also take a look at these similar questions:
Have a GraphWidget fill the entire View in AndroidPlot
How do I remove all space around a chart in AndroidPlot?

How to set OpenGl Coordinates?

With following code I am getting my triangle in top right corner of the graph, which tells me that the 0,0 is in the center of the window. What should I do to bring it in the corner of the window, i.e. bottom left?
#include <GL/glut.h>
void displayCube()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0);
glVertex3f(0.5, 0, 0);
glVertex3f(0.25, 0.25, 0);
glEnd();
glFlush();
}
int main(int argc, char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cube");
glutDisplayFunc(displayCube);
glutMainLoop();
return 0;
}
OpenGL uses a set of matrix transformation to move from original model space to screen/window space.
In you example, there is default identity projection so you are 'moving' in box -1 to 1 in each direction.
point (0.0, 0.0, 0.0) is in the centre. (-1, 0, 0) is on the left side, (1, 0, 0) is on the right, (0, 1, 0) is top.
try to figure out the rest :)
http://www.songho.ca/opengl/gl_transform.html

Why texture not applying?

I have a cube with texture, when I changed my data arrays to VBO(glGen,etc.) my cube renders with grey color.But if I use something like glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, myBuf); all is ok. What's problem? Please help me.
- (void)render:(CADisplayLink*)displayLink {
glClearColor(0.0f, 1.0f,0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
CC3GLMatrix *projection = [CC3GLMatrix matrix];
float h = 4.0f * self.frame.size.height / self.frame.size.width;
[projection populateFromFrustumLeft:-2 andRight:2 andBottom:-h/2 andTop:h/2 andNear:2 andFar:4];
glUniformMatrix4fv(_projectionUniform, 1, 0, projection.glMatrix);
CC3GLMatrix *modelView = [CC3GLMatrix matrix];
[modelView populateFromTranslation:CC3VectorMake(sin(CACurrentMediaTime()), 0, -2)];
_currentRotation += displayLink.duration * 90;
[modelView rotateBy:CC3VectorMake(_currentRotation, _currentRotation, -1)];
glUniformMatrix4fv(_modelViewUniform, 1, 0, modelView.glMatrix);
// 1
glViewport(0, 0, self.frame.size.width, self.frame.size.height);
// 2
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glUniform1i(uniformTexture, 0);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(_positionSlot);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeIndexes);
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_FLOAT, 0, 0, texCoord);
glEnableVertexAttribArray(ATTRIB_TEXCOORD);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, (void*)0);
//glBindTexture(GL_TEXTURE_2D, 0);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
}
You need to use client states to apply details on your vertex buffer, see glEnableClientState and glClientActiveTexture manual entries.
Try to change your code in this way.
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableClientState(GL_VERTEX_ARRAY); // NEW! we need to enable client state.
glActiveClientTexture(GL_TEXTURE0); // CHANGED! We need to use textures in the client state.
See also How to call glDrawElements with static TexCoords and Dynamic Vertices
What version of OpenGL are you using? I hope OpenGL ES 2.0

Problems when i using GetTextExtentExPoint to calculate the extent of a string

I created a font object and selected it into the device content, then calculate the extent of the string using WIN32 API GetTextExtentExPoint, but the extent i got is the extent while using system default font.
For example, when i using system default font, the extent of the string is 36 pixel width and 16 pixel height, and 72 pixel width and 24 pixel height while using the font i created. But, i always got 36 pixel no mater using system default font or the font i created.
What's the problem with my codes?
Codes:
HDC hDC = GetDC();
ATLASSERT(hDC);
HFONT _hFontTitle = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
ATLASSERT(hSysFont);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontTitle = CreateFont(16, 12, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 16;
lf.lfWidth = 12;
lf.lfWeight = FW_BOLD;
_hFontTitle = CreateFontIndirect(&lf);
ATLASSERT(_hFontTitle);
}
HFONT _hFontContent = 0;
HFONT hSysFont = (HFONT)GetCurrentObject(hDC, OBJ_FONT);
LOGFONT lf;
if(0 == GetObject(hSysFont, sizeof(LOGFONT), &lf))
_hFontContent = CreateFont(12, 9, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FIXED_PITCH|FF_DONTCARE, _T("Fixedsys"));
else{
lf.lfHeight = 12;
lf.lfWidth = 9;
lf.lfWeight = FW_NORMAL;
_hFontContent = CreateFontIndirect(&lf);
ATLASSERT(_hFontContent);
}
SIZE sizeTitle = TextMetricsHelper::GetTextLayout(hDC, _szTitle.c_str(), _szTitle.size(), _hFontTitle);
SIZE sizeContent = TextMetricsHelper::GetTextLayout(hDC, _szContent.c_str(), _szContent.size(), _hFontContent);
While GetTextLayout is:
SIZE GetTextLayout(HDC hDC, LPCTSTR lpszText, unsigned int cbText, HFONT hFont)
{
//RECT rcText = {0, 0, 8, 10};
HFONT hOldFont = (HFONT)SelectObject(hDC, (HGDIOBJ)hFont);
SIZE textSize;
GetTextExtentPoint32(hDC, lpszText, cbText, &textSize);
//GetTextExtentExPoint(hDC, lpszText, cbText, 0, 0, 0, &sizeOfTitle);
//DrawText(hDC, lpszText, cbText, &rcText, DT_CALCRECT);
SelectObject(hDC, hOldFont);
return textSize;
}
Don't know much about creating fonts and so on, but I notice that both your CreateFont lines assign their result to the same variable _hFontTitle. Is this the intention ?

Resources