JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtpgraphlabels.h
1/*
2 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
3
4
5
6 This software is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License (LGPL) as published by
8 the Free Software Foundation, either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License (LGPL) for more details.
15
16 You should have received a copy of the GNU Lesser General Public License (LGPL)
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef jkqtpgraphlabels_H
21#define jkqtpgraphlabels_H
22
23
24#include <QString>
25#include <QPainter>
26#include <QPair>
27#include <functional>
28#include "jkqtplotter/jkqtptools.h"
29#include "jkqtplotter/jkqtplotter_imexport.h"
30#include "jkqtplotter/jkqtpgraphsbase.h"
31#include "jkqtplotter/graphs/jkqtpgraphlabelstylemixin.h"
32
33// forward declarations
34class JKQTBasePlotter;
35class JKQTPlotter;
37class JKQTPDatastore;
38
39
40
41/*! \brief This graph plots a series of data labels. This can be used to add number-labels to e.g. a barchart.
42 \ingroup jkqtplotter_graphlabels
43
44 Sometimes it is necessary to display the numeric value of a datapoint right in the plot.
45 To achieve this, JKQTPXYGraphLabels is provided, which draws a text-label next to each datapoint in a
46 series of x- and y-values (thus it is derived from JKQTPXYGraph). Each label is drawn with a little offset
47 from the actual datapoint. Under each label, a small (stylable) box is drawn, which is by default simply
48 the same color as the background color of complete plot, but with transparency, thus providing a certain
49 level of contrast to the text, even if drawn above other plot elements. Use the properties of the parent
50 mix-in class JKQTPGraphValueLabelStyleMixin to style the text and the box.
51
52 Here is an example of labels together with a bars and lines chart:
53
54 \image html JKQTPGLabelAwayFromXAxis.png
55
56 Also other styles of boxes are available:
57
58 \image html JKQTPGLSimpleBoxAndLine.png
59
60 To achieve this, use code like this:
61 \code
62 // create barchart and line-chart to display the data:
63 JKQTPBarVerticalGraph* graph1=new JKQTPBarVerticalGraph(&plot);
64 graph1->setBarPositionColumn(columnX);
65 graph1->setBarHeightColumn(columnY);
66
67 JKQTPXYLineGraph* graph2=new JKQTPXYLineGraph(&plot);
68 graph2->setXColumn(columnX);
69 graph2->setYColumn(columnY);
70
71 // create the data labels:
72 JKQTPXYGraphLabels* graphLabels=new JKQTPXYGraphLabels(JKQTPXYGraphLabels::YValueLabel, &plot);
73 graphLabels->setXColumn(graph1->getXColumn());
74 graphLabels->setYColumn(graph1->getYColumn());
75 graphLabels->setLabelPosition(JKQTPGLabelAwayFromXAxis),
76
77 // add the graphs to the plot, so it is actually displayed
78 plot.addGraph(graph1);
79 plot.addGraph(graph2);
80 plot.addGraph(graphLabels);
81 \endcode
82
83 The text shown in the label is determined by a functor of type JKQTPXYGraphLabels::LabelGenerator that calculates
84 it from the labels position and data-index.
85 This functor can be custimized (set setCustomLabelGenerator() or the matching constructor), or it may be one of
86 three default immplementations, that display:
87 - the current x-value (JKQTPXYGraphLabels::XValueLabel constructor arguent or setDefaultXLabelGenerator() )
88 - the current y-value (JKQTPXYGraphLabels::YValueLabel constructor arguent or setDefaultYLabelGenerator() )
89 - the current x- and y-value (JKQTPXYGraphLabels::XYValueLabel constructor arguent or setDefaultXYLabelGenerator() )
90 .
91
92 The default generators can be further customized with a series of properties on how to format their output:
93 - setXValueLabelFormat(), setYValueLabelFormat(), setXYValueLabelFormat() ... these set the basic label and are
94 typically only \c "$%1$" or \c "$%1/%2$" , but you can set them to any string (\c %1 / \c %2 is replaced by
95 the string-converted x-/y-values) and e.g. add a prefix, like \c setXValueLabelFormat("$x=%1$").
96 You can use any LaTeX-markup that is allowed by JKQTMathText here!
97 - setXDefaultConverter(), setXDefaultPrecision(), setXBelowIsZero(), ... (all these exist in X and Y variants!)
98 .
99
100 \see \ref JKQTPlotterGraphLabelsExample
101
102 */
104 Q_OBJECT
105 public:
107 XValueLabel, //!< \brief generates a label with the x-coordinate only, calls setDefaultXLabelGenerator()
108 YValueLabel, //!< \brief generates a label with the y-coordinate only, calls setDefaultYLabelGenerator()
109 XYValueLabel //!< \brief generates a label with the x- and y-coordinate, calls setDefaultXYLabelGenerator()
110 };
111 Q_ENUM(LabelContentsDefaultType)
112 /** \brief type of a functor that generates a label */
113 typedef std::function<QString(double,double,int)> LabelGenerator;
114
115 /** \brief class constructor, generates a JKQTPXYGraphLabels with one of the default label generator functors (x-value, y-value or x+y-values) */
117 /** \brief class constructor, generates a JKQTPXYGraphLabels with one of the default label generator functors (x-value, y-value or x+y-values) */
119 /** \brief class constructor, generates a JKQTPXYGraphLabels with a custom LabelGenerator functor */
120 JKQTPXYGraphLabels(const LabelGenerator& lgen, JKQTBasePlotter* parent=nullptr);
121 /** \brief class constructor, generates a JKQTPXYGraphLabels with a custom LabelGenerator functor */
123
124 /** \brief plots the graph to the plotter object specified as parent */
125 virtual void draw(JKQTPEnhancedPainter& painter) override;
126 /** \brief plots a key marker inside the specified rectangle \a rect */
127 virtual void drawKeyMarker(JKQTPEnhancedPainter& painter, const QRectF& rect) override;
128 /** \brief returns the color to be used for the key label */
129 virtual QColor getKeyLabelColor() const override;
130 /** \copydoc JKQTPPlotElement::getXMinMax() */
131 virtual bool getXMinMax(double& minx, double& maxx, double& smallestGreaterZero) override;
132 /** \copydoc JKQTPPlotElement::getYMinMax() */
133 virtual bool getYMinMax(double& miny, double& maxy, double& smallestGreaterZero) override;
134
135
136 /** \brief set color of line and symbol */
137 void setColor(QColor c, bool setTextColor_=true, bool setFrameColor_=true);
138 /** \brief sets a custom label geneator function of type LabelGenerator to use
139 *
140 * \see m_labelGenerator, setDefaultXLabelGenerator(), setDefaultYLabelGenerator(), setDefaultXYLabelGenerator()
141 */
142 void setCustomLabelGenerator(const LabelGenerator& labgen);
143 /** \brief sets the label geneator function to a default implementation, that prints the x-value only
144 *
145 * The generator uses m_xValueLabelFormat, m_xDefaultConverter, ...
146 * \see m_labelGenerator, setCustomLabelGenerator(), setDefaultXLabelGenerator(), setDefaultYLabelGenerator(), setDefaultXYLabelGenerator()
147 */
148 void setDefaultXLabelGenerator();
149 /** \brief sets the label geneator function to a default implementation, that prints the y-value only
150 *
151 * The generator uses m_yValueLabelFormat, m_yDefaultConverter, ...
152 * \see m_labelGenerator, setCustomLabelGenerator(), setDefaultXLabelGenerator(), setDefaultYLabelGenerator(), setDefaultXYLabelGenerator()
153 */
154 void setDefaultYLabelGenerator();
155 /** \brief sets the label geneator function to a default implementation, that prints the x- and y-value
156 *
157 * The generator uses m_xyValueLabelFormat, m_xDefaultConverter, m_yDefaultConverter, ...
158 * \see m_labelGenerator, setCustomLabelGenerator(), setDefaultXLabelGenerator(), setDefaultYLabelGenerator(), setDefaultXYLabelGenerator()
159 */
160 void setDefaultXYLabelGenerator();
161 protected:
162 /** \brief generates the label at position (\a x , \a y ) and at the given data \a index */
163 QString generateLabel(double x, double y, int index) const;
164 private:
165 /** \brief generate the label for the datapoint at location (\a x , \a y ) and being the \a index -th value in the columns */
166 LabelGenerator m_labelGenerator;
167};
168
169
170
171
172#endif // jkqtpgraphlabels_H
base class for 2D plotter classes (used by the plotter widget JKQTPlotter)
Definition jkqtpbaseplotter.h:394
this virtual class is the base for any type of coordinate axis, to be drawn by JKQTBasePlotter.
Definition jkqtpcoordinateaxes.h:181
This class manages data columns (with entries of type double ), used by JKQTPlotter/JKQTBasePlotter t...
Definition jkqtpdatastorage.h:282
this class extends the QPainter
Definition jkqtpenhancedpainter.h:33
This Mix-In class provides setter/getter methods, storage and other facilities for value labels in gr...
Definition jkqtpgraphlabelstylemixin.h:118
This virtual JKQTPGraph descendent may be used as base for all graphs that use at least two columns t...
Definition jkqtpgraphsbase.h:586
This graph plots a series of data labels. This can be used to add number-labels to e....
Definition jkqtpgraphlabels.h:103
std::function< QString(double, double, int)> LabelGenerator
type of a functor that generates a label
Definition jkqtpgraphlabels.h:113
LabelContentsDefaultType
Definition jkqtpgraphlabels.h:106
@ YValueLabel
generates a label with the y-coordinate only, calls setDefaultYLabelGenerator()
Definition jkqtpgraphlabels.h:108
@ XValueLabel
generates a label with the x-coordinate only, calls setDefaultXLabelGenerator()
Definition jkqtpgraphlabels.h:107
This Mix-In class provides setter/getter methods, and tools for x- and y-value label formatting (i....
Definition jkqtpgraphlabelstylemixin.h:316
plotter widget for scientific plots (uses JKQTBasePlotter to do the actual drawing)
Definition jkqtplotter.h:364
#define JKQTPLOTTER_LIB_EXPORT
Definition jkqtplotter_imexport.h:89