ChartDirector 饼状,柱状图如何实时更新数据

Jason_YF 2016-10-11 06:23:10
刚接触的ChartDirector,按照官方实例倒是可以很容易的绘制一个图表,但是想要扩展,却不好下手。 简单的饼状图绘制代码:

QHBoxLayout *m_hLayout=new QHBoxLayout;
QPushButton *m_pushButton=new QPushButton;
m_pushButton->setText(tr("截图"));

connect(m_pushButton,SIGNAL(clicked()),this,SLOT(test()));

m_hLayout->addWidget(m_pushButton);
m_hLayout->addStretch();

m_pViewer = new QChartViewer(this);
m_pVLayout->addLayout(m_hLayout);
m_pVLayout->addWidget(m_pViewer);
m_pVLayout->addStretch();
this->setLayout(m_pVLayout);
// Create a PieChart object of size 360 x 300 pixels
m_piechart = new PieChart(860, 600);

m_piechart->setDefaultFonts("mingliu.ttc", "mingliu.ttc Bold");
// Set the center of the pie at (180, 140) and the radius to 100 pixels
m_piechart->setPieSize(300, 260, 230);

// Add a title to the pie chart
m_piechart->addTitle(strTitle.toStdString().c_str(),"mingliu.ttc Bold",18)->setMargin(0,0,8,8);

// add a legend box where the top left corner is at (330, 50)
m_piechart->addLegend(650, 60);

// Output the chart
double data[] = {1, 2, 3};

// The labels for the bar chart
const char *labels[] = {"项目1", "项目2", "项目3"};
m_piechart->setData(DoubleArray(data, sizeof(data)/sizeof(data[0])), StringArray(labels,
sizeof(labels)/sizeof(labels[0])));
m_pViewer->setChart(m_piechart);
m_pViewer->show();

效果如下:


PieChart类的接口不多,按照如下代码修改显示数据:

double data[] = {3, 2, 1,2};
// The labels for the bar chart
const char *labels[] = {"test1", "test2", "test3", "test4"};
m_piechart->setData(DoubleArray(data, sizeof(data)/sizeof(data[0])), StringArray(labels,
sizeof(labels)/sizeof(labels[0])));
m_pViewer->setChart(m_piechart);
m_pViewer->updateViewPort(true, true);
m_pViewer->updateDisplay();

结果图表没有发生改变,没办法,只能按如下方法瞎搞一番:

m_pVLayout->removeWidget(m_pViewer);;
delete m_pViewer;
delete m_piechart;

m_pViewer = new QChartViewer(this);
m_pVLayout->addWidget(m_pViewer);
//this->setLayout(m_pVLayout);
// Create a PieChart object of size 360 x 300 pixels
m_piechart = new PieChart(860, 600);
...........

执行如上代码后,图表数据会更新,但是图表会位移,而且这种方法在UI实时更新数据时不可靠,重复的new和delete ,影响运行效率,不知各位用什么方法来更新ChartDirector 饼状,柱状图的数据?
...全文
653 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjp226 2017-05-08
  • 打赏
  • 举报
回复
楼主,问题最终解决了吗?

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