Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Sum of series in legend" option #7970

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/area.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ values.
*Scale Y-Axis to Data Bounds*:: The default Y axis bounds are zero and the maximum value returned in the data. Check
this box to change both upper and lower bounds to match the values returned in the data.
*Show Tooltip*:: Check this box to enable the display of tooltips.
*Show Sum of Series*:: Show a sum of all the values in each series next to it's label in the legend

[float]
[[area-viewing-detailed-information]]
Expand Down
1 change: 1 addition & 0 deletions docs/line.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ values.
*Show Tooltip*:: Check this box to enable the display of tooltips.
*Scale Y-Axis to Data Bounds*:: The default Y-axis bounds are zero and the maximum value returned in the data. Check
this box to change both upper and lower bounds to match the values returned in the data.
*Show Sum of Series*:: Show a sum of all the values in each series next to it's label in the legend

After changing options, click the *Apply changes* button to update your visualization, or the grey *Discard
changes* button to keep your visualization in its current state.
Expand Down
2 changes: 2 additions & 0 deletions docs/pie.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ Select the *Options* tab to change the following aspects of the table:

*Donut*:: Display the chart as a sliced ring instead of a sliced pie.
*Show Tooltip*:: Check this box to enable the display of tooltips.
*Show Sum of Series*:: Show a sum of all the values in each series next to it's label in the legend


After changing options, click the *Apply changes* button to update your visualization, or the grey *Discard
changes* button to keep your visualization in its current state.
Expand Down
2 changes: 2 additions & 0 deletions docs/vertbar.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Checkboxes are available to enable and disable the following behaviors:
*Show Tooltip*:: Check this box to enable the display of tooltips.
*Scale Y-Axis to Data Bounds*:: The default Y axis bounds are zero and the maximum value returned in the data. Check
this box to change both upper and lower bounds to match the values returned in the data.
*Show Sum of Series*:: Show a sum of all the values in each series next to it's label in the legend


