in src/converter/dataConverter.ts [88:375]
public process(options: IConverterOptions): IDataRepresentation {
const {
dataView,
viewport,
hasSelection,
} = options;
const {
colorPalette,
createSelectionIdBuilder,
} = this.constructorOptions;
const settings: Settings = Settings.parse(dataView) as Settings;
let axisType: DataRepresentationTypeEnum = DataRepresentationTypeEnum.None;
const dataRepresentation: IDataRepresentation = {
groups: [],
isGrouped: false,
margin: {
bottom: 0,
left: 0,
right: 0,
top: 0,
},
series: [],
settings,
sortedSeries: [],
variance: [],
variances: [],
viewport,
x: {
axisType,
format: undefined,
max: undefined,
metadata: undefined,
min: undefined,
name: undefined,
scale: DataRepresentationScale.create(),
values: [],
},
};
if (!dataView
|| !dataView.categorical
|| !dataView.categorical.categories
|| !dataView.categorical.categories[0]
|| !dataView.categorical.categories[0].values
|| !dataView.categorical.values
|| !dataView.categorical.values.grouped
) {
return dataRepresentation;
}
const axisCategory: powerbi.DataViewCategoryColumn = dataView.categorical.categories[0];
dataRepresentation.x.metadata = axisCategory.source;
dataRepresentation.x.name = axisCategory.source.displayName;
settings.parse(dataView);
const axisCategoryType: powerbi.ValueTypeDescriptor = axisCategory.source.type;
if (axisCategoryType.text) {
settings.xAxis.type = AxisType.categorical;
}
if (axisCategoryType.text || settings.xAxis.type === AxisType.categorical) {
axisType = DataRepresentationTypeEnum.StringType;
} else if (axisCategoryType.dateTime) {
axisType = DataRepresentationTypeEnum.DateType;
} else if (axisCategoryType.integer || axisCategoryType.numeric) {
axisType = DataRepresentationTypeEnum.NumberType;
}
settings.parseSettings(viewport, axisType);
dataRepresentation.x.axisType = axisType;
let maxThickness: number = NaN;
let seriesColorIndex: number = 0;
if (dataView.categorical.values
&& dataView.categorical.values.source
&& dataView.categorical.values.source.displayName
&& settings.legend.titleText === undefined
) {
settings.legend.titleText = dataView.categorical.values.source.displayName;
}
const axisCategoryFormat: string = this.getFormatStringByColumn(axisCategory && axisCategory.source);
dataRepresentation.settings.dateValueKPI.setColumnFormat(axisCategoryFormat);
dataRepresentation.settings.tooltipLabel.setColumnFormat(axisCategoryFormat);
// Applies series formats
dataRepresentation.x.format = dataRepresentation.settings.dateValueKPI.getFormat();
dataView.categorical.values.grouped().forEach((columnGroup: powerbi.DataViewValueColumnGroup) => {
const groupedValues: powerbi.DataViewValueColumn[] = columnGroup.values;
const currentKPIColumn: powerbi.DataViewValueColumn[] = groupedValues
.filter((groupedValue: powerbi.DataViewValueColumn) => {
return groupedValue.source.roles[kpiColumn.name];
});
const kpiIndexes: number[] = (currentKPIColumn
&& currentKPIColumn[0]
&& currentKPIColumn[0].values as number[]
) || [];
groupedValues.forEach((groupedValue: powerbi.DataViewValueColumn) => {
const format: string = this.getFormatStringByColumn(groupedValue.source);
if (groupedValue.source.roles[kpiIndicatorValueColumn.name]) {
dataRepresentation.variances[0] = groupedValue.values as number[];
settings.kpiIndicatorValue.setColumnFormat(format);
settings.tooltipVariance.setColumnFormat(format);
}
if (groupedValue.source.roles[secondKPIIndicatorValueColumn.name]) {
dataRepresentation.variances[1] = groupedValue.values as number[];
settings.secondKPIIndicatorValue.setColumnFormat(format);
settings.secondTooltipVariance.setColumnFormat(format);
}
let groupIndex: number = -1;
if (groupedValue.source.roles[valuesColumn.name]) {
groupIndex = 0;
} else if (groupedValue.source.roles[secondaryValuesColumn.name]) {
groupIndex = 1;
}
if (groupIndex !== -1) {
if (!dataRepresentation.groups[groupIndex]) {
dataRepresentation.groups[groupIndex] = {
series: [],
y: {
format,
max: undefined,
min: undefined,
scale: DataRepresentationScale.create(),
},
};
}
const seriesGroup: IDataRepresentationSeriesGroup = dataRepresentation.groups[groupIndex];
const currentPoint: IDataRepresentationPointIndexed = {
color: undefined,
index: NaN,
kpiIndex: NaN,
x: null,
y: NaN,
};
const seriesSettings: SeriesSettings = (SeriesSettings.getDefault() as SeriesSettings);
for (const propertyName in seriesSettings) {
const descriptor: BaseDescriptor = seriesSettings[propertyName];
const defaultDescriptor: BaseDescriptor = settings[propertyName];
if (descriptor && descriptor.applyDefault && defaultDescriptor) {
descriptor.applyDefault(defaultDescriptor);
}
}
seriesSettings.parseObjects(columnGroup.objects || groupedValue.source.objects);
if (!seriesSettings.line.fillColor
&& colorPalette
&& colorPalette.getColor
) {
seriesSettings.line.fillColor = colorPalette.getColor(`${seriesColorIndex}`).value;
seriesColorIndex++;
}
const seriesY: IDataRepresentationAxisBase = {
max: undefined,
min: undefined,
};
const { points, gradientPoints } = this.getGradientPoints(
axisCategory.values as DataRepresentationAxisValueType[],
dataRepresentation,
seriesGroup,
groupedValue,
seriesY,
kpiIndexes,
seriesSettings,
settings,
currentPoint,
);
const isGrouped: boolean = columnGroup && !!columnGroup.identity;
if (isGrouped) {
dataRepresentation.isGrouped = isGrouped;
}
const identity: powerbi.extensibility.ISelectionId = createSelectionIdBuilder()
.withSeries(
dataView.categorical.values,
isGrouped
? columnGroup
: groupedValue,
)
.withMeasure(groupedValue.source.queryName)
.createSelectionId();
if (isNaN(maxThickness) || seriesSettings.line.thickness > maxThickness) {
maxThickness = seriesSettings.line.thickness;
}
const name: string = isGrouped && columnGroup.name
? `${columnGroup.name} - ${groupedValue.source.displayName}`
: groupedValue.source.displayName;
const groupName: string = isGrouped && columnGroup.name
? `${columnGroup.name}`
: undefined;
seriesGroup.series.push({
current: currentPoint,
domain: seriesY,
format,
gradientPoints,
groupName,
hasSelection,
identity,
name,
points,
selected: false,
settings: seriesSettings,
y: seriesGroup.y,
});
}
});
});
dataRepresentation.x.values = axisCategory.values as DataRepresentationAxisValueType[];
this.getXAxisScale(
dataRepresentation.x.scale,
dataRepresentation.x.min,
dataRepresentation.x.max,
dataRepresentation.x.axisType,
axisCategory.values,
);
dataRepresentation.margin = settings.dots.getMarginByThickness(
maxThickness,
dataRepresentation.margin,
);
const group: IDataRepresentationSeriesGroup = dataRepresentation.groups
&& (dataRepresentation.groups[0] || dataRepresentation.groups[1]);
if (dataRepresentation.variances[0]) {
dataRepresentation.variance.push(dataRepresentation.variances[0]
&& dataRepresentation.variances[0].length
&& dataRepresentation.variances[0].slice(-1)[0] || NaN);
} else {
dataRepresentation.variance.push(this.getVarianceByCurrentPointsOfSeries(
group && group.series[0],
group && group.series[1],
));
}
if (dataRepresentation.variances[1]) {
dataRepresentation.variance.push(dataRepresentation.variances[1]
&& dataRepresentation.variances[1].length
&& dataRepresentation.variances[1].slice(-1)[0] || NaN);
} else {
dataRepresentation.variance.push(this.getVarianceByCurrentPointsOfSeries(
group && group.series[0],
group && group.series[2],
));
}
return dataRepresentation;
}