Code Monkey home page Code Monkey logo

ng-vega's Introduction

ng-vega NPM version

Angular directive for rendering Vega specs. This project was forked and modified from angular-vega which was written for Vega 1 and became inactive. The current version of ng-vega supports Vega 2.

Demo

  • Simple demo -- Select dataset/renderer to see the chart changes and see the code to see how it was implemented.
  • Vega editor demo -- Implement Vega editor using ng-vega.

For more information about Vega, please refer to official documentation.

Usage

angular.module('exampleApp', ['ngVega'])
<div vega spec="spec" vega-data="testData" vega-renderer="'svg'" vega-on-parse="myCallback(view)"></div>
  • spec is $scope.spec in your controller.

  • vega-data (optional) can be used to pass dynamic data. In the example above, it is bound to $scope.testData. Data can be a function to modify the values (Vega 2 syntax) or raw values (and ng-vega will convert it to a function to make it work for you).

$scope.testData = {
  // function to modify dataset name "table"
  table: function(data){
    data.remove(function(d){return true;})
      insert([{a: 3}, {a: 4}])
  }
}

$scope.testData = {
  // raw values for dataset name "table"
  table: [{a: 1},{a: 2}]
}
  • vega-renderer (optional) can be used to set renderer ('canvas' or 'svg'). Don't forget the quote.

  • vega-on-parse (optional) can be used to get notified when spec is parsed and to receive a handle to the View Component that Vega has built for the corresponding chart. In the example above, this parameter is bound to $scope.myCallback.

$scope.myCallback = function(view) {
  // perform any work you need to do once the spec is parsed
  console.log('Vega spec has been parsed.');
  // make use of the View Component API as you wish
  view.on('click', function(event, item) {
    console.log('clicked on ' + JSON.stringify(item));
  });
};

Installation

bower install ng-vega --save

or

npm install ng-vega --save

Import into your project

Angular module ngVega will be available once you do one of the following:

Choice 1. Global

Adding this library via <script> tag is the simplest way.

<script src="path/to/angular.js"></script>
<script src="path/to/vega.js"></script>
<script src="path/to/ng-vega.min.js"></script>
Choice 2: AMD

If you use requirejs, this library support AMD out of the box.

require.config({
  paths: {
    angular:   'path/to/angular',
    vega:      'path/to/vega',
    'ng-vega': 'path/to/ng-vega'
  }
});
require(['ng-vega'], function() {
  // do something
});
Choice 3: node.js / browserify
require('ng-vega');

Author

Krist Wongsuphasawat / @kristw

Copyright (c) 2016 Krist Wongsuphasawat. MIT License

ng-vega's People

Contributors

eptify avatar kristw avatar mhuyck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ng-vega's Issues

Does it work with Vega 2?

I'm trying to get this working with the latest version of vega but I keep having issues. I then noticed all your examples are using Vega 1.5. Does this work with Vega 2?

Can't initate world map chart

I have the following configuration ($scope.spec) for a Vega world map chart

{
      width: 900,
      height: 500,
      autosize: 'none',    

      encode: {
        update: {
          fill: {signal: 'background'}
        }
      },
    
      signals: [
        {
          name: 'type',
          value: 'mercator'
        },
        { name: 'scale', value: 150 },
        { name: 'rotate0', value: 0 },
        { name: 'rotate1', value: 0 },
        { name: 'rotate2', value: 0 },
        { name: 'center0', value: 0 },
        { name: 'center1', value: 0 },
        { name: 'translate0', update: 'width / 2' },
        { name: 'translate1', update: 'height / 2' },
    
        { name: 'graticuleDash', value: 0 },
        { name: 'borderWidth', value: 1 },
        { name: 'background', value: '#acacac'
          },
        { name: 'invert', value: false }
      ],
    
      projections: [
        {
          name: 'projection',
          type: {signal: 'type'},
          scale: {signal: 'scale'},
          rotate: [
            {signal: 'rotate0'},
            {signal: 'rotate1'},
            {signal: 'rotate2'}
          ],
          center: [
            {signal: 'center0'},
            {signal: 'center1'}
          ],
          translate: [
            {signal: 'translate0'},
            {signal: 'translate1'}
          ]
        }
      ],
    
      data: [
        {
          name: 'world',
          url: '../../../chartsData/world-110m.json',
          format: {
            type: 'topojson',
            feature: 'countries'
          }
        }
      ],
    
      marks: [
        {
          type: 'shape',
          from: {data: 'world'},
          encode: {
            update: {
              strokeWidth: {signal: '+borderWidth'},
              stroke: {signal: "invert ? '#777': '#fff'"},
              fill: {signal: "invert ? '#fff': '#abcdef'"},
              zindex: {value: 0}
            },
            hover: {
              strokeWidth: {signal: '+borderWidth + 1'},
              stroke: {value: '#10ca5a'},
              zindex: {value: 1}
            }
          },
          transform: [
            { type: 'geoshape', projection: 'projection' }
          ]
        }
      ]
    }
  }

And when I create an instance of the chart, according to the example I have the following errors:
[Vega Err] PARSE DATA FAILED: world SyntaxError: Unexpected token < in JSON at position 0
[Vega Err] PARSE DATA FAILED: world SyntaxError: Unexpected token < in JSON at position 0

I've reviewed my JSON file, and seems like it's not being added to my configuration. I also even let the url property in blank and that error stills appearing.

How can I instantiate a world map chart, or what is missing on my configuration?

Thanks in advance.

Publish latest package.json to npm

The latest version of ng-vega published on npm has a dependency on Vega "~1.5.0". That version of Vega seemed to either require the use of node-gyp or had a dependency which did. For those of us who don't want to install Python 2 (and any other native module build dependenies) in order to get ng-vega through npm, it would be great if you could publish you latest package.json to npm.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.