diff --git a/crates/nu-command/src/charting/histogram.rs b/crates/nu-command/src/charting/histogram.rs index 38bb734be5..b6161ec56c 100644 --- a/crates/nu-command/src/charting/histogram.rs +++ b/crates/nu-command/src/charting/histogram.rs @@ -72,7 +72,7 @@ impl Command for Histogram { let frequency_column_name = match frequency_name_arg { Some(inner) => { let span = inner.span; - if ["value", "count", "percentage"].contains(&inner.item.as_str()) { + if ["value", "count", "quantile", "percentage"].contains(&inner.item.as_str()) { return Err(ShellError::UnsupportedInput( "frequency-column-name can't be 'value', 'count' or 'percentage'" .to_string(), @@ -208,27 +208,29 @@ fn histogram_impl( let result_cols = vec![ value_column_name.to_string(), "count".to_string(), + "quantile".to_string(), "percentage".to_string(), freq_column.to_string(), ]; const MAX_FREQ_COUNT: f64 = 100.0; for (val, count) in counter.into_iter() { - let (percentage, freq) = { - let percentage = match calc_method { - PercentageCalcMethod::Normalize => (count as f64 / total_cnt as f64), - PercentageCalcMethod::Relative => (count as f64 / max_cnt as f64), - }; - ( - format!("{:.2}%", percentage * 100_f64), - "*".repeat((MAX_FREQ_COUNT * percentage).floor() as usize), - ) + let quantile = match calc_method { + PercentageCalcMethod::Normalize => (count as f64 / total_cnt as f64), + PercentageCalcMethod::Relative => (count as f64 / max_cnt as f64), }; + let percentage = format!("{:.2}%", quantile * 100_f64); + let freq = "*".repeat((MAX_FREQ_COUNT * quantile).floor() as usize); + result.push(Value::Record { cols: result_cols.clone(), vals: vec![ val.into_value(), Value::Int { val: count, span }, + Value::Float { + val: quantile, + span, + }, Value::String { val: percentage, span, diff --git a/crates/nu-command/tests/commands/histogram.rs b/crates/nu-command/tests/commands/histogram.rs index fc3722790e..95f453d715 100644 --- a/crates/nu-command/tests/commands/histogram.rs +++ b/crates/nu-command/tests/commands/histogram.rs @@ -126,7 +126,7 @@ fn count() { let actual = nu!( cwd: ".", pipeline( r#" - echo [[bit]; [1] [0] [0] [0] [0] [0] [0] [1]] + echo [[bit]; [1] [0] [0] [0] [0] [0] [0] [1] [1]] | histogram bit --percentage-type relative | sort-by count | reject frequency @@ -134,7 +134,7 @@ fn count() { "# )); - let bit_json = r#"[ { "bit": 1, "count": 2, "percentage": "33.33%" }, { "bit": 0, "count": 6, "percentage": "100.00%" }]"#; + let bit_json = r#"[ { "bit": 1, "count": 3, "quantile": 0.5, "percentage": "50.00%" }, { "bit": 0, "count": 6, "quantile": 1, "percentage": "100.00%" }]"#; assert_eq!(actual.out, bit_json); } @@ -152,7 +152,7 @@ fn count_with_normalize_percentage() { "# )); - let bit_json = r#"[ { "bit": 1, "count": 2, "percentage": "25.00%" }, { "bit": 0, "count": 6, "percentage": "75.00%" }]"#; + let bit_json = r#"[ { "bit": 1, "count": 2, "quantile": 0.25, "percentage": "25.00%" }, { "bit": 0, "count": 6, "quantile": 0.75, "percentage": "75.00%" }]"#; assert_eq!(actual.out, bit_json); }