Skip to content

Commit

Permalink
User Chart on Overview - Part 2 of 3
Browse files Browse the repository at this point in the history
.. User charts can be scaled (lines, texts, markers, legend)

.. Plot area background color honours overview card color
   when we're on an overview.

.. when adding a user chart to an overview make it span a couple
   of columns and 3 times deeper than a metric tile.

NOTE:

   There are two bugs that need to be squashed individually
   and are related to user charts on overview:

   1. Mouse event handling seems to be broken for user
      charts in Overview.

   2. User chart axis colors are always black when the series
      are configured to use named colors.
  • Loading branch information
liversedge committed Aug 3, 2021
1 parent a0f77fb commit 1552732
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 59 deletions.
21 changes: 17 additions & 4 deletions src/Charts/GenericChart.cpp
Expand Up @@ -83,6 +83,8 @@ GenericChart::GenericChart(QWidget *parent, Context *context) : QWidget(parent),
// watch for color/themes change
connect(context, SIGNAL(configChanged(qint32)), this, SLOT(configChanged(qint32)));

// default bgcolor
bgcolor = GColor(CPLOTBACKGROUND);
configChanged(0);
}

Expand All @@ -91,9 +93,9 @@ GenericChart::configChanged(qint32)
{
setUpdatesEnabled(false);

setProperty("color", GColor(CPLOTBACKGROUND));
setProperty("color", bgcolor);
QPalette palette;
palette.setBrush(QPalette::Background, QBrush(GColor(CRIDEPLOTBACKGROUND)));
palette.setBrush(QPalette::Background, QBrush(bgcolor));
setPalette(palette); // propagates to children

// set style sheets
Expand All @@ -103,9 +105,18 @@ GenericChart::configChanged(qint32)
setUpdatesEnabled(true);
}

void
GenericChart::setBackgroundColor(QColor bgcolor)
{
if (bgcolor != this->bgcolor) {
this->bgcolor = bgcolor;
configChanged(0);
}
}

