Skip to content

Commit eeb4c03

Browse files
[Explore vis] Fix histogram missing bucket (opensearch-project#11298)
fix: histogram x-axis be displayed as category, refactor histogram with custom series so the x-axis can be properly encoded as number Signed-off-by: Yulong Ruan <ruanyl@amazon.com> --------- Signed-off-by: Yulong Ruan <ruanyl@amazon.com> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
1 parent 77294ed commit eeb4c03

11 files changed

Lines changed: 714 additions & 127 deletions

File tree

changelogs/fragments/11298.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fix:
2+
- Histogram x-axis incorrectly have type 'category' ([#11298](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/11298))

src/plugins/explore/public/components/visualizations/bar/bar_chart_utils.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { BarChartStyle } from './bar_vis_config';
2222
import { getColors, DEFAULT_GREY } from '../theme/default_colors';
2323
import { BaseChartStyle, PipelineFn, EChartsSpecState } from '../utils/echarts_spec';
2424
import { getSeriesDisplayName } from '../utils/series';
25-
import { HistogramChartStyle } from '../histogram/histogram_vis_config';
2625

2726
export const inferTimeIntervals = (data: Array<Record<string, any>>, field: string | undefined) => {
2827
if (!data || data.length === 0 || !field) {
@@ -196,19 +195,11 @@ export const buildThresholdColorEncoding = (
196195
return colorLayer;
197196
};
198197

199-
type Options =
200-
| {
201-
kind: 'bar';
202-
styles: BarChartStyle;
203-
categoryField: string;
204-
seriesFields: string[] | ((headers?: string[]) => string[]);
205-
}
206-
| {
207-
kind: 'histogram';
208-
styles: HistogramChartStyle;
209-
categoryField: string;
210-
seriesFields: string[] | ((headers?: string[]) => string[]);
211-
};
198+
interface Options {
199+
styles: BarChartStyle;
200+
categoryField: string;
201+
seriesFields: string[] | ((headers?: string[]) => string[]);
202+
}
212203

213204
/**
214205
* Create bar series configuration
@@ -226,23 +217,17 @@ export const createBarSeries = <T extends BaseChartStyle>(options: Options): Pip
226217
seriesFields = seriesFields(transformedData[0]);
227218
}
228219

229-
const thresholdLines =
230-
options.kind === 'bar'
231-
? generateThresholdLines(options.styles?.thresholdOptions, options.styles?.switchAxes)
232-
: generateThresholdLines(options.styles?.thresholdOptions);
220+
const thresholdLines = generateThresholdLines(
221+
options.styles?.thresholdOptions,
222+
options.styles?.switchAxes
223+
);
233224

234-
const encodeX =
235-
options.kind === 'bar' ? adjustOppositeSymbol(options.styles?.switchAxes, 'x') : 'x';
236-
const encodeY =
237-
options.kind === 'bar' ? adjustOppositeSymbol(options.styles?.switchAxes, 'y') : 'y';
225+
const encodeX = adjustOppositeSymbol(options.styles?.switchAxes, 'x');
226+
const encodeY = adjustOppositeSymbol(options.styles?.switchAxes, 'y');
238227

239228
let barWidth: string | undefined;
240229
if (styles.barSizeMode === 'manual') {
241230
barWidth = `${(styles.barWidth || 0.7) * 100}%`;
242-
} else {
243-
if (options.kind === 'histogram') {
244-
barWidth = '99%';
245-
}
246231
}
247232

248233
const series = seriesFields.map((seriesField, index) => {
@@ -298,7 +283,6 @@ export const createFacetBarSeries = <T extends BaseChartStyle>({
298283
// facet into one chart
299284
if (!Array.isArray(transformedData?.[0]?.[0])) {
300285
const simpleBar = createBarSeries({
301-
kind: 'bar',
302286
styles,
303287
categoryField,
304288
seriesFields,

src/plugins/explore/public/components/visualizations/bar/to_expression.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const createBarSpec = (
101101
buildVisMap({
102102
seriesFields: (headers) => (headers ?? []).filter((h) => h !== categoryField),
103103
}),
104-
createBarSeries({ kind: 'bar', styles, categoryField, seriesFields: [valueField] }),
104+
createBarSeries({ styles, categoryField, seriesFields: [valueField] }),
105105
assembleSpec
106106
)({
107107
data: transformedData,
@@ -251,7 +251,6 @@ export const createTimeBarChart = (
251251
seriesFields: (headers) => (headers ?? []).filter((h) => h !== timeField),
252252
}),
253253
createBarSeries({
254-
kind: 'bar',
255254
styles,
256255
categoryField: timeField,
257256
seriesFields: [valueField],
@@ -421,7 +420,6 @@ export const createGroupedTimeBarChart = (
421420
seriesFields: (headers) => (headers ?? []).filter((h) => h !== timeField),
422421
}),
423422
createBarSeries({
424-
kind: 'bar',
425423
styles,
426424
categoryField: timeField,
427425
seriesFields(headers) {
@@ -787,7 +785,6 @@ export const createStackedBarSpec = (
787785
seriesFields: (headers) => (headers ?? []).filter((h) => h !== categoryField),
788786
}),
789787
createBarSeries({
790-
kind: 'bar',
791788
styles,
792789
categoryField,
793790
seriesFields(headers) {
@@ -930,7 +927,7 @@ export const createDoubleNumericalBarChart = (
930927
buildVisMap({
931928
seriesFields: (headers) => (headers ?? []).filter((h) => h !== categoryField),
932929
}),
933-
createBarSeries({ kind: 'bar', styles, categoryField, seriesFields: [valueField] }),
930+
createBarSeries({ styles, categoryField, seriesFields: [valueField] }),
934931
assembleSpec
935932
)({
936933
data: transformedData,

0 commit comments

Comments
 (0)