├── .gitignore ├── samples ├── js │ ├── VizGrammarSettings.js │ └── topojson.js ├── css │ ├── VizGrammar.css │ ├── axes.css │ └── custom.css ├── charts │ ├── number │ │ ├── number.css │ │ └── index.html │ ├── scatter │ │ └── index.html │ ├── table │ │ └── index.html │ ├── line │ │ └── index.html │ ├── map │ │ └── index.html │ ├── area │ │ └── index.html │ ├── arc │ │ └── index.html │ └── bar │ │ └── index.html └── jsonMapFiles │ └── usaInfo.json ├── gh-deploy.bat ├── gh-deploy.sh ├── charts ├── chart.core.js ├── chart.number.js ├── custom.stack.js ├── chart.table.js ├── chart.line.js ├── chart.area.js ├── chart.scatter.js ├── chart.arc.js ├── chart.map.js └── chart.bar.js ├── package.json ├── issue_template.md ├── GruntFile.js ├── pull_request_template.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /samples/js/VizGrammarSettings.js: -------------------------------------------------------------------------------- 1 | var vizgSettings = { 2 | colorScale: ['#058DC7', '#50B432', '#DA524A', '#E08437'] 3 | } -------------------------------------------------------------------------------- /samples/css/VizGrammar.css: -------------------------------------------------------------------------------- 1 | .tooltipClass { 2 | background-color: #e5f2ff; 3 | padding: 4px 8px; 4 | 5 | } 6 | .tooltipClass p{ 7 | margin: 0; 8 | padding: 0; 9 | 10 | } -------------------------------------------------------------------------------- /samples/css/axes.css: -------------------------------------------------------------------------------- 1 | body svg.marks .mark-group .background { 2 | fill: #121822 !important; 3 | } 4 | 5 | body svg.marks .mark-group .mark-text text { 6 | fill: #7D8596 !important; 7 | } 8 | 9 | body svg.marks .mark-group .mark-rule line { 10 | stroke: #E3E5E6 !important; 11 | } -------------------------------------------------------------------------------- /samples/charts/number/number.css: -------------------------------------------------------------------------------- 1 | p { 2 | margin: 0; 3 | display: block; 4 | text-align: center; 5 | font-weight: bold; 6 | } 7 | 8 | .titledChartNumberTorqueContent { 9 | color:steelblue; 10 | font-size:18px; 11 | font-weight: normal; 12 | padding: 10px 0; 13 | } 14 | 15 | .valdChartNumberTorqueContent { 16 | color:#438CAD; 17 | font-size:50px; 18 | font-weight: normal; 19 | } 20 | 21 | 22 | .diffdChartNumberTorqueContent { 23 | color:#438CAD; 24 | font-size:14px; 25 | } 26 | 27 | .diffPercentagedChartNumberTorqueContent { 28 | color:#ff944d; 29 | font-size:14px; 30 | } -------------------------------------------------------------------------------- /gh-deploy.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | title Deploy Samples to gh-pages 3 | 4 | echo "samples" folder contains will deploy to gh-pages branch 5 | echo ======================================================= 6 | echo please commit all the changes before continue 7 | echo. 8 | 9 | SET /P ANSWER=Do you want to continue (Y/N)? 10 | 11 | if /i {%ANSWER%}=={y} (goto :yes) 12 | if /i {%ANSWER%}=={yes} (goto :yes) 13 | goto :no 14 | 15 | :yes 16 | echo. 17 | echo Deploying..... 18 | echo. 19 | git subtree push --prefix samples origin gh-pages 20 | echo. 21 | echo Finished! 22 | pause 23 | exit /b 0 24 | 25 | :no 26 | echo. 27 | pause 28 | exit /b 1 -------------------------------------------------------------------------------- /gh-deploy.sh: -------------------------------------------------------------------------------- 1 | #/bin/sh 2 | echo 3 | echo ========================================================= 4 | echo "samples" folder contains will deploy to gh-pages branch 5 | echo please commit all the changes before continue 6 | echo ========================================================= 7 | echo 8 | echo -n "Do you want to continue [Y/N]?" 9 | old_stty_cfg=$(stty -g) 10 | stty raw -echo 11 | answer=$( while ! head -c 1 | grep -i '[ny]' ;do true ;done ) 12 | stty $old_stty_cfg 13 | if echo "$answer" | grep -iq "^y" ;then 14 | echo 15 | echo Deploying..... 16 | echo 17 | git pull 18 | git subtree push --prefix samples origin gh-pages 19 | echo 20 | echo Finished! 21 | sleep 22 | else 23 | sleep 24 | fi 25 | exit -------------------------------------------------------------------------------- /charts/chart.core.js: -------------------------------------------------------------------------------- 1 | var vizg = function(dataTable, config) { 2 | dataTable = buildTable(dataTable); 3 | if (typeof config.charts !== "undefined" && config.charts.length == 1) { 4 | //Set chart config properties for main 5 | for (var property in config.charts[0]) { 6 | if (config.charts[0].hasOwnProperty(property)) { 7 | config[property] = config.charts[0][property]; 8 | } 9 | } 10 | 11 | this.chart = new window[config.type]([dataTable], config); 12 | } 13 | }; 14 | 15 | vizg.prototype.draw = function(div, callback) { 16 | this.chart.draw(div, callback); 17 | }; 18 | 19 | vizg.prototype.insert = function(data) { 20 | this.chart.insert(buildData(data, this.chart.metadata)); 21 | }; 22 | 23 | vizg.prototype.getSpec = function() { 24 | return this.chart.getSpec(); 25 | }; -------------------------------------------------------------------------------- /samples/jsonMapFiles/usaInfo.json: -------------------------------------------------------------------------------- 1 | {"Alabama":"01","Alaska":"02","Arizona":"04","Arkansas":"05","California":"06","Colorado":"08","Connecticut":"09","Delaware":"10","District of Columbia":"11","Florida":"12","Geogia":"13","Hawaii":"15","Idaho":"16","Illinois":"17","Indiana":"18","Iowa":"19","Kansas":"20","Kentucky":"21","Louisiana":"22","Maine":"23","Maryland":"24","Massachusetts":"25","Michigan":"26","Minnesota":"27","Mississippi":"28","Missouri":"29","Montana":"30","Nebraska":"31","Nevada":"32","New Hampshire":"33","New Jersey":"34","New Mexico":"35","New York":"36","North Carolina":"37","North Dakota":"38","Ohio":"39","Oklahoma":"40","Oregon":"41","Pennsylvania":"42","Rhode Island":"44","South Carolina":"45","South Dakota":"46","Tennessee":"47","Texas":"48","Utah":"49","Vermont":"50","Virginia":"51","Washington":"53","West Virginia":"54","Wisconsin":"55","Wyoming":"56"} -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "VizGrammar", 3 | "version": "2.0.0", 4 | "description": "Visualization library for Javascript that is based on d3", 5 | "keywords": [ 6 | "vizg", 7 | "vizGrammar", 8 | "wso2", 9 | "visualization", 10 | "interaction", 11 | "grammar", 12 | "data", 13 | "d3" 14 | ], 15 | "author": { 16 | "name": "WSO2", 17 | "url": "http://wso2.com/" 18 | }, 19 | "contributors": [ 20 | { 21 | "name": "Tharik Kanaka", 22 | "url": "http://github.com/keizer619" 23 | } 24 | ], 25 | "license": "Apache-2.0", 26 | "repository": { 27 | "type": "git", 28 | "url": "http://github.com/wso2/VizGrammar/" 29 | }, 30 | "devDependencies": { 31 | "grunt": "~0.4.5", 32 | "grunt-contrib-uglify": "~0.5.0", 33 | "grunt-contrib-concat": "~0.5.1", 34 | "grunt-contrib-copy":"~0.8.2" 35 | } 36 | } -------------------------------------------------------------------------------- /issue_template.md: -------------------------------------------------------------------------------- 1 | **Description:** 2 | 3 | 4 | **Suggested Labels:** 5 | 6 | 7 | **Suggested Assignees:** 8 | 9 | 10 | **Affected Product Version:** 11 | 12 | **OS, DB, other environment details and versions:** 13 | 14 | **Steps to reproduce:** 15 | 16 | 17 | **Related Issues:** 18 | -------------------------------------------------------------------------------- /GruntFile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 3 | var wso2banner = '/*\n * Copyright (c) <%= grunt.template.today("yyyy") %>, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.\n *' 4 | +'\n * Licensed under the Apache License, Version 2.0 (the "License");\n' 5 | +' * you may not use this file except in compliance with the License./\n' 6 | +' * You may obtain a copy of the License at\n *\n *' 7 | +' http://www.apache.org/licenses/LICENSE-2.0\n *\n' 8 | +' * Unless required by applicable law or agreed to in writing, software\n' 9 | +' * distributed under the License is distributed on an "AS IS" BASIS,\n' 10 | +' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n' 11 | +' * See the License for the specific language governing permissions and\n' 12 | +' * limitations under the License.\n */\n'; 13 | 14 | grunt.initConfig({ 15 | pkg: grunt.file.readJSON('package.json'), 16 | concat: { 17 | options: { 18 | banner: wso2banner, 19 | separator: ';' 20 | }, 21 | dist: { 22 | src: ['charts/*.js'], 23 | dest: 'dist/<%= pkg.name %>.js' 24 | } 25 | }, 26 | uglify: { 27 | options: { 28 | banner: wso2banner 29 | }, 30 | dist: { 31 | files: { 32 | 'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>'] 33 | } 34 | } 35 | }, 36 | copy: { 37 | main: { 38 | files: [ 39 | { expand: true, cwd: 'dist', src: ['VizGrammar.min.js'], dest: 'samples/js/' } 40 | ], 41 | }, 42 | } 43 | }); 44 | 45 | grunt.loadNpmTasks('grunt-contrib-uglify'); 46 | grunt.loadNpmTasks('grunt-contrib-concat'); 47 | grunt.loadNpmTasks('grunt-contrib-copy'); 48 | grunt.registerTask('default', ['concat', 'uglify', 'copy']); 49 | 50 | }; -------------------------------------------------------------------------------- /charts/chart.number.js: -------------------------------------------------------------------------------- 1 | 2 | var number = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | this.data = dataTable[0].values 5 | var marks =[]; 6 | this.spec = {}; 7 | 8 | config = checkConfig(config, this.metadata); 9 | this.config = config; 10 | dataTable[0].name= config.title; 11 | 12 | }; 13 | 14 | number.prototype.draw = function(div) { 15 | div = div.replace("#",""); 16 | var contentId = div+"Content"; 17 | var textContent = ""; 18 | 19 | if (this.data != null && this.data.length != 0) { 20 | textContent = this.data[this.data.length - 1][this.metadata.names[this.config.x]]; 21 | } 22 | 23 | var divContent = "

" + this.config.title+ "

" 24 | + "

" + textContent + "

" 25 | + "

0

" 26 | + "

0%

"; 27 | 28 | document.getElementById(div).innerHTML = divContent; 29 | this.view = contentId; 30 | }; 31 | 32 | number.prototype.insert = function(data) { 33 | var current = data[data.length-1][this.metadata.names[this.config.x]]; 34 | var previous = document.getElementById("val" + this.view).innerHTML; 35 | var difference = current - previous; 36 | var diffPercentage; 37 | var sign = ""; 38 | 39 | if (previous != "" && previous != 0) { 40 | diffPercentage = (difference / previous).toFixed(2) * 100 + "%"; 41 | } else { 42 | diffPercentage = ""; 43 | } 44 | 45 | if (difference > 0) { 46 | sign = "+"; 47 | } 48 | 49 | document.getElementById("diffPercentage" + this.view).innerHTML = sign + diffPercentage; 50 | document.getElementById("diff" + this.view).innerHTML = sign + difference; 51 | document.getElementById("val" + this.view).innerHTML = current; 52 | 53 | }; 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /charts/custom.stack.js: -------------------------------------------------------------------------------- 1 | 2 | var stack = function(dataTable, config) { 3 | this.barChart = new bar(dataTable, config); 4 | this.metadata = this.barChart.metadata; 5 | 6 | var spec = this.barChart.getSpec(); 7 | 8 | spec.axes = [spec.axes[0]]; 9 | spec.axes[0].grid = false; 10 | 11 | spec.data.push( 12 | { 13 | "name": "selectedPoints", 14 | "modify": [ 15 | {"type": "clear", "test": "!multi"}, 16 | {"type": "toggle", "signal": "clickedPoint", "field": "id"} 17 | ] 18 | }); 19 | 20 | spec.signals.push( { 21 | "name": "clickedPoint", 22 | "init": 0, 23 | "verbose": true, 24 | "streams": [{"type": "click", "expr": "datum._id"}] 25 | }, 26 | { 27 | "name": "multi", 28 | "init": false, 29 | "verbose": true, 30 | "streams": [{"type": "click", "expr": "datum._id"}] 31 | }); 32 | 33 | var textMark = JSON.parse(JSON.stringify(spec.marks[0])); 34 | textMark.type = "text"; 35 | textMark.properties.update.text = {"field" :spec.marks[0].properties.update.fill.field}; 36 | textMark.properties.update.x.offset = 10; 37 | textMark.properties.update.y.offset = -5; 38 | textMark.properties.update.fill = {"value": config.legendTitleColor}; 39 | delete textMark.properties.update.y2; 40 | delete textMark.properties.hover; 41 | spec.marks.push(textMark); 42 | 43 | delete spec.marks[0].properties.hover; 44 | spec.marks[0].properties.update.fill = [ 45 | { 46 | "test": "indata('selectedPoints', datum._id, 'id')", 47 | "value": config.selectionColor 48 | },spec.marks[0].properties.update.fill 49 | ]; 50 | 51 | this.barChart.setSpec(spec); 52 | 53 | }; 54 | 55 | stack.prototype.draw = function(div, callbacks) { 56 | this.barChart.draw(div, callbacks); 57 | }; 58 | 59 | stack.prototype.insert = function(data) { 60 | this.barChart.insert(data); 61 | }; 62 | 63 | stack.prototype.getSpec = function() { 64 | return this.barChart.getSpec(); 65 | }; 66 | 67 | -------------------------------------------------------------------------------- /pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Purpose 2 | > Describe the problems, issues, or needs driving this feature/fix and include links to related issues in the following format: Resolves issue1, issue2, etc. 3 | 4 | ## Goals 5 | > Describe the solutions that this feature/fix will introduce to resolve the problems described above 6 | 7 | ## Approach 8 | > Describe how you are implementing the solutions. Include an animated GIF or screenshot if the change affects the UI (email documentation@wso2.com to review all UI text). Include a link to a Markdown file or Google doc if the feature write-up is too long to paste here. 9 | 10 | ## User stories 11 | > Summary of user stories addressed by this change> 12 | 13 | ## Release note 14 | > Brief description of the new feature or bug fix as it will appear in the release notes 15 | 16 | ## Documentation 17 | > Link(s) to product documentation that addresses the changes of this PR. If no doc impact, enter “N/A” plus brief explanation of why there’s no doc impact 18 | 19 | ## Training 20 | > Link to the PR for changes to the training content in https://github.com/wso2/WSO2-Training, if applicable 21 | 22 | ## Certification 23 | > Type “Sent” when you have provided new/updated certification questions, plus four answers for each question (correct answer highlighted in bold), based on this change. Certification questions/answers should be sent to certification@wso2.com and NOT pasted in this PR. If there is no impact on certification exams, type “N/A” and explain why. 24 | 25 | ## Marketing 26 | > Link to drafts of marketing content that will describe and promote this feature, including product page changes, technical articles, blog posts, videos, etc., if applicable 27 | 28 | ## Automation tests 29 | - Unit tests 30 | > Code coverage information 31 | - Integration tests 32 | > Details about the test cases and coverage 33 | 34 | ## Security checks 35 | - Followed secure coding standards in http://wso2.com/technical-reports/wso2-secure-engineering-guidelines? yes/no 36 | - Ran FindSecurityBugs plugin and verified report? yes/no 37 | - Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets? yes/no 38 | 39 | ## Samples 40 | > Provide high-level details about the samples related to this feature 41 | 42 | ## Related PRs 43 | > List any other related PRs 44 | 45 | ## Migrations (if applicable) 46 | > Describe migration steps and platforms on which migration has been tested 47 | 48 | ## Test environment 49 | > List all JDK versions, operating systems, databases, and browser/versions on which this feature/fix was tested 50 | 51 | ## Learning 52 | > Describe the research phase and any blog posts, patterns, libraries, or add-ons you used to solve the problem. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##Overview 2 | VizGrammar is a wrapper around powerful d3.js and vega.js library. It makes charting easy by adding required boilerplate code so that developers/designers can get started in few minutes. 3 | 4 | A chart can be drawn in a given location by simply calling 5 | ```javascript 6 | var chart = new vizg(data, configSingle); 7 | chart.draw("#chartDiv"); 8 | ``` 9 | where 10 | 11 | - canvas is the div element which contains the gadget, 12 | - config is the additional gadget configurations specified as a JSON object 13 | - dataTable is the JSON object that holds the dataset in a tabular format 14 | 15 | ## Build Process 16 | 17 | To manually build VizGrammar, you need to have [npm](https://www.npmjs.com/) and [grunt](http://gruntjs.com/) installed. 18 | 19 | 1. Run `npm install` in the VizGrammar directory to install dependencies. 20 | 2. Run `grunt`, this will combine JS files and do the minification. 21 | 22 | ##Getting Started 23 | Download [d3.js](http://d3js.org/ "d3js.org") and [vega.js](http://vega.github.io/ "vega.github.io"). 24 | Download the latest vizg.js. 25 | 26 | ##Documentation 27 | [VizGrammar Documentation](https://github.com/wso2/VizGrammar/wiki) 28 | 29 | ##Data table 30 | VizGrammar required you to arrange your source dataset in a tabular way similar to follwing JSON format. 31 | ```javascript 32 | { 33 | "metadata":{ 34 | "names":["Column1","Column2",...], 35 | "types":['ordinal', 'linear',] 36 | }, 37 | "data": [ 38 | ["value1",numericValue1,...], 39 | ["value2",numericValue2,...], 40 | ] 41 | } 42 | ``` 43 | 44 | Sample data table would be like following: 45 | ```javascript 46 | var data = [ 47 | { 48 | "metadata" : { 49 | "names" : ["rpm","torque","horsepower", "EngineType"], 50 | "types" : ["linear","linear", "ordinal","ordinal"] 51 | }, 52 | "data": [ 53 | [8000, 75, 120, "Piston"], [9000, 81, 130, "Rotary"]] 54 | } 55 | ]; 56 | ``` 57 | 58 | 59 | metadata.names is an array consists of column names/fields of the table where metadata.types records their types (ordinal (C) or linear). 60 | names and types are aligned together in a way that "Coulmn1" => 'ordinal' and "Coulmn2" => 'linear' and so on. 61 | 62 | data section is a collection of arrays of data rows. Single row is stored as an array and their element order follows the order of metadata.names. 63 | 64 | 65 | ##Chart Config 66 | 67 | Once the data structure is ready, igviz will try to draw a table out of it. 68 | 69 | Users can add additional parameters to the gadget using config object. It is a JSON object that has well known configuration properties. For example, default table implementation can be changed to a bar chart by setting chartType property in the config object. 70 | 71 | E.g Following is the bare minimum set of configurations require to draw a bar chart from above tabular data format. 72 | ```javascript 73 | var config = { 74 | x : "rpm", 75 | charts : [{type: "line", y : "torque", color: "EngineType"}], 76 | maxLength: 10, 77 | width: 400, 78 | height: 200 79 | } 80 | ``` 81 | 82 | ##Chart Canvas 83 | 84 | This is the div element where chart will be renedered on. VizGrammar accepts the id attribute of a div element formatted as "#chartDiv" 85 | ```javascript 86 | var chart = new vizg(data, configSingle); 87 | chart.draw("#chartDiv"); 88 | ``` 89 | 90 | ##Plot Chart 91 | 92 | Drawring a chart means simply calling 93 | ```javascript 94 | chart.insert([[8000, 74, 120, "Rotary"]]); 95 | ``` 96 | with parameters. VizGrammar currently supports six chart types including table, bar,line, scatter and map. Single number chart and an area chart is still in development. Drill down capabilities will be added to bar chart later. 97 | 98 | Visit [VizGrammar Samples Web Site](http://dunithd.github.io/igviz-site/samples/index.html) to see sample chart types and their documentation. Please note that the documentation is still in progress :) 99 | 100 | You can also follow the samples folder so that you'll get a better idea. 101 | 102 | 103 | -------------------------------------------------------------------------------- /samples/css/custom.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: #1E2531; 3 | color: #fff; 4 | padding-top: 80px; 5 | } 6 | 7 | body.widget { 8 | background: transparent; 9 | } 10 | 11 | .jumbotron{ 12 | background-color: #131820 !important; 13 | color: #636C7C !important; 14 | } 15 | 16 | 17 | .col-1, 18 | .col-2, 19 | .col-3, 20 | .col-4, 21 | .col-5, 22 | .panel, 23 | .panel-heading, 24 | .col-8, 25 | .col-9, 26 | .col-10, 27 | .col-11, 28 | .col-12 { 29 | background-color: #1E2531 !important; 30 | color: #636C7C !important; 31 | 32 | } 33 | 34 | .col-1 { 35 | width: 8.33333333%; 36 | } 37 | 38 | .col-2 { 39 | width: 16.66666667%; 40 | } 41 | 42 | .col-3 { 43 | width: 25%; 44 | } 45 | 46 | .col-4 { 47 | width: 33.33333333%; 48 | } 49 | 50 | .col-5 { 51 | width: 41.66666667%; 52 | } 53 | 54 | .col-6 { 55 | width: 50%; 56 | } 57 | 58 | .col-7 { 59 | width: 58.33333333%; 60 | } 61 | 62 | .col-8 { 63 | width: 66.66666667%; 64 | } 65 | 66 | .col-9 { 67 | width: 75%; 68 | } 69 | 70 | .col-10 { 71 | width: 83.33333333%; 72 | } 73 | 74 | .col-11 { 75 | width: 91.66666667%; 76 | } 77 | 78 | .col-12 { 79 | width: 100%; 80 | } 81 | 82 | .status-message { 83 | display: block; 84 | height: 100%; 85 | width: 100%; 86 | } 87 | 88 | .status-message > .message-info, 89 | .status-message > .message-warning, 90 | .status-message > .message-danger, 91 | .status-message > .message-success { 92 | position: fixed; 93 | z-index: 2; 94 | width: 100%; 95 | height: 100%; 96 | top: 50%; 97 | left: 50%; 98 | margin: 0; 99 | -webkit-transform: translate(-50%, -50%); 100 | -moz-transform: translate(-50%, -50%); 101 | -ms-transform: translate(-50%, -50%); 102 | -o-transform: translate(-50%, -50%); 103 | transform: translate(-50%, -50%); 104 | } 105 | 106 | /* Dark Theme Styles */ 107 | body.dark .tt-hint { 108 | background: transparent !important; 109 | } 110 | 111 | body.dark .tt-input { 112 | border: 1px solid #6B7992 !important; 113 | color: #E3E5E6; 114 | } 115 | 116 | body.dark .form-control-btn { 117 | color: #E3E5E6; 118 | } 119 | 120 | body.dark #date-select .btn-default { 121 | background: #636C7C; 122 | color: #fff; 123 | border-color: #353C48; 124 | } 125 | 126 | body.dark #date-select .btn-default:hover, body.dark #date-select .btn-default[aria-expanded=true] { 127 | background: #1E2531; 128 | } 129 | 130 | #date-select .btn-default[aria-expanded=true] { 131 | background: #e6e6e6; 132 | } 133 | 134 | body.dark #date-select .btn-default.active, body.dark #date-select .btn-default.active:hover { 135 | background: #438CAD; 136 | border-color: #353C48; 137 | } 138 | 139 | body.dark .stats { 140 | color: #E3E5E6 !important; 141 | } 142 | 143 | body.dark .message { 144 | background: #1E2531; 145 | color: #E3E5E6 !important; 146 | } 147 | 148 | .breadcrumb { 149 | background: #1E2531; 150 | } 151 | 152 | .breadcrumb a:visited { 153 | color: #E3E5E6; 154 | } 155 | 156 | #canvas-tooltip { 157 | background: #c4c4c4 !important; 158 | color: #000; 159 | opacity: 0.9; 160 | font-size: 12px; 161 | } 162 | 163 | body.dark #canvas-tooltip { 164 | background: #000 !important; 165 | color: #fff; 166 | } 167 | 168 | /* DataTables issue fixing */ 169 | .table { 170 | font-size: 14px; 171 | } 172 | 173 | div.dataTables_length select { 174 | background: transparent; 175 | } 176 | 177 | body.dark div.dataTables_length select { 178 | color: #fff; 179 | border-color: #353C48; 180 | } 181 | 182 | .dataTables_filter input { 183 | margin: 0 0 0 14px !important; 184 | background: transparent; 185 | padding: 0 5px; 186 | border: 1px solid #747F8F; 187 | } 188 | 189 | div.dataTables_length select option { 190 | color: #31383e; 191 | } 192 | 193 | div.dataTables_processing { 194 | background: transparent !important; 195 | padding-top: 30px !important; 196 | } 197 | 198 | .dataTablesBottom { 199 | margin: 20px 0 0 0; 200 | } 201 | 202 | .pagination > .active > a, 203 | .pagination > .active > a:focus, 204 | .pagination > .active > a:hover, 205 | .pagination > .active > span, .pagination > 206 | .active > span:focus, 207 | .pagination > .active > span:hover { 208 | background: #438CAD; 209 | border-color: #438CAD; 210 | } 211 | 212 | .pagination > li > a, 213 | .pagination > li > span { 214 | background: transparent; 215 | color: #353C48; 216 | } 217 | 218 | body.dark .pagination > li > a, 219 | body.dark .pagination > li > a:focus, 220 | body.dark .pagination > li > span, 221 | body.dark .pagination > li.active > span:hover, 222 | body.dark .pagination > li.active > a:hover { 223 | color: #E3E5E6; 224 | border-color: #353C48; 225 | } 226 | 227 | .pagination > li > span:hover, 228 | .pagination > li > a:hover { 229 | color: #353C48; 230 | } 231 | 232 | body[media-screen=xs] .hidden-xs, 233 | body[media-screen=sm] .hidden-sm, 234 | body[media-screen=md] .hidden-md, 235 | body[media-screen=lg] .hidden-lg { 236 | display: none !important; 237 | } 238 | 239 | body[media-screen=xs] .visible-xs, 240 | body[media-screen=sm] .visible-sm, 241 | body[media-screen=md] .visible-md, 242 | body[media-screen=lg] .visible-lg { 243 | display: block !important; 244 | } 245 | 246 | body[media-screen=xs] .visible-inline-xs, 247 | body[media-screen=sm] .visible-inline-sm, 248 | body[media-screen=md] .visible-inline-md, 249 | body[media-screen=lg] .visible-inline-lg { 250 | display: inline !important; 251 | } 252 | 253 | body[media-screen=xs] .visible-inline-block-xs, 254 | body[media-screen=sm] .visible-inline-block-sm, 255 | body[media-screen=md] .visible-inline-block-md, 256 | body[media-screen=lg] .visible-inline-block-lg { 257 | display: inline-block !important; 258 | } -------------------------------------------------------------------------------- /charts/chart.table.js: -------------------------------------------------------------------------------- 1 | 2 | var table = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | this.data = dataTable[0].values 5 | var marks =[]; 6 | this.spec = {}; 7 | config = checkConfig(config, this.metadata); 8 | this.config = config; 9 | dataTable[0].name= config.title; 10 | 11 | if (this.config.columnTitles == null) { 12 | this.config.columnTitles = this.config.columns; 13 | } 14 | 15 | }; 16 | 17 | table.prototype.draw = function(div) { 18 | var table = d3.select(div).append("table") 19 | .attr( "class", "table table-bordered") 20 | .attr("id", this.config.title); 21 | 22 | // set up the table header 23 | table.append('thead').attr("align", "center") 24 | .append('tr') 25 | .selectAll('th') 26 | .data(this.config.columnTitles) 27 | .enter() 28 | .append('th') 29 | .html(function (d) { return d }); 30 | 31 | table.append('tbody').attr("id", "tableChart-"+this.config.title); 32 | this.setupData(this.data, this.config); 33 | 34 | table.selectAll("thead th") 35 | .html(function(column) { 36 | return column.charAt(0).toUpperCase() + column.substr(1); 37 | }); 38 | 39 | 40 | }; 41 | 42 | table.prototype.insert = function(data) { 43 | this.setupData(data, this.config); 44 | }; 45 | 46 | 47 | table.prototype.setupData = function (dataset, config) { 48 | var data = []; 49 | var allColumns = this.metadata.names; 50 | 51 | //Select specified columns from dataset 52 | for (var i = 0; i < dataset.length; i++) { 53 | var row = {}; 54 | 55 | for (var x = 0; x < config.columns.length; x++) { 56 | row[config.columns[x]] = dataset[i][config.columns[x]]; 57 | } 58 | data.push(row); 59 | } 60 | 61 | //Select Rows by x Axis 62 | var rows = d3.select('#tableChart-'+config.title) 63 | .selectAll('tr') 64 | .data(data, function(d) { return d[config.key]}) 65 | 66 | var entertd = rows.enter() 67 | .append('tr') 68 | .selectAll('td') 69 | .data(function(row) { 70 | return config.columns.map(function(column) { 71 | return {column: column, value: row[column]}; 72 | }); 73 | }) 74 | .enter() 75 | .append('td') 76 | 77 | //Color cell background 78 | if (config.color != -1) { 79 | d3.select('#tableChart-'+config.title) 80 | .selectAll('td') 81 | .attr('bgcolor', 82 | function(d) { 83 | var column = d.key || d.column; 84 | 85 | var color; 86 | if (typeof config.colorScale == "string") { 87 | color = window["d3"]["scale"][config.colorScale]().range(); 88 | } else { 89 | color = config.colorScale; 90 | } 91 | 92 | var colorIndex; 93 | for(var i = 0; i < allColumns.length; i += 1) { 94 | if(allColumns[i] === column) { 95 | colorIndex = i; 96 | } 97 | } 98 | 99 | if (typeof d.value == "string" && (config.color == "*" || column == allColumns[config.color])) { 100 | 101 | var colorDomain; 102 | 103 | if (config.colorDomain == -1) { 104 | colorDomain = [d3.min(d3.select('#tableChart-'+config.title) .selectAll('tr') .data(), function(d) { return d[column]; }), 105 | d3.max(d3.select('#tableChart-'+config.title) .selectAll('tr') .data(), function(d) { return d[column]; })] 106 | 107 | } else { 108 | colorDomain = config.colorDomain 109 | } 110 | 111 | var color; 112 | if (typeof config.colorScale == "string") { 113 | color = window["d3"]["scale"][config.colorScale]().range(); 114 | } else { 115 | color = config.colorScale; 116 | } 117 | 118 | var colorScale = d3.scale.ordinal() 119 | .range(color) 120 | .domain(colorDomain); 121 | return colorScale(d.value); 122 | 123 | } else if (config.color == "*" || column == allColumns[config.color]){ 124 | 125 | var colorScale = d3.scale.linear() 126 | .range(['#f2f2f2', color[colorIndex]]) 127 | .domain([d3.min(d3.select('#tableChart-'+config.title) .selectAll('tr') .data(), function(d) { return d[column]; }), 128 | d3.max(d3.select('#tableChart-'+config.title) .selectAll('tr') .data(), function(d) { return d[column]; })] 129 | ); 130 | 131 | return colorScale(d.value); 132 | } 133 | 134 | }); 135 | } 136 | 137 | 138 | 139 | entertd.append('span') 140 | var td = rows.selectAll('td') 141 | .style({"padding": "0px 10px 0px 10px"}) 142 | 143 | .data(function(d) { return d3.map(d).entries() }) 144 | .attr('class', function (d) { return d.key }) 145 | 146 | 147 | 148 | 149 | td.select('span') 150 | .html(function(d) { 151 | return d.value 152 | }) 153 | //Remove data items when it hits maxLength 154 | if (config.maxLength != -1 && d3.select('tbody').selectAll('tr').data().length > config.maxLength) { 155 | var allowedDataset = d3.select('tbody').selectAll('tr').data().slice(d3.select('tbody').selectAll('tr').data().length- config.maxLength, config.maxLength); 156 | d3.select('tbody').selectAll('tr').data(allowedDataset, 157 | function(d) { 158 | return(d); 159 | }) 160 | .remove(); 161 | } 162 | } -------------------------------------------------------------------------------- /charts/chart.line.js: -------------------------------------------------------------------------------- 1 | 2 | var line = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | var marks =[]; 5 | var signals = []; 6 | this.spec = {}; 7 | 8 | config = checkConfig(config, this.metadata); 9 | this.config = config; 10 | dataTable[0].name= config.title; 11 | 12 | var scales = getXYScales(config, this.metadata); 13 | 14 | if (config.mode == "stack") { 15 | var aggregateData = { 16 | "name": "stack", 17 | "source": config.title, 18 | "transform": [ 19 | { 20 | "type": "aggregate", 21 | "groupby": [this.metadata.names[config.x]], 22 | "summarize": [{"field": this.metadata.names[config.y], "ops": ["sum"]}] 23 | } 24 | ] 25 | }; 26 | 27 | dataTable.push(aggregateData); 28 | yColumn = "sum_"+ this.metadata.names[config.y]; 29 | yDomain = "stack"; 30 | 31 | scales[1].domain = {"data": yDomain, "field": yColumn}; 32 | 33 | } 34 | 35 | 36 | 37 | if (config.color != -1) { 38 | 39 | if (config.colorDomain == -1) { 40 | config.colorDomain = {"data": config.title, "field": this.metadata.names[config.color]}; 41 | } 42 | 43 | var colorScale = { 44 | "name": "color", 45 | "type": "ordinal", 46 | "domain": config.colorDomain, 47 | "range": config.colorScale 48 | }; 49 | scales.push(colorScale); 50 | 51 | var legendTitle = "Legend"; 52 | 53 | if (config.title != "table") { 54 | legendTitle = config.title; 55 | } 56 | 57 | if (this.config.legend) { 58 | this.spec.legends = getLegend(this.config); 59 | } 60 | } 61 | 62 | 63 | var axes = getXYAxes(config, "x", "x", "y", "y"); 64 | 65 | 66 | marks.push(getLineMark(config, this.metadata)); 67 | config.fillOpacity = 0; 68 | config.markSize = 1000; 69 | marks.push(getSymbolMark(config, this.metadata)); 70 | config.fillOpacity = 1; 71 | config.markSize = 15; 72 | marks.push(getSymbolMark(config, this.metadata)); 73 | 74 | if (config.range) { 75 | signals = getRangeSignals(config, signals); 76 | marks = getRangeMark(config, marks); 77 | } 78 | 79 | this.spec.width = config.width; 80 | this.spec.height = config.height; 81 | this.spec.axes = axes; 82 | this.spec.data = dataTable; 83 | this.spec.scales = scales; 84 | this.spec.padding = config.padding; 85 | this.spec.marks = marks; 86 | this.spec.signals = signals; 87 | }; 88 | 89 | line.prototype.draw = function(div, callbacks) { 90 | 91 | if(this.config.maxLength != -1){ 92 | var dataset = this.spec.data[0].values; 93 | var maxValue = this.config.maxLength; 94 | if(dataset.length >= this.config.maxLength){ 95 | var allowedDataSet = []; 96 | var startingPoint = dataset.length - maxValue; 97 | for(var i = startingPoint; i < dataset.length;i++){ 98 | allowedDataSet.push(dataset[i]); 99 | } 100 | this.spec.data[0].values = allowedDataSet; 101 | } 102 | } 103 | 104 | drawChart(div, this, callbacks); 105 | 106 | }; 107 | 108 | line.prototype.insert = function(data) { 109 | //Removing events when max value is enabled 110 | if (this.config.maxLength != -1 && this.config.maxLength < (this.view.data(this.config.title).values().length + data.length)) { 111 | var removeFunction = (function(d) { 112 | return d[this.metadata.names[this.config.x]] == oldData; 113 | }).bind(this); 114 | 115 | for (i = 0; i < data.length; i++) { 116 | var oldData = this.view.data(this.config.title).values()[i][this.metadata.names[this.config.x]]; 117 | this.view.data(this.config.title).remove(removeFunction); 118 | } 119 | } 120 | 121 | this.view.data(this.config.title).insert(data); 122 | this.view.update(); 123 | }; 124 | 125 | line.prototype.getSpec = function() { 126 | return this.spec; 127 | }; 128 | 129 | 130 | function getLineMark(config, metadata){ 131 | interpolation = "linear"; 132 | if (config.interpolation){ 133 | interpolation = config.interpolation; 134 | } 135 | 136 | var mark; 137 | if (config.color != -1 && config.mode == "stack") { 138 | mark = { 139 | "type": "group", 140 | "from": { 141 | "data": config.title, 142 | "transform": [ 143 | {"type": "stack", "groupby": [metadata.names[config.x]], "sortby": [metadata.names[config.color]], "field": metadata.names[config.y]}, 144 | {"type": "facet", "groupby": [metadata.names[config.color]]} 145 | ] 146 | }, 147 | "marks": [ 148 | { 149 | "type": "line", 150 | "properties": { 151 | "update": { 152 | "x": {"scale": "x", "field": metadata.names[config.x]}, 153 | "y": {"scale": "y", "field": "layout_start"}, 154 | "y2": {"scale": "y", "field": "layout_end"}, 155 | "stroke": {"scale": "color", "field": metadata.names[config.color]}, 156 | "strokeWidth": {"value": 2}, 157 | "interpolate": {"value": interpolation} 158 | }, 159 | "hover": { 160 | "strokeOpacity": {"value": 0.5} 161 | } 162 | } 163 | } 164 | ] 165 | }; 166 | } else if (config.color != -1) { 167 | mark = { 168 | "type": "group", 169 | "from": { 170 | "data": config.title, 171 | "transform": [{"type": "facet", "groupby": [metadata.names[config.color]]}] 172 | }, 173 | "marks": [ 174 | { 175 | "type": "line", 176 | "properties": { 177 | "update": { 178 | "x": {"scale": "x", "field": metadata.names[config.x]}, 179 | "y": {"scale": "y", "field": metadata.names[config.y]}, 180 | "stroke": {"scale": "color", "field": metadata.names[config.color]}, 181 | "strokeWidth": {"value": 2}, 182 | "interpolate": {"value": interpolation} 183 | }, 184 | "hover": { 185 | "strokeOpacity": {"value": 0.5} 186 | } 187 | } 188 | } 189 | ] 190 | }; 191 | 192 | } else{ 193 | mark = { 194 | "type": "line", 195 | "from": {"data": config.title}, 196 | "properties": { 197 | "update": { 198 | 199 | "x": {"scale": "x", "field": metadata.names[config.x]}, 200 | "y": {"scale": "y", "field": metadata.names[config.y]}, 201 | "stroke": { "value": config.markColor}, 202 | "strokeWidth": {"value": 2}, 203 | "interpolate": {"value": interpolation} 204 | }, 205 | "hover": { 206 | "fillOpacity": {"value": 0.5} 207 | } 208 | } 209 | }; 210 | } 211 | 212 | 213 | return mark; 214 | } 215 | 216 | -------------------------------------------------------------------------------- /charts/chart.area.js: -------------------------------------------------------------------------------- 1 | 2 | var area = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | var marks =[]; 5 | var signals = []; 6 | this.spec = {}; 7 | 8 | config = checkConfig(config, this.metadata); 9 | this.config = config; 10 | dataTable[0].name= config.title; 11 | 12 | var scales = getXYScales(config, this.metadata); 13 | 14 | if (config.mode == "stack") { 15 | var aggregateData = { 16 | "name": "stack", 17 | "source": config.title, 18 | "transform": [ 19 | { 20 | "type": "aggregate", 21 | "groupby": [this.metadata.names[config.x]], 22 | "summarize": [{"field": this.metadata.names[config.y], "ops": ["sum"]}] 23 | } 24 | ] 25 | }; 26 | 27 | dataTable.push(aggregateData); 28 | yColumn = "sum_"+ this.metadata.names[config.y]; 29 | yDomain = "stack"; 30 | 31 | scales[1].domain = {"data": yDomain, "field": yColumn}; 32 | 33 | } 34 | 35 | 36 | //Make Y scale zero false as area should filled to minimum value 37 | delete scales[1].zero; 38 | 39 | if (config.color != -1) { 40 | 41 | if (config.colorDomain == -1) { 42 | config.colorDomain = {"data": config.title, "field": this.metadata.names[config.color]}; 43 | } 44 | 45 | var colorScale = { 46 | "name": "color", 47 | "type": "ordinal", 48 | "domain": config.colorDomain, 49 | "range": config.colorScale 50 | }; 51 | scales.push(colorScale); 52 | 53 | var legendTitle = "Legend"; 54 | 55 | if (config.title != "table") { 56 | legendTitle = config.title; 57 | } 58 | 59 | if (this.config.legend) { 60 | this.spec.legends = getLegend(this.config); 61 | } 62 | } 63 | 64 | 65 | var axes = getXYAxes(config, "x", "x", "y", "y"); 66 | 67 | 68 | marks.push(getLineMark(config, this.metadata)); 69 | 70 | config.fillOpacity = 0.7; 71 | marks.push(getAreaMark(config, this.metadata)); 72 | config.fillOpacity = 0; 73 | config.markSize = 1000; 74 | marks.push(getSymbolMark(config, this.metadata)); 75 | config.fillOpacity = 1; 76 | config.markSize = 15; 77 | marks.push(getSymbolMark(config, this.metadata)); 78 | 79 | if (config.range) { 80 | signals = getRangeSignals(config, signals); 81 | marks = getRangeMark(config, marks); 82 | } 83 | 84 | this.spec.width = config.width; 85 | this.spec.height = config.height; 86 | this.spec.axes = axes; 87 | this.spec.data = dataTable; 88 | this.spec.scales = scales; 89 | this.spec.padding = config.padding; 90 | this.spec.marks = marks; 91 | this.spec.signals = signals; 92 | }; 93 | 94 | area.prototype.draw = function(div, callbacks) { 95 | 96 | if(this.config.maxLength != -1){ 97 | var dataset = this.spec.data[0].values; 98 | var maxValue = this.config.maxLength; 99 | if(dataset.length >= this.config.maxLength){ 100 | var allowedDataSet = []; 101 | var startingPoint = dataset.length - maxValue; 102 | for(var i = startingPoint; i < dataset.length;i++){ 103 | allowedDataSet.push(dataset[i]); 104 | } 105 | this.spec.data[0].values = allowedDataSet; 106 | } 107 | } 108 | 109 | drawChart(div, this, callbacks); 110 | 111 | }; 112 | 113 | area.prototype.insert = function(data) { 114 | //Removing events when max value is enabled 115 | if (this.config.maxLength != -1 && this.config.maxLength < (this.view.data(this.config.title).values().length + data.length)) { 116 | var removeFunction = (function(d) { 117 | return d[this.metadata.names[this.config.x]] == oldData; 118 | }).bind(this); 119 | 120 | for (i = 0; i < data.length; i++) { 121 | var oldData = this.view.data(this.config.title).values()[i][this.metadata.names[this.config.x]]; 122 | this.view.data(this.config.title).remove(removeFunction); 123 | } 124 | } 125 | 126 | this.view.data(this.config.title).insert(data); 127 | this.view.update(); 128 | }; 129 | 130 | area.prototype.getSpec = function() { 131 | return this.spec; 132 | }; 133 | 134 | 135 | function getAreaMark(config, metadata){ 136 | interpolation = "linear"; 137 | if (config.interpolation){ 138 | interpolation = config.interpolation; 139 | } 140 | 141 | var mark; 142 | if (config.color != -1 && config.mode == "stack") { 143 | mark = { 144 | "type": "group", 145 | "from": { 146 | "data": config.title, 147 | "transform": [ 148 | {"type": "stack", "groupby": [metadata.names[config.x]], "sortby": [metadata.names[config.color]], "field": metadata.names[config.y]}, 149 | {"type": "facet", "groupby": [metadata.names[config.color]]} 150 | ] 151 | }, 152 | "marks": [ 153 | { 154 | "type": "area", 155 | "properties": { 156 | "update": { 157 | "x": {"scale": "x", "field": metadata.names[config.x]}, 158 | "y": {"scale": "y", "field": "layout_start"}, 159 | "y2": {"scale": "y", "field": "layout_end"}, 160 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 161 | "strokeWidth": {"value": 2}, 162 | "fillOpacity": {"value": config.fillOpacity}, 163 | "interpolate": {"value": interpolation} 164 | }, 165 | "hover": { 166 | "strokeOpacity": {"value": 0.5} 167 | } 168 | } 169 | } 170 | ] 171 | }; 172 | } else if (config.color != -1) { 173 | mark = { 174 | "type": "group", 175 | "from": { 176 | "data": config.title, 177 | "transform": [{"type": "facet", "groupby": [metadata.names[config.color]]}] 178 | }, 179 | "marks": [ 180 | { 181 | "type": "area", 182 | "properties": { 183 | "update": { 184 | "x": {"scale": "x", "field": metadata.names[config.x]}, 185 | "y": {"scale": "y", "field": metadata.names[config.y]}, 186 | "y2": {"scale": "y", "value": 0}, 187 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 188 | "strokeWidth": {"value": 2}, 189 | "fillOpacity": {"value": config.fillOpacity}, 190 | "interpolate": {"value": interpolation} 191 | }, 192 | "hover": { 193 | "strokeOpacity": {"value": 0.5} 194 | } 195 | } 196 | } 197 | ] 198 | }; 199 | 200 | } else{ 201 | mark = { 202 | "type": "area", 203 | "from": {"data": config.title}, 204 | "properties": { 205 | "update": { 206 | 207 | "x": {"scale": "x", "field": metadata.names[config.x]}, 208 | "y": {"scale": "y", "field": metadata.names[config.y]}, 209 | "y2": {"scale": "y", "value": 0}, 210 | "fill": { "value": config.markColor}, 211 | "strokeWidth": {"value": 2}, 212 | "fillOpacity": {"value": config.fillOpacity}, 213 | "interpolate": {"value": interpolation} 214 | }, 215 | "hover": { 216 | "fillOpacity": {"value": 0.5} 217 | } 218 | } 219 | }; 220 | } 221 | 222 | 223 | return mark; 224 | } 225 | 226 | -------------------------------------------------------------------------------- /samples/charts/number/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Number Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 64 | 65 |
66 | 67 |
68 | 69 |
70 |
71 |
72 |

73 | Number Chart Sample 74 |

75 |
76 |
77 |
78 |
79 | 80 |
81 |
82 | 83 |
84 |
85 |
86 |

87 | Data Collection 88 |

89 |
90 |
91 |
92 |
 93 | {
 94 |   "metadata" : {
 95 |       "names" : ["rpm","torque","horsepower", "EngineType"],
 96 |       "types" : ["linear","linear", "ordinal","ordinal"]
 97 |   },
 98 |   "data": [
 99 |     [0, 10, 1, "Piston"]]
100 | };
101 | 
102 | 103 | 104 |
105 |
106 | 107 |
108 |
109 | 110 | 111 | 112 |
113 |
114 |
115 |

116 | Configurations 117 |

118 |
119 |
120 |
121 | {
122 |     x : "rpm",
123 |     title :"Torque of Engine",
124 |     charts : [{type: "number"}],
125 |     width: 400,
126 |     height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
heightpxheight of the chart
chartsobjectCollection of charts
164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 |
PropertyValueDescription
typeStringTitle of the table
181 | 182 |
183 | 184 |
185 |
186 | 187 | 221 |
222 | 223 |
224 | 225 | 226 | 231 | 232 | 233 | -------------------------------------------------------------------------------- /samples/charts/scatter/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Scatter Plot Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 63 | 64 |
65 | 66 |
67 | 68 |
69 |
70 |
71 |

72 | Scatter Plot Chart Sample 73 |

74 |
75 |
76 |
77 |
78 | 79 |
80 |
81 | 82 |
83 |
84 |
85 |

86 | Data Collection 87 |

88 |
89 |
90 |
91 |
 92 | {
 93 |   "metadata" : {
 94 |     "names" : ["rpm","torque","horsepower", "weight", "EngineType"],
 95 |     "types" : ["linear","linear", "linear","linear", "ordinal"]
 96 |   },
 97 |   "data": [
 98 |     [1,41600,3.5,79.91,0.8,0.03,"Piston"],
 99 |     [2,37800,3.5,79.65,1.3,0.06,"Piston"]
100 | };
101 | 
102 | 103 | 104 |
105 |
106 | 107 |
108 |
109 | 110 | 111 | 112 |
113 |
114 |
115 |

116 | Configurations 117 |

118 |
119 |
120 |
121 | {
122 |         type: "scatter",
123 |         charts : [
124 |             {type: "scatter",
125 |              x : "rpm",  
126 |              y : "torque",
127 |              color: "horsepower", 
128 |              size : "weight", 
129 |              maxLength: 30}], 
130 |              
131 |         width: 400,
132 |         height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
202 | 203 |
204 | 205 |
206 |
207 | 208 | 253 |
254 | 255 |
256 | 257 | 258 | 263 | 264 | 265 | -------------------------------------------------------------------------------- /charts/chart.scatter.js: -------------------------------------------------------------------------------- 1 | var scatter = function(dataTable, config) { 2 | 3 | this.metadata = dataTable[0].metadata; 4 | var marks = []; 5 | var signals ; 6 | this.spec = {}; 7 | 8 | config = checkConfig(config, this.metadata); 9 | this.config = config; 10 | dataTable[0].name = config.title; 11 | 12 | var rScale = { 13 | "name": "size", 14 | "type": "linear", 15 | "range": [0,576], 16 | "domain": {"data": config.title, "field": this.metadata.names[config.size]} 17 | }; 18 | 19 | var cScale = { 20 | "name": "color", 21 | "type": this.metadata.types[config.color], 22 | "range": config.colorScale, 23 | "domain": {"data": config.title, "field": this.metadata.names[config.color]} 24 | }; 25 | 26 | var scales = getXYScales(config, this.metadata); 27 | scales.push(rScale); 28 | scales.push(cScale); 29 | 30 | var axes = getXYAxes(config, "x", "x", "y", "y"); 31 | 32 | marks.push(getScatterMark(config, this.metadata)); 33 | 34 | if (this.config.legend 35 | && this.metadata.types[config.color] != "linear") { 36 | this.spec.legends = getLegend(this.config); 37 | } 38 | 39 | this.spec.width = config.width; 40 | this.spec.height = config.height; 41 | this.spec.axes = axes; 42 | this.spec.data = dataTable; 43 | this.spec.scales = scales; 44 | this.spec.padding = config.padding; 45 | this.spec.marks = marks; 46 | 47 | }; 48 | 49 | scatter.prototype.draw = function(div, callbacks) { 50 | var viewUpdateFunction = (function(chart) { 51 | if(this.config.tooltip.enabled){ 52 | createTooltip(div); 53 | this.view = chart({el:div}).renderer(this.config.renderer).update(); 54 | bindTooltip(div,this.view,this.config,this.metadata); 55 | } else { 56 | this.view = chart({el:div}).renderer(this.config.renderer).update(); 57 | } 58 | if (callbacks != null) { 59 | for (var i = 0; i= this.config.maxLength){ 70 | var allowedDataSet = []; 71 | var startingPoint = dataset.length - maxValue; 72 | for(var i = startingPoint; i < dataset.length;i++){ 73 | allowedDataSet.push(dataset[i]); 74 | } 75 | this.spec.data[0].values = allowedDataSet; 76 | } 77 | } 78 | 79 | vg.parse.spec(this.spec, viewUpdateFunction); 80 | }; 81 | 82 | scatter.prototype.insert = function(data) { 83 | 84 | var xAxis = this.metadata.names[this.config.x]; 85 | var yAxis = this.metadata.names[this.config.y]; 86 | var size = this.metadata.names[this.config.size]; 87 | var color = this.metadata.names[this.config.color]; 88 | 89 | if (this.config.maxLength != -1 && this.config.maxLength < (this.view.data(this.config.title).values().length + data.length)) { 90 | 91 | var allDataSet = this.view.data(this.config.title).values().concat(data); 92 | var allowedRemovableDataSet = []; 93 | for (i = 0; i < allDataSet.length - this.config.maxLength; i++) { 94 | allowedRemovableDataSet.push(this.view.data(this.config.title).values()[i][xAxis]); 95 | } 96 | 97 | for (i = 0; i < data.length; i++) { 98 | var isValueMatched = false; 99 | this.view.data(this.config.title).update(function(d) { 100 | return d[xAxis] == data[i][xAxis]; }, 101 | yAxis, 102 | function(d) { 103 | isValueMatched = true; 104 | return data[i][yAxis]; 105 | }); 106 | 107 | this.view.data(this.config.title).update(function(d) { 108 | return d[xAxis] == data[i][xAxis]; }, 109 | color, 110 | function(d) { 111 | isValueMatched = true; 112 | return data[i][color]; 113 | }); 114 | 115 | this.view.data(this.config.title).update(function(d) { 116 | return d[xAxis] == data[i][xAxis]; }, 117 | size, 118 | function(d) { 119 | isValueMatched = true; 120 | return data[i][size]; 121 | }); 122 | 123 | if(isValueMatched){ 124 | var isIndexRemoved = false; 125 | 126 | var index = allowedRemovableDataSet.indexOf(data[i][xAxis]); 127 | if (index > -1) { 128 | // updated value matched in allowed removable values 129 | isIndexRemoved = true; 130 | allowedRemovableDataSet.splice(index, 1); 131 | } 132 | 133 | if(!isIndexRemoved){ 134 | // updated value NOT matched in allowed removable values 135 | allowedRemovableDataSet.splice((allowedRemovableDataSet.length - 1), 1); 136 | } 137 | 138 | } else { 139 | //insert the new data 140 | this.view.data(this.config.title).insert([data[i]]); 141 | this.view.update(); 142 | } 143 | } 144 | 145 | var oldData; 146 | var removeFunction = function(d) { 147 | return d[xAxis] == oldData; 148 | }; 149 | 150 | for (i = 0; i < allowedRemovableDataSet.length; i++) { 151 | oldData = allowedRemovableDataSet[i]; 152 | this.view.data(this.config.title).remove(removeFunction); 153 | } 154 | } else{ 155 | for (i = 0; i < data.length; i++) { 156 | var isValueMatched = false; 157 | this.view.data(this.config.title).update(function(d) { 158 | return d[xAxis] == data[i][xAxis]; }, 159 | yAxis, 160 | function(d) { 161 | isValueMatched = true; 162 | return data[i][yAxis]; 163 | }); 164 | 165 | this.view.data(this.config.title).update(function(d) { 166 | return d[xAxis] == data[i][xAxis]; }, 167 | color, 168 | function(d) { 169 | isValueMatched = true; 170 | return data[i][color]; 171 | }); 172 | 173 | this.view.data(this.config.title).update(function(d) { 174 | return d[xAxis] == data[i][xAxis]; }, 175 | size, 176 | function(d) { 177 | isValueMatched = true; 178 | return data[i][size]; 179 | }); 180 | 181 | if(!isValueMatched){ 182 | this.view.data(this.config.title).insert([data[i]]); 183 | } 184 | } 185 | } 186 | this.view.update({duration: 200}); 187 | }; 188 | 189 | scatter.prototype.getSpec = function() { 190 | return this.spec; 191 | }; 192 | 193 | 194 | function getScatterMark(config, metadata){ 195 | var fill; 196 | var size; 197 | 198 | if (config.color == -1) { 199 | fill = {"value": config.markColor}; 200 | } else { 201 | fill = {"scale": "color", "field": metadata.names[config.color]}; 202 | } 203 | 204 | if (config.size == -1) { 205 | size = {"value": config.markSize * 50}; 206 | } else { 207 | size = {"scale":"size","field":metadata.names[config.size]}; 208 | } 209 | 210 | var mark = { 211 | 212 | "type": "symbol", 213 | "from": {"data": config.title}, 214 | "properties": { 215 | "update": { 216 | "x": {"scale": "x", "field": metadata.names[config.x]}, 217 | "y": {"scale": "y", "field": metadata.names[config.y]}, 218 | "fill": fill, 219 | "size": size, 220 | "fillOpacity": {"value": 1} 221 | }, 222 | "hover": { 223 | "fillOpacity": {"value": 0.5}, 224 | "cursor": {"value": config.hoverCursor} 225 | } 226 | } 227 | 228 | } 229 | ; 230 | 231 | 232 | return mark; 233 | } 234 | 235 | function getScatterToolTipMark(config, metadata) { 236 | config.toolTip.height = 50; 237 | config.toolTip.y = -50; 238 | 239 | var mark = getToolTipMark(config, metadata); 240 | var sizeText = { 241 | "type": "text", 242 | "properties": { 243 | "update": { 244 | "x": {"value": 6}, 245 | "y": {"value": 44}, 246 | "text": {"template": "Size \t (" + metadata.names[config.size] + ") \t {{hover." + metadata.names[config.size] + "}}"}, 247 | "fill": {"value": "black"} 248 | } 249 | } 250 | }; 251 | mark.marks.push(sizeText); 252 | return mark; 253 | } 254 | -------------------------------------------------------------------------------- /charts/chart.arc.js: -------------------------------------------------------------------------------- 1 | 2 | var arc = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | var marks =[]; 5 | this.spec = {}; 6 | 7 | config = checkConfig(config, this.metadata); 8 | this.config = config; 9 | dataTable[0].name= config.title; 10 | 11 | var summarize = {}; 12 | summarize[this.metadata.names[config.x]] = "sum"; 13 | 14 | dataTable.push({ 15 | "name": "summary", 16 | "source": config.title, 17 | "transform": [ 18 | { 19 | "type": "aggregate", 20 | "summarize": summarize 21 | } 22 | ] 23 | }); 24 | 25 | dataTable.push( { 26 | "name": "layout", 27 | "source": "table", 28 | "transform": [ 29 | {"type": "cross", "with": "summary"}, 30 | {"type": "pie","field": "a." + this.metadata.names[config.x]}, 31 | { 32 | "type": "formula", 33 | "field": "percentage", 34 | "expr": "datum.a."+this.metadata.names[config.x] 35 | +" / datum.b.sum_"+this.metadata.names[config.x]+" * 100" 36 | } 37 | ] 38 | }); 39 | 40 | var scales = []; 41 | 42 | if (config.colorDomain == -1) { 43 | config.colorDomain = {"data": config.title, "field": this.metadata.names[config.color]}; 44 | } 45 | 46 | 47 | var colorScale = { 48 | "name": "color", 49 | "type": "ordinal", 50 | "domain": config.colorDomain, 51 | "range": config.colorScale 52 | }; 53 | scales.push(colorScale); 54 | 55 | 56 | if (config.percentage && 57 | (config.mode == "pie" || config.mode == "donut")) { 58 | marks.push(getPieText(config, this.metadata)); 59 | } else if (config.percentage) { 60 | //Push complimentary value to fill the arc 61 | dataTable.push( 62 | { 63 | "name": "arc", 64 | "values": [{"type": "YES"}] 65 | }); 66 | marks.push(getPieMidText(config, this.metadata)); 67 | } 68 | 69 | marks.push(getPieMark(config, this.metadata)); 70 | 71 | var legendTitle = "Legend"; 72 | 73 | if (config.title != "table") { 74 | legendTitle = config.title; 75 | } 76 | 77 | if (this.config.legend) { 78 | this.spec.legends = getLegend(this.config); 79 | } 80 | 81 | this.spec.width = config.width; 82 | this.spec.height = config.height; 83 | this.spec.data = dataTable; 84 | this.spec.scales = scales; 85 | this.spec.padding = config.padding; 86 | this.spec.marks = marks; 87 | }; 88 | 89 | arc.prototype.draw = function(div, callbacks) { 90 | 91 | var viewUpdateFunction = (function(chart) { 92 | if(this.config.tooltip.enabled){ 93 | this.config.tooltip.type = "arc"; 94 | createTooltip(div); 95 | this.view = chart({el:div}).renderer(this.config.renderer).update(); 96 | bindTooltip(div,this.view,this.config,this.metadata); 97 | } else { 98 | this.view = chart({el:div}).renderer(this.config.renderer).update(); 99 | } 100 | 101 | if (callbacks != null) { 102 | for (var i = 0; i= this.config.maxLength){ 113 | var allowedDataSet = []; 114 | var startingPoint = dataset.length - maxValue; 115 | for(var i = startingPoint; i < dataset.length;i++){ 116 | allowedDataSet.push(dataset[i]); 117 | } 118 | this.spec.data[0].values = allowedDataSet; 119 | } 120 | } 121 | 122 | vg.parse.spec(this.spec, viewUpdateFunction); 123 | }; 124 | 125 | arc.prototype.insert = function(data) { 126 | 127 | var color = this.metadata.names[this.config.color]; 128 | var x = this.metadata.names[this.config.x]; 129 | var view =this.view; 130 | 131 | 132 | 133 | var updated = false; 134 | 135 | for (i = 0; i < data.length; i++) { 136 | this.view.data(this.config.title) 137 | .update( 138 | function(d) { 139 | return d[color] == data[i][color]; 140 | }, 141 | x, 142 | function(d) { 143 | updated = true; 144 | return data[i][x]; 145 | }); 146 | } 147 | 148 | if (updated == false) { 149 | view.data(this.config.title).insert(data); 150 | } 151 | 152 | this.view.update({duration: 500}); 153 | }; 154 | 155 | arc.prototype.getSpec = function() { 156 | return this.spec; 157 | }; 158 | 159 | 160 | function getPieMark(config, metadata){ 161 | var innerRadius; 162 | if (config.mode == "donut") { 163 | var innerRadius = config.height / 5 * ( 1 + config.innerRadius); 164 | } else if (config.mode == "pie") { 165 | var innerRadius = 0; 166 | } else { 167 | config.innerRadius += 0.5; 168 | var innerRadius = config.height / 5 * ( 1 + config.innerRadius); 169 | } 170 | 171 | var title = config.title; 172 | var fieldAlias = "" 173 | 174 | if (config.type != null) { 175 | title = "layout"; 176 | fieldAlias = "a."; 177 | } 178 | 179 | var mark = { 180 | "type": "arc", 181 | "from": {"data": title}, 182 | "properties": { 183 | "update": { 184 | "x": {"field": {"group": "width"}, "mult": 0.5}, 185 | "y": {"field": {"group": "height"}, "mult": 0.5}, 186 | "startAngle": {"field": "layout_start"}, 187 | "endAngle": {"field": "layout_end"}, 188 | "innerRadius": {"value": innerRadius}, 189 | "outerRadius": {"value": config.height * 0.4}, 190 | "fill": {"scale": "color", "field": fieldAlias + metadata.names[config.color]}, 191 | "fillOpacity": {"value": 1} 192 | }, 193 | 194 | "hover": { 195 | "fillOpacity": {"value": 0.8}, 196 | "cursor": {"value": config.hoverCursor} 197 | } 198 | } 199 | }; 200 | 201 | return mark; 202 | }; 203 | function getPieMidText(config, metadata){ 204 | var mark = { 205 | "type": "text", 206 | "from": {"data": "layout"}, 207 | "properties": { 208 | "update": { 209 | "x": {"field": {"group": "width"}, "mult": 0.5}, 210 | "y": {"field": {"group": "height"}, "mult": 0.5}, 211 | "radius": { "value": 0}, 212 | "theta": {"field": "layout_mid"}, 213 | "fill": [ 214 | { 215 | "test": "indata('arc', datum.a."+metadata.names[config.color]+", 'type')", 216 | "scale": "color", "field": metadata.names[config.color] 217 | }, 218 | {} 219 | ], 220 | "align": {"value": "center"}, 221 | "baseline": {"value": "middle"}, 222 | "fontSize":{"value": config.height/9}, 223 | "text": {"template": "{{datum.percentage | number:'.2f'}}%"} 224 | 225 | } 226 | } 227 | }; 228 | return mark; 229 | }; 230 | 231 | 232 | function getPieText(config, metadata){ 233 | var mark = { 234 | "type": "text", 235 | "from": {"data": "layout"}, 236 | "properties": { 237 | "update": { 238 | "x": {"field": {"group": "width"}, "mult": 0.5}, 239 | "y": {"field": {"group": "height"}, "mult": 0.5}, 240 | "radius": { "value": config.height * 0.5}, 241 | "theta": {"field": "layout_mid"}, 242 | "fill": {"value": "#000"}, 243 | "align": {"value": "center"}, 244 | "baseline": {"value": "middle"}, 245 | "text": {"template": "{{datum.percentage | number:'.2f'}}%"} 246 | 247 | } 248 | } 249 | }; 250 | 251 | return mark; 252 | }; 253 | -------------------------------------------------------------------------------- /samples/charts/table/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Table Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 62 | 63 |
64 | 65 |
66 | 67 |
68 |
69 |
70 |

71 | Number Chart Sample 72 |

73 |
74 |
75 |
76 |
77 | 78 |
79 |
80 | 81 |
82 |
83 |
84 |

85 | Data Collection 86 |

87 |
88 |
89 |
90 |
 91 | {
 92 |   "metadata" : {
 93 |       "names" : ["rpm","torque","horsepower", "EngineType"],
 94 |       "types" : ["linear","linear", "ordinal","ordinal"]
 95 |   },
 96 |   "data": [
 97 |     [1, 10, 1, "Piston"]]
 98 | };
 99 | 
100 | 101 | 102 |
103 |
104 | 105 |
106 |
107 | 108 | 109 | 110 |
111 |
112 |
113 |

114 | Configurations 115 |

116 |
117 |
118 |
119 | {
120 |     key : "rpm",
121 |     charts : [{type: "table",  y : "torque"}],
122 |     maxLength: 7,
123 |     columns:["EngineType",  "torque", "rpm"],
124 |     width: 400,
125 |     height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
195 | 196 |
197 | 198 |
199 |
200 | 201 | 252 |
253 | 254 |
255 | 256 | 257 |
258 |
259 |

VizGrammar.js v1.0.0 | © WSO2 Inc.

260 |
261 |
262 | 263 | 264 | -------------------------------------------------------------------------------- /samples/charts/line/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Bar Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 63 | 64 |
65 | 66 |
67 | 68 |
69 |
70 |
71 |

72 | Group Multi Line Chart Sample 73 |

74 |
75 |
76 |
77 |
78 |
79 | 80 |
81 |
82 | 83 |
84 |
85 |
86 |

87 | Single Line Chart Sample 88 |

89 |
90 |
91 |
92 |
93 | 94 |
95 |
96 | 97 | 156 |
157 | 158 |
159 |
160 |
161 |
162 |

163 | Data Collection 164 |

165 |
166 |
167 |
168 |
169 | {
170 |   "metadata" : {
171 |       "names" : ["rpm","torque","horsepower", "EngineType"],
172 |       "types" : ["linear","linear", "ordinal","ordinal"]
173 |   },
174 |   "data": [
175 |     [1, 10, 1, "Piston"]]
176 | };
177 | 
178 | 179 | 180 |
181 |
182 | 183 |
184 |
185 | 186 | 187 |
188 |
189 |
190 |

191 | Configurations 192 |

193 |
194 |
195 |
196 | 197 |
198 | {
199 |   x : "rpm",
200 |   charts : [{type: "line",  y : "torque", color: "EngineType"}],
201 |   maxLength: 10,
202 |   width: 400,
203 |   height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
colorStringAttribute name for color dimension
277 | 278 |
279 |
280 |
281 |
282 | 283 |
284 | 285 |
286 | 287 | 288 |
289 |
290 |

VizGrammar.js v1.0.0 | © WSO2 Inc.

291 |
292 |
293 | 294 | 295 | -------------------------------------------------------------------------------- /samples/charts/map/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Map Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 62 | 63 |
64 | 65 |
66 | 67 |
68 |
69 |
70 |

71 | World Map Chart Sample 72 |

73 |
74 |
75 |
76 |
77 | 78 |
79 |
80 | 81 |
82 |
83 |
84 |

85 | US Map Chart Sample 86 |

87 |
88 |
89 |
90 |
91 | 92 |
93 |
94 | 95 |
96 |
97 |
98 |

99 | Europe Map Chart Sample 100 |

101 |
102 |
103 |
104 |
105 | 106 |
107 |
108 | 109 |
110 |
111 |
112 |

113 | Data Collection 114 |

115 |
116 |
117 |
118 |
119 | {
120 |   "metadata" : {
121 |       "names" : ["Country","Inflation"],
122 |       "types" : ["ordinal","linear"]
123 |   },
124 |   "data": [
125 |     ["Afghanistan",4.23],
126 |     ["EGY",1.23],
127 |     ["Afghanistan",2.23],
128 |     ["United States",8.23],
129 | };
130 | 
131 | 132 | 133 |
134 |
135 | 136 |
137 |
138 | 139 | 140 | 141 |
142 |
143 |
144 |

145 | Configurations 146 |

147 |
148 |
149 |
150 | {
151 |         x : "Country",
152 |         charts : [{type: "map",  y : "Inflation"}],
153 |         width: 400,
154 |         height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
224 | 225 |
226 | 227 |
228 |
229 | 230 | 347 |
348 | 349 |
350 | 351 | 352 |
353 |
354 |

VizGrammar.js v1.0.0 | © WSO2 Inc.

355 |
356 |
357 | 358 | 359 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /samples/charts/area/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Area Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 63 | 64 |
65 | 66 |
67 | 68 |
69 |
70 |
71 |

72 | Area Chart Sample 73 |

74 |
75 |
76 |
77 |
78 |
79 | 80 |
81 |
82 | 83 |
84 |
85 |
86 |

87 | Group Area Chart Sample 88 |

89 |
90 |
91 |
92 |
93 | 94 |
95 |
96 | 97 |
98 |
99 |
100 |

101 | Stack Area Chart Sample 102 |

103 |
104 |
105 |
106 |
107 | 108 |
109 |
110 | 111 |
112 |
113 |
114 |

115 | Data Collection 116 |

117 |
118 |
119 |
120 |
121 | {
122 |   "metadata" : {
123 |       "names" : ["rpm","torque","horsepower", "EngineType"],
124 |       "types" : ["linear","linear", "ordinal","ordinal"]
125 |   },
126 |   "data": [
127 |     [1, 10, 1, "Piston"]]
128 | };
129 | 
130 | 131 | 132 |
133 |
134 | 135 |
136 |
137 | 138 | 139 | 140 |
141 |
142 |
143 |

144 | Configurations 145 |

146 |
147 |
148 |
149 | {
150 |    
151 |     charts : [{type: "area",  x : "rpm", y : "torque"}],
152 |     maxLength: 10,
153 |     width: 400,
154 |     height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
224 | 225 |
226 | 227 |
228 |
229 | 230 | 316 |
317 | 318 |
319 | 320 | 321 |
322 |
323 |

VizGrammar.js v1.0.0 | © WSO2 Inc.

324 |
325 |
326 | 327 | 328 | -------------------------------------------------------------------------------- /charts/chart.map.js: -------------------------------------------------------------------------------- 1 | var map = function(dataTable, config) { 2 | 3 | this.metadata = dataTable[0].metadata; 4 | var marks ; 5 | var signals ; 6 | var predicates = []; 7 | var legends = []; 8 | this.spec = {}; 9 | var geoInfoJson ; 10 | 11 | geoInfoJson = loadGeoMapCodes(config.helperUrl); 12 | config = checkConfig(config, this.metadata); 13 | this.config = config; 14 | this.config.geoInfoJson = geoInfoJson; 15 | 16 | for (i = 0; i < dataTable[0].values.length; i++) { 17 | for (var key in dataTable[0].values[i]) { 18 | if(key == dataTable[0].metadata.names[config.x]){ 19 | if (dataTable[0].values[i].hasOwnProperty(key)) { 20 | dataTable[0].values[i].unitName = dataTable[0].values[i][key]; 21 | dataTable[0].values[i][key] = getMapCode(dataTable[0].values[i][key], config.mapType,geoInfoJson); 22 | break; 23 | } 24 | } 25 | } 26 | }; 27 | 28 | dataTable[0].name = config.title; 29 | dataTable[0].transform = [ 30 | { 31 | "type": "formula", 32 | "field": "v", 33 | "expr": "datum."+this.metadata.names[config.y] 34 | } 35 | ]; 36 | 37 | if (config.tooltip.enabled) { 38 | marks = getMapMark(config, this.metadata); 39 | signals = getMapSignals(); 40 | this.spec.signals = signals; 41 | } 42 | 43 | dataTable.push(getTopoJson(config,this.metadata)); 44 | predicates.push(getMapPredicates()); 45 | 46 | if (config.legend) { 47 | legends.push(getMapLegends(config,this.metadata)); 48 | } 49 | 50 | var cScale = { 51 | "name": "color", 52 | "type": "linear", 53 | "domain": {"data": "geoData","field": "zipped.v"}, 54 | "domainMin": 0.0, 55 | "zero": false, 56 | "range": config.colorScale 57 | }; 58 | 59 | if (config.highlight == "single" || config.highlight == "multi") { 60 | 61 | var multiTest; 62 | 63 | if (config.highlight == "multi") { 64 | multiTest = "!multi"; 65 | } else { 66 | multiTest = "multi"; 67 | } 68 | 69 | 70 | dataTable.push( 71 | { 72 | "name": "selectedPoints", 73 | "modify": [ 74 | {"type": "clear", "test": multiTest}, 75 | {"type": "toggle", "signal": "clickedPoint", "field": "id"} 76 | ] 77 | }); 78 | 79 | signals.push( { 80 | "name": "clickedPoint", 81 | "init": 0, 82 | "verbose": true, 83 | "streams": [{"type": "click", "expr": "datum._id"}] 84 | }, 85 | { 86 | "name": "multi", 87 | "init": false, 88 | "verbose": true, 89 | "streams": [{"type": "click", "expr": "datum._id"}] 90 | }); 91 | 92 | marks[0].properties.update.fillOpacity = [ 93 | { 94 | "test": "indata('selectedPoints', datum._id, 'id')", 95 | "value": 1 96 | },{"value":config.selectionOpacity} 97 | ]; 98 | 99 | } 100 | 101 | var scales = [cScale]; 102 | 103 | this.spec.width = config.width; 104 | this.spec.height = config.height; 105 | this.spec.data = dataTable; 106 | this.spec.scales = scales; 107 | this.spec.padding = config.padding; 108 | this.spec.marks = marks; 109 | this.spec.predicates = predicates; 110 | this.spec.legends = legends; 111 | this.spec.signals = signals; 112 | 113 | }; 114 | 115 | map.prototype.draw = function(div, callbacks) { 116 | var viewUpdateFunction = (function(chart) { 117 | this.view = chart({el:div}).renderer(this.config.renderer).update(); 118 | 119 | if (callbacks != null) { 120 | for (var i = 0; i 2 | 3 | 4 | 5 | 6 | 7 | VizGrammar.js - Arc Chart Sample 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 45 |
46 |
47 | 48 |
49 |
50 |
51 |

52 | Donut Chart Sample 53 |

54 |
55 |
56 |
57 |
58 |
59 | 60 |
61 |
62 |
63 |
64 |
65 |

66 | Pie Chart Sample 67 |

68 |
69 |
70 |
71 |
72 | 73 |
74 |
75 | 76 |
77 | 78 | 79 |
80 | 81 |
82 |
83 |
84 |

85 | Arc Chart Sample 86 |

87 |
88 |
89 |
90 | 91 |
92 | 93 |
94 |
95 |
96 |
97 |
98 |

99 | Arc Chart Sample 100 |

101 |
102 |
103 |
104 |
105 | 106 |
107 |
108 | 109 |
110 | 111 |
112 | 113 | 114 | 115 |
116 |
117 |
118 |

119 | Data Collection 120 |

121 |
122 |
123 |
124 |
125 | {
126 |   "metadata" : {
127 |       "names" : ["rpm","torque","horsepower", "EngineType"],
128 |       "types" : ["linear","linear", "ordinal","ordinal"]
129 |   },
130 |   "data": [
131 |     [1, 10, 1, "Piston"]]
132 | };
133 | 
134 | 135 | 136 |
137 |
138 | 139 |
140 |
141 | 142 |
143 |
144 |
145 |

146 | Configurations 147 |

148 |
149 |
150 |
151 |     {
152 |         charts : [{type:"arc", x:"torque", color:"EngineType", mode:"donut"}],
153 |         width: 300,
154 |         height: 250
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
223 | 224 |
225 | 226 |
227 |
228 | 229 | 230 |
231 | 232 | 371 |
372 | 373 | 374 | 375 |
376 |
377 |

VizGrammar.js v1.0.0 | © WSO2 Inc.

378 |
379 |
380 | 381 | -------------------------------------------------------------------------------- /samples/charts/bar/index.html: -------------------------------------------------------------------------------- 1 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | VizGrammar.js - Bar Chart Sample 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 63 | 64 |
65 | 66 |
67 | 68 |
69 |
70 |
71 |

72 | Bar Chart Sample 73 |

74 |
75 |
76 |
77 |
78 | 79 |
80 |
81 | 82 |
83 |
84 |
85 |

86 | Horizontal Bar Chart Sample 87 |

88 |
89 |
90 |
91 |
92 | 93 |
94 |
95 | 96 |
97 |
98 |
99 |

100 | Stack Bar Chart Sample 101 |

102 |
103 |
104 |
105 |
106 | 107 |
108 |
109 |
110 |
111 |
112 |

113 | Horizontal Stack Bar Chart Sample 114 |

115 |
116 |
117 |
118 |
119 | 120 |
121 |
122 | 123 |
124 |
125 |
126 |

127 | Group Bar Chart Sample 128 |

129 |
130 |
131 |
132 |
133 | 134 |
135 |
136 |
137 |
138 |
139 |

140 | Horizontal Group Bar Chart Sample 141 |

142 |
143 |
144 |
145 |
146 | 147 |
148 |
149 | 150 | 151 |
152 |
153 |
154 |

155 | Data Collection 156 |

157 |
158 |
159 |
160 |
161 | {
162 |   "metadata" : {
163 |       "names" : ["rpm","torque","horsepower", "EngineType"],
164 |       "types" : ["linear","linear", "ordinal","ordinal"]
165 |   },
166 |   "data": [
167 |     [1, 10, 1, "Piston"]]
168 | };
169 | 
170 | 171 | 172 |
173 |
174 | 175 |
176 |
177 | 178 | 179 | 180 |
181 |
182 |
183 |

184 | Configurations 185 |

186 |
187 |
188 |
189 | {
190 |     type: "bar",
191 |     x : "rpm",
192 |     charts : [{type: "bar",  y : "torque", color:"EngineType"}],
193 |     maxLength: 6,
194 |     width: 400,
195 |     height: 200
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 |
PropertyValueDescription
titleStringTitle of the table
xStringcAttribute name for X-axis
maxLengthintMaximum length of chart datatable
heightpxheight of the chart
chartsobjectCollection of charts
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 |
PropertyValueDescription
typeStringTitle of the table
yStringAttribute name for Y-axis
265 | 266 |
267 | 268 |
269 |
270 | 271 | 378 |
379 | 380 |
381 | 382 | 383 | 388 | 389 | 390 | -------------------------------------------------------------------------------- /charts/chart.bar.js: -------------------------------------------------------------------------------- 1 | 2 | var bar = function(dataTable, config) { 3 | this.metadata = dataTable[0].metadata; 4 | var marks =[]; 5 | var scales =[]; 6 | this.spec = {}; 7 | var yColumn; 8 | var yDomain; 9 | 10 | var xRange; 11 | var yRange; 12 | var xAxesType; 13 | var yAxesType; 14 | var signals = []; 15 | 16 | config = checkConfig(config, this.metadata); 17 | this.config = config; 18 | dataTable[0].name= config.title; 19 | 20 | if (config.orientation == "left") { 21 | xRange = "height"; 22 | yRange = "width"; 23 | xAxesType = "y"; 24 | yAxesType = "x"; 25 | } else { 26 | xRange = "width"; 27 | yRange = "height"; 28 | xAxesType = "x"; 29 | yAxesType = "y"; 30 | } 31 | 32 | if (config.color != -1) { 33 | var legendTitle = "Legend"; 34 | if (config.title != "table") { 35 | legendTitle = config.title; 36 | } 37 | if (config.colorDomain == -1) { 38 | config.colorDomain = {"data": config.title, "field": this.metadata.names[config.color]}; 39 | } 40 | 41 | var colorScale = { 42 | "name": "color", 43 | "type": "ordinal", 44 | "domain": config.colorDomain, 45 | "range": config.colorScale 46 | }; 47 | 48 | scales.push(colorScale); 49 | 50 | 51 | 52 | if (config.mode == "stack") { 53 | var aggregateData = { 54 | "name": "stack", 55 | "source": config.title, 56 | "transform": [ 57 | { 58 | "type": "aggregate", 59 | "groupby": [this.metadata.names[config.x]], 60 | "summarize": [{"field": this.metadata.names[config.y], "ops": ["sum"]}] 61 | } 62 | ] 63 | }; 64 | 65 | dataTable.push(aggregateData); 66 | yColumn = "sum_"+ this.metadata.names[config.y]; 67 | yDomain = "stack"; 68 | 69 | } else { 70 | yColumn = this.metadata.names[config.y]; 71 | yDomain = config.title; 72 | } 73 | 74 | if (this.config.legend) { 75 | this.spec.legends = getLegend(this.config); 76 | } 77 | } else { 78 | yColumn = this.metadata.names[config.y]; 79 | yDomain = config.title; 80 | } 81 | 82 | var xScale = { 83 | "name": "x", 84 | "type": "ordinal", 85 | "range": xRange, 86 | "domain": config.xScaleDomain 87 | }; 88 | 89 | if (config.mode == "group") { 90 | xScale.padding = 0.2; 91 | } 92 | 93 | if (config.yScaleDomain.constructor !== Array) { 94 | config.yScaleDomain = {"data": yDomain, "field": yColumn}; 95 | } 96 | 97 | var yScale = { 98 | "name": "y", 99 | "type": this.metadata.types[config.y], 100 | "range": yRange, 101 | "domain": config.yScaleDomain 102 | }; 103 | 104 | 105 | 106 | scales.push(xScale); 107 | scales.push(yScale); 108 | 109 | var axes = getXYAxes(config, xAxesType, "x", yAxesType, "y"); 110 | 111 | if (config.color != -1 && config.mode == "stack") { 112 | marks.push(getStackBarMark(config, this.metadata)); 113 | } else if (config.color != -1 && config.mode == "group") { 114 | marks.push(getGroupBarMark(config, this.metadata)); 115 | } else { 116 | marks.push(getBarMark(config, this.metadata)); 117 | } 118 | 119 | if (config.range) { 120 | signals = getRangeSignals(config, signals); 121 | marks = getRangeMark(config, marks); 122 | } 123 | 124 | if (config.orientation == "left" && config.text != null) { 125 | 126 | var xVal = {"value": 5}; 127 | 128 | if (config.textAlign == "right") { 129 | var xVal = {"scale": "y","field": this.metadata.names[config.y], "offset": 2}; 130 | } 131 | 132 | marks.push({ 133 | "type": "text", 134 | "from": {"data": "table"}, 135 | "properties": { 136 | "update": { 137 | "x": xVal, 138 | "dy": { 139 | "scale": "x", 140 | "band": true, 141 | "mult": 0.5 142 | }, 143 | "y": {"scale": "x","field": this.metadata.names[config.x]}, 144 | "align":{"value": "left"}, 145 | "text": {"field": this.metadata.names[config.text]}, 146 | "fill": {"value": config.textColor} 147 | } 148 | } 149 | }); 150 | } 151 | 152 | if (config.highlight == "single" || config.highlight == "multi") { 153 | 154 | var multiTest; 155 | 156 | if (config.highlight == "multi") { 157 | multiTest = "!multi"; 158 | } else { 159 | multiTest = "multi"; 160 | } 161 | 162 | 163 | dataTable.push( 164 | { 165 | "name": "selectedPoints", 166 | "modify": [ 167 | {"type": "clear", "test": multiTest}, 168 | {"type": "toggle", "signal": "clickedPoint", "field": "id"} 169 | ] 170 | }); 171 | 172 | signals.push( { 173 | "name": "clickedPoint", 174 | "init": 0, 175 | "verbose": true, 176 | "streams": [{"type": "click", "expr": "datum._id"}] 177 | }, 178 | { 179 | "name": "multi", 180 | "init": false, 181 | "verbose": true, 182 | "streams": [{"type": "click", "expr": "datum._id"}] 183 | }); 184 | 185 | if (config.selectionColor == "") { 186 | marks[0].properties.update.fillOpacity = [ 187 | { 188 | "test": "indata('selectedPoints', datum._id, 'id')", 189 | "value": 1 190 | },{"value":config.selectionOpacity} 191 | ]; 192 | } else { 193 | marks[0].properties.update.fill = [ 194 | { 195 | "test": "indata('selectedPoints', datum._id, 'id')", 196 | "value": config.selectionColor 197 | },marks[0].properties.update.fill 198 | ]; 199 | } 200 | } 201 | 202 | this.spec.width = config.width; 203 | this.spec.height = config.height; 204 | this.spec.axes = axes; 205 | this.spec.data = dataTable; 206 | this.spec.scales = scales; 207 | this.spec.padding = config.padding; 208 | this.spec.marks = marks; 209 | this.spec.signals = signals; 210 | 211 | var specc = JSON.stringify(this.spec); 212 | }; 213 | 214 | bar.prototype.draw = function(div, callbacks) { 215 | if(this.config.maxLength != -1){ 216 | var dataset = this.spec.data[0].values; 217 | var maxValue = this.config.maxLength; 218 | if(dataset.length >= this.config.maxLength){ 219 | var allowedDataSet = []; 220 | var startingPoint = dataset.length - maxValue; 221 | for(var i = startingPoint; i < dataset.length;i++){ 222 | allowedDataSet.push(dataset[i]); 223 | } 224 | this.spec.data[0].values = allowedDataSet; 225 | } 226 | } 227 | this.config.tooltip.type = "rect"; 228 | drawChart(div, this, callbacks); 229 | }; 230 | 231 | bar.prototype.insert = function(data) { 232 | 233 | var xAxis = this.metadata.names[this.config.x]; 234 | var yAxis = this.metadata.names[this.config.y]; 235 | var size = this.metadata.names[this.config.size]; 236 | var color = this.metadata.names[this.config.color]; 237 | 238 | if (this.config.maxLength != -1 && this.config.maxLength < (this.view.data(this.config.title).values().length + data.length)) { 239 | 240 | var allDataSet = this.view.data(this.config.title).values().concat(data); 241 | var allowedRemovableDataSet = []; 242 | for (i = 0; i < allDataSet.length - this.config.maxLength; i++) { 243 | allowedRemovableDataSet.push(this.view.data(this.config.title).values()[i][xAxis]); 244 | } 245 | 246 | for (i = 0; i < data.length; i++) { 247 | var isValueMatched = false; 248 | this.view.data(this.config.title).update(function(d) { 249 | if (color == null) { 250 | return d[xAxis] == data[i][xAxis]; 251 | } else { 252 | return d[xAxis] == data[i][xAxis] && d[color] == data[i][color]; 253 | } 254 | }, 255 | yAxis, 256 | function(d) { 257 | isValueMatched = true; 258 | return data[i][yAxis]; 259 | }); 260 | 261 | if(isValueMatched){ 262 | var isIndexRemoved = false; 263 | 264 | var index = allowedRemovableDataSet.indexOf(data[i][xAxis]); 265 | if (index > -1) { 266 | // updated value matched in allowed removable values 267 | isIndexRemoved = true; 268 | allowedRemovableDataSet.splice(index, 1); 269 | } 270 | 271 | if(!isIndexRemoved){ 272 | // updated value NOT matched in allowed removable values 273 | allowedRemovableDataSet.splice((allowedRemovableDataSet.length - 1), 1); 274 | } 275 | 276 | } else { 277 | //insert the new data 278 | this.view.data(this.config.title).insert([data[i]]); 279 | this.view.update(); 280 | } 281 | } 282 | 283 | var oldData; 284 | var removeFunction = function(d) { 285 | return d[xAxis] == oldData; 286 | }; 287 | 288 | for (i = 0; i < allowedRemovableDataSet.length; i++) { 289 | oldData = allowedRemovableDataSet[i]; 290 | this.view.data(this.config.title).remove(removeFunction); 291 | } 292 | } else{ 293 | for (i = 0; i < data.length; i++) { 294 | var isValueMatched = false; 295 | this.view.data(this.config.title).update(function(d) { 296 | if (color == null) { 297 | return d[xAxis] == data[i][xAxis]; 298 | } else { 299 | return d[xAxis] == data[i][xAxis] && d[color] == data[i][color]; 300 | } 301 | }, 302 | yAxis, 303 | function(d) { 304 | isValueMatched = true; 305 | return data[i][yAxis]; 306 | }); 307 | 308 | if(!isValueMatched){ 309 | this.view.data(this.config.title).insert([data[i]]); 310 | } 311 | } 312 | } 313 | 314 | //Group does not support duration update animation 315 | if (this.config.mode == "group") { 316 | this.view.update(); 317 | } else { 318 | this.view.update({duration: 200}); 319 | } 320 | }; 321 | 322 | bar.prototype.getSpec = function() { 323 | return this.spec; 324 | }; 325 | 326 | bar.prototype.setSpec = function(spec) { 327 | this.spec = spec; 328 | } 329 | 330 | 331 | function getBarMark(config, metadata){ 332 | var markContent; 333 | if (config.orientation == "left") { 334 | markContent = { 335 | "y": {"scale": "x", "field": metadata.names[config.x]}, 336 | "height": {"scale": "x", "band": true, "offset": calculateBarGap(config)}, 337 | "x": {"scale": "y", "field": metadata.names[config.y]}, 338 | "x2": {"scale": "y", "value": 0}, 339 | "fill": {"value": config.markColor}, 340 | "fillOpacity": {"value": config.fillOpacity} 341 | }; 342 | } else { 343 | markContent = { 344 | "x": {"scale": "x", "field": metadata.names[config.x]}, 345 | "width": {"scale": "x", "band": true, "offset": calculateBarGap(config)}, 346 | "y": {"scale": "y", "field": metadata.names[config.y]}, 347 | "y2": {"scale": "y", "value": 0}, 348 | "fill": {"value": config.markColor}, 349 | "fillOpacity": {"value": config.fillOpacity} 350 | }; 351 | } 352 | 353 | var mark = { 354 | "name": "bars", 355 | "type": "rect", 356 | "from": {"data": config.title}, 357 | "properties": { 358 | "update": markContent, 359 | "hover": { 360 | "fillOpacity": {"value": 0.5}, 361 | "cursor": {"value": config.hoverCursor} 362 | } 363 | } 364 | }; 365 | 366 | 367 | return mark; 368 | } 369 | 370 | function getStackBarMark(config, metadata){ 371 | var markContent; 372 | if (config.orientation == "left") { 373 | mark = { 374 | "name": "bars", 375 | "type": "rect", 376 | "from": { 377 | "data": config.title, 378 | "transform": [ 379 | { "type": "stack", 380 | "groupby": [metadata.names[config.x]], 381 | "sortby": [metadata.names[config.color]], 382 | "field":metadata.names[config.y]} 383 | ] 384 | }, 385 | "properties": { 386 | "update": { 387 | "y": {"scale": "x", "field": metadata.names[config.x]}, 388 | "height": {"scale": "x", "band": true, "offset": calculateBarGap(config)}, 389 | "x": {"scale": "y", "field": "layout_start"}, 390 | "x2": {"scale": "y", "field": "layout_end"}, 391 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 392 | "fillOpacity": {"value": config.fillOpacity} 393 | }, 394 | "hover": { 395 | "fillOpacity": {"value": 0.5}, 396 | "cursor": {"value": config.hoverCursor} 397 | } 398 | } 399 | }; 400 | } else { 401 | 402 | mark = { 403 | "name": "bars", 404 | "type": "rect", 405 | "from": { 406 | "data": config.title, 407 | "transform": [ 408 | { "type": "stack", 409 | "groupby": [metadata.names[config.x]], 410 | "sortby": [metadata.names[config.color]], 411 | "field":metadata.names[config.y]} 412 | ] 413 | }, 414 | "properties": { 415 | "update": { 416 | "x": {"scale": "x", "field": metadata.names[config.x]}, 417 | "width": {"scale": "x", "band": true, "offset": calculateBarGap(config)}, 418 | "y": {"scale": "y", "field": "layout_start"}, 419 | "y2": {"scale": "y", "field": "layout_end"}, 420 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 421 | "fillOpacity": {"value": config.fillOpacity} 422 | }, 423 | "hover": { 424 | "fillOpacity": {"value": 0.5}, 425 | "cursor": {"value": config.hoverCursor} 426 | } 427 | } 428 | }; 429 | } 430 | 431 | 432 | 433 | 434 | return mark; 435 | } 436 | 437 | function getGroupBarMark(config, metadata){ 438 | var mark; 439 | if (config.orientation == "left") { 440 | mark = { 441 | "name": "bars", 442 | "type": "group", 443 | "from": { 444 | "data": config.title, 445 | "transform": [{"type":"facet", "groupby": [metadata.names[config.x]]}] 446 | }, 447 | "properties": { 448 | "update": { 449 | "y": {"scale": "x", "field": "key"}, 450 | "height": {"scale": "x", "band": true} 451 | } 452 | }, 453 | "scales": [ 454 | { 455 | "name": "pos", 456 | "type": "ordinal", 457 | "range": "height", 458 | "domain": {"field": metadata.names[config.color]} 459 | } 460 | ], 461 | "marks": [ 462 | { 463 | "name": "bar", 464 | "type": "rect", 465 | "properties": { 466 | "update": { 467 | "y": {"scale": "pos", "field": metadata.names[config.color]}, 468 | "height": {"scale": "pos", "band": true, "offset": calculateBarGap(config)}, 469 | "x": {"scale": "y", "field": metadata.names[config.y]}, 470 | "x2": {"scale": "y", "value": 0}, 471 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 472 | "fillOpacity": {"value": config.fillOpacity} 473 | }, 474 | "hover": { 475 | "fillOpacity": {"value": 0.5}, 476 | "cursor": {"value": config.hoverCursor} 477 | } 478 | } 479 | } 480 | ] 481 | }; 482 | } else { 483 | mark = { 484 | "name": "bars", 485 | "type": "group", 486 | "from": { 487 | "data": config.title, 488 | "transform": [{"type":"facet", "groupby": [metadata.names[config.x]]}] 489 | }, 490 | "properties": { 491 | "update": { 492 | "x": {"scale": "x", "field": "key"}, 493 | "width": {"scale": "x", "band": true} 494 | } 495 | }, 496 | "scales": [ 497 | { 498 | "name": "pos", 499 | "type": "ordinal", 500 | "range": "width", 501 | "domain": {"field": metadata.names[config.color]} 502 | } 503 | ], 504 | "marks": [ 505 | { 506 | "name": "bar", 507 | "type": "rect", 508 | "properties": { 509 | "update": { 510 | "x": {"scale": "pos", "field": metadata.names[config.color]}, 511 | "width": {"scale": "pos", "band": true, "offset": calculateBarGap(config)}, 512 | "y": {"scale": "y", "field": metadata.names[config.y]}, 513 | "y2": {"scale": "y", "value": 0}, 514 | "fill": {"scale": "color", "field": metadata.names[config.color]}, 515 | "fillOpacity": {"value": config.fillOpacity} 516 | }, 517 | "hover": { 518 | "fillOpacity": {"value": 0.5}, 519 | "cursor": {"value": config.hoverCursor} 520 | } 521 | } 522 | } 523 | ] 524 | }; 525 | } 526 | return mark; 527 | } 528 | 529 | function calculateBarGap(config){ 530 | 531 | var xWidth; 532 | var widthWeight = 30; 533 | 534 | if (config.orientation == "left") { 535 | xWidth = config.height; 536 | } else { 537 | xWidth = config.width 538 | } 539 | 540 | if (config.mode == "group") { 541 | widthWeight = 80; 542 | } 543 | 544 | return -config.barGap * (xWidth/widthWeight); 545 | 546 | } -------------------------------------------------------------------------------- /samples/js/topojson.js: -------------------------------------------------------------------------------- 1 | !function() { 2 | var topojson = { 3 | version: "1.6.19", 4 | mesh: function(topology) { return object(topology, meshArcs.apply(this, arguments)); }, 5 | meshArcs: meshArcs, 6 | merge: function(topology) { return object(topology, mergeArcs.apply(this, arguments)); }, 7 | mergeArcs: mergeArcs, 8 | feature: featureOrCollection, 9 | neighbors: neighbors, 10 | presimplify: presimplify 11 | }; 12 | 13 | function stitchArcs(topology, arcs) { 14 | var stitchedArcs = {}, 15 | fragmentByStart = {}, 16 | fragmentByEnd = {}, 17 | fragments = [], 18 | emptyIndex = -1; 19 | 20 | // Stitch empty arcs first, since they may be subsumed by other arcs. 21 | arcs.forEach(function(i, j) { 22 | var arc = topology.arcs[i < 0 ? ~i : i], t; 23 | if (arc.length < 3 && !arc[1][0] && !arc[1][1]) { 24 | t = arcs[++emptyIndex], arcs[emptyIndex] = i, arcs[j] = t; 25 | } 26 | }); 27 | 28 | arcs.forEach(function(i) { 29 | var e = ends(i), 30 | start = e[0], 31 | end = e[1], 32 | f, g; 33 | 34 | if (f = fragmentByEnd[start]) { 35 | delete fragmentByEnd[f.end]; 36 | f.push(i); 37 | f.end = end; 38 | if (g = fragmentByStart[end]) { 39 | delete fragmentByStart[g.start]; 40 | var fg = g === f ? f : f.concat(g); 41 | fragmentByStart[fg.start = f.start] = fragmentByEnd[fg.end = g.end] = fg; 42 | } else { 43 | fragmentByStart[f.start] = fragmentByEnd[f.end] = f; 44 | } 45 | } else if (f = fragmentByStart[end]) { 46 | delete fragmentByStart[f.start]; 47 | f.unshift(i); 48 | f.start = start; 49 | if (g = fragmentByEnd[start]) { 50 | delete fragmentByEnd[g.end]; 51 | var gf = g === f ? f : g.concat(f); 52 | fragmentByStart[gf.start = g.start] = fragmentByEnd[gf.end = f.end] = gf; 53 | } else { 54 | fragmentByStart[f.start] = fragmentByEnd[f.end] = f; 55 | } 56 | } else { 57 | f = [i]; 58 | fragmentByStart[f.start = start] = fragmentByEnd[f.end = end] = f; 59 | } 60 | }); 61 | 62 | function ends(i) { 63 | var arc = topology.arcs[i < 0 ? ~i : i], p0 = arc[0], p1; 64 | if (topology.transform) p1 = [0, 0], arc.forEach(function(dp) { p1[0] += dp[0], p1[1] += dp[1]; }); 65 | else p1 = arc[arc.length - 1]; 66 | return i < 0 ? [p1, p0] : [p0, p1]; 67 | } 68 | 69 | function flush(fragmentByEnd, fragmentByStart) { 70 | for (var k in fragmentByEnd) { 71 | var f = fragmentByEnd[k]; 72 | delete fragmentByStart[f.start]; 73 | delete f.start; 74 | delete f.end; 75 | f.forEach(function(i) { stitchedArcs[i < 0 ? ~i : i] = 1; }); 76 | fragments.push(f); 77 | } 78 | } 79 | 80 | flush(fragmentByEnd, fragmentByStart); 81 | flush(fragmentByStart, fragmentByEnd); 82 | arcs.forEach(function(i) { if (!stitchedArcs[i < 0 ? ~i : i]) fragments.push([i]); }); 83 | 84 | return fragments; 85 | } 86 | 87 | function meshArcs(topology, o, filter) { 88 | var arcs = []; 89 | 90 | if (arguments.length > 1) { 91 | var geomsByArc = [], 92 | geom; 93 | 94 | function arc(i) { 95 | var j = i < 0 ? ~i : i; 96 | (geomsByArc[j] || (geomsByArc[j] = [])).push({i: i, g: geom}); 97 | } 98 | 99 | function line(arcs) { 100 | arcs.forEach(arc); 101 | } 102 | 103 | function polygon(arcs) { 104 | arcs.forEach(line); 105 | } 106 | 107 | function geometry(o) { 108 | if (o.type === "GeometryCollection") o.geometries.forEach(geometry); 109 | else if (o.type in geometryType) geom = o, geometryType[o.type](o.arcs); 110 | } 111 | 112 | var geometryType = { 113 | LineString: line, 114 | MultiLineString: polygon, 115 | Polygon: polygon, 116 | MultiPolygon: function(arcs) { arcs.forEach(polygon); } 117 | }; 118 | 119 | geometry(o); 120 | 121 | geomsByArc.forEach(arguments.length < 3 122 | ? function(geoms) { arcs.push(geoms[0].i); } 123 | : function(geoms) { if (filter(geoms[0].g, geoms[geoms.length - 1].g)) arcs.push(geoms[0].i); }); 124 | } else { 125 | for (var i = 0, n = topology.arcs.length; i < n; ++i) arcs.push(i); 126 | } 127 | 128 | return {type: "MultiLineString", arcs: stitchArcs(topology, arcs)}; 129 | } 130 | 131 | function mergeArcs(topology, objects) { 132 | var polygonsByArc = {}, 133 | polygons = [], 134 | components = []; 135 | 136 | objects.forEach(function(o) { 137 | if (o.type === "Polygon") register(o.arcs); 138 | else if (o.type === "MultiPolygon") o.arcs.forEach(register); 139 | }); 140 | 141 | function register(polygon) { 142 | polygon.forEach(function(ring) { 143 | ring.forEach(function(arc) { 144 | (polygonsByArc[arc = arc < 0 ? ~arc : arc] || (polygonsByArc[arc] = [])).push(polygon); 145 | }); 146 | }); 147 | polygons.push(polygon); 148 | } 149 | 150 | function exterior(ring) { 151 | return cartesianRingArea(object(topology, {type: "Polygon", arcs: [ring]}).coordinates[0]) > 0; // TODO allow spherical? 152 | } 153 | 154 | polygons.forEach(function(polygon) { 155 | if (!polygon._) { 156 | var component = [], 157 | neighbors = [polygon]; 158 | polygon._ = 1; 159 | components.push(component); 160 | while (polygon = neighbors.pop()) { 161 | component.push(polygon); 162 | polygon.forEach(function(ring) { 163 | ring.forEach(function(arc) { 164 | polygonsByArc[arc < 0 ? ~arc : arc].forEach(function(polygon) { 165 | if (!polygon._) { 166 | polygon._ = 1; 167 | neighbors.push(polygon); 168 | } 169 | }); 170 | }); 171 | }); 172 | } 173 | } 174 | }); 175 | 176 | polygons.forEach(function(polygon) { 177 | delete polygon._; 178 | }); 179 | 180 | return { 181 | type: "MultiPolygon", 182 | arcs: components.map(function(polygons) { 183 | var arcs = []; 184 | 185 | // Extract the exterior (unique) arcs. 186 | polygons.forEach(function(polygon) { 187 | polygon.forEach(function(ring) { 188 | ring.forEach(function(arc) { 189 | if (polygonsByArc[arc < 0 ? ~arc : arc].length < 2) { 190 | arcs.push(arc); 191 | } 192 | }); 193 | }); 194 | }); 195 | 196 | // Stitch the arcs into one or more rings. 197 | arcs = stitchArcs(topology, arcs); 198 | 199 | // If more than one ring is returned, 200 | // at most one of these rings can be the exterior; 201 | // this exterior ring has the same winding order 202 | // as any exterior ring in the original polygons. 203 | if ((n = arcs.length) > 1) { 204 | var sgn = exterior(polygons[0][0]); 205 | for (var i = 0, t; i < n; ++i) { 206 | if (sgn === exterior(arcs[i])) { 207 | t = arcs[0], arcs[0] = arcs[i], arcs[i] = t; 208 | break; 209 | } 210 | } 211 | } 212 | 213 | return arcs; 214 | }) 215 | }; 216 | } 217 | 218 | function featureOrCollection(topology, o) { 219 | return o.type === "GeometryCollection" ? { 220 | type: "FeatureCollection", 221 | features: o.geometries.map(function(o) { return feature(topology, o); }) 222 | } : feature(topology, o); 223 | } 224 | 225 | function feature(topology, o) { 226 | var f = { 227 | type: "Feature", 228 | id: o.id, 229 | properties: o.properties || {}, 230 | geometry: object(topology, o) 231 | }; 232 | if (o.id == null) delete f.id; 233 | return f; 234 | } 235 | 236 | function object(topology, o) { 237 | var absolute = transformAbsolute(topology.transform), 238 | arcs = topology.arcs; 239 | 240 | function arc(i, points) { 241 | if (points.length) points.pop(); 242 | for (var a = arcs[i < 0 ? ~i : i], k = 0, n = a.length, p; k < n; ++k) { 243 | points.push(p = a[k].slice()); 244 | absolute(p, k); 245 | } 246 | if (i < 0) reverse(points, n); 247 | } 248 | 249 | function point(p) { 250 | p = p.slice(); 251 | absolute(p, 0); 252 | return p; 253 | } 254 | 255 | function line(arcs) { 256 | var points = []; 257 | for (var i = 0, n = arcs.length; i < n; ++i) arc(arcs[i], points); 258 | if (points.length < 2) points.push(points[0].slice()); 259 | return points; 260 | } 261 | 262 | function ring(arcs) { 263 | var points = line(arcs); 264 | while (points.length < 4) points.push(points[0].slice()); 265 | return points; 266 | } 267 | 268 | function polygon(arcs) { 269 | return arcs.map(ring); 270 | } 271 | 272 | function geometry(o) { 273 | var t = o.type; 274 | return t === "GeometryCollection" ? {type: t, geometries: o.geometries.map(geometry)} 275 | : t in geometryType ? {type: t, coordinates: geometryType[t](o)} 276 | : null; 277 | } 278 | 279 | var geometryType = { 280 | Point: function(o) { return point(o.coordinates); }, 281 | MultiPoint: function(o) { return o.coordinates.map(point); }, 282 | LineString: function(o) { return line(o.arcs); }, 283 | MultiLineString: function(o) { return o.arcs.map(line); }, 284 | Polygon: function(o) { return polygon(o.arcs); }, 285 | MultiPolygon: function(o) { return o.arcs.map(polygon); } 286 | }; 287 | 288 | return geometry(o); 289 | } 290 | 291 | function reverse(array, n) { 292 | var t, j = array.length, i = j - n; while (i < --j) t = array[i], array[i++] = array[j], array[j] = t; 293 | } 294 | 295 | function bisect(a, x) { 296 | var lo = 0, hi = a.length; 297 | while (lo < hi) { 298 | var mid = lo + hi >>> 1; 299 | if (a[mid] < x) lo = mid + 1; 300 | else hi = mid; 301 | } 302 | return lo; 303 | } 304 | 305 | function neighbors(objects) { 306 | var indexesByArc = {}, // arc index -> array of object indexes 307 | neighbors = objects.map(function() { return []; }); 308 | 309 | function line(arcs, i) { 310 | arcs.forEach(function(a) { 311 | if (a < 0) a = ~a; 312 | var o = indexesByArc[a]; 313 | if (o) o.push(i); 314 | else indexesByArc[a] = [i]; 315 | }); 316 | } 317 | 318 | function polygon(arcs, i) { 319 | arcs.forEach(function(arc) { line(arc, i); }); 320 | } 321 | 322 | function geometry(o, i) { 323 | if (o.type === "GeometryCollection") o.geometries.forEach(function(o) { geometry(o, i); }); 324 | else if (o.type in geometryType) geometryType[o.type](o.arcs, i); 325 | } 326 | 327 | var geometryType = { 328 | LineString: line, 329 | MultiLineString: polygon, 330 | Polygon: polygon, 331 | MultiPolygon: function(arcs, i) { arcs.forEach(function(arc) { polygon(arc, i); }); } 332 | }; 333 | 334 | objects.forEach(geometry); 335 | 336 | for (var i in indexesByArc) { 337 | for (var indexes = indexesByArc[i], m = indexes.length, j = 0; j < m; ++j) { 338 | for (var k = j + 1; k < m; ++k) { 339 | var ij = indexes[j], ik = indexes[k], n; 340 | if ((n = neighbors[ij])[i = bisect(n, ik)] !== ik) n.splice(i, 0, ik); 341 | if ((n = neighbors[ik])[i = bisect(n, ij)] !== ij) n.splice(i, 0, ij); 342 | } 343 | } 344 | } 345 | 346 | return neighbors; 347 | } 348 | 349 | function presimplify(topology, triangleArea) { 350 | var absolute = transformAbsolute(topology.transform), 351 | relative = transformRelative(topology.transform), 352 | heap = minAreaHeap(); 353 | 354 | if (!triangleArea) triangleArea = cartesianTriangleArea; 355 | 356 | topology.arcs.forEach(function(arc) { 357 | var triangles = [], 358 | maxArea = 0, 359 | triangle; 360 | 361 | // To store each point’s effective area, we create a new array rather than 362 | // extending the passed-in point to workaround a Chrome/V8 bug (getting 363 | // stuck in smi mode). For midpoints, the initial effective area of 364 | // Infinity will be computed in the next step. 365 | for (var i = 0, n = arc.length, p; i < n; ++i) { 366 | p = arc[i]; 367 | absolute(arc[i] = [p[0], p[1], Infinity], i); 368 | } 369 | 370 | for (var i = 1, n = arc.length - 1; i < n; ++i) { 371 | triangle = arc.slice(i - 1, i + 2); 372 | triangle[1][2] = triangleArea(triangle); 373 | triangles.push(triangle); 374 | heap.push(triangle); 375 | } 376 | 377 | for (var i = 0, n = triangles.length; i < n; ++i) { 378 | triangle = triangles[i]; 379 | triangle.previous = triangles[i - 1]; 380 | triangle.next = triangles[i + 1]; 381 | } 382 | 383 | while (triangle = heap.pop()) { 384 | var previous = triangle.previous, 385 | next = triangle.next; 386 | 387 | // If the area of the current point is less than that of the previous point 388 | // to be eliminated, use the latter's area instead. This ensures that the 389 | // current point cannot be eliminated without eliminating previously- 390 | // eliminated points. 391 | if (triangle[1][2] < maxArea) triangle[1][2] = maxArea; 392 | else maxArea = triangle[1][2]; 393 | 394 | if (previous) { 395 | previous.next = next; 396 | previous[2] = triangle[2]; 397 | update(previous); 398 | } 399 | 400 | if (next) { 401 | next.previous = previous; 402 | next[0] = triangle[0]; 403 | update(next); 404 | } 405 | } 406 | 407 | arc.forEach(relative); 408 | }); 409 | 410 | function update(triangle) { 411 | heap.remove(triangle); 412 | triangle[1][2] = triangleArea(triangle); 413 | heap.push(triangle); 414 | } 415 | 416 | return topology; 417 | }; 418 | 419 | function cartesianRingArea(ring) { 420 | var i = -1, 421 | n = ring.length, 422 | a, 423 | b = ring[n - 1], 424 | area = 0; 425 | 426 | while (++i < n) { 427 | a = b; 428 | b = ring[i]; 429 | area += a[0] * b[1] - a[1] * b[0]; 430 | } 431 | 432 | return area * .5; 433 | } 434 | 435 | function cartesianTriangleArea(triangle) { 436 | var a = triangle[0], b = triangle[1], c = triangle[2]; 437 | return Math.abs((a[0] - c[0]) * (b[1] - a[1]) - (a[0] - b[0]) * (c[1] - a[1])); 438 | } 439 | 440 | function compareArea(a, b) { 441 | return a[1][2] - b[1][2]; 442 | } 443 | 444 | function minAreaHeap() { 445 | var heap = {}, 446 | array = [], 447 | size = 0; 448 | 449 | heap.push = function(object) { 450 | up(array[object._ = size] = object, size++); 451 | return size; 452 | }; 453 | 454 | heap.pop = function() { 455 | if (size <= 0) return; 456 | var removed = array[0], object; 457 | if (--size > 0) object = array[size], down(array[object._ = 0] = object, 0); 458 | return removed; 459 | }; 460 | 461 | heap.remove = function(removed) { 462 | var i = removed._, object; 463 | if (array[i] !== removed) return; // invalid request 464 | if (i !== --size) object = array[size], (compareArea(object, removed) < 0 ? up : down)(array[object._ = i] = object, i); 465 | return i; 466 | }; 467 | 468 | function up(object, i) { 469 | while (i > 0) { 470 | var j = ((i + 1) >> 1) - 1, 471 | parent = array[j]; 472 | if (compareArea(object, parent) >= 0) break; 473 | array[parent._ = i] = parent; 474 | array[object._ = i = j] = object; 475 | } 476 | } 477 | 478 | function down(object, i) { 479 | while (true) { 480 | var r = (i + 1) << 1, 481 | l = r - 1, 482 | j = i, 483 | child = array[j]; 484 | if (l < size && compareArea(array[l], child) < 0) child = array[j = l]; 485 | if (r < size && compareArea(array[r], child) < 0) child = array[j = r]; 486 | if (j === i) break; 487 | array[child._ = i] = child; 488 | array[object._ = i = j] = object; 489 | } 490 | } 491 | 492 | return heap; 493 | } 494 | 495 | function transformAbsolute(transform) { 496 | if (!transform) return noop; 497 | var x0, 498 | y0, 499 | kx = transform.scale[0], 500 | ky = transform.scale[1], 501 | dx = transform.translate[0], 502 | dy = transform.translate[1]; 503 | return function(point, i) { 504 | if (!i) x0 = y0 = 0; 505 | point[0] = (x0 += point[0]) * kx + dx; 506 | point[1] = (y0 += point[1]) * ky + dy; 507 | }; 508 | } 509 | 510 | function transformRelative(transform) { 511 | if (!transform) return noop; 512 | var x0, 513 | y0, 514 | kx = transform.scale[0], 515 | ky = transform.scale[1], 516 | dx = transform.translate[0], 517 | dy = transform.translate[1]; 518 | return function(point, i) { 519 | if (!i) x0 = y0 = 0; 520 | var x1 = (point[0] - dx) / kx | 0, 521 | y1 = (point[1] - dy) / ky | 0; 522 | point[0] = x1 - x0; 523 | point[1] = y1 - y0; 524 | x0 = x1; 525 | y0 = y1; 526 | }; 527 | } 528 | 529 | function noop() {} 530 | 531 | if (typeof define === "function" && define.amd) define(topojson); 532 | else if (typeof module === "object" && module.exports) module.exports = topojson; 533 | else this.topojson = topojson; 534 | }(); 535 | --------------------------------------------------------------------------------