2012-02-28 26 views
0

有沒有什麼方法可以爲使用Qt中的KDChart庫創建的餅圖中的每個項目設置文本標籤?如何爲KDChart餅圖的項目設置標籤?

更具體地說,我沒有在這種特殊情況下使用Model/View架構。我通過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等)和餅圖被錯誤地繪製 - 一些扇區與其他扇區重疊。在其他類型的圖表(例如條形圖)中,所有數據都可以正確顯示。

你對物品標籤有什麼想法嗎?

P.S.我想通過跳過一些餅圖來填充餅圖的列有什麼問題。如果您不一致地填充列(跳過其中的一部分),那麼只需將這些跳過的列的值明確設置爲零即可。它將解決錯誤的餅圖可視化問題。

大概KDChart應該自己找出跳過的列,並將其自動設置爲null,但不會。所以你自己來做。

希望,這會幫助別人。

回答

0

我已經找到了我自己的解決方案。考慮到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(有它的KDChart::AbstractDiagram類中的方法 - 沒有任何PARAMS AbstractDiagram::dataValueAttributes()TextAttributes如果你不需要獨特每個標籤的文本參數。