楼主: kychan
7953 58

【2015新书】Data Visualization with JavaScript [推广有奖]

41
RollingRock 发表于 2015-6-22 06:03:08
thanks a ton...a great book!

42
sgcc_zhs 发表于 2015-6-26 15:30:20
回复可见,回复可见 回复可见 。。。

43
wininghe 发表于 2015-10-14 10:28:27
Subjective Socioeconomic Status Predicts Human Ventral Striatal Responses to Social Status Information

44
Lisrelchen 发表于 2015-10-14 11:08:15

Step 1: Include the Required JavaScript Libraries

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <title></title>
  6.   </head>
  7.   <body>
  8.     <!-- Content goes here -->
  9.     <!--[if lt IE 9]><script src="js/excanvas.min.js"></script><![endif]-->
  10.     <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
  11.     </script>
  12.     <script src="//cdnjs.cloudflare.com/ajax/libs/flot/0.7/jquery.flot.min.js">
  13.     </script>
  14.   </body>
  15. </html>
  16. As you can see, we’re including the JavaScript libraries at the end of the document. This approach lets the browser load the document’s entire HTML markup and begin laying out the page while it waits for the server to provide the JavaScript libraries.
复制代码

45
Lisrelchen 发表于 2015-10-14 11:08:55

Step 2: Set Aside a <div> Element to Hold the Chart

  1. Step 2: Set Aside a <div> Element to Hold the Chart
  2. Within our document, we need to create a <div> element to contain the chart we’ll construct. This element must have an explicit height and width, or Flot won’t be able to construct the chart. We can indicate the element’s size in a CSS style sheet, or we can place it directly on the element itself. Here’s how the document might look with the latter approach.

  3.    <!DOCTYPE html>
  4.    <html lang="en">
  5.      <head>
  6.        <meta charset="utf-8">
  7.        <title></title>
  8.      </head>
  9.      <body>
  10. ➊     <div id="chart" style="width:600px;height:400px;"></div>
  11.        <!--[if lt IE 9]><script src="js/excanvas.min.js"></script><![endif]-->
  12.        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js">
  13.        </script>
  14.        <script src="//cdnjs.cloudflare.com/ajax/libs/flot/0.7/jquery.flot.min.js">
  15.        </script>
  16.      </body>
  17.    </html>
复制代码

46
Lisrelchen 发表于 2015-10-14 11:09:59

