You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "arulferiandevops (via GitHub)" <gi...@apache.org> on 2024/07/10 05:22:07 UTC
[I] Histogram Metrics other than COUNT [superset]
arulferiandevops opened a new issue, #29534:
URL: https://github.com/apache/superset/issues/29534
Hi all
I would like to ask related to HISTOGRAM CHART METRICS. "On the histogram chart, I can only find the COUNT metric, but I want to use the percentage metric. Is it possible to do that?
## Screenshot

## Description
we aim to create histograms using percentage metrics instead of count. As shown in the attached Histogram.png, there is no option to set the metric in the histogram chart. Is it possible to create?
Thanks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "rusackas (via GitHub)" <gi...@apache.org>.
rusackas commented on issue #29534:
URL: https://github.com/apache/superset/issues/29534#issuecomment-2221099533
CC @michael-s-molina as author of the newer (ECharts) histogram chart.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "arulferiandevops (via GitHub)" <gi...@apache.org>.
arulferiandevops commented on issue #29534:
URL: https://github.com/apache/superset/issues/29534#issuecomment-2219614886
Hi @dosu , I have already deployed Apache Superset using Helm values. To make the code changes you mentioned above (using the normalize property), where in the file/folder should I make the changes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "michael-s-molina (via GitHub)" <gi...@apache.org>.
michael-s-molina commented on issue #29534:
URL: https://github.com/apache/superset/issues/29534#issuecomment-2221157323
Hi @arulferiandevops. You can check the `normalize` option in the Customize tab.
<img width="1478" alt="Screenshot 2024-07-10 at 15 14 47" src="https://github.com/apache/superset/assets/70410625/890601a6-87e9-428b-8cae-e8de32d9e010">
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "michael-s-molina (via GitHub)" <gi...@apache.org>.
michael-s-molina closed issue #29534: Histogram Metrics other than COUNT
URL: https://github.com/apache/superset/issues/29534
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
dosubot[bot] commented on issue #29534:
URL: https://github.com/apache/superset/issues/29534#issuecomment-2219592357
<!-- Greeting -->
Hey @arulferiandevops! I'm here to help you with your issue. Let's see what we can do!
<!-- Answer -->
Yes, it is possible to create histograms with percentage metrics instead of count in Apache Superset. This can be achieved by using the `normalize` property in the `Histogram` component, which allows you to display the histogram with normalized (percentage) values.
Here is an example of how to configure the `Histogram` component to display percentage metrics:
```ts
import { BarSeriesOption, EChartsOption } from 'echarts';
import { CallbackDataParams } from 'echarts/types/src/util/types';
import { CategoricalColorNamespace, NumberFormats, getNumberFormatter, getPercentFormatter, tooltipHtml } from '@superset-ui/core';
import { HistogramChartProps, HistogramTransformedProps } from './types';
import { LegendOrientation, LegendType, Refs } from '../types';
import { defaultGrid, defaultYAxis } from '../defaults';
import { getLegendProps } from '../utils/series';
import { getDefaultTooltip } from '../utils/tooltip';
export default function transformProps(
chartProps: HistogramChartProps,
): HistogramTransformedProps {
const refs: Refs = {};
let focusedSeries: number | undefined;
const {
formData,
height,
hooks,
legendState = {},
queriesData,
theme,
width,
} = chartProps;
const { onLegendStateChanged } = hooks;
const {
colorScheme,
column,
groupby = [],
normalize,
showLegend,
showValue,
sliceId,
xAxisTitle,
yAxisTitle,
} = formData;
const { data } = queriesData[0];
const colorFn = CategoricalColorNamespace.getScale(colorScheme);
const formatter = getNumberFormatter(
normalize ? NumberFormats.FLOAT_2_POINT : NumberFormats.INTEGER,
);
const percentFormatter = getPercentFormatter(NumberFormats.PERCENT_2_POINT);
const groupbySet = new Set(groupby);
const xAxisData: string[] = Object.keys(data[0]).filter(
key => !groupbySet.has(key),
);
const barSeries: BarSeriesOption[] = data.map(datum => {
const seriesName =
groupby.length > 0
? groupby.map(key => datum[getColumnLabel(key)]).join(', ')
: getColumnLabel(column);
const seriesData = Object.keys(datum)
.filter(key => groupbySet.has(key) === false)
.map(key => datum[key] as number);
return {
name: seriesName,
type: 'bar',
data: seriesData,
itemStyle: {
color: colorFn(seriesName, sliceId),
},
label: {
show: showValue,
position: 'top',
formatter: params => {
const { value } = params;
return formatter.format(value as number);
},
},
};
});
const legendOptions = barSeries.map(series => series.name as string);
if (isEmpty(legendState)) {
legendOptions.forEach(legend => {
legendState[legend] = true;
});
}
const tooltipFormatter = (params: CallbackDataParams[]) => {
const title = params[0].name;
const rows = params.map(param => {
const { marker, seriesName, value } = param;
return [`${marker}${seriesName}`, formatter.format(value as number)];
});
if (groupby.length > 0) {
const total = params.reduce(
(acc, param) => acc + (param.value as number),
0,
);
if (!normalize) {
rows.forEach((row, i) =>
row.push(
percentFormatter.format((params[i].value as number) / (total || 1)),
),
);
}
const totalRow = ['Total', formatter.format(total)];
if (!normalize) {
totalRow.push(percentFormatter.format(1));
}
rows.push(totalRow);
}
return tooltipHtml(rows, title, focusedSeries);
};
const onFocusedSeries = (index?: number | undefined) => {
focusedSeries = index;
};
const echartOptions: EChartsOption = {
grid: {
...defaultGrid,
left: '5%',
right: '5%',
top: '10%',
bottom: '10%',
},
xAxis: {
data: xAxisData,
name: xAxisTitle,
nameGap: 35,
type: 'category',
nameLocation: 'middle',
},
yAxis: {
...defaultYAxis,
name: yAxisTitle,
nameGap: normalize ? 55 : 40,
type: 'value',
nameLocation: 'middle',
axisLabel: {
formatter: (value: number) => formatter.format(value),
},
},
series: barSeries,
legend: {
...getLegendProps(
LegendType.Scroll,
LegendOrientation.Top,
showLegend,
theme,
false,
legendState,
),
data: legendOptions,
},
tooltip: {
...getDefaultTooltip(refs),
trigger: 'axis',
formatter: tooltipFormatter,
},
};
return {
refs,
formData,
width,
height,
echartOptions,
onFocusedSeries,
onLegendStateChanged,
};
}
```
In this example, setting the `normalize` property to `true` will display the histogram with percentage metrics instead of counts <sup>[[1]](https://github.com/apache/superset/blob/master/superset-frontend/plugins/plugin-chart-table/src/controlPanel.tsx)</sup><sup>[[2]](https://github.com/apache/superset/blob/master/superset-frontend/plugins/legacy-plugin-chart-histogram/src/Histogram.jsx)</sup><sup>[[3]](https://github.com/apache/superset/blob/master/superset-frontend/plugins/plugin-chart-echarts/src/Histogram/transformProps.ts)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org
Re: [I] Histogram Metrics other than COUNT [superset]
Posted by "dosubot[bot] (via GitHub)" <gi...@apache.org>.
dosubot[bot] commented on issue #29534:
URL: https://github.com/apache/superset/issues/29534#issuecomment-2219621612
<!-- Answer -->
To use the `normalize` property to display histograms with percentage metrics instead of counts in your deployed Apache Superset, you should make changes in three places:
1. **Frontend Configuration**: Update the file `superset-frontend/plugins/plugin-chart-echarts/src/Histogram/controlPanel.tsx`.
The relevant section of the file where the `normalize` property is defined is:
```tsx
{
name: 'normalize',
config: {
type: 'CheckboxControl',
label: t('Normalize'),
description: t(`
The normalize option transforms the histogram values into proportions or
probabilities by dividing each bin's count by the total count of data points.
This normalization process ensures that the resulting values sum up to 1,
enabling a relative comparison of the data's distribution and providing a
clearer understanding of the proportion of data points within each bin.`),
default: false,
},
}
```
Ensure that this configuration is included in the `controlPanelSections` array within the `config` object in the `controlPanel.tsx` file <sup>[[1]](https://github.com/apache/superset/blob/master/superset-frontend/plugins/plugin-chart-echarts/src/Histogram/controlPanel.tsx)</sup>.
2. **Frontend Transformation**: Update the file `superset-frontend/plugins/plugin-chart-echarts/src/Histogram/transformProps.ts`.
Ensure that the `normalize` parameter is handled correctly in the `transformProps` function:
```ts
export default function transformProps(
chartProps: HistogramChartProps,
): HistogramTransformedProps {
// ... existing code ...
const {
formData,
height,
hooks,
legendState = {},
queriesData,
theme,
width,
} = chartProps;
const { onLegendStateChanged } = hooks;
const {
colorScheme,
column,
groupby = [],
normalize,
showLegend,
showValue,
sliceId,
xAxisTitle,
yAxisTitle,
} = formData;
const { data } = queriesData[0];
const colorFn = CategoricalColorNamespace.getScale(colorScheme);
const formatter = getNumberFormatter(
normalize ? NumberFormats.FLOAT_2_POINT : NumberFormats.INTEGER,
);
const percentFormatter = getPercentFormatter(NumberFormats.PERCENT_2_POINT);
const groupbySet = new Set(groupby);
const xAxisData: string[] = Object.keys(data[0]).filter(
key => !groupbySet.has(key),
);
const barSeries: BarSeriesOption[] = data.map(datum => {
const seriesName =
groupby.length > 0
? groupby.map(key => datum[getColumnLabel(key)]).join(', ')
: getColumnLabel(column);
const seriesData = Object.keys(datum)
.filter(key => groupbySet.has(key) === false)
.map(key => datum[key] as number);
return {
name: seriesName,
type: 'bar',
data: seriesData,
itemStyle: {
color: colorFn(seriesName, sliceId),
},
label: {
show: showValue,
position: 'top',
formatter: params => {
const { value } = params;
return formatter.format(value as number);
},
},
};
});
const legendOptions = barSeries.map(series => series.name as string);
if (isEmpty(legendState)) {
legendOptions.forEach(legend => {
legendState[legend] = true;
});
}
const tooltipFormatter = (params: CallbackDataParams[]) => {
const title = params[0].name;
const rows = params.map(param => {
const { marker, seriesName, value } = param;
return [`${marker}${seriesName}`, formatter.format(value as number)];
});
if (groupby.length > 0) {
const total = params.reduce(
(acc, param) => acc + (param.value as number),
0,
);
if (!normalize) {
rows.forEach((row, i) =>
row.push(
percentFormatter.format((params[i].value as number) / (total || 1)),
),
);
}
const totalRow = ['Total', formatter.format(total)];
if (!normalize) {
totalRow.push(percentFormatter.format(1));
}
rows.push(totalRow);
}
return tooltipHtml(rows, title, focusedSeries);
};
const onFocusedSeries = (index?: number | undefined) => {
focusedSeries = index;
};
const echartOptions: EChartsOption = {
grid: {
...defaultGrid,
left: '5%',
right: '5%',
top: '10%',
bottom: '10%',
},
xAxis: {
data: xAxisData,
name: xAxisTitle,
nameGap: 35,
type: 'category',
nameLocation: 'middle',
},
yAxis: {
...defaultYAxis,
name: yAxisTitle,
nameGap: normalize ? 55 : 40,
type: 'value',
nameLocation: 'middle',
axisLabel: {
formatter: (value: number) => formatter.format(value),
},
},
series: barSeries,
legend: {
...getLegendProps(
LegendType.Scroll,
LegendOrientation.Top,
showLegend,
theme,
false,
legendState,
),
data: legendOptions,
},
tooltip: {
...getDefaultTooltip(refs),
trigger: 'axis',
formatter: tooltipFormatter,
},
};
return {
refs,
formData,
width,
height,
echartOptions,
onFocusedSeries,
onLegendStateChanged,
};
}
```
Ensure that the `normalize` parameter is correctly handled in the `transformProps` function <sup>[[2]](https://github.com/apache/superset/blob/master/superset-frontend/plugins/plugin-chart-echarts/src/Histogram/transformProps.ts)</sup>.
3. **Backend Processing**: Update the file `superset/utils/pandas_postprocessing/histogram.py`.
Ensure that the `normalize` parameter is handled correctly in the `histogram` function:
```py
def histogram(
df: DataFrame,
column: str,
groupby: list[str] | None,
bins: int = 5,
cumulative: bool = False,
normalize: bool = False,
) -> DataFrame:
# ... existing code ...
if normalize:
histogram_df = histogram_df / histogram_df.values.sum()
# reorder the columns to have the groupby columns first
histogram_df = histogram_df.reset_index().loc[:, groupby + bin_edges_str]
return histogram_df
```
Ensure that the `normalize` parameter is correctly handled in the `histogram` function <sup>[[3]](https://github.com/apache/superset/blob/master/superset/utils/pandas_postprocessing/histogram.py)</sup>.
By making these changes in the frontend configuration, frontend transformation, and backend processing, you will enable the `normalize` property to display histograms with percentage metrics instead of counts in Apache Superset.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org