Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | import { Injectable } from '@angular/core'; |
| 17 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 19 | import * as d3 from 'd3'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 20 | |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 21 | |
| 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 |
| 32 | const lightNorm: string[] = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00']; |
| 33 | const lightMute: string[] = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d']; |
| 34 | |
| 35 | const darkNorm: string[] = ['#5b99d2', '#66cef6', '#d05a55', '#0f9d58', '#ba7941', '#3dc0bf', '#56af00']; |
| 36 | const darkMute: string[] = ['#9ebedf', '#abdef5', '#d79a96', '#7cbe99', '#cdab8d', '#96d5d5', '#a0c96d']; |
| 37 | |
| 38 | const 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 Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 49 | /** |
| 50 | * ONOS GUI -- SVG -- Util Service |
| 51 | * |
| 52 | * The SVG Util Service provides a miscellany of utility functions. |
| 53 | */ |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 54 | @Injectable({ |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 55 | providedIn: 'root', |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 56 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 57 | export class SvgUtilService { |
| 58 | |
| 59 | constructor( |
| 60 | private fs: FnService, |
| 61 | private log: LogService |
| 62 | ) { |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 63 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 64 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 65 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 66 | this.log.debug('SvgUtilService constructed'); |
| 67 | } |
| 68 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 69 | translate(x: number[], y?: any): string { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 70 | if (this.fs.isA(x) && x.length === 2 && !y) { |
| 71 | return 'translate(' + x[0] + ',' + x[1] + ')'; |
| 72 | } |
| 73 | return 'translate(' + x + ',' + y + ')'; |
| 74 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 75 | |
| 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 Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 95 | 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 Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 99 | 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 Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 126 | dom.forEach((id, i) => { |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 127 | const x = i * 20; |
| 128 | const y = k * 20; |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 129 | g.append('circle') |
| 130 | .attr('cx', x) |
| 131 | .attr('cy', y) |
| 132 | .attr('r', 5) |
| 133 | .attr('fill', getColor(id, muted, theme)); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 134 | }); |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 135 | 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 Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 142 | g.append('text').text(theme + what) |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 143 | .attr('x', 142) |
| 144 | .attr('y', k * 20 + 2) |
| 145 | .attr('fill', 'white'); |
| 146 | // .style('font-size', '4pt'); |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return { |
| 152 | testCard: testCard, |
| 153 | getColor: getColor, |
| 154 | }; |
| 155 | } |
| 156 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 157 | } |