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 "vtkRenderer.h"
17 #include "vtkRenderWindow.h"
18 #include "vtkNew.h"
19 #include "vtkChartXY.h"
20 #include "vtkPlotPoints.h"
21 #include "vtkTable.h"
22 #include "vtkFloatArray.h"
23 #include "vtkStringArray.h"
24 #include "vtkContextView.h"
25 #include "vtkContextScene.h"
26 #include "vtkRenderWindowInteractor.h"
27
28 //----------------------------------------------------------------------------
TestScatterPlot(int,char * [])29 int TestScatterPlot(int , char * [])
30 {
31 // Set up a 2D scene, add an XY chart to it
32 vtkNew<vtkContextView> view;
33 view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
34 view->GetRenderWindow()->SetSize(400, 300);
35 vtkNew<vtkChartXY> chart;
36 view->GetScene()->AddItem(chart.GetPointer());
37 chart->SetShowLegend(true);
38
39 // Create a table with some points in it...
40 vtkNew<vtkTable> table;
41 vtkNew<vtkFloatArray> arrX;
42 arrX->SetName("X Axis");
43 table->AddColumn(arrX.GetPointer());
44 vtkNew<vtkFloatArray> arrC;
45 arrC->SetName("Cosine");
46 table->AddColumn(arrC.GetPointer());
47 vtkNew<vtkFloatArray> arrS;
48 arrS->SetName("Sine");
49 table->AddColumn(arrS.GetPointer());
50 vtkNew<vtkFloatArray> arrT;
51 arrT->SetName("Tan");
52 table->AddColumn(arrT.GetPointer());
53 vtkNew<vtkStringArray> labels;
54 labels->SetName("Labels");
55 table->AddColumn(labels.GetPointer());
56
57 // Test charting with a few more points...
58 int numPoints = 40;
59 float inc = 7.5 / (numPoints-1);
60 table->SetNumberOfRows(numPoints);
61 for (int i = 0; i < numPoints; ++i)
62 {
63 table->SetValue(i, 0, i * inc);
64 table->SetValue(i, 1, cos(i * inc) + 0.0);
65 table->SetValue(i, 2, sin(i * inc) + 0.0);
66 table->SetValue(i, 3, tan(i * inc) + 0.5);
67 if (i % 2)
68 {
69 table->SetValue(i, 4, vtkStdString("Odd"));
70 }
71 else
72 {
73 table->SetValue(i, 4, vtkStdString("Even"));
74 }
75 }
76
77 // Add multiple line plots, setting the colors etc
78 vtkPlot *points = chart->AddPlot(vtkChart::POINTS);
79 points->SetInputData(table.GetPointer(), 0, 1);
80 points->SetColor(0, 0, 0, 255);
81 points->SetWidth(1.0);
82 points->SetIndexedLabels(labels.GetPointer());
83 points->SetTooltipLabelFormat("%i from %l (%x, %y)");
84 vtkPlotPoints::SafeDownCast(points)->SetMarkerStyle(vtkPlotPoints::CROSS);
85 points = chart->AddPlot(vtkChart::POINTS);
86 points->SetInputData(table.GetPointer(), 0, 2);
87 points->SetColor(0, 0, 0, 255);
88 points->SetWidth(1.0);
89 vtkPlotPoints::SafeDownCast(points)->SetMarkerStyle(vtkPlotPoints::PLUS);
90 points = chart->AddPlot(vtkChart::POINTS);
91 points->SetInputData(table.GetPointer(), 0, 3);
92 points->SetColor(0, 0, 255, 255);
93 points->SetWidth(4.0);
94 points->SetIndexedLabels(labels.GetPointer());
95
96 //Finally render the scene and compare the image to a reference image
97 view->GetRenderWindow()->SetMultiSamples(0);
98 view->GetInteractor()->Initialize();
99 view->GetInteractor()->Start();
100
101 return EXIT_SUCCESS;
102 }
103