blob: 6107d16bb116c1cdd9a3bd8589797a5081c47cac [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { Injectable } from '@angular/core';
17import { FnService } from '../util/fn.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Sean Condonf4f54a12018-10-10 23:25:46 +010019import * as d3 from 'd3';
Sean Condon83fc39f2018-04-19 18:56:13 +010020
21/**
22 * ONOS GUI -- SVG -- Util Service
23 *
24 * The SVG Util Service provides a miscellany of utility functions.
25 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010026@Injectable({
Bhavesh72ead492018-07-19 16:29:18 +053027 providedIn: 'root',
Sean Condonfd6d11b2018-06-02 20:29:49 +010028})
Sean Condon83fc39f2018-04-19 18:56:13 +010029export class SvgUtilService {
Sean Condonf4f54a12018-10-10 23:25:46 +010030 lightNorm: string[];
31 lightMute: string[];
32 darkNorm: string[];
33 darkMute: string[];
34 colors: any;
Sean Condon83fc39f2018-04-19 18:56:13 +010035
36 constructor(
37 private fs: FnService,
38 private log: LogService
39 ) {
Sean Condonf4f54a12018-10-10 23:25:46 +010040
41 // --- Ordinal scales for 7 values.
42 // TODO: migrate these colors to the theme service.
43
44 // Colors per Mojo-Design's color palette.. (version one)
45 // blue red dk grey steel lt blue lt red lt grey
46 // var lightNorm = ['#5b99d2', '#d05a55', '#716b6b', '#7e9aa8', '#66cef6', '#db7773', '#aeada8' ],
47 // lightMute = ['#a8cceb', '#f1a7a7', '#b9b5b5', '#bdcdd5', '#a8e9fd', '#f8c9c9', '#d7d6d4' ],
48
49 // Colors per Mojo-Design's color palette.. (version two)
50 // blue lt blue red green brown teal lime
51 this.lightNorm = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00'];
52 this.lightMute = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d'];
53
54 this.darkNorm = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00'];
55 this.darkMute = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d'];
56
57
58 this.colors = {
59 light: {
60 norm: d3.scaleOrdinal().range(this.lightNorm),
61 mute: d3.scaleOrdinal().range(this.lightMute),
62 },
63 dark: {
64 norm: d3.scaleOrdinal().range(this.darkNorm),
65 mute: d3.scaleOrdinal().range(this.darkMute),
66 },
67 };
68
Sean Condon83fc39f2018-04-19 18:56:13 +010069 this.log.debug('SvgUtilService constructed');
70 }
71
Sean Condonfd6d11b2018-06-02 20:29:49 +010072 translate(x: number[], y?: any): string {
Sean Condon83fc39f2018-04-19 18:56:13 +010073 if (this.fs.isA(x) && x.length === 2 && !y) {
74 return 'translate(' + x[0] + ',' + x[1] + ')';
75 }
76 return 'translate(' + x + ',' + y + ')';
77 }
Sean Condonf4f54a12018-10-10 23:25:46 +010078
79 scale(x, y) {
80 return 'scale(' + x + ',' + y + ')';
81 }
82
83 skewX(x) {
84 return 'skewX(' + x + ')';
85 }
86
87 rotate(deg) {
88 return 'rotate(' + deg + ')';
89 }
90
91 cat7() {
92 const tcid = 'd3utilTestCard';
93
94 function getColor(id, muted, theme) {
95 // NOTE: since we are lazily assigning domain ids, we need to
96 // get the color from all 4 scales, to keep the domains
97 // in sync.
98 const ln = this.colors.light.norm(id);
99 const lm = this.colors.light.mute(id);
100 const dn = this.colors.dark.norm(id);
101 const dm = this.colors.dark.mute(id);
102 if (theme === 'dark') {
103 return muted ? dm : dn;
104 } else {
105 return muted ? lm : ln;
106 }
107 }
108
109 function testCard(svg) {
110 let g = svg.select('g#' + tcid);
111 const dom = d3.range(7);
112 let k;
113 let muted;
114 let theme;
115 let what;
116
117 if (!g.empty()) {
118 g.remove();
119
120 } else {
121 g = svg.append('g')
122 .attr('id', tcid)
123 .attr('transform', 'scale(4)translate(20,20)');
124
125 for (k = 0; k < 4; k++) {
126 muted = k % 2;
127 what = muted ? ' muted' : ' normal';
128 theme = k < 2 ? 'light' : 'dark';
129 dom.forEach(function (id, i) {
130 const x = i * 20;
131 const y = k * 20;
132 const f = getColor(id, muted, theme);
133 g.append('circle').attr({
134 cx: x,
135 cy: y,
136 r: 5,
137 fill: f,
138 });
139 });
140 g.append('rect').attr({
141 x: 140,
142 y: k * 20 - 5,
143 width: 32,
144 height: 10,
145 rx: 2,
146 fill: '#888',
147 });
148 g.append('text').text(theme + what)
149 .attr({
150 x: 142,
151 y: k * 20 + 2,
152 fill: 'white',
153 })
154 .style('font-size', '4pt');
155 }
156 }
157 }
158
159 return {
160 testCard: testCard,
161 getColor: getColor,
162 };
163 }
164
Sean Condon83fc39f2018-04-19 18:56:13 +0100165}