📄 grapher-grapher-cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - grapher/grapher.cpp example file</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align=center>Grapher Plugin</h1> <p> This example graphs data from a simple text file. It demonstrates the use of the QNPInstance::writeReady() and QNPInstance::write() functions. <p> To build the example, you must first build the <a href=nsplugin.html>Qt Netscape Plugin Extension</a> library. Then type <tt>make</tt> in <tt>extensions/nsplugin/examples/grapher/</tt> and copy the resulting <tt>grapher.so</tt> or <tt>npgrapher.dll</tt> to the Plugins directory of your WWW browser. <p> <EMBED ALIGN=LEFT WIDTH=49% HEIGHT=300 SRC=graph.g1n graphstyle=pie fontfamily=times fontsize=18> <p> The text file it accepts as input has a title line, then a sequence of lines with a number, then a string. The plugin displays a pie chart of the numbers, each segment labelled by the associated string. The user can select a bar chart view of the same data by selecting from the menu that appears when they point at the plugin. <p> The HTML tag used to embed the graph is:<small><pre> <EMBED SRC=graph.g1n ALIGN=LEFT WIDTH=49% HEIGHT=300 graphstyle=pie fontfamily=times fontsize=18></pre></small> Note that some HTML arguments (which we have capitalized here) are interpreted by the browser, while others are used by the plugin.<br clear>With the simplicity and cross-platform nature of Qt-based plugins,pages like <a href="http://www.netcraft.com/survey/">Netcraft'sServer Graphs</a> can be provided much more efficiently for boththe service provider and consumer. Data need not be convertedto an image at the server.<br clear><pre>#include "grapher.h"Graph::Graph( GraphModel& mdl ) : model(mdl), style(Bar), pieRotationTimer(0), pieRotation(0){ // Create a menubar for the widget // menubar = new <a name="QMenuBar"></a><a href="qmenubar.html">QMenuBar</a>( this ); stylemenu = new <a name="QPopupMenu"></a><a href="qpopupmenu.html">QPopupMenu</a>; stylemenu->setCheckable(TRUE); for ( Style s = Pie; styleName[s]; s = Style(s+1)) { stylemenu->insertItem(styleName[s], s+100); } connect(stylemenu, SIGNAL(activated(int)), this, SLOT(setStyleFromMenu(int))); setStyle(Pie); menubar->insertItem("Style", stylemenu); menubar->insertSeparator(); <a href="qpopupmenu.html">QPopupMenu</a>* help = new <a href="qpopupmenu.html">QPopupMenu</a>; help-><a name="insertItem"></a><a href="qmenudata.html#deddb9">insertItem</a>( "About plugin...", this, SIGNAL(aboutPlugin()) ); help-><a href="qmenudata.html#deddb9">insertItem</a>( "About data...", this, SIGNAL(aboutData()) ); menubar->insertItem("Help", help);}Graph::~Graph(){}void <a name="421"></a>Graph::setStyle(Style s){ if (style != s) { if (pieRotationTimer) <a name="killTimer"></a><a href="qobject.html#46d1c6">killTimer</a>(pieRotationTimer); stylemenu->setItemChecked(100+style, FALSE); style = s; if ( style == Pie ) pieRotationTimer = startTimer( 80 ); else pieRotationTimer = 0; stylemenu->setItemChecked(100+style, TRUE); <a name="update"></a><a href="qwidget.html#f3f721">update</a>(); }}void <a name="420"></a>Graph::timerEvent(<a name="QTimerEvent"></a><a href="qtimerevent.html">QTimerEvent</a>*){ pieRotation = ( pieRotation + 6 ) % 360; <a name="repaint"></a><a href="qwidget.html#138c7b">repaint</a>(false); }void <a name="421"></a>Graph::setStyle(const char* stext){ for ( Style s = Pie; styleName[s]; s = Style(s+1) ) { if ( qstricmp(stext,styleName[s])==0 ) { <a name="setStyle"></a><a href="#421">setStyle</a>(s); return; } }}void <a name="422"></a>Graph::enterInstance(){ menubar->show();}void <a name="423"></a>Graph::leaveInstance(){ menubar->hide();}void <a name="424"></a>Graph::paintError(const char* e){ <a name="QPainter"></a><a href="qpainter.html">QPainter</a> p(this); int w = width(); p.<a name="drawText"></a><a href="qpainter.html#0f088f">drawText</a>(w/8, 0, w-w/4, <a name="height"></a><a href="qwidget.html#e3c588">height</a>(), AlignCenter|WordBreak, e);}void <a name="425"></a>Graph::paintBar(<a name="QPaintEvent"></a><a href="qpaintevent.html">QPaintEvent</a>* event){ if ( model.colType(0) != GraphModel::Numeric ) { <a name="paintError"></a><a href="#424">paintError</a>("First column not numeric, cannot draw bar graph\n"); return; } <a name="QList"></a><a href="qlist.html">QList</a><GraphModel::Datum>& data = model.graphData(); double max = 0.0; for (GraphModel::Datum* rowdata = data.first(); rowdata; rowdata = data.next()) { if (rowdata[0].dbl > max) max = rowdata[0].dbl; } const uint w = width(); const uint h = height(); <a href="qpainter.html">QPainter</a> p(this); p.<a name="setClipRect"></a><a href="qpainter.html#898839">setClipRect</a>(event-><a name="rect"></a><a href="qpaintevent.html#2d6e18">rect</a>()); if ( w > data.count() ) { // More pixels than data int x = 0; int i = 0; <a name="QFontMetrics"></a><a href="qfontmetrics.html">QFontMetrics</a> fm=fontMetrics(); int fh = fm.<a name="height"></a><a href="qfontmetrics.html#341253">height</a>(); for (GraphModel::Datum* rowdata = data.first(); rowdata; rowdata = data.next()) { <a name="QColor"></a><a href="qcolor.html">QColor</a> c; c.<a name="setHsv"></a><a href="qcolor.html#e47175">setHsv</a>( (i * 255)/data.count(), 255, 255 );// rainbow effect p.<a name="setBrush"></a><a href="qpainter.html#3e0cc8">setBrush</a>(c); int bw = (w-w/4-x)/(data.count()-i); int bh = int((h-h/4-1)*rowdata[0].dbl/max); p.<a name="drawRect"></a><a href="qpainter.html#4c0077">drawRect</a>( w/8+x, h-h/8-1-bh, bw, bh ); // ### This causes a crash, so comment out for now /*if (model.colType(1) == GraphModel::Label) { p.<a href="qpainter.html#0f088f">drawText</a>(w/8+x, h-h/8, bw, fh+h/8, WordBreak|AlignTop|AlignHCenter, *rowdata[1].str); }*/ i++; x+=bw; } } else { // More data than pixels int x = 0; int i = 0; double av = 0.0; int n = 0; for (GraphModel::Datum* rowdata = data.first(); rowdata; rowdata = data.next()) { int bx = i*w/data.count(); if (bx > x) { <a href="qcolor.html">QColor</a> c; c.<a href="qcolor.html#e47175">setHsv</a>( (x * 255)/w, 255, 255 );// rainbow effect p.<a name="setPen"></a><a href="qpainter.html#0183e4">setPen</a>(c); int bh = int(h*av/n/max); p.<a name="drawLine"></a><a href="qpainter.html#e3a489">drawLine</a>(x,h-1,x,h-bh); av = 0.0; n = 0; x = bx; } av += rowdata[0].dbl; n++; i++; } }}void <a name="426"></a>Graph::paintPie(<a href="qpaintevent.html">QPaintEvent</a>* event){ if ( model.colType(0) != GraphModel::Numeric ) { <a href=#424>paintError</a>("First column not numeric, cannot draw pie graph\n"); return; } <a href="qlist.html">QList</a><GraphModel::Datum>& data = model.graphData(); double total = 0.0; GraphModel::Datum* rowdata; for (rowdata = data.first(); rowdata; rowdata = data.next()) { total += rowdata[0].dbl; } // Only use first column for pie chart if ( !total ) return; int apos = (pieRotation-90)*16; const int w = width(); const int h = height(); const int xd = w - w/5; const int yd = h - h/5; pm.resize(<a name="width"></a><a href="qwidget.html#2edab1">width</a>(),<a href="qwidget.html#e3c588">height</a>()); pm.fill(<a name="backgroundColor"></a><a href="qwidget.html#d5be64">backgroundColor</a>()); <a href="qpainter.html">QPainter</a> p(&pm); p.<a name="setFont"></a><a href="qpainter.html#998df2">setFont</a>(<a name="font"></a><a href="qwidget.html#167922">font</a>()); p.<a href="qpainter.html#898839">setClipRect</a>(event-><a href="qpaintevent.html#2d6e18">rect</a>()); int i = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -