Qt第三库QCustomPlot—绘图四

styleddemo.png
  • 坐标轴:QCPAxis(xAxis,yAxis,xAxis1,yAxis2等四个成员变量)
    setTickStep(double step);//设置刻度间距
    setTickVector(const QVector<double> &vec);//将坐标刻度设置为vec
customPlot->legend->setVisible(true);  //图例legend
QFont legendFont = font();  
legendFont.setPointSize(9); 
customPlot->legend->setFont(legendFont);  
customPlot->legend->setBrush(QBrush(QColor(255,255,255,230)));  //填充色
customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignBottom|Qt::AlignRight);
customPlot->addGraph(customPlot->yAxis, customPlot->xAxis);  
customPlot->graph(0)->setPen(QPen(QColor(255, 100, 0))); //折线笔色
customPlot->graph(0)->setBrush(QBrush(QPixmap("./dali.png")));  //填充图片  
customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);  
customPlot->graph(0)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5)); //折线风格
customPlot->graph(0)->setName("Left maxwell function"); //折线名称
// prepare data:
QVector<double> x1(20), y1(20);
QVector<double> x2(100), y2(100);
QVector<double> x3(20), y3(20);
QVector<double> x4(20), y4(20);
for (int i=0; i<x1.size(); ++i)
{
  x1[i] = i/(double)(x1.size()-1)*10;
  y1[i] = qCos(x1[i]*0.8+qSin(x1[i]*0.16+1.0))*qSin(x1[i]*0.54)+1.4;
}
for (int i=0; i<x2.size(); ++i)
{
  x2[i] = i/(double)(x2.size()-1)*10;
  y2[i] = qCos(x2[i]*0.85+qSin(x2[i]*0.165+1.1))*qSin(x2[i]*0.50)+1.7;
}
for (int i=0; i<x3.size(); ++i)
{
  x3[i] = i/(double)(x3.size()-1)*10;
  y3[i] = 0.05+3*(0.5+qCos(x3[i]*x3[i]*0.2+2)*0.5)/(double)(x3[i]+0.7)+qrand()/(double)RAND_MAX*0.01;
}
for (int i=0; i<x4.size(); ++i)
{
  x4[i] = x3[i];
  y4[i] = (0.5-y3[i])+((x4[i]-2)*(x4[i]-2)*0.02);
}
 
// create and configure plottables:
QCPGraph *graph1 = customPlot->addGraph();
graph1->setData(x1, y1);
graph1->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, QPen(Qt::black, 1.5), QBrush(Qt::white), 9));
graph1->setPen(QPen(QColor(120, 120, 120), 2));
 
QCPGraph *graph2 = customPlot->addGraph();
graph2->setData(x2, y2);
graph2->setPen(Qt::NoPen);
graph2->setBrush(QColor(200, 200, 200, 20));
graph2->setChannelFillGraph(graph1);
 
QCPBars *bars1 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars1->setWidth(9/(double)x3.size());
bars1->setData(x3, y3);
bars1->setPen(Qt::NoPen);
bars1->setBrush(QColor(10, 140, 70, 160));
 
QCPBars *bars2 = new QCPBars(customPlot->xAxis, customPlot->yAxis);
bars2->setWidth(9/(double)x4.size());
bars2->setData(x4, y4);
bars2->setPen(Qt::NoPen);
bars2->setBrush(QColor(10, 100, 50, 70));
bars2->moveAbove(bars1);
 
// move bars above graphs and grid below bars:
customPlot->addLayer("abovemain", customPlot->layer("main"), QCustomPlot::limAbove);
customPlot->addLayer("belowmain", customPlot->layer("main"), QCustomPlot::limBelow);
graph1->setLayer("abovemain");
customPlot->xAxis->grid()->setLayer("belowmain");
customPlot->yAxis->grid()->setLayer("belowmain");
 
// set some pens, brushes and backgrounds:
customPlot->xAxis->setBasePen(QPen(Qt::white, 1));
customPlot->yAxis->setBasePen(QPen(Qt::white, 1));
customPlot->xAxis->setTickPen(QPen(Qt::white, 1));
customPlot->yAxis->setTickPen(QPen(Qt::white, 1));
customPlot->xAxis->setSubTickPen(QPen(Qt::white, 1));
customPlot->yAxis->setSubTickPen(QPen(Qt::white, 1));
customPlot->xAxis->setTickLabelColor(Qt::white);
customPlot->yAxis->setTickLabelColor(Qt::white);
customPlot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
customPlot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
customPlot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
customPlot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
customPlot->xAxis->grid()->setSubGridVisible(true);
customPlot->yAxis->grid()->setSubGridVisible(true);
customPlot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
customPlot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
customPlot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
customPlot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
QLinearGradient plotGradient;
plotGradient.setStart(0, 0);
plotGradient.setFinalStop(0, 350);
plotGradient.setColorAt(0, QColor(80, 80, 80));
plotGradient.setColorAt(1, QColor(50, 50, 50));
customPlot->setBackground(plotGradient);
QLinearGradient axisRectGradient;
axisRectGradient.setStart(0, 0);
axisRectGradient.setFinalStop(0, 350);
axisRectGradient.setColorAt(0, QColor(80, 80, 80));
axisRectGradient.setColorAt(1, QColor(30, 30, 30));
customPlot->axisRect()->setBackground(axisRectGradient);
 
customPlot->rescaleAxes();
customPlot->yAxis->setRange(0, 2);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,045评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,114评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,120评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,902评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,828评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,132评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,590评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,258评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,408评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,335评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,385评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,068评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,660评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,747评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,967评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,406评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,970评论 2 341

推荐阅读更多精彩内容

  • 一、下载Charts后可以到里面的内容 将下载后解压后的整个文件夹复制到工程里 但是只导入 工程Charts.xc...
    浪周周_f157阅读 1,081评论 0 1
  • 以下内容是从开源库自带的例子中选出的有实用性的高级用法及部分代码 CPTLineCap类:箭头指示器 例如: CP...
    jerry_tom_b8阅读 2,782评论 2 3
  • 这篇文章主要讲述Echarts设置字体和线条的颜色相关操作笔记,希望文章对你有所帮助,主要是自己的在线笔记吧。我在...
    蓝色梦想家阅读 9,846评论 1 1
  • 第一步:引入chart.js文件。此工具库在全局命名空间中定义了chart变量。 chart.js文件下载Rele...
    西可目阅读 3,617评论 2 4
  • 一个很好的图表制作第三方框架的使用:Charts 创建折线图 lineChartView,创建如下图的效果 初始化...
    StoneWing阅读 3,321评论 5 5