[float]
[[vertbar-viewing-detailed-information]]
Expand Down
3 changes: 2 additions & 1 deletion src/core_plugins/kbn_vislib_vis_types/public/area.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export default function HistogramVisType(Private) {
},
scales: ['linear', 'log', 'square root'],
modes: ['stacked', 'overlap', 'percentage', 'wiggle', 'silhouette'],
editor: areaTemplate
editor: areaTemplate,
isLegendCountSupported: true
},
schemas: new Schemas([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@
Show Tooltip
</label>
</div>
<div class="vis-option-item" ng-show="showLegendCount()">
<label>
<input type="checkbox" ng-model="vis.params.addLegendCount">
Show Sum of Series
</label>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module.directive('vislibBasicOptions', function ($parse, $compile) {
return {
restrict: 'E',
template: vislibBasicOptionsTemplate,
replace: true
replace: true,
link: function ($scope, $el) {
$scope.showLegendCount = legendData => {
return $scope.vis.type.params.isLegendCountSupported;
};
}
};
});
3 changes: 2 additions & 1 deletion src/core_plugins/kbn_vislib_vis_types/public/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function HistogramVisType(Private) {
},
scales: ['linear', 'log', 'square root'],
modes: ['stacked', 'percentage', 'grouped'],
editor: histogramTemplate
editor: histogramTemplate,
isLegendCountSupported: true
},
schemas: new Schemas([
{
Expand Down
3 changes: 2 additions & 1 deletion src/core_plugins/kbn_vislib_vis_types/public/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default function HistogramVisType(Private) {
yAxis: {}
},
scales: ['linear', 'log', 'square root'],
editor: lineTemplate
editor: lineTemplate,
isLegendCountSupported: true
},
schemas: new Schemas([
{
Expand Down
3 changes: 2 additions & 1 deletion src/core_plugins/kbn_vislib_vis_types/public/pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default function HistogramVisType(Private) {
legendPosition: 'right',
isDonut: false
},
editor: pieTemplate
editor: pieTemplate,
isLegendCountSupported: true
},
responseConverter: false,
hierarchicalData: true,
Expand Down
29 changes: 28 additions & 1 deletion src/ui/public/agg_response/hierarchical/_build_split.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import _ from 'lodash';
import collectKeys from 'ui/agg_response/hierarchical/_collect_keys';
import AggResponseHierarchicalTransformAggregationProvider from 'ui/agg_response/hierarchical/_transform_aggregation';
export default function biuldSplitProvider(Private) {
let transformer = Private(AggResponseHierarchicalTransformAggregationProvider);
function showLabelCount(vis) {
if (!vis || !vis.params || !vis.params.addLegendCount) return false;
if (!vis.type.params.isLegendCountSupported) return false;
return true;
}
function getSize(name, children) {
let nextChildren = _.pluck(children, 'children');
let sizes = _.pluck(children, 'size');
let size = _.reduce(children, (sum, child) => {
if (child.children) sum += getSize(name, child.children);
if (child.name === name) sum += child.size;
return sum;
}, 0);
return size;
}
return function (agg, metric, aggData) {
// Ceate the split structure
// Create the split structure
let split = { label: '', slices: { children: [] } };

// Transform the aggData into splits
split.slices.children = transformer(agg, metric, aggData);

// Collect all the keys
split.names = collectKeys(split.slices.children);

split.labels = {};
_.map(split.names, name => {
const size = getSize(name, split.slices.children);
if (showLabelCount(agg.vis)) {
split.labels[name] = `${name} (${size})`;
} else {
split.labels[name] = name;
}
});

return split;
};
};
2 changes: 1 addition & 1 deletion src/ui/public/agg_response/hierarchical/_collect_keys.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from 'lodash';
export default function collectKeys(children) {
export default function collectKeys(children, showCounts = false) {
let nextChildren = _.pluck(children, 'children');
let keys = _.pluck(children, 'name');
return _(nextChildren)
Expand Down
26 changes: 25 additions & 1 deletion src/ui/public/agg_response/point_series/_get_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ export default function PointSeriesGetSeries(Private) {
let getPoint = Private(AggResponsePointSeriesGetPointProvider);
let addToSiri = Private(AggResponsePointSeriesAddToSiriProvider);

return function getSeries(rows, chart) {
function showLabelCount(vis) {
if (!vis || !vis.params || !vis.params.addLegendCount) return false;
if (!vis.type.params.isLegendCountSupported) return false;
return true;
}

function canCountMetric(siri) {
const metricType = siri.values[0].aggConfigResult.aggConfig._opts.type;
if (siri.values.length === 0) return false;
if (!siri.values[0].aggConfigResult.aggConfig) return false;
if (['count', 'cardinality', 'sum'].indexOf(metricType) === -1) return false;
return true;
}

return function getSeries(rows, chart, vis) {
let aspects = chart.aspects;
let multiY = _.isArray(aspects.y);
let yScale = chart.yScale;
Expand Down Expand Up @@ -50,6 +64,16 @@ export default function PointSeriesGetSeries(Private) {
});
}

series = _.map(series, siri => {
if (showLabelCount(vis) && canCountMetric(siri)) {
const count = siri.values.reduce((prev, curr) => prev + curr.y, 0);
siri.legendLabel = `${siri.label} (${count})`;
} else {
siri.legendLabel = siri.label;
}
return siri;
});

return series;
};
};
2 changes: 1 addition & 1 deletion src/ui/public/agg_response/point_series/point_series.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function PointSeriesProvider(Private) {
setupOrderedDateXAxis(vis, chart);
}

chart.series = getSeries(table.rows, chart);
chart.series = getSeries(table.rows, chart, vis);

delete chart.aspects;
return chart;
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ export default function DataFactory(Private) {
obj.slices = self._removeZeroSlices(obj.slices);

_.forEach(self.getNames(obj, columns), function (name) {
name.legendLabel = obj.labels[name.label];
names.push(name);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/visualize/visualize_legend.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
ng-click="showDetails = !showDetails"
ng-class="showDetails ? 'legend-value-full' : 'legend-value-truncate'"
class="legend-value-title"
tooltip="{{legendData.label}}"
tooltip="{{legendData.legendLabel}}"
>
<i class="fa fa-circle" ng-style="{color: getColor(legendData.label)}"></i> {{legendData.label}}
<i class="fa fa-circle" ng-style="{color: getColor(legendData.label)}"></i> {{legendData.legendLabel}}
</div>

<div ng-if="showDetails" class="legend-value-details">
Expand Down
4 changes: 1 addition & 3 deletions src/ui/public/visualize/visualize_legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ uiModules.get('kibana')
return a.concat(b);
}, []);
return _.compact(_.uniq(values, 'label'));
}


};
}
};
});