با مجموعهها، منظم بمانید ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
آنچه تا کنون پوشش داده ایم برای بسیاری از صفحات وب کافی است: شما نمودار خود را در صفحه ترسیم کرده اید. با این حال، اگر میخواهید کلیکهای کاربر را دریافت کنید، یا نیاز به دستکاری ویژگیها یا دادهها در نموداری که قبلاً ترسیم کردهاید، دارید، باید به رویدادهای پرتاب شده توسط نمودار گوش دهید.
همه نمودارها انواعی از رویدادها را نشان می دهند. در اینجا رایج ترین آنها هستند:
آماده - زمانی که نمودار بر روی صفحه رسم می شود پرتاب می شود و آماده پاسخگویی به روش ها است. اگر نیاز به درخواست اطلاعات از نمودار دارید، به این رویداد گوش دهید.
انتخاب - زمانی که کاربر چیزی را در نمودار انتخاب میکند پرتاب میشود: معمولاً با کلیک کردن روی یک نوار یا برش پای.
خطا - زمانی که نمودار نمی تواند داده های ارسال شده را ارائه کند، پرتاب می شود، معمولاً به این دلیل که قالب DataTable اشتباه است.
onmouseover و onmouseout - زمانی که کاربر به ترتیب ماوس را روی یک عنصر نمودار خاص یا خاموش می کند، پرتاب می شود.
گوش دادن به رویدادها ساده است. به سادگی google.visualization.events.addListener() را فراخوانی کنید که در یک دسته به نمودار منتقل می شود، نام رویدادی که می خواهید گرفت، و نام کنترل کننده ای که باید هنگام پرتاب رویداد فراخوانی کنید. شما می توانید این متد را با هر دسته نموداری فراخوانی کنید، حتی اگر هنوز draw() را فراخوانی نکرده باشید. توجه داشته باشید که اگر میخواهید شنونده قبل از حذف خودش دقیقاً یک بار فراخوانی شود، میتوانید با google.visualization.events.addOneTimeListener() تماس بگیرید.
در اینجا یک قطعه کد جزئی وجود دارد که نحوه ثبت نام برای گرفتن رویداد انتخابی نمودار را نشان می دهد:
load libraries... function drawChart() { prepare data... var chart = new google.visualization.PieChart(document.getElementById('chart_div')); // The select handler. Call the chart's getSelection() method function selectHandler() { var selectedItem = chart.getSelection()[0]; if (selectedItem) { var value = data.getValue(selectedItem.row, selectedItem.column); alert('The user selected ' + value); } } // Listen for the 'select' event, and call my function selectHandler() when // the user selects something on the chart. google.visualization.events.addListener(chart, 'select', selectHandler); draw the chart... }
شکل زیر نمونه کد Hello Charts را با شنونده رویداد انتخابی جدید نشان می دهد. خودتان آن را امتحان کنید.
<html> <head> <!--Load the AJAX API--> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <script type="text/javascript"> // Load the Visualization API and the piechart package. google.charts.load('current', {'packages':['corechart']}); // Set a callback to run when the Google Visualization API is loaded. google.charts.setOnLoadCallback(drawChart); // Callback that creates and populates a data table, // instantiates the pie chart, passes in the data and // draws it. function drawChart() { // Create the data table. var data = new google.visualization.DataTable(); data.addColumn('string', 'Topping'); data.addColumn('number', 'Slices'); data.addRows([ ['Mushrooms', 3], ['Onions', 1], ['Olives', 1], ['Zucchini', 1], ['Pepperoni', 2] ]); // Set chart options var options = {'title':'How Much Pizza I Ate Last Night', 'width':400, 'height':300}; // Instantiate and draw our chart, passing in some options. var chart = new google.visualization.PieChart(document.getElementById('chart_div')); function selectHandler() { var selectedItem = chart.getSelection()[0]; if (selectedItem) { var topping = data.getValue(selectedItem.row, 0); alert('The user selected ' + topping); } } google.visualization.events.addListener(chart, 'select', selectHandler); chart.draw(data, options); } </script> </head> <body> <!--Div that will hold the pie chart--> <div id="chart_div" style="width:400; height:300"></div> </body> </html>
تاریخ آخرین بهروزرسانی 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\u003eGoogle Charts throw events like \u003ccode\u003eready\u003c/code\u003e, \u003ccode\u003eselect\u003c/code\u003e, \u003ccode\u003eerror\u003c/code\u003e, \u003ccode\u003eonmouseover\u003c/code\u003e, and \u003ccode\u003eonmouseout\u003c/code\u003e to enable user interaction and manipulation.\u003c/p\u003e\n"],["\u003cp\u003eTo catch these events, use \u003ccode\u003egoogle.visualization.events.addListener()\u003c/code\u003e to register a handler function.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eselect\u003c/code\u003e event is commonly used to respond to user clicks on chart elements, enabling actions like displaying selected data.\u003c/p\u003e\n"],["\u003cp\u003eA provided code example demonstrates how to implement a \u003ccode\u003eselect\u003c/code\u003e event listener to display an alert with the selected data.\u003c/p\u003e\n"]]],[],null,["What we've covered so far is sufficient for many web pages: you've drawn your chart on the page. However, if you want to catch user clicks, or need to manipulate properties or data in a chart that you've already drawn, you need to listen for events thrown by the chart.\n\nAll charts throw some kinds of events. Here are the most common:\n\n- *ready* - Thrown when the chart is drawn on the page and ready to respond to methods. Listen for this event if you need to request information from the chart.\n- *select* - Thrown when the user selects something on the chart: typically by clicking on a bar or pie slice.\n- *error* - Thrown when the chart can't render the data passed in, typically because the `DataTable` format is wrong.\n- *onmouseover* and *onmouseout* - Thrown when the user mouses over or off of a specific chart element, respectively.\n\nListening for events is simple; simply call [`google.visualization.events.addListener()`](/chart/interactive/docs/reference#addlistener) passing in a handle to the chart, the name of the event to catch, and the name of a handler to call when the event is thrown. You can call this method with any chart handle, even if you haven't called `draw()` yet. Note that you can call [`google.visualization.events.addOneTimeListener()`](/chart/interactive/docs/reference#addonetimelistener) if you want the listener to be called exactly once before removing itself.\n\nHere's a partial code snippet showing how to register to catch a chart's *select* event: \n\n```javascript\nload libraries...\n\nfunction drawChart() {\n\n prepare data...\n\n var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n\n // The select handler. Call the chart's getSelection() method\n function selectHandler() {\n var selectedItem = chart.getSelection()[0];\n if (selectedItem) {\n var value = data.getValue(selectedItem.row, selectedItem.column);\n alert('The user selected ' + value);\n }\n }\n\n // Listen for the 'select' event, and call my function selectHandler() when\n // the user selects something on the chart.\n google.visualization.events.addListener(chart, 'select', selectHandler);\n\n draw the chart...\n\n}\n```\n\nThe following shows the Hello Charts code example with a new select event listener. Try it out yourself. \n\n```html\n\u003chtml\u003e\n \u003chead\u003e\n \u003c!--Load the AJAX API--\u003e\n \u003cscript type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"\u003e\u003c/script\u003e\n \u003cscript type=\"text/javascript\"\u003e\n\n // Load the Visualization API and the piechart package.\n google.charts.load('current', {'packages':['corechart']});\n\n // Set a callback to run when the Google Visualization API is loaded.\n google.charts.setOnLoadCallback(drawChart);\n\n // Callback that creates and populates a data table,\n // instantiates the pie chart, passes in the data and\n // draws it.\n function drawChart() {\n\n // Create the data table.\n var data = new google.visualization.DataTable();\n data.addColumn('string', 'Topping');\n data.addColumn('number', 'Slices');\n data.addRows([\n ['Mushrooms', 3],\n ['Onions', 1],\n ['Olives', 1], \n ['Zucchini', 1],\n ['Pepperoni', 2]\n ]);\n\n // Set chart options\n var options = {'title':'How Much Pizza I Ate Last Night',\n 'width':400,\n 'height':300};\n \n // Instantiate and draw our chart, passing in some options.\n var chart = new google.visualization.PieChart(document.getElementById('chart_div'));\n\n function selectHandler() {\n var selectedItem = chart.getSelection()[0];\n if (selectedItem) {\n var topping = data.getValue(selectedItem.row, 0);\n alert('The user selected ' + topping);\n }\n }\n\n google.visualization.events.addListener(chart, 'select', selectHandler); \n chart.draw(data, options);\n }\n\n \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003c!--Div that will hold the pie chart--\u003e\n \u003cdiv id=\"chart_div\" style=\"width:400; height:300\"\u003e\u003c/div\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n``` \n[**Next: *Security and Privacy***](/chart/interactive/docs/security_privacy)\n\n**More Information**\n\n- [Handling Events](/chart/interactive/docs/events)\n- [Controls and Dashboards](/chart/interactive/docs/gallery/controls)\n- [getSelection()](/chart/interactive/docs/reference#visgetselection)\n- [`google.visualization.events.addListener()`](/chart/interactive/docs/reference)"]]