[vtkusers] vtkChartXY to imitate vtkLegendScaleActor behaviour.
madz
madaramh at gmail.com
Thu Feb 27 00:10:13 EST 2014
I need a to display a set of drag-able points in a chart. To achieve this I
used vtkSplineWidget. But the problem occurs when I have to display the axis
legends and grid lines in the chart.
I tried using the vtkSplineWidget in vtkChartXY but the points does not
occur where the co-ordinates in the chart displays them to be.
Code eg -
#include <vtkSmartPointer.h>
#include <vtkChartXY.h>
#include <vtkContextScene.h>
#include <vtkContextView.h>
#include <vtkFloatArray.h>
#include <vtkPlotPoints.h>
#include <vtkTable.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkSplineWidget.h>
void main(){
double valueArr[4] = {12.4,11.2,10,15.5};
int x = 4;
// Set up a 2D scene, add an XY chart to it
vtkSmartPointer<vtkContextView> view =
vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(500, 400);
vtkSmartPointer<vtkChartXY> chart =
vtkSmartPointer<vtkChartXY>::New();
view->GetScene()->AddItem(chart);
chart->SetShowLegend(true);
// Create a table with some points in it...
vtkSmartPointer<vtkTable> table =
vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> arrX =
vtkSmartPointer<vtkFloatArray>::New();
arrX->SetName("X Axis");
table->AddColumn(arrX);
vtkSmartPointer<vtkFloatArray> arrC =
vtkSmartPointer<vtkFloatArray>::New();
arrC->SetName("Y Axis");
table->AddColumn(arrC);
vtkSmartPointer<vtkPoints> po = vtkSmartPointer<vtkPoints>::New();
// Test charting with a few more points...
table->SetNumberOfRows(x);
for (int i = 0; i < x; i++)
{
table->SetValue(i, 0, i+1 );
table->SetValue(i, 1, valueArr[i]);
po->InsertNextPoint(i,valueArr[i],0);
}
// Add multiple scatter plots, setting the colors etc
vtkPlot *points = chart->AddPlot(vtkChart::POINTS);
vtkPlot *line = chart->AddPlot(vtkChart::LINE);
#if VTK_MAJOR_VERSION <= 5
points->SetInput(table, 0, 1);
line->SetInput(table, 0, 1);
#else
points->SetInputData(table, 0, 1);
line->SetInputData(table, 0, 1);
#endif
points->SetColor(0, 0, 255, 255);
points->SetWidth(1.0);
line->SetColor(0, 255, 0, 255);
line->SetWidth(1.0);
vtkPlotPoints::SafeDownCast(points)->SetMarkerStyle(vtkPlotPoints::CIRCLE);
points = chart->AddPlot(vtkChart::POINTS);
line = chart->AddPlot(vtkChart::LINE);
vtkSmartPointer<vtkSplineWidget> splineWidget =
vtkSmartPointer<vtkSplineWidget>::New();
splineWidget->SetInteractor(view->GetInteractor());
splineWidget->InitializeHandles(po);
splineWidget->SetHandleSize( 0.01 );
splineWidget->GetHandleProperty()->SetColor(0,1,0);
double bnds[6];
po->GetBounds( bnds );
splineWidget->PlaceWidget( bnds[0], bnds[1], bnds[2], bnds[3], bnds[4],
bnds[5] );
splineWidget->SetNumberOfHandles(po->GetNumberOfPoints()); //not needed
splineWidget->SetCurrentRenderer( view->GetRenderer() );
splineWidget->SetDefaultRenderer( view->GetRenderer());
vtkProperty* prop = vtkProperty::New();
prop->SetColor(0,0,0 );
splineWidget->SetLineProperty( prop );
splineWidget->On();
//Finally render the scene
view->GetInteractor()->Initialize();
view->GetRenderWindow()->SetWindowName("XY Simple Graph");
view->GetInteractor()->Start();
}
Then I tried the vtkLegendScaleActor, but this does not display any grid
lines.
Code eg -
#include <vtkSmartPointer.h>
#include <vtkLegendScaleActor.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkSplineWidget.h>
void main(){
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
// An interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
double valueArr[4] = {12.4,11.2,10,15.5};
int x = 4;
vtkSmartPointer<vtkPoints> po = vtkSmartPointer<vtkPoints>::New();
for (int i = 0; i < x; i++)
{
po->InsertNextPoint(i,valueArr[i],0);
}
vtkSmartPointer<vtkSplineWidget> splineWidget =
vtkSmartPointer<vtkSplineWidget>::New();
splineWidget->SetInteractor(renderWindowInteractor);
splineWidget->InitializeHandles(po);
splineWidget->SetHandleSize( 0.01 );
splineWidget->GetHandleProperty()->SetColor(0,1,0);
double bnds[6];
po->GetBounds( bnds );
splineWidget->PlaceWidget( bnds[0], bnds[1], bnds[2], bnds[3], bnds[4],
bnds[5] );
splineWidget->SetNumberOfHandles(po->GetNumberOfPoints()); //not needed
splineWidget->SetCurrentRenderer( renderer );
splineWidget->SetDefaultRenderer( renderer);
vtkProperty* prop = vtkProperty::New();
prop->SetColor(0,0,0 );
vtkSmartPointer<vtkLegendScaleActor> legendScaleActor =
vtkSmartPointer<vtkLegendScaleActor>::New();
legendScaleActor->SetLabelModeToXYCoordinates();
legendScaleActor->GetRightAxis()->GetProperty()->SetColor(0,0,0);
legendScaleActor->GetRightAxis()->AdjustLabelsOn();
legendScaleActor->GetRightAxis()->GetLabelTextProperty()->SetColor(0,0,0);
legendScaleActor->GetLeftAxis()->GetProperty()->SetColor(0,0,0);
legendScaleActor->GetLeftAxis()->GetLabelTextProperty()->SetColor(0,0,0);
legendScaleActor->GetLeftAxis()->AdjustLabelsOn();
legendScaleActor->GetTopAxis()->GetProperty()->SetColor(0,0,0);
legendScaleActor->GetTopAxis()->GetLabelTextProperty()->SetColor(0,0,0);
legendScaleActor->GetTopAxis()->AdjustLabelsOn();
legendScaleActor->GetBottomAxis()->GetProperty()->SetColor(0,0,0);
legendScaleActor->GetBottomAxis()->GetLabelTextProperty()->SetColor(0,0,0);
legendScaleActor->GetBottomAxis()->AdjustLabelsOn();
legendScaleActor->LegendVisibilityOff();
renderer->AddActor(legendScaleActor);
renderer->ResetCamera();
renderWindow->Render();
renderer->SetBackground(1,1,1);
renderWindowInteractor->Start();
}
Is there a way to set the grid line and axis legend positions to imitate
the vtkLegendScaleActor or to set the vtkSplineWidget point co-ordinates
according to the vtkChartXY?
Any help would be appreciated, Thank you.
--
View this message in context: http://vtk.1045678.n5.nabble.com/vtkChartXY-to-imitate-vtkLegendScaleActor-behaviour-tp5726125.html
Sent from the VTK - Users mailing list archive at Nabble.com.
More information about the vtkusers
mailing list