[vtkusers] Data not getting to chart

Barney Sperlin bsperlinus at yahoo.com
Mon Oct 18 11:35:31 EDT 2010


Hello, beginner question:
     Data I'm reading is not getting charted.
     After reading a space-delimited file of stock market data I'm trying to view a chart of this and the chart comes out as a single straight line at a 45° angle.  There should be 2 lines, neither of which are straight.  I suspect that both are being plotted, one on top of the other.
     When I break the program in the reading loop the values appear correctly so I think that reading the file is not the problem.  Is the data getting into the table?  Debugging doesn't show it.  Is the table transferring properly to the chart?
     I'm using C++ in VS 2005 Express.
     Below is my code, largely taken from an online example:

// Points.cpp :
// READING A CSV FILE, OUTPUT FROM OPENOFFICE, OF WEEKLY DOW  AND VARIOUS MEASURES,
//  SPACE DELIMITED.  COMMAND LINE ARGUMENT PUT IN GUI AT: PROJECTS->POINTS PROPERTIES->
// PROGRAM BASED ON http://vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadPlainText
// AS SUGGESTED BY BILL LORENSEN FROM VTKUSERS LIST ON 10/1/2010
// MODIFICATIONS 10/2010

#include <vtkAxes.h>
#include <vtkSmartPointer.h>
#include <vtkPolyLine.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkParticleReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkChartXY.h>
#include <vtkPlot.h>
#include <vtkTable.h>
#include <vtkFloatArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <sstream>
 
int main(int argc, char* argv[])
{
  //Verify input arguments
  if ( argc != 2 )
    {
    std::cout << "Usage: " << argv[0]
              << " Filename(.xyz)" << std::endl;
    return EXIT_FAILURE;
    }
  //get all data from the file
  std::string filename = argv[1];
  std::ifstream fin(filename.c_str());
 
  //Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  std::string line;
  vtkSmartPointer<vtkPoints> dow_points =
    vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkPoints> consensus_points =
    vtkSmartPointer<vtkPoints>::New();

  vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
  vtkSmartPointer<vtkFloatArray> arrayX = vtkSmartPointer<vtkFloatArray>::New();
  vtkSmartPointer<vtkFloatArray> dow_vals = vtkSmartPointer<vtkFloatArray>::New();
  vtkSmartPointer<vtkFloatArray> consensus_vals = vtkSmartPointer<vtkFloatArray>::New();
  arrayX->SetName("Week #");
  table->AddColumn(arrayX);
  dow_vals->SetName("DOW");
  table->AddColumn(dow_vals);
  consensus_vals->SetName("Consensus %");
  table->AddColumn(consensus_vals);
  table->SetNumberOfRows(10);    // NEED TO BE AWARE OF THIS MAGIC NUMBER

  vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
  view->GetRenderer()->SetBackground(0.0, 0.0, 0.0);
  
  vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();

  // INPUT 8 DOUBLE VALUES FROM A SPACE-DELIMITED CSV FILE
  double loop = 0.0, dow, consensus, c,d,e,f,g,h;
  std::stringstream linestream;
    
  while(std::getline(fin, line))
  {
    loop++;
    linestream << line;
    linestream >> dow >> consensus >> c >> d >> e >> f >> g >> h;
    dow_points->InsertNextPoint(loop, dow, 0);
    consensus_points->InsertNextPoint(loop, consensus, 0);
    if (loop < 10.0)
    {
      table->SetValue(loop,0,loop);
      table->SetValue(loop,1,dow);
      table->SetValue(loop,2,consensus);
    }
  }
 
  fin.close();

  //- - - - - - - - - - - - - - -

  // THE NEXT SECTION OF CODE IS DUPLICATED BELOW FOR ANOTHER SERIES
  // A NATURAL PLACE TO MAKE A FUTURE FUNCTION CALL
  vtkSmartPointer<vtkPolyData> dow_polydata =
    vtkSmartPointer<vtkPolyData>::New();
  dow_polydata->SetPoints(dow_points);

  vtkSmartPointer<vtkVertexGlyphFilter> dow_glyphFilter =
    vtkSmartPointer<vtkVertexGlyphFilter>::New();
  dow_glyphFilter->SetInputConnection(dow_polydata->GetProducerPort());
  dow_glyphFilter->Update();
  
  // Visualize
   //Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> dow_mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  dow_mapper->SetInputConnection(dow_glyphFilter->GetOutputPort());

  vtkSmartPointer<vtkActor> dow_actor =
    vtkSmartPointer<vtkActor>::New();
  dow_actor->SetMapper(dow_mapper);

  //renderer->AddActor(dow_actor);
  view->GetScene()->AddItem(chart);
  chart->DebugOn();
  vtkPlot *line1 = chart->AddPlot(vtkChart::LINE);
  line1->SetInput(table,0,1);
  line1->SetColor(0,255,0,255);    // RGBA
  line1->SetWidth(5.0);
   
  //- - - - - - - - - - - - - - - - - - - -

  vtkSmartPointer<vtkPolyData> consensus_polydata =
    vtkSmartPointer<vtkPolyData>::New();

  consensus_polydata->SetPoints(consensus_points);

  vtkSmartPointer<vtkVertexGlyphFilter> consensus_glyphFilter =
   vtkSmartPointer<vtkVertexGlyphFilter>::New();
  consensus_glyphFilter->SetInputConnection(consensus_polydata->GetProducerPort());
  consensus_glyphFilter->Update();

  vtkSmartPointer<vtkPolyDataMapper> consensus_mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  consensus_mapper->SetInputConnection(consensus_glyphFilter->GetOutputPort());

   vtkSmartPointer<vtkActor> consensus_actor =
    vtkSmartPointer<vtkActor>::New();
  consensus_actor->SetMapper(consensus_mapper);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  //renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->SetRenderWindow(view->GetRenderWindow());

  vtkPlot *line2 = chart->AddPlot(vtkChart::LINE);
  line2->SetInput(table, 0, 2);
  line2->SetColor(255,0,0,255);
  line2->SetWidth(5.0);
  //Add the actor to the scene
  //renderer->AddActor(consensus_actor);

  //- - - - - - - - - - - - - - - - - - - -
  
  // THIN RED, YELLOW AND GREEN LINE SEGMENTS WITH DATA POINTS SCALED WITHIN THEM.  HARD TO SEE AXES
  vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();
  axes->SetOrigin( 0.0, 0.0, 0.0 );
  axes->SetScaleFactor(120.0);    // AXES STAY VISIBLE BUT DATA POINTS SHRINK TOWARD ORIGIN AS VALUE INCREASES

  vtkSmartPointer<vtkPolyDataMapper> axesMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  axesMapper->SetInputConnection(axes->GetOutputPort());

  vtkSmartPointer<vtkActor> axesActor = vtkSmartPointer<vtkActor>::New();
  axesActor->SetMapper(axesMapper);
 
  renderer->AddActor(axesActor);

  renderer->SetBackground(0.0, 0.0, 0.0); // RGB BACKGROUND
 
  //Render and interact
  renderWindow->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}





      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20101018/a8c0e250/attachment.htm>


More information about the vtkusers mailing list