/** * Created by mm28969 on 10/24/16. */ // declare let d3; import * as d3 from "d3"; import {convertValueToAttribute} from "../mmviz-common/index"; import {createViewModel} from "./layout"; import {LayoutScale} from "./scale"; import {ScaleTypeEnum} from "./scale_container"; /** * Creates a layouter for mapping data to a scatter chart. * @param colorScaleKey - unique key to lookup the color scale. * @param areaScaleKey - unique key to lookup the area scale. * @param shapeScaleKey - unique key to lookup the shape scale. */ export function scatterLayoutCreator(colorScaleKey = "color", areaScaleKey = "area" , shapeScaleKey = "shape"){ // layout extender return function(dataModel, layoutScale: LayoutScale){ let x0, y0, xDomain = d3.extent(dataModel.dataArray, dataModel.xValueMap), yDomain = d3.extent(dataModel.dataArray, dataModel.yValueMap), areaDomain; layoutScale.extendDomainX(xDomain).extendDomainY(yDomain); if(dataModel.areaValueMap){ areaDomain = d3.extent(dataModel.dataArray, dataModel.areaValueMap); layoutScale.extendAreaScale(areaScaleKey, areaDomain); } x0 = d3.min(layoutScale.xScale.range()); y0 = d3.min(layoutScale.yScale.range()); function layoutDatum(d){ let key, x, y, width, color, shape, scaleContainer, dLayout: any = {}; color = layoutScale.mapValue(colorScaleKey, dataModel, d); dLayout.color = color; dLayout.category = color.key; dLayout.categoryAttr = "category-" + convertValueToAttribute(color.key); shape = layoutScale.mapValue(shapeScaleKey, dataModel, d); dLayout.shape = shape; dLayout.categoryShape = shape.key; dLayout.categoryShapeAttr = "category-shape-" + convertValueToAttribute(shape.key); dLayout.radius = layoutScale.mapAreaRadiusValue(areaScaleKey, dataModel, d); key = dataModel.keyMap(d); x = dataModel.xValueMap(d); y = dataModel.yValueMap(d); dLayout.key = key; dLayout.x = layoutScale.xScale(x); dLayout.x0 = x0; dLayout.key =key; scaleContainer = layoutScale.getScaleContainer("x"); if(scaleContainer.type === ScaleTypeEnum.BAND){ width = layoutScale.xScale.bandwidth(); dLayout.x = dLayout.x + (width * 0.5); dLayout.x0 = dLayout.x0 + (width * 0.5); } dLayout.y = layoutScale.yScale(y); dLayout.y0 = y0; if(dataModel.detailsMap){ dLayout.details = dataModel.detailsMap(d); } return dLayout; } // layouter return function(){ let d, dLayout, viewModel: any = createViewModel(); for (d of dataModel.dataArray) { dLayout = layoutDatum(d); viewModel.dataArray.push(dLayout); } return viewModel; } } }