1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestLinePlot.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 #include "vtkRenderWindow.h"
17 #include "vtkSmartPointer.h"
18 #include "vtkAxis.h"
19 #include "vtkChartXY.h"
20 #include "vtkPlot.h"
21 #include "vtkTable.h"
22 #include "vtkFloatArray.h"
23 #include "vtkContextView.h"
24 #include "vtkContextScene.h"
25 #include "vtkRenderWindowInteractor.h"
26 
27 //----------------------------------------------------------------------------
TestScientificPlot(int,char * [])28 int TestScientificPlot(int, char * [])
29 {
30   // Set up a 2D scene, add an XY chart to it
31   vtkSmartPointer<vtkContextView> view =
32       vtkSmartPointer<vtkContextView>::New();
33   view->GetRenderWindow()->SetSize(400, 400);
34   vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
35   view->GetScene()->AddItem(chart);
36 
37   // Create a table with some points in it...
38   vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
39   vtkSmartPointer<vtkFloatArray> arrX = vtkSmartPointer<vtkFloatArray>::New();
40   arrX->SetName("X Axis");
41   table->AddColumn(arrX);
42   vtkSmartPointer<vtkFloatArray> arrC = vtkSmartPointer<vtkFloatArray>::New();
43   arrC->SetName("cos");
44   table->AddColumn(arrC);
45   vtkSmartPointer<vtkFloatArray> arrS = vtkSmartPointer<vtkFloatArray>::New();
46   arrS->SetName("sin");
47   table->AddColumn(arrS);
48   vtkSmartPointer<vtkFloatArray> arrS2 = vtkSmartPointer<vtkFloatArray>::New();
49   arrS2->SetName("x^3");
50   table->AddColumn(arrS2);
51   // Test charting with a few more points...
52   int numPoints = 69;
53   float inc = 3.0 / (numPoints-1);
54   table->SetNumberOfRows(numPoints);
55   for (int i = 0; i < numPoints; ++i)
56     {
57     double v = -1.0 + i * inc;
58     table->SetValue(i, 0, v);
59     table->SetValue(i, 1, cos(v));
60     table->SetValue(i, 2, sin(v) + 0.0);
61     table->SetValue(i, 3, v*v*v);
62     }
63 
64   // Add multiple line plots, setting the colors etc
65   vtkPlot *line = chart->AddPlot(vtkChart::LINE);
66   line->SetInputData(table, 0, 1);
67   line->SetColor(0, 255, 0, 255);
68   line = chart->AddPlot(vtkChart::LINE);
69   line->SetInputData(table, 0, 2);
70   line->SetColor(255, 0, 0, 255);
71   line = chart->AddPlot(vtkChart::POINTS);
72   line->SetInputData(table, 0, 3);
73   line->SetColor(0, 0, 255, 255);
74 
75   // Set up a scientific plot...
76   chart->SetDrawAxesAtOrigin(true);
77   chart->SetShowLegend(true);
78   chart->GetAxis(vtkAxis::LEFT)->SetRange(1.0, -1.5);
79   chart->GetAxis(vtkAxis::LEFT)->SetNotation(2);
80   chart->GetAxis(vtkAxis::LEFT)->SetPrecision(1);
81   chart->GetAxis(vtkAxis::LEFT)->SetBehavior(vtkAxis::FIXED);
82   chart->GetAxis(vtkAxis::LEFT)->SetTitle("");
83   chart->GetAxis(vtkAxis::BOTTOM)->SetRange(-1.0, 1.5);
84   chart->GetAxis(vtkAxis::BOTTOM)->SetNotation(2);
85   chart->GetAxis(vtkAxis::BOTTOM)->SetPrecision(1);
86   chart->GetAxis(vtkAxis::BOTTOM)->SetBehavior(vtkAxis::FIXED);
87   chart->GetAxis(vtkAxis::BOTTOM)->SetTitle("");
88 
89   //Finally render the scene and compare the image to a reference image
90   view->GetRenderWindow()->SetMultiSamples(0);
91   view->GetInteractor()->Initialize();
92   view->GetInteractor()->Start();
93   return EXIT_SUCCESS;
94 }
95