(
container: Drawable, data: XYPlotData,
opts: XYPlotOptions = {})
| 58 | * @doc {heading: 'Charts', namespace: 'render'} |
| 59 | */ |
| 60 | export async function linechart( |
| 61 | container: Drawable, data: XYPlotData, |
| 62 | opts: XYPlotOptions = {}): Promise<void> { |
| 63 | // Nest data if necessary before further processing |
| 64 | const _data = Array.isArray(data.values[0]) ? data.values as Point2D[][] : |
| 65 | [data.values] as Point2D[][]; |
| 66 | const numValues = _data[0].length; |
| 67 | |
| 68 | // Create series names if none were passed in. |
| 69 | const _series: string[] = |
| 70 | data.series ? data.series : _data.map((_, i) => `Series ${i + 1}`); |
| 71 | assert( |
| 72 | _series.length === _data.length, |
| 73 | 'Must have an equal number of series labels as there are data series'); |
| 74 | |
| 75 | if (opts.seriesColors != null) { |
| 76 | assert( |
| 77 | opts.seriesColors.length === _data.length, |
| 78 | 'Must have an equal number of series colors as there are data series'); |
| 79 | } |
| 80 | |
| 81 | const vlChartValues: VLChartValue[] = []; |
| 82 | for (let valueIdx = 0; valueIdx < numValues; valueIdx++) { |
| 83 | const v: VLChartValue = { |
| 84 | x: valueIdx, |
| 85 | }; |
| 86 | |
| 87 | _series.forEach((seriesName, seriesIdx) => { |
| 88 | const seriesValue = _data[seriesIdx][valueIdx].y; |
| 89 | v[seriesName] = seriesValue; |
| 90 | v[`${seriesName}-name`] = seriesName; |
| 91 | }); |
| 92 | vlChartValues.push(v); |
| 93 | } |
| 94 | |
| 95 | const options = Object.assign({}, defaultOpts, opts); |
| 96 | |
| 97 | const yScale = (): {}|undefined => { |
| 98 | if (options.zoomToFit) { |
| 99 | return {'zero': false}; |
| 100 | } else if (options.yAxisDomain != null) { |
| 101 | return {'domain': options.yAxisDomain}; |
| 102 | } |
| 103 | return undefined; |
| 104 | }; |
| 105 | |
| 106 | const sharedEncoding = { |
| 107 | x: { |
| 108 | field: 'x', |
| 109 | type: options.xType, |
| 110 | title: options.xLabel, |
| 111 | }, |
| 112 | tooltip: [ |
| 113 | {field: 'x', type: 'quantitative'}, |
| 114 | ..._series.map(seriesName => { |
| 115 | return { |
| 116 | field: seriesName, |
| 117 | type: 'quantitative', |
no test coverage detected
searching dependent graphs…