summaryrefslogtreecommitdiffstats
path: root/grapher/Graph.cxx
blob: 079cd63e2158b4fdeb1d9c0830fba6b0607d521f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// systemtap grapher
// Copyright (C) 2009 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#include "Graph.hxx"

#include <sstream>
#include <iostream>
#include <iomanip>

namespace systemtap
{
using namespace std;
using namespace std::tr1;

GraphDataList GraphDataBase::graphData;
sigc::signal<void> GraphDataBase::graphDataChanged;

int64_t Graph::_currentTime = 0;

Graph::Graph(double x, double y)
  : _width(600), _height(200), _graphX(0), _graphY(0),
    _graphWidth(580), _graphHeight(180),
    _lineWidth(2), _autoScaling(true), _autoScrolling(true),
    _zoomFactor(1.0), _xOffset(20.0), _yOffset(0.0),
    _playButton(new CairoPlayButton), _timeBase(0),
    _left(0), _right(1), _top(5.0), _bottom(0.0)
{
  setOrigin(x, y);
  _graphX = x;
  _graphY = y;
}
  
  
void Graph::draw(Cairo::RefPtr<Cairo::Context> cr)
{
    
  if (_autoScaling)
    {
      // Find latest time.
      _right =  _currentTime / 1000;
      // Assume 1 pixel = 5 milliseconds
      _left = _right - static_cast<int64_t>(5000 / _zoomFactor);
    }
  cr->save();
  double horizScale = getHorizontalScale();
  cr->translate(_xOffset, _yOffset);
  cr->set_line_width(_lineWidth);

  for (GraphDataList::iterator itr = _datasets.begin(), e = _datasets.end();
       itr != e;
       ++itr)
    {
      shared_ptr<GraphDataBase> graphData = *itr;
      cr->save();
      cr->translate(0.0, _graphHeight);
      cr->scale(1.0, -1.0);
      graphData->style->draw(graphData, this, cr);
      cr->restore();
      cr->save();
      cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL,
                           Cairo::FONT_WEIGHT_BOLD);
      cr->set_font_size(14.0);
      cr->set_source_rgba(1.0, 1.0, 1.0, .8);
    
      if (!graphData->title.empty())
        {
          cr->move_to(20.0, 20.0);
          cr->show_text(graphData->title);
        }
      if (!graphData->xAxisText.empty())
        {
          cr->move_to(10.0, _graphHeight - 5);
          cr->show_text(graphData->xAxisText);
        }
      if (!graphData->yAxisText.empty())
        {
          cr->save();
          cr->translate(10.0, _height - 10.0);
          cr->rotate(-M_PI / 2.0);
          cr->move_to(10.0, 0.0);
          cr->show_text(graphData->yAxisText);
          cr->restore();
        }
      cr->restore();
        
    }
  cr->restore();
  // Draw axes
  double diff = static_cast<double>(_right - _left);
  int64_t majorUnit
    = static_cast<int64_t>(pow(10.0, floor(log(diff) / log(10.0))));
  int64_t startTime = ((_left - _timeBase) / majorUnit) * majorUnit + _timeBase;
  cr->save();
  cr->set_source_rgba(1.0, 1.0, 1.0, .9);
  cr->set_line_cap(Cairo::LINE_CAP_BUTT);
  cr->set_line_width(_lineWidth);
  cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL,
                       Cairo::FONT_WEIGHT_NORMAL);
  cr->set_font_size(10.0);
  cr->translate(_xOffset, 0.0);
  cr->move_to(0.0, _yOffset);
  cr->line_to(0.0, _height);
  cr->move_to(0.0, _graphHeight);
  cr->line_to(_graphWidth - _xOffset, _graphHeight);
  cr->stroke();
  std::valarray<double> dash(1);
  dash[0] = _graphHeight / 10;
  cr->set_dash(dash, 0);
  double prevTextAdvance = 0;
  for (int64_t tickVal = startTime; tickVal <= _right; tickVal += majorUnit)
    {
      double x = (tickVal - _right) * horizScale + _graphWidth;
      cr->move_to(x, _yOffset);
      cr->line_to(x, _graphHeight);
      std::ostringstream stream;
      stream << (tickVal - _timeBase);
      Cairo::TextExtents extents;
      cr->get_text_extents(stream.str(), extents);
      // Room for this label?
      if (x + extents.x_bearing > prevTextAdvance)
        {
          cr->move_to(x, _graphHeight + 5 + extents.height);
          cr->show_text(stream.str());
          prevTextAdvance = x + extents.x_advance;
        }
    }
  cr->stroke();
  cr->restore();
    
  if (!_autoScrolling)
    {
      _playButton->setVisible(true);
      _playButton->setOrigin(_graphWidth / 2 - 25, .875 * _graphHeight - 50);
      _playButton->draw(cr);
    }
    
}

void Graph::addGraphData(shared_ptr<GraphDataBase> data)
{
  _datasets.push_back(data);
}

void Graph::getExtents(int64_t& left, int64_t& right, double& top,
                       double& bottom) const
{
  left = _left;
  right = _right;
  top = _top;
  bottom = _bottom;
}

void Graph::setExtents(int64_t left, int64_t right, double top, double bottom)
{
  _left = left;
  _right = right;
  _top = top;
  _bottom = bottom;
}

bool Graph::containsPoint(double x, double y)
{
  return x >= _x0 && x < _x0 + _width && y >= _y0 && y < _y0 + _height;
}

int64_t Graph::getTimeAtPoint(double x)
{
  return (x - _graphWidth) / getHorizontalScale() + _right;
}

void Graph::window2GraphCoords(double x, double y,
                               double& xgraph, double& ygraph)
{
  xgraph = x -_xOffset;
  ygraph = -(y - _graphY) + _yOffset + _graphHeight;
}
}