diff options
-rw-r--r-- | grapher/Graph.cxx | 7 | ||||
-rw-r--r-- | grapher/Graph.hxx | 1 | ||||
-rw-r--r-- | grapher/GraphStyle.cxx | 48 | ||||
-rw-r--r-- | grapher/GraphStyle.hxx | 4 |
4 files changed, 47 insertions, 13 deletions
diff --git a/grapher/Graph.cxx b/grapher/Graph.cxx index 55ffcdf2..57f1dc9a 100644 --- a/grapher/Graph.cxx +++ b/grapher/Graph.cxx @@ -205,4 +205,11 @@ namespace systemtap return (_left + (_right - _left) * ((x - _xOffset)/(_zoomFactor * _graphWidth))); } + + void Graph::window2GraphCoords(double x, double y, + double& xgraph, double& ygraph) + { + xgraph = x -_xOffset; + ygraph = -y + _yOffset + _graphHeight; + } } diff --git a/grapher/Graph.hxx b/grapher/Graph.hxx index b9efb2a2..044a66d3 100644 --- a/grapher/Graph.hxx +++ b/grapher/Graph.hxx @@ -41,6 +41,7 @@ namespace systemtap std::tr1::shared_ptr<CairoPlayButton> _playButton; DatasetList& getDatasets() { return _datasets; } int64_t getTimeAtPoint(double x); + void window2GraphCoords(double x, double y, double& xgraph, double& ygraph); protected: DatasetList _datasets; int64_t _left; diff --git a/grapher/GraphStyle.cxx b/grapher/GraphStyle.cxx index 55fc73f4..cf3855e3 100644 --- a/grapher/GraphStyle.cxx +++ b/grapher/GraphStyle.cxx @@ -132,20 +132,9 @@ namespace systemtap ditr != de; ++ditr) { - size_t dataIndex = ditr - graphData->times.begin(); + // size_t dataIndex = ditr - graphData->times.begin(); double eventHeight = graph->_graphHeight * (graphData->scale / 100.0); cr->save(); - cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL, - Cairo::FONT_WEIGHT_NORMAL); - cr->set_font_size(12.0); - cr->set_source_rgba(graphData->color[0], graphData->color[1], - graphData->color[2], 1.0); - cr->save(); - cr->scale(1.0, -1.0); - cr->move_to((*ditr - left) * horizScale, - -eventHeight - 3.0 * graph->_lineWidth - 2.0); - cr->show_text(stringData->data[dataIndex]); - cr->restore(); cr->rectangle((*ditr - left) * horizScale - 1.5 * graph->_lineWidth, eventHeight - 1.5 * graph->_lineWidth, 3.0 * graph->_lineWidth, 3.0 * graph->_lineWidth); @@ -153,5 +142,38 @@ namespace systemtap cr->restore(); } } - + + ssize_t GraphStyleEvent::dataIndexAtPoint(double x, double y, + shared_ptr<GraphDataBase> graphData, + shared_ptr<Graph> graph) + { + shared_ptr<GraphData<string> > stringData + = dynamic_pointer_cast<GraphData<string> >(graphData); + if (!stringData) + return -1; + int64_t left, right; + double top, bottom; + graph->getExtents(left, right, top, bottom); + double horizScale = (graph->_zoomFactor * graph->_graphWidth + / static_cast<double>(right - left)); + double eventHeight = graph->_graphHeight * (graphData->scale / 100.0); + 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); + // easier to transform x,y into graph coordinates + double xgraph, ygraph; + graph->window2GraphCoords(x, y, xgraph, ygraph); + double yrect = eventHeight - 1.5 * 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 + && yrect <= ygraph && ygraph < yrect + 3.0 * graph->_lineWidth) + return static_cast<ssize_t>(distance(lower, ditr)); + } + return -1; + } } diff --git a/grapher/GraphStyle.hxx b/grapher/GraphStyle.hxx index 9625f451..ce75d698 100644 --- a/grapher/GraphStyle.hxx +++ b/grapher/GraphStyle.hxx @@ -48,6 +48,10 @@ namespace systemtap public: void draw(std::tr1::shared_ptr<GraphDataBase> graphData, Graph* graph, Cairo::RefPtr<Cairo::Context> cr); + virtual ssize_t dataIndexAtPoint(double x, double y, + std::tr1::shared_ptr<GraphDataBase> + graphData, + std::tr1::shared_ptr<Graph> graph); static GraphStyleEvent instance; }; } |