xylinechartitem.cpp
72 lines
| 1.7 KiB
| text/x-c
|
CppLexer
|
r21 | #include "xylinechartitem_p.h" | ||
|
r67 | #include "axisitem_p.h" | ||
|
r21 | #include "qxychartseries.h" | ||
#include <QPainter> | ||||
#include <QStyleOptionGraphicsItem> | ||||
#include <QDebug> | ||||
|
r30 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||
|
r21 | |||
|
r103 | XYLineChartItem::XYLineChartItem(QXYChartSeries* series,QGraphicsItem *parent) : | ||
|
r104 | ChartItem(parent), | ||
|
r103 | m_series(series), | ||
m_pathItem(new QGraphicsPathItem(this)) | ||||
|
r21 | { | ||
|
r85 | setFlags(QGraphicsItem::ItemClipsChildrenToShape); | ||
|
r21 | } | ||
|
r104 | void XYLineChartItem::setSize(const QSize &size) | ||
|
r103 | { | ||
m_rect = QRect(0, 0, size.width(), size.height()); | ||||
|
r85 | prepareGeometryChange(); | ||
updateGeometry(); | ||||
|
r103 | } | ||
|
r85 | |||
|
r67 | void XYLineChartItem::setPlotDomain(const PlotDomain& data) | ||
{ | ||||
m_plotDomain=data; | ||||
|
r85 | prepareGeometryChange(); | ||
updateGeometry(); | ||||
|
r67 | } | ||
|
r25 | |||
|
r67 | QRectF XYLineChartItem::boundingRect() const | ||
{ | ||||
|
r108 | return m_rect; | ||
} | ||||
void XYLineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | ||||
{ | ||||
m_pathItem->setPen(m_series->pen()); | ||||
|
r67 | } | ||
|
r108 | |||
|
r85 | /* | ||
QPainterPath XYLineChartItem::shape() const | ||||
{ | ||||
return m_pathItem->shape(); | ||||
} | ||||
*/ | ||||
|
r67 | void XYLineChartItem::updateGeometry() | ||
{ | ||||
if (!m_rect.isValid()) return; | ||||
|
r25 | |||
|
r85 | const qreal deltaX = m_rect.width()/m_plotDomain.spanX(); | ||
const qreal deltaY = m_rect.height()/m_plotDomain.spanY(); | ||||
|
r25 | |||
|
r85 | QPainterPath path; | ||
|
r25 | |||
|
r67 | for (int j = 0; j < m_series->count(); ++j) { | ||
qreal dx = m_series->x(j) - m_plotDomain.m_minX; | ||||
qreal dy = m_series->y(j) - m_plotDomain.m_minY; | ||||
qreal x = (dx * deltaX) + m_rect.left(); | ||||
qreal y = - (dy * deltaY) + m_rect.bottom(); | ||||
|
r85 | if(j==0) path.moveTo(x,y); | ||
else path.lineTo(x,y); | ||||
|
r67 | } | ||
|
r21 | |||
|
r85 | m_pathItem->setPath(path); | ||
|
r108 | m_pathItem->setPen(m_series->pen()); | ||
|
r86 | m_pathItem->setBrush(Qt::NoBrush); | ||
|
r21 | } | ||
|
r30 | QTCOMMERCIALCHART_END_NAMESPACE | ||