Skip to content

Commit 491a909

Browse files
committed
feat: add plot/vega/scale/discrete
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent a256141 commit 491a909

10 files changed

Lines changed: 566 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var DiscreteScale = require( './../lib' );
22+
23+
var scale = new DiscreteScale({
24+
'name': 'xScale',
25+
'type': 'ordinal'
26+
});
27+
28+
console.log( scale.toJSON() );
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MAIN //
22+
23+
/**
24+
* Returns a new change event object.
25+
*
26+
* @private
27+
* @param {string} property - property name
28+
* @returns {Object} event object
29+
*/
30+
function event( property ) { // eslint-disable-line stdlib/no-redeclare
31+
return {
32+
'type': 'update',
33+
'source': 'scale',
34+
'property': property
35+
};
36+
}
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = event;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Discrete scale constructor.
23+
*
24+
* @module @stdlib/plot/vega/scale/discrete
25+
*
26+
* @example
27+
* var DiscreteScale = require( '@stdlib/plot/vega/scale/discrete' );
28+
*
29+
* var scale = new DiscreteScale({
30+
* 'name': 'xScale',
31+
* 'type': 'ordinal'
32+
* });
33+
* // returns <DiscreteScale>
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable no-restricted-syntax, no-invalid-this */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var logger = require( 'debug' );
26+
var isObject = require( '@stdlib/assert/is-object' );
27+
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
28+
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
29+
var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
30+
var hasProp = require( '@stdlib/assert/has-property' );
31+
var inherit = require( '@stdlib/utils/inherit' );
32+
var transformErrorMessage = require( '@stdlib/plot/vega/base/transform-validation-message' );
33+
var instance2json = require( '@stdlib/plot/vega/base/to-json' );
34+
var Scale = require( '@stdlib/plot/vega/scale/base/ctor' );
35+
var format = require( '@stdlib/string/format' );
36+
var properties = require( './properties.json' );
37+
38+
// Note: keep the following in alphabetical order according to the `require` path...
39+
var getProperties = require( './properties/get.js' );
40+
41+
var getType = require( './type/get.js' );
42+
var setType = require( './type/set.js' );
43+
44+
45+
// VARIABLES //
46+
47+
var debug = logger( 'vega:discrete-scale:main' );
48+
49+
50+
// MAIN //
51+
52+
/**
53+
* Discrete scale constructor.
54+
*
55+
* @constructor
56+
* @param {Options} options - constructor options
57+
* @param {string} options.name - scale name
58+
* @param {string} options.type - scale type
59+
* @param {(Collection|Object|Signal)} [options.domain] - domain of associated data values
60+
* @param {number} [options.domainMax] - maximum value in the scale domain (overrides the `domain` option)
61+
* @param {number} [options.domainMin] - minimum value in the scale domain (overrides the `domain` option)
62+
* @param {number} [options.domainMid] - single mid-point value inserted into a two-element domain
63+
* @param {Collection} [options.domainRaw] - array of raw domain values which overrides the `domain` property
64+
* @param {(string|Object)} [options.interpolate] - scale range interpolation method
65+
* @param {(Collection|Object|Signal|string)} [options.range] - scale range
66+
* @param {boolean} [options.reverse=false] - boolean indicating whether to reverse the order of the scale range
67+
* @param {boolean} [options.round=false] - boolean indicating whether to round numeric output values to integers
68+
* @throws {TypeError} options argument must be an object
69+
* @throws {Error} must provide valid options
70+
* @returns {DiscreteScale} scale instance
71+
*
72+
* @example
73+
* var scale = new DiscreteScale({
74+
* 'name': 'xScale',
75+
* 'type': 'ordinal'
76+
* });
77+
* // returns <DiscreteScale>
78+
*/
79+
function DiscreteScale( options ) {
80+
var v;
81+
var k;
82+
var i;
83+
if ( !( this instanceof DiscreteScale ) ) {
84+
return new DiscreteScale( options );
85+
}
86+
if ( !isObject( options ) ) {
87+
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
88+
}
89+
// Check for required properties...
90+
if ( !hasProp( options, 'name' ) ) {
91+
throw new TypeError( 'invalid argument. Options argument must specify the scale name.' );
92+
}
93+
if ( !hasProp( options, 'type' ) ) {
94+
throw new TypeError( 'invalid argument. Options argument must specify the scale type.' );
95+
}
96+
Scale.call( this, {
97+
'name': options.name,
98+
'type': options.type
99+
});
100+
101+
// Validate provided options by attempting to assign option values to corresponding fields...
102+
for ( i = 0; i < properties.length; i++ ) {
103+
k = properties[ i ];
104+
if ( !hasProp( options, k ) ) {
105+
continue;
106+
}
107+
v = options[ k ];
108+
try {
109+
this[ k ] = v;
110+
} catch ( err ) {
111+
debug( 'Encountered an error. Error: %s', err.message );
112+
113+
// FIXME: retain thrown error type
114+
throw new Error( transformErrorMessage( err.message ) );
115+
}
116+
}
117+
return this;
118+
}
119+
120+
/*
121+
* Inherit from a parent prototype.
122+
*/
123+
inherit( DiscreteScale, Scale );
124+
125+
/**
126+
* Constructor name.
127+
*
128+
* @private
129+
* @name name
130+
* @memberof DiscreteScale
131+
* @readonly
132+
* @type {string}
133+
*/
134+
setNonEnumerableReadOnly( DiscreteScale, 'name', 'DiscreteScale' );
135+
136+
/**
137+
* Scale properties.
138+
*
139+
* @name properties
140+
* @memberof DiscreteScale.prototype
141+
* @type {Array<string>}
142+
*
143+
* @example
144+
* var scale = new DiscreteScale({
145+
* 'name': 'xScale',
146+
* 'type': 'ordinal'
147+
* });
148+
*
149+
* var v = scale.properties;
150+
* // returns [...]
151+
*/
152+
setNonEnumerableReadOnlyAccessor( DiscreteScale.prototype, 'properties', getProperties );
153+
154+
/**
155+
* Scale type.
156+
*
157+
* @name type
158+
* @memberof DiscreteScale.prototype
159+
* @type {string}
160+
*
161+
* @example
162+
* var scale = new DiscreteScale({
163+
* 'name': 'xScale',
164+
* 'type': 'ordinal'
165+
* });
166+
*
167+
* var v = scale.type;
168+
* // returns 'ordinal'
169+
*/
170+
setReadWriteAccessor( DiscreteScale.prototype, 'type', getType, setType );
171+
172+
/**
173+
* Serializes an instance to a JSON object.
174+
*
175+
* ## Notes
176+
*
177+
* - This method is implicitly invoked by `JSON.stringify`.
178+
*
179+
* @name toJSON
180+
* @memberof DiscreteScale.prototype
181+
* @type {Function}
182+
* @returns {Object} JSON object
183+
*
184+
* @example
185+
* var scale = new DiscreteScale({
186+
* 'name': 'xScale',
187+
* 'type': 'ordinal'
188+
* });
189+
*
190+
* var v = scale.toJSON();
191+
* // returns {...}
192+
*/
193+
setNonEnumerableReadOnly( DiscreteScale.prototype, 'toJSON', function toJSON() {
194+
return instance2json( this, properties );
195+
});
196+
197+
198+
// EXPORTS //
199+
200+
module.exports = DiscreteScale;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
"domain",
3+
"domainMax",
4+
"domainMin",
5+
"domainMid",
6+
"domainRaw",
7+
"interpolate",
8+
"name",
9+
"range",
10+
"reverse",
11+
"round",
12+
"type"
13+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var properties = require( './../properties.json' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the list of enumerable properties.
30+
*
31+
* @private
32+
* @returns {Array<string>} properties
33+
*/
34+
function get() {
35+
return properties.slice();
36+
}
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = get;

0 commit comments

Comments
 (0)