设置图例位置
m_Chart.data()->legend()->setGeometry(QRectF(this->width()-(this->width()/3)-30 ,this->height()-100-(this->height()/3),this->width()/3,this->height()/3));
#ifndef YJQCHARVIEW_H
#define YJQCHARVIEW_H
#include <QWidget>
#include <QtCharts>
#include <QPointF>
#include "YjModelProject.h"
class modifyChartPropertyWidget;
class YJQCharView :public QWidget
{
Q_OBJECT
public:
YJQCharView(QWidget *parent = nullptr);
~YJQCharView();
//////////////////
/// 函数名称:drawWithData
/// 函数功能:根据数据画表格
/// 返回值:
/// 函数参数:
/////////////////////////////////////////////////////////////////////////
bool drawWithData();
/////////////////////////////////////////////////////////////////////////
/// 函数名称:addLine
/// 函数功能:添加一条线
/// 返回值:
/// 函数参数: title 线名称 line 添加一条线
/////////////////////////////////////////////////////////////////////////
bool addLine(QString title,QLineSeries* line);
/////////////////////////////////////////////////////////////////////////
/// 函数名称:addPoint
/// 函数功能:给某条线后追加点
/// 返回值:
/// 函数参数:title 线名称 point点
/////////////////////////////////////////////////////////////////////////
bool addPoint(QString title,QPointF &point);
/////////////////////////////////////////////////////////////////////////
/// 函数名称:changeLineName
/// 函数功能:修改线名称
/// 返回值:
/// 函数参数:
/////////////////////////////////////////////////////////////////////////
bool changeLineName (QString &oldName,QString &newName);
/////////////////////////////////////////////////////////////////////////
/// 函数名称:deleteLine
/// 函数功能:删除某条线
/// 返回值:
/// 函数参数:Name 线名称
/////////////////////////////////////////////////////////////////////////
bool deleteLine(QString &Name);
///////////////////////////////////////////////////
/// \brief setMy_data
/// \param value 保存的项目数据指针
/////////////////////////////////////////////////////
void setMy_data(YjTrainResultForCharts *value);
void setColor(QString str,QColor &color);
void serlegend(); // 图例
void setVersionID(const QString &value);
void resizeEvent(QResizeEvent *event); //
public slots:
////////////////////////////////////////////////////////////////
/// \brief SaveChartViewSvg
/// 保存矢量图
////////////////////////////////////////////////////////////////
void SaveChartViewSvg();
void connectMarkers();
void disconnectMarkers();
void handleMarkerClicked();
protected:
QString versionID;//版本ID
YjTrainResultForCharts my_data;//保存当前数据指针
QMap<QString,QLineSeries*> m_DrawData;//保存当前要画的数据
QWidget *m_DownloadWidget;//下载按钮布局窗口
QPushButton *m_DownloadBtn;//下载按钮
QSharedPointer<QChartView> m_ChartView;//表格显示
QSharedPointer<QChart> m_Chart;// 智能指针表格对象
QSharedPointer<QValueAxis> m_AxisX;// 智能指针x坐标轴对象
QSharedPointer<QValueAxis> m_AxisY;// 智能指针Y坐标轴对象
QSharedPointer<modifyChartPropertyWidget> modifyWidget;//修改属性窗体指针
};
#endif // YJQCHARVIEW_H
#include "YJQCharView.h"
#include <QRectF>
#include <QtCharts/QLegendMarker>
#include <QtCharts/QXYLegendMarker>
#include "modifychartpropertywidget.h"
#include "YjCreateQjsonMessage.h"
#include "YjNetWorkMgr.h"
#include <QFont>
#include <QSvgGenerator>
#include <QHBoxLayout>
#include <QSpacerItem>
YJQCharView::YJQCharView(QWidget *parent)
:QWidget(parent),
m_ChartView(new QChartView),
m_Chart(new QChart),
m_AxisX(new QValueAxis),
m_AxisY(new QValueAxis),
modifyWidget(new modifyChartPropertyWidget)
{
//初始化按钮样式
QString m_style = "QPushButton{ color:rgb(255, 255, 225); border: 1px solid rgb(255, 255, 255); border-radius:10px; }"
"QPushButton::hover{}QPushButton::pressed{background-color:rgb(19,170,156);}";
m_DownloadBtn = new QPushButton(this);
m_DownloadBtn->setStyleSheet(m_style);
m_DownloadBtn->setText(QStringLiteral("下载"));
m_DownloadBtn->setMinimumHeight(40);
m_DownloadBtn->setMinimumWidth(120);
m_DownloadWidget = new QWidget();
initSlot();
initWidget();
}
YJQCharView::~YJQCharView()
{
if(m_DownloadWidget != nullptr)
{
delete m_DownloadWidget;
m_DownloadWidget = nullptr;
}
}
void YJQCharView::initWidget()
{
m_ChartView->setChart(m_Chart.data());
QVBoxLayout * layout = new QVBoxLayout;
layout->setMargin(0);
this->setLayout(layout);
layout->addWidget(m_ChartView.data());
m_AxisX.data()->setRange(0,1);
m_AxisY.data()->setRange(0,1);
//下载按钮 生成布局
QHBoxLayout * h_layout = new QHBoxLayout();
h_layout->setMargin(0);
m_DownloadWidget->setMaximumHeight(45);
m_DownloadWidget->setLayout(h_layout);
h_layout->addStretch();
h_layout->addWidget(m_DownloadBtn);
layout->addWidget(m_DownloadWidget);
}
void YJQCharView::initData()
{
m_DrawData.clear();
m_Chart.data()->setTitle(my_data.title);
if(my_data.titleFont != NULL && my_data.titleFont != "")
{
QFont font(my_data.titleFont);
m_Chart->setTitleFont(font);
}
if(my_data.xyFont != NULL && my_data.xyFont != "")
{
QFont font(my_data.xyFont);
m_AxisX.data()->setTitleFont(font);
m_AxisY.data()->setTitleFont(font);
}
m_AxisX.data()->setTitleText(my_data.Xaxistitle);
m_AxisY.data()->setTitleText(my_data.Yaxistitle);
for(auto obj:my_data.lineList)
{
QLineSeries *line = new QLineSeries();
QPen pen;
pen.setWidth(obj.weight);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
pen.setStyle(Qt::SolidLine);
line->setPen(pen);
for(auto obj1:obj.point)
{
line->append(obj1.rx(),obj1.ry());
}
QString l_name = obj.name +" (AUC ="+obj.auc+")";
// qDebug()<<"vl_name:"<<l_name;
line->setName(l_name);
line->setColor(obj.color);
m_DrawData.insert(obj.name,line);
}
}
void YJQCharView::initSlot()
{
// connect(m_DownloadBtn,&QPushButton::click(),this,&YJQCharView::SaveChartViewSvg()));
connect(m_DownloadBtn,SIGNAL(clicked()),this,SLOT(SaveChartViewSvg()));
}
bool YJQCharView::drawWithData()
{
m_Chart.data()->removeAllSeries();
//辅组线
QLineSeries *line = new QLineSeries();
line->setName(QStringLiteral("辅助线"));
line->append(0,0);
line->append(1,1);
QPen pen;
pen.setWidth(2);
pen.setColor(Qt::red);
pen.setCapStyle(Qt::RoundCap);
pen.setJoinStyle(Qt::RoundJoin);
pen.setStyle(Qt::DashLine);
line->setPen(pen);
m_Chart->addSeries(line);
for(auto obj:m_DrawData)
{
m_Chart->addSeries(obj);
m_Chart->setAxisX(m_AxisX.data(),obj);
m_Chart->setAxisY(m_AxisY.data(),obj);
}
return true;
}
bool YJQCharView::addLine(QString title, QLineSeries *line)
{
auto it = m_DrawData.insert(title,line);//插入到列表内
return true;
}
bool YJQCharView::addPoint(QString title, QPointF &point)
{
m_DrawData.value(title)->append(point.rx(),point.ry());
return true;
}
bool YJQCharView::changeLineName(QString &oldName, QString &newName)
{
if(m_DrawData.contains(oldName))
{
m_DrawData.insert(newName,m_DrawData.value(oldName));
m_DrawData.remove(oldName);
return true;
}else
{
return false;
}
}
bool YJQCharView::deleteLine(QString &Name)
{
if(m_DrawData.contains(Name))
{
m_DrawData.remove(Name);
return true;
}
return false;
}
void YJQCharView::setMy_data(YjTrainResultForCharts *value)
{
my_data.Xaxistitle = value->Xaxistitle;
my_data.Yaxistitle = value->Yaxistitle;
my_data.chartType = value->chartType;
my_data.lineList = value->lineList;
// qDebug()<<"1:"<<my_data.lineList.size();
// qDebug()<<"2:"<<value->lineList.size();
my_data.title = value->title;
my_data.type = value->type;
initData();
drawWithData();
serlegend();
connectMarkers();
}
void YJQCharView::setColor(QString str, QColor &color)
{
if(m_DrawData.contains(str))
{
m_DrawData.value(str)->setColor(color);
}
}
//图例相关
m_Chart.data()->legend() //拿到图例
void YJQCharView::serlegend()
{
m_Chart.data()->legend()->setVisible(true);
QLegend *legend = m_Chart.data()->legend();
// legend->setMarkerShape()
//legend->attachToChart();
if (legend->isAttachedToChart()) {
legend->detachFromChart();
//m_Chart.data()->legend()
if(my_data.lineFont != NULL && my_data.lineFont != "")
{
QFont font(my_data.lineFont);
m_Chart.data()->legend()->setFont(font);
}
m_Chart.data()->legend()->setBackgroundVisible(false);
m_Chart.data()->legend()->setBrush(QBrush(QColor(128, 128, 128, 128)));
m_Chart.data()->legend()->setPen(QPen(QColor(192, 192, 192, 192)));
// qDebug()<<"width"<<this->width()<<":"<<this->width()/4 <<" :"<<this->width()-(this->width()/4);
// qDebug()<<"height"<<this->height()<<":"<<this->height()/4<<" :"<<this->height()-(this->height()/4);
m_Chart.data()->legend()->setGeometry(QRectF(this->width()-(this->width()/3)-30,this->height()-100-(this->height()/3),this->width()/3,this->height()/3));
m_Chart.data()->legend()->update();
m_Chart.data()->legend()->setAlignment(Qt::AlignLeft);
}else{
//legend->attachToChart();
// legend->setBackgroundVisible(false);
}
update();
}
void YJQCharView::connectMarkers()
{
// Connect all markers to handler
const auto markers = m_Chart.data()->legend()->markers();
for (QLegendMarker *marker : markers) {
// Disconnect possible existing connection to avoid multiple connections
QObject::disconnect(marker, &QLegendMarker::clicked,
this, &YJQCharView::handleMarkerClicked);
QObject::connect(marker, &QLegendMarker::clicked, this, &YJQCharView::handleMarkerClicked);
//QObject::connect(marker, &QLegendMarker::, this, &YJQCharView::handleMarkerClicked);
}
}
void YJQCharView::disconnectMarkers()
{
const auto markers = m_Chart.data()->legend()->markers();
for (QLegendMarker *marker : markers) {
QObject::disconnect(marker, &QLegendMarker::clicked,
this, &YJQCharView::handleMarkerClicked);
}
}
void YJQCharView::handleMarkerClicked()
{
QLegendMarker* marker = qobject_cast<QLegendMarker*> (sender());
Q_ASSERT(marker);
switch (marker->type())
{
case QLegendMarker::LegendMarkerTypeXY:
{
modifyWidget.data()->setMy_data(&my_data);
modifyWidget.data()->exec();
if(!modifyWidget.data()->getFlag())
{
return;
}
my_data.title = modifyWidget.data()->getTitleStr();
my_data.Xaxistitle = modifyWidget.data()->getXTitleStr();
my_data.Yaxistitle = modifyWidget.data()->getYTitleStr();
my_data.titleFont = modifyWidget.data()->getTitleFont().toString();
my_data.xyFont = modifyWidget.data()->getXyfont().toString();
my_data.lineFont = modifyWidget.data()->getLinefont().toString();
QString m_data;
YjCreateQjsonMessage::createRocQJson(my_data,m_data);
QString trainortest;
if(my_data.type == 1)
{
trainortest = "trainRoc";
}else{
trainortest = "testRoc";
}
QString l_data = QString("versionId=%1&%2=%3").arg(versionID).arg(trainortest).arg(m_data);
YjNetWorkMgr::getInstanceNetWork()->setNetWorkOperator(YjMesType::Yj_NetType_UpdateChartData, l_data,"");
initData();
drawWithData();
connectMarkers();
break;
}
default:
{
qDebug() << "Unknown marker type";
break;
}
}
}
void YJQCharView::setVersionID(const QString &value)
{
versionID = value;
}
void YJQCharView::resizeEvent(QResizeEvent *event)
{
m_Chart.data()->legend()->setGeometry(QRectF(this->width()-(this->width()/3)-30 ,this->height()-100-(this->height()/3),this->width()/3,this->height()/3));
m_Chart.data()->legend()->setAlignment(Qt::AlignLeft);
m_Chart.data()->legend()->update();
QWidget::resizeEvent(event);
}
保存svg矢量图
void YJQCharView::SaveChartViewSvg()
{
QString filePath = QFileDialog::getSaveFileName(this, "Save SVG","", "SVG files (*.svg)");
if (filePath == "")
return;
QSvgGenerator generator;
generator.setFileName(filePath);
generator.setSize(QSize(this->width(), this->height()));
generator.setViewBox(QRect(0, 0, this->width(), this->height()));
generator.setTitle("SVG Example");
generator.setDescription("This SVG file is generated by Qt.");
QPainter painter;
painter.begin(&generator);
m_ChartView.data()->render(&painter);
painter.end();
}