Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chart Mappings & Data Labels #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/chartMappings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const axisChartMappings = {
export const chartMappings = {
calls_per_day: {
columns: [
{
Expand All @@ -8,10 +8,12 @@ export const axisChartMappings = {
{
key: 'Answered',
xAxis: false,
name: "Answered Calls"
},
{
key: 'Missed',
xAxis: false,
name: "Missed Calls"
}
]
},
Expand Down Expand Up @@ -57,10 +59,7 @@ export const axisChartMappings = {
xAxis: false,
}
]
}
};

export const listChartMappings = {
},
top_customers_by_calls: {
columns: [
{
Expand All @@ -85,4 +84,4 @@ export const listChartMappings = {
}
]
}
};
};
25 changes: 12 additions & 13 deletions src/components/axisChart/axis-chart.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="../../../node_modules/@types/angular/index.d.ts" />
import { axisChartMappings } from '../../chartMappings';
import { FsData, FsDataResponse } from '../../services/FsData.service';
import { FsSeriesTranslation } from '../../services/FsData.service';
import { DataLabelTransformer } from "../../services/DataLabelTransformer.service";
import { ChartMapping } from "../../services/ChartMapping.service";
import './bar-chart.styles.scss';

interface ChartResponse {
Expand All @@ -24,15 +24,19 @@ export abstract class AxisChartWidget {
protected fromDate: string;
protected toDate: string;

constructor(protected $scope: ng.IScope, protected FsData: FsData) {

constructor(
protected $scope: ng.IScope,
protected FsData: FsData,
protected DataLabelTransformer: DataLabelTransformer,
protected ChartMapping: ChartMapping
) {
}

protected setResponse(dataset: string, value: FsDataResponse): any {
let data: any[] = [];
let labels: string[] = [];
let series: any[] = [];
const chartMap = axisChartMappings[dataset];
const chartMap = this.ChartMapping.getMappingForType(dataset);
if (chartMap === undefined) {
throw new Error(`Chartmapping missing for ${dataset}`);
}
Expand All @@ -53,14 +57,9 @@ protected setResponse(dataset: string, value: FsDataResponse): any {
return a.name < b.name ? -1 : 1;
}
})
.map(d => {
if (d[xAxisColumn.key].length > 15) {
return d[xAxisColumn.key].substr(0, 18)+'...';
} else {
return d[xAxisColumn.key];
}
})
.filter((entry, index, arr) => labels.indexOf(entry) === -1);
.map(d => d[xAxisColumn.key])
.filter((entry, index, arr) => labels.indexOf(entry) === -1)
.map(this.DataLabelTransformer.transform);
labels.push(...dataLabels);

const objKeys = Object.keys(ds.data[0]);
Expand Down
11 changes: 9 additions & 2 deletions src/components/bar-chart/bar-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { AxisChartWidget } from '../axisChart/axis-chart.component';
import { Component } from '../../decorators';
import { FsData } from '../../services/FsData.service';
import { DataLabelTransformer } from "../../services/DataLabelTransformer.service";
import { ChartMapping } from "../../services/ChartMapping.service";
import '../axisChart/bar-chart.styles.scss';

@Component({
Expand Down Expand Up @@ -31,8 +33,13 @@ import '../axisChart/bar-chart.styles.scss';
})
export class BarChartWidget extends AxisChartWidget {

constructor(protected $scope: ng.IScope, protected FsData: FsData) {
super($scope, FsData);
constructor(
protected $scope: ng.IScope,
protected FsData: FsData,
protected DataLabelTransformer: DataLabelTransformer,
protected ChartMapping: ChartMapping
) {
super($scope, FsData, DataLabelTransformer, ChartMapping);
}

}
11 changes: 9 additions & 2 deletions src/components/line-chart/line-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { AxisChartWidget } from '../axisChart/axis-chart.component';
import { Component } from '../../decorators';
import { FsData } from '../../services/FsData.service';
import { DataLabelTransformer } from "../../services/DataLabelTransformer.service";
import { ChartMapping } from "../../services/ChartMapping.service";
import '../axisChart/line-chart.styles.scss';

@Component({
Expand Down Expand Up @@ -31,8 +33,13 @@ import '../axisChart/line-chart.styles.scss';
})
export class LineChartWidget extends AxisChartWidget {

constructor(protected $scope: ng.IScope, protected FsData: FsData) {
super($scope, FsData);
constructor(
protected $scope: ng.IScope,
protected FsData: FsData,
protected DataLabelTransformer: DataLabelTransformer,
protected ChartMapping: ChartMapping
) {
super($scope, FsData, DataLabelTransformer, ChartMapping);
}

}
9 changes: 8 additions & 1 deletion src/components/single-value/single-value.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import './single-value.styles.scss';
title: '@',
type: '@',
segment: '@',
suffix: '@',
fromDate: '@',
toDate: '@'
},
Expand All @@ -25,6 +26,7 @@ export class SingleValueWidget {
private type: string;
private segments: string[] = ['all_data'];
private value: any;
private suffix: string;
private fromDate: string;
private toDate: string;

Expand All @@ -33,7 +35,12 @@ export class SingleValueWidget {
}

private setResponse(value: FsDataResponse): any {
this.value = value.datasources[0].data;
let data = value.datasources[0].data;
if(this.suffix) {
this.value = `${data} ${this.suffix}`;
} else {
this.value = data
}
}

async $onChanges() {
Expand Down
20 changes: 13 additions & 7 deletions src/components/top-list/top-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { listChartMappings } from '../../chartMappings';
import { Component } from '../../decorators';
import { FsData, FsDataResponse } from '../../services/FsData.service';
import { ChartMapping } from "../../services/ChartMapping.service";
import './top-list.styles.scss';

interface ToplistData {
Expand All @@ -14,12 +15,12 @@ interface ToplistData {
<h3>{{vm.title}}</h3>
<table cell-spacing="0" cell-padding="0">
<thead>
<th>Name</th>
<th>Value</th>
<th>{{vm.keyTitle}}</th>
<th>{{vm.valueTitle}}</th>
</thead>
<tbody>
<tr
ng-repeat="item in vm.list | orderBy:'value':true | limitTo: 4"
ng-repeat="item in vm.list | orderBy:'value':true | limitTo: vm.limit"
>
<td ng-bind="item.name"></td>
<td ng-bind="item.value"></td>
Expand All @@ -29,6 +30,9 @@ interface ToplistData {
`,
bindings: {
title: '@',
keyTitle: '@',
valueTitle: '@',
limit: '@',
type: '@',
segments: '<',
fromDate: '@',
Expand All @@ -44,13 +48,15 @@ export class TopListWidget {
private fromDate: string;
private toDate: string;

constructor (private $scope: ng.IScope, private FsData: FsData) {

}
constructor (
private $scope: ng.IScope,
private FsData: FsData,
private ChartMapping: ChartMapping
) {}

private setResponse(dataset: string, value: FsDataResponse): any {
let data: any[] = [];
const chartMap = listChartMappings[dataset];
const chartMap = this.ChartMapping.getMappingForType(dataset);
if (chartMap === undefined) {
throw new Error(`Chartmapping missing for ${dataset}`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@
type="top_customers_by_calls"
from-date="{{ctrl.fromDate}}"
to-date="{{ctrl.toDate}}"
limit=4
></fs-top-list>
<fs-top-list
title="Geographic origin"
type="geographic_origin"
key_title="Country"
value_title="Calls"
limit=4
></fs-top-list>
<fs-single-value
title="Average calls per user"
Expand All @@ -76,6 +80,7 @@
<fs-single-value
title="Call durations"
type="call_durations"
suffix="min"
></fs-single-value>
<fs-single-value
title="Missed calls"
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ declare var angular: angular.IAngularStatic;
import { LineChartWidget } from "./components/line-chart/line-chart.component";
import { BarChartWidget } from "./components/bar-chart/bar-chart.component";
import { TopListWidget } from "./components/top-list/top-list.component";
import {SingleValueWidget } from "./components/single-value/single-value.component";
import { SingleValueWidget } from "./components/single-value/single-value.component";
import { FsData } from "./services/FsData.service";
import { DataLabelTransformer } from "./services/DataLabelTransformer.service";
import { ChartMapping } from "./services/ChartMapping.service";
import { chartMappings } from "./chartMappings";

angular
.module('fs-widgets', ['chart.js'])
.constant("ChartMappings", chartMappings)
.service("ChartMapping", ["ChartMappings", ChartMapping])
.provider("FsData", [() => new FsData()])
.service("DataLabelTransformer", [() => new DataLabelTransformer()])
.run(($http: ng.IHttpService) => $http.defaults.headers.common['Access-Control-Allow-Origin'] = '*')
.component('fsLineChart', LineChartWidget)
.component('fsBarChart', BarChartWidget)
Expand Down
30 changes: 30 additions & 0 deletions src/services/ChartMapping.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface AxisChartMappings {
[type: String]: ChartMappingModel
}

export interface ChartMappingModel {
type: String;
sort: boolean;
columns: [AxisMapping];
}

export interface AxisMapping {
key: String;
xAxis: boolean;
name: String;
}

class ChartMapping implements ng.IServiceProvider {

private axisChartMappings: AxisChartMappings;

constructor(axisChartMappings: AxisChartMappings) {
this.axisChartMappings = axisChartMappings;
}

getMappingForType(type: String): ChartMappingModel {
return this.axisChartMappings[type];
}
}

export { ChartMapping };
9 changes: 9 additions & 0 deletions src/services/DataLabelTransformer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This class is just a placeholder
// You can replace it with your own service if you need to
class DataLabelTransformer implements ng.IServiceProvider {
transform(seriesName): string {
return seriesName;
}
}

export { DataLabelTransformer };