VTK-vtkXYPlotActor中XY轴数据来源

   vtkXYPlotActor接收两类数据输入,分别是vtkDataSet和vtkDataObject。

1.使用vtkPolyData作为输入数据,y数据一直是标量数据

先生成一些点数据和标量数据

//这是准备显示的数据
    Create(vtkPolyData, polydata);
    Create(vtkPoints, points);
    Create(vtkFloatArray, scalar);
    polydata->SetPoints(points);

    polydata->GetPointData()->SetScalars(scalar);

    //创造点数据
    for(int i = 0; i < 100; i++)
    {
        double point[3] = {0.5 * i, 2.0 * i, 3.0 * i};
        points->InsertNextPoint(point);
        scalar->InsertNextValue(std::sin(i * 0.1));
    }

1.1默认使用点索引值作为X轴数据来源

    //创建图表
    Create(vtkXYPlotActor, plot);
    //添加上面创造的数据
    plot->AddDataSetInput(polydata);

可以看到X坐标范围0-100,就是索引值范围。

1.2使用点的x分量作为X轴数据来源

//创建图表
    Create(vtkXYPlotActor, plot);
    //添加上面创造的数据
    plot->AddDataSetInput(polydata);
    //X轴数据来源
    plot->SetXValuesToValue();
    //第0条曲线使用点组分0,就是使用X分量作为X轴数据来源
    plot->SetPointComponent(0, 0);

可以看到X轴范围0-50,就是点的x分量范围。

1.3使用点的y分量作为X轴数据来源

    //创建图表
    Create(vtkXYPlotActor, plot);
    //添加上面创造的数据
    plot->AddDataSetInput(polydata);
    //X轴数据来源
    plot->SetXValuesToValue();
    //第0条曲线使用点组分1,就是使用Y分量作为X轴数据来源
    plot->SetPointComponent(0, 1);

可以看到X轴范围0-200,就是点的Y分量范围。

1.3使用点的z分量作为X轴数据来源

    //创建图表
    Create(vtkXYPlotActor, plot);
    //添加上面创造的数据
    plot->AddDataSetInput(polydata);
    //X轴数据来源
    plot->SetXValuesToValue();
    //第0条曲线使用点组分2,就是使用Z分量作为X轴数据来源
    plot->SetPointComponent(0, 2);

可以看到X轴范围0-300,就是点的Z分量范围。

2.使用vtkDataObject作为输入数据,这个模式可以自由的设置X轴和Y轴数据来源,但是资料太少,探索了一段时间。


#include<vtkFieldData.h>
#include<vtkFloatArray.h>
#include<vtkDataObject.h>
#include<vtkXYPlotActor.h>
#include<vtkLegendBoxActor.h>

#include<vtkNew.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkPolyDataMapper.h>
#include<vtkRenderWindowInteractor.h>

#include<vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)



#define Create(type,name)    vtkNew<type> name


int main()
{
    Create(vtkRenderer, render);
    Create(vtkRenderWindow, window);
    window->AddRenderer(render);
    Create(vtkRenderWindowInteractor, interactor);
    interactor->SetRenderWindow(window);


    //这是准备显示的数据
    Create(vtkDataObject, object);
    Create(vtkFloatArray, xAxis);        //x轴数据
    Create(vtkFloatArray, yAxis);        //y轴数据
    Create(vtkFieldData, field);
    object->SetFieldData(field);
    field->AddArray(xAxis);
    field->AddArray(yAxis);


    for(int i = 0; i < 100; i++)                    //创造正弦数据
    {
        float x = i * 0.1;
        float y = std::sin(x);
        xAxis->InsertNextValue(x);
        yAxis->InsertNextValue(y);
    }

    //创建图标
    Create(vtkXYPlotActor, plot);
    //添加上面创造的数据
    plot->AddDataObjectInput(object);

    //设置曲线0的X,使用数据0,就是xAxis
    plot->SetDataObjectXComponent(0, 0);
    //设置曲线0的Y,使用数据1,就是yAxis
    plot->SetDataObjectYComponent(0, 1);
    //Y轴标题放在最上部,避免遮挡
    plot->SetYTitlePositionToTop();
    //显示图例
    plot->SetLegend(true);
    //图例文字设为“Sin”
    plot->GetLegendActor()->SetEntryString(0, "Sin");

    render->AddActor(plot);


    interactor->Start();
    return 0;
}

  • 14
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值