1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 88 | // Package of defining Vega-lite Specification's json schema 'use strict'; require('../globals'); var schema = module.exports = {}, util = require('../util'), toMap = util.toMap, colorbrewer = require('colorbrewer'); var VALID_AGG_OPS = require('vega/src/transforms/Aggregate').VALID_OPS; // TODO(#620) refer to vega schema // var vgStackSchema = require('vega/src/transforms/Stack').schema; schema.util = require('./schemautil'); schema.marktype = { type: 'string', enum: ['point', 'tick', 'bar', 'line', 'area', 'circle', 'square', 'text'] }; schema.aggregate = { type: 'string', enum: VALID_AGG_OPS, supportedEnums: { Q: VALID_AGG_OPS, O: ['median','min','max'], N: [], T: ['mean', 'median', 'min', 'max'], '': ['count'] }, supportedTypes: toMap([Q, N, O, T, '']) }; schema.getSupportedRole = function(encType) { return schema.schema.properties.encoding.properties[encType].supportedRole; }; schema.timeUnits = ['year', 'month', 'day', 'date', 'hours', 'minutes', 'seconds']; schema.defaultTimeFn = 'month'; schema.timeUnit = { type: 'string', enum: schema.timeUnits, supportedTypes: toMap([T]) }; schema.scale_type = { type: 'string', // TODO(kanitw) read vega's schema here, add description enum: ['linear', 'log', 'pow', 'sqrt', 'quantile'], default: 'linear', supportedTypes: toMap([Q]) }; schema.field = { type: 'object', properties: { name: { type: 'string' } } }; var clone = util.duplicate; var merge = schema.util.merge; schema.MAXBINS_DEFAULT = 15; var bin = { type: ['boolean', 'object'], default: false, properties: { maxbins: { type: 'integer', default: schema.MAXBINS_DEFAULT, minimum: 2, description: 'Maximum number of bins.' } }, supportedTypes: toMap([Q]) // TODO: add O after finishing #81 }; var typicalField = merge(clone(schema.field), { type: 'object', properties: { type: { type: 'string', enum: [N, O, Q, T] }, aggregate: schema.aggregate, timeUnit: schema.timeUnit, bin: bin, scale: { type: 'object', properties: { /* Common Scale Properties */ type: schema.scale_type, /* Quantitative Scale Properties */ nice: { type: 'string', enum: ['second', 'minute', 'hour', 'day', 'week', 'month', 'year'], supportedTypes: toMap([T]) }, zero: { type: 'boolean', description: 'Include zero', default: true, supportedTypes: toMap([Q, T]) }, /* Vega-lite only Properties */ useRawDomain: { type: 'boolean', default: undefined, description: 'Use the raw data range as scale domain instead of ' + 'aggregated data for aggregate axis. ' + 'This option does not work with sum or count aggregate' + 'as they might have a substantially larger scale range.' + 'By default, use value from config.useRawDomain.' } } } } }); var onlyOrdinalField = merge(clone(schema.field), { type: 'object', supportedRole: { dimension: true }, properties: { type: { type: 'string', enum: [N, O, Q, T] // ordinal-only field supports Q when bin is applied and T when time unit is applied. }, timeUnit: schema.timeUnit, bin: bin, aggregate: { type: 'string', enum: ['count'], supportedTypes: toMap([N, O]) // FIXME this looks weird to me } } }); var axisMixin = { type: 'object', supportedMarktypes: {point: true, tick: true, bar: true, line: true, area: true, circle: true, square: true}, properties: { axis: { type: 'object', properties: { /* Vega Axis Properties */ format: { type: 'string', default: undefined, // auto description: 'The formatting pattern for axis labels. '+ 'If not undefined, this will be determined by ' + 'small/largeNumberFormat and the max value ' + 'of the field.' }, grid: { type: 'boolean', default: true, description: 'A flag indicate if gridlines should be created in addition to ticks.' }, layer: { type: 'string', default: 'back', description: 'A string indicating if the axis (and any gridlines) should be placed above or below the data marks. One of "front" (default) or "back".' }, orient: { type: 'string', default: undefined, enum: ['top', 'right', 'left', 'bottom'], description: 'The orientation of the axis. One of top, bottom, left or right. The orientation can be used to further specialize the axis type (e.g., a y axis oriented for the right edge of the chart).' }, ticks: { type: 'integer', default: 5, minimum: 0, description: 'A desired number of ticks, for axes visualizing quantitative scales. The resulting number may be different so that values are "nice" (multiples of 2, 5, 10) and lie within the underlying scale\'s range.' }, /* Vega Axis Properties that are automatically populated by Vega-lite */ title: { type: 'string', default: undefined, description: 'A title for the axis. (Shows field name and its function by default.)' }, /* Vega-lite only */ maxLabelLength: { type: 'integer', default: 25, minimum: 0, description: 'Truncate labels that are too long.' }, labelAngle: { type: 'integer', default: undefined, // auto minimum: 0, maximum: 360, description: 'Angle by which to rotate labels. Set to 0 to force horizontal.' }, titleMaxLength: { type: 'integer', default: undefined, minimum: 0, description: 'Max length for axis title if the title is automatically generated from the field\'s description' }, titleOffset: { type: 'integer', default: undefined, // auto description: 'A title offset value for the axis.' }, } } } }; var sortMixin = { type: 'object', properties: { sort: { default: undefined, supportedTypes: toMap([N, O]), oneOf: [ { type: 'string', enum: ['ascending', 'descending'] }, { // sort by aggregation of another field type: 'object', required: ['field', 'op'], properties: { field: { type: 'string', description: 'The field name to aggregate over.' }, op: { type: 'string', enum: VALID_AGG_OPS, description: 'The field name to aggregate over.' }, order: { type: 'string', enum: ['ascending', 'descending'] } } } ] } } }; var bandMixin = { type: 'object', properties: { band: { type: 'object', properties: { size: { type: 'integer', minimum: 0, default: undefined }, padding: { type: 'integer', minimum: 0, default: 1 } } } } }; var legendMixin = { type: 'object', properties: { legend: { type: 'object', description: 'Properties of a legend.', properties: { title: { type: 'string', default: undefined, description: 'A title for the legend. (Shows field name and its function by default.)' }, orient: { type: 'string', default: 'right', description: 'The orientation of the legend. One of "left" or "right". This determines how the legend is positioned within the scene. The default is "right".' } } } } }; var textMixin = { type: 'object', supportedMarktypes: {'text': true}, properties: { align: { type: 'string', default: 'right' }, baseline: { type: 'string', default: 'middle' }, color: { type: 'string', role: 'color', default: '#000000' }, margin: { type: 'integer', default: 4, minimum: 0 }, placeholder: { type: 'string', default: 'Abc' }, font: { type: 'object', properties: { weight: { type: 'string', enum: ['normal', 'bold'], default: 'normal' }, size: { type: 'integer', default: 10, minimum: 0 }, family: { type: 'string', default: 'Helvetica Neue' }, style: { type: 'string', default: 'normal', enum: ['normal', 'italic'] } } }, format: { type: 'string', default: undefined, // auto description: 'The formatting pattern for text value. '+ 'If not undefined, this will be determined by ' + 'small/largeNumberFormat and the max value ' + 'of the field.' }, } }; var sizeMixin = { type: 'object', supportedMarktypes: {point: true, bar: true, circle: true, square: true, text: true}, properties: { value: { type: 'integer', default: 30, minimum: 0, description: 'Size of marks.' } } }; var colorMixin = { type: 'object', supportedMarktypes: {point: true, tick: true, bar: true, line: true, area: true, circle: true, square: true, 'text': true}, properties: { value: { type: 'string', role: 'color', default: '#4682b4', description: 'Color to be used for marks.' }, opacity: { type: 'number', default: undefined, // auto minimum: 0, maximum: 1 }, scale: { type: 'object', properties: { range: { type: ['string', 'array'], default: undefined, description: 'Color palette, if undefined vega-lite will use data property' + 'to pick one from c10palette, c20palette, or ordinalPalette.' //FIXME }, c10palette: { type: 'string', default: 'category10', enum: [ // Tableau 'category10', 'category10k', // Color Brewer 'Pastel1', 'Pastel2', 'Set1', 'Set2', 'Set3' ] }, c20palette: { type: 'string', default: 'category20', enum: ['category20', 'category20b', 'category20c'] }, ordinalPalette: { type: 'string', default: undefined, description: 'Color palette to encode ordinal variables.', enum: util.keys(colorbrewer) }, quantitativeRange: { type: 'array', default: ['#AFC6A3', '#09622A'], // tableau greens // default: ['#ccece6', '#00441b'], // BuGn.9 [2-8] description: 'Color range to encode quantitative variables.', minItems: 2, maxItems: 2, items: { type: 'string', role: 'color' } } } } } }; var stackMixin = { type: 'object', properties: { stack: { type: ['boolean', 'object'], default: true, description: 'Enable stacking (for bar and area marks only).', properties: { reverse: { type: 'boolean', default: false, description: 'Whether to reverse the stack\'s sortby.' }, offset: { type: 'string', default: undefined, enum: ['zero', 'center', 'normalize'] // TODO(#620) refer to Vega spec once it doesn't throw error // enum: vgStackSchema.properties.offset.oneOf[0].enum } } } } }; var shapeMixin = { type: 'object', supportedMarktypes: {point: true, circle: true, square: true}, properties: { value: { type: 'string', enum: ['circle', 'square', 'cross', 'diamond', 'triangle-up', 'triangle-down'], default: 'circle', description: 'Mark to be used.' }, filled: { type: 'boolean', default: false, description: 'Whether the shape\'s color should be used as fill color instead of stroke color.' } } }; var detailMixin = { type: 'object', supportedMarktypes: {point: true, tick: true, line: true, circle: true, square: true} }; var rowMixin = { properties: { height: { type: 'number', minimum: 0, default: 150 } } }; var colMixin = { properties: { width: { type: 'number', minimum: 0, default: 150 }, axis: { properties: { maxLabelLength: { type: 'integer', default: 12, minimum: 0, description: 'Truncate labels that are too long.' } } } } }; var facetMixin = { type: 'object', supportedMarktypes: {point: true, tick: true, bar: true, line: true, area: true, circle: true, square: true, text: true}, properties: { padding: { type: 'number', minimum: 0, maximum: 1, default: 0.1 } } }; var requiredNameType = { required: ['name', 'type'] }; var multiRoleField = merge(clone(typicalField), { supportedRole: { measure: true, dimension: true } }); var quantitativeField = merge(clone(typicalField), { supportedRole: { measure: true, dimension: 'ordinal-only' // using size to encoding category lead to order interpretation } }); var onlyQuantitativeField = merge(clone(typicalField), { supportedRole: { measure: true } }); var x = merge(clone(multiRoleField), axisMixin, bandMixin, requiredNameType, sortMixin); var y = clone(x); var facet = merge(clone(onlyOrdinalField), requiredNameType, facetMixin, sortMixin); var row = merge(clone(facet), axisMixin, rowMixin); var col = merge(clone(facet), axisMixin, colMixin); var size = merge(clone(quantitativeField), legendMixin, sizeMixin, sortMixin); var color = merge(clone(multiRoleField), legendMixin, colorMixin, stackMixin, sortMixin); var shape = merge(clone(onlyOrdinalField), legendMixin, shapeMixin, sortMixin); var detail = merge(clone(onlyOrdinalField), detailMixin, stackMixin, sortMixin); // we only put aggregated measure in pivot table var text = merge(clone(onlyQuantitativeField), textMixin, sortMixin); // TODO add label var data = { type: 'object', properties: { // data source formatType: { type: 'string', enum: ['json', 'csv'], default: 'json' }, url: { type: 'string', default: undefined }, values: { type: 'array', default: undefined, description: 'Pass array of objects instead of a url to a file.', items: { type: 'object', additionalProperties: true } }, // we generate a vega filter transform filter: { type: 'string', default: undefined, description: 'A string containing the filter Vega expression. Use `datum` to refer to the current data object.' }, // we generate a vega formula transform formulas: { type: 'array', default: undefined, description: 'Array of formula transforms. Formulas are applied before filter.', items: { type: 'object', properties: { field: { type: 'string', description: 'The property name in which to store the computed formula value.' }, expr: { type: 'string', description: 'A string containing an expression for the formula. Use the variable `datum` to to refer to the current data object.' } } } } } }; var config = { type: 'object', properties: { // template width: { type: 'integer', default: undefined }, height: { type: 'integer', default: undefined }, viewport: { type: 'array', items: { type: 'integer' }, default: undefined }, gridColor: { type: 'string', role: 'color', default: '#000000' }, gridOpacity: { type: 'number', minimum: 0, maximum: 1, default: 0.5 }, // filter null // TODO(#597) revise this config filterNull: { type: 'object', properties: { N: {type:'boolean', default: false}, O: {type:'boolean', default: false}, Q: {type:'boolean', default: true}, T: {type:'boolean', default: true} } }, toggleSort: { type: 'string', default: O }, autoSortLine: { type: 'boolean', default: true }, // single plot singleHeight: { // will be overwritten by bandWidth * (cardinality + padding) type: 'integer', default: 200, minimum: 0 }, singleWidth: { // will be overwritten by bandWidth * (cardinality + padding) type: 'integer', default: 200, minimum: 0 }, // band size largeBandSize: { type: 'integer', default: 21, minimum: 0 }, smallBandSize: { //small multiples or single plot with high cardinality type: 'integer', default: 12, minimum: 0 }, largeBandMaxCardinality: { type: 'integer', default: 10 }, // small multiples cellPadding: { type: 'number', default: 0.1 }, cellGridColor: { type: 'string', role: 'color', default: '#000000' }, cellGridOpacity: { type: 'number', minimum: 0, maximum: 1, default: 0.15 }, cellBackgroundColor: { type: 'string', role: 'color', default: 'rgba(0,0,0,0)' }, textCellWidth: { type: 'integer', default: 90, minimum: 0 }, // marks strokeWidth: { type: 'integer', default: 2, minimum: 0 }, singleBarOffset: { type: 'integer', default: 5, minimum: 0 }, // scales timeScaleLabelLength: { type: 'integer', default: 3, minimum: 0, description: 'Max length for values in dayScaleLabel and monthScaleLabel. Zero means using full names in dayScaleLabel/monthScaleLabel.' }, dayScaleLabel: { type: 'array', items: { type: 'string' }, default: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], description: 'Axis labels for day of week, starting from Sunday.' + '(Consistent with Javascript -- See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay.' }, monthScaleLabel: { type: 'array', items: { type: 'string' }, default: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], description: 'Axis labels for month.' }, // other characterWidth: { type: 'integer', default: 6 }, maxSmallNumber: { type: 'number', default: 10000, description: 'maximum number that a field will be considered smallNumber.'+ 'Used for axis labelling.' }, smallNumberFormat: { type: 'string', default: '', description: 'D3 Number format for axis labels and text tables '+ 'for number <= maxSmallNumber. Used for axis labelling.' }, largeNumberFormat: { type: 'string', default: '.3s', description: 'D3 Number format for axis labels and text tables ' + 'for number > maxSmallNumber.' }, timeFormat: { type: 'string', default: '%Y-%m-%d', description: 'Date format for axis labels.' }, useRawDomain: { type: 'boolean', default: false, description: 'Use the raw data range as scale domain instead of ' + 'aggregated data for aggregate axis. ' + 'This option does not work with sum or count aggregate' + 'as they might have a substantially larger scale range.' + 'By default, use value from config.useRawDomain.' } } }; /** @type Object Schema of a vega-lite specification */ schema.schema = { $schema: 'http://json-schema.org/draft-04/schema#', description: 'Schema for Vega-lite specification', type: 'object', required: ['marktype', 'encoding', 'data'], properties: { data: data, marktype: schema.marktype, encoding: { type: 'object', properties: { x: x, y: y, row: row, col: col, size: size, color: color, shape: shape, text: text, detail: detail } }, config: config } }; schema.encTypes = util.keys(schema.schema.properties.encoding.properties); /** Instantiate a verbose vl spec from the schema */ schema.instantiate = function() { return schema.util.instantiate(schema.schema); }; |