// set chart settings
bool
GenericChart::initialiseChart(QString title, int type, bool animate, int legendpos, bool stack, int orientation)
GenericChart::initialiseChart(QString title, int type, bool animate, int legendpos, bool stack, int orientation, double scale)
{
// Remember the settings, we use them for every new plot
this->title = title;
Expand All @@ -114,6 +125,7 @@ GenericChart::initialiseChart(QString title, int type, bool animate, int legendp
this->legendpos = legendpos;
this->stack = stack;
this->orientation = orientation;
this->scale = scale;

// store info as its passed to us
newSeries.clear();
Expand Down Expand Up @@ -370,7 +382,8 @@ GenericChart::finaliseChart()
for(int i=0; i<newPlots.count(); i++) {

// set initial parameters
newPlots[i].plot->initialiseChart(title, type, animate, legendpos);
newPlots[i].plot->setBackgroundColor(bgcolor);
newPlots[i].plot->initialiseChart(title, type, animate, legendpos, scale);

// add curves
QListIterator<GenericSeriesInfo>s(newPlots[i].series);
Expand Down
11 changes: 9 additions & 2 deletions src/Charts/GenericChart.h
Expand Up @@ -140,7 +140,7 @@ class GenericChartInfo {
public:

// default values, better here than spread across the codebase.
GenericChartInfo() : localinfo(NULL), type(GC_CHART_LINE), animate(false), legendpos(2), stack(false), orientation(Qt::Vertical) {}
GenericChartInfo() : localinfo(NULL), type(GC_CHART_LINE), animate(false), legendpos(2), stack(false), orientation(Qt::Vertical), scale(1.0) {}

// available for use (e.g. UserChartSettings)
void *localinfo;
Expand All @@ -153,6 +153,8 @@ class GenericChartInfo {
int legendpos;
bool stack; // stack series instead of on same chart
int orientation; // layout horizontal or vertical
double scale; // scale font sizes
QColor bgcolor; // background color of plots and legends
};


Expand Down Expand Up @@ -258,7 +260,7 @@ class GenericChart : public QWidget {
public slots:

// set chart settings
bool initialiseChart(QString title, int type, bool animate, int legendpos, bool stack, int orientation);
bool initialiseChart(QString title, int type, bool animate, int legendpos, bool stack, int orientation, double scale=1.0f);

// add a curve, associating an axis
bool addCurve(QString name, QVector<double> xseries, QVector<double> yseries, QVector<QString> fseries, QString xname, QString yname,
Expand All @@ -277,6 +279,9 @@ class GenericChart : public QWidget {
// annotations
bool annotateLabel(QString name, QStringList strings); // add a label alongside a series

// plot background
void setBackgroundColor(QColor);

// post processing clean up / add decorations / helpers etc
void finaliseChart();

Expand All @@ -296,6 +301,8 @@ class GenericChart : public QWidget {
int legendpos;
bool stack; // stack series instead of on same chart
int orientation; // layout horizontal or vertical
double scale;
QColor bgcolor;

// once series, axis etc have all been
// cached, we need to pre-process the data
Expand Down
29 changes: 17 additions & 12 deletions src/Charts/GenericLegend.cpp
Expand Up @@ -26,8 +26,8 @@

#include <limits>

GenericLegendItem::GenericLegendItem(Context *context, QWidget *parent, QString name, QColor color) :
QWidget(parent), context(context), name(name), color(color), datetime(false)
GenericLegendItem::GenericLegendItem(Context *context, GenericLegend *parent, QString name, QColor color) :
QWidget(parent), context(context), name(name), color(color), legend(parent), datetime(false)
{

value=0;
Expand All @@ -51,13 +51,15 @@ GenericLegendItem::GenericLegendItem(Context *context, QWidget *parent, QString
void
GenericLegendItem::configChanged(qint32)
{
static const double gl_margin = 5 * dpiXFactor;
static const double gl_spacer = 2 * dpiXFactor;
static const double gl_block = 7 * dpiXFactor;
static const double gl_linewidth = 1 * dpiXFactor;
static const double gl_margin = 5 * dpiXFactor*legend->plot()->scale();
static const double gl_spacer = 2 * dpiXFactor*legend->plot()->scale();
static const double gl_block = 7 * dpiXFactor*legend->plot()->scale();
static const double gl_linewidth = 1 * dpiXFactor*legend->plot()->scale();

// we just set geometry for now.
QFont f; // based on what just got set in prefs
// we need to scale...
f.setPointSizeF(f.pointSizeF() * legend->plot()->scale());
QFontMetricsF fm(f);

// so now the string we would display
Expand Down Expand Up @@ -117,7 +119,7 @@ GenericLegendItem::paintEvent(QPaintEvent *)
painter.save();

// fill background first
painter.setBrush(QBrush(GColor(CPLOTBACKGROUND)));
painter.setBrush(QBrush(legend->plot()->backgroundColor()));
painter.setPen(Qt::NoPen);
painter.drawRect(0,0,geometry().width()-1, geometry().height()-1);

Expand Down Expand Up @@ -148,9 +150,12 @@ GenericLegendItem::paintEvent(QPaintEvent *)
string = Utils::removeDP(string);

// set pen to series color for now
if (enabled) painter.setPen(GCColor::invertColor(GColor(CPLOTBACKGROUND))); // use invert - usually black or white
if (enabled) painter.setPen(GCColor::invertColor(legend->plot()->backgroundColor())); // use invert - usually black or white
else painter.setPen(Qt::gray);
painter.setFont(QFont());

QFont f;
f.setPointSizeF(f.pointSize() * legend->plot()->scale());
painter.setFont(f);

// series
painter.drawText(namerect, Qt::TextSingleLine, name);
Expand All @@ -159,15 +164,15 @@ GenericLegendItem::paintEvent(QPaintEvent *)

}

GenericLegend::GenericLegend(Context *context, GenericPlot *plot) : context(context), plot(plot)
GenericLegend::GenericLegend(Context *context, GenericPlot *plot) : context(context), plot_(plot)
{
layout = new QHBoxLayout(this);
layout->addStretch();

orientation=Qt::Horizontal;
xname="";
clickable=true;
setAutoFillBackground(true);
setAutoFillBackground(false);

connect(context, SIGNAL(configChanged(qint32)), this, SLOT(configChanged(qint32)));

Expand All @@ -179,7 +184,7 @@ void
GenericLegend::configChanged(qint32)
{
QPalette pal;
pal.setBrush(backgroundRole(), GColor(CPLOTBACKGROUND));
pal.setBrush(backgroundRole(), plot()->backgroundColor());
setPalette(pal);
repaint();
}
Expand Down
9 changes: 7 additions & 2 deletions src/Charts/GenericLegend.h
Expand Up @@ -43,12 +43,13 @@
#include "RCanvas.h"

class GenericPlot;
class GenericLegend;
class GenericLegendItem : public QWidget {

Q_OBJECT

public:
GenericLegendItem(Context *context, QWidget *parent, QString name, QColor color);
GenericLegendItem(Context *context, GenericLegend *parent, QString name, QColor color);

Q_SIGNALS:
void clicked(QString name, bool enabled); // someone clicked on a legend and enabled/disabled it
Expand All @@ -69,6 +70,7 @@ class GenericLegendItem : public QWidget {
Context *context;
QString name;
QColor color;
GenericLegend *legend;

bool hasvalue;
bool enabled;
Expand All @@ -94,6 +96,9 @@ class GenericLegend : public QWidget {
void addLabel(QLabel *label);
void removeSeries(QString name);
void removeAllSeries();
void setScale(double);

GenericPlot *plot() { return plot_; }

Q_SIGNALS:
void clicked(QString name, bool enabled); // someone clicked on a legend and enabled/disabled it
Expand All @@ -111,7 +116,7 @@ class GenericLegend : public QWidget {
// a label has a unique name, not directly tide to
// a series or axis value, it depends...
Context *context;
GenericPlot *plot;
GenericPlot *plot_;
QBoxLayout *layout;
QMap<QString,GenericLegendItem*> items;
QString xname;
Expand Down
46 changes: 27 additions & 19 deletions src/Charts/GenericPlot.cpp
Expand Up @@ -33,6 +33,7 @@ QString GenericPlot::gl_timeformat = QString("hh:mm:ss");

GenericPlot::GenericPlot(QWidget *parent, Context *context) : QWidget(parent), context(context)
{
setAutoFillBackground(true);

// set a minimum height
setMinimumHeight(gl_minheight *dpiXFactor);
Expand Down Expand Up @@ -201,18 +202,25 @@ GenericPlot::configChanged(qint32)
{
// tinted palette for headings etc
QPalette palette;
palette.setBrush(QPalette::Window, QBrush(GColor(CPLOTBACKGROUND)));
palette.setBrush(QPalette::Window, QBrush(bgcolor_));
palette.setColor(QPalette::WindowText, GColor(CPLOTMARKER));
palette.setColor(QPalette::Text, GColor(CPLOTMARKER));
palette.setColor(QPalette::Base, GCColor::alternateColor(GColor(CPLOTBACKGROUND)));
palette.setColor(QPalette::Base, GCColor::alternateColor(bgcolor_));
setPalette(palette);

// chart colors
chartview->setBackgroundBrush(QBrush(GColor(CPLOTBACKGROUND)));
qchart->setBackgroundBrush(QBrush(GColor(CPLOTBACKGROUND)));
chartview->setBackgroundBrush(QBrush(bgcolor_));
qchart->setBackgroundBrush(QBrush(bgcolor_));
qchart->setBackgroundPen(QPen(GColor(CPLOTMARKER)));
}

void
GenericPlot::setBackgroundColor(QColor bgcolor)
{
this->bgcolor_ = bgcolor;
configChanged(0);
}

void
GenericPlot::setSeriesVisible(QString name, bool visible)
{
Expand Down Expand Up @@ -380,7 +388,7 @@ GenericPlot::plotAreaChanged()
}

bool
GenericPlot::initialiseChart(QString title, int type, bool animate, int legpos)
GenericPlot::initialiseChart(QString title, int type, bool animate, int legpos, double scale)
{
// if we changed the type, all series must go
if (charttype != type) {
Expand All @@ -403,6 +411,7 @@ GenericPlot::initialiseChart(QString title, int type, bool animate, int legpos)
bottom=true;
barsets.clear();
havelegend.clear();
this->scale_ = scale;

// reset selections etc
selector->reset();
Expand Down Expand Up @@ -469,8 +478,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
QString dname = QString("d_%1").arg(name);

// standard colors are encoded 1,1,x - where x is the index into the colorset
QColor applyColor = QColor(color);
if (applyColor.red() == 1 && applyColor.green() == 1) applyColor = GColor(applyColor.blue());
QColor applyColor = RGBColor(QColor(color));

// if curve already exists, remove it
if (charttype==GC_CHART_LINE || charttype==GC_CHART_SCATTER || charttype==GC_CHART_PIE) {
Expand Down Expand Up @@ -518,7 +526,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
left = !left;

// yaxis color matches, but not done for xaxis above
yaxis->labelcolor = yaxis->axiscolor = QColor(applyColor);
yaxis->labelcolor = yaxis->axiscolor = applyColor;

// add to list
axisinfos.insert(yname, yaxis);
Expand All @@ -541,7 +549,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
add->setBrush(Qt::NoBrush);
QPen pen(applyColor);
pen.setStyle(static_cast<Qt::PenStyle>(linestyle));
pen.setWidth(size);
pen.setWidth(size * scale_);
add->setPen(pen);
add->setOpacity(double(opacity) / 100.0); // 0-100% to 0.0-1.0 values

Expand All @@ -564,7 +572,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
else {
if (datalabels) {
add->setPointLabelsVisible(true); // is false by default
add->setPointLabelsColor(QColor(applyColor));
add->setPointLabelsColor(applyColor);
add->setPointLabelsFormat("@yPoint");
}
// fill curve?
Expand Down Expand Up @@ -616,15 +624,15 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
// for our data points
if (linestyle == 0 && datalabels) {
dec->setPointLabelsVisible(true); // is false by default
dec->setPointLabelsColor(QColor(applyColor));
dec->setPointLabelsColor(applyColor);
dec->setPointLabelsFormat("@yPoint");
}

// aesthetics
if (symbol == 1) dec->setMarkerShape(QScatterSeries::MarkerShapeCircle);
else if (symbol == 2) dec->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
dec->setMarkerSize(size*6);
QColor col=QColor(applyColor);
dec->setMarkerSize(size*6*scale_);
QColor col=applyColor;
dec->setBrush(QBrush(col));
dec->setPen(Qt::NoPen);
dec->setOpacity(double(opacity) / 100.0); // 0-100% to 0.0-1.0 values
Expand Down Expand Up @@ -659,8 +667,8 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
if (symbol == 0) add->setVisible(false); // no marker !
else if (symbol == 1) add->setMarkerShape(QScatterSeries::MarkerShapeCircle);
else if (symbol == 2) add->setMarkerShape(QScatterSeries::MarkerShapeRectangle);
add->setMarkerSize(size);
QColor col=QColor(applyColor);
add->setMarkerSize(size*scale_);
QColor col=applyColor;
add->setBrush(QBrush(col));
add->setPen(Qt::NoPen);
add->setOpacity(double(opacity) / 100.0); // 0-100% to 0.0-1.0 values
Expand All @@ -681,7 +689,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse

if (datalabels) {
add->setPointLabelsVisible(true); // is false by default
add->setPointLabelsColor(QColor(applyColor));
add->setPointLabelsColor(applyColor);
add->setPointLabelsFormat("@yPoint");
}

Expand Down Expand Up @@ -715,7 +723,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
dec->setBrush(Qt::NoBrush);
QPen pen(applyColor);
pen.setStyle(static_cast<Qt::PenStyle>(linestyle));
pen.setWidth(size);
pen.setWidth(size*scale_);
dec->setPen(pen);
dec->setOpacity(double(opacity) / 100.0); // 0-100% to 0.0-1.0 values

Expand Down Expand Up @@ -745,7 +753,7 @@ GenericPlot::addCurve(QString name, QVector<double> xseries, QVector<double> yse
QBarSet *add= new QBarSet(name);

// aesthetics
add->setBrush(QBrush(QColor(applyColor)));
add->setBrush(QBrush(applyColor));
add->setPen(Qt::NoPen);

// data and min/max values
Expand Down Expand Up @@ -938,7 +946,7 @@ GenericPlot::finaliseChart()
// once we've done the basics, lets do the aesthetics
QFont stGiles; // hoho - Chart Font St. Giles ... ok you have to be British to get this joke
stGiles.fromString(appsettings->value(this, GC_FONT_CHARTLABELS, QFont().toString()).toString());
stGiles.setPointSize(appsettings->value(NULL, GC_FONT_CHARTLABELS_SIZE, 8).toInt());
stGiles.setPointSizeF(appsettings->value(NULL, GC_FONT_CHARTLABELS_SIZE, 8).toInt() * scale_);
add->setTitleFont(stGiles);
add->setLabelsFont(stGiles);

Expand Down

0 comments on commit 1552732

Please sign in to comment.