VTK/Charts/API

From KitwarePublic
< VTK‎ | Charts
Revision as of 14:08, 19 October 2009 by Marcus.hanwell (talk | contribs) (Added some more details about possible API layout)
Jump to navigationJump to search

After looking at various other frameworks, tools and products out there here are some thoughts on the API. Employing a factory based interface seems like it would give us the most intuitive API. So a chart could be instantiated in the following ways,

vtkChart *chart = vtkChart::New(); // Instantiate a new chart
vtkPlot *barPlot = chart->AddPlot(Chart::BAR); // Add a bar plot
barPlot->SetData(table, 0, 2); // Set the data source to table, column 0 is x, column 2 is y
barPlot->SetWidth(20); // Set the width of the bars
vtkPlot *linePlot = chart->AddPlot(Chart::LINE); // Add a line plot
linePlot->SetData(table, 0, 2); // Set the data source to table, column 0 is x, column 2 is y
linePlot->SetWidth(2); // Set the line width

There has been a lot of talk about API. Do we want a declarative API? This would make constructs such as,

vtkChart *chart = vtkChart::New();
chart->SetWidth(200)
  ->SetHeight(200);

chart->AddPlot(vtkChart::BAR)
  ->SetData(table, 1, 2)
  ->SetWidth(20)
  ->SetLabel("My Data");

This would require setter functions that returned a pointer to the object, i.e.

vtkChart * vtkChart::SetWidth(int width);
vtkPlot * vtkChart::AddPlot(enum);
vtkPlot * vtkPlot::SetLabel(const char *label);

One nice thing about this approach is that ignoring the pointer returned allows traditional VTK syntax to be used.

vtkPlot *plot = chart->AddPlot(vtkChart::BAR);
plot->SetData(table, 1, 2);
plot->SetWidth(20);
plot->SetLabel("My Data");

Admittedly it is not in keeping with the current pattern used in VTK, there are numerous examples of its use in other libraries and frameworks. It should possibly be more of a global change to the library API, rather than isolated to one or two components.