diff options
Diffstat (limited to 'grapher/Graph.cxx')
-rw-r--r-- | grapher/Graph.cxx | 74 |
1 files changed, 15 insertions, 59 deletions
diff --git a/grapher/Graph.cxx b/grapher/Graph.cxx index dc808fa4..079cd63e 100644 --- a/grapher/Graph.cxx +++ b/grapher/Graph.cxx @@ -19,7 +19,9 @@ 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), @@ -39,59 +41,13 @@ void Graph::draw(Cairo::RefPtr<Cairo::Context> cr) if (_autoScaling) { - // line separation - int linesPossible = (int)(_graphWidth / (_lineWidth + 2.0)); // Find latest time. - int64_t latestTime = 0; - for (GraphDataList::iterator ditr = _datasets.begin(), - de = _datasets.end(); - ditr != de; - ++ditr) - { - if (!(*ditr)->times.empty()) - { - int64_t lastDataTime = (*ditr)->times.back(); - if (lastDataTime > latestTime) - latestTime = lastDataTime; - } - } - int64_t minDiff = 0; - int64_t maxTotal = 0; - for (GraphDataList::iterator ditr = _datasets.begin(), - de = _datasets.end(); - ditr != de; - ++ditr) - { - GraphDataBase::TimeList& gtimes = (*ditr)->times; - if (gtimes.size() <= 1) - continue; - double totalDiff = 0.0; - for (GraphDataBase::TimeList::reverse_iterator ritr = gtimes.rbegin(), - re = gtimes.rend(); - ritr + 1 != gtimes.rend(); - ritr++) - { - int64_t timeDiff = *ritr - *(ritr + 1); - if (timeDiff < minDiff || (timeDiff != 0 && minDiff == 0)) - minDiff = timeDiff; - if (minDiff != 0 - && ((totalDiff + timeDiff) / minDiff + 1) > linesPossible) - break; - totalDiff += timeDiff; - } - if (totalDiff > maxTotal) - maxTotal = totalDiff; - } - // Now we have a global scale. - _right = latestTime; - if (maxTotal != 0) - _left = latestTime - maxTotal; - else - _left = _right - 1; + _right = _currentTime / 1000; + // Assume 1 pixel = 5 milliseconds + _left = _right - static_cast<int64_t>(5000 / _zoomFactor); } cr->save(); - double horizScale - = _zoomFactor * _graphWidth / static_cast<double>(_right - _left); + double horizScale = getHorizontalScale(); cr->translate(_xOffset, _yOffset); cr->set_line_width(_lineWidth); @@ -138,7 +94,7 @@ void Graph::draw(Cairo::RefPtr<Cairo::Context> cr) 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 / majorUnit) * majorUnit; + 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); @@ -146,10 +102,11 @@ void Graph::draw(Cairo::RefPtr<Cairo::Context> cr) cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_size(10.0); - cr->move_to(_xOffset, _yOffset); - cr->line_to(_xOffset, _height); - cr->move_to(_xOffset, _graphHeight); - cr->line_to(_graphWidth, _graphHeight); + 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; @@ -157,7 +114,7 @@ void Graph::draw(Cairo::RefPtr<Cairo::Context> cr) double prevTextAdvance = 0; for (int64_t tickVal = startTime; tickVal <= _right; tickVal += majorUnit) { - double x = (tickVal - _left) * horizScale + _xOffset; + double x = (tickVal - _right) * horizScale + _graphWidth; cr->move_to(x, _yOffset); cr->line_to(x, _graphHeight); std::ostringstream stream; @@ -213,8 +170,7 @@ bool Graph::containsPoint(double x, double y) int64_t Graph::getTimeAtPoint(double x) { - return (_left - + (_right - _left) * ((x - _xOffset)/(_zoomFactor * _graphWidth))); + return (x - _graphWidth) / getHorizontalScale() + _right; } void Graph::window2GraphCoords(double x, double y, |