grafana/public/app/plugins/panel/piechart/suggestions.ts
sam boyer 33fd83f7e3
kindsys: Adapt to new PanelCfg schema interface (#65297)
* kindsys: Adapt to new PanelCfg schema interface

* building locally

* Remove Panel prefix in cue files

* Regenerate

* Update imports

* fixup! Merge branch 'remove-panel-prefix' into sdboyer/redundant-panelcfg-prefixes

* Fix formatting

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
Co-authored-by: Tania B <yalyna.ts@gmail.com>
2023-05-15 23:07:54 -04:00

82 lines
1.9 KiB
TypeScript

import { VisualizationSuggestionsBuilder } from '@grafana/data';
import { SuggestionName } from 'app/types/suggestions';
import { PieChartLabels, Options, PieChartType } from './panelcfg.gen';
export class PieChartSuggestionsSupplier {
getSuggestionsForData(builder: VisualizationSuggestionsBuilder) {
const list = builder.getListAppender<Options, {}>({
name: SuggestionName.PieChart,
pluginId: 'piechart',
options: {
reduceOptions: {
values: false,
calcs: ['lastNotNull'],
},
displayLabels: [PieChartLabels.Percent],
legend: {
placement: 'right',
values: [],
} as any,
},
cardOptions: {
previewModifier: (s) => {
// Hide labels in preview
s.options!.legend.showLegend = false;
},
},
});
const { dataSummary } = builder;
if (!dataSummary.hasNumberField) {
return;
}
if (dataSummary.hasStringField && dataSummary.frameCount === 1) {
// if many values this or single value PieChart is not a good option
if (dataSummary.rowCountTotal > 30 || dataSummary.rowCountTotal < 2) {
return;
}
list.append({
name: SuggestionName.PieChart,
options: {
reduceOptions: {
values: true,
calcs: [],
},
},
});
list.append({
name: SuggestionName.PieChartDonut,
options: {
reduceOptions: {
values: true,
calcs: [],
},
pieType: PieChartType.Donut,
},
});
return;
}
if (dataSummary.numberFieldCount > 30 || dataSummary.numberFieldCount < 2) {
return;
}
list.append({
name: SuggestionName.PieChart,
});
list.append({
name: SuggestionName.PieChartDonut,
options: {
pieType: PieChartType.Donut,
},
});
}
}