From 4fca37ae3561da7d0664e0f943146398a8e554dd Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Tue, 22 Dec 2009 19:48:20 +0100 Subject: grapher: scale from right end of graph The right side of the graph represents the most recent time. Since there is never anything interesting to the right of that, it makes sense to have the origin of the scaling be there. * grapher/Graph.hxx (getHorizontalScale): new function * grapher/GraphStyle.cxx (GraphStyleBar::draw, GraphStyleDot::draw, GraphStyleEvent::draw): Use cairo transform functions to set up scaling. (GraphStyleBar::dataIndexAtPoint, GraphStyleEvent::dataIndexAtPoint): Base calculations on scaling from right. --- grapher/Graph.cxx | 17 +++++++-------- grapher/Graph.hxx | 8 +++++++ grapher/GraphStyle.cxx | 57 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 28 deletions(-) (limited to 'grapher') diff --git a/grapher/Graph.cxx b/grapher/Graph.cxx index afb52c74..4d697ae6 100644 --- a/grapher/Graph.cxx +++ b/grapher/Graph.cxx @@ -47,8 +47,7 @@ void Graph::draw(Cairo::RefPtr cr) _left = _right - 5000; } cr->save(); - double horizScale - = _zoomFactor * _graphWidth / static_cast(_right - _left); + double horizScale = getHorizontalScale(); cr->translate(_xOffset, _yOffset); cr->set_line_width(_lineWidth); @@ -103,10 +102,11 @@ void Graph::draw(Cairo::RefPtr 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 dash(1); dash[0] = _graphHeight / 10; @@ -114,7 +114,7 @@ void Graph::draw(Cairo::RefPtr 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; @@ -170,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, diff --git a/grapher/Graph.hxx b/grapher/Graph.hxx index d9f615b3..0853d988 100644 --- a/grapher/Graph.hxx +++ b/grapher/Graph.hxx @@ -51,6 +51,14 @@ public: int64_t getTimeAtPoint(double x); void window2GraphCoords(double x, double y, double& xgraph, double& ygraph); static void setCurrentTime(int64_t time) { _currentTime = time; } + + /* + * universal horizontal factor + */ + double getHorizontalScale() + { + return _zoomFactor * _graphWidth / static_cast(_right - _left); + } protected: GraphDataList _datasets; int64_t _left; diff --git a/grapher/GraphStyle.cxx b/grapher/GraphStyle.cxx index 5a6621bd..86cb823c 100644 --- a/grapher/GraphStyle.cxx +++ b/grapher/GraphStyle.cxx @@ -31,8 +31,15 @@ void GraphStyleBar::draw(std::tr1::shared_ptr graphData, int64_t left, right; double top, bottom; graph->getExtents(left, right, top, bottom); - double horizScale = (graph->_zoomFactor * graph->_graphWidth - / static_cast(right - left)); + double horizScale = graph->getHorizontalScale(); + cr->save(); + double lineWidth = cr->get_line_width(); + cr->translate(graph->_graphWidth, 0.0); + cr->scale(horizScale, 1.0); + cr->translate(left - right, 0.0); + cr->set_line_width(lineWidth / horizScale); + cr->set_source_rgba(graphData->color[0], graphData->color[1], + graphData->color[2], 1.0); GraphDataBase::TimeList::iterator lower = lower_bound(graphData->times.begin(), graphData->times.end(), left); GraphDataBase::TimeList::iterator upper @@ -42,14 +49,13 @@ void GraphStyleBar::draw(std::tr1::shared_ptr graphData, ++ditr) { size_t dataIndex = ditr - graphData->times.begin(); - cr->set_source_rgba(graphData->color[0], graphData->color[1], - graphData->color[2], 1.0); - cr->move_to((*ditr - left) * horizScale, 0); - cr->line_to((*ditr - left) * horizScale, + cr->move_to((*ditr - left), 0); + cr->line_to((*ditr - left), realData->data[dataIndex] * graph->_graphHeight / graphData->scale); cr->stroke(); } + cr->restore(); } ssize_t GraphStyleBar::dataIndexAtPoint(double x, double y, @@ -63,7 +69,7 @@ ssize_t GraphStyleBar::dataIndexAtPoint(double x, double y, int64_t left, right; double top, bottom; graph->getExtents(left, right, top, bottom); - double t = graph->getTimeAtPoint(x); + int64_t t = graph->getTimeAtPoint(x); TimeListPair range = equal_range(graphData->times.begin(), graphData->times.end(), t); if (range.first == graphData->times.end()) @@ -89,12 +95,14 @@ void GraphStyleDot::draw(std::tr1::shared_ptr graphData, int64_t left, right; double top, bottom; graph->getExtents(left, right, top, bottom); - double horizScale = (graph->_zoomFactor * graph->_graphWidth - / static_cast(right - left)); + double horizScale = graph->getHorizontalScale();; GraphDataBase::TimeList::iterator lower = lower_bound(graphData->times.begin(), graphData->times.end(), left); GraphDataBase::TimeList::iterator upper = upper_bound(graphData->times.begin(), graphData->times.end(), right); + cr->translate(graph->_graphWidth, 0.0); + cr->scale(horizScale, 1.0); + cr->translate(left - right, 0.0); cr->set_source_rgba(graphData->color[0], graphData->color[1], graphData->color[2], 1.0); @@ -103,12 +111,14 @@ void GraphStyleDot::draw(std::tr1::shared_ptr graphData, ++ditr) { size_t dataIndex = ditr - graphData->times.begin(); - cr->arc((*ditr - left) * horizScale, + // XXX Fix unequal scale in x, y + cr->arc((*ditr - left), (realData->data[dataIndex] * graph->_graphHeight / graphData->scale), - graph->_lineWidth / 2.0, 0.0, M_PI * 2.0); + (graph->_lineWidth / (2.0 * horizScale)), 0.0, M_PI * 2.0); cr->fill(); } + cr->restore(); } GraphStyleEvent GraphStyleEvent::instance; @@ -123,8 +133,7 @@ void GraphStyleEvent::draw(std::tr1::shared_ptr graphData, int64_t left, right; double top, bottom; graph->getExtents(left, right, top, bottom); - double horizScale = (graph->_zoomFactor * graph->_graphWidth - / static_cast(right - left)); + double horizScale = graph->getHorizontalScale(); double eventHeight = graph->_graphHeight * (graphData->scale / 100.0); cr->save(); cr->set_line_width(3 * graph->_lineWidth); @@ -134,6 +143,12 @@ void GraphStyleEvent::draw(std::tr1::shared_ptr graphData, cr->line_to(graph->_graphWidth, eventHeight); cr->stroke(); cr->restore(); + cr->save(); + // Global translation for user scaling + cr->translate(graph->_graphWidth, 0.0); + cr->scale(horizScale, 1.0); + cr->translate(left - right, 0.0); + GraphDataBase::TimeList::iterator lower = lower_bound(graphData->times.begin(), graphData->times.end(), left); GraphDataBase::TimeList::iterator upper @@ -147,12 +162,15 @@ void GraphStyleEvent::draw(std::tr1::shared_ptr graphData, cr->save(); cr->set_source_rgba(graphData->color[0], graphData->color[1], graphData->color[2], 1.0); - cr->rectangle((*ditr - left) * horizScale - 1.5 * graph->_lineWidth, + // Do cairo transformations here instead of our own math? + cr->rectangle((*ditr - left) - (1.5 * graph->_lineWidth / horizScale), eventHeight - 1.5 * graph->_lineWidth, - 3.0 * graph->_lineWidth, 3.0 * graph->_lineWidth); + 3.0 * (graph->_lineWidth / horizScale), + 3.0 * graph->_lineWidth); cr->fill(); cr->restore(); } + cr->restore(); } ssize_t GraphStyleEvent::dataIndexAtPoint(double x, double y, @@ -166,8 +184,7 @@ ssize_t GraphStyleEvent::dataIndexAtPoint(double x, double y, int64_t left, right; double top, bottom; graph->getExtents(left, right, top, bottom); - double horizScale = (graph->_zoomFactor * graph->_graphWidth - / static_cast(right - left)); + double horizScale = graph->getHorizontalScale(); double eventHeight = graph->_graphHeight * (graphData->scale / 100.0); GraphDataBase::TimeList::iterator lower = lower_bound(graphData->times.begin(), graphData->times.end(), left); @@ -177,12 +194,14 @@ ssize_t GraphStyleEvent::dataIndexAtPoint(double x, double y, double xgraph, ygraph; graph->window2GraphCoords(x, y, xgraph, ygraph); double yrect = eventHeight - 1.5 * graph->_lineWidth; + double rectWidth = 3 * graph->_lineWidth; for (GraphDataBase::TimeList::iterator ditr = lower, de = upper; ditr != de; ++ditr) { - double xrect = (*ditr - left) * horizScale - 1.5 * graph->_lineWidth; - if (xrect <= xgraph && xgraph < xrect + 3.0 * graph->_lineWidth + double xrect = ((*ditr - right) * horizScale + graph->_graphWidth + - .5 * rectWidth); + if (xrect <= xgraph && xgraph < xrect + rectWidth && yrect <= ygraph && ygraph < yrect + 3.0 * graph->_lineWidth) return static_cast(distance(graphData->times.begin(), ditr)); } -- cgit