/** * Created by mm28969 on 10/24/16. * modified by mm17060 on 6/12/17 */ // declare let d3; import * as d3 from "d3"; import {convertValueToAttribute} from "../mmviz-common/index"; import {Layout, createViewModel} from "./layout"; import {LayoutScale} from "./scale"; export function pieLayoutCreator(colorScaleKey = "color", innerRadiusPercent=0) { let isGoodRadius = innerRadiusPercent >= 0 && innerRadiusPercent < 1; if (!isGoodRadius) { throw "Inner radius percent value expected to be between zero and one."; } // layout extender return function (dataModel, layoutScale: LayoutScale) { let center = layoutScale.drawArea.center, radius = layoutScale.drawArea.radius; // layouter return function () { let color, dataPieArray = d3.pie().sort(null).value(dataModel.valueMap)(dataModel.dataArray), viewModel: any = createViewModel(); viewModel.center = center; viewModel.radius = radius; viewModel.innerRadius = radius * innerRadiusPercent; viewModel.dataArray = dataPieArray; for (let dLayout of dataPieArray) { dLayout.key = dataModel.keyMap(dLayout.data); color = layoutScale.mapValue(colorScaleKey, dataModel, dLayout.data); dLayout.color = color; dLayout.category = color.key; dLayout.categoryAttr = "category-" + convertValueToAttribute(color.key); if (dataModel.detailsMap) { dLayout.details = dataModel.detailsMap(dLayout.data); } } return viewModel; } } }