[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2024-07-10 (世界標準時間)。"],[[["\u003cp\u003e\u003ccode\u003eSimpleExampleServlet\u003c/code\u003e provides a basic example of a data source implementation within the library, inheriting from \u003ccode\u003eDataSourceServlet\u003c/code\u003e and requiring a servlet container to run.\u003c/p\u003e\n"],["\u003cp\u003eRunning and testing \u003ccode\u003eSimpleExampleServlet\u003c/code\u003e involves setting up a web application, like on Apache Tomcat, to host the servlet and using a visualization (like the provided \u003ccode\u003egetting_started.html\u003c/code\u003e) to display the queried data.\u003c/p\u003e\n"],["\u003cp\u003eThe setup process includes creating a web application directory structure, configuring \u003ccode\u003eweb.xml\u003c/code\u003e to define the servlet, deploying necessary library files, and accessing the servlet and visualization through specific URLs.\u003c/p\u003e\n"]]],[],null,["# Getting Started with Data Sources\n\nThis section introduces `SimpleExampleServlet`, which is\nthe simplest example implementation of a data source that is provided\nwith the library. This section also provides step-by-step instructions\non how to run and test `SimpleExampleServlet`.\n\n- [Introducing `SimpleExampleServlet`](#introducing)\n- [Running and testing `SimpleExampleServlet`](#running)\n\nIntroducing `SimpleExampleServlet`\n----------------------------------\n\nThe `SimpleExampleServlet` class is located in the `examples`\npackage. This class provides an example of the simplest implementation\nof a data source. `SimpleExampleServlet` inherits from `DataSourceServlet`,\nimplements `generateDataTable()`, and must be run\nwithin a servlet container.\n\nA snippet of `SimpleExampleServlet` is provided below. The `generateDataTable` function\nexposes data to the library. This function creates a data table description,\ndefines the data table columns, and populates the data table with data.\nThe library handles all other actions required to return the data table\nto the querying visualization. \n\n```gdscript\n// This example extends DataSourceServlet\npublic class SimpleExampleServlet extends DataSourceServlet {\n\n @Override\n public DataTable generateDataTable(Query query, HttpServletRequest request) {\n // Create a data table,\n DataTable data = new DataTable();\n ArrayList cd = new ArrayList();\n cd.add(new ColumnDescription(\"name\", ValueType.TEXT, \"Animal name\"));\n cd.add(new ColumnDescription(\"link\", ValueType.TEXT, \"Link to wikipedia\"));\n cd.add(new ColumnDescription(\"population\", ValueType.NUMBER, \"Population size\"));\n cd.add(new ColumnDescription(\"vegeterian\", ValueType.BOOLEAN, \"Vegetarian?\"));\n\n data.addColumns(cd);\n\n // Fill the data table.\n try {\n data.addRowFromValues(\"Aye-aye\", \"http://en.wikipedia.org/wiki/Aye-aye\", 100, true);\n data.addRowFromValues(\"Sloth\", \"http://en.wikipedia.org/wiki/Sloth\", 300, true);\n data.addRowFromValues(\"Leopard\", \"http://en.wikipedia.org/wiki/Leopard\", 50, false);\n data.addRowFromValues(\"Tiger\", \"http://en.wikipedia.org/wiki/Tiger\", 80, false);\n } catch (TypeMismatchException e) {\n System.out.println(\"Invalid type!\");\n }\n return data;\n }\n}\n```\n\nRunning and testing `SimpleExampleServlet`\n------------------------------------------\n\nThis section provides instructions on how to run and test `SimpleExampleServlet`.\n\nIf you haven't already done so, see the [Installation](/chart/interactive/docs/dev/dsl_install) section\nfor information about prerequisites, and instructions on how to download\nand build the library. Make sure you install a web server that also functions\nas a servlet container, such as Apache Tomcat, if you do not already have\none on your system. The instructions in this section are specific to Apache\nTomcat on a Windows system.\n\nTo run and test `SimpleExampleServlet`, create a web application\nthat\nruns the `SimpleExampleServlet` data source,\nthen run an example web\npage with a visualization showing data queried from the data\nsource. This is described in the following sections:\n\n- [Creating a Web Application on Apache Tomcat](#webapp)\n- [Using a Visualization to View the Data](#visualization)\n\n### Creating a Web Application on Apache Tomcat\n\nFollow or adapt the instructions below to create a web application\non Apache Tomcat. These instructions are specific to Apache Tomcat on a\nWindows system:\n\n1. Navigate to the directory in which you installed Tomcat. This is written in this document as `\u003ctomcat_home\u003e`. \n2. Navigate to the `webapps` subdirectory. \n3. Create a subdirectory called `myWebApp`. \n4. Change to the subdirectory you have just created and create another subdirectory called `WEB-INF`. \n5. Change to the `WEB-INF` subdirectory and create another subdirectory called `lib`. \n The full path should be `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n6. Copy `web.xml` from `\u003cdata_source_library_install\u003e/examples/src/html` to the `WEB-INF` directory. Where `\u003cdata_source_library_install\u003e` is the directory in which you installed the data source library. The following lines in `web.xml` define and map `SimpleExampleServlet`: \n\n ```carbon\n \u003cservlet\u003e\n \u003cservlet-name\u003eMy Servlet\u003c/servlet-name\u003e\n \u003cdescription\u003eMy servlet description.\u003c/description\u003e\n \u003cservlet-class\u003eSimpleExampleServlet\u003c/servlet-class\u003e\n \u003c/servlet\u003e\n\n \u003cservlet-mapping\u003e\n \u003cservlet-name\u003eMy Servlet\u003c/servlet-name\u003e\n \u003curl-pattern\u003e/simpleexample\u003c/url-pattern\u003e\n \u003c/servlet-mapping\u003e\n ```\n7. Navigate to the directory in which you installed the data source library. This is written in this document as `\u003cdata_source_library_install\u003e`. \n8. Copy all the dependency packages to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. The packages are installed in `\u003cdata_source_library_install\u003e/lib`, unless you put them in a different directory. \n9. If you have built the library yourself, copy `visualization-datasource-1.0.2.jar` and `visualization-datasource-examples.jar` \n from `\u003cdata_source_library_install\u003e/build` \n to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n\n If you have unzipped the zip file, copy `visualization-datasource-1.0.2.jar` and `visualization-datasource-examples.jar` \n from `\u003cdata_source_library_install\u003e` \n to `\u003ctomcat_home\u003e/webapps/myWebApp/WEB-INF/lib`. \n Note that the version number in the jar file name may vary depending on the latest version number. \n10. Start Tomcat, or restart Tomcat if it is already running. \n11. Click the following link: \n\n \u003chttp://localhost:8080/myWebApp/simpleexample\u003e \n\n The screen displays 6-7 lines of text, depending on your screen width. \n The text begins with `google.visualization.Query.setResponse ` \n and ends with `/Tiger'},{v:80.0},{v:false}]}]}});` \n\n This is the data that is returned by your data source to a querying visualization.\n\n### Using a Visualization\nto View the Data\n\nThe `getting_started.html` file in the `\u003cdata_source_library_install\u003e/examples/src/html` directory\ncan be used to view a visualization of the data. The following line, taken\nfrom `getting_started.html`, specifies\nthe servlet to use. The servlet mapping was set up in step 8 of [Creating\na Web Application on Apache Tomcat](#webapp). \n\n```gdscript\nvar query = new google.visualization.Query('simpleexample');\n```\n\nFor more information on how to specify a visualization and use the\nquery language, see [Using\nCharts](/chart/interactive/docs) and the [Query Language\nReference](/chart/interactive/docs/querylanguage).\n\nFollow, or adapt, the instructions below to view a visualization\nof the data provided by the data source:\n\n1. Copy the `getting_started.html` file from the `\u003cdata_source_library_install\u003e/examples/src/html` directory \n to the `\u003ctomcat_home\u003e/webapps/myWebApp/` directory. \n2. Click the following link \u003chttp://localhost:8080/myWebApp/getting_started.html\u003e, you should see the following: \n\n \u003cbr /\u003e\n\n \u003cbr /\u003e\n\n That's it! You have set up your first data source.\n\nNext Steps\n----------\n\nThe next example is described in the [Using an\nExternal Data Store](/chart/interactive/docs/dev/dsl_csv) section. Alternatively\nyou can return to the [Introduction](/chart/interactive/docs/dev/dsl_intro), or\nexplore the following links:\n\n- For an introduction to the library's most commonly used classes, see [Key Classes](/chart/interactive/docs/dev/dsl_packages#keyclasses).\n- For an example of how to implement your own flow of events, and query capabilities, see [Defining Capabilities and the Flow of Events](/chart/interactive/docs/dev/dsl_httpservlet).\n- If you do not want to inherit from `DataSourceServlet`, you can implement a data source as described in [Using\n Your Own Servlet](/chart/interactive/docs/dev/dsl_key_concepts#servlet). For example you might not want to inherit from `DataSourceServlet` if you inherit a servlet from another class.\n- If you do not want to use a servlet, see [Implementing a Non-servlet Data Source](/chart/interactive/docs/dev/dsl_key_concepts#nonservlet).\n- To learn about how to pass parameters from an application to a data source, see [Passing Parameters to `DataTableGenerator.generateDataTable`](/chart/interactive/docs/dev/dsl_key_concepts#params)."]]