如何设置KDChart饼图项目的标签
How to set labels for items of a KDChart pie diagram?
是否有任何方法可以为饼图的每个项设置文本标签,该饼图是使用Qt中的KDChart库创建的?
更具体地说,在这种特殊情况下,我不使用模型/视图体系结构。我通过KDChart::Widget
创建它,并仅使用Widget::setDataCell()
填充图表。
似乎有几种方法可以为轴设置文本标签,但我还没有遇到类似的饼图。不管怎样,这不是我需要的东西。我想为某些点设置标签,而不是为其轴设置标签。在应用于饼图时,它将类似于标题扇区。
我想,也许通过使用带有填充值的KDChart::Legend
,我可以实现所需的行为,但它没有起作用。
这是一个代码示例,也许它会有所帮助。但请记住,它已经改变了(清除了混乱的行),我还没有测试它的正确性:
KDChart::Widget* newChart = new KDChart::Widget;
newChart->setType( KDChart::Widget::Pie );
int curColNo = 0; // it's not a size_t 'coz setDataCell requires an int
for( QVector::const_iterator curValueIt = response.begin(); curValueIt != response.end(); ++curValueIt )
{
newChart->setDataCell( 0, curColNo, *curValueIt );
newChart->diagram()->setBrush( curColNo++, QBrush( m_responsesColors[curValueIt] ) );
m_legend->addDiagram( newChart->diagram() );
}
m_mainLayout.addWidget( newChart, m_curLayoutRowNo, m_curLayoutColNo );
还有一件事——我试图用不一致的列号(0,2,5,9等)填充它,但饼图绘制错误——一些扇区与其他扇区重叠。在其他类型的图表(例如条形图)中,所有数据都被正确地可视化。
你对商品标签有什么想法吗?
附言我已经弄清楚了跳过饼图的一些列来填充饼图的列有什么问题。如果不一致地填充列(跳过其中一些列),那么只需显式地将跳过的列的值设置为零。它将修复错误的饼图可视化问题。
也许KDChart应该自己找出跳过的列,并自动将其设置为null,但它不会。所以你自己做吧。
希望这能帮助到别人。
我自己找到了一个解决方案。考虑到KDChart库上的少量信息,我将其发布在这里,希望它能帮助有类似问题的人。
该解决方案在KDChart层次结构中占有相当深的位置。您需要手动打开标签显示。我为它创建了一个单独的函数。
void setValuesVisible( KDChart::AbstractDiagram* diagram, bool visible ) throw()
{
const QFont font( QFont( "Comic", 10 ) ); // the font for all labels
const int colCount = diagram->model()->columnCount();
for ( int iColumn = 0; iColumn < colCount; ++iColumn )
{
//QBrush brush( diagram->brush( iColumn ) ); // here you can get a color of the specified column
KDChart::DataValueAttributes a( diagram->dataValueAttributes( iColumn ) );
KDChart::TextAttributes ta( a.textAttributes() );
ta.setRotation( 0 );
ta.setFont( font );
ta.setAutoRotate( true );
//ta.setPen( QPen( brush.color() ) ); // here you can change a color of the current label's text
ta.setVisible( visible ); // this line turns on labels display
a.setTextAttributes( ta );
a.setVisible( true );
diagram->setDataValueAttributes( iColumn, a);
}
diagram->update();
}
请记住,有一个较短的解决方案-如果您不需要每个标签的唯一文本参数,只需设置"全局"DataValueAttributes
的TextAttributes
(KDChart::AbstractDiagram
类中有一个方法-AbstractDiagram::dataValueAttributes()
,没有任何参数)。
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 根据组合框中所选项目设置标签文本 (QT/C++)
- 触摸开始时更改菜单项目标签颜色
- 如何设置KDChart饼图项目的标签