From 6197b0dc80c4f87000d26213293fe2cb72fbe081 Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Tue, 1 Dec 2009 19:05:09 +0100 Subject: Refactor drawing of different styles of graph into classes * grapher/GraphStyle.cxx: New file * grapher/GraphStyle.hxx: New file * grapher/Graph(draw): Move much drawing code to GraphStyle::draw --- grapher/GraphStyle.cxx | 131 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 grapher/GraphStyle.cxx (limited to 'grapher/GraphStyle.cxx') diff --git a/grapher/GraphStyle.cxx b/grapher/GraphStyle.cxx new file mode 100644 index 00000000..887ed55f --- /dev/null +++ b/grapher/GraphStyle.cxx @@ -0,0 +1,131 @@ +#include "GraphStyle.hxx" + +#include "GraphData.hxx" +#include "Graph.hxx" + +namespace systemtap +{ + using namespace std; + using namespace tr1; + + GraphStyleBar GraphStyleBar::instance; + + void GraphStyleBar::draw(std::tr1::shared_ptr graphData, + Graph* graph, Cairo::RefPtr cr) + { + shared_ptr > realData + = dynamic_pointer_cast >(graphData); + if (!realData) + return; + int64_t left, right; + double top, bottom; + graph->getExtents(left, right, top, bottom); + double horizScale = (graph->_zoomFactor * graph->_graphWidth + / static_cast(right - left)); + 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); + for (GraphDataBase::TimeList::iterator ditr = lower, de = upper; + ditr != de; + ++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, + realData->data[dataIndex] * graph->_graphHeight + / graphData->scale); + cr->stroke(); + } + } + + GraphStyleDot GraphStyleDot::instance; + + void GraphStyleDot::draw(std::tr1::shared_ptr graphData, + Graph* graph, Cairo::RefPtr cr) + { + shared_ptr > realData + = dynamic_pointer_cast >(graphData); + if (!realData) + return; + int64_t left, right; + double top, bottom; + graph->getExtents(left, right, top, bottom); + double horizScale = (graph->_zoomFactor * graph->_graphWidth + / static_cast(right - left)); + 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->set_source_rgba(graphData->color[0], graphData->color[1], + graphData->color[2], 1.0); + + for (GraphDataBase::TimeList::iterator ditr = lower, de = upper; + ditr != de; + ++ditr) + { + size_t dataIndex = ditr - graphData->times.begin(); + cr->arc((*ditr - left) * horizScale, + (realData->data[dataIndex] + * graph->_graphHeight / graphData->scale), + graph->_lineWidth / 2.0, 0.0, M_PI * 2.0); + cr->fill(); + } + } + + GraphStyleEvent GraphStyleEvent::instance; + + void GraphStyleEvent::draw(std::tr1::shared_ptr graphData, + Graph* graph, Cairo::RefPtr cr) + { + shared_ptr > stringData + = dynamic_pointer_cast >(graphData); + if (!stringData) + return; + int64_t left, right; + double top, bottom; + graph->getExtents(left, right, top, bottom); + double horizScale = (graph->_zoomFactor * graph->_graphWidth + / static_cast(right - left)); + double eventHeight = graph->_graphHeight * (graphData->scale / 100.0); + cr->save(); + cr->set_line_width(3 * graph->_lineWidth); + cr->set_source_rgba(graphData->color[0], graphData->color[1], + graphData->color[2], .33); + cr->move_to(0, eventHeight); + cr->line_to(graph->_graphWidth, eventHeight); + cr->stroke(); + cr->restore(); + 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); + for (GraphDataBase::TimeList::iterator ditr = lower, de = upper; + ditr != de; + ++ditr) + { + 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); + cr->fill(); + cr->restore(); + } + } + +} -- cgit From c10fce7d6aaa57a4f94f9d7aeea906597456f7ce Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Wed, 2 Dec 2009 19:27:07 +0100 Subject: Make the hover text conform to data displayed. Start of code to be more selective about the association between the hover text and the underling graph. Also, show the data set name in hover text. * grapher/GraphStyle.hxx (dataIndexAtPoint): New virtual function. * grapher/GraphStyle.cxx (dataIndexAtPoint): Implementation for bar graphs * grapher/GraphWidget.cxx (onHoverTimeout): Use dataIndexAtPoint. --- grapher/GraphStyle.cxx | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'grapher/GraphStyle.cxx') diff --git a/grapher/GraphStyle.cxx b/grapher/GraphStyle.cxx index 887ed55f..55fc73f4 100644 --- a/grapher/GraphStyle.cxx +++ b/grapher/GraphStyle.cxx @@ -8,6 +8,9 @@ namespace systemtap using namespace std; using namespace tr1; + typedef pair TimeListPair; + GraphStyleBar GraphStyleBar::instance; void GraphStyleBar::draw(std::tr1::shared_ptr graphData, @@ -41,6 +44,29 @@ namespace systemtap } } + ssize_t GraphStyleBar::dataIndexAtPoint(double x, double y, + shared_ptr graphData, + shared_ptr graph) + { + shared_ptr > realData + = dynamic_pointer_cast >(graphData); + if (!realData) + return -1; + int64_t left, right; + double top, bottom; + graph->getExtents(left, right, top, bottom); + double t = graph->getTimeAtPoint(x); + TimeListPair range + = equal_range(graphData->times.begin(), graphData->times.end(), t); + size_t dataIndex = distance(graphData->times.begin(), range.first); + double val = realData->data[dataIndex]; + double ycoord = val * graph->_graphHeight / graphData->scale; + if (y >= graph->_yOffset + graph->_graphHeight - ycoord) + return static_cast(dataIndex); + else + return -1; + } + GraphStyleDot GraphStyleDot::instance; void GraphStyleDot::draw(std::tr1::shared_ptr graphData, -- cgit