Qchart的图形显示依附于QChartView,创建一个QChartView继承类,通过窗口部件的提升进行图表的显示
添加了触摸屏的双指捏合的缩放(测试用,参数采用的固定值,没有做太多的处理)
一、简单认识QLineSeries
QLineSeries属于折线类,它继承于QXYSeries类,可以使用QXYSeries类所有方法,对折线进行属性设置
二、创建折线
qsrand(QTime::currentTime().second());
//创建折线数据
for(int i = 0 ; i < 40;i++)
{
int value = (qrand() % 100);
m_line->append(i, value);
}
//x轴显示范围
m_X->setRange(0,20);
//y轴显示范围
m_Y->setRange(0,100);
//添加坐标
this->chart()->addAxis(m_X,Qt::AlignBottom);
this->chart()->addAxis(m_Y,Qt::AlignLeft);
//隐藏图例
//this->chart()->legend()->hide();
this->chart()->addSeries(m_line);
三、设置抗锯齿
setRenderHint(QPainter::Antialiasing);
四、设置缩放功能
//设置橡皮筋 用来图形缩放功能,右击缩放,左击拖拽放大(坐标系不会发生变化),如果需要坐标系同步变化需要对鼠标事件重写(待测试)
setRubberBand(QChartView::VerticalRubberBand);
五、图例的简单设置
1、采用setAlignment函数设置图例的显示位置(只能在坐标系的四周显示)
this->chart()->legend()->setAlignment(Qt::AlignRight);
2、自定义图例的显示位置,需要先进行图例与图像的分离设置
this->chart()->legend()->detachFromChart();
3、更换图例的图标显示
//MarkerShapeRectangle、MarkerShapeFromSeries、、MarkerShapeCircle
this->chart()->legend()->setMarkerShape(QLegend::MarkerShapeCircle)
六、自定义图例的图标样式文章来源:https://www.toymoban.com/news/detail-676338.html
//修改图例内容的大小
QFont font = this->chart()->legend()->font();
font.setPointSizeF(22);
this->chart()->legend()->setFont(font);
// 图片大小应和上面定义的图标大小一致或小于(画布)
QImage star(30, 30, QImage::Format_ARGB32);
star.fill(Qt::transparent);
// 加载图片
QPixmap image = QPixmap(":/tmp/state.png");
QPainter painter(&star);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QRgb(0xf6a625));
painter.setBrush(painter.pen().color());
painter.drawPixmap(0,0,18,18,image);
//标签字体颜色
this->chart()->legend()->markers().at(0)->setLabelBrush(QBrush(QColor(Qt::red)));
//图标背景填充
this->chart()->legend()->markers().at(0)->setBrush(QBrush(star));
//原有的图标边框设置为透明色
this->chart()->legend()->markers().at(0)->setPen(QPen(Qt::transparent));
//设置图例的位置与大小
this->chart()->legend()->setGeometry(600,50,150,30);
this->chart()->legend()->update();
//如果想更好的优化图例,可以自定义一个图框用来实现图例的效果
效果:
补(测试触摸屏的双指缩放):文章来源地址https://www.toymoban.com/news/detail-676338.html
//重写事件分发任务
bool M_ChartView::event(QEvent *pEvent)
{
switch (pEvent->type())
{
//触屏
case QEvent::TouchBegin:
return touchBeginEvent(pEvent); //自定义处理函数
case QEvent::TouchUpdate:
return touchUpdateEvent(pEvent);
case QEvent::TouchEnd:
return touchEndEvent(pEvent);
}
//这句不能丢,会出现奇怪的问题
return QWidget::event(pEvent);
}
bool M_ChartView::touchBeginEvent(QEvent *event)
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchStartPoints = touchEvent->touchPoints();
touchEvent->accept();//表示要处理该事件,就不会再被添加到事件队列了
return true;
}
bool M_ChartView::touchUpdateEventProcess(QEvent *event)
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
QList<QTouchEvent::TouchPoint> touchUpdatePoints = touchEvent->touchPoints();
// 多指触控
if (2 == touchUpdatePoints.count())
{
QPoint firstPoint = touchUpdatePoints.at(0).screenPos().toPoint(); // 第一触点坐标
QPoint lastPoint = touchUpdatePoints.at(1).lastScreenPos().toPoint(); // 第二触点坐标
if(!m_tochu)
{
m_fristPoint = firstPoint;
m_endPoint = lastPoint;
m_Text->clear();
m_tochu = true;
}
//Y轴
QValueAxis *axisY=dynamic_cast<QValueAxis*>(this->chart()->axisY());
const double yMin=axisY->min();
const double yMax=axisY->max();
//为了方便测试 参数都使用的固定值
if(m_fristPoint.y() < m_endPoint.y())
{
if(m_endPoint.y() < lastPoint.y())
{
//放大
if(yMax - yMin > 2)
{
this->chart()->axisY()->setRange(yMin + 1,yMax - 1);
if(yMax - yMin < 15)
{
axisY->setTickCount(axisY->max()- axisY->min() +1);
}
else
{
axisY->setTickCount(11);
}
}
if(yMin == yMax)
{
this->chart()->axisY()->setRange(yMin,yMax);
axisY->setTickCount(axisY->max() - axisY->min() +1);
}
}
else {
//缩小
if(yMin >= 1)
{
this->chart()->axisY()->setRange(yMin - 1,yMax + 1);
if(yMax - yMin < 15)
{
axisY->setTickCount(axisY->max()- axisY->min() +1);
}
else
{
axisY->setTickCount(11);
}
}
}
}
else
{
if(m_endPoint.y() > lastPoint.y())
{
//放大
if(yMax - yMin > 2)
{
this->chart()->axisY()->setRange(yMin + 1,yMax - 1);
if(yMax - yMin < 15)
{
axisY->setTickCount(axisY->max() - axisY->min() + 1);
}
else
{
axisY->setTickCount(11);
}
}
if(yMin == yMax)
{
this->chart()->axisY()->setRange(yMin,yMax);
axisY->setTickCount(axisY->max() - axisY->min() + 2);
}
}
else {
//缩小
if(yMin >= 1)
{
this->chart()->axisY()->setRange(yMin - 1,yMax + 1);
if(yMax - yMin < 15)
{
axisY->setTickCount(axisY->max()- axisY->min() +1);
}
else
{
axisY->setTickCount(11);
}
}
}
}
//X轴
QValueAxis *axisX=dynamic_cast<QValueAxis*>(this->chart()->axisX());
const double xMin=axisX->min();
const double xMax=axisX->max();
if(m_fristPoint.x() < m_endPoint.x())
{
if(m_endPoint.x() < lastPoint.x())
{
if(xMax - xMin > 2)
{
this->chart()->axisX()->setRange(xMin + 1,xMax - 1);
if(xMax - xMin < 20)
{
axisX->setTickCount(axisX->max()- axisX->min() +1);
}
else
{
axisX->setTickCount(11);
}
}
if(xMin == xMax)
{
this->chart()->axisX()->setRange(xMin,xMax);
axisX->setTickCount(axisX->max() - axisX->min() +1);
}
}
else
{
//缩小
if(xMin >= 1)
{
this->chart()->axisX()->setRange(xMin - 1,xMax + 1);
if(xMax - xMin < 20)
{
axisX->setTickCount(axisX->max()- axisX->min() +1);
}
else
{
axisX->setTickCount(11);
}
}
}
}
else
{
if(m_endPoint.x() > lastPoint.x())
{
//放大
if(xMax - xMin > 2)
{
this->chart()->axisX()->setRange(xMin + 1,xMax - 1);
if(xMax - xMin < 20)
{
axisX->setTickCount(axisX->max() - axisX->min() + 1);
}
else
{
axisX->setTickCount(11);
}
}
if(xMin == xMax)
{
this->chart()->axisX()->setRange(xMin,xMax);
axisX->setTickCount(axisX->max() - axisX->min() + 1);
}
}
else
{
//缩小
if(xMin >= 1)
{
this->chart()->axisX()->setRange(xMin - 1,xMax + 1);
if(xMax - xMin < 20)
{
axisX->setTickCount(axisX->max()- axisX->min() +1);
}
else
{
axisX->setTickCount(11);
}
}
}
}
}
return true;
}
bool M_ChartView::touchEndEvent(QEvent *event)
{
m_tochu = false;
return true;
}
到了这里,关于QChart——折线的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!