vtk chart - how to draw axis labels at the end of axis lines?

1.1k views Asked by At

Is it possible to draw axis labels at the end of labels? See picture for details:

http://habrastorage.org/files/94f/41a/b18/94f41ab1897a4bde815644bf287b4af9.png

If it is possible, how?

vtkNew<vtkContextView> contextView;

vtkNew<vtkTable> table;
vtkNew<vtkChartXY> chart;
vtkNew<vtkDoubleArray> timeAxis;
vtkNew<vtkDoubleArray> realDataAxis;
vtkNew<vtkDoubleArray> measuredDataAxis;

// ...

contextView->SetInteractor(vtkWidget->GetInteractor());
vtkWidget->SetRenderWindow(contextView->GetRenderWindow());

timeAxis->SetName("t");
realDataAxis->SetName("x(t)");
measuredDataAxis->SetName("x*(t)");

table->AddColumn(timeAxis.Get());
table->AddColumn(realDataAxis.Get());
table->AddColumn(measuredDataAxis.Get());

chart->SetShowLegend(true);
auto legend = chart->GetLegend();
legend->GetLabelProperties()->SetFontSize(14);
legend->GetLabelProperties()->SetFontFamilyToTimes();

auto xAxis = chart->GetAxis(vtkAxis::BOTTOM);
xAxis->SetTitle(QApplication::translate("DataSetChartWidget", "t").toStdString());

auto xTitleProps = xAxis->GetTitleProperties();
xTitleProps->SetFontSize(14);
xTitleProps->SetFontFamilyToTimes();

auto xLabelProps = xAxis->GetLabelProperties();
xLabelProps->SetFontSize(14);
xLabelProps->SetFontFamilyToTimes();

auto yAxis = chart->GetAxis(vtkAxis::LEFT);
yAxis->SetTitle(QApplication::translate("DataSetChartWidget", "x(t), x*(t)").toStdString());

auto yTitleProps = yAxis->GetTitleProperties();
yTitleProps->SetFontSize(14);
yTitleProps->SetFontFamilyToTimes();

auto yLabelProps = yAxis->GetLabelProperties();
yLabelProps->SetFontSize(14);
yLabelProps->SetFontFamilyToTimes();

contextView->GetScene()->AddItem(chart.Get());

And code for plotting:

auto realPlot = chart->AddPlot(vtkChart::LINE);
realPlot->SetInputData(table.Get(), 0, 1);

auto measuredPlot = chart->AddPlot(vtkChart::POINTS);
measuredPlot->SetInputData(table.Get(), 0, 2);
1

There are 1 answers

1
JohnnyQ On

You can set the vertical and horizontal alignment of the axis title.

e.g.

myVtkChart->GetAxis(0)->GetTitleProperties()->SetJustificationToLeft();

in your case it should be sufficient to call

xTitleProps->SetJustificationToRight();
yTitleProps->SetJustificationToLeft();

but it seems that the title width is not scaled to the axes length. This means its probably necessary to put some white spaces into your title string to get the expected result.