تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يتناول هذا القسم الترميز SimpleExampleServlet، وهو أبسط مثال على تنفيذ مصدر البيانات الذي يتم توفيره مع المكتبة. يوفّر هذا القسم أيضًا تعليمات مفصّلة
حول كيفية تشغيل واختبار SimpleExampleServlet.
تقع الفئة SimpleExampleServlet في الحزمة examples. تقدم هذه الفئة مثالاً
على أبسط تنفيذ لمصدر البيانات. يتم اكتساب SimpleExampleServlet من DataSourceServlet،
وتنفيذ generateDataTable()، ويجب تشغيله
داخل حاوية serlet.
في ما يلي مقتطف عن SimpleExampleServlet. تعرض الدالة generateDataTable البيانات إلى المكتبة. تنشئ هذه الدالة وصفًا لجدول البيانات، وتحدد
أعمدة جدول البيانات، وتعبئ جدول البيانات بالبيانات.
تتعامل المكتبة مع جميع الإجراءات الأخرى المطلوبة لإرجاع جدول البيانات إلى تصور الاستعلام.
// This example extends DataSourceServlet
public class SimpleExampleServlet extends DataSourceServlet {
@Override
public DataTable generateDataTable(Query query, HttpServletRequest request) {
// Create a data table,
DataTable data = new DataTable();
ArrayList cd = new ArrayList();
cd.add(new ColumnDescription("name", ValueType.TEXT, "Animal name"));
cd.add(new ColumnDescription("link", ValueType.TEXT, "Link to wikipedia"));
cd.add(new ColumnDescription("population", ValueType.NUMBER, "Population size"));
cd.add(new ColumnDescription("vegeterian", ValueType.BOOLEAN, "Vegetarian?"));
data.addColumns(cd);
// Fill the data table.
try {
data.addRowFromValues("Aye-aye", "http://en.wikipedia.org/wiki/Aye-aye", 100, true);
data.addRowFromValues("Sloth", "http://en.wikipedia.org/wiki/Sloth", 300, true);
data.addRowFromValues("Leopard", "http://en.wikipedia.org/wiki/Leopard", 50, false);
data.addRowFromValues("Tiger", "http://en.wikipedia.org/wiki/Tiger", 80, false);
} catch (TypeMismatchException e) {
System.out.println("Invalid type!");
}
return data;
}
}
تشغيل واختبار SimpleExampleServlet
يقدّم هذا القسم تعليمات حول كيفية تشغيل واختبار "SimpleExampleServlet".
إذا لم يسبق لك إجراء ذلك، فاطلع على قسم التثبيت للحصول على معلومات حول المتطلبات الأساسية وإرشادات حول كيفية تنزيل المكتبة وإنشائها. تأكد من تثبيت خادم ويب يعمل كذلك كحاوية serlet مثل Apache Tomcat، وذلك إذا لم يكن لديك خادم على النظام لديك. التعليمات الواردة في هذا القسم خاصة ببرنامج Apache
Tomcat على نظام التشغيل Windows.
لتشغيل SimpleExampleServlet واختباره، أنشِئ تطبيق ويب
يشغّل مصدر بيانات SimpleExampleServlet،
ثم شغِّل مثالاً على صفحة ويب مع عرض مرئي يوضّح البيانات التي تم الاستعلام عنها من مصدر
البيانات. تم توضيح ذلك في الأقسام التالية:
اتبع التعليمات الواردة أدناه أو اضبطها لإنشاء تطبيق ويب
على Apache Tomcat. هذه التعليمات خاصة ببرنامج Apache Tomcat على نظام التشغيل Windows:
انتقِل إلى الدليل الذي ثبَّت فيه Tomcat. تمت كتابة ذلك
في هذا المستند باسم <tomcat_home>.
انتقِل إلى
الدليل الفرعي webapps.
إنشاء دليل فرعي باسم myWebApp.
غيِّر إلى الدليل الفرعي الذي أنشأته للتو وأنشئ دليلاً فرعيًا آخر باسم WEB-INF.
غيِّر إلى الدليل الفرعي WEB-INF وأنشئ دليلاً فرعيًا آخر باسم lib.
يجب أن يكون المسار الكامل <tomcat_home>/webapps/myWebApp/WEB-INF/lib.
انسخ web.xml من <data_source_library_install>/examples/src/html
إلى الدليل WEB-INF. حيث <data_source_library_install> هو الدليل الذي تم فيه تثبيت مكتبة مصادر البيانات.
توضّح الأسطر التالية في web.xml السمة SimpleExampleServlet وتربطها:
انتقِل إلى الدليل الذي ثبَّت فيه مكتبة مصادر البيانات.
تمت كتابة ذلك في هذا المستند باسم <data_source_library_install>.
انسخ جميع حزم التبعية
إلى <tomcat_home>/webapps/myWebApp/WEB-INF/lib. يتم تثبيت
الحزم في <data_source_library_install>/lib،
ما لم تضعها في دليل مختلف.
إذا كنت قد أنشأت المكتبة بنفسك، انسخ visualization-datasource-1.0.2.jar وvisualization-datasource-examples.jar
من <data_source_library_install>/build
إلى <tomcat_home>/webapps/myWebApp/WEB-INF/lib.
في حال فك ضغط ملف ZIP، انسخ visualization-datasource-1.0.2.jar وvisualization-datasource-examples.jar
من <data_source_library_install>
إلى <tomcat_home>/webapps/myWebApp/WEB-INF/lib.
يُرجى العِلم أنّ رقم الإصدار الوارد في اسم ملف jar قد يختلف حسب أحدث رقم إصدار.
ابدأ تشغيل Tomcat أو أعِد تشغيل Tomcat إذا كان قيد التشغيل.
تعرض الشاشة بين 6 و7 أسطر من النص، استنادًا إلى عرض الشاشة.
يبدأ النص بـ google.visualization.Query.setResponse
وينتهي بـ /Tiger'},{v:80.0},{v:false}]}]}});
هذه هي البيانات التي يعرضها مصدر البيانات إلى تمثيل بصري للاستعلام.
استخدام التمثيل المرئي
لعرض البيانات
ويمكن استخدام ملف getting_started.html في الدليل <data_source_library_install>/examples/src/html لعرض مرئيات للبيانات. يحدد السطر التالي، المأخوذ من getting_started.html،
الخادم الذي سيتم استخدامه. تم إعداد تعيين serlet في الخطوة 8 من إنشاء تطبيق ويب على Apache Tomcat.
var query = new google.visualization.Query('simpleexample');
للاطّلاع على مقدمة عن الفئات الأكثر استخدامًا في المكتبة،
يمكنك الاطّلاع على الصفوف الرئيسية.
للاطّلاع على مثال حول كيفية تنفيذ تدفّق الأحداث الخاص بك وإمكانات طلبات البحث، اطّلِع على تعريف الإمكانيات وتدفق الأحداث.
إذا كنت لا تريد اكتساب نسخ من DataSourceServlet،
يمكنك تنفيذ مصدر بيانات كما هو موضّح في استخدام
موقعك الخاص. على سبيل المثال، قد لا ترغب في الاكتساب من DataSourceServlet إذا
كنت تكتسب خادمًا من فئة أخرى.
تاريخ التعديل الأخير: 2024-07-10 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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)."]]