@swimlane/ngx-charts
Version:
Declarative Charting Framework for Angular2 and beyond!
997 lines (986 loc) • 588 kB
JavaScript
/**
* ngx-charts v"3.0.4" (https://github.com/swimlane/ngx-charts)
* Copyright 2016
* Licensed under MIT
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("@angular/common"), require("@angular/core"), require("@angular/platform-browser"), require("d3-array"), require("d3-brush"), require("d3-color"), require("d3-force"), require("d3-format"), require("d3-hierarchy"), require("d3-interpolate"), require("d3-scale"), require("d3-selection"), require("d3-shape"), require("moment"), require("rxjs/Rx"));
else if(typeof define === 'function' && define.amd)
define("ngx-charts", ["@angular/common", "@angular/core", "@angular/platform-browser", "d3-array", "d3-brush", "d3-color", "d3-force", "d3-format", "d3-hierarchy", "d3-interpolate", "d3-scale", "d3-selection", "d3-shape", "moment", "rxjs/Rx"], factory);
else if(typeof exports === 'object')
exports["ngx-charts"] = factory(require("@angular/common"), require("@angular/core"), require("@angular/platform-browser"), require("d3-array"), require("d3-brush"), require("d3-color"), require("d3-force"), require("d3-format"), require("d3-hierarchy"), require("d3-interpolate"), require("d3-scale"), require("d3-selection"), require("d3-shape"), require("moment"), require("rxjs/Rx"));
else
root["ngx-charts"] = factory(root["@angular/common"], root["@angular/core"], root["@angular/platform-browser"], root["d3-array"], root["d3-brush"], root["d3-color"], root["d3-force"], root["d3-format"], root["d3-hierarchy"], root["d3-interpolate"], root["d3-scale"], root["d3-selection"], root["d3-shape"], root["moment"], root["rxjs/Rx"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_0__, __WEBPACK_EXTERNAL_MODULE_3__, __WEBPACK_EXTERNAL_MODULE_4__, __WEBPACK_EXTERNAL_MODULE_5__, __WEBPACK_EXTERNAL_MODULE_6__, __WEBPACK_EXTERNAL_MODULE_7__, __WEBPACK_EXTERNAL_MODULE_8__, __WEBPACK_EXTERNAL_MODULE_9__, __WEBPACK_EXTERNAL_MODULE_10__, __WEBPACK_EXTERNAL_MODULE_11__, __WEBPACK_EXTERNAL_MODULE_12__, __WEBPACK_EXTERNAL_MODULE_13__, __WEBPACK_EXTERNAL_MODULE_1__, __WEBPACK_EXTERNAL_MODULE_14__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmory imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmory exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/index.ts");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./src/area-chart/area-chart-normalized.component.ts":
/***/ function(module, exports, __webpack_require__) {
"use strict";
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = __webpack_require__(0);
var d3_1 = __webpack_require__("./src/d3.ts");
var view_dimensions_helper_1 = __webpack_require__("./src/common/view-dimensions.helper.ts");
var color_helper_1 = __webpack_require__("./src/common/color.helper.ts");
var base_chart_component_1 = __webpack_require__("./src/common/base-chart.component.ts");
var moment = __webpack_require__(1);
var id_1 = __webpack_require__("./src/utils/id.ts");
var AreaChartNormalizedComponent = (function (_super) {
__extends(AreaChartNormalizedComponent, _super);
function AreaChartNormalizedComponent() {
_super.apply(this, arguments);
this.legend = false;
this.showGridLines = true;
this.curve = d3_1.default.shape.curveLinear;
this.activeEntries = [];
this.activate = new core_1.EventEmitter();
this.deactivate = new core_1.EventEmitter();
this.margin = [10, 20, 10, 20];
this.xAxisHeight = 0;
this.yAxisWidth = 0;
this.timelineHeight = 50;
this.timelinePadding = 10;
}
AreaChartNormalizedComponent.prototype.update = function () {
var _this = this;
_super.prototype.update.call(this);
this.zone.run(function () {
_this.dims = view_dimensions_helper_1.calculateViewDimensions({
width: _this.width,
height: _this.height,
margins: _this.margin,
showXAxis: _this.xAxis,
showYAxis: _this.yAxis,
xAxisHeight: _this.xAxisHeight,
yAxisWidth: _this.yAxisWidth,
showXLabel: _this.showXAxisLabel,
showYLabel: _this.showYAxisLabel,
showLegend: _this.legend,
legendType: _this.schemeType
});
if (_this.timeline) {
_this.dims.height -= (_this.timelineHeight + _this.margin[2] + _this.timelinePadding);
}
_this.xDomain = _this.getXDomain();
if (_this.filteredDomain) {
_this.xDomain = _this.filteredDomain;
}
_this.yDomain = _this.getYDomain();
_this.seriesDomain = _this.getSeriesDomain();
_this.xScale = _this.getXScale(_this.xDomain, _this.dims.width);
_this.yScale = _this.getYScale(_this.yDomain, _this.dims.height);
var _loop_1 = function(i) {
var val = _this.xSet[i];
var d0 = 0;
var total = 0;
for (var _i = 0, _a = _this.results; _i < _a.length; _i++) {
var group = _a[_i];
var d = group.series.find(function (item) {
var a = item.name;
var b = val;
if (_this.scaleType === 'time') {
a = a.valueOf();
b = b.valueOf();
}
return a === b;
});
if (d) {
total += d.value;
}
}
for (var _b = 0, _c = _this.results; _b < _c.length; _b++) {
var group = _c[_b];
var d = group.series.find(function (item) {
var a = item.name;
var b = val;
if (_this.scaleType === 'time') {
a = a.valueOf();
b = b.valueOf();
}
return a === b;
});
if (d) {
d.d0 = d0;
d.d1 = d0 + d.value;
d0 += d.value;
}
else {
d = {
name: val,
value: 0,
d0: d0,
d1: d0
};
group.series.push(d);
}
if (total > 0) {
d.d0 = (d.d0 * 100) / total;
d.d1 = (d.d1 * 100) / total;
}
else {
d.d0 = 0;
d.d1 = 0;
}
}
};
for (var i = 0; i < _this.xSet.length; i++) {
_loop_1(i);
}
_this.updateTimeline();
_this.setColors();
_this.legendOptions = _this.getLegendOptions();
_this.transform = "translate(" + _this.dims.xOffset + " , " + _this.margin[0] + ")";
var pageUrl = window.location.href;
_this.clipPathId = 'clip' + id_1.id().toString();
_this.clipPath = "url(" + pageUrl + "#" + _this.clipPathId + ")";
});
};
AreaChartNormalizedComponent.prototype.updateTimeline = function () {
if (this.timeline) {
this.timelineWidth = this.width;
if (this.legend) {
this.timelineWidth = this.dims.width;
}
this.timelineXDomain = this.getXDomain();
this.timelineXScale = this.getXScale(this.timelineXDomain, this.timelineWidth);
this.timelineYScale = this.getYScale(this.yDomain, this.timelineHeight);
this.timelineTransform = "translate(" + this.dims.xOffset + ", " + -this.margin[2] + ")";
}
};
AreaChartNormalizedComponent.prototype.getXDomain = function () {
var values = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];
if (!values.includes(d.name)) {
values.push(d.name);
}
}
}
this.scaleType = this.getScaleType(values);
var domain = [];
if (this.scaleType === 'time') {
values = values.map(function (v) { return moment(v).toDate(); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [new Date(min), new Date(max)];
}
else if (this.scaleType === 'linear') {
values = values.map(function (v) { return Number(v); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [min, max];
}
else {
domain = values;
}
this.xSet = values;
return domain;
};
AreaChartNormalizedComponent.prototype.getYDomain = function () {
return [0, 100];
};
AreaChartNormalizedComponent.prototype.getSeriesDomain = function () {
return this.results.map(function (d) { return d.name; });
};
AreaChartNormalizedComponent.prototype.getXScale = function (domain, width) {
var scale;
if (this.scaleType === 'time') {
scale = d3_1.default.scaleTime()
.range([0, width])
.domain(domain);
}
else if (this.scaleType === 'linear') {
scale = d3_1.default.scaleLinear()
.range([0, width])
.domain(domain);
}
else if (this.scaleType === 'ordinal') {
scale = d3_1.default.scalePoint()
.range([0, width])
.padding(0.1)
.domain(domain);
}
return scale;
};
AreaChartNormalizedComponent.prototype.getYScale = function (domain, height) {
return d3_1.default.scaleLinear()
.range([height, 0])
.domain(domain);
};
AreaChartNormalizedComponent.prototype.getScaleType = function (values) {
var date = true;
var num = true;
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
var value = values_1[_i];
if (!this.isDate(value)) {
date = false;
}
if (typeof value !== 'number') {
num = false;
}
}
if (date) {
return 'time';
}
if (num) {
return 'linear';
}
return 'ordinal';
};
AreaChartNormalizedComponent.prototype.isDate = function (value) {
if (value instanceof Date) {
return true;
}
return false;
};
AreaChartNormalizedComponent.prototype.updateDomain = function (domain) {
this.filteredDomain = domain;
this.xDomain = this.filteredDomain;
this.xScale = this.getXScale(this.xDomain, this.dims.width);
};
AreaChartNormalizedComponent.prototype.updateHoveredVertical = function (item) {
this.hoveredVertical = item.value;
};
AreaChartNormalizedComponent.prototype.hideCircles = function () {
this.hoveredVertical = null;
};
AreaChartNormalizedComponent.prototype.onClick = function (data, series) {
if (series) {
data.series = series.name;
}
this.select.emit(data);
};
AreaChartNormalizedComponent.prototype.trackBy = function (index, item) {
return item.name;
};
AreaChartNormalizedComponent.prototype.setColors = function () {
var domain;
if (this.schemeType === 'ordinal') {
domain = this.seriesDomain;
}
else {
domain = this.yDomain;
}
this.colors = new color_helper_1.ColorHelper(this.scheme, this.schemeType, domain, this.customColors);
};
AreaChartNormalizedComponent.prototype.getLegendOptions = function () {
var opts = {
scaleType: this.schemeType,
colors: undefined,
domain: []
};
if (opts.scaleType === 'ordinal') {
opts.domain = this.seriesDomain;
opts.colors = this.colors;
}
else {
opts.domain = this.yDomain;
opts.colors = this.colors.scale;
}
return opts;
};
AreaChartNormalizedComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
AreaChartNormalizedComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
AreaChartNormalizedComponent.prototype.onActivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
if (idx > -1) {
return;
}
this.activeEntries = [item].concat(this.activeEntries);
this.activate.emit({ value: item, entries: this.activeEntries });
};
AreaChartNormalizedComponent.prototype.onDeactivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
this.activeEntries.splice(idx, 1);
this.activeEntries = this.activeEntries.slice();
this.deactivate.emit({ value: event, entries: this.activeEntries });
};
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "legend", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "xAxis", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "yAxis", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "showXAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "showYAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "xAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "yAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "timeline", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "gradient", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Boolean)
], AreaChartNormalizedComponent.prototype, "showGridLines", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartNormalizedComponent.prototype, "curve", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Array)
], AreaChartNormalizedComponent.prototype, "activeEntries", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], AreaChartNormalizedComponent.prototype, "schemeType", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', core_1.EventEmitter)
], AreaChartNormalizedComponent.prototype, "activate", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', core_1.EventEmitter)
], AreaChartNormalizedComponent.prototype, "deactivate", void 0);
__decorate([
core_1.HostListener('mouseleave'),
__metadata('design:type', Function),
__metadata('design:paramtypes', []),
__metadata('design:returntype', void 0)
], AreaChartNormalizedComponent.prototype, "hideCircles", null);
AreaChartNormalizedComponent = __decorate([
core_1.Component({
selector: 'ngx-charts-area-chart-normalized',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\">\n <svg:defs>\n <svg:clipPath [attr.id]=\"clipPathId\">\n <svg:rect\n [attr.width]=\"dims.width + 10\"\n [attr.height]=\"dims.height + 10\"\n [attr.transform]=\"'translate(-5, -5)'\"/>\n </svg:clipPath>\n </svg:defs>\n <svg:g [attr.transform]=\"transform\" class=\"area-chart chart\">\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\">\n </svg:g>\n <svg:g ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\">\n </svg:g>\n <svg:g [attr.clip-path]=\"clipPath\">\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-area-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [activeEntries]=\"activeEntries\"\n [gradient]=\"gradient\"\n normalized=\"true\"\n [curve]=\"curve\"\n />\n </svg:g>\n <svg:g ngx-charts-area-tooltip\n [xSet]=\"xSet\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [results]=\"results\"\n [height]=\"dims.height\"\n [colors]=\"colors\"\n [showPercentage]=\"true\"\n (hover)=\"updateHoveredVertical($event)\"\n />\n <svg:g *ngFor=\"let series of results\">\n <svg:g ngx-charts-circle-ceries\n type=\"stacked\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [activeEntries]=\"activeEntries\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [visibleValue]=\"hoveredVertical\"\n (select)=\"onClick($event, series)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </svg:g>\n </svg:g>\n <svg:g ngx-charts-timeline\n *ngIf=\"timeline && scaleType === 'time'\"\n [attr.transform]=\"timelineTransform\"\n [results]=\"results\"\n [view]=\"[timelineWidth, height]\"\n [height]=\"timelineHeight\"\n [scheme]=\"scheme\"\n [customColors]=\"customColors\"\n [legend]=\"legend\"\n [scaleType]=\"scaleType\"\n (onDomainChange)=\"updateDomain($event)\">\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-area-series\n [xScale]=\"timelineXScale\"\n [yScale]=\"timelineYScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n normalized=\"true\"\n [curve]=\"curve\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
changeDetection: core_1.ChangeDetectionStrategy.OnPush
}),
__metadata('design:paramtypes', [])
], AreaChartNormalizedComponent);
return AreaChartNormalizedComponent;
}(base_chart_component_1.BaseChartComponent));
exports.AreaChartNormalizedComponent = AreaChartNormalizedComponent;
/***/ },
/***/ "./src/area-chart/area-chart-stacked.component.ts":
/***/ function(module, exports, __webpack_require__) {
"use strict";
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = __webpack_require__(0);
var view_dimensions_helper_1 = __webpack_require__("./src/common/view-dimensions.helper.ts");
var color_helper_1 = __webpack_require__("./src/common/color.helper.ts");
var base_chart_component_1 = __webpack_require__("./src/common/base-chart.component.ts");
var moment = __webpack_require__(1);
var id_1 = __webpack_require__("./src/utils/id.ts");
var d3_1 = __webpack_require__("./src/d3.ts");
var AreaChartStackedComponent = (function (_super) {
__extends(AreaChartStackedComponent, _super);
function AreaChartStackedComponent() {
_super.apply(this, arguments);
this.legend = false;
this.showGridLines = true;
this.curve = d3_1.default.shape.curveLinear;
this.activeEntries = [];
this.activate = new core_1.EventEmitter();
this.deactivate = new core_1.EventEmitter();
this.margin = [10, 20, 10, 20];
this.xAxisHeight = 0;
this.yAxisWidth = 0;
this.timelineHeight = 50;
this.timelinePadding = 10;
}
AreaChartStackedComponent.prototype.update = function () {
var _this = this;
_super.prototype.update.call(this);
this.zone.run(function () {
_this.dims = view_dimensions_helper_1.calculateViewDimensions({
width: _this.width,
height: _this.height,
margins: _this.margin,
showXAxis: _this.xAxis,
showYAxis: _this.yAxis,
xAxisHeight: _this.xAxisHeight,
yAxisWidth: _this.yAxisWidth,
showXLabel: _this.showXAxisLabel,
showYLabel: _this.showYAxisLabel,
showLegend: _this.legend,
legendType: _this.schemeType
});
if (_this.timeline) {
_this.dims.height -= (_this.timelineHeight + _this.margin[2] + _this.timelinePadding);
}
_this.xDomain = _this.getXDomain();
if (_this.filteredDomain) {
_this.xDomain = _this.filteredDomain;
}
_this.yDomain = _this.getYDomain();
_this.seriesDomain = _this.getSeriesDomain();
_this.xScale = _this.getXScale(_this.xDomain, _this.dims.width);
_this.yScale = _this.getYScale(_this.yDomain, _this.dims.height);
var _loop_1 = function(i) {
var val = _this.xSet[i];
var d0 = 0;
for (var _i = 0, _a = _this.results; _i < _a.length; _i++) {
var group = _a[_i];
var d = group.series.find(function (item) {
var a = item.name;
var b = val;
if (_this.scaleType === 'time') {
a = a.valueOf();
b = b.valueOf();
}
return a === b;
});
if (d) {
d.d0 = d0;
d.d1 = d0 + d.value;
d0 += d.value;
}
else {
d = {
name: val,
value: 0,
d0: d0,
d1: d0
};
group.series.push(d);
}
}
};
for (var i = 0; i < _this.xSet.length; i++) {
_loop_1(i);
}
_this.updateTimeline();
_this.setColors();
_this.legendOptions = _this.getLegendOptions();
_this.transform = "translate(" + _this.dims.xOffset + " , " + _this.margin[0] + ")";
var pageUrl = window.location.href;
_this.clipPathId = 'clip' + id_1.id().toString();
_this.clipPath = "url(" + pageUrl + "#" + _this.clipPathId + ")";
});
};
AreaChartStackedComponent.prototype.updateTimeline = function () {
if (this.timeline) {
this.timelineWidth = this.width;
if (this.legend) {
this.timelineWidth = this.dims.width;
}
this.timelineXDomain = this.getXDomain();
this.timelineXScale = this.getXScale(this.timelineXDomain, this.timelineWidth);
this.timelineYScale = this.getYScale(this.yDomain, this.timelineHeight);
this.timelineTransform = "translate(" + this.dims.xOffset + ", " + -this.margin[2] + ")";
}
};
AreaChartStackedComponent.prototype.getXDomain = function () {
var values = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];
if (!values.includes(d.name)) {
values.push(d.name);
}
}
}
this.scaleType = this.getScaleType(values);
var domain = [];
if (this.scaleType === 'time') {
values = values.map(function (v) { return moment(v).toDate(); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [new Date(min), new Date(max)];
}
else if (this.scaleType === 'linear') {
values = values.map(function (v) { return Number(v); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [min, max];
}
else {
domain = values;
}
this.xSet = values;
return domain;
};
AreaChartStackedComponent.prototype.getYDomain = function () {
var _this = this;
var domain = [];
var _loop_2 = function(i) {
var val = this_1.xSet[i];
var sum = 0;
for (var _i = 0, _a = this_1.results; _i < _a.length; _i++) {
var group = _a[_i];
var d = group.series.find(function (item) {
var a = item.name;
var b = val;
if (_this.scaleType === 'time') {
a = a.valueOf();
b = b.valueOf();
}
return a === b;
});
if (d) {
sum += d.value;
}
}
domain.push(sum);
};
var this_1 = this;
for (var i = 0; i < this.xSet.length; i++) {
_loop_2(i);
}
var min = Math.min.apply(Math, [0].concat(domain));
var max = Math.max.apply(Math, domain);
return [min, max];
};
AreaChartStackedComponent.prototype.getSeriesDomain = function () {
return this.results.map(function (d) { return d.name; });
};
AreaChartStackedComponent.prototype.getXScale = function (domain, width) {
var scale;
if (this.scaleType === 'time') {
scale = d3_1.default.scaleTime()
.range([0, width])
.domain(domain);
}
else if (this.scaleType === 'linear') {
scale = d3_1.default.scaleLinear()
.range([0, width])
.domain(domain);
}
else if (this.scaleType === 'ordinal') {
scale = d3_1.default.scalePoint()
.range([0, width])
.padding(0.1)
.domain(domain);
}
return scale;
};
AreaChartStackedComponent.prototype.getYScale = function (domain, height) {
return d3_1.default.scaleLinear()
.range([height, 0])
.domain(domain);
};
AreaChartStackedComponent.prototype.getScaleType = function (values) {
var date = true;
var num = true;
for (var _i = 0, values_1 = values; _i < values_1.length; _i++) {
var value = values_1[_i];
if (!this.isDate(value)) {
date = false;
}
if (typeof value !== 'number') {
num = false;
}
}
if (date) {
return 'time';
}
if (num) {
return 'linear';
}
return 'ordinal';
};
AreaChartStackedComponent.prototype.isDate = function (value) {
if (value instanceof Date) {
return true;
}
return false;
};
AreaChartStackedComponent.prototype.updateDomain = function (domain) {
this.filteredDomain = domain;
this.xDomain = this.filteredDomain;
this.xScale = this.getXScale(this.xDomain, this.dims.width);
};
AreaChartStackedComponent.prototype.updateHoveredVertical = function (item) {
this.hoveredVertical = item.value;
};
AreaChartStackedComponent.prototype.hideCircles = function () {
this.hoveredVertical = null;
};
AreaChartStackedComponent.prototype.onClick = function (data, series) {
if (series) {
data.series = series.name;
}
this.select.emit(data);
};
AreaChartStackedComponent.prototype.trackBy = function (index, item) {
return item.name;
};
AreaChartStackedComponent.prototype.setColors = function () {
var domain;
if (this.schemeType === 'ordinal') {
domain = this.seriesDomain;
}
else {
domain = this.yDomain;
}
this.colors = new color_helper_1.ColorHelper(this.scheme, this.schemeType, domain, this.customColors);
};
AreaChartStackedComponent.prototype.getLegendOptions = function () {
var opts = {
scaleType: this.schemeType,
colors: undefined,
domain: []
};
if (opts.scaleType === 'ordinal') {
opts.domain = this.seriesDomain;
opts.colors = this.colors;
}
else {
opts.domain = this.yDomain;
opts.colors = this.colors.scale;
}
return opts;
};
AreaChartStackedComponent.prototype.updateYAxisWidth = function (_a) {
var width = _a.width;
this.yAxisWidth = width;
this.update();
};
AreaChartStackedComponent.prototype.updateXAxisHeight = function (_a) {
var height = _a.height;
this.xAxisHeight = height;
this.update();
};
AreaChartStackedComponent.prototype.onActivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
if (idx > -1) {
return;
}
this.activeEntries = [item].concat(this.activeEntries);
this.activate.emit({ value: item, entries: this.activeEntries });
};
AreaChartStackedComponent.prototype.onDeactivate = function (item) {
var idx = this.activeEntries.findIndex(function (d) {
return d.name === item.name && d.value === item.value;
});
this.activeEntries.splice(idx, 1);
this.activeEntries = this.activeEntries.slice();
this.deactivate.emit({ value: event, entries: this.activeEntries });
};
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "legend", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "xAxis", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "yAxis", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "showXAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "showYAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "xAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "yAxisLabel", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "timeline", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "gradient", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Boolean)
], AreaChartStackedComponent.prototype, "showGridLines", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Object)
], AreaChartStackedComponent.prototype, "curve", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', Array)
], AreaChartStackedComponent.prototype, "activeEntries", void 0);
__decorate([
core_1.Input(),
__metadata('design:type', String)
], AreaChartStackedComponent.prototype, "schemeType", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', core_1.EventEmitter)
], AreaChartStackedComponent.prototype, "activate", void 0);
__decorate([
core_1.Output(),
__metadata('design:type', core_1.EventEmitter)
], AreaChartStackedComponent.prototype, "deactivate", void 0);
__decorate([
core_1.HostListener('mouseleave'),
__metadata('design:type', Function),
__metadata('design:paramtypes', []),
__metadata('design:returntype', void 0)
], AreaChartStackedComponent.prototype, "hideCircles", null);
AreaChartStackedComponent = __decorate([
core_1.Component({
selector: 'ngx-charts-area-chart-stacked',
template: "\n <ngx-charts-chart\n [view]=\"[width, height]\"\n [showLegend]=\"legend\"\n [legendOptions]=\"legendOptions\"\n [activeEntries]=\"activeEntries\"\n (legendLabelClick)=\"onClick($event)\"\n (legendLabelActivate)=\"onActivate($event)\"\n (legendLabelDeactivate)=\"onDeactivate($event)\">\n <svg:defs>\n <svg:clipPath [attr.id]=\"clipPathId\">\n <svg:rect\n [attr.width]=\"dims.width + 10\"\n [attr.height]=\"dims.height + 10\"\n [attr.transform]=\"'translate(-5, -5)'\"/>\n </svg:clipPath>\n </svg:defs>\n <svg:g [attr.transform]=\"transform\" class=\"area-chart chart\">\n <svg:g ngx-charts-x-axis\n *ngIf=\"xAxis\"\n [xScale]=\"xScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showXAxisLabel\"\n [labelText]=\"xAxisLabel\"\n (dimensionsChanged)=\"updateXAxisHeight($event)\">\n </svg:g>\n <svg:g ngx-charts-y-axis\n *ngIf=\"yAxis\"\n [yScale]=\"yScale\"\n [dims]=\"dims\"\n [showGridLines]=\"showGridLines\"\n [showLabel]=\"showYAxisLabel\"\n [labelText]=\"yAxisLabel\"\n (dimensionsChanged)=\"updateYAxisWidth($event)\">\n </svg:g>\n <svg:g [attr.clip-path]=\"clipPath\">\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-area-series\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n [activeEntries]=\"activeEntries\"\n stacked=\"true\"\n [curve]=\"curve\"\n />\n </svg:g>\n <svg:g ngx-charts-area-tooltip\n [xSet]=\"xSet\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [results]=\"results\"\n [height]=\"dims.height\"\n [colors]=\"colors\"\n (hover)=\"updateHoveredVertical($event)\"\n />\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-circle-ceries\n type=\"stacked\"\n [xScale]=\"xScale\"\n [yScale]=\"yScale\"\n [colors]=\"colors\"\n [activeEntries]=\"activeEntries\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [visibleValue]=\"hoveredVertical\"\n (select)=\"onClick($event, series)\"\n (activate)=\"onActivate($event)\"\n (deactivate)=\"onDeactivate($event)\"\n />\n </svg:g>\n </svg:g>\n </svg:g>\n <svg:g ngx-charts-timeline\n *ngIf=\"timeline && scaleType === 'time'\"\n [attr.transform]=\"timelineTransform\"\n [results]=\"results\"\n [view]=\"[timelineWidth, height]\"\n [height]=\"timelineHeight\"\n [scheme]=\"scheme\"\n [customColors]=\"customColors\"\n [legend]=\"legend\"\n [scaleType]=\"scaleType\"\n (onDomainChange)=\"updateDomain($event)\">\n <svg:g *ngFor=\"let series of results; trackBy:trackBy\">\n <svg:g ngx-charts-area-series\n [xScale]=\"timelineXScale\"\n [yScale]=\"timelineYScale\"\n [colors]=\"colors\"\n [data]=\"series\"\n [scaleType]=\"scaleType\"\n [gradient]=\"gradient\"\n stacked=\"true\"\n [curve]=\"curve\"\n />\n </svg:g>\n </svg:g>\n </ngx-charts-chart>\n ",
changeDetection: core_1.ChangeDetectionStrategy.OnPush
}),
__metadata('design:paramtypes', [])
], AreaChartStackedComponent);
return AreaChartStackedComponent;
}(base_chart_component_1.BaseChartComponent));
exports.AreaChartStackedComponent = AreaChartStackedComponent;
/***/ },
/***/ "./src/area-chart/area-chart.component.ts":
/***/ function(module, exports, __webpack_require__) {
"use strict";
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = __webpack_require__(0);
var view_dimensions_helper_1 = __webpack_require__("./src/common/view-dimensions.helper.ts");
var color_helper_1 = __webpack_require__("./src/common/color.helper.ts");
var base_chart_component_1 = __webpack_require__("./src/common/base-chart.component.ts");
var moment = __webpack_require__(1);
var id_1 = __webpack_require__("./src/utils/id.ts");
var d3_1 = __webpack_require__("./src/d3.ts");
var AreaChartComponent = (function (_super) {
__extends(AreaChartComponent, _super);
function AreaChartComponent() {
_super.apply(this, arguments);
this.showGridLines = true;
this.curve = d3_1.default.shape.curveLinear;
this.activeEntries = [];
this.activate = new core_1.EventEmitter();
this.deactivate = new core_1.EventEmitter();
this.margin = [10, 20, 10, 20];
this.xAxisHeight = 0;
this.yAxisWidth = 0;
this.timelineHeight = 50;
this.timelinePadding = 10;
}
AreaChartComponent.prototype.update = function () {
var _this = this;
_super.prototype.update.call(this);
this.zone.run(function () {
_this.dims = view_dimensions_helper_1.calculateViewDimensions({
width: _this.width,
height: _this.height,
margins: _this.margin,
showXAxis: _this.xAxis,
showYAxis: _this.yAxis,
xAxisHeight: _this.xAxisHeight,
yAxisWidth: _this.yAxisWidth,
showXLabel: _this.showXAxisLabel,
showYLabel: _this.showYAxisLabel,
showLegend: _this.legend,
legendType: _this.schemeType
});
if (_this.timeline) {
_this.dims.height -= (_this.timelineHeight + _this.margin[2] + _this.timelinePadding);
}
_this.xDomain = _this.getXDomain();
if (_this.filteredDomain) {
_this.xDomain = _this.filteredDomain;
}
_this.yDomain = _this.getYDomain();
_this.seriesDomain = _this.getSeriesDomain();
_this.xScale = _this.getXScale(_this.xDomain, _this.dims.width);
_this.yScale = _this.getYScale(_this.yDomain, _this.dims.height);
_this.updateTimeline();
_this.setColors();
_this.legendOptions = _this.getLegendOptions();
_this.transform = "translate(" + _this.dims.xOffset + ", " + _this.margin[0] + ")";
var pageUrl = window.location.href;
_this.clipPathId = 'clip' + id_1.id().toString();
_this.clipPath = "url(" + pageUrl + "#" + _this.clipPathId + ")";
});
};
AreaChartComponent.prototype.updateTimeline = function () {
if (this.timeline) {
this.timelineWidth = this.width;
if (this.legend) {
this.timelineWidth = this.dims.width;
}
this.timelineXDomain = this.getXDomain();
this.timelineXScale = this.getXScale(this.timelineXDomain, this.timelineWidth);
this.timelineYScale = this.getYScale(this.yDomain, this.timelineHeight);
this.timelineTransform = "translate(" + this.dims.xOffset + ", " + -this.margin[2] + ")";
}
};
AreaChartComponent.prototype.getXDomain = function () {
var values = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];
if (!values.includes(d.name)) {
values.push(d.name);
}
}
}
this.scaleType = this.getScaleType(values);
var domain = [];
if (this.scaleType === 'time') {
values = values.map(function (v) { return moment(v).toDate(); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [min, max];
}
else if (this.scaleType === 'linear') {
values = values.map(function (v) { return Number(v); });
var min = Math.min.apply(Math, values);
var max = Math.max.apply(Math, values);
domain = [min, max];
}
else {
domain = values;
}
this.xSet = values;
return domain;
};
AreaChartComponent.prototype.getYDomain = function () {
var domain = [];
for (var _i = 0, _a = this.results; _i < _a.length; _i++) {
var results = _a[_i];
for (var _b = 0, _c = results.series; _b < _c.length; _b++) {
var d = _c[_b];