Wijmo UI for the Web
AngularJS and wijbarchart
Wijmo User Guide > AngularJS Directives > AngularJS and Chart Widgets > AngularJS and wijbarchart

In this Angular getting started guide, you'll learn how to use the BarChart widget in an HTML project using HTML markup, jQuery script, and AngularJS directives.

For a more complex sample that has the controller and model in separate files, see Wijmo BarChart in the AngularJS Directive Gallery on our web site.

  1. To create a new HTML page in your favorite text editor, add the following code and save the document with an .html extension. Notice that the <HTML> and <body> tags are different from our usual tags.
    • The <HTML> tag is marked as an Angular app with the ng-app directive, in our script, we will specify that it uses Wijmo.
    • The <body> tag is used to define the scope of the Angular controller that we create. You can use another tag such as a <div> for the scope, but for our purposes, we will use the entire body. The ng-controller directive specifies the controller to use within this scope.

    Drop down and copy markup

    Paste in your favorite text editor.
    Copy Code
    <!DOCTYPE HTML>
    <HTML ng-app="MyApp">
    <head>
    </head>
    <body ng-controller="MyController">
    </body>
    </HTML>
    
  2. Add links to the dependencies to your HTML page within the <head> tags. Find the latest dependencies in the content delivery network (CDN) file at wijmo cdn. Along with our usual references, we add two script references for Angular that must come after the jQuery references, so they are the last two lines:
    • One is for AngularJS itself.
    • The other is for Wijmo's Angular javascript integration library.

    Drop down and copy references to paste inside the head tags

    References
    Copy Code
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js" type="text/javascript"></script>
    
    <!--Theme-->
    <link href="http://cdn.wijmo.com/themes/aristo/jquery-wijmo.css" rel="stylesheet" type="text/css" />
    
    <!--Wijmo Widgets CSS-->
    <link href="http://cdn.wijmo.com/jquery.wijmo-pro.all.3.20183.140.min.css" rel="stylesheet" type="text/css" />
    
    <!-- Wijmo Scripts -->
    <script src="http://cdn.wijmo.com/jquery.wijmo-open.all.3.20161.90.min.js" type="text/javascript"></script>
    <script src="http://cdn.wijmo.com/jquery.wijmo-pro.all.3.20161.90.min.js" type="text/javascript"></script>
    
    <!-- Angular -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
    <script src="http://cdn.wijmo.com/interop/angular.wijmo.3.20161.90.min.js"></script>
  3. Within the <head> tags, below the references, add the following script to set the app to use Wijmo, and to create a model and controller.
    • We use a model to specify two data fields that we link to data supplied in the controller.
    • We use a $scope parameter on the controller to control data context for the model
    • We add an optional $locale parameter on the controller.
    • We also add a MyTooltips function to use for the hint option.
    • We add six data rows to the list in the controller.
    To see how this is done in jQuery without Angular, see Format All Numeric Values.
    Script
    Copy Code
    <script type="text/javascript">
        var app = angular.module("MyApp", ["wijmo"]);
        //AutoMaker class
        function AutoMaker(data) {
            this.Maker = data.Maker;
            this.Sales = data.Sales;
        };
        
        //MyTooltips class
        function MyTooltips(){
            return this.data.label + '\n ' +  Globalize.format(this.y, "p0") + '';
        };
    
        function MyController($scope, $locale) {
            $scope.list = [
                  new AutoMaker({ Maker: "Ford", Sales: .05 }),
                  new AutoMaker({ Maker: "GM", Sales: .04 }),
                  new AutoMaker({ Maker: "Chrysler", Sales: .21 }),
                  new AutoMaker({ Maker: "Toyota", Sales: .27 }),
                  new AutoMaker({ Maker: "Nissan", Sales: .1 }),
                  new AutoMaker({ Maker: "Honda", Sales: .24 })
            ];
        }
    </script>
  4. Add the following markup within the <body> tags to create the widget.
    • We use a <wij-barchart> directive to create the widget, and set several options as attributes within the element:
      • We set the dataSource option (data-source directive) to specify the "list" from our controller.
      • We set the height and width options.
      • We set the chartLabelFormatString option (chart-label-format-string directive) to use percentage formatting with no decimal places.
    • We use nested elements in the markup to set the axisseriesList, and hint options.
      • Inside our axis directive, we nest the x and y axes, and set some options.
      • Inside our series-list directive, we nest a data directive with nested x and y options that bind to the data fields in the model.
      • In the hint directive, we set the content to the MyTooltips function we created in the script.
    Markup
    Copy Code
    <wij-barchart data-source="list" height="400" width="600" chart-label-format-string="p0">
        <axis>
            <x text="Auto Makers"></x>
            <y text="Percent Increase in Sales Over Prior Year" anno-format-string="p0"></y>
        </axis>
        <series-list>
            <series label="2012 Auto Sales">
                <data>
                    <x bind="Maker"></x>
                    <y bind="Sales"></y>
                </data>
            </series>
        </series-list>
        <hint content="MyTooltips"></hint>
    </wij-barchart>
  5. Save your HTML file and open it in a browser. The widget appears like the following live widget.
    MISSING WIDGET TYPE: The "Live Widget" Widget Type could not be found. The "Live Widget" Widget Type may have been deleted since this Widget was created.
See Also

How To

Reference