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)

Animation and live data (Intermediate)


This recipe concentrates on a major and common widget for animations, known as the wijexpander. We will also see how to pass, handle, and manage live data via a Wijmo widget, which will in turn provide useful information to the users.

Getting ready

The wijexpander object works in a way that allows the user to expand a bar or region, which could be a div HTML element, and have visibility into embedded web content such as a web page like CNN. We will see how to use the wijexpander object to display embedded web content via a given URL. Copy or recreate the following code:

<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" />
<style type="text/css">
    #my-wij-expander
    {
        width: 550px;
    }
    #webpage-content{
      height: 350px;
    }
</style>

</head>
<body>
<div id="my-wij-expander">
    <h3>cnn.com</h3>
    <div id="webpage-content"></div>
</div>

<script id="scriptInit" type="text/javascript">
    $(document).ready(function () { 
        $("#my-wij-expander").wijexpander({ 
            contentUrl: "http://www.cnn.com",
            expandDirection: "top" //This expands the element
        });
    });
</script>
</html>

How to do it...

  1. First of all, we reference all the requisite code we need to run Wijmo.

  2. Next, we create a div tag where the referenced URL content from CNN will be embedded.

  3. Then, we create a script that instantiates a wijexpander widget by applying the necessary configurations for our expected expansion behavior and referencing the URL with which we intend to display its content.

  4. Lastly, we add some basic styling to our content areas. If the code runs successfully, we should have a wijexpander object similar to the following screenshot:

  5. Clicking on the expansion arrow will collapse the gray div area, thereby revealing the hidden URL content (CNN in this case) as follows:

There's more...

Here are a few other common options or configurations that would help you further customize your wijexpander widget:

  • disabled: This option determines whether to disable or enable the behavior of the widget. If set to false, the widget becomes enabled. An example is shown as follows:

    $("#myPrivateElement").wijexpander({ disabled: true });
  • expandDirection: This option determines the direction that the content will expand to, as used in the preceding code. The available options for this property are top, left, right, and bottom. An example is shown as follows:

        $("#my-wij-expander").wijexpander({ expandDirection: "left" });

Wijmo expanders also have events and callbacks that are activated before and/or after certain events. For more information on wijexpander widgets, visit the following link:

http://wijmo.com/wiki/index.php/Expander

Live data

Wijmo has claimed that all their charts have been optimized to support live data streaming. Data routinely gets fed into the widgets at any given frequency. This data automatically and continually recalculates and, subsequently, redraws the chart to convey useful desired information to the user. We will illustrate this using a Wijmo line chart, which displays information as a series of data points connected by straight point segments.

Enter the following code into your favorite code editor:

<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" />
<script id="scriptInit" type="text/javascript">
        var myX = [], myY = []; 
        var randomValuesCount = 10; 
        var duration = 3000; 
        var dx = 0; 
        var intervalRandomData = null; 
  
        $(document).ready(function () { 
            for (dx = 0; dx < randomValuesCount; dx++) { 
                myX.push(dx); 
//random Y data points
                myY.push(createRandomValue()); 
            } 
      //Instantiating the wijlinechart on #wijlinechart
            $("#wijlinechart").wijlinechart({ 
                showChartLabels: false, 
                width: 700, 
                height: 425, 
                shadow: false, 
                animation: { 
                    enabled: false
                }, 
                seriesTransition: { 
                    enabled: false
                }, 
                legend: { visible: false }, 
                hint: { enable: false }, 
                header: { text: "Wijmo Live Data Simulation" }, 
                axis: 
                { 
                    y: { min: 0, max: 100, autoMin:false, autoMax: false } 
                }, 
//creating the data points for the chart
                seriesList: [ 
                    { 
                        data: { 
                            x: myX, 
                            y: myY 
                        }, 
                        markers: { 
                            visible: true, 
                            type: "circle"
                        } 
                    } 
                ], 
                seriesStyles: [{ "stroke-width": 3, stroke: "#00a6dd"}], 
                seriesHoverStyles: [{ "stroke-width": 4}] 
  
            }); 
  
            doAnimate(); 
  
            intervalRandomData = setInterval(function () { 
                $("#wijlinechart").wijlinechart("addSeriesPoint", 0, { x: dx++, y: createRandomValue() }, true); //keep adding points to the series
                doAnimate(); 
            }, duration); 
        }); 
  
        function doAnimate() { 
            var path = $("#wijlinechart").wijlinechart("getLinePath", 0), //handle to the path
                markers = $("#wijlinechart").wijlinechart("getLineMarkers", 0), //handle to the markers
                box = path.getBBox(), 
                width = $("#wijlinechart").wijlinechart("option", "width") / 10, 
                anim = Raphael.animation({transform: Raphael.format("...t{0},0", -width)}, duration); //how we want to animate. The animation mechanism is referenced by variable anim.
            path.animate(anim); //animate the path using anim. 
            if (path.shadow) { 
                var pathShadow = path.shadow; 
                pathShadow.animate(anim); 
            } 
            markers.animate(anim); //without this line, the markers will not animate or move along with the path. We also want to animate the markers using anim.
          var rect = box.x + " " + (box.y - 5) + " " + box.width + " " + (box.height + 10); //Comment A
            path.wijAttr("clip-rect", rect); //Comment B
            markers.attr("clip-rect", rect); //Comment C
            //Comments A, B, and C code lines prevent the line chart animation from going outside the chart.
        } 
  
        function createRandomValue() { 
            var val = Math.round(Math.random() * 100); 
            return val; 
        } 
  
    </script>
    </head>
    <body>
    <div> 
        <div> 
            <h2>Streaming Live Random Data</h2> 
        </div> 
        <div class="main"> 
            <div id="wijlinechart"></div> 
            <div class="demo-options"></div> 
        </div> 
        <div class="footer"> 
            <p> 
        This demo simulates streaming live randomized data using the <u>getLinePath</u> and <u>getLineMarkers</u> methods. 
            </p> 
        </div> 
    </div>
</body>
</html>

If the preceding code runs successfully, we should have an animated chart that looks like this:

The preceding code that generated this animation is properly commented to give reasons as to why certain lines were coded the way they were. In a nutshell, anyone could customize the code to stream live data from a server or a constantly-fed spreadsheet or XML data, using the JavaScript calls within certain timed intervals. The JavaScript function setInterval() is also pretty helpful in achieving this.

Note

The preceding code is similar to the one found at http://wijmo.com/demo/explore/?widget=LineChart&sample=Streaming%20data.