diff options
author | Tim Moore <timoore@redhat.com> | 2009-12-01 19:05:09 +0100 |
---|---|---|
committer | Tim Moore <timoore@redhat.com> | 2009-12-02 19:39:37 +0100 |
commit | 6197b0dc80c4f87000d26213293fe2cb72fbe081 (patch) | |
tree | 23e7b4716f9d618e0891d67cee127c614177b370 /grapher | |
parent | 06e217d9990635be43a59233d75c504385d1e243 (diff) | |
download | systemtap-steved-6197b0dc80c4f87000d26213293fe2cb72fbe081.tar.gz systemtap-steved-6197b0dc80c4f87000d26213293fe2cb72fbe081.tar.xz systemtap-steved-6197b0dc80c4f87000d26213293fe2cb72fbe081.zip |
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
Diffstat (limited to 'grapher')
-rw-r--r-- | grapher/Graph.cxx | 66 | ||||
-rw-r--r-- | grapher/GraphData.hxx | 12 | ||||
-rw-r--r-- | grapher/GraphStyle.cxx | 131 | ||||
-rw-r--r-- | grapher/GraphStyle.hxx | 43 | ||||
-rw-r--r-- | grapher/Makefile.am | 2 | ||||
-rw-r--r-- | grapher/Makefile.in | 24 | ||||
-rw-r--r-- | grapher/StapParser.cxx | 4 |
7 files changed, 204 insertions, 78 deletions
diff --git a/grapher/Graph.cxx b/grapher/Graph.cxx index e7c03bd1..4b5b8eb6 100644 --- a/grapher/Graph.cxx +++ b/grapher/Graph.cxx @@ -88,74 +88,10 @@ namespace systemtap ++itr) { shared_ptr<GraphDataBase> graphData = *itr; - shared_ptr<GraphData<double> > realData - = dynamic_pointer_cast<GraphData<double> >(*itr); - shared_ptr<GraphData<string> > stringData - = dynamic_pointer_cast<GraphData<string> >(*itr); cr->save(); cr->translate(0.0, _graphHeight); cr->scale(1.0, -1.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); - // event bar - if (graphData->style == GraphDataBase::EVENT) - { - double eventHeight = _graphHeight * (graphData->scale / 100.0); - cr->save(); - cr->set_line_width(3 * _lineWidth); - cr->set_source_rgba(graphData->color[0], graphData->color[1], - graphData->color[2], .33); - cr->move_to(0, eventHeight); - cr->line_to(_graphWidth, eventHeight); - cr->stroke(); - cr->restore(); - } - 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); - if (graphData->style == GraphDataBase::BAR && realData) - { - cr->move_to((*ditr - _left) * horizScale, 0); - cr->line_to((*ditr - _left) * horizScale, - realData->data[dataIndex] * _graphHeight - / graphData->scale); - cr->stroke(); - } - else if (graphData->style == GraphDataBase::DOT && realData) - { - cr->arc((*ditr - _left) * horizScale, - realData->data[dataIndex] * _graphHeight / graphData->scale, - _lineWidth / 2.0, 0.0, M_PI * 2.0); - cr->fill(); - } - else if (graphData->style == GraphDataBase::EVENT && stringData) - { - double eventHeight = _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->save(); - cr->scale(1.0, -1.0); - cr->move_to((*ditr - _left) * horizScale, - -eventHeight -3.0 * _lineWidth - 2.0); - cr->show_text(stringData->data[dataIndex]); - cr->restore(); - cr->rectangle((*ditr - _left) * horizScale - 1.5 * _lineWidth, - eventHeight - 1.5 * _lineWidth, - 3.0 * _lineWidth, 3.0 * _lineWidth); - cr->fill(); - cr->restore(); - } - } + graphData->style->draw(graphData, this, cr); cr->restore(); cr->save(); cr->select_font_face("Sans", Cairo::FONT_SLANT_NORMAL, diff --git a/grapher/GraphData.hxx b/grapher/GraphData.hxx index e06ffdb8..9a267fc5 100644 --- a/grapher/GraphData.hxx +++ b/grapher/GraphData.hxx @@ -11,19 +11,17 @@ #include <boost/circular_buffer.hpp> +#include "GraphStyle.hxx" + namespace systemtap { struct GraphDataBase { virtual ~GraphDataBase() {} - enum Style - { BAR, - DOT, - EVENT - }; + typedef boost::circular_buffer<int64_t> TimeList; GraphDataBase(TimeList::capacity_type cap = 50000) - : scale(1.0), style(BAR), times(cap) + : scale(1.0), style(&GraphStyleBar::instance), times(cap) { color[0] = 0.0; color[1] = 1.0; color[2] = 0.0; } @@ -31,7 +29,7 @@ namespace systemtap // size of grid square at "normal" viewing double scale; double color[3]; - Style style; + GraphStyle* style; std::string title; std::string xAxisText; std::string yAxisText; 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<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr) + { + shared_ptr<GraphData<double> > realData + = dynamic_pointer_cast<GraphData<double> >(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<double>(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<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr) + { + shared_ptr<GraphData<double> > realData + = dynamic_pointer_cast<GraphData<double> >(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<double>(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<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr) + { + shared_ptr<GraphData<string> > stringData + = dynamic_pointer_cast<GraphData<string> >(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<double>(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(); + } + } + +} diff --git a/grapher/GraphStyle.hxx b/grapher/GraphStyle.hxx new file mode 100644 index 00000000..4a0cd955 --- /dev/null +++ b/grapher/GraphStyle.hxx @@ -0,0 +1,43 @@ +#ifndef SYSTEMTAP_GRAPHSTYLE_HXX +#define SYSTEMTAP_GRAPHSTYLE_HXX 1 +#include <tr1/memory> + +#include <cairomm/context.h> + +namespace systemtap +{ + class GraphDataBase; + class Graph; + + class GraphStyle + { + public: + virtual void draw(std::tr1::shared_ptr<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr) = 0; + }; + + class GraphStyleBar : public GraphStyle + { + public: + virtual void draw(std::tr1::shared_ptr<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr); + static GraphStyleBar instance; + }; + + class GraphStyleDot : public GraphStyle + { + public: + virtual void draw(std::tr1::shared_ptr<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr); + static GraphStyleDot instance; + }; + + class GraphStyleEvent : public GraphStyle + { + public: + virtual void draw(std::tr1::shared_ptr<GraphDataBase> graphData, + Graph* graph, Cairo::RefPtr<Cairo::Context> cr); + static GraphStyleEvent instance; + }; +} +#endif diff --git a/grapher/Makefile.am b/grapher/Makefile.am index 73f4cd8c..5bca286a 100644 --- a/grapher/Makefile.am +++ b/grapher/Makefile.am @@ -7,7 +7,7 @@ man_MANS = stapgraph.1 # the libglade ones. stapgraph_CPPFLAGS = -DPKGDATADIR='"${pkgdatadir}"' stapgraph_CXXFLAGS = $(libglade_CFLAGS) -Wall -Werror -stapgraph_SOURCES = grapher.cxx StapParser.cxx Graph.cxx GraphWidget.cxx CairoWidget.cxx +stapgraph_SOURCES = grapher.cxx StapParser.cxx Graph.cxx GraphWidget.cxx CairoWidget.cxx GraphStyle.cxx stapgraph_LDADD = $(libglade_LIBS) dist_pkgdata_DATA = graph-dialog.glade stap-start.glade endif diff --git a/grapher/Makefile.in b/grapher/Makefile.in index 34b0cd7a..f06402bc 100644 --- a/grapher/Makefile.in +++ b/grapher/Makefile.in @@ -49,13 +49,14 @@ am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" \ "$(DESTDIR)$(pkgdatadir)" PROGRAMS = $(bin_PROGRAMS) am__stapgraph_SOURCES_DIST = grapher.cxx StapParser.cxx Graph.cxx \ - GraphWidget.cxx CairoWidget.cxx + GraphWidget.cxx CairoWidget.cxx GraphStyle.cxx @BUILD_GRAPHER_TRUE@am_stapgraph_OBJECTS = \ @BUILD_GRAPHER_TRUE@ stapgraph-grapher.$(OBJEXT) \ @BUILD_GRAPHER_TRUE@ stapgraph-StapParser.$(OBJEXT) \ @BUILD_GRAPHER_TRUE@ stapgraph-Graph.$(OBJEXT) \ @BUILD_GRAPHER_TRUE@ stapgraph-GraphWidget.$(OBJEXT) \ -@BUILD_GRAPHER_TRUE@ stapgraph-CairoWidget.$(OBJEXT) +@BUILD_GRAPHER_TRUE@ stapgraph-CairoWidget.$(OBJEXT) \ +@BUILD_GRAPHER_TRUE@ stapgraph-GraphStyle.$(OBJEXT) stapgraph_OBJECTS = $(am_stapgraph_OBJECTS) am__DEPENDENCIES_1 = @BUILD_GRAPHER_TRUE@stapgraph_DEPENDENCIES = $(am__DEPENDENCIES_1) @@ -243,7 +244,7 @@ top_srcdir = @top_srcdir@ # the libglade ones. @BUILD_GRAPHER_TRUE@stapgraph_CPPFLAGS = -DPKGDATADIR='"${pkgdatadir}"' @BUILD_GRAPHER_TRUE@stapgraph_CXXFLAGS = $(libglade_CFLAGS) -Wall -Werror -@BUILD_GRAPHER_TRUE@stapgraph_SOURCES = grapher.cxx StapParser.cxx Graph.cxx GraphWidget.cxx CairoWidget.cxx +@BUILD_GRAPHER_TRUE@stapgraph_SOURCES = grapher.cxx StapParser.cxx Graph.cxx GraphWidget.cxx CairoWidget.cxx GraphStyle.cxx @BUILD_GRAPHER_TRUE@stapgraph_LDADD = $(libglade_LIBS) @BUILD_GRAPHER_TRUE@dist_pkgdata_DATA = graph-dialog.glade stap-start.glade all: all-am @@ -331,6 +332,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-CairoWidget.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-Graph.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-GraphStyle.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-GraphWidget.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-StapParser.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stapgraph-grapher.Po@am__quote@ @@ -430,6 +432,22 @@ stapgraph-CairoWidget.obj: CairoWidget.cxx @AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='CairoWidget.cxx' object='stapgraph-CairoWidget.obj' libtool=no @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(stapgraph_CPPFLAGS) $(CPPFLAGS) $(stapgraph_CXXFLAGS) $(CXXFLAGS) -c -o stapgraph-CairoWidget.obj `if test -f 'CairoWidget.cxx'; then $(CYGPATH_W) 'CairoWidget.cxx'; else $(CYGPATH_W) '$(srcdir)/CairoWidget.cxx'; fi` + +stapgraph-GraphStyle.o: GraphStyle.cxx +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(stapgraph_CPPFLAGS) $(CPPFLAGS) $(stapgraph_CXXFLAGS) $(CXXFLAGS) -MT stapgraph-GraphStyle.o -MD -MP -MF $(DEPDIR)/stapgraph-GraphStyle.Tpo -c -o stapgraph-GraphStyle.o `test -f 'GraphStyle.cxx' || echo '$(srcdir)/'`GraphStyle.cxx +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/stapgraph-GraphStyle.Tpo $(DEPDIR)/stapgraph-GraphStyle.Po +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='GraphStyle.cxx' object='stapgraph-GraphStyle.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(stapgraph_CPPFLAGS) $(CPPFLAGS) $(stapgraph_CXXFLAGS) $(CXXFLAGS) -c -o stapgraph-GraphStyle.o `test -f 'GraphStyle.cxx' || echo '$(srcdir)/'`GraphStyle.cxx + +stapgraph-GraphStyle.obj: GraphStyle.cxx +@am__fastdepCXX_TRUE@ $(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(stapgraph_CPPFLAGS) $(CPPFLAGS) $(stapgraph_CXXFLAGS) $(CXXFLAGS) -MT stapgraph-GraphStyle.obj -MD -MP -MF $(DEPDIR)/stapgraph-GraphStyle.Tpo -c -o stapgraph-GraphStyle.obj `if test -f 'GraphStyle.cxx'; then $(CYGPATH_W) 'GraphStyle.cxx'; else $(CYGPATH_W) '$(srcdir)/GraphStyle.cxx'; fi` +@am__fastdepCXX_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/stapgraph-GraphStyle.Tpo $(DEPDIR)/stapgraph-GraphStyle.Po +@am__fastdepCXX_FALSE@ $(AM_V_CXX) @AM_BACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='GraphStyle.cxx' object='stapgraph-GraphStyle.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(stapgraph_CPPFLAGS) $(CPPFLAGS) $(stapgraph_CXXFLAGS) $(CXXFLAGS) -c -o stapgraph-GraphStyle.obj `if test -f 'GraphStyle.cxx'; then $(CYGPATH_W) 'GraphStyle.cxx'; else $(CYGPATH_W) '$(srcdir)/GraphStyle.cxx'; fi` install-man1: $(man_MANS) @$(NORMAL_INSTALL) test -z "$(man1dir)" || $(MKDIR_P) "$(DESTDIR)$(man1dir)" diff --git a/grapher/StapParser.cxx b/grapher/StapParser.cxx index ddc14b2d..af5f2d7d 100644 --- a/grapher/StapParser.cxx +++ b/grapher/StapParser.cxx @@ -102,7 +102,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range) std::tr1::shared_ptr<GraphData<double> > dataSet(new GraphData<double>); if (style == "dot") - dataSet->style = GraphDataBase::DOT; + dataSet->style = &GraphStyleDot::instance; dataSet->color[0] = (hexColor >> 16) / 255.0; dataSet->color[1] = ((hexColor >> 8) & 0xff) / 255.0; dataSet->color[2] = (hexColor & 0xff) / 255.0; @@ -114,7 +114,7 @@ vector<string> commaSplit(const boost::sub_range<Glib::ustring>& range) { std::tr1::shared_ptr<GraphData<string> > dataSet(new GraphData<string>); - dataSet->style = GraphDataBase::EVENT; + dataSet->style = &GraphStyleEvent::instance; dataSet->color[0] = (hexColor >> 16) / 255.0; dataSet->color[1] = ((hexColor >> 8) & 0xff) / 255.0; dataSet->color[2] = (hexColor & 0xff) / 255.0; |