Book Image

Instant Wijmo Widgets How-to

By : Tochi Eke-Okoro
Book Image

Instant Wijmo Widgets How-to

By: Tochi Eke-Okoro

Overview of this book

With Wijmo you will always be one step ahead of regular UI developers. Wijmo doesn't require a re-invention of the wheel; it is written with JQuery, so it supports regular JQuery and even native Javascript. You can initialize any widget you want to use for your project and customize its functionality and styling. With Wijmo, the possibilities are limitless!!"Instant Wijmo Widgets How-to" is perfect for aspiring and experienced UI developers. It describes basic techniques of initializing and deploying various UI widgets that serve a specific purpose. It is an ideal book for aspiring and experienced UI developers. It describes basic techniques for initializing and deploying various UI widgets that serve a specific purpose."Instant Wijmo Widgets How-to" is structured to take you from easy to seemingly difficult topics. This implies that some widgets are easier to initialize than others. However the overall deployment of a widget is basic and straightforward, without the complexities encountered in various JQuery plugins. Every topic discussed in this book is interesting, engaging, and exciting. There is no strict direction from one chapter to the next.. Most chapters are independent of each other. This creates the flexibility to delve into just what you need to resolve a particular task. "Instant Wijmo Widgets How-to" serves as an important inventory for a UI developer, equipping the reader with skills to solve most UI related issues.
Table of Contents (7 chapters)

Bar chart (Simple)


According to Wikipedia, a bar chart or bar graph is a chart with rectangular bars with lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A vertical bar chart is sometimes called a column bar chart.

Getting ready

Before we fully jump into bar charts, it is important to have some insight into Wijmo dependencies and how to reference them in our source code.

Having listed the requisite Wijmo dependencies, we will see how to reference them in any project and customize them further. All the latest dependencies can be found at http://wijmo.com/downloads/#wijmo-cdn.

How to do it…

  1. Create an HTML file and reference the dependencies listed in the preceding table. We can achieve this by inserting the links listed in the preceding table.

    <html>
        <head>
      <!--jQuery References-->
      <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.1.min.js" 
           type="text/javascript"></script>
      <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.17/jquery-ui.min.js" type="text/javascript"></script>
      <!--Wijmo Widgets JavaScript-->
      <script src="http://cdn.wijmo.com/jquery.wijmo-open.all.2.0.0.min.js"  
           type="text/javascript"></script>
      <script src=http://cdn.wijmo.com/jquery.wijmo-complete.all.2.0.0.min.js"
          type="text/javascript"></script>
     <!--Theme-->
     <link href="http://cdn.wijmo.com/themes/rocket/jquery-wijmo.css" rel="stylesheet" 
          type="text/css" title="rocket-jqueryui" />
     <!--Wijmo Widgets CSS-->
     <link href="http://cdn.wijmo.com/jquery.wijmo-complete.all.2.0.0.min.css" 
     rel="stylesheet" type="text/css" />
        </head>
        <body></body>
    </html>
  2. The wijbarchart object needs a target in the HTML document to render itself to. Create a div tag within the HTML tags, with a reference ID or class for wijbarchart to render to.

    <div id="wijbarchart" class="ui-widget ui-widget-content ui-corner-all" style="width:    400px;height: 300px"></div>
  3. Let us initialize the wijbarchart object and populate it with some data. This can be achieved via the following jQuery code:

    <script id="scriptInit" type="text/javascript">
      //wait for the page to completely load…
            $(document).ready(function () {
                $("#wijbarchart").wijbarchart({
    
         //define the values for x and y axes…
                    axis: {
                        y: {
                            text: "Total Automobile Sales (Per Hundred Thousand)"
                        },
                        x: {
                            text: ""
                        }
                    },
          //the hint will display on hover of the chart
                    hint: {
                        content: function () {
                            return this.data.label + '\n ' + this.y + '';
                        }
                    },
         //The chart's title or header
                    header: {
                        text: "US Toyota Automobile Statistics (Dummy Data)"
                    },
         //seriesList defines the label, legend and data for x and y…
                    seriesList: [{
                        label: "US",
                        legendEntry: true,
                        data: { x: ['Toyota Camry', 'Toyota Corolla', 'Toyota Sienna'], 
          y:    [12.35, 21.50, 30.56] }
                    }],
         //define the color, stroke and opacity for the chart
                    seriesStyles: [{
                        fill: "#8ede43", stroke: "#7fc73c", opacity: 0.8
                    }],
                    
                    seriesHoverStyles: [{ 
    "stroke-width": "1.5", opacity: 1
                    }]
                });
            });    
        </script>
  4. Now we save the code and run the widget by launching the HTML file in any web browser of our choice. The wijbarchart object shown will be similar to the following screenshot:

There's more...

There are various aspects of the wijbarchart object one might want to customize. Let us customize the appearance of the wijbarchart object by changing the color of the bars, applying some gradient to it, and specifying the opacity.

Changing the a ppearance of our wijbarchart object

Changing the appearance of a Wijmo bar chart is easily achievable by updating the seriesStyles property as follows:

  seriesStyles: [{
                fill: "40-#BD0070-#718680", stroke: "#1261C0", opacity: 0.5
               }]

The fill property of the object in the seriesStyles array is set to the gradient value "40-#BD0070-#718680". If we run the code after replacing the previous seriesStyles value with the new one, we will have something like this:

Another useful customization is the ability to rotate labels on the axes. This will slant the labels in a specified angle of your choice. To achieve this, we would have to update the axis property of our wijbarchart object as follows:

axis: {
                y: {
                    text: "Total Automobile Sales",
                     
                },
                x: {
                    text: "",
                    labels: {
                        style: {
                            rotation: -45
                        }
                    }
                }
            },

Applying this axis property update to our wijbarchart object and running it will render a widget similar to this:

This completes our section on creating a simple Wijmo bar chart widget. This has obviously been a simple and painless process. We set up all references or links to the requisite Wijmo files, which are available and hosted in the Wijmo CDN, as listed in the preceding table. We continued to develop the wijbarchart object by setting the various configurations required for a proper widget display, such as the axis, hint, header, seriesList, seriesStyles, and seriesHoverStyles properties.

These configurations, also known as options, are not the only ones available for creating a bar chart widget. The team at Wijmo actually did create a ton of options for user customizations; a few useful Wijmo bar chart options are listed as follows:

  • animation: This option handles all animation effects, such as easing and duration. All charts are animated by default. By animation, I refer to the duration for completely drawing the chart. Its default value is {enabled: true, duration: 400}.

    Here is an example of how to use the animation property:

                $("#wijbarchart).wijbarchart({
        animation:{
            enabled:true,
            duration:800
          }...
        });
  • footer: This option designates the footer of the chart widget. Its default value is {visible:false, style:{fill:"#fff", stroke:"none"}, textStyle:{fill:"#000", stroke:"none"}, compass:"south", orientation:"horizontal"}.

    Here is an example of how to use the footer property:

    $("#wijbarchart").wijbarchart({
       footer: {
          text:"widget footer",
          style:{
             fill:"#f1f1f1",
             stroke:"#010101"
          }
      }
    });
  • horizontal: This option value determines whether to render a horizontal or vertical bar chart. Its default value is true.

    Here is an example of how to use the horizontal property:

          $("#wijbarchart).wijbarchart({ 
             horizontal: true
           });

See also

For more information on Wijmo bar chart options, please visit the Wijmo bar chart wiki at http://wijmo.com/wiki/index.php/Barchart.