qcustomplot圆_QCustomplot(二) 源码解读

1 customPlot->legend->setVisible(true);

2

3 // 生成2种随机的蜡烛图数据,第一个是蜡烛图数据,第二个是美国线数据

4 int n = 500;

5 QVector time(n), value1(n), value2(n);

6 QDateTime start = QDateTime(QDate(2014, 6, 11));

7 start.setTimeSpec(Qt::UTC);

8 double startTime = start.toTime_t();

9 double binSize = 3600*24; // 1天的数据

10 time[0] = startTime;

11 value1[0] = 60;

12 value2[0] = 20;

13 qsrand(9);//生成随机数时给指定的种子,那么生成的随机数都是相同的,因此每次运行后得到的结果都是不变的

14 for (int i=1; i

15 {

16 time[i] = startTime + 3600*i;

17 value1[i] = value1[i-1] + (qrand()/(double)RAND_MAX-0.5)*10;

18 value2[i] = value2[i-1] + (qrand()/(double)RAND_MAX-0.5)*3;

19 }

20

21 // 初始化一个蜡烛图指针:

22 QCPFinancial *candlesticks = new QCPFinancial(customPlot->xAxis, customPlot->yAxis);

23 candlesticks->setName("Candlestick");

24 candlesticks->setChartStyle(QCPFinancial::csCandlestick);//设置图表类型为蜡烛图

25 candlesticks->data()->set(QCPFinancial::timeSeriesToOhlc(time, value1, binSize, startTime));//设置数据

26 candlesticks->setWidth(binSize*0.9);//设置每一个数据项的绘制宽度

27 candlesticks->setTwoColored(true);//设置是否显示两种颜色

28 candlesticks->setBrushPositive(QColor(245, 245, 245));//设置收>开画刷

29 candlesticks->setBrushNegative(QColor(40, 40, 40));//设置收

30 candlesticks->setPenPositive(QPen(QColor(0, 0, 0)));//设置收>开画笔

31 candlesticks->setPenNegative(QPen(QColor(0, 0, 0)));//设置收>开画笔

32

33 // 初始化一个美国线图指针:

34 QCPFinancial *ohlc = new QCPFinancial(customPlot->xAxis, customPlot->yAxis);

35 ohlc->setName("OHLC");

36 ohlc->setChartStyle(QCPFinancial::csOhlc);//设置图表类型为美国线

37 ohlc->data()->set(QCPFinancial::timeSeriesToOhlc(time, value2, binSize/3.0, startTime)); //为了区分于蜡烛图显示,

38 ohlc->setWidth(binSize*0.2);

39 ohlc->setTwoColored(true);

40

41 // 创建一个坐标轴矩形

42 QCPAxisRect *volumeAxisRect = new QCPAxisRect(customPlot);

43 customPlot->plotLayout()->addElement(1, 0, volumeAxisRect);

44 volumeAxisRect->setMaximumSize(QSize(QWIDGETSIZE_MAX, 100));

45 volumeAxisRect->axis(QCPAxis::atBottom)->setLayer("axes");

46 volumeAxisRect->axis(QCPAxis::atBottom)->grid()->setLayer("grid");

47 // 设置自己构造的坐标轴矩形属性

48 customPlot->plotLayout()->setRowSpacing(0);

49 volumeAxisRect->setAutoMargins(QCP::msLeft|QCP::msRight|QCP::msBottom);

50 volumeAxisRect->setMargins(QMargins(0, 0, 0, 0));

51 // 生成两种颜色的柱状图

52 customPlot->setAutoAddPlottableToLegend(false);//是否自动生成图例

53 QCPBars *volumePos = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));

54 QCPBars *volumeNeg = new QCPBars(volumeAxisRect->axis(QCPAxis::atBottom), volumeAxisRect->axis(QCPAxis::atLeft));

55 for (int i=0; i

56 {

57 int v = qrand()%20000+qrand()%20000+qrand()%20000-10000*3;

58 (v < 0 ? volumeNeg : volumePos)->addData(startTime+3600*5.0*i, qAbs(v)); //构造随机数据

59 }

60 volumePos->setWidth(3600*4);

61 volumePos->setPen(Qt::NoPen);

62 volumePos->setBrush(QColor(100, 180, 110));

63 volumeNeg->setWidth(3600*4);

64 volumeNeg->setPen(Qt::NoPen);

65 volumeNeg->setBrush(QColor(180, 90, 90));

66

67 // 设置自己构造的坐标轴矩形的x轴和QCustomPlot中的坐标轴矩形(默认的会生成一个)x轴同步,两个坐标轴永远显示的坐标范围是一样的

68 connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), volumeAxisRect->axis(QCPAxis::atBottom), SLOT(setRange(QCPRange)));

69 connect(volumeAxisRect->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis, SLOT(setRange(QCPRange)));

70 // 构造一个新的坐标轴刻度计算类

71 QSharedPointer dateTimeTicker(new QCPAxisTickerDateTime);

72 dateTimeTicker->setDateTimeSpec(Qt::UTC);

73 dateTimeTicker->setDateTimeFormat("dd. MMMM");

74 volumeAxisRect->axis(QCPAxis::atBottom)->setTicker(dateTimeTicker);//赋予自己构造的坐标轴矩形的x轴一个新的刻度计算类

75 volumeAxisRect->axis(QCPAxis::atBottom)->setTickLabelRotation(15);

76 customPlot->xAxis->setBasePen(Qt::NoPen);

77 customPlot->xAxis->setTickLabels(false);//不显示坐标轴文本

78 customPlot->xAxis->setTicks(false); //  不显示坐标轴 (这个接口实现的不友好,后续文章我会具体说到)

79 customPlot->xAxis->setTicker(dateTimeTicker);//赋予默认的坐标轴矩形的x轴一个新的刻度计算类

80 customPlot->rescaleAxes();

81 customPlot->xAxis->scaleRange(1.025, customPlot->xAxis->range().center());

82 customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());

83

84 // 设置两个坐标轴矩形左右对齐

85 QCPMarginGroup *group = new QCPMarginGroup(customPlot);

86 customPlot->axisRect()->setMarginGroup(QCP::msLeft|QCP::msRight, group);

87 volumeAxisRect->setMarginGroup(QCP::msLeft|QCP::msRight, group);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值