Step 3: Prepare the Data

  1. Step 3: Prepare the Data
  2. In later examples we’ll see how to get the data directly from the World Bank’s web service, but for this example, let’s keep things simple and assume we have the data already downloaded and formatted for JavaScript. (For brevity, only excerpts are shown here. The book’s source code includes the full data set.)

  3. var eas = [[1960,0.1558],[1961,0.1547],[1962,0.1574], // Data continues...
  4. var ecs = [[1960,0.4421],[1961,0.4706],[1962,0.5145], // Data continues...
  5. var lcn = [[1960,0.0811],[1961,0.0860],[1962,0.0990], // Data continues...
  6. var mea = [[1968,0.0383],[1969,0.0426],[1970,0.0471], // Data continues...
  7. var sas = [[1960,0.0478],[1961,0.0383],[1962,0.0389], // Data continues...
  8. var ssf = [[1960,0.0297],[1961,0.0308],[1962,0.0334], // Data continues...
  9. This data includes the historical GDP (in current US dollars) for major regions of the world, from 1960 to 2011. The names of the variables are the World Bank region codes.
复制代码

47
Lisrelchen 发表于 2015-10-14 11:10:49

Step 4: Draw the Chart

  1. Step 4: Draw the Chart
  2. Before we add any interactivity, let’s check out the chart itself. The Flot library provides a simple function call to create a static graph. We call the jQuery extension plot and pass it two parameters. The first parameter identifies the HTML element that should contain the chart, and the second parameter provides the data as an array of data sets. In this case, we pass in an array with the series we defined earlier for each region.

  3. $(function () {
  4.     $.plot($("#chart"), [ eas, ecs, lcn, mea, sas, ssf ]);
  5. });
复制代码

48
Lisrelchen 发表于 2015-10-14 11:11:26

Step 5: Add the Controls

  1. Step 5: Add the Controls
  2. Now that we have a chart we’re happy with, we can add the HTML controls to interact with it. For this example, our goal is fairly simple: our users should be able to pick which regions appear on the graph. We’ll give them that option with a set of checkboxes, one for each region. Here’s the markup to include the checkboxes.

  3. <label><input type="checkbox"> East Asia & Pacific</label>
  4. <label><input type="checkbox"> Europe & Central Asia</label>
  5. <label><input type="checkbox"> Latin America & Caribbean</label>
  6. <label><input type="checkbox"> Middle East & North Africa</label>
  7. <label><input type="checkbox"> South Asia</label>
  8. <label><input type="checkbox"> Sub-Saharan Africa</label>
  9. You may be surprised to see that we’ve placed the <input> controls inside the <label> elements. Although it looks a little unusual, that’s almost always the best approach. When we do that, the browser interprets clicks on the label as clicks on the control, whereas if we separate the labels from the controls, it forces the user to click on the tiny checkbox itself to have any effect.

  10. On our web page, we’d like to place the controls on the right side of the chart. We can do that by creating a containing <div> and making the chart and the controls float (left) within it. While we’re experimenting with the layout, it’s easiest to simply add the styling directly in the HTML markup. In a production implementation, you might want to define the styles in an external style sheet.

  11. <div id="visualization">
  12.     <div id="chart" style="width:500px;height:333px;float:left"></div>
  13.     <div id="controls" style="float:left;">
  14.         <label><input type="checkbox"> East Asia & Pacific</label>
  15.         <label><input type="checkbox"> Europe & Central Asia</label>
  16.         <label><input type="checkbox"> Latin America & Caribbean</label>
  17.         <label><input type="checkbox"> Middle East & North Africa</label>
  18.         <label><input type="checkbox"> South Asia</label>
  19.         <label><input type="checkbox"> Sub-Saharan Africa</label>
  20.     </div>
  21. </div>
复制代码

49
Lisrelchen 发表于 2015-10-14 11:12:07

Step 6: Define the Data Structure for the Interaction

  1. Step 6: Define the Data Structure for the Interaction
  2. Now that the general layout looks good, we can turn back to JavaScript. First we need to expand our data to track the interaction state. Instead of storing the data as simple arrays of values, we’ll use an array of objects. Each object will include the corresponding data values along with other properties.

  3. var source = [
  4.     { data: eas, show: true, color: "#FE4C4C", name: "East Asia & Pacific" },
  5.     { data: ecs, show: true, color: "#B6ED47", name: "Europe & Central Asia" },
  6.     { data: lcn, show: true, color: "#2D9999",
  7.       name: "Latin America & Caribbean" },
  8.     { data: mea, show: true, color: "#A50000",
  9.       name: "Middle East & North Africa" },
  10.     { data: sas, show: true, color: "#679A00", name: "South Asia" },
  11.     { data: ssf, show: true, color: "#006363", name: "Sub-Saharan Africa" }
  12. ];
  13. Each object includes the data points for a region, and it also gives us a place to define additional properties, including the label for the series and other status information. One property that we want to track is whether the series should be included on the chart (using the key show). We also need to specify the color for each line; otherwise, the Flot library will pick the color dynamically based on how many regions are visible at the same time, and we won’t be able to match the color with the control legend.
复制代码

50
Lisrelchen 发表于 2015-10-14 11:16:33

Step 8: Add the Controls Using JavaScript

  1. Step 8: Add the Controls Using JavaScript
  2. Now that our new data structure can provide the chart input, let’s use it to add the checkbox controls to the page as well. The jQuery .each() function is a convenient way to iterate through the array of regions. Its parameters include an array of objects and a function to execute on each object in the array. That function takes two parameters, the array index and the array object.

  3. $.each(source, function(idx, region) {
  4.     var input = $("<input>").attr("type","checkbox").attr("id","chk-"+idx);
  5.     if (region.show) {
  6.         $(input).prop("checked",true);
  7.     }
  8.     var span = $("<span>").css({
  9.         "background-color": region.color,
  10.         "display":          "inline-block",
  11.         "height":           "0.9em",
  12.         "width":            "0.9em",
  13.         "margin-right":     "0.25em",
  14.     });
  15.     var label = $("<label>").append(input).append(span).append(region.name);
  16.     $("#controls").append(label);
  17. });
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-31 14:03