{ "cells": [ { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['OBJECTID', 'Record_ID', 'Call_Type', 'Description', 'Reporting_Officer', 'Unit_ID', 'Report_Created', 'Location', 'Police_District', 'Beat', 'Grid', 'X_Coordinate', 'Y_Coordinate', 'Day_of_Week', 'Occurence_Date', 'Occurence_Time', 'Received_Date', 'Received_Time', 'Dispatch_Date', 'Dispatch_Time', 'Enroute_Date', 'Enroute_Time', 'At_Scene_Date', 'At_Scene_Time', 'Clear_Date', 'Clear_Time']\n" ] } ], "source": [ "import requests\n", "import matplotlib.pyplot as py_plot\n", "import pandas as pd\n", "from IPython.display import display, HTML\n", "from pyproj import Proj, transform\n", "import folium\n", "from folium import plugins\n", "import math\n", "\n", "#url from api explorer tab in open data platform\n", "api_url = 'https://services5.arcgis.com/54falWtcpty3V47Z/arcgis/rest/services/cad_calls_year3/FeatureServer/0/query?where=1%3D1&outFields=*&outSR=4326&f=json'\n", "\n", "#function to return a list of column headers and rows of data from the json api call\n", "def opendata_api(api_call):\n", " n = 0\n", " #build empty list for all data\n", " all_data = []\n", " #get a count of total records in api request\n", " call_count = requests.get(api_call,\"&returnCountOnly=true\").json()\n", " count = call_count['count']\n", " #calculate number of requests to make due to 1000 max record count\n", " loop_count = math.ceil(count/1000)\n", "\n", " for count in range(loop_count):\n", " call_json = requests.get(api_call,'&resultOffset='+str(n)).json()\n", " \n", " #build a list of dictionaries by getting the values of the features key\n", " calls_feats = call_json[\"features\"]\n", " #loop to get the values of the attributes dictionary\n", " for line in calls_feats:\n", " all_data.append(line[\"attributes\"])\n", " n+=1000\n", " #print(\"Number of requests is \", count)\n", " \n", " #return the keys of the first dictionary get column names\n", " fields = list(all_data[0].keys())\n", "\n", " #a list comprehension to populate a list of attributes into the list we are building called rows\n", " rows = [list(item.values()) for item in all_data]\n", " return rows,fields\n", "\n", "make_request = opendata_api(api_url)\n", "data = make_request[0]\n", "columns = make_request[1]\n", "\n", "print(columns)\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, '6360891', '415E', 'DISTURBANCE-NOISE', '0883', '2C14', 'N', '4101 INNOVATOR DR', '1', '1A', '0336', 6700606, 1996674, 'Sun', 1483228800000, '18:40:50', 1483228800000, '18:40:50', 1483228800000, '18:49:57', 1483228800000, '18:50:12', 1483228800000, '18:57:57', 1483228800000, '19:06:44'], [2, '6360930', '927P', 'SUSPICIOUS SUBJECT/CIRCUMSTANCE-IN PROGRESS', '0694', '1C12', 'N', '2941 ADVANTAGE WAY', '1', '1A', '0313', 6696606, 2000630, 'Sun', 1483228800000, '19:45:17', 1483228800000, '19:45:17', 1483228800000, '19:45:17', 1483228800000, '19:45:17', 1483228800000, '19:45:17', 1483228800000, '19:47:44'], [3, '6361374', 'TSTOP', 'TRAFFIC STOP', '552', 'XRY12', 'N', '2ND ST / NEASHAM CIR', '3', '3M', '0751', 6703083, 1973628, 'Sun', 1483228800000, '19:14:48', 1483228800000, '19:14:48', 1483228800000, '19:14:48', 1483228800000, '19:14:48', 1483228800000, '19:14:48', 1483228800000, '19:20:08'], [4, '6361293', 'AU', 'ALL UNITS BROADCAST', '', '', 'N', '636 TENAYA AVE', '1', '1C', '0418', 6710884, 1988977, 'Sun', 1483228800000, '00:02:11', 1483228800000, '00:02:11', -2208988800000, ' ', -2208988800000, ' ', -2208988800000, ' ', 1483228800000, '00:13:48'], [5, '6360110', '927S', 'SHOTS FIRED - LESS THAN 15 AGO', '', '', 'N', '635 KESNER AVE', '2', '2A', '0512', 6718588, 1993201, 'Sun', 1483228800000, '00:00:34', 1483228800000, '00:00:34', -2208988800000, ' ', -2208988800000, ' ', -2208988800000, ' ', 1483228800000, '00:01:25']]\n" ] } ], "source": [ "#print the first 5 records of data \n", "print(data[0:5])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "DISTURBANCE-CLARIFY 33343\n", "TRAFFIC STOP 23475\n", "SUSPICIOUS SUBJECT/CIRCUMSTANCE-IN PROGRESS 17511\n", "ALL UNITS BROADCAST 17369\n", "SUSPICIOUS VEHICLE-OCCUPIED 14087\n", "WELFARE CHECK 13259\n", "SUBJECT STOP 11366\n", "INCOMPLETE CALL FOR POLICE 10333\n", "ALARM-SECURE NO EVID OF CRIME 7108\n", "MISDEMEANOR ASSAULT-IN PROGRESS 5439\n", "REPORT NUMBER ASSIGNMENT 5316\n", "ERRAND 5078\n", "ALARM-CANCEL 4721\n", "VEHICLE ACCIDENT-NO OR UNKNOWN INJURIES 4289\n", "BUSINESS CHECK 3869\n", "CHECK ON HAZARD 3850\n", "DISTURBANCE-FAMILY 3841\n", "ALARM-ACCIDENTAL 3767\n", "PARK CHECK 3622\n", "ALARM ADVISED-FULL RESPONSE 3232\n", "VEHICLE ACCIDENT-INJURIES 3226\n", "MEDICAL AID-CLARIFY IN TEXT 2874\n", "STOLEN VEHICLE-REPORT 2837\n", "SHOTS FIRED - LESS THAN 15 AGO 2705\n", "SUPPLEMENTAL EMPLOYMENT 2374\n", "DISTURBANCE-NOISE 2311\n", "SUSPICIOUS VEHICLE-UNOCCUPIED 2150\n", "DOMESTIC VIOLENCE-IN PROGRESS 2123\n", "DISTURBANCE-WEAPON 1764\n", "ATTEMPT PICKUP/WARRANT SERVICE 1757\n", " ... \n", "CARJACKING-REPORT 20\n", "SHOOTING INTO UNOCCUPIED VEHICLE-LESS THAN 15 AGO 20\n", "LOCAL GOVERNMENT-FLOODING 20\n", "CAR CLOUT-CSI 20\n", "GRAND THEFT-REPORT REFERRAL 17\n", "GRAND THEFT-IN PROGRESS 16\n", "NARCOTIC ACTIVITY-REPORT 16\n", "BOMB THREAT 16\n", "GRAND THEFT-LESS THAN 5 AGO 12\n", "MEDICAL AID-SUSPECT ON SCENE 12\n", "DEAD BODY-CSI 11\n", "MISDEMEANOR HIT & RUN-CSI 10\n", "GRAND THEFT-CSI 7\n", "KIDNAPPING-CSI 7\n", "DIRECTED PATROL-AVAILABLE 6\n", "DIRECTED PATROL-PRIORITY 1,2,3 6\n", "CARJACKING-IN PROGRESS 6\n", "CAL ID 5\n", "ARSON-CSI 4\n", "HOMICIDE 4\n", "POSSESSION OF STOLEN PROPERTY-CSI 3\n", "DIRE EMERGENCY 3\n", "NARCOTIC ACTIVITY-CSI 2\n", "EVIDENCE LAB 2\n", "REFER TO OUTSIDE AGENCY 2\n", "EXPLOSIVE DEVICE-CSI 2\n", "PETTY THEFT-CSI 2\n", "DIRECTED PATROL-POP-NOT AVAIL 1\n", "SHOOTING INTO INHABITED DWELLING-IN PROGRESS 1\n", "CARJACKING-LESS THAN 15 AGO-INJURIES 1\n", "Name: Description, dtype: int64" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#building a pandas dataframe called \"calls\"\n", "calls = pd.DataFrame.from_records(data, columns=columns)\n", "\n", "#returns the counts for each unique value in the \"Description\" field\n", "display(calls['Description'].value_counts())" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "count 267065\n", "unique 189\n", "top DISTURBANCE-CLARIFY\n", "freq 33343\n", "Name: Description, dtype: object" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#describe method on the \"description\" field\n", "calls['Description'].describe()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OBJECTIDRecord_IDCall_TypeDescriptionReporting_OfficerUnit_IDReport_CreatedLocationPolice_DistrictBeat...Received_DateReceived_TimeDispatch_DateDispatch_TimeEnroute_DateEnroute_TimeAt_Scene_DateAt_Scene_TimeClear_DateClear_Time
016360891415EDISTURBANCE-NOISE08832C14N4101 INNOVATOR DR11A...148322880000018:40:50148322880000018:49:57148322880000018:50:12148322880000018:57:57148322880000019:06:44
126360930927PSUSPICIOUS SUBJECT/CIRCUMSTANCE-IN PROGRESS06941C12N2941 ADVANTAGE WAY11A...148322880000019:45:17148322880000019:45:17148322880000019:45:17148322880000019:45:17148322880000019:47:44
236361374TSTOPTRAFFIC STOP552XRY12N2ND ST / NEASHAM CIR33M...148322880000019:14:48148322880000019:14:48148322880000019:14:48148322880000019:14:48148322880000019:20:08
346361293AUALL UNITS BROADCASTN636 TENAYA AVE11C...148322880000000:02:11-2208988800000-2208988800000-2208988800000148322880000000:13:48
456360110927SSHOTS FIRED - LESS THAN 15 AGON635 KESNER AVE22A...148322880000000:00:34-2208988800000-2208988800000-2208988800000148322880000000:01:25
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " OBJECTID Record_ID Call_Type Description \\\n", "0 1 6360891 415E DISTURBANCE-NOISE \n", "1 2 6360930 927P SUSPICIOUS SUBJECT/CIRCUMSTANCE-IN PROGRESS \n", "2 3 6361374 TSTOP TRAFFIC STOP \n", "3 4 6361293 AU ALL UNITS BROADCAST \n", "4 5 6360110 927S SHOTS FIRED - LESS THAN 15 AGO \n", "\n", " Reporting_Officer Unit_ID Report_Created Location \\\n", "0 0883 2C14 N 4101 INNOVATOR DR \n", "1 0694 1C12 N 2941 ADVANTAGE WAY \n", "2 552 XRY12 N 2ND ST / NEASHAM CIR \n", "3 N 636 TENAYA AVE \n", "4 N 635 KESNER AVE \n", "\n", " Police_District Beat ... Received_Date Received_Time \\\n", "0 1 1A ... 1483228800000 18:40:50 \n", "1 1 1A ... 1483228800000 19:45:17 \n", "2 3 3M ... 1483228800000 19:14:48 \n", "3 1 1C ... 1483228800000 00:02:11 \n", "4 2 2A ... 1483228800000 00:00:34 \n", "\n", " Dispatch_Date Dispatch_Time Enroute_Date Enroute_Time At_Scene_Date \\\n", "0 1483228800000 18:49:57 1483228800000 18:50:12 1483228800000 \n", "1 1483228800000 19:45:17 1483228800000 19:45:17 1483228800000 \n", "2 1483228800000 19:14:48 1483228800000 19:14:48 1483228800000 \n", "3 -2208988800000 -2208988800000 -2208988800000 \n", "4 -2208988800000 -2208988800000 -2208988800000 \n", "\n", " At_Scene_Time Clear_Date Clear_Time \n", "0 18:57:57 1483228800000 19:06:44 \n", "1 19:45:17 1483228800000 19:47:44 \n", "2 19:14:48 1483228800000 19:20:08 \n", "3 1483228800000 00:13:48 \n", "4 1483228800000 00:01:25 \n", "\n", "[5 rows x 26 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#using the display() function to view calls dataframe as a table\n", "display(calls[0:5])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "Fri 40600\n", "Wed 39900\n", "Thu 38994\n", "Tue 38194\n", "Sat 37713\n", "Mon 36651\n", "Sun 35013\n", "Name: Day_of_Week, dtype: int64" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#count of incidents by day of week\n", "calls['Day_of_Week'].value_counts()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#convert x,y coords to float data type if they are string or int\n", "def parse_float(x):\n", " try:\n", " x = float(x)\n", " except Exception:\n", " x = 0\n", " return x\n", "calls['X_Coordinate'] = calls['X_Coordinate'].apply(parse_float)\n", "calls['Y_Coordinate'] = calls['Y_Coordinate'].apply(parse_float)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OBJECTIDRecord_IDCall_TypeDescriptionReporting_OfficerUnit_IDReport_CreatedLocationPolice_DistrictBeat...Received_DateReceived_TimeDispatch_DateDispatch_TimeEnroute_DateEnroute_TimeAt_Scene_DateAt_Scene_TimeClear_DateClear_Time
4244256360868901AVEHICLE ACCIDENT-INJURIESNGRAND AVE / I 80 EB22A...148322880000018:35:58-2208988800000-2208988800000-2208988800000148322880000018:39:50
7307316361248901AVEHICLE ACCIDENT-INJURIES811IV94NI 80 E / HWY 65 ON NUIUI...148331520000005:45:35148331520000005:47:10148331520000005:47:10148331520000005:47:10148331520000006:04:08
8378386361853901AVEHICLE ACCIDENT-INJURIESN3500 FLORIN RD55B...148331520000018:10:40-2208988800000-2208988800000-2208988800000148331520000018:16:30
8468476361996901AVEHICLE ACCIDENT-INJURIES09542C57NCENTER PKWY / MACK RD55C...148331520000020:14:11148331520000020:15:15148331520000020:17:12148331520000020:31:09148331520000021:15:18
138513866362865901AVEHICLE ACCIDENT-INJURIESNRIO LINDA BLVD / RIVERA DR22B...148340160000017:25:10-2208988800000-2208988800000-2208988800000148340160000017:25:51
\n", "

5 rows × 26 columns

\n", "
" ], "text/plain": [ " OBJECTID Record_ID Call_Type Description \\\n", "424 425 6360868 901A VEHICLE ACCIDENT-INJURIES \n", "730 731 6361248 901A VEHICLE ACCIDENT-INJURIES \n", "837 838 6361853 901A VEHICLE ACCIDENT-INJURIES \n", "846 847 6361996 901A VEHICLE ACCIDENT-INJURIES \n", "1385 1386 6362865 901A VEHICLE ACCIDENT-INJURIES \n", "\n", " Reporting_Officer Unit_ID Report_Created Location \\\n", "424 N GRAND AVE / I 80 EB \n", "730 811 IV94 N I 80 E / HWY 65 ON N \n", "837 N 3500 FLORIN RD \n", "846 0954 2C57 N CENTER PKWY / MACK RD \n", "1385 N RIO LINDA BLVD / RIVERA DR \n", "\n", " Police_District Beat ... Received_Date Received_Time \\\n", "424 2 2A ... 1483228800000 18:35:58 \n", "730 UI UI ... 1483315200000 05:45:35 \n", "837 5 5B ... 1483315200000 18:10:40 \n", "846 5 5C ... 1483315200000 20:14:11 \n", "1385 2 2B ... 1483401600000 17:25:10 \n", "\n", " Dispatch_Date Dispatch_Time Enroute_Date Enroute_Time At_Scene_Date \\\n", "424 -2208988800000 -2208988800000 -2208988800000 \n", "730 1483315200000 05:47:10 1483315200000 05:47:10 1483315200000 \n", "837 -2208988800000 -2208988800000 -2208988800000 \n", "846 1483315200000 20:15:15 1483315200000 20:17:12 1483315200000 \n", "1385 -2208988800000 -2208988800000 -2208988800000 \n", "\n", " At_Scene_Time Clear_Date Clear_Time \n", "424 1483228800000 18:39:50 \n", "730 05:47:10 1483315200000 06:04:08 \n", "837 1483315200000 18:16:30 \n", "846 20:31:09 1483315200000 21:15:18 \n", "1385 1483401600000 17:25:51 \n", "\n", "[5 rows x 26 columns]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#create a variable that contains only accident Vehicle Reports from \"Description\" field\n", "accident = calls[calls['Description'] == 'VEHICLE ACCIDENT-INJURIES']\n", "\n", "display(accident[0:5])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "scrolled": false }, "outputs": [ { "data": { "application/javascript": [ "/* Put everything inside the global mpl namespace */\n", "window.mpl = {};\n", "\n", "\n", "mpl.get_websocket_type = function() {\n", " if (typeof(WebSocket) !== 'undefined') {\n", " return WebSocket;\n", " } else if (typeof(MozWebSocket) !== 'undefined') {\n", " return MozWebSocket;\n", " } else {\n", " alert('Your browser does not have WebSocket support.' +\n", " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", " 'Firefox 4 and 5 are also supported but you ' +\n", " 'have to enable WebSockets in about:config.');\n", " };\n", "}\n", "\n", "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", " this.id = figure_id;\n", "\n", " this.ws = websocket;\n", "\n", " this.supports_binary = (this.ws.binaryType != undefined);\n", "\n", " if (!this.supports_binary) {\n", " var warnings = document.getElementById(\"mpl-warnings\");\n", " if (warnings) {\n", " warnings.style.display = 'block';\n", " warnings.textContent = (\n", " \"This browser does not support binary websocket messages. \" +\n", " \"Performance may be slow.\");\n", " }\n", " }\n", "\n", " this.imageObj = new Image();\n", "\n", " this.context = undefined;\n", " this.message = undefined;\n", " this.canvas = undefined;\n", " this.rubberband_canvas = undefined;\n", " this.rubberband_context = undefined;\n", " this.format_dropdown = undefined;\n", "\n", " this.image_mode = 'full';\n", "\n", " this.root = $('
');\n", " this._root_extra_style(this.root)\n", " this.root.attr('style', 'display: inline-block');\n", "\n", " $(parent_element).append(this.root);\n", "\n", " this._init_header(this);\n", " this._init_canvas(this);\n", " this._init_toolbar(this);\n", "\n", " var fig = this;\n", "\n", " this.waiting = false;\n", "\n", " this.ws.onopen = function () {\n", " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", " fig.send_message(\"send_image_mode\", {});\n", " if (mpl.ratio != 1) {\n", " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", " }\n", " fig.send_message(\"refresh\", {});\n", " }\n", "\n", " this.imageObj.onload = function() {\n", " if (fig.image_mode == 'full') {\n", " // Full images could contain transparency (where diff images\n", " // almost always do), so we need to clear the canvas so that\n", " // there is no ghosting.\n", " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", " }\n", " fig.context.drawImage(fig.imageObj, 0, 0);\n", " };\n", "\n", " this.imageObj.onunload = function() {\n", " this.ws.close();\n", " }\n", "\n", " this.ws.onmessage = this._make_on_message_function(this);\n", "\n", " this.ondownload = ondownload;\n", "}\n", "\n", "mpl.figure.prototype._init_header = function() {\n", " var titlebar = $(\n", " '
');\n", " var titletext = $(\n", " '
');\n", " titlebar.append(titletext)\n", " this.root.append(titlebar);\n", " this.header = titletext[0];\n", "}\n", "\n", "\n", "\n", "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "\n", "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", "\n", "}\n", "\n", "mpl.figure.prototype._init_canvas = function() {\n", " var fig = this;\n", "\n", " var canvas_div = $('
');\n", "\n", " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", "\n", " function canvas_keyboard_event(event) {\n", " return fig.key_event(event, event['data']);\n", " }\n", "\n", " canvas_div.keydown('key_press', canvas_keyboard_event);\n", " canvas_div.keyup('key_release', canvas_keyboard_event);\n", " this.canvas_div = canvas_div\n", " this._canvas_extra_style(canvas_div)\n", " this.root.append(canvas_div);\n", "\n", " var canvas = $('');\n", " canvas.addClass('mpl-canvas');\n", " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", "\n", " this.canvas = canvas[0];\n", " this.context = canvas[0].getContext(\"2d\");\n", "\n", " var backingStore = this.context.backingStorePixelRatio ||\n", "\tthis.context.webkitBackingStorePixelRatio ||\n", "\tthis.context.mozBackingStorePixelRatio ||\n", "\tthis.context.msBackingStorePixelRatio ||\n", "\tthis.context.oBackingStorePixelRatio ||\n", "\tthis.context.backingStorePixelRatio || 1;\n", "\n", " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", "\n", " var rubberband = $('');\n", " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", "\n", " var pass_mouse_events = true;\n", "\n", " canvas_div.resizable({\n", " start: function(event, ui) {\n", " pass_mouse_events = false;\n", " },\n", " resize: function(event, ui) {\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " stop: function(event, ui) {\n", " pass_mouse_events = true;\n", " fig.request_resize(ui.size.width, ui.size.height);\n", " },\n", " });\n", "\n", " function mouse_event_fn(event) {\n", " if (pass_mouse_events)\n", " return fig.mouse_event(event, event['data']);\n", " }\n", "\n", " rubberband.mousedown('button_press', mouse_event_fn);\n", " rubberband.mouseup('button_release', mouse_event_fn);\n", " // Throttle sequential mouse events to 1 every 20ms.\n", " rubberband.mousemove('motion_notify', mouse_event_fn);\n", "\n", " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", "\n", " canvas_div.on(\"wheel\", function (event) {\n", " event = event.originalEvent;\n", " event['data'] = 'scroll'\n", " if (event.deltaY < 0) {\n", " event.step = 1;\n", " } else {\n", " event.step = -1;\n", " }\n", " mouse_event_fn(event);\n", " });\n", "\n", " canvas_div.append(canvas);\n", " canvas_div.append(rubberband);\n", "\n", " this.rubberband = rubberband;\n", " this.rubberband_canvas = rubberband[0];\n", " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", " this.rubberband_context.strokeStyle = \"#000000\";\n", "\n", " this._resize_canvas = function(width, height) {\n", " // Keep the size of the canvas, canvas container, and rubber band\n", " // canvas in synch.\n", " canvas_div.css('width', width)\n", " canvas_div.css('height', height)\n", "\n", " canvas.attr('width', width * mpl.ratio);\n", " canvas.attr('height', height * mpl.ratio);\n", " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", "\n", " rubberband.attr('width', width);\n", " rubberband.attr('height', height);\n", " }\n", "\n", " // Set the figure to an initial 600x600px, this will subsequently be updated\n", " // upon first draw.\n", " this._resize_canvas(600, 600);\n", "\n", " // Disable right mouse context menu.\n", " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", " return false;\n", " });\n", "\n", " function set_focus () {\n", " canvas.focus();\n", " canvas_div.focus();\n", " }\n", "\n", " window.setTimeout(set_focus, 100);\n", "}\n", "\n", "mpl.figure.prototype._init_toolbar = function() {\n", " var fig = this;\n", "\n", " var nav_element = $('
')\n", " nav_element.attr('style', 'width: 100%');\n", " this.root.append(nav_element);\n", "\n", " // Define a callback function for later on.\n", " function toolbar_event(event) {\n", " return fig.toolbar_button_onclick(event['data']);\n", " }\n", " function toolbar_mouse_event(event) {\n", " return fig.toolbar_button_onmouseover(event['data']);\n", " }\n", "\n", " for(var toolbar_ind in mpl.toolbar_items) {\n", " var name = mpl.toolbar_items[toolbar_ind][0];\n", " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", " var image = mpl.toolbar_items[toolbar_ind][2];\n", " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", "\n", " if (!name) {\n", " // put a spacer in here.\n", " continue;\n", " }\n", " var button = $('