/** * Created by mm28969 on 12/1/16. */ // declare let d3; import * as d3 from "d3"; import {convertValueToAttribute, OrientationEnum} from "../mmviz-common/index"; import {createViewModel} from "./layout"; import {LayoutScale} from "./scale"; export function barStackLayoutCreator(orientationEnum = OrientationEnum.VERTICAL, colorScaleKey = "color") { // layout extender return function(dataModel, layoutScale: LayoutScale) { let xDomain, yDomain; if (OrientationEnum.VERTICAL === orientationEnum) { xDomain = dataModel.categoryArray; yDomain = [dataModel.valueMin, dataModel.valueMax]; } else if (OrientationEnum.HORIZONTAL === orientationEnum) { xDomain = [dataModel.valueMin, dataModel.valueMax]; yDomain = dataModel.categoryArray; } layoutScale.extendDomainX(xDomain).extendDomainY(yDomain); function layoutDatum(stackDataItem, stackData){ let dLayout, dataKey, hasValue, category, x, x0, x1, y, y0, y1, width, height ; category = stackDataItem.data.category; dataKey = stackData.key + "_" + category; if (OrientationEnum.VERTICAL === orientationEnum) { y0 = layoutScale.yScale(stackDataItem[0]); y1 = layoutScale.yScale(stackDataItem[1]); hasValue = (!isNaN(y0) && !isNaN(y1)); if(hasValue){ x = layoutScale.xScale(category); width = layoutScale.xScale.bandwidth(); if(y1 > y0){ y = y0; height = y1 - y0; } else { y = y1; height = y0 - y1; } if(height >= 0){ dLayout = { key: dataKey, xKey: category, category: stackData.key, x: x, y: y, width: width, height: height }; } } } else if (OrientationEnum.HORIZONTAL === orientationEnum) { x0 = layoutScale.xScale(stackDataItem[0]); x1 = layoutScale.xScale(stackDataItem[1]); hasValue = (!isNaN(x0) && !isNaN(x1)); if(hasValue){ if(x1 > x0){ x = x0; width = x1 - x0; } else { x = x1; width = x0 - x1; } y = layoutScale.yScale(category); height = layoutScale.yScale.bandwidth(); if(width >= 0){ dLayout = { key: dataKey, yKey: category, category: stackData.key, x: x, y: y, width: width, height: height }; } } } return dLayout; } // layouter return function(){ let stackData, stackDataItem, key, color, dLayout, viewModel: any = createViewModel(); for (stackData of dataModel.dataArray) { key = stackData.key; color = layoutScale.mapValue(colorScaleKey, dataModel, stackData); for (stackDataItem of stackData) { if(stackDataItem.original){ dLayout = layoutDatum(stackDataItem, stackData); if(dLayout !== undefined){ dLayout.color = color; dLayout.categoryAttr = "category-" + convertValueToAttribute(color.key); if(dataModel.detailsMap){ dLayout.details = dataModel.detailsMap(stackDataItem.original); } viewModel.dataArray.push(dLayout); } } } } return viewModel; } } }