|
|
|
@ -3,27 +3,36 @@ import { ChartwerkPod, VueChartwerkPodMixin, TimeFormat, AxisFormat } from '@cha
|
|
|
|
|
import { BarConfig } from './models/bar_options'; |
|
|
|
|
import { BarSeries } from './models/bar_series'; |
|
|
|
|
import { BarAnnotation } from './models/bar_annotation'; |
|
|
|
|
import { DataProcessor } from './models/data_processor'; |
|
|
|
|
import { DataProcessor, DataRow, DataItem } from './models/data_processor'; |
|
|
|
|
import { BarGroup } from './models/bar_group'; |
|
|
|
|
import { setBarScaleY, setBarScaleX } from './models/bar_state'; |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
BarSerie, BarOptions, DiscreteConfig, NonDiscreteConfig, |
|
|
|
|
BarPodType, SizeType, CallbackEvent, Color, Opacity, |
|
|
|
|
ColorFormatter, OpacityFormatter, |
|
|
|
|
Datapoint, ValueX, ValueY, |
|
|
|
|
} from './types'; |
|
|
|
|
|
|
|
|
|
import { BarSerie, BarOptions } from './types'; |
|
|
|
|
import { findClosest } from './utils'; |
|
|
|
|
|
|
|
|
|
import * as d3 from 'd3'; |
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> { |
|
|
|
|
barYScale: null | d3.ScaleLinear<number, number> = null; |
|
|
|
|
dataProcessor: DataProcessor; |
|
|
|
|
series: BarSeries; |
|
|
|
|
options: BarConfig; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(el: HTMLElement, _series: BarSerie[] = [], _options: BarOptions = {}) { |
|
|
|
|
super(el, _series, _options); |
|
|
|
|
this.series = new BarSeries(_series); |
|
|
|
|
this.options = new BarConfig(_options); |
|
|
|
|
this.dataProcessor = new DataProcessor(this.series.visibleSeries, this.options); |
|
|
|
|
|
|
|
|
|
setBarScaleY(this.state, this.dataProcessor.dataRows, this.options); |
|
|
|
|
setBarScaleX(this.state, this.dataProcessor.dataRows, this.options); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected renderMetrics(): void { |
|
|
|
@ -31,151 +40,33 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
|
|
|
|
|
this.renderNoDataPointsMessage(); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.setBarPodScales(); |
|
|
|
|
this.setSeriesDataForRendering(); |
|
|
|
|
this.renderSerie(this._seriesDataForRendring); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get isMatchingDisabled(): boolean { |
|
|
|
|
return this.options.barOptions.matching === false || this.seriesUniqKeys.length === 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setSeriesDataForRendering(): void { |
|
|
|
|
if(this.isMatchingDisabled) { |
|
|
|
|
this._seriesDataForRendring = this.getZippedDataForRender(this.series.visibleSeries); |
|
|
|
|
} else { |
|
|
|
|
const matchedSeries = this.seriesForMatching.map( |
|
|
|
|
(series: BarSerie[], idx: number) => this.getZippedDataForRender(series) |
|
|
|
|
this.dataProcessor.dataRows.forEach((dataRow: DataRow) => { |
|
|
|
|
const barGroup = new BarGroup( |
|
|
|
|
this.overlay, this.metricContainer, |
|
|
|
|
dataRow, this.options, this.state, |
|
|
|
|
{ width: this.width, height: this.height, zeroHorizon: this.state.yScale(0) }, |
|
|
|
|
this.dataProcessor.dataRows.length |
|
|
|
|
); |
|
|
|
|
this._seriesDataForRendring = this.mergeMacthedSeriesAndSort(matchedSeries); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setBarPodScales(): void { |
|
|
|
|
// TODO: its a hack to avoid infinite scale function recalling
|
|
|
|
|
// It should be fixed by implenting BarState
|
|
|
|
|
this.barYScale = this.getYScale(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
renderSerie(data: any): void { |
|
|
|
|
this.metricContainer.selectAll(`.rects-container`) |
|
|
|
|
.data(data) |
|
|
|
|
.enter().append('g') |
|
|
|
|
.attr('class', 'rects-container') |
|
|
|
|
.each((d: RowValues, rowIndex: number, nodes: any) => { |
|
|
|
|
const container = d3.select(nodes[rowIndex]); |
|
|
|
|
container.selectAll('rect') |
|
|
|
|
.data(d.values) |
|
|
|
|
.enter().append('rect') |
|
|
|
|
.style('fill', (val, idx) => this.getBarColor(d, val, idx, rowIndex)) |
|
|
|
|
.attr('opacity', () => this.getBarOpacity(d)) |
|
|
|
|
.attr('x', (val: number, idx: number) => { |
|
|
|
|
return this.getBarPositionX(d.key, idx); |
|
|
|
|
}) |
|
|
|
|
.attr('y', (val: number, idx: number) => { |
|
|
|
|
return this.getBarPositionY(val, idx, d.values); |
|
|
|
|
}) |
|
|
|
|
.attr('width', this.barWidth) |
|
|
|
|
.attr('height', (val: number) => this.getBarHeight(val)) |
|
|
|
|
.on('contextmenu', this.contextMenu.bind(this)) |
|
|
|
|
.on('mouseover', this.redirectEventToOverlay.bind(this)) |
|
|
|
|
.on('mousemove', this.redirectEventToOverlay.bind(this)) |
|
|
|
|
.on('mouseout', this.redirectEventToOverlay.bind(this)) |
|
|
|
|
.on('mousedown', () => { d3.event.stopPropagation(); }); |
|
|
|
|
|
|
|
|
|
// render bar annotations, its all hardcoded
|
|
|
|
|
if(_.isEmpty(this.options.barOptions.annotations)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
// find all series for single matchedKey
|
|
|
|
|
const series = _.filter(this.series.visibleSeries, serie => _.includes(d.serieTarget, serie.target)); |
|
|
|
|
const matchedKeys = _.map(series, serie => serie.matchedKey); // here matchedKeys should be equal
|
|
|
|
|
const key = matchedKeys[0]; |
|
|
|
|
const annotation = _.find(this.options.barOptions.annotations, a => a.key === key); |
|
|
|
|
if(!key || !annotation) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const annotationOptions = { |
|
|
|
|
size: this.barWidth, |
|
|
|
|
max: this.options.barOptions.maxAnnotationSize, |
|
|
|
|
min: this.options.barOptions.minAnnotationSize, |
|
|
|
|
color: annotation.color, |
|
|
|
|
}; |
|
|
|
|
new BarAnnotation(this.overlay, container, annotationOptions); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
redirectEventToOverlay(): void { |
|
|
|
|
this.overlay?.node().dispatchEvent(new MouseEvent(d3.event.type, d3.event)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getBarOpacity(rowValues: RowValues): number { |
|
|
|
|
if(this.options.barOptions.opacityFormatter === undefined) { |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
return this.options.barOptions.opacityFormatter(rowValues); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getBarColor(rowValues: RowValues, val: number, i: number, rowIndex: number): string { |
|
|
|
|
if(_.isFunction(rowValues.colors[i])) { |
|
|
|
|
return (rowValues.colors[i] as Function)({ rowData: rowValues, val, stackedIndex: i, rowIndex }); |
|
|
|
|
} |
|
|
|
|
return (rowValues.colors[i] as string); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mergeMacthedSeriesAndSort(matchedSeries: any[]) { |
|
|
|
|
// TODO: refactor
|
|
|
|
|
if(matchedSeries.length === 0) { |
|
|
|
|
throw new Error('Cant mergeMacthedSeriesAndSort'); |
|
|
|
|
} |
|
|
|
|
if(matchedSeries.length === 1) { |
|
|
|
|
return matchedSeries[0]; |
|
|
|
|
} |
|
|
|
|
let unionSeries = _.clone(matchedSeries[0]); |
|
|
|
|
for(let i = 1; i < matchedSeries.length; i++){ |
|
|
|
|
unionSeries = [...unionSeries, ...matchedSeries[i]]; |
|
|
|
|
} |
|
|
|
|
const sortedSeries = _.sortBy(unionSeries, ['key']); |
|
|
|
|
return sortedSeries; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get seriesUniqKeys(): string[] { |
|
|
|
|
if(!this.series.isSeriesAvailable) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
const keys = this.series.visibleSeries.map(serie => serie.matchedKey); |
|
|
|
|
const uniqKeys = _.uniq(keys); |
|
|
|
|
const filteredKeys = _.filter(uniqKeys, key => key !== undefined); |
|
|
|
|
return filteredKeys; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get seriesForMatching(): BarSerie[][] { |
|
|
|
|
if(this.seriesUniqKeys.length === 0) { |
|
|
|
|
return [this.series.visibleSeries]; |
|
|
|
|
} |
|
|
|
|
const seriesList = this.seriesUniqKeys.map(key => { |
|
|
|
|
const seriesWithKey = _.filter(this.series.visibleSeries, serie => serie.matchedKey === key); |
|
|
|
|
return seriesWithKey; |
|
|
|
|
this.renderAnnotationForGroup(barGroup, dataRow); |
|
|
|
|
}); |
|
|
|
|
return seriesList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getZippedDataForRender(series: BarSerie[]): RowValues[] { |
|
|
|
|
if(series.length === 0) { |
|
|
|
|
throw new Error('There is no visible series'); |
|
|
|
|
renderAnnotationForGroup(barGroup: BarGroup, dataRow: DataRow): void { |
|
|
|
|
if(this.options.barType === BarPodType.DISCRETE) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const target = _.first(dataRow.items).target; |
|
|
|
|
const serie = this.series.getSerieByTarget(target); |
|
|
|
|
if(!serie.annotation?.enable) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
const keysColumn = _.map(series[0].datapoints, row => row[0]); |
|
|
|
|
const valuesColumns = _.map(series, serie => _.map(serie.datapoints, row => row[1])); |
|
|
|
|
// @ts-ignore
|
|
|
|
|
const additionalValuesColumns = _.map(series, serie => _.map(serie.datapoints, row => row[2] !== undefined ? row[2] : null)); |
|
|
|
|
const zippedAdditionalValuesColumn = _.zip(...additionalValuesColumns); |
|
|
|
|
const zippedValuesColumn = _.zip(...valuesColumns); |
|
|
|
|
const colors = _.map(series, serie => serie.colorFormatter || serie.color); |
|
|
|
|
const tagrets = _.map(series, serie => serie.target); |
|
|
|
|
const zippedData = _.zip(keysColumn, zippedValuesColumn, zippedAdditionalValuesColumn, tagrets); |
|
|
|
|
const data = _.map(zippedData, row => { return { key: row[0], values: row[1], additionalValues: row[2], colors, serieTarget: tagrets } }); |
|
|
|
|
return data.filter(v => v.key !== undefined); |
|
|
|
|
const annotationOptions = { |
|
|
|
|
size: barGroup.getGroupWidth(), |
|
|
|
|
max: serie.annotation.size.max, |
|
|
|
|
min: serie.annotation.size.min, |
|
|
|
|
color: serie.annotation.color, |
|
|
|
|
}; |
|
|
|
|
new BarAnnotation(this.overlay, barGroup.getGroupContainer(), annotationOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public renderSharedCrosshair(values: { x?: number, y?: number }): void { |
|
|
|
@ -198,28 +89,29 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
|
|
|
|
|
this.crosshair.select('#crosshair-line-x') |
|
|
|
|
.attr('x1', eventX) |
|
|
|
|
.attr('x2', eventX); |
|
|
|
|
|
|
|
|
|
const series = this.getSeriesPointFromMousePosition(eventX); |
|
|
|
|
const dataRow = this.getDataRowFromMousePosition(eventX); |
|
|
|
|
|
|
|
|
|
this.options.callbackMouseMove({ |
|
|
|
|
x: d3.event.pageX, |
|
|
|
|
y: d3.event.pageY, |
|
|
|
|
time: this.state.xScale.invert(eventX), |
|
|
|
|
series, |
|
|
|
|
chartX: eventX, |
|
|
|
|
chartWidth: this.width |
|
|
|
|
position: { |
|
|
|
|
eventX: event[0], |
|
|
|
|
eventY: event[1], |
|
|
|
|
pageX: d3.event.pageX, |
|
|
|
|
pageY: d3.event.pageY, |
|
|
|
|
valueX: this.state.xScale.invert(event[0]), |
|
|
|
|
valueY: this.state.yScale.invert(event[1]), |
|
|
|
|
}, |
|
|
|
|
data: dataRow, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getSeriesPointFromMousePosition(eventX: number): any[] | undefined { |
|
|
|
|
getDataRowFromMousePosition(eventX: number): DataRow | undefined { |
|
|
|
|
if(!this.series.isSeriesAvailable) { |
|
|
|
|
return undefined; |
|
|
|
|
} |
|
|
|
|
const mousePoisitionKey = Math.ceil(this.state.xScale.invert(eventX)); |
|
|
|
|
const keys = _.map(this._seriesDataForRendring, el => el.key); |
|
|
|
|
const keys = _.map(this.dataProcessor.dataRows, (dataRow: DataRow) => dataRow.key); |
|
|
|
|
const idx = findClosest(keys, mousePoisitionKey); |
|
|
|
|
|
|
|
|
|
return this._seriesDataForRendring[idx]; |
|
|
|
|
return this.dataProcessor.dataRows[idx]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
onMouseOver(): void { |
|
|
|
@ -231,120 +123,6 @@ export class ChartwerkBarPod extends ChartwerkPod<BarSerie, BarOptions> {
|
|
|
|
|
this.options.callbackMouseOut(); |
|
|
|
|
this.crosshair.style('display', 'none'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
contextMenu(): void { |
|
|
|
|
// maybe it is not the best name, but i took it from d3.
|
|
|
|
|
d3.event.preventDefault(); // do not open browser's context menu.
|
|
|
|
|
|
|
|
|
|
const event = d3.mouse(this.chartContainer.node()); |
|
|
|
|
const eventX = event[0]; |
|
|
|
|
const series = this.getSeriesPointFromMousePosition(eventX); |
|
|
|
|
this.options.callbackContextMenu({ |
|
|
|
|
pageX: d3.event.pageX, |
|
|
|
|
pageY: d3.event.pageY, |
|
|
|
|
xVal: this.state.xScale.invert(eventX), |
|
|
|
|
series, |
|
|
|
|
chartX: eventX |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get barWidth(): number { |
|
|
|
|
// TODO: here we use first value + timeInterval as bar width. It is not a good idea
|
|
|
|
|
const xAxisStartValue = _.first(this.series.visibleSeries[0].datapoints)[0]; |
|
|
|
|
let width = this.state.xScale(xAxisStartValue + this.timeInterval) / 2; |
|
|
|
|
if(this.options.barOptions.barWidth !== undefined) { |
|
|
|
|
// barWidth now has axis-x dimension
|
|
|
|
|
width = this.state.xScale(this.state.getMinValueX() + this.options.barOptions.barWidth); |
|
|
|
|
} |
|
|
|
|
let rectColumns = this.series.visibleSeries.length; |
|
|
|
|
if(this.options.barOptions.stacked === true) { |
|
|
|
|
rectColumns = 1; |
|
|
|
|
} |
|
|
|
|
return this.updateBarWidthWithBorders(width / rectColumns); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateBarWidthWithBorders(width: number): number { |
|
|
|
|
let barWidth = width; |
|
|
|
|
if(this.options.barOptions.minBarWidth !== undefined) { |
|
|
|
|
barWidth = Math.max(barWidth, this.options.barOptions.minBarWidth); |
|
|
|
|
} |
|
|
|
|
if(this.options.barOptions.maxBarWidth !== undefined) { |
|
|
|
|
barWidth = Math.min(barWidth, this.options.barOptions.maxBarWidth); |
|
|
|
|
} |
|
|
|
|
return barWidth; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getBarHeight(value: number): number { |
|
|
|
|
// TODO: Property 'sign' does not exist on type 'Math'
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const height = Math.sign(value) * (this.barYScale(0) - this.barYScale(value)); |
|
|
|
|
return height; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getBarPositionX(key: number, idx: number): number { |
|
|
|
|
let xPosition: number = this.state.xScale(key); |
|
|
|
|
if(this.options.barOptions.stacked === false) { |
|
|
|
|
xPosition += idx * this.barWidth; |
|
|
|
|
} |
|
|
|
|
return xPosition; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getBarPositionY(val: number, idx: number, values: number[]): number { |
|
|
|
|
let yPosition: number = this.barYScale(Math.max(val, 0)); |
|
|
|
|
if(this.options.barOptions.stacked === true) { |
|
|
|
|
const previousBarsHeight = _.sum( |
|
|
|
|
_.map(_.range(idx), i => this.getBarHeight(values[i])) |
|
|
|
|
); |
|
|
|
|
yPosition -= previousBarsHeight; |
|
|
|
|
} |
|
|
|
|
return yPosition; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getYScale(): d3.ScaleLinear<number, number> { |
|
|
|
|
if( |
|
|
|
|
this.state.getMinValueY() === undefined || |
|
|
|
|
this.state.getMaxValueY() === undefined |
|
|
|
|
) { |
|
|
|
|
return d3.scaleLinear() |
|
|
|
|
.domain([1, 0]) |
|
|
|
|
.range([0, this.height]); |
|
|
|
|
} |
|
|
|
|
const yMaxValue = this.getYMaxValue(); |
|
|
|
|
return d3.scaleLinear() |
|
|
|
|
.domain([yMaxValue, Math.min(this.state.getMinValueY(), 0)]) |
|
|
|
|
.range([0, this.height]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getYMaxValue(): number | undefined { |
|
|
|
|
if(!this.series.isSeriesAvailable) { |
|
|
|
|
return undefined; |
|
|
|
|
} |
|
|
|
|
if(this.options.axis.y.range) { |
|
|
|
|
return _.max(this.options.axis.y.range); |
|
|
|
|
} |
|
|
|
|
let maxValue: number; |
|
|
|
|
if(this.options.barOptions.stacked === true) { |
|
|
|
|
if(this.options.barOptions.matching === true && this.seriesUniqKeys.length > 0) { |
|
|
|
|
const maxValues = this.seriesForMatching.map(series => { |
|
|
|
|
const valuesColumns = _.map(series, serie => _.map(serie.datapoints, row => row[1])); |
|
|
|
|
const zippedValuesColumn = _.zip(...valuesColumns); |
|
|
|
|
return maxValue = _.max(_.map(zippedValuesColumn, row => _.sum(row))); |
|
|
|
|
}); |
|
|
|
|
return _.max(maxValues); |
|
|
|
|
} else { |
|
|
|
|
const valuesColumns = _.map(this.series.visibleSeries, serie => _.map(serie.datapoints, row => row[1])); |
|
|
|
|
const zippedValuesColumn = _.zip(...valuesColumns); |
|
|
|
|
maxValue = _.max(_.map(zippedValuesColumn, row => _.sum(row))); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
maxValue = _.max( |
|
|
|
|
this.series.visibleSeries.map( |
|
|
|
|
serie => _.maxBy<number[]>(serie.datapoints, dp => dp[1])[0] |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
return Math.max(maxValue, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// it is used with Vue.component, e.g.: Vue.component('chartwerk-bar-chart', VueChartwerkBarChartObject)
|
|
|
|
@ -370,4 +148,10 @@ export const VueChartwerkBarChartObject = {
|
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export { BarSerie, BarOptions, TimeFormat, AxisFormat }; |
|
|
|
|
export { |
|
|
|
|
BarSerie, BarOptions, TimeFormat, AxisFormat, |
|
|
|
|
DiscreteConfig, NonDiscreteConfig, |
|
|
|
|
BarPodType, SizeType, CallbackEvent, Color, Opacity, |
|
|
|
|
ColorFormatter, OpacityFormatter, |
|
|
|
|
Datapoint, ValueX, ValueY, DataItem, DataRow, |
|
|
|
|
}; |
|
|
|
|