[vtkusers] vtkChartXY - need help

Eric E. Monson emonson at cs.duke.edu
Wed Jun 22 09:52:41 EDT 2011


Hey Amitesh,

You can just build the second table you mentioned and then add a plot to the chart which uses that second table as its input. The chart should take care of it just fine.

I'll append a modified version of that example you quoted, which I think does what you're wanting. (The orange dotted line is the new data series and it has many fewer points and a different X range than the first two lines.)

Hope this helps,
-Eric

· · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
Eric E Monson
Duke Visualization Technology Group




#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkChartXY.h>
#include <vtkPen.h>
#include <vtkPlot.h>
#include <vtkTable.h>
#include <vtkFloatArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
 
int main(int, char *[])
{
  // 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("Cosine");
  table->AddColumn(arrC);
 
  vtkSmartPointer<vtkFloatArray> arrS = 
    vtkSmartPointer<vtkFloatArray>::New();
  arrS->SetName("Sine");
  table->AddColumn(arrS);
 
  // Fill in the table with some example values
  int numPoints = 69;
  float inc = 7.5 / (numPoints-1);
  table->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
  {
    table->SetValue(i, 0, i * inc);
    table->SetValue(i, 1, cos(i * inc));
    table->SetValue(i, 2, sin(i * inc));
  }
 
  // Create a second table with some points in it
  vtkSmartPointer<vtkTable> table2 = 
    vtkSmartPointer<vtkTable>::New();
 
  vtkSmartPointer<vtkFloatArray> arrX2 = 
    vtkSmartPointer<vtkFloatArray>::New();
  arrX2->SetName("X Axis 2");
  table2->AddColumn(arrX2);
 
  vtkSmartPointer<vtkFloatArray> arrC2 = 
    vtkSmartPointer<vtkFloatArray>::New();
  arrC2->SetName("Cosine 2");
  table2->AddColumn(arrC2);
 
  // Fill in the second table with some example values
  numPoints = 8;
  inc = 10.5 / (numPoints-1);
  table2->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
  {
    table2->SetValue(i, 0, i * inc + 5);
    table2->SetValue(i, 1, cos(i * inc));
  }
 
  // Set up the view
  vtkSmartPointer<vtkContextView> view = 
    vtkSmartPointer<vtkContextView>::New();
  view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
 
  // Add multiple line plots, setting the colors etc
  vtkSmartPointer<vtkChartXY> chart = 
    vtkSmartPointer<vtkChartXY>::New();
  view->GetScene()->AddItem(chart);
  vtkPlot *line = chart->AddPlot(vtkChart::LINE);
  line->SetInput(table, 0, 1);
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(1.0);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetInput(table, 0, 2);
  line->SetColor(255, 0, 0, 255);
  line->SetWidth(5.0);
 
  // Add a plot using the second table
  line = chart->AddPlot(vtkChart::LINE);
  line->SetInput(table2, 0, 1);
  line->SetColor(255, 128, 0, 255);
  line->SetWidth(3.0);
  line->GetPen()->SetLineType(vtkPen::DASH_LINE);
 
  // Set up an interactor and start
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(view->GetRenderWindow());
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

On Jun 21, 2011, at 3:17 PM, Amitesh Kumar wrote:

> I have a question about vtkChartXY and wonder if anybody would be willing to answer it?
> Currently as in the example shown in http://www.vtk.org/Wiki/VTK/Examples/Cxx/Plotting/LinePlot the x-axis is defined as 
> 
> table->SetValue(i, 0, i * inc);
> 
> while the datasets are defined as
> 
> table->SetValue(i, 1, cos(i * inc));
> table->SetValue(i, 2, sin(i * inc));
> 
> Which means that both plot 1 and plot 2 need to have exact same correspondence with x-axis with same number of rows and x-axis values. Lets say I have two sets of data, one in which x-axis starts from 1-10 and and  another in which the x-axis starts from 5-20 and the number of points (rows) on first set is different compared with that of the second set. I clearly would need to have two tables in the current scheme of things. How can this be plotted using vtkChartXY or any other methods in VTK. Is there any chart which would support it? Are there any examples of the same? I am using version 5.9  of VTK. I really need some help on this!
> 
> Thank You
> 
> Amitesh
> 
> 
> 
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> 
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110622/e833494f/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TwoTablesPlotted.png
Type: image/png
Size: 3187 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20110622/e833494f/attachment.png>


More information about the vtkusers mailing list