(
container: Drawable, data: Array<{value: number}>|number[]|TypedArray,
opts: HistogramOpts = {})
| 48 | * @doc {heading: 'Charts', namespace: 'render'} |
| 49 | */ |
| 50 | export async function histogram( |
| 51 | container: Drawable, data: Array<{value: number}>|number[]|TypedArray, |
| 52 | opts: HistogramOpts = {}) { |
| 53 | const values = prepareData(data); |
| 54 | |
| 55 | const options = Object.assign({}, defaultOpts, opts); |
| 56 | |
| 57 | const embedOpts = { |
| 58 | actions: false, |
| 59 | mode: 'vega-lite' as Mode, |
| 60 | defaultStyle: false, |
| 61 | }; |
| 62 | |
| 63 | const histogramContainer = subSurface(container, 'histogram'); |
| 64 | if (opts.stats !== false) { |
| 65 | const statsContainer = subSurface(container, 'stats', { |
| 66 | prepend: true, |
| 67 | }); |
| 68 | let stats: HistogramStats; |
| 69 | |
| 70 | if (opts.stats) { |
| 71 | stats = opts.stats; |
| 72 | } else { |
| 73 | stats = arrayStats(values.map(x => x.value)); |
| 74 | } |
| 75 | renderStats(stats, statsContainer, {fontSize: options.fontSize}); |
| 76 | } |
| 77 | |
| 78 | // If there are no data values return early |
| 79 | if (values.length === 0) { |
| 80 | return undefined; |
| 81 | } |
| 82 | |
| 83 | // Now that we have rendered stats we need to remove any NaNs and Infinities |
| 84 | // before rendering the histogram |
| 85 | const filtered = []; |
| 86 | for (let i = 0; i < values.length; i++) { |
| 87 | const val = values[i].value; |
| 88 | if (val != null && isFinite(val)) { |
| 89 | filtered.push(values[i]); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | const histogramSpec: VisualizationSpec = { |
| 94 | |
| 95 | 'width': options.width || getDefaultWidth(histogramContainer), |
| 96 | 'height': options.height || getDefaultHeight(histogramContainer), |
| 97 | 'padding': 0, |
| 98 | 'autosize': { |
| 99 | 'type': 'fit', |
| 100 | 'contains': 'padding', |
| 101 | 'resize': true, |
| 102 | }, |
| 103 | 'data': {'values': filtered}, |
| 104 | 'mark': { |
| 105 | 'type': 'bar', |
| 106 | 'tooltip': true, |
| 107 | }, |
no test coverage detected
searching dependent graphs…