blob: 75104860dffef3f7b0d6e9eacecdb7943ec062d5 [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
Sean Condon55c30532018-10-29 12:26:57 +000021
22// --- Ordinal scales for 7 values.
23// TODO: migrate these colors to the theme service.
24
25// Colors per Mojo-Design's color palette.. (version one)
26// blue red dk grey steel lt blue lt red lt grey
27// var lightNorm = ['#5b99d2', '#d05a55', '#716b6b', '#7e9aa8', '#66cef6', '#db7773', '#aeada8' ],
28// lightMute = ['#a8cceb', '#f1a7a7', '#b9b5b5', '#bdcdd5', '#a8e9fd', '#f8c9c9', '#d7d6d4' ],
29
30// Colors per Mojo-Design's color palette.. (version two)
31// blue lt blue red green brown teal lime
32const lightNorm: string[] = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00'];
33const lightMute: string[] = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d'];
34
35const darkNorm: string[] = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00'];
36const darkMute: string[] = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d'];
37
38const colors = {
39 light: {
40 norm: d3.scaleOrdinal().range(lightNorm),
41 mute: d3.scaleOrdinal().range(lightMute),
42 },
43 dark: {
44 norm: d3.scaleOrdinal().range(darkNorm),
45 mute: d3.scaleOrdinal().range(darkMute),
46 },
47};
48
Sean Condon83fc39f2018-04-19 18:56:13 +010049/**
50 * ONOS GUI -- SVG -- Util Service
51 *
52 * The SVG Util Service provides a miscellany of utility functions.
53 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010054@Injectable({
Bhavesh72ead492018-07-19 16:29:18 +053055 providedIn: 'root',
Sean Condonfd6d11b2018-06-02 20:29:49 +010056})
Sean Condon83fc39f2018-04-19 18:56:13 +010057export class SvgUtilService {
58
59 constructor(
60 private fs: FnService,
61 private log: LogService
62 ) {
Sean Condonf4f54a12018-10-10 23:25:46 +010063
Sean Condonf4f54a12018-10-10 23:25:46 +010064
Sean Condonf4f54a12018-10-10 23:25:46 +010065
Sean Condon83fc39f2018-04-19 18:56:13 +010066 this.log.debug('SvgUtilService constructed');
67 }
68
Sean Condonfd6d11b2018-06-02 20:29:49 +010069 translate(x: number[], y?: any): string {
Sean Condon83fc39f2018-04-19 18:56:13 +010070 if (this.fs.isA(x) && x.length === 2 && !y) {
71 return 'translate(' + x[0] + ',' + x[1] + ')';
72 }
73 return 'translate(' + x + ',' + y + ')';
74 }
Sean Condonf4f54a12018-10-10 23:25:46 +010075
76 scale(x, y) {
77 return 'scale(' + x + ',' + y + ')';
78 }
79
80 skewX(x) {
81 return 'skewX(' + x + ')';
82 }
83
84 rotate(deg) {
85 return 'rotate(' + deg + ')';
86 }
87
88 cat7() {
89 const tcid = 'd3utilTestCard';
90
91 function getColor(id, muted, theme) {
92 // NOTE: since we are lazily assigning domain ids, we need to
93 // get the color from all 4 scales, to keep the domains
94 // in sync.
Sean Condon55c30532018-10-29 12:26:57 +000095 const ln = colors.light.norm(id);
96 const lm = colors.light.mute(id);
97 const dn = colors.dark.norm(id);
98 const dm = colors.dark.mute(id);
Sean Condonf4f54a12018-10-10 23:25:46 +010099 if (theme === 'dark') {
100 return muted ? dm : dn;
101 } else {
102 return muted ? lm : ln;
103 }
104 }
105
106 function testCard(svg) {
107 let g = svg.select('g#' + tcid);
108 const dom = d3.range(7);
109 let k;
110 let muted;
111 let theme;
112 let what;
113
114 if (!g.empty()) {
115 g.remove();
116
117 } else {
118 g = svg.append('g')
119 .attr('id', tcid)
120 .attr('transform', 'scale(4)translate(20,20)');
121
122 for (k = 0; k < 4; k++) {
123 muted = k % 2;
124 what = muted ? ' muted' : ' normal';
125 theme = k < 2 ? 'light' : 'dark';
Sean Condon55c30532018-10-29 12:26:57 +0000126 dom.forEach((id, i) => {
Sean Condonf4f54a12018-10-10 23:25:46 +0100127 const x = i * 20;
128 const y = k * 20;
Sean Condon55c30532018-10-29 12:26:57 +0000129 g.append('circle')
130 .attr('cx', x)
131 .attr('cy', y)
132 .attr('r', 5)
133 .attr('fill', getColor(id, muted, theme));
Sean Condonf4f54a12018-10-10 23:25:46 +0100134 });
Sean Condon55c30532018-10-29 12:26:57 +0000135 g.append('rect')
136 .attr('x', 140)
137 .attr('y', k * 20 - 5)
138 .attr('width', 32)
139 .attr('height', 10)
140 .attr('rx', 2)
141 .attr('fill', '#888');
Sean Condonf4f54a12018-10-10 23:25:46 +0100142 g.append('text').text(theme + what)
Sean Condon55c30532018-10-29 12:26:57 +0000143 .attr('x', 142)
144 .attr('y', k * 20 + 2)
145 .attr('fill', 'white');
146 // .style('font-size', '4pt');
Sean Condonf4f54a12018-10-10 23:25:46 +0100147 }
148 }
149 }
150
151 return {
152 testCard: testCard,
153 getColor: getColor,
154 };
155 }
156
Sean Condon83fc39f2018-04-19 18:56:13 +0100157}