[vtkusers] Simple vtkXYPlot
Michael Rivera
riveram_68 at yahoo.com
Tue Jul 1 14:13:10 EDT 2003
Hi Walter,
I've been playing around with this approach too. It seems to be a bit
stubborn but here's what I've found up to now.
The basic idea is that somehow you set up a DataObject. You can do
this from a 'FieldData' Object; the example I've given has two columns.
Now it seems that vtkXYPlotActor can only plot DataObjects or DataSets.
Hence the conversion from FieldData to DataObject.
Unfortunately, it does not seem that vtkXYPlotActor can only plot one
DataObject at a time on the same pair of axes; i.e. there does not seem
to be a way to address the DataObject such that vtkXYPlotActor would
plot single X and multiple Y. I'm still looking into this.
You'll have to manipulate the DataObject as show below. I have an
example code in C++ that should work.
By the way, I am not crazy about the rendering of this 2DActor. If
there is way to make it more efficient let me know.
Sincerely,
Mike Rivera
******************************************************
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkXYPlotActor.h"
#include "vtkProperty2D.h"
#include "vtkDataObject.h"
#include "vtkFieldData.h"
#include "vtkDataSet.h"
#include "vtkFloatArray.h"
int main()
{
int i, DataSize = 100;
float xpts[100][1], ypts[100][1];
// Create the rendering stuff
vtkRenderWindow *renWin = vtkRenderWindow::New();
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
vtkRenderer *ren = vtkRenderer::New();
renWin->AddRenderer(ren);
//
vtkFloatArray *xCoords = vtkFloatArray::New();
vtkFloatArray *yCoords = vtkFloatArray::New();
xCoords->SetNumberOfTuples(DataSize);
yCoords->SetNumberOfTuples(DataSize);
//
for(i=0; i<DataSize; i++)
{
xpts[i][0] = float(i);
ypts[i][0] = sqrt(float(i));
}
//
for(i=0; i<DataSize; i++)
{
xCoords->SetTuple(i,xpts[i]);
yCoords->SetTuple(i,ypts[i]);
}
// First Plot
vtkFieldData *curve = vtkFieldData::New();
curve -> AddArray(xCoords);
curve -> AddArray(yCoords);
//
vtkDataObject *plot = vtkDataObject::New();
plot->SetFieldData(curve);
//
vtkXYPlotActor *xyplot = vtkXYPlotActor::New();
xyplot -> AddDataObjectInput(plot);
xyplot -> SetDataObjectPlotModeToRows();
xyplot -> SetDataObjectXComponent(0,0);
xyplot -> SetDataObjectYComponent(0,1);
xyplot -> GetPositionCoordinate()->SetValue(0,0.0,0);
xyplot -> GetPosition2Coordinate()->SetValue(1,1,0);
xyplot -> PlotPointsOn();
xyplot -> PlotLinesOff();
xyplot -> GetProperty() -> SetPointSize(5);
xyplot -> SetXRange(0,100);
xyplot -> SetYRange(0,20);
xyplot -> SetPlotColor(0,1,1,0);
ren->SetBackground(0.1,0.2,0.4);
ren->AddActor2D(xyplot);
//
renWin->Render();
// interact with data
iren->Initialize();
iren->Start();
//
xCoords->Delete();
yCoords->Delete();
xyplot->Delete();
curve->Delete();
iren->Delete();
ren->Delete();
renWin->Delete();
//
return 0;
}
__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com
More information about the vtkusers
mailing list