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/GraphStyle.cxx | 57 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'grapher/GraphStyle.cxx') 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