Skip to content

Commit

Permalink
Axis group by fill zero for bar charts
Browse files Browse the repository at this point in the history
.. when aggregating on a date axis we do not fill gaps
   e.g. when no rides for a period. This causes the bar
   chart to be somewhat misleading.
  • Loading branch information
liversedge committed Aug 18, 2021
1 parent 29485aa commit 7cc2219
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/Charts/UserChart.cpp
Expand Up @@ -216,8 +216,8 @@ UserChart::setRide(const RideItem *item)

// groupBy uses pass by reference and will update what is passed
// we update the ucd result as its used elsewhere
if (xgroupby > 0) groupBy(xgroupby, series.aggregateby, series.xseries, series.yseries);
if (ygroupby > 0) groupBy(ygroupby, series.aggregateby, series.yseries, series.xseries);
if (xgroupby > 0) groupBy(xgroupby, series.aggregateby, series.xseries, series.yseries, chartinfo.type == GC_CHART_BAR);
if (ygroupby > 0) groupBy(ygroupby, series.aggregateby, series.yseries, series.xseries, chartinfo.type == GC_CHART_BAR);

// this is a bit of a hack, but later processing references ucd->x and y, so we
// update them since they have been smoothed/aggregated.
Expand Down Expand Up @@ -381,7 +381,7 @@ UserChart::setRide(const RideItem *item)
// genericchart has intervened) they are converted to MSsincetheEpoch
// but at this point, we have days since Jan 1 1900
void
UserChart::groupBy(int groupby, int aggregateby, QVector<double> &xseries, QVector<double> &yseries)
UserChart::groupBy(int groupby, int aggregateby, QVector<double> &xseries, QVector<double> &yseries, bool fillzero)
{
// used to aggregate
double aggregate=0;
Expand Down Expand Up @@ -410,6 +410,17 @@ UserChart::groupBy(int groupby, int aggregateby, QVector<double> &xseries, QVect

newx << epoch.daysTo(dateForGroup(groupby, lastgroup));
newy << aggregate;

if (fillzero) {

// we fill gaps with zero for some chart types
// notably category based charts e.g. bar chart
for(int j=lastgroup+1; j<group;j++) {
newx << epoch.daysTo(dateForGroup(groupby, j));
newy << 0;
}
}

aggregate = 0;
lastgroup = group;
groupcount = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Charts/UserChart.h
Expand Up @@ -59,7 +59,7 @@ class UserChart : public QWidget {
void setGraphicsItem(QGraphicsItem *);

// groupBy day, week, month etc where x is dates and y is values
void groupBy(int groupby, int aggregateby, QVector<double> &xseries, QVector<double> &yseries);
void groupBy(int groupby, int aggregateby, QVector<double> &xseries, QVector<double> &yseries, bool fillzero);
long groupForDate(int groupby, QDate date);
QDate dateForGroup(int groupby, long);

Expand Down

0 comments on commit 7cc2219

Please sign in to comment.