以下为本人原创,如有转载请注明
qt版本5.2 QtQuick2.0
显示:可横向拖动 ,只需将数据写入数组,折线图可自己按比例生成。端点,最后端点,标度,内部区间阴影,都会自动生成。
源码地址 http://download.csdn.net/detail/zzjzmdx/7113719
源码包包含:main.cpp app.qml linechart.cpp linechart.h
main.c
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//qmlRegisterType<PieChart>("Charts", 1, 0, "PieChart");
qmlRegisterType<LineChart>("LineCharts", 1, 0, "LineChart");
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("app.qml"));
view.show();
return app.exec();
}
linechart.h
class LineChart : public QQuickPaintedItem
{
Q_OBJECT
public:
explicit LineChart(QQuickPaintedItem *parent = 0);
void paint(QPainter *painter);
Q_PROPERTY(QColor color READ color WRITE setColor)
QColor color() const;
void setColor(const QColor &color);
Q_INVOKABLE void clearChart();
signals:
void chartCleared();
public slots:
private:
//QString m_name;
QColor m_color;
int monthCount;
int nameWidth;
int monthNow;
int dayNow;
int topDistance;//上部距离
};
linechart.cpp
void LineChart::paint(QPainter *painter)
{
QDateTime current_date_time = QDateTime::currentDateTime();
painter->setRenderHint(QPainter::Antialiasing);//平滑直线
monthNow= current_date_time.date().month();
dayNow= current_date_time.date().day();
if(monthNow==2){
nameWidth=120*28+2;
monthCount = 28;
}else if(monthNow==1||monthNow==3||monthNow==5||monthNow==7
||monthNow==8||monthNow==10||monthNow==12){
nameWidth=120*31+2;
monthCount = 31;
}else{
nameWidth=120*30+2;
monthCount = 30;
}
int i=0;
int flag=0;
for(;i<31;i++){
if(flag<traffic[i]){
flag=traffic[i];
}
}
painter->translate(0,530);//设置原点位置
QPen penLine(Qt::white,3);
painter->setPen(penLine);
for (int var = 0; var < dayNow; ++var) {
if(var==0){
painter->drawLine(0,0,60,-traffic[var]*(530-topDistance)/flag);
}else{
painter->drawLine((60+(var-1)*120),-traffic[var-1]*(530-topDistance)/flag,120*var+60,-traffic[var]*(530-topDistance)/flag);
}
}
for (int var = 0; var < dayNow; ++var) {
if(var==(dayNow-1)){
QPen Endpoint(Qt::white,10);
painter->setPen(Endpoint);
painter->drawEllipse((60+var*120)-5,(-traffic[var]*(530-topDistance)/flag)-5,10,10);//-5
}else{
//009955
// QPen Endpoint(Qt::white,3);
// painter->setPen(Endpoint);
// painter->drawEllipse((60+var*120)-5,(-traffic[var]*(530-topDistance)/flag)-5,10,10);//-5
// QColor insideBrushColor0(0,153,85,25);
// painter->setBrush(insideBrushColor0);
// QPen penpoints0(insideBrushColor0,10);
// painter->setPen(penpoints0);
painter->drawEllipse((60+var*120)-5,(-traffic[var]*(530-topDistance)/flag)-5,10,10);//-5
}
}
QPen Vertical(Qt::white,1);
painter->setPen(Vertical);
for (int var = 0; var < dayNow; ++var) {
painter->drawLine((60+var*120),0,(60+var*120),-traffic[var]*(530-topDistance)/flag+5);
}
QFont notToday("white",20);
painter->setFont(notToday);
for (int var = 0; var < dayNow; ++var) {
if(var==(dayNow-1)){
QFont isToday("white",30);
painter->setFont(isToday);
QString flagstr="";
painter->drawText((60+var*120-60),-traffic[var]*(530-topDistance)/flag-65,"TodayUsed");
flagstr = QString::number(traffic[var])+"MB";
if(traffic[var]>100){
painter->drawText((60+var*120-55),-traffic[var]*(530-topDistance)/flag-25,flagstr);
}else{
painter->drawText((60+var*120-35),-traffic[var]*(530-topDistance)/flag-25,flagstr);
}
}else{
QString flagstr="";
flagstr = QString::number(traffic[var])+"MB";
painter->drawText((60+var*120-45),-traffic[var]*(530-topDistance)/flag-25,flagstr);
}
}
QColor insideBrushColor(255,255,255,25);
QPen penpoints(insideBrushColor,0.15);
painter->setPen(penpoints);
painter->setBrush(insideBrushColor);
QPointF points[31];
points[0]=QPointF(0.0, 0.0);
for (int var = 1; var < dayNow+1; ++var) {
float x = (60+(var-1)*120);
float y = (-traffic[var-1]*(530-topDistance)/flag);
points[var]=QPointF(x, y);
}
points[dayNow+1]=QPointF(60+(dayNow-1)*120, 0.0);
painter->drawConvexPolygon(points,31);
}