[vtkusers] vtkChartXY - need help

Eric E. Monson emonson at cs.duke.edu
Wed Jun 29 10:52:59 EDT 2011


Hey Amitesh,

I don't have time to try out things myself right now, but I see that at least two of the Charts Cxx tests include text labels:

[vtk_source]/Charts/Testing/Cxx/TestStackedBarGraph.cxx
[vtk_source]/Charts/Testing/Cxx/TestStackedPlot.cxx

Maybe you can look at those examples and figure it out? If you have too much trouble, just write again and maybe I can do some more testing on my end.

Talk to you later,
-Eric


On Jun 28, 2011, at 3:16 PM, Amitesh Kumar wrote:

> Thank you very much for your reply!  My apology for writing late but it is only today that I finally gotten around to test my code. I have one final question regarding vtk charts. If my x-axis labels are string or char* then how can I display them? In my case my x-axis labels are in the form of "x1-x2". I first tried using  xLegends (in my code) as vtkStringArray with actual xAxis labels in there but that didn't work as my guess was that vtkCharts API tried to sort the points in the table. When that didn't work I tried using xLegends as vtkIntArray with indices 0 to n-1 for n bars in the bar plot which correctly created the plot. However 0 to n-1 is clearly not the right label in my case and I want use the values stored in xLabels (vtkStringArray) for my x-axis labels. I tried using setindexedLabels method as shown below but that absolutely didn't seem to have any effect on the x-axis labels which were all int. 
> 
> void vtkPlot::SetIndexedLabels(vtkStringArray * labels)
> 
> My plotting part of the code is as following. Note I am using vtk nightly 5.9 for this. Do you have any idea how to solve this problem.
> 
> Thank You 
> 
> Amitesh
> 
> -----------------------------------------------------------------------------------------------------------------------------------------
> 	std::stringstream ss3 (std::stringstream::in | std::stringstream::out);
>         vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
> 	view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
> 	view->GetRenderWindow()->SetSize(800, 600);
> 	vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
> 	view->GetScene()->AddItem(chart);
> 
>         vector<int>output = getDistribution(nPartition); //my internal function
> 	int numTuples = output.size();
> 
> 	vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
> 	vtkSmartPointer<vtkIntArray> xLegends = vtkSmartPointer<vtkIntArray>::New();
> 	xLegends->SetName("BarNumber");
> 	std::vector<string> Labels; 
> 	vtkSmartPointer<vtkStringArray> xLabels = vtkSmartPointer<vtkStringArray>::New();
> 
> 	vtkSmartPointer<vtkIntArray>qData = vtkSmartPointer<vtkIntArray>::New();
> 	qData->SetName(ext2);
> 	table->AddColumn(xLegends);
> 	table->AddColumn(qData);
> 	table->SetNumberOfRows(numTuples);
> 	xLabels->SetNumberOfValues(numTuples);
> 	
> 	if(numTuples > nPartition)
> 	{	
> 		Labels.push_back(string("Pre"));
> 		xLabels->SetValue(0, "Pre");
> 		table->SetValue(0,0,0);
> 		table->SetValue(0,1,output[nPartition]);
> 		for(int i = 0; i <nPartition; i++)
> 		{
> 			float minR, maxR;
> 			getPartitionRange(i, nPartition, minR, maxR); //my internal function
> 			ss3.str("");
> 			ss3<<setprecision(2)<<minR<<"-"<<setprecision(2)<<maxR;
> 			Labels.push_back(ss3.str());
> 			xLabels->SetValue(i+1, ss3.str().c_str());
> 			table->SetValue(i+1,0,i+1);
> 			table->SetValue(i+1,1,output[i]);
> 		}
> 		Labels.push_back(string("Post"));
> 		xLabels->SetValue(nPartition+1, "Post");
> 		table->SetValue(nPartition+1,0,nPartition+1);
> 		table->SetValue(nPartition+1,1,output[nPartition+1]);
> 	}
> 	else
> 	{
> 		for(int i = 0; i <nPartition; i++)
> 		{
> 			float minR, maxR;
> 			getPartitionRange(i, nPartition, minR, maxR);  //my internal function
> 			ss3.str("");
> 			ss3<<setprecision(2)<<minR<<"-"<<setprecision(2)<<maxR;
> 			xLabels->SetValue(i, ss3.str().c_str());
> 			Labels.push_back(ss3.str());
> 			//table->SetValue(i,0,ss3.str().c_str());
> 			table->SetValue(i,0,i);
> 			table->SetValue(i,1,output[i]);
> 		}
> 	}
> 
> 	vtkPlot *barPlot = 0;
> 	barPlot = chart->AddPlot(vtkChart::BAR);
> 	barPlot->SetInput(table, 0, 1);
> 	barPlot->SetColor(0, 0, 255, 255);	//Blue
> 
> 	barPlot->SetIndexedLabels(xLabels); //This doesn't seem to work. 
> 	
> 	//barPlot->GetLabels()->SetNumberOfValues(Labels.size());
> 	//for(int i=0; i<Labels.size(); i++)
> 		//barPlot->GetLabels()->SetValue(i, Labels[i].c_str());
> 
> 	chart->SetShowLegend(true);
> 	chart->Update();
> 	view->Update();
> 
> 
> On Wed, Jun 22, 2011 at 9:52 AM, Eric E. Monson <emonson at cs.duke.edu> wrote:
> 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
> 
> <TwoTablesPlotted.png>
> 
> 
> #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
> 
> 
> <TriangleMetrics.Area.jpg>

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


More information about the vtkusers mailing list