diff --git a/README.md b/README.md index 86059c11..b48b4e11 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ![Dashboard](static/img/launcher-icon-1x.png) PiFire -## Raspberry Pi Zero W based Smoker Grill Controller +## Raspberry Pi based Smoker Grill Controller ***Note:*** *This project is continuously evolving, and thus this readme will likely be improved over time, as I find the inspiration to make adjustments. That being said, I'm sure there will be many errors that I have overlooked or sections that I haven't updated. This project is something I've done for both fun and for self-education. If you decide to implement this project for yourself, and run into issues/challenges, feel free to submit an issue here on GitHub. However, I would highly encourage you to dig in and debug the issue as much as you can on your own for the sake of growing your own knowledge. Also, I have a very demanding day job, a family, and lots of barbecue to make - so please have patience with me.* @@ -26,26 +26,27 @@ Just as with the PiSmoker project, I had a few goals in mind. I also wanted to * Supports several different OLED and LCD screens * SSD1306 OLED Display * ST7789 TFT Display - * ILI9341 TFT Display (now with more rotation options) + * ILI9341 TFT Display (320x240 resolution only) + * DSI Touch Display (**Currently Experimental**) - Requires Raspberry Pi with DSI interface (non-Pi Zero) and is resource heavy, so Pi 3B+ or later recommended * Physical Button Input / Control (depending on the display, three button inputs) * Encoder support for, so you can control your grill with a spinny knob. * One (1) Grill Probe and Many Food Probes * Tunable probe inputs to allow for many different probe manufacturers - * Supports the ADS1115 ADC, ADS1015 ADC, and MAX31865 RTD devices for measuring probes + * Supports the ADS1115 ADC, ADS1015 ADC, and MAX31865 RTD devices for measuring probes (Experimental DS18B20 and MCP9600 support added) * Probe tuning tool to help develop probe profiles - * **NEW!** - Any number of probe inputs, limited only by the number of devices that the Raspberry Pi can support - * **NEW!** - Virtual Probes to allow you to do things like averaging probes, finding highest and lowest values of certain probes, etc. + * Any number of probe inputs, limited only by the number of devices that the Raspberry Pi can support + * Virtual Probes to allow you to do things like averaging probes, finding highest and lowest values of certain probes, etc. * Cook Timer - Moved to the Top Bar for Easy Access * Notifications (Grill / Food Probes / Timer) - * Supports Apprise, IFTTT, Pushover, and Pushbullet Notification Services + * Supports Apprise, IFTTT, Pushover, and Pushbullet Notification Services and now MQTT! * Smoke Plus Feature to deliver more smoke during Smoke / Hold modes * Safety settings to prevent over-temp, startup failure, or firepot flameout (and overload) * Save temperature history for all probes / set points to a cook file that can be updated with images, notes, and even downloaded to your devices. * Wood Pellet Tracking Manager - Now includes estimates of pellet usage. * Pellet Level Sensor Support - * VL53L0X Time of Flight Sensor + * VL53L0X Time of Flight Sensor (recommended) * HCSR04 Ultrasonic Sensor -* Socket IO for Android Application Support _(GitHub User [@weberbox](https://github.com/weberbox) has made a Android client app under development here: [https://github.com/weberbox/PiFire-Android](https://github.com/weberbox/PiFire-Android))_ **(NOTE: Due to the large amount of architectural changes in v1.5.0, the Android App update is still in development. The current version of PiFire has implemented a compatibility layer so that it doesn't break compatibility with the current Android App. If you have a non-standard probe setup or more than two food probes, you may see issues. There may still be bugs, so please do submit issues on GitHub if you experience any.)** +* Socket IO for Android Application Support _(GitHub User [@weberbox](https://github.com/weberbox) has made a Android client app under development here: [https://github.com/weberbox/PiFire-Android](https://github.com/weberbox/PiFire-Android))_ * Recipes / Recipe Mode - Integrated recipe creation and a new mode for developing a recipe 'program' that will control the grill for you and follow the recipe that was programmed. * Lid open detection during hold mode to pause the controller and prevent overshoots. * ...And much more! @@ -107,7 +108,8 @@ I've added a discord server [here](https://discord.gg/F9mbCrbrZS) which can be a * 7/2022 - Release v1.3.4 - Another monthly release with some bug fixes and some new features. The biggest new feature of this month was the addition of Annotations in the history graph. This gives you helpful tags on the graph (see below) with indicators of the mode changes. Of course you can turn this off in the history page if you don't like it. * 10/2022 - Release v1.3.5 - Bug fixes, feature refinements and brand new features galore in this latest release. Added a new ADS1115 module (using Adafruits Circuit Python), due to some reports of issues with the existing ADS1115 module. These can be optionally selected in the configuration wizard. PWM Fan Support and a boatload of code cleanup was introduced, thanks to contributor @weberbox. Support for saving cook files was introduced in this version, so that you can go back to older cooks, edit some of the information and add images and comments. Added a Prime Mode to allow you to prime the fire pot with pellets prior to a cook, and even prime & startup. Added estimated pellet usage to the pellet manager, which will attempt to track just how many pellets you have used since your last load of pellets. Added Apprise notification capability thanks to contributor @calonmerc. 320x200 displays have been update and added timers to specific modes. And even more! * 6/2023 - Release v1.5.0 - Arguably one of the biggest overhauls to PiFire since it's inception. The Probe system has been completely refactored to allow for multiple probe sensing devices (i.e. ADS1115, MAX31865, or even Virtual Probes to augment your inputs). This extension of the probe system, allows for any number of probe inputs to be tracked in PiFire, allowing from notifications and tracking of history for each probe. The sky is the limit! With this change the the probe architecture, a number of other things needed to be modified/updated, including the notification system, the history/charting, the dashboards, cookfiles and recipe modes. Note that if you are updating to this version, your settings will be upgraded in the process and you will not be able to roll back to a previous version (unless you restore from a backup of your settings). -* **11/2023** - Release v1.6.0 - In this month comes another huge update with lots of new features and bug fixes. Many thanks to the users from discord that have been testing along the way (as well as submitting some bugs), what a great community we have! Many of these features have been deployed on our development branch for some time, so they should be relatively stable. Please do file issues on GitHub if you find any new bugs with the formal release. With that, enjoy and happy grilling/smoking! +* 11/2023 - Release v1.6.0 - In this month comes another huge update with lots of new features and bug fixes. Many thanks to the users from discord that have been testing along the way (as well as submitting some bugs), what a great community we have! Many of these features have been deployed on our development branch for some time, so they should be relatively stable. Please do file issues on GitHub if you find any new bugs with the formal release. With that, enjoy and happy grilling/smoking! +* **5/2024** - Release v1.7.0 - Lot's of new updates in this release with the UI, new features (i.e. exit startup temp, etc.) and new device support. Some improvements to the tuning tools (including an auto-tuning tool). Under the covers improvements for stability and cleanup. As usual, submit issues to GitHub if you run into anything. Enjoy! ### Credits @@ -149,7 +151,7 @@ This project is licensed under the MIT license. ``` MIT License -Copyright (c) 2020 - 2023 Ben Parmeter and Contributors +Copyright (c) 2020 - 2024 Ben Parmeter and Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/app.py b/app.py index a344c7dd..50d734cd 100755 --- a/app.py +++ b/app.py @@ -30,8 +30,6 @@ import pathlib from threading import Thread from datetime import datetime -from common import generate_uuid, epoch_to_time, prepare_csv -from common.hacks import hack_read_control, hack_write_control, hack_read_settings, hack_write_settings, hack_prepare_data, hack_read_current from updater import * # Library for doing project updates from GitHub from file_mgmt.common import fixup_assets, read_json_file_data, update_json_file_data, remove_assets from file_mgmt.cookfile import read_cookfile, upgrade_cookfile, prepare_chartdata @@ -81,15 +79,20 @@ def dash(): errors = read_errors() warnings = read_warnings() - dash_template = 'dash_default.html' - for dash in settings['dashboard']['dashboards']: - if dash['name'] == settings['dashboard']['current']: - dash_template = dash['html_name'] - break + current = settings['dashboard']['current'] + dash_template = settings['dashboard']['dashboards'][current].get('html_name', 'dash_default.html') + dash_data = settings['dashboard']['dashboards'].get(current, {}) + + ''' Check if control process is up and running. ''' + process_command(action='sys', arglist=['check_alive'], origin='dash') # Request supported commands + data = _get_system_command_output(requested='check_alive') + if data['result'] != 'OK': + errors.append('The control process did not respond to a request and may be stopped. Try reloading the page or restarting the system. Check logs for details.') return render_template(dash_template, settings=settings, control=control, + dash_data=dash_data, errors=errors, warnings=warnings, page_theme=settings['globals']['page_theme'], @@ -102,6 +105,9 @@ def hopper_level(): pelletdb['archive'][pelletdb['current']['pelletid']]['wood'] return jsonify({ 'hopper_level' : pelletdb['current']['hopper_level'], 'cur_pellets' : cur_pellets_string }) +''' +This route will be deprecated in an upcoming release and has been replaced with the API calls /api/[get,set]/timer +''' @app.route('/timer', methods=['POST','GET']) def timer(): global settings @@ -252,7 +258,8 @@ def history_update(action=None): json_response['annotations'] = _prepare_annotations(displayed_starttime) json_response['mode'] = control['mode'] json_response['ui_hash'] = create_ui_hash() - + json_response['timestamp'] = int(time.time() * 1000) + return jsonify(json_response) elif action == 'refresh': @@ -793,144 +800,184 @@ def updatecookdata(action=None): return jsonify({'result' : result}) return jsonify({'result' : 'ERROR'}) - - -@app.route('/tuning/', methods=['POST','GET']) -@app.route('/tuning', methods=['POST','GET']) -def tuning_page(action=None): - global settings +@app.route('/tuner/', methods=['POST','GET']) +@app.route('/tuner', methods=['POST','GET']) +def tuner_page(action=None): + global settings control = read_control() + + # This POST path will load/render portions of the tuner page + if request.method == 'POST' and ('form' in request.content_type): + requestform = request.form + if 'command' in requestform.keys(): + if 'render' in requestform['command']: + render_string = "{% from '_macro_tuner.html' import render_" + requestform["value"] + " %}{{ render_" + requestform["value"] + "(settings, control) }}" + return render_template_string(render_string, settings=settings, control=control) + + # This POST path provides data back to the page + if request.method == 'POST' and 'json' in request.content_type: + requestjson = request.json + command = requestjson.get('command', None) + if command == 'stop_tuning': + if control['tuning_mode']: + control['tuning_mode'] = False # Disable tuning mode + write_control(control, origin='app') + if control['mode'] == 'Monitor': + # If in Monitor Mode, stop + control['mode'] = 'Stop' # Go to Stop mode + control['updated'] = True + write_control(control, origin='app') + if command == 'read_tr': + if not control['tuning_mode']: + control['tuning_mode'] = True # Enable tuning mode + write_control(control, origin='app') - if(control['mode'] == 'Stop'): - alert = 'Warning! Grill must be in an active mode to perform tuning (i.e. Monitor Mode, Smoke Mode, ' \ - 'Hold Mode, etc.)' - else: - alert = '' - - pagectrl = {} + if control['mode'] == 'Stop': + # Turn on Monitor Mode if the system is stopped + control['mode'] = 'Monitor' # Enable monitor mode + control['updated'] = True + write_control(control, origin='app') - pagectrl['refresh'] = 'off' - pagectrl['selected'] = 'none' - pagectrl['showcalc'] = 'false' - pagectrl['low_trvalue'] = '' - pagectrl['med_trvalue'] = '' - pagectrl['high_trvalue'] = '' - pagectrl['low_tempvalue'] = '' - pagectrl['med_tempvalue'] = '' - pagectrl['high_tempvalue'] = '' + cur_probe_tr = read_tr() + if requestjson['probe_selected'] in cur_probe_tr.keys(): + return jsonify({ 'trohms' : cur_probe_tr[requestjson['probe_selected']]}) + else: + return jsonify({ 'trohms' : 0 }) + if command == 'manual_finish' or command == 'auto_finish': + if control['tuning_mode']: + control['tuning_mode'] = False # Disable tuning mode + write_control(control, origin='app') + if control['mode'] == 'Monitor': + # If in Monitor Mode, stop + control['mode'] = 'Stop' # Go to Stop mode + control['updated'] = True + write_control(control, origin='app') + + tunerManualHighTemp = requestjson.get('tunerManualHighTemp', 0.1) + tunerManualHighTemp = 0 if tunerManualHighTemp == '' else int(tunerManualHighTemp) + tunerManualHighTr = requestjson.get('tunerManualHighTr', 0.1) + tunerManualHighTr = 0 if tunerManualHighTr == '' else int(tunerManualHighTr) + + tunerManualMediumTemp = requestjson.get('tunerManualMediumTemp', 0.1) + tunerManualMediumTemp = 0 if tunerManualMediumTemp == '' else int(tunerManualMediumTemp) + tunerManualMediumTr = requestjson.get('tunerManualMediumTr', 0.1) + tunerManualMediumTr = 0 if tunerManualMediumTr == '' else int(tunerManualMediumTr) + + tunerManualLowTemp = requestjson.get('tunerManualLowTemp', 0.1) + tunerManualLowTemp = 0 if tunerManualLowTemp == '' else int(tunerManualLowTemp) + tunerManualLowTr = requestjson.get('tunerManualLowTr', 0.1) + tunerManualLowTr = 0 if tunerManualLowTr == '' else int(tunerManualLowTr) + + a, b, c = _calc_shh_coefficients(tunerManualLowTemp, tunerManualMediumTemp, + tunerManualHighTemp, tunerManualLowTr, + tunerManualMediumTr, tunerManualHighTr, + units=settings['globals']['units']) + tr_points = [int(tunerManualHighTr), int(tunerManualMediumTr), int(tunerManualLowTr)] + labels, chart_data = _calc_shh_chart(a, b, c, units=settings['globals']['units'], temp_range=220, tr_points=tr_points) + return jsonify({'labels' : labels, 'chart_data' : chart_data, 'coefficients' : {'a' : a, 'b': b, 'c': c}}) + if command == 'read_auto_status': + first_run = False + if not control['tuning_mode']: + control['tuning_mode'] = True # Enable tuning mode + write_control(control, origin='app') + read_autotune(flush=True) # Flush autotune data + first_run = True - if request.method == 'POST': - response = request.form - if 'probe_select' in response: - pagectrl['selected'] = response['probe_select'] - pagectrl['refresh'] = 'on' - control['tuning_mode'] = True # Enable tuning mode - write_control(control, origin='app') - - if'pause' in response: - if response['low_trvalue'] != '': - pagectrl['low_trvalue'] = response['low_trvalue'] - if response['med_trvalue'] != '': - pagectrl['med_trvalue'] = response['med_trvalue'] - if response['high_trvalue'] != '': - pagectrl['high_trvalue'] = response['high_trvalue'] - - if response['low_tempvalue'] != '': - pagectrl['low_tempvalue'] = response['low_tempvalue'] - if response['med_tempvalue'] != '': - pagectrl['med_tempvalue'] = response['med_tempvalue'] - if response['high_tempvalue'] != '': - pagectrl['high_tempvalue'] = response['high_tempvalue'] - - pagectrl['refresh'] = 'off' - control['tuning_mode'] = False # Disable tuning mode while paused + if control['mode'] == 'Stop': + # Turn on Monitor Mode if the system is stopped + control['mode'] = 'Monitor' # Enable monitor mode + control['updated'] = True write_control(control, origin='app') - elif 'save' in response: - if response['low_trvalue'] != '': - pagectrl['low_trvalue'] = response['low_trvalue'] - if response['med_trvalue'] != '': - pagectrl['med_trvalue'] = response['med_trvalue'] - if response['high_trvalue'] != '': - pagectrl['high_trvalue'] = response['high_trvalue'] - - if response['low_tempvalue'] != '': - pagectrl['low_tempvalue'] = response['low_tempvalue'] - if response['med_tempvalue'] != '': - pagectrl['med_tempvalue'] = response['med_tempvalue'] - if response['high_tempvalue'] != '': - pagectrl['high_tempvalue'] = response['high_tempvalue'] - - if (pagectrl['low_tempvalue'] != '' and pagectrl['med_tempvalue'] != '' and - pagectrl['high_tempvalue'] != ''): - pagectrl['refresh'] = 'off' - control['tuning_mode'] = False # Disable tuning mode when complete - write_control(control, origin='app') - pagectrl['showcalc'] = 'true' - a, b, c = _calc_shh_coefficients(int(pagectrl['low_tempvalue']), int(pagectrl['med_tempvalue']), - int(pagectrl['high_tempvalue']), int(pagectrl['low_trvalue']), - int(pagectrl['med_trvalue']), int(pagectrl['high_trvalue']), units=settings['globals']['units']) - pagectrl['a'] = a - pagectrl['b'] = b - pagectrl['c'] = c - - pagectrl['templist'] = '' - pagectrl['trlist'] = '' - - range_size = abs(int(pagectrl['low_trvalue']) - int(pagectrl['high_trvalue'])) - range_step = int(range_size / 20) - - if int(pagectrl['low_trvalue']) < int(pagectrl['high_trvalue']): - # Add 5% to the resistance at the low temperature side - low_tr_range = int(int(pagectrl['low_trvalue']) - (range_size * 0.05)) - # Add 5% to the resistance at the high temperature side - high_tr_range = int(int(pagectrl['high_trvalue']) + (range_size * 0.05)) - # Swap Tr values for the loop below, so that we start with a low value and go high - high_tr_range, low_tr_range = low_tr_range, high_tr_range - # Swapped Value Case (i.e. Low Temp = Low Resistance) - for index in range(high_tr_range, low_tr_range, range_step): - if index == high_tr_range: - pagectrl['trlist'] = str(index) - pagectrl['templist'] = str(_tr_to_temp(index, a, b, c, units=settings['globals']['units'])) - else: - pagectrl['trlist'] = str(index) + ', ' + pagectrl['trlist'] - pagectrl['templist'] = str(_tr_to_temp(index, a, b, c, units=settings['globals']['units'])) + ', ' + pagectrl['templist'] - else: - # Add 5% to the resistance at the low temperature side - low_tr_range = int(int(pagectrl['low_trvalue']) + (range_size * 0.05)) - # Add 5% to the resistance at the high temperature side - high_tr_range = int(int(pagectrl['high_trvalue']) - (range_size * 0.05)) - # Normal Value Case (i.e. Low Temp = High Resistance) - for index in range(high_tr_range, low_tr_range, range_step): - if index == high_tr_range: - pagectrl['trlist'] = str(index) - pagectrl['templist'] = str(_tr_to_temp(index, a, b, c, units=settings['globals']['units'])) - else: - pagectrl['trlist'] += ', ' + str(index) - pagectrl['templist'] += ', ' + str(_tr_to_temp(index, a, b, c, units=settings['globals']['units'])) + status_data = { + 'current_tr' : 0, + 'current_temp' : 0, + 'high_tr' : 0, + 'high_temp' : 0, + 'medium_tr' : 0, + 'medium_temp' : 0, + 'low_tr' : 0, + 'low_temp' : 0, + 'ready' : False + } + + # Get Tr Data from all probes + cur_probe_tr = read_tr() + if requestjson['probe_selected'] in cur_probe_tr.keys(): + status_data['current_tr'] = cur_probe_tr[requestjson['probe_selected']] + else: + status_data['current_tr'] = -1 + + # Get Temp Data from all probes + cur_probe_temps = read_current() + if requestjson['probe_reference'] in cur_probe_temps['P'].keys(): + status_data['current_temp'] = cur_probe_temps['P'][requestjson['probe_reference']] + elif requestjson['probe_reference'] in cur_probe_temps['F'].keys(): + status_data['current_temp'] = cur_probe_temps['F'][requestjson['probe_reference']] + elif requestjson['probe_reference'] in cur_probe_temps['AUX'].keys(): + status_data['current_temp'] = cur_probe_temps['AUX'][requestjson['probe_reference']] + else: + status_data['current_temp'] = -1 + + # Some probes (i.e. the DS18B20) may be slow to respond when Monitor mode starts, and may report 0 degrees + # Thus we should ignore these first few data points if they are 0 + autotune_data_size = read_autotune(size_only=True) + if (autotune_data_size > 4 or status_data['current_temp'] > 0) and \ + status_data['current_tr'] >= 0 and \ + status_data['current_temp'] >= 0 and \ + not first_run: + # Record Temperature / Tr Values in Auto-Tune Record + data = { + 'ref_T' : status_data['current_temp'], + 'probe_Tr' : status_data['current_tr'] + } + write_autotune(data) + + data = read_autotune() + if len(data) > 10: + temp_list = [] + tr_list = [] + for datapoint in data: + temp_list.append(datapoint['ref_T']) + tr_list.append(datapoint['probe_Tr']) + + # Determine High Temp / Tr + status_data['high_temp'] = max(temp_list) + index = temp_list.index(max(temp_list)) + status_data['high_tr'] = tr_list[index] + + # Determine Low Temp / Tr + status_data['low_temp'] = min(temp_list) + index = temp_list.index(min(temp_list)) + status_data['low_tr'] = tr_list[index] + + # Determine Medium Temp / Tr + # Find best fit to Medium Temp + medium_temp = ((status_data['high_temp'] - status_data['low_temp']) // 2) + status_data['low_temp'] + delta_temp = 1000 # Initial value is outside of any normal expected bounds + for index, temp in enumerate(temp_list): + if abs(temp - medium_temp) < delta_temp: + delta_temp = abs(temp - medium_temp) + delta_index = index + status_data['medium_temp'] = temp_list[delta_index] + status_data['medium_tr'] = tr_list[delta_index] + # Minimum range to be able to calculate temp + if settings['globals']['units'] == 'F': + min_range = 50 else: - pagectrl['refresh'] = 'on' - control['tuning_mode'] = True # Enable tuning mode - write_control(control, origin='app') - - return render_template('tuning.html', - control=control, - settings=settings, - pagectrl=pagectrl, - page_theme=settings['globals']['page_theme'], - grill_name=settings['globals']['grill_name'], - alert=alert) - -@app.route('/_gettr', methods=['GET', 'POST']) -def get_tr(): - requestjson = request.json - cur_probe_tr = read_tr() - if requestjson['probe_selected'] in cur_probe_tr.keys(): - return jsonify({ 'trohms' : cur_probe_tr[requestjson['probe_selected']]}) - else: - return jsonify({ 'trohms' : 0 }) + min_range = 25 + + if (status_data['high_temp'] - status_data['low_temp']) >= min_range: + status_data['ready'] = True + return jsonify(status_data) + + return render_template('tuner.html', + control=control, + settings=settings, + page_theme=settings['globals']['page_theme'], + grill_name=settings['globals']['grill_name']) @app.route('/events/', methods=['POST','GET']) @app.route('/events', methods=['POST','GET']) @@ -1603,6 +1650,25 @@ def settings_page(action=None): if 'influxdb_bucket' in response: settings['notify_services']['influxdb']['bucket'] = response['influxdb_bucket'] + if _is_checked(response, 'mqtt_enabled'): + settings['notify_services']['mqtt']['enabled'] = True + else: + settings['notify_services']['mqtt']['enabled'] = False + if 'mqtt_id' in response: + settings['notify_services']['mqtt']['id'] = response['mqtt_id'] + if 'mqtt_broker' in response: + settings['notify_services']['mqtt']['broker'] = response['mqtt_broker'] + if 'mqtt_port' in response: + settings['notify_services']['mqtt']['port'] = response['mqtt_port'] + if 'mqtt_user' in response: + settings['notify_services']['mqtt']['username'] = response['mqtt_user'] + if 'mqtt_pw' in response: + settings['notify_services']['mqtt']['password'] = response['mqtt_pw'] + if 'mqtt_auto_d' in response: + settings['notify_services']['mqtt']['homeassistant_autodiscovery_topic'] = response['mqtt_auto_d'] + if 'mqtt_freq' in response: + settings['notify_services']['mqtt']['update_sec'] = response['mqtt_freq'] + if 'delete_device' in response: DeviceID = response['delete_device'] settings['notify_services']['onesignal']['devices'].pop(DeviceID) @@ -1694,10 +1760,17 @@ def settings_page(action=None): 'name' : response['Name'], 'id' : UniqueID } - event['type'] = 'updated' - event['text'] = 'Successfully added ' + response['Name'] + ' profile.' + print(f'Response: {response}') + if response.get('apply_profile', False): + probe_selected = response['apply_profile'] + for index, probe in enumerate(settings['probe_settings']['probe_map']['probe_info']): + if probe['label'] == probe_selected: + settings['probe_settings']['probe_map']['probe_info'][index]['profile'] = settings['probe_settings']['probe_profiles'][UniqueID] + # Write the new probe profile to disk write_settings(settings) + event['type'] = 'updated' + event['text'] = 'Successfully added ' + response['Name'] + ' profile.' except: event['type'] = 'error' @@ -1819,24 +1892,33 @@ def settings_page(action=None): write_settings(settings) write_control(control, origin='app') - if request.method == 'POST' and action == 'timers': + if request.method == 'POST' and action == 'startup': response = request.form - if _is_not_blank(response, 'shutdown_timer'): - settings['globals']['shutdown_timer'] = int(response['shutdown_timer']) - if _is_not_blank(response, 'startup_timer'): - settings['globals']['startup_timer'] = int(response['startup_timer']) + if _is_not_blank(response, 'shutdown_duration'): + settings['shutdown']['shutdown_duration'] = int(response['shutdown_duration']) + if _is_not_blank(response, 'startup_duration'): + settings['startup']['duration'] = int(response['startup_duration']) if _is_checked(response, 'auto_power_off'): - settings['globals']['auto_power_off'] = True + settings['shutdown']['auto_power_off'] = True else: - settings['globals']['auto_power_off'] = False + settings['shutdown']['auto_power_off'] = False if _is_checked(response, 'smartstart_enable'): - settings['smartstart']['enabled'] = True + settings['startup']['smartstart']['enabled'] = True else: - settings['smartstart']['enabled'] = False - - settings['start_to_mode']['after_startup_mode'] = response['after_startup_mode'] - settings['start_to_mode']['primary_setpoint'] = int(response['startup_mode_setpoint']) + settings['startup']['smartstart']['enabled'] = False + if _is_not_blank(response, 'smartstart_exit_temp'): + settings['startup']['smartstart']['exit_temp'] = int(response['smartstart_exit_temp']) + if _is_not_blank(response, 'startup_exit_temp'): + settings['startup']['startup_exit_temp'] = int(response['startup_exit_temp']) + if _is_not_blank(response, 'prime_on_startup'): + prime_amount = int(response['prime_on_startup']) + if prime_amount < 0 or prime_amount > 200: + prime_amount = 0 # Validate input, set to disabled if exceeding limits. + settings['startup']['prime_on_startup'] = int(response['prime_on_startup']) + + settings['startup']['start_to_mode']['after_startup_mode'] = response['after_startup_mode'] + settings['startup']['start_to_mode']['primary_setpoint'] = int(response['startup_mode_setpoint']) event['type'] = 'updated' event['text'] = 'Successfully updated startup/shutdown settings.' @@ -1935,6 +2017,10 @@ def settings_page(action=None): settings['safety']['reigniteretries'] = int(response['reigniteretries']) if _is_not_blank(response, 'maxtemp'): settings['safety']['maxtemp'] = int(response['maxtemp']) + if _is_checked(response, 'startup_check'): + settings['safety']['startup_check'] = True + else: + settings['safety']['startup_check'] = False event['type'] = 'updated' event['text'] = 'Successfully updated safety settings.' @@ -2010,14 +2096,14 @@ def settings_page(action=None): Smart Start Settings ''' if request.method == 'GET' and action == 'smartstart': - temps = settings['smartstart']['temp_range_list'] - profiles = settings['smartstart']['profiles'] + temps = settings['startup']['smartstart']['temp_range_list'] + profiles = settings['startup']['smartstart']['profiles'] return(jsonify({'temps_list' : temps, 'profiles' : profiles})) if request.method == 'POST' and action == 'smartstart': response = request.json - settings['smartstart']['temp_range_list'] = response['temps_list'] - settings['smartstart']['profiles'] = response['profiles'] + settings['startup']['smartstart']['temp_range_list'] = response['temps_list'] + settings['startup']['smartstart']['profiles'] = response['profiles'] write_settings(settings) return(jsonify({'result' : 'success'})) @@ -2051,7 +2137,10 @@ def admin_page(action=None): global settings control = read_control() pelletdb = read_pellet_db() - notify = '' + + errors = [] + warnings = [] + success = [] if not os.path.exists(BACKUP_PATH): os.mkdir(BACKUP_PATH) @@ -2063,18 +2152,16 @@ def admin_page(action=None): if action == 'reboot': event = "Admin: Reboot" write_log(event) - if is_raspberry_pi(): - os.system("sleep 3 && sudo reboot &") server_status = 'rebooting' + reboot_system() return render_template('shutdown.html', action=action, page_theme=settings['globals']['page_theme'], grill_name=settings['globals']['grill_name']) elif action == 'shutdown': event = "Admin: Shutdown" write_log(event) - if is_raspberry_pi(): - os.system("sleep 3 && sudo shutdown -h now &") server_status = 'shutdown' + shutdown_system() return render_template('shutdown.html', action=action, page_theme=settings['globals']['page_theme'], grill_name=settings['globals']['grill_name']) @@ -2143,6 +2230,14 @@ def admin_page(action=None): zip_file = _zip_files_logs('logs') return send_file(zip_file, as_attachment=True, max_age=0) + if 'download_settings' in response: + return send_file('settings.json', as_attachment=True, max_age=0) + + if 'download_control' in response: + filename = '/tmp/control_general.json' + write_generic_json(control, filename) + return send_file(filename, as_attachment=True, max_age=0) + if 'backupsettings' in response: backup_file = backup_settings() return send_file(backup_file, as_attachment=True, max_age=0) @@ -2165,7 +2260,7 @@ def admin_page(action=None): if remote_file and _allowed_file(remote_file.filename): filename = secure_filename(remote_file.filename) remote_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) - notify = "success" + success.append('Successfully restored settings.') new_settings = read_settings(filename=BACKUP_PATH+filename) write_settings(new_settings) server_status = 'restarting' @@ -2173,9 +2268,9 @@ def admin_page(action=None): return render_template('shutdown.html', action='restart', page_theme=settings['globals']['page_theme'], grill_name=settings['globals']['grill_name']) else: - notify = "error" + errors.append('There was an error restoring settings. File either is a disallowed type or was not found.') else: - notify = "error" + errors.append('There was an error restoring settings. Restore file wasn\'t specified or found') if 'backuppelletdb' in response: backup_file = backup_pellet_db(action='backup') @@ -2189,20 +2284,20 @@ def admin_page(action=None): if local_file != 'none': pelletdb = read_pellet_db(filename=BACKUP_PATH+local_file) write_pellet_db(pelletdb) - notify = "success" + success.append('Successfully restored pellet database.') elif remote_file.filename != '': # If the user does not select a file, the browser submits an # empty file without a filename. if remote_file and _allowed_file(remote_file.filename): filename = secure_filename(remote_file.filename) remote_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) - notify = "success" + success.append('Successfully restored pellet database.') pelletdb = read_pellet_db(filename=BACKUP_PATH+filename) write_pellet_db(pelletdb) else: - notify = "error" + errors.append('There was an error restoring the pellet database. File either is a disallowed type or was not found.') else: - notify = "error" + errors.append('There was an error restoring pellet database. Restore file wasn\'t specified or found') if request.method == 'POST' and action == 'boot': response = request.form @@ -2214,26 +2309,63 @@ def admin_page(action=None): write_settings(settings) + ''' + Get System Information + ''' + + # TODO: Convert uptime, cpu_info, infconfig to system commands uptime = os.popen('uptime').readline() cpu_info = os.popen('cat /proc/cpuinfo').readlines() ifconfig = os.popen('ifconfig').readlines() - if is_raspberry_pi(): - temp = _check_cpu_temp() - else: - temp = '---' + supported_cmds = _get_supported_cmds() + + if 'check_wifi_quality' in supported_cmds: + process_command(action='sys', arglist=['check_wifi_quality'], origin='admin') # Request supported commands + data = _get_system_command_output(requested='check_wifi_quality') + if data['result'] != 'OK': + event = data['message'] + errors.append(event) + control['system']['wifi_quality_value'] = data['data'].get('wifi_quality_value', None) + control['system']['wifi_quality_max'] = data['data'].get('wifi_quality_max', None) + control['system']['wifi_quality_percentage'] = data['data'].get('wifi_quality_percentage', None) + + if 'check_throttled' in supported_cmds: + process_command(action='sys', arglist=['check_throttled'], origin='admin') # Request supported commands + data = _get_system_command_output(requested='check_throttled') + if data['result'] != 'OK': + event = data['message'] + errors.append(event) + control['system']['cpu_throttled'] = data['data'].get('cpu_throttled', None) + control['system']['cpu_under_voltage'] = data['data'].get('cpu_under_voltage', None) + + if control['system']['cpu_throttled'] or control['system']['cpu_under_voltage']: + event = "CPU Throttled / Undervoltage event has occurred. Check your power supply for proper voltage." + errors.append(event) + print(event) + + if 'check_cpu_temp' in supported_cmds: + process_command(action='sys', arglist=['check_cpu_temp'], origin='admin') # Request supported commands + data = _get_system_command_output(requested='check_cpu_temp') + if data['result'] != 'OK': + event = data['message'] + errors.append(event) + control['system']['cpu_temp'] = data['data'].get('cpu_temp', None) + + write_control(control) debug_mode = settings['globals']['debug_mode'] url = request.url_root - return render_template('admin.html', settings=settings, notify=notify, uptime=uptime, cpuinfo=cpu_info, temp=temp, + return render_template('admin.html', settings=settings, uptime=uptime, cpuinfo=cpu_info, ifconfig=ifconfig, debug_mode=debug_mode, qr_content=url, control=control, page_theme=settings['globals']['page_theme'], - grill_name=settings['globals']['grill_name'], files=files) + grill_name=settings['globals']['grill_name'], + files=files, errors=errors, warnings=warnings, success=success) @app.route('/manual/', methods=['POST','GET']) @app.route('/manual', methods=['POST','GET']) @@ -2296,13 +2428,30 @@ def manual_page(action=None): page_theme=settings['globals']['page_theme'], grill_name=settings['globals']['grill_name']) -@app.route('/api/', methods=['POST','GET']) @app.route('/api', methods=['POST','GET']) -def api_page(action=None): +@app.route('/api/', methods=['POST','GET']) +@app.route('/api//', methods=['POST','GET']) +@app.route('/api///', methods=['POST','GET']) +@app.route('/api////', methods=['POST','GET']) +@app.route('/api/////', methods=['POST','GET']) +def api_page(action=None, arg0=None, arg1=None, arg2=None, arg3=None): global settings global server_status - if request.method == 'GET': + if action in ['get', 'set', 'cmd', 'sys']: + #print(f'action={action}\narg0={arg0}\narg1={arg1}\narg2={arg2}\narg3={arg3}') + arglist = [] + arglist.extend([arg0, arg1, arg2, arg3]) + + data = process_command(action=action, arglist=arglist, origin='api') + + if action == 'sys': + ''' If system command, wait for output from control ''' + data = _get_system_command_output(requested=arg0) + + return jsonify(data), 201 + + elif request.method == 'GET': if action == 'settings': return jsonify({'settings':settings}), 201 elif action == 'server': @@ -2342,6 +2491,7 @@ def api_page(action=None): status['lid_open_endtime'] = display['lid_open_endtime'] status['p_mode'] = display['p_mode'] status['outpins'] = display['outpins'] + status['startup_timestamp'] = display['startup_timestamp'] status['ui_hash'] = create_ui_hash() return jsonify({'current':current_temps, 'notify_data':notify_data, 'status':status}), 201 elif action == 'hopper': @@ -2352,6 +2502,7 @@ def api_page(action=None): return jsonify({'hopper_level': pelletlevel, 'hopper_pellets': pellets}) else: return jsonify({'Error':'Received GET request, without valid action'}), 404 + elif request.method == 'POST': if not request.json: event = "Local API Call Failed" @@ -2360,9 +2511,12 @@ def api_page(action=None): else: request_json = request.json if(action == 'settings'): + settings = deep_update(settings, request.json) + ''' for key in settings.keys(): if key in request_json.keys(): settings[key].update(request_json.get(key, {})) + ''' write_settings(settings) return jsonify({'settings':'success'}), 201 elif(action == 'control'): @@ -2383,6 +2537,7 @@ def api_page(action=None): @app.route('/wizard', methods=['GET', 'POST']) def wizard(action=None): global settings + control = read_control() wizardData = read_wizard() errors = [] @@ -2403,7 +2558,6 @@ def wizard(action=None): write_settings(settings) return redirect('/') if action=='finish': - control = read_control() if control['mode'] == 'Stop': wizardInstallInfo = prepare_wizard_data(r) store_wizard_install_info(wizardInstallInfo) @@ -2411,8 +2565,6 @@ def wizard(action=None): os.system(f'{python_exec} wizard.py &') # Kickoff Installation return render_template('wizard-finish.html', page_theme=settings['globals']['page_theme'], grill_name=settings['globals']['grill_name'], wizardData=wizardData) - else: - errors.append('PiFire configuration wizard cannot be run while the system is active. Please stop the current cook before continuing.') if action=='modulecard': module = r['module'] @@ -2431,10 +2583,13 @@ def wizard(action=None): else: wizardInstallInfo = wizardInstallInfoExisting(wizardData, settings) - store_wizard_install_info(wizardInstallInfo) + store_wizard_install_info(wizardInstallInfo) + + if control['mode'] != 'Stop': + errors.append('PiFire configuration wizard cannot be run while the system is active. Please stop the current cook before continuing.') return render_template('wizard.html', settings=settings, page_theme=settings['globals']['page_theme'], - grill_name=settings['globals']['grill_name'], wizardData=wizardData, wizardInstallInfo=wizardInstallInfo, errors=errors) + grill_name=settings['globals']['grill_name'], wizardData=wizardData, wizardInstallInfo=wizardInstallInfo, control=control, errors=errors) def get_settings_dependencies_values(settings, moduleData): moduleSettings = {} @@ -2960,7 +3115,7 @@ def update_page(action=None): update_data = get_update_data(settings) if 'update_remote_branches' in r: - if is_raspberry_pi(): + if is_real_hardware(): os.system(f'{python_exec} %s %s &' % ('updater.py', '-r')) # Update branches from remote time.sleep(5) # Artificial delay to avoid race condition return redirect('/update') @@ -3058,8 +3213,12 @@ def _allowed_file(filename): filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def _check_cpu_temp(): - temp = os.popen('vcgencmd measure_temp').readline() - return temp.replace("temp=","") + process_command(action='sys', arglist=['check_cpu_temp'], origin='admin') # Request supported commands + data = _get_system_command_output(requested='check_cpu_temp') + control = read_control() + control['system']['cpu_temp'] = data['data'].get('cpu_temp', None) + write_control(control) + return f"{control['system']['cpu_temp']}C" def create_ui_hash(): global settings @@ -3302,24 +3461,29 @@ def _calc_shh_coefficients(t1, t2, t3, r1, r2, r3, units='F'): return(a, b, c) -def _temp_to_tr(temp_f, a, b, c): +def _temp_to_tr(temp, a, b, c, units='F'): + ''' + # Not recommended for use, as it commonly produces a complex number + ''' + try: - temp_k = ((temp_f - 32) * (5 / 9)) + 273.15 + if units == 'F': + temp_k = ((temp - 32) * (5 / 9)) + 273.15 + else: + temp_k = temp + 273.15 # https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation # Inverse of the equation, to determine Tr = Resistance Value of the thermistor - # Not recommended for use, as it commonly produces a complex number - - x = (1 / (2 * c)) * (a - (1 / temp_k)) - - y = math.sqrt(math.pow((b / (3 * c)), 3) + math.pow(x, 2)) - - Tr = math.exp(((y - x) ** (1 / 3)) - ((y + x) ** (1 / 3))) + x = (a - (1 / temp_k)) / c + y1 = math.pow((b/(3*c)), 3) + y2 = ((x*x)/4) + y = math.sqrt(y1+y2) # If the result of y1 + y2 is negative, this will throw an exception + Tr = math.exp(math.pow(y - (x/2), (1/3)) - math.pow(y + (x/2), (1/3))) except: Tr = 0 - return int(Tr) + return int(Tr) def _tr_to_temp(tr, a, b, c, units='F'): try: @@ -3330,13 +3494,38 @@ def _tr_to_temp(tr, a, b, c, units='F'): t1 = (b * ln_ohm) # b[ln(ohm)] t2 = c * math.pow(ln_ohm, 3) # c[ln(ohm)]^3 temp_k = 1/(a + t1 + t2) # calculate temperature in Kelvin - result = temp_k - 273.15 # Kelvin to Celsius - if units=='F': - result = result * (9 / 5) + 32 # Celsius to Fahrenheit + temp_c = temp_k - 273.15 # Kelvin to Celsius + temp_f = temp_c * (9 / 5) + 32 # Celsius to Fahrenheit except: - result = 0.0 + temp_c = 0.0 + temp_f = 0 + if units == 'F': + return int(temp_f) # Return Calculated Temperature and Thermistor Value in Ohms + else: + return temp_c + +def _calc_shh_chart(a, b, c, units='F', temp_range=220, tr_points=[]): + ''' + Based on SHH Coefficients determined during tuning, show Temp (x) vs. Tr (y) chart + ''' + + labels = [] + + for label in range(0, temp_range, temp_range//20): + labels.append(label) - return int(result) # Return Calculated Temperature and Thermistor Value in Ohms + chart_data = [] + + for T in labels: + R = _temp_to_tr(T, a, b, c, units=units) + if R != 0: + chart_data.append({'x': int(T), 'y': int(R)}) + else: + # Error/Exception occurred calculating the temperature, break and return + chart_data = [] + break + + return labels, chart_data def _str_td(td): s = str(td).split(", ", 1) @@ -3365,13 +3554,29 @@ def _zip_files_logs(dir_name): archive.write(file_path, arcname=file_path.relative_to(directory)) return file_name -def _deep_dict_update(orig_dict, new_dict): - for key, value in new_dict.items(): - if isinstance(value, Mapping): - orig_dict[key] = _deep_dict_update(orig_dict.get(key, {}), value) - else: - orig_dict[key] = value - return orig_dict +def _get_supported_cmds(): + process_command(action='sys', arglist=['supported_commands'], origin='admin') # Request supported commands + data = _get_system_command_output(requested='supported_commands') + if data['result'] != 'ERROR': + return data['data']['supported_cmds'] + else: + return data + +def _get_system_command_output(requested='supported_commands', timeout=1): + system_output = RedisQueue('control:systemo') + endtime = timeout + time.time() + while time.time() < endtime: + while system_output.length() > 0: + data = system_output.pop() + if data['command'][0] == requested: + return data + + return { + 'command' : [requested, None, None, None], + 'result' : 'ERROR', + 'message' : 'The requested command output could not be found.', + 'data' : {'Response_Was' : 'To_Fast'} + } ''' ============================================================================== @@ -3409,25 +3614,9 @@ def emit_dash_data(): previous_data = '' while (clients > 0): - settings = hack_read_settings() - control = hack_read_control() + control = read_control() pelletdb = read_pellet_db() - - probes_enabled = settings['probe_settings']['probes_enabled'] - cur_probe_temps = hack_read_current() - - current_temps = { - 'grill_temp' : cur_probe_temps[0], - 'probe1_temp' : cur_probe_temps[1], - 'probe2_temp' : cur_probe_temps[2] } - enabled_probes = { - 'grill' : bool(probes_enabled[0]), - 'probe1' : bool(probes_enabled[1]), - 'probe2' : bool(probes_enabled[2]) } - probe_titles = { - 'grill_title' : control['probe_titles']['grill_title'], - 'probe1_title' : control['probe_titles']['probe1_title'], - 'probe2_title' : control['probe_titles']['probe2_title'] } + probe_info = read_current() if control['timer']['end'] - time.time() > 0 or bool(control['timer']['paused']): timer_info = { @@ -3447,11 +3636,7 @@ def emit_dash_data(): } current_data = { - 'cur_probe_temps' : current_temps, - 'probes_enabled' : enabled_probes, - 'probe_titles' : probe_titles, - 'set_points' : control['setpoints'], - 'notify_req' : control['notify_req'], + 'probe_info' : probe_info, 'notify_data' : control['notify_data'], 'timer_info' : timer_info, 'current_mode' : control['mode'], @@ -3462,12 +3647,10 @@ def emit_dash_data(): if force_refresh: socketio.emit('grill_control_data', current_data) - #socketio.emit('grill_control_data', current_data, broadcast=True) force_refresh = False socketio.sleep(2) elif previous_data != current_data: socketio.emit('grill_control_data', current_data) - #socketio.emit('grill_control_data', current_data, broadcast=True) previous_data = current_data socketio.sleep(2) else: @@ -3475,7 +3658,7 @@ def emit_dash_data(): @socketio.on('get_app_data') def get_app_data(action=None, type=None): - settings = hack_read_settings() + global settings if action == 'settings_data': return settings @@ -3489,23 +3672,6 @@ def get_app_data(action=None, type=None): for x in range(min(num_events, 60)): events_trim.append(event_list[x]) return { 'events_list' : events_trim } - - elif action == 'history_data': - num_items = settings['history_page']['minutes'] * 20 - data_blob = hack_prepare_data(num_items, True, settings['history_page']['datapoints']) - # Converting time format from 'time from epoch' to H:M:S - # @weberbox: Trying to keep the legacy format for the time labels so that I don't break the Android app - for index in range(0, len(data_blob['label_time_list'])): - data_blob['label_time_list'][index] = datetime.datetime.fromtimestamp( - int(data_blob['label_time_list'][index]) / 1000).strftime('%H:%M:%S') - - return { 'grill_temp_list' : data_blob['grill_temp_list'], - 'grill_settemp_list' : data_blob['grill_settemp_list'], - 'probe1_temp_list' : data_blob['probe1_temp_list'], - 'probe1_settemp_list' : data_blob['probe1_settemp_list'], - 'probe2_temp_list' : data_blob['probe2_temp_list'], - 'probe2_settemp_list' : data_blob['probe2_settemp_list'], - 'label_time_list' : data_blob['label_time_list'] } elif action == 'info_data': return { @@ -3516,83 +3682,20 @@ def get_app_data(action=None, type=None): 'outpins' : settings['outpins'], 'inpins' : settings['inpins'], 'dev_pins' : settings['dev_pins'], - 'server_version' : settings['versions']['server'] } + 'server_version' : settings['versions']['server'], + 'server_build' : settings['versions']['build'] } elif action == 'manual_data': - control = hack_read_control() + control = read_control() return { 'manual' : control['manual'], 'mode' : control['mode'] } - - elif action == 'backup_list': - if not os.path.exists(BACKUP_PATH): - os.mkdir(BACKUP_PATH) - files = os.listdir(BACKUP_PATH) - for file in files[:]: - if not _allowed_file(file): - files.remove(file) - - if type == 'settings': - for file in files[:]: - if not file.startswith('PiFire_'): - files.remove(file) - return json.dumps(files) - - if type == 'pelletdb': - for file in files[:]: - if not file.startswith('PelletDB_'): - files.remove(file) - return json.dumps(files) - - elif action == 'backup_data': - time_now = datetime.datetime.now() - time_str = time_now.strftime('%m-%d-%y_%H%M%S') - - if type == 'settings': - backup_settings() - return settings - - if type == 'pelletdb': - backup_file = BACKUP_PATH + 'PelletDB_' + time_str + '.json' - os.system(f'cp pelletdb.json {backup_file}') - return read_pellet_db() - - elif action == 'updater_data': - avail_updates_struct = get_available_updates() - - if avail_updates_struct['success']: - commits_behind = avail_updates_struct['commits_behind'] - else: - message = avail_updates_struct['message'] - write_log(message) - return {'response': {'result':'error', 'message':'Error: ' + message }} - - if commits_behind > 0: - logs_result = get_log(commits_behind) - else: - logs_result = None - - update_data = {} - update_data['branch_target'], error_msg = get_branch() - update_data['branches'], error_msg = get_available_branches() - update_data['remote_url'], error_msg = get_remote_url() - update_data['remote_version'], error_msg = get_remote_version() - - return { 'check_success' : avail_updates_struct['success'], - 'version' : settings['versions']['server'], - 'branches' : update_data['branches'], - 'branch_target' : update_data['branch_target'], - 'remote_url' : update_data['remote_url'], - 'remote_version' : update_data['remote_version'], - 'commits_behind' : commits_behind, - 'logs_result' : logs_result, - 'error_message' : error_msg } else: return {'response': {'result':'error', 'message':'Error: Received request without valid action'}} @socketio.on('post_app_data') def post_app_data(action=None, type=None, json_data=None): - settings = hack_read_settings() + global settings if json_data is not None: request = json.loads(json_data) @@ -3603,17 +3706,19 @@ def post_app_data(action=None, type=None, json_data=None): if type == 'settings': for key in request.keys(): if key in settings.keys(): - settings = _deep_dict_update(settings, request) - hack_write_settings(settings) + settings = deep_update(settings, request) + write_settings(settings) return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Key not found in settings'}} elif type == 'control': - control = hack_read_control() + control = read_control() for key in request.keys(): if key in control.keys(): - control = _deep_dict_update(control, request) - hack_write_control(control, origin='app-socketio') + ''' + Updating of control input data is now done in common.py > execute_commands() + ''' + write_control(request, origin='app-socketio') return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Key not found in control'}} @@ -3667,20 +3772,20 @@ def post_app_data(action=None, type=None, json_data=None): elif action == 'units_action': if type == 'f_units' and settings['globals']['units'] == 'C': settings = convert_settings_units('F', settings) - hack_write_settings(settings) - control = hack_read_control() + write_settings(settings) + control = read_control() control['updated'] = True control['units_change'] = True - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') write_log("Changed units to Fahrenheit") return {'response': {'result':'success'}} elif type == 'c_units' and settings['globals']['units'] == 'F': settings = convert_settings_units('C', settings) - hack_write_settings(settings) - control = hack_read_control() + write_settings(settings) + control = read_control() control['updated'] = True control['units_change'] = True - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') write_log("Changed units to Celsius") return {'response': {'result':'success'}} else: @@ -3692,7 +3797,7 @@ def post_app_data(action=None, type=None, json_data=None): device = request['onesignal_device']['onesignal_player_id'] if device in settings['onesignal']['devices']: settings['onesignal']['devices'].pop(device) - hack_write_settings(settings) + write_settings(settings) return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Device not specified'}} @@ -3709,17 +3814,17 @@ def post_app_data(action=None, type=None, json_data=None): pelletdb['current']['date_loaded'] = now pelletdb['current']['est_usage'] = 0 pelletdb['log'][now] = request['pellets_action']['profile'] - control = hack_read_control() + control = read_control() control['hopper_check'] = True - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') write_pellet_db(pelletdb) return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Profile not included in request'}} elif type == 'hopper_check': - control = hack_read_control() + control = read_control() control['hopper_check'] = True - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') return {'response': {'result':'success'}} elif type == 'edit_brands': if 'delete_brand' in request['pellets_action']: @@ -3761,9 +3866,9 @@ def post_app_data(action=None, type=None, json_data=None): 'comments' : request['pellets_action']['comments'] } if request['pellets_action']['add_and_load']: pelletdb['current']['pelletid'] = profile_id - control = hack_read_control() + control = read_control() control['hopper_check'] = True - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') now = str(datetime.datetime.now()) now = now[0:19] pelletdb['current']['date_loaded'] = now @@ -3812,9 +3917,12 @@ def post_app_data(action=None, type=None, json_data=None): return {'response': {'result':'error', 'message':'Error: Received request without valid type'}} elif action == 'timer_action': - control = hack_read_control() + control = read_control() + for index, notify_obj in enumerate(control['notify_data']): + if notify_obj['type'] == 'timer': + break if type == 'start_timer': - control['notify_req']['timer'] = True + control['notify_data'][index]['req'] = True if control['timer']['paused'] == 0: now = time.time() control['timer']['start'] = now @@ -3822,10 +3930,10 @@ def post_app_data(action=None, type=None, json_data=None): seconds = request['timer_action']['hours_range'] * 60 * 60 seconds = seconds + request['timer_action']['minutes_range'] * 60 control['timer']['end'] = now + seconds - control['notify_data']['timer_shutdown'] = request['timer_action']['timer_shutdown'] - control['notify_data']['timer_keep_warm'] = request['timer_action']['timer_keep_warm'] + control['notify_data'][index]['shutdown'] = request['timer_action']['timer_shutdown'] + control['notify_data'][index]['keep_warm'] = request['timer_action']['timer_keep_warm'] write_log('Timer started. Ends at: ' + epoch_to_time(control['timer']['end'])) - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Start time not specified'}} @@ -3834,111 +3942,30 @@ def post_app_data(action=None, type=None, json_data=None): control['timer']['end'] = (control['timer']['end'] - control['timer']['paused']) + now control['timer']['paused'] = 0 write_log('Timer unpaused. Ends at: ' + epoch_to_time(control['timer']['end'])) - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') return {'response': {'result':'success'}} elif type == 'pause_timer': - control['notify_req']['timer'] = False + control['notify_data'][index]['req'] = False now = time.time() control['timer']['paused'] = now write_log('Timer paused.') - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') return {'response': {'result':'success'}} elif type == 'stop_timer': - control['notify_req']['timer'] = False + control['notify_data'][index]['req'] = False control['timer']['start'] = 0 control['timer']['end'] = 0 control['timer']['paused'] = 0 - control['notify_data']['timer_shutdown'] = False - control['notify_data']['timer_keep_warm'] = False + control['notify_data'][index]['shutdown'] = False + control['notify_data'][index]['keep_warm'] = False write_log('Timer stopped.') - hack_write_control(control, origin='app-socketio') + write_control(control, origin='app-socketio') return {'response': {'result':'success'}} else: return {'response': {'result':'error', 'message':'Error: Received request without valid type'}} else: return {'response': {'result':'error', 'message':'Error: Received request without valid action'}} -@socketio.on('post_updater_data') -def updater_action(type='none', branch=None): - global settings - - if settings['globals']['venv']: - python_exec = 'bin/python' - else: - python_exec = 'python' - - if type == 'change_branch': - if branch is not None: - success, status, output = change_branch(branch) - message = f'Changing to {branch} branch \n' - if success: - dependencies = 'Installing any required dependencies \n' - message += dependencies - if install_dependencies() == 0: - message += output - restart_scripts() - return {'response': {'result':'success', 'message': message }} - else: - return {'response': {'result':'error', 'message':'Error: Dependencies were not installed properly'}} - else: - return {'response': {'result':'error', 'message':'Error: ' + output }} - else: - return {'response': {'result':'error', 'message':'Error: Branch not specified in request'}} - - elif type == 'do_update': - if branch is not None: - success, status, output = install_update() - message = f'Attempting update on {branch} \n' - if success: - dependencies = 'Installing any required dependencies \n' - message += dependencies - if install_dependencies() == 0: - message += output - restart_scripts() - return {'response': {'result':'success', 'message': message }} - else: - return {'response': {'result':'error', 'message':'Error: Dependencies were not installed properly'}} - else: - return {'response': {'result':'error', 'message':'Error: ' + output }} - else: - return {'response': {'result':'error', 'message':'Error: Branch not specified in request'}} - - elif type == 'update_remote_branches': - if is_raspberry_pi(): - os.system(f'{python_exec} updater.py -r') # Update branches from remote - #time.sleep(2) - return {'response': {'result':'success', 'message': 'Branches successfully updated from remote' }} - else: - return {'response': {'result':'error', 'message': 'System is not a Raspberry Pi. Branches not updated.' }} - else: - return {'response': {'result':'error', 'message':'Error: Received request without valid action'}} - -@socketio.on('post_restore_data') -def post_restore_data(type='none', filename='none', json_data=None): - - if type == 'settings': - if filename != 'none': - read_settings(filename=BACKUP_PATH+filename) - restart_scripts() - return {'response': {'result':'success'}} - elif json_data is not None: - write_settings(json.loads(json_data)) - return {'response': {'result':'success'}} - else: - return {'response': {'result':'error', 'message':'Error: Filename or JSON data not supplied'}} - - elif type == 'pelletdb': - if filename != 'none': - read_pellet_db(filename=BACKUP_PATH+filename) - return {'response': {'result':'success'}} - elif json_data is not None: - write_pellet_db(json.loads(json_data)) - return {'response': {'result':'success'}} - else: - return {'response': {'result':'error', 'message':'Error: Filename or JSON data not supplied'}} - else: - return {'response': {'result':'error', 'message':'Error: Received request without valid type'}} - ''' ============================================================================== Main Program Start @@ -3947,7 +3974,7 @@ def post_restore_data(type='none', filename='none', json_data=None): settings = read_settings(init=True) if __name__ == '__main__': - if is_raspberry_pi(): + if is_real_hardware(): socketio.run(app, host='0.0.0.0') else: socketio.run(app, host='0.0.0.0', debug=True) diff --git a/auto-install/install.sh b/auto-install/install.sh old mode 100755 new mode 100644 index 8751f1c4..4e385980 --- a/auto-install/install.sh +++ b/auto-install/install.sh @@ -39,7 +39,7 @@ r=$(( r < 20 ? 20 : r )) c=$(( c < 70 ? 70 : c )) # Display the welcome dialog -whiptail --msgbox --backtitle "Welcome" --title "PiFire Automated Installer" "This installer will transform your Raspberry Pi into a connected Smoker Controller. NOTE: This installer is intended to be run on a fresh install of Raspberry Pi OS Lite 32-Bit Bullseye or later." ${r} ${c} +whiptail --msgbox --backtitle "Welcome" --title "PiFire Automated Installer" "This installer will transform your Single Board Computer into a connected Smoker Controller. NOTE: This installer is intended to be run on a fresh install of Raspberry Pi OS Lite 32-Bit Bullseye or later." ${r} ${c} # Starting actual steps for installation clear @@ -134,6 +134,8 @@ python -m pip install scikit-fuzzy python -m pip install scikit-learn python -m pip install ratelimitingfilter python -m pip install "pillow>=9.2.0" +python -m pip install paho-mqtt +python -m pip install psutil # Setup config.txt to enable busses clear @@ -151,6 +153,15 @@ echo "i2c-dev" | $SUDO tee -a /etc/modules > /dev/null # Enable Hardware PWM - Needed for hardware PWM support echo "dtoverlay=pwm,pin=13,func=4" | $SUDO tee -a /boot/config.txt > /dev/null +# Setup backlight / power permissions if a DSI screen is installed +clear +echo "*************************************************************************" +echo "** **" +echo "** Configuring Backlight UDEV Rules **" +echo "** **" +echo "*************************************************************************" +echo 'SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"' | $SUDO tee -a /etc/udev/rules.d/backlight-permissions.rules > /dev/null + ### Setup nginx to proxy to gunicorn clear echo "*************************************************************************" diff --git a/build.xml b/build.xml deleted file mode 100644 index e0b57051..00000000 --- a/build.xml +++ /dev/null @@ -1,103 +0,0 @@ - - - Software for managing a pellet smoker - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/common/common.py b/common/common.py index 637010d7..59567969 100644 --- a/common/common.py +++ b/common/common.py @@ -24,7 +24,9 @@ import uuid import random import logging +from collections.abc import Mapping from ratelimitingfilter import RateLimitingFilter +from common.redis_queue import RedisQueue # ***************************************** # Constants and Globals @@ -93,9 +95,6 @@ def default_settings(): 'triggerlevel' : 'LOW', 'buttonslevel' : 'HIGH', 'disp_rotation' : 0, - 'shutdown_timer' : 60, - 'startup_timer' : 240, - 'auto_power_off' : False, 'dc_fan': False, 'standalone': True, 'units' : 'F', @@ -106,7 +105,8 @@ def default_settings(): 'boot_to_monitor' : False, # Set to True to boot directly into monitor mode 'prime_ignition' : False, # Set to True to enable the igniter in prime & startup mode 'updated_message' : False, # Set to True to display a pop-up message after the system has been updated - 'venv' : False # Set to True if running in virtual environment (needed for Raspberry Pi OS Bookworm) + 'venv' : False, # Set to True if running in virtual environment (needed for Raspberry Pi OS Bookworm) + 'real_hw' : True # Set to True if running on real hardware (i.e. Raspberry Pi), False if running in a test environment } if os.path.exists('bin'): @@ -153,11 +153,16 @@ def default_settings(): } settings['controller'] = { - 'selected' : 'pid', + 'selected' : 'pid' } settings['controller']['config'] = _default_controller_config() + settings['display'] = { + 'selected' : 'none' + } + settings['display']['config'] = _default_display_config() + settings['keep_warm'] = { 'temp' : 165, 's_plus' : False @@ -203,7 +208,8 @@ def default_settings(): 'minstartuptemp' : 75, # User Defined. Minimum temperature allowed for startup. 'maxstartuptemp' : 100, # User Defined. Take this value if the startup temp is higher than maxstartuptemp 'maxtemp' : 550, # User Defined. If temp exceeds value in any mode, shut off. (including monitor mode) - 'reigniteretries' : 1 # Number of tries to reignite grill if it has gone below the safe temp (0 to disable) + 'reigniteretries' : 1, # Number of tries to reignite grill if it has gone below the safe temp (0 to disable) + 'startup_check' : True # True = Enabled } settings['pelletlevel'] = { @@ -224,50 +230,70 @@ def default_settings(): 'time' : math.trunc(time.time()) } - settings['smartstart'] = { - 'enabled' : False, # Disable Smart Start by default on new installations - 'temp_range_list' : [60, 80, 90], # Min Temps for Each Profile - 'profiles' : [ - { - 'startuptime' : 360, - 'augerontime' : 15, - 'p_mode' : 0 - }, - { - 'startuptime' : 360, - 'augerontime' : 15, - 'p_mode' : 1 - }, - { - 'startuptime' : 240, - 'augerontime' : 15, - 'p_mode' : 3 - }, - { - 'startuptime' : 240, - 'augerontime' : 15, - 'p_mode' : 5 - } - ] + settings['startup'] = { + 'duration' : 240, # Default startup time (seconds) + 'prime_on_startup' : 0, # Prime Amount (grams) [0 = disabled] + 'startup_exit_temp' : 0, # Exit startup at this temperature threshold. [0 = disabled] + 'start_to_mode' : { + 'after_startup_mode' : 'Smoke', # Transition to this mode after startup completes + 'primary_setpoint' : 165 # If Hold, set the setpoint + }, + 'smartstart' : { + 'enabled' : False, # Disable Smart Start by default on new installations + 'exit_temp' : 120, # Exit temperature - exits smart start if this temperature is achieved + 'temp_range_list' : [60, 80, 90], # Min Temps for Each Profile + 'profiles' : [ + { + 'startuptime' : 360, + 'augerontime' : 15, + 'p_mode' : 0 + }, + { + 'startuptime' : 360, + 'augerontime' : 15, + 'p_mode' : 1 + }, + { + 'startuptime' : 240, + 'augerontime' : 15, + 'p_mode' : 3 + }, + { + 'startuptime' : 240, + 'augerontime' : 15, + 'p_mode' : 5 + } + ] + } } - settings['start_to_mode'] = { - 'after_startup_mode' : 'Smoke', - 'primary_setpoint' : 165 # If Hold, set the setpoint + settings['shutdown'] = { + 'shutdown_duration' : 240, # Default Shutdown time (seconds) + 'auto_power_off' : False # Power off the system after shutdown (False = disabled) } settings['dashboard'] = { 'current' : 'Default', - 'dashboards' : [ - { 'name' : 'Default', + 'dashboards' : { + 'Default' : { + 'name' : 'Default', 'friendly_name' : 'Default Dashboard', - 'html_name' : 'dash_default.html' - }, - { 'name' : 'Basic', + 'html_name' : 'dash_default.html', + 'custom' : { + 'hidden_cards' : [] + }, + 'config' : {} + }, + 'Basic' : { + 'name' : 'Basic', 'friendly_name' : 'Basic Dashboard', - 'html_name' : 'dash_basic.html' + 'html_name' : 'dash_basic.html', + 'custom' : { + 'hidden_cards' : [] + }, + 'config' : {} } - ] + } } settings['notify_services'] = default_notify_services() @@ -277,8 +303,9 @@ def default_settings(): 'clearhistoryonstart' : True, # Clear history when StartUp Mode selected 'autorefresh' : 'on', # Sets history graph to auto refresh ('live' graph) 'datapoints' : 60, # Number of data points to show on the history chart - 'probe_config' : default_probe_config(settings) + 'probe_config' : {} # Empty probe config } + settings['history_page']['probe_config'] = default_probe_config(settings) settings['recipe'] = {} settings['recipe']['probe_map'] = _default_recipe_probe_map(settings) @@ -295,6 +322,18 @@ def _default_controller_config(): return config +def _default_display_config(): + display_metadata = read_generic_json('./wizard/wizard_manifest.json') + display_metadata = display_metadata['modules']['display'] + + config = {} + for display in display_metadata: + config[display] = {} + for option in display_metadata[display]['config']: + config[display][option['option_name']] = option['default'] + + return config + def _default_recipe_probe_map(settings): recipe_probe_map = { 'primary' : '', @@ -315,21 +354,28 @@ def default_probe_config(settings): for probe in settings['probe_settings']['probe_map']['probe_info']: if probe['type'] in ['Primary', 'Food']: label = probe['label'] - probe_config[label] = { - 'name' : probe['name'], - 'type' : probe['type'], - 'enabled' : probe['enabled'], - 'line_color' : COLOR_LIST[color_index][0], - 'line_color_target' : COLOR_LIST[color_index][1], - 'dash_setpoint' : True, - 'bg_color' : COLOR_LIST[color_index][0], - 'bg_color_target' : COLOR_LIST[color_index][1], - 'fill' : False - } - if probe['type'] == 'Primary': - probe_config[label]['bg_color_setpoint'] = COLOR_LIST[color_index][0] - probe_config[label]['line_color_setpoint'] = COLOR_LIST[color_index][0] - color_index += 1 + # Check if the label exists in settings already. + if label in settings['history_page']['probe_config'].keys(): + probe_config[label] = settings['history_page']['probe_config'][label] + else: + probe_config[label] = { + 'name' : probe['name'], + 'type' : probe['type'], + 'enabled' : probe['enabled'], + 'line_color' : COLOR_LIST[color_index][0], + 'line_color_target' : COLOR_LIST[color_index][1], + 'dash_setpoint' : True, + 'bg_color' : COLOR_LIST[color_index][0], + 'bg_color_target' : COLOR_LIST[color_index][1], + 'fill' : False + } + if probe['type'] == 'Primary': + probe_config[label]['bg_color_setpoint'] = COLOR_LIST[color_index][0] + probe_config[label]['line_color_setpoint'] = COLOR_LIST[color_index][0] + color_index += 1 + # If color index has gotten to the end of the COLOR_LIST, loop back to zero + if color_index >= len(COLOR_LIST): + color_index = 0 return probe_config def default_notify_services(): @@ -372,6 +418,17 @@ def default_notify_services(): 'org': '', 'bucket': '' } + + services['mqtt'] = { + "broker": "homeassistant.local", + "enabled": False, + "homeassistant_autodiscovery_topic": "homeassistant", + "id": "PiFire", + "password": "", + "port": "1883", + "update_sec": "30", + "username": "" + } return services @@ -449,6 +506,10 @@ def default_control(): control['prime_amount'] = 10 # Default Prime Amount in Grams + control['startup_timestamp'] = 0 # Timestamp of startup, used for cook time + + control['system'] = {} + return(control) def default_notify(settings): @@ -465,7 +526,8 @@ def default_notify(settings): 'name' : probe[1], 'type' : 'probe', 'req' : False, - 'target' : 0, + 'target' : 0, + 'eta' : None, 'shutdown' : False, 'keep_warm' : False, } @@ -492,6 +554,16 @@ def default_notify(settings): } notify_data.append(notify_info) + ''' Add TEST notification data to list ''' + notify_info = { + 'label' : 'Test', + 'type' : 'test', + 'req' : False, + 'shutdown' : False, + 'keep_warm' : False, + } + notify_data.append(notify_info) + return notify_data def get_probe_list(settings): @@ -698,6 +770,9 @@ def read_control(flush=False): # Remove all control structures in Redis DB (not history or current) cmdsts.delete('control:general') cmdsts.delete('control:command') + cmdsts.delete('control:write') + cmdsts.delete('control:systemq') + cmdsts.delete('control:systemo') # The following set's no persistence so that we don't get writes to the disk / SDCard cmdsts.config_set('appendonly', 'no') cmdsts.config_set('save', '') @@ -723,13 +798,13 @@ def write_control(control, direct_write=False, origin='unknown'): if direct_write: cmdsts.set('control:general', json.dumps(control)) else: + # Add changes to control write queue control['origin'] = origin - cmdsts.rpush('control:command', json.dumps(control)) - #print(f' -> Command Pushed to Queue by {origin}') + cmdsts.rpush('control:write', json.dumps(control)) -def execute_commands(): +def execute_control_writes(): """ - Execute Control Commands in Queue from Redis DB + Execute Control Writes in Queue from Redis DB :param None @@ -738,24 +813,12 @@ def execute_commands(): global cmdsts status = 'OK' - while cmdsts.llen('control:command') > 0: + while cmdsts.llen('control:write') > 0: control = read_control() - command = json.loads(cmdsts.lpop('control:command')) + command = json.loads(cmdsts.lpop('control:write')) command.pop('origin') - for key in control.keys(): - if key in command.keys(): - if key in ['safety', 'timer', 'manual', 'smart_start']: - control[key].update(command.get(key, {})) - elif key in ['recipe']: - for subkey in control[key].keys(): - if subkey in command[key].keys(): - if subkey in ['step_data']: - control[key][subkey].update(command[key].get(subkey, {})) - else: - control[key][subkey] = command[key][subkey] - else: - control[key] = command[key] - write_control(control, direct_write=True, origin='executor') + control = deep_update(control, command) + write_control(control, direct_write=True, origin='writer') return status def read_errors(flush=False): @@ -946,24 +1009,22 @@ def read_settings(filename='settings.json', init=False, retry_count=0): ''' Downgrade Path ''' backup_settings() # Backup Old Settings Before Performing Downgrade settings = downgrade_settings(settings, settings_default) - update_settings = True + update_settings = True + elif (settings_default['versions']['server'] == settings['versions']['server']) and (settings['versions']['build'] < settings_default['versions']['build']): + ''' Minor Upgrade Path ''' + prev_ver = semantic_ver_to_list(settings['versions']['server']) + settings = upgrade_settings(prev_ver, settings, settings_default) + settings['versions'] = settings_default['versions'] + update_settings = True if settings['versions'].get('build', None) != settings_default['versions']['build']: settings['versions']['build'] = settings_default['versions']['build'] update_settings = True # Overlay the original settings on top of the default settings - for key in settings_default.keys(): - if key in settings.keys(): - for subkey in settings_default[key].keys(): - if subkey not in settings[key].keys(): - update_settings = True - settings_default[key].update(settings.get(key, {})) - else: - update_settings = True - - # Move all changes to the settings variable - settings = settings_default + settings = deep_update(settings_default, settings) + update_settings = True + settings['history_page']['probe_config'] = default_probe_config(settings) # Fix issue with probe_configs resetting to defaults if update_settings or filename != 'settings.json': # If any of the keys were added, then write back the changes write_settings(settings) @@ -1034,7 +1095,7 @@ def upgrade_settings(prev_ver, settings, settings_default): if prev_ver[0] <=1 and prev_ver[1] <= 4: settings['versions'] = settings_default['versions'] settings['globals']['first_time_setup'] = True # Force configuration for probes - settings['start_to_mode']['primary_setpoint'] = settings['start_to_mode']['grill1_setpoint'] + settings['startup']['start_to_mode']['primary_setpoint'] = settings['start_to_mode']['grill1_setpoint'] settings['start_to_mode'].pop('grill1_setpoint') settings['dashboard'] = settings_default['dashboard'] # Move Notification Settings @@ -1055,6 +1116,27 @@ def upgrade_settings(prev_ver, settings, settings_default): settings['cycle_data'].pop('SmokeCycleTime') # Remove old SmokeCycleTime settings['cycle_data']['SmokeOnCycleTime'] = 15 # Name change for SmokeCycleTime variable settings['cycle_data']['SmokeOffCycleTime'] = 45 # Added SmokeOffCycleTime variable + ''' Check if upgrading from v1.6.x or v1.7.0 build 7 ''' + if (prev_ver[0] <=1 and prev_ver[1] <= 6) or (prev_ver[0] ==1 and prev_ver[1] == 7 and settings['versions'].get('build', 0) <= 7): + settings['dashboard'] = settings_default['dashboard'] + ''' Check if upgrading from v1.7.0 build 45 ''' + if (prev_ver[0] <=1 and prev_ver[1] <= 6) or (prev_ver[0] ==1 and prev_ver[1] == 7 and settings['versions'].get('build', 0) <= 45): + # Move startup defaults to new 'startup' section of settings + settings['startup'] = settings_default['startup'] + settings['startup']['duration'] = settings['globals'].get('startup_timer', settings_default['startup']['duration']) + settings['globals'].pop('startup_timer', None) + settings['startup']['startup_exit_temp'] = settings['globals'].get('startup_exit_temp', settings_default['startup']['startup_exit_temp']) + settings['globals'].pop('startup_exit_temp', None) + settings['startup']['start_to_mode'] = settings.get('start_to_mode', settings_default['startup']['start_to_mode']) + settings.pop('start_to_mode', None) + settings['startup']['smartstart'] = settings.get('smartstart', settings_default['startup']['smartstart']) + settings.pop('smartstart', None) + settings['shutdown'] = settings_default['shutdown'] + settings['shutdown']['shutdown_duration'] = settings['globals'].get('shutdown_timer', settings_default['shutdown']['shutdown_duration']) + settings['globals'].pop('shutdown_timer', None) + settings['shutdown']['auto_power_off'] = settings['globals'].get('auto_power_off', settings_default['shutdown']['auto_power_off']) + settings['globals'].pop('auto_power_off', None) + ''' Import any new probe profiles ''' for profile in list(settings_default['probe_settings']['probe_profiles'].keys()): if profile not in list(settings['probe_settings']['probe_profiles'].keys()): @@ -1365,8 +1447,10 @@ def write_current(in_data): current = {} current['P'] = in_data['probe_history']['primary'] current['F'] = in_data['probe_history']['food'] + current['AUX'] = in_data['probe_history']['aux'] current['PSP'] = in_data['primary_setpoint'] current['NT'] = in_data['notify_targets'] + current['TS'] = int(time.time() * 1000) # Timestamp cmdsts.set('control:current', json.dumps(current)) def read_current(zero_out=False): @@ -1385,7 +1469,8 @@ def read_current(zero_out=False): 'P' : {}, 'F' : {}, 'PSP' : 0, - 'NT' : {} + 'NT' : {}, + 'AUX' : {} } for probe in settings['probe_settings']['probe_map']['probe_info']: @@ -1393,6 +1478,8 @@ def read_current(zero_out=False): current['P'][probe['label']] = 0 if probe['type'] == 'Food': current['F'][probe['label']] = 0 + if probe['type'] == 'Aux': + current['AUX'][probe['label']] = 0 current['NT'][probe['label']] = 0 cmdsts.set('control:current', json.dumps(current)) @@ -1427,6 +1514,29 @@ def read_tr(): return(tr_data) +def write_autotune(data): + global cmdsts + # Push data string to the list in the last position + cmdsts.rpush('control:autotune', json.dumps(data)) + +def read_autotune(flush=False, size_only=False): + global cmdsts + + output_data = [] + # If a flushhistory is requested, then flush the control:history key (and data) + if flush: + if cmdsts.exists('control:autotune'): + cmdsts.delete('control:autotune') + elif size_only: + size = cmdsts.llen('control:autotune') + return size + elif cmdsts.exists('control:autotune'): + autotune_data = cmdsts.lrange('control:autotune', 0, -1) + for datapoint in autotune_data: + output_data.append(json.loads(datapoint)) + + return output_data + def prepare_csv(data=[], filename=''): # Create filename if no name specified if(filename == ''): @@ -1514,42 +1624,50 @@ def convert_settings_units(units, settings): :return: Updated Settings """ settings['globals']['units'] = units + settings['startup']['startup_exit_temp'] = convert_temp(units, settings['startup']['startup_exit_temp']) settings['safety']['maxstartuptemp'] = convert_temp(units, settings['safety']['maxstartuptemp']) settings['safety']['maxtemp'] = convert_temp(units, settings['safety']['maxtemp']) settings['safety']['minstartuptemp'] = convert_temp(units, settings['safety']['minstartuptemp']) settings['smoke_plus']['max_temp'] = convert_temp(units, settings['smoke_plus']['max_temp']) settings['smoke_plus']['min_temp'] = convert_temp(units, settings['smoke_plus']['min_temp']) settings['keep_warm']['temp'] = convert_temp(units, settings['keep_warm']['temp']) - for temp in range(0, len(settings['smartstart']['temp_range_list'])): - settings['smartstart']['temp_range_list'][temp] = convert_temp( - units, settings['smartstart']['temp_range_list'][temp]) + for temp in range(0, len(settings['startup']['smartstart']['temp_range_list'])): + settings['startup']['smartstart']['temp_range_list'][temp] = convert_temp( + units, settings['startup']['smartstart']['temp_range_list'][temp]) + settings['startup']['smartstart']['exit_temp'] = convert_temp(units, settings['startup']['smartstart']['exit_temp']) return(settings) -# ************************************** -# is_raspberrypi() function borrowed from user https://raspberrypi.stackexchange.com/users/126953/chris -# in post: https://raspberrypi.stackexchange.com/questions/5100/detect-that-a-python-program-is-running-on-the-pi -# ************************************** -def is_raspberry_pi(): +def is_real_hardware(settings=None): """ - Check if device is a Raspberry Pi + Check if running on real hardware as opposed to a prototype/test environment. - :return: True if Raspberry Pi. False otherwise + :return: True if running on real hardware (i.e. Raspberry Pi), else False. """ - try: - with io.open('/sys/firmware/devicetree/base/model', 'r') as m: - if 'raspberry pi' in m.read().lower(): return True - except Exception: - pass - return False + if settings == None: + settings = read_settings() + + return True if settings['globals']['real_hw'] else False def restart_scripts(): """ Restart the Control and WebApp Scripts """ - print('[DEBUG MSG] Restarting Scripts... ') - command = "sleep 3 && sudo service supervisor restart &" - if is_raspberry_pi(): - os.system(command) + if is_real_hardware(): + os.system("sleep 3 && sudo service supervisor restart &") + +def reboot_system(): + """ + Reboot the system + """ + if is_real_hardware(): + os.system("sleep 3 && sudo reboot &") + +def shutdown_system(): + """ + Shutdown the system + """ + if is_real_hardware(): + os.system("sleep 3 && sudo shutdown -h now &") def read_wizard(filename='wizard/wizard_manifest.json'): """ @@ -1810,6 +1928,7 @@ def read_status(init=False): "units": "F", "mode": "Stop", "recipe": False, + "startup_timestamp" : 0, "start_time": 0, "start_duration": 0, "shutdown_duration": 0, @@ -1830,4 +1949,626 @@ def read_status(init=False): else: status = json.loads(cmdsts.get('control:status')) - return status \ No newline at end of file + return status + +def get_probe_info(probe_info): + ''' Create a structure with probe information for the display to use. ''' + probe_structure = { + 'primary' : {}, + 'food' : [] + } + for probe in probe_info: + if probe['type'] == 'Primary': + probe_structure['primary']['name'] = probe['name'] + probe_structure['primary']['label'] = probe['label'] + elif probe['type'] == 'Food': + food_probe = { + 'name' : probe['name'], + 'label' : probe['label'] + } + probe_structure['food'].append(food_probe) + + return probe_structure + +# Borrowed from: https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth +# Attributed to Alex Martelli and Alex Telon +def deep_update(dictionary, updates): + for key, value in updates.items(): + if isinstance(value, Mapping): + dictionary[key] = deep_update(dictionary.get(key, {}), value) + else: + dictionary[key] = value + return dictionary + +MODE_MAP = { + 'startup' : 'Startup', + 'smoke' : 'Smoke', + 'shutdown' : 'Shutdown', + 'stop' : 'Stop', + 'reignite' : 'Reignite', + 'monitor' : 'Monitor', + 'error' : 'Error', + 'prime' : 'Prime', + 'hold' : 'Hold', + 'manual' : 'Manual' +} + +# Borrowed from: https://pythonhow.com/how/check-if-a-string-is-a-float/ +# Attributed to Python How +# Slightly modified to check if string is None +def is_float(string): + if string is not None: + if string.replace(".", "").isnumeric(): + return True + return False + +def process_command(action=None, arglist=[], origin='unknown', direct_write=False): + ''' + Process incoming command from API or elsewhere + ''' + data = {} + data['result'] = 'OK' + data['message'] = 'Command was accepted successfully.' + data['data'] = {} + + control = read_control() + settings = read_settings() + + ''' Populate any empty args with None just in case ''' + num_args = len(arglist) + max_args = 4 # Needs updating if API adds deeper number of arguments + + for _ in range(max_args - num_args): + arglist.append(None) + + if action == 'get': + ''' GET Commands ''' + + if arglist[0] == 'temp': + ''' + Get Temperature + /api/get/temp/{probe label} + + + Returns: + { + 'temp' : + 'result' : 'OK' + } + ''' + current_temps = read_current() + + if arglist[1] in current_temps['P'].keys(): + data['data']['temp'] = current_temps['P'][arglist[1]] + elif arglist[1] in current_temps['F'].keys(): + data['data']['temp'] = current_temps['F'][arglist[1]] + elif arglist[1] in current_temps['AUX'].keys(): + data['data']['temp'] = current_temps['AUX'][arglist[1]] + else: + data['result'] = 'ERROR' + data['message'] = f'Probe {arglist[1]} not found or not specified.' + + elif arglist[0] == 'current': + ''' + Get Current Temp Data Structure + /api/get/current + + Returns (Example): + { + "AUX": {}, + "F": { + "Probe1": 204, + "Probe2": 206 + }, + "NT": { + "Grill": 0, + "Probe1": 0, + "Probe2": 0 + }, + "P": { + "Grill": 518 + }, + "PSP": 0, + "TS": 1707345482984 + } + ''' + current_temps = read_current() + + data['data'] = current_temps + + elif arglist[0] == 'mode': + ''' + Get Current Mode + /api/get/mode + + Returns: + { + 'mode' : + } + ''' + data['data']['mode'] = control['mode'] + + elif arglist[0] == 'hopper': + ''' + Get Hopper Level + /api/get/hopper + + Returns: + { + 'hopper' : + } + ''' + control['hopper_check'] = True + write_control(control, direct_write=direct_write, origin=origin) + time.sleep(3) + pelletdb = read_pellet_db() + data['data']['hopper'] = pelletdb['current']['hopper_level'] + + elif arglist[0] == 'timer': + ''' + Get Timer Data + /api/get/timer + + Returns: + { + 'start' : control['timer']['start'], + 'paused' : control['timer']['paused'], + 'end' : control['timer']['end'], + 'shutdown' : control['notify_data'][]['shutdown'], + 'keep_warm' : control['notify_data'][]['keep_warm'], + } + ''' + data['data']['start'] = control['timer']['start'] + data['data']['paused'] = control['timer']['paused'] + data['data']['end'] = control['timer']['end'] + ''' Get index of timer object ''' + for index, notify_obj in enumerate(control['notify_data']): + if notify_obj['type'] == 'timer': + break + data['data']['shutdown'] = control['notify_data'][index]['shutdown'] + data['data']['keep_warm'] = control['notify_data'][index]['keep_warm'] + + elif arglist[0] == 'notify': + ''' + Get Notify Data + /api/get/notify + + Returns: + [ + { + "eta": null, + "keep_warm": false, + "label": "Grill", + "name": "GrillMain", + "req": false, + "shutdown": false, + "target": 0, + "type": "probe" + }, + ... + { + "keep_warm": false, + "label": "Hopper", + "last_check": 0, + "req": true, + "shutdown": false, + "type": "hopper" + } + ] + ''' + data['data'] = control['notify_data'] + + elif arglist[0] == 'status': + ''' + Get Status Information for Key Items + /api/get/status + + Returns (Example): + { + "display_mode": "Stop", + "lid_open_detected": false, + "lid_open_endtime": 0, + "mode": "Stop", + "name": "Development", + "outpins": { + "auger": false, + "fan": false, + "igniter": false, + "power": false + }, + "p_mode": 0, + "prime_amount": 0, + "prime_duration": 0, + "s_plus": false, + "shutdown_duration": 10, + "start_duration": 30, + "start_time": 0, + "startup_timestamp": 0, + "status": "", + "ui_hash": 5734093427135650890, + "units": "F" + } + ''' + status = read_status() + + data['data']['mode'] = control['mode'] + data['data']['display_mode'] = status['mode'] + data['data']['status'] = control['status'] + data['data']['s_plus'] = control['s_plus'] + data['data']['units'] = settings['globals']['units'] + data['data']['name'] = settings['globals']['grill_name'] + data['data']['start_time'] = status['start_time'] + data['data']['start_duration'] = status['start_duration'] + data['data']['shutdown_duration'] = status['shutdown_duration'] + data['data']['prime_duration'] = status['prime_duration'] + data['data']['prime_amount'] = status['prime_amount'] + data['data']['lid_open_detected'] = status['lid_open_detected'] + data['data']['lid_open_endtime'] = status['lid_open_endtime'] + data['data']['p_mode'] = status['p_mode'] + data['data']['outpins'] = status['outpins'] + data['data']['startup_timestamp'] = status['startup_timestamp'] + data['data']['ui_hash'] = hash(json.dumps(settings['probe_settings']['probe_map']['probe_info'])) + + else: + data['result'] = 'ERROR' + data['message'] = f'Get API Argument: [{arglist[0]}] not recognized.' + + elif action == 'set': + ''' SET Commands ''' + + if arglist[0] == 'psp': + ''' + Primary Setpoint + /api/set/psp/{integer/float temperature} + ''' + if is_float(arglist[1]): + control['mode'] = 'Hold' + if settings['globals']['units'] == 'F': + control['primary_setpoint'] = int(float(arglist[1])) + else: + control['primary_setpoint'] = float(arglist[1]) + control['updated'] = True + write_control(control, direct_write=direct_write, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Primary set point should be an integer or float in degrees {settings["globals"]["units"]}' + + elif arglist[0] == 'mode': + ''' + Mode + /api/set/mode/{mode} where mode = 'startup', 'smoke', 'shutdown', 'stop', 'reignite', 'monitor', 'error' + /api/set/mode/prime/{prime amount in grams}[/{next mode}] + /api/set/mode/hold/{integer/float temperature} + ''' + if arglist[1] in ['startup', 'smoke', 'shutdown', 'stop', 'reignite', 'monitor', 'error', 'manual']: + control['mode'] = MODE_MAP[arglist[1]] + control['updated'] = True + write_control(control, direct_write=direct_write, origin=origin) + elif arglist[1] == 'prime': + try: + if arglist[2] is not None: + if arglist[2].isdigit(): + control['mode'] = MODE_MAP[arglist[1]] + control['prime_amount'] = int(arglist[2]) + control['updated'] = True + if arglist[3] in ['startup', 'monitor']: + control['next_mode'] = MODE_MAP[arglist[3]] + else: + control['next_mode'] = 'Stop' + write_control(control, direct_write=direct_write, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Prime amount should be an integer in grams.' + else: + data['result'] = 'ERROR' + data['message'] = f'Prime amount not specified.' + except: + data['result'] = 'ERROR' + data['message'] = f'Set Mode {arglist[1]} with {arglist[2]} caused an exception.' + elif arglist[1] == 'hold': + if arglist[2] is not None: + if is_float(arglist[2]): + control['mode'] = MODE_MAP[arglist[1]] + if settings['globals']['units'] == 'F': + control['primary_setpoint'] = int(float(arglist[2])) + else: + control['primary_setpoint'] = float(arglist[2]) + control['updated'] = True + write_control(control, direct_write=direct_write, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Set Mode {arglist[1]} with {arglist[2]} failed [not a number].' + else: + data['result'] = 'ERROR' + data['message'] = f'Set Mode {arglist[1]} with {arglist[2]} failed [no hold temp specified].' + else: + data['result'] = 'ERROR' + data['message'] = f'Get API Argument: {arglist[2]} not recognized.' + + elif arglist[0] == 'pmode': + ''' + PMode + /api/set/pmode/{pmode value} where pmode value is between 0-9 + ''' + if arglist[1] is not None: + if arglist[1].isdigit(): + if int(arglist[1]) >= 0 and int(arglist[1]) < 10: + settings['cycle_data']['PMode'] = int(arglist[1]) + write_settings(settings) + control['settings_update'] = True + write_control(control, direct_write=False, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Set PMode out of range(0-9): {arglist[1]}' + else: + data['result'] = 'ERROR' + data['message'] = f'Set PMode invalid value.' + else: + data['result'] = 'ERROR' + data['message'] = f'Set PMode invalid arguments.' + + elif arglist[0] == 'splus': + ''' + Smoke Plus + /api/set/splus/{true/false} + ''' + if arglist[1] == 'true': + control['s_plus'] = True + else: + control['s_plus'] = False + write_control(control, direct_write=direct_write, origin=origin) + + elif arglist[0] == 'notify': + ''' + Notify Settings + /api/set/notify/{object}/ where object = probe label, 'Timer', 'Hopper' + + /api/set/notify/{object}/req/{true/false} + /api/set/notify/{object}/target/{value} (not valid for Timer or Hopper) + /api/set/notify/{object}/shutdown/{true/false} + /api/set/notify/{object}/keep_warm/{true/false} + ''' + + if arglist[1] is not None: + found = False + for index, object in enumerate(control['notify_data']): + if object['label'] == arglist[1]: + print('FOUND') + found = True + if arglist[2] in ['req', 'shutdown', 'keep_warm']: + if arglist[3] == 'true': + control['notify_data'][index][arglist[2]] = True + else: + control['notify_data'][index][arglist[2]] = False + elif arglist[2] == 'target' and arglist[1] not in ['Timer', 'Hopper']: + if is_float(arglist[3]): + if settings['globals']['units'] == 'F': + control['notify_data'][index]['target'] = int(float(arglist[3])) + else: + control['primary_setpoint'] = float(arglist[3]) + else: + data['result'] = 'ERROR' + data['message'] = f'Notify object target value invalid or missing.' + else: + data['result'] = 'ERROR' + data['message'] = f'Notify object update failed.' + break + if not found: + data['result'] = 'ERROR' + data['message'] = f'Notify object label {arglist[1]} was not found.' + else: + write_control(control, direct_write=False, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Notify object label was not specified.' + + elif arglist[0] == 'pwm': + ''' + PWM Control + + /api/set/pwm/{true/false} + ''' + if arglist[1] == 'true': + control['pwm_control'] = True + else: + control['pwm_control'] = False + write_control(control, direct_write=direct_write, origin=origin) + + elif arglist[0] == 'duty_cycle': + ''' + Duty Cycle + + /api/set/duty_cycle/{0-100 percent} + ''' + if is_float(arglist[1]): + duty_cycle = int(arglist[1]) + if duty_cycle >= 0 and duty_cycle <= 100: + control['duty_cycle'] = duty_cycle + write_control(control, direct_write=False, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Duty cycle must be an integer between 0-100.' + else: + data['result'] = 'ERROR' + data['message'] = f'Duty cycle must be specified as an integer between 0-100 percent.' + + elif arglist[0] == 'tuning_mode': + ''' + Tuning Mode Enable + + /api/set/tuning_mode/{true/false} + ''' + if arglist[1] == 'true': + control['tuning_mode'] = True + else: + control['tuning_mode'] = False + write_control(control, direct_write=direct_write, origin=origin) + + elif arglist[0] == 'timer': + ''' + Timer Control + + /api/set/timer/start/{seconds} + /api/set/timer/pause + /api/set/timer/stop + /api/set/timer/shutdown/{true/false} + /api/set/timer/keep_warm/{true/false} + ''' + + ''' Get index of timer object ''' + for index, notify_obj in enumerate(control['notify_data']): + if notify_obj['type'] == 'timer': + break + ''' Get timestamp ''' + now = time.time() + + if arglist[1] == 'start': + control['notify_data'][index]['req'] = True + # If starting new timer + if control['timer']['paused'] == 0: + control['timer']['start'] = now + if is_float(arglist[2]): + seconds = int(float(arglist[2])) + control['timer']['end'] = now + seconds + else: + control['timer']['end'] = now + 60 + write_log('Timer started. Ends at: ' + epoch_to_time(control['timer']['end'])) + write_control(control, direct_write=direct_write, origin='app') + else: # If Timer was paused, restart with new end time. + control['timer']['end'] = (control['timer']['end'] - control['timer']['paused']) + now + control['timer']['paused'] = 0 + write_log('Timer unpaused. Ends at: ' + epoch_to_time(control['timer']['end'])) + write_control(control, direct_write=direct_write, origin='app') + elif arglist[1] == 'pause': + if control['timer']['start'] != 0: + control['notify_data'][index]['req'] = False + control['timer']['paused'] = now + write_log('Timer paused.') + write_control(control, direct_write=direct_write, origin='app') + else: + control['notify_data'][index]['req'] = False + control['timer']['start'] = 0 + control['timer']['end'] = 0 + control['timer']['paused'] = 0 + control['notify_data'][index]['shutdown'] = False + control['notify_data'][index]['keep_warm'] = False + write_log('Timer cleared.') + write_control(control, direct_write=direct_write, origin='app') + elif arglist[1] == 'stop': + control['notify_data'][index]['req'] = False + control['timer']['start'] = 0 + control['timer']['end'] = 0 + control['timer']['paused'] = 0 + control['notify_data'][index]['shutdown'] = False + control['notify_data'][index]['keep_warm'] = False + write_log('Timer stopped.') + write_control(control, direct_write=direct_write, origin='app') + elif arglist[1] == 'shutdown': + if arglist[2] == 'true': + control['notify_data'][index]['shutdown'] = True + else: + control['notify_data'][index]['shutdown'] = False + write_control(control, direct_write=direct_write, origin=origin) + elif arglist[1] == 'keep_warm': + if arglist[2] == 'true': + control['notify_data'][index]['keep_warm'] = True + else: + control['notify_data'][index]['keep_warm'] = False + write_control(control, direct_write=direct_write, origin=origin) + else: + data['result'] = 'ERROR' + data['message'] = f'Timer command not recognized.' + + elif arglist[0] == 'manual': + ''' + Manual Control + Note: Must already be in Manual mode (see set/mode command) + /api/set/manual/power/{true/false} + /api/set/manual/igniter/{true/false} + /api/set/manual/fan/{true/false} + /api/set/manual/auger/{true/false} + /api/set/manual/pwm/{speed} + ''' + + if control['mode'] == 'Manual': + if arglist[1] == 'power': + control['manual']['change'] = True + if arglist[2] == 'true': + control['manual']['power'] = True + else: + control['manual']['power'] = False + elif arglist[1] == 'igniter': + control['manual']['change'] = True + if arglist[2] == 'true': + control['manual']['igniter'] = True + else: + control['manual']['igniter'] = False + elif arglist[1] == 'fan': + control['manual']['change'] = True + if arglist[2] == 'true': + control['manual']['fan'] = True + else: + control['manual']['fan'] = False + control['manual']['pwm'] = 100 + elif arglist[1] == 'auger': + control['manual']['change'] = True + if arglist[2] == 'true': + control['manual']['auger'] = True + else: + control['manual']['auger'] = False + elif arglist[1] == 'pwm' and is_float(arglist[2]): + control['manual']['change'] = True + control['manual']['pwm'] = int(float(arglist[2])) + else: + data['result'] = 'ERROR' + data['message'] = f'Manual command not recognized or contained an error.' + if control['manual']['change']: + write_control(control, direct_write=direct_write, origin=origin) + + else: + data['result'] = 'ERROR' + data['message'] = f'Before changing manual outputs, system must be put into Manual mode.' + + else: + data['result'] = 'ERROR' + data['message'] = f'Set API Argument: {arglist[0]} not recognized.' + + elif action == 'cmd': + ''' System CMD Commands ''' + + if arglist[0] == 'restart': + ''' + Restart Scripts + /api/cmd/restart + ''' + restart_scripts() + + elif arglist[0] == 'reboot': + ''' + Reboot System + /api/cmd/reboot + ''' + reboot_system() + + elif arglist[0] == 'shutdown': + ''' + Shutdown System + /api/cmd/shutdown + ''' + shutdown_system() + + else: + data['result'] = 'ERROR' + data['message'] = f'CMD API Argument: {arglist[0]} not recognized.' + + elif action == 'sys': + ''' System Control Commands ''' + + system_command_queue = RedisQueue('control:systemq') + system_command_queue.push(arglist) + + else: + data['result'] = 'ERROR' + data['message'] = f'Action [{action}] not valid/recognized.' + + return data \ No newline at end of file diff --git a/common/hacks.py b/common/hacks.py deleted file mode 100644 index 99f21f07..00000000 --- a/common/hacks.py +++ /dev/null @@ -1,313 +0,0 @@ -''' -Hacks to convert certain APIs / Formats to PiFire v1.3.5 format to maintain compatibility with the current Android App -''' -import datetime -from common import read_control, write_control, read_settings, write_settings, read_history, read_current, unpack_history - -def hack_read_settings(): - settings = read_settings() - # Move notification settings out of ['notify_services'] - notify_services = settings['notify_services'] - for service in notify_services: - settings[service] = notify_services[service] - settings.pop('notify_services') - - # Add 'grill_probe_settings' - settings['grill_probe_settings'] = { - "grill_probe" : "grill_probe1", - "grill_probe_enabled" : [1, 0, 0], - "grill_probes": { - "grill_probe1": { - "name": "Grill Probe 1" - }, - "grill_probe2": { - "name": "Grill Probe 2" - }, - "grill_probe3": { - "name": "Avg Grill Probes" - } - } - } - - # Add items to 'probe_settings' - settings['probe_settings']['probe_options'] = [ - "ADC0", - "ADC1", - "ADC2", - "ADC3" - ] - settings['probe_settings']['probe_sources'] = [ - "ADC0", - "ADC1", - "ADC2", - "ADC3" - ] - settings['probe_settings']['probes_enabled'] = [ - 1, - 1, - 1 - ] - - # Add 'probe_types' (Copy from probe_settings) - settings['probe_types'] = { - "grill1type": "PT-1000-OEM", - "grill2type": "TWPS00", - "probe1type": "TWPS00", - "probe2type": "TWPS00" - } - - current = read_current() - grill1_key = list(current['P'].keys())[0] - - probe1_key = '' - if len(list(current['F'].keys())) > 0: - probe1_key = list(current['F'].keys())[0] - - probe2_key = '' - if len(list(current['F'].keys())) > 1: - probe2_key = list(current['F'].keys())[1] - - grill2_key = '' - if len(list(current['F'].keys())) > 2: - grill2_key = list(current['F'].keys())[2] - - for item in settings['probe_settings']['probe_map']['probe_info']: - if item['label'] == grill1_key: - settings['probe_types']['grill1type'] = item['profile']['id'] - if item['label'] == probe1_key: - settings['probe_types']['probe1type'] = item['profile']['id'] - if item['label'] == probe2_key: - settings['probe_types']['probe2type'] = item['profile']['id'] - if item['label'] == grill2_key: - settings['probe_types']['grill2type'] = item['profile']['id'] - - return settings - -def hack_write_settings(settings): - # Move notification settings into ['notify_services'] - settings['notify_services'] = {} - for key in ['apprise', 'ifttt', 'influxdb', 'onesignal', 'pushbullet', 'pushover']: - settings['notify_services'][key] = settings[key] - settings.pop(key) - - # Remove 'grill_probe_settings' - settings.pop('grill_probe_settings') - - # Remove 'probe_settings' items - settings['probe_settings'].pop('probe_options') - settings['probe_settings'].pop('probe_sources') - settings['probe_settings'].pop('probes_enabled') - - # Remove 'probe_types' item - settings.pop('probe_types') - - write_settings(settings) - -def hack_read_control(): - control = read_control() - current = read_current() - grill1_key = list(current['P'].keys())[0] - - probe1_key = '' - if len(list(current['F'].keys())) > 0: - probe1_key = list(current['F'].keys())[0] - - probe2_key = '' - if len(list(current['F'].keys())) > 1: - probe2_key = list(current['F'].keys())[1] - - grill2_key = '' - if len(list(current['F'].keys())) > 2: - grill2_key = list(current['F'].keys())[2] - - notify_data = control['notify_data'].copy() - - control['setpoints'] = { - 'grill' : control['primary_setpoint'], - 'probe1' : 0, - 'probe2' : 0, - 'grill_notify' : 0 - } - control['notify_req'] = { - 'grill' : False, - 'probe1' : False, - 'probe2' : False, - 'timer' : False - } - control['notify_data'] = { - 'hopper_low' : False, - 'p1_shutdown' : False, - 'p2_shutdown' : False, - 'timer_shutdown' : False, - 'p1_keep_warm' : False, - 'p2_keep_warm' : False, - 'timer_keep_warm' : False - } - - control['probe_titles'] = { - 'grill_title' : 'Grill', - 'probe1_title' : 'Probe 1', - 'probe2_title' : 'Probe 2', - } - - for item in notify_data: - if item['label'] == grill1_key: - control['setpoints']['grill_notify'] = item['target'] - control['notify_req']['grill'] = item['req'] - control['probe_titles']['grill_title'] = item['name'] - if item['label'] == probe1_key: - control['setpoints']['probe1'] = item['target'] - control['notify_req']['probe1'] = item['req'] - control['notify_data']['p1_shutdown'] = item['shutdown'] - control['notify_data']['p1_keep_warm'] = item['keep_warm'] - control['probe_titles']['probe1_title'] = item['name'] - if item['label'] == probe2_key: - control['setpoints']['probe2'] = item['target'] - control['notify_req']['probe2'] = item['req'] - control['notify_data']['p2_shutdown'] = item['shutdown'] - control['notify_data']['p2_keep_warm'] = item['keep_warm'] - control['probe_titles']['probe2_title'] = item['name'] - if item['label'] == 'Timer': - control['notify_req']['timer'] = item['req'] - control['notify_data']['timer_shutdown'] = item['shutdown'] - control['notify_data']['timer_keep_warm'] = item['keep_warm'] - if item['label'] == 'Hopper': - control['notify_data']['hopper_low'] = item['req'] - - return control - -def hack_write_control(control_in, direct_write=False, origin='unknown'): - control_cur = read_control() - current = read_current() - control = control_in.copy() - - control['notify_data'] = control_cur['notify_data'] # Overwrite with new notify structure - - control.pop('setpoints') # Not used in updated control - control.pop('notify_req') # Not used in updated control - control.pop('probe_titles') # Not used in updated control - - # If you are changing the set point temperature while in Hold Mode, then you may need a direct write - if (control_cur['mode'] == 'Hold') and (control_cur['primary_setpoint'] != control_in['setpoints']['grill']): - direct_write=True - # If you are changing from any mode to hold mode, then you may need a direct write - if (control_cur['mode'] != 'Hold') and (control['mode'] == 'Hold'): - direct_write=True - - control['primary_setpoint'] = control_in['setpoints']['grill'] - - grill1_key = list(current['P'].keys())[0] - - probe1_key = '' - if len(list(current['F'].keys())) > 0: - probe1_key = list(current['F'].keys())[0] - - probe2_key = '' - if len(list(current['F'].keys())) > 1: - probe2_key = list(current['F'].keys())[1] - - for index, item in enumerate(control['notify_data']): - if item['label'] == grill1_key: - control['notify_data'][index]['target'] = control_in['setpoints']['grill_notify'] - control['notify_data'][index]['req'] = control_in['notify_req']['grill'] - if item['label'] == probe1_key: - control['notify_data'][index]['target'] = control_in['setpoints']['probe1'] - control['notify_data'][index]['req'] = control_in['notify_req']['probe1'] - control['notify_data'][index]['shutdown'] = control_in['notify_data']['p1_shutdown'] - control['notify_data'][index]['keep_warm'] = control_in['notify_data']['p1_keep_warm'] - if item['label'] == probe2_key: - control['notify_data'][index]['target'] = control_in['setpoints']['probe2'] - control['notify_data'][index]['req'] = control_in['notify_req']['probe2'] - control['notify_data'][index]['shutdown'] = control_in['notify_data']['p2_shutdown'] - control['notify_data'][index]['keep_warm'] = control_in['notify_data']['p2_keep_warm'] - if item['label'] == 'Timer': - control['notify_data'][index]['req'] = control_in['notify_req']['timer'] - control['notify_data'][index]['shutdown'] = control_in['notify_data']['timer_shutdown'] - control['notify_data'][index]['keep_warm'] = control_in['notify_data']['timer_keep_warm'] - if item['label'] == 'Hopper': - control['notify_data'][index]['req'] = control_in['notify_data']['hopper_low'] - # Direct Write is required due to timing issues - however this may lead to some commands being missed during active modes. - write_control(control, direct_write=direct_write, origin=origin) - -def hack_prepare_data(num_items=10, reduce=True, data_points=60): - # num_items: Number of items to store in the data blob - settings = read_settings() - units = settings['globals']['units'] - - data_struct = read_history(num_items) - - unpacked_history = unpack_history(data_struct) - - list_length = len(unpacked_history['T']) # Length of list(s) - - data_blob = {} - - data_blob['label_time_list'] = unpacked_history['T'] - - grill_key = list(unpacked_history['P'].keys())[0] - data_blob['grill_temp_list'] = unpacked_history['P'][grill_key] - data_blob['grill_settemp_list'] = unpacked_history['PSP'] - - probe1_key = '' - if len(list(unpacked_history['F'].keys())) > 0: - probe1_key = list(unpacked_history['F'].keys())[0] - data_blob['probe1_temp_list'] = unpacked_history['F'][probe1_key] - data_blob['probe1_settemp_list'] = unpacked_history['NT'][probe1_key] - else: - data_blob['probe1_temp_list'] = [] - data_blob['probe1_settemp_list'] = [] - for index in range(list_length): - data_blob['probe1_temp_list'].append(0) - data_blob['probe1_settemp_list'].append(0) - - probe2_key = '' - if len(list(unpacked_history['F'].keys())) > 1: - probe2_key = list(unpacked_history['F'].keys())[1] - data_blob['probe2_temp_list'] = unpacked_history['F'][probe2_key] - data_blob['probe2_settemp_list'] = unpacked_history['NT'][probe2_key] - else: - data_blob['probe2_temp_list'] = [] - data_blob['probe2_settemp_list'] = [] - for index in range(list_length): - data_blob['probe2_temp_list'].append(0) - data_blob['probe2_settemp_list'].append(0) - - if (list_length < num_items) and (list_length > 0): - num_items = list_length - - if reduce and (num_items > data_points): - step = int(num_items/data_points) - temp_blob = data_blob.copy() - data_blob = {} - for key, value in temp_blob.items(): - for index in range(list_length - num_items, list_length, step): - data_blob[key].append(temp_blob[key][index]) - - if (list_length == 0): - now = datetime.datetime.now() - #time_now = now.strftime('%H:%M:%S') - time_now = int(now.timestamp() * 1000) # Use timestamp format (int) instead of H:M:S format in string - for index in range(num_items): - data_blob['label_time_list'].append(time_now) - data_blob['grill_temp_list'].append(0) - data_blob['grill_settemp_list'].append(0) - data_blob['probe1_temp_list'].append(0) - data_blob['probe1_settemp_list'].append(0) - data_blob['probe2_temp_list'].append(0) - data_blob['probe2_settemp_list'].append(0) - - return(data_blob) - -def hack_read_current(): - current = read_current() - current_out = [0, 0, 0] - - current_out[0] = current['P'][list(current['P'].keys())[0]] - - if len(list(current['F'].keys())) > 0: - current_out[1] = current['F'][list(current['F'].keys())[0]] - - if len(list(current['F'].keys())) > 1: - current_out[2] = current['F'][list(current['F'].keys())[1]] - - return current_out \ No newline at end of file diff --git a/common/process_mon.py b/common/process_mon.py index 55faeaf2..46cde4e4 100644 --- a/common/process_mon.py +++ b/common/process_mon.py @@ -26,7 +26,7 @@ import threading import subprocess import logging -from common import create_logger, is_raspberry_pi +from common import create_logger, is_real_hardware ''' ============================================================================== @@ -44,7 +44,7 @@ def __init__(self, process, command, timeout=5): self.active = False self.kill = False - self.is_pi = is_raspberry_pi() + self.is_real_hw = is_real_hardware() # Setup logging log_level = logging.ERROR @@ -85,8 +85,8 @@ def _heartbeat_check(self): message = f'The {self.process} process experienced a timeout event (no heartbeat detected in {self.timeout} seconds) and is being reset.' self.event_logger.error(message) self.process_logger.error(message) - # Execute command on raspberry pi only - if self.is_pi: + # Execute command on real hardware only + if self.is_real_hw: subprocess.run(self.command) else: print(message) diff --git a/common/redis_queue.py b/common/redis_queue.py new file mode 100644 index 00000000..224800d4 --- /dev/null +++ b/common/redis_queue.py @@ -0,0 +1,33 @@ +""" +Class to create a generic redis based queue +""" +import redis +import json + + +class RedisQueue(): + def __init__(self, hashname): + self.hashname = hashname + self.redis_db = redis.StrictRedis('localhost', 6379, charset="utf-8", decode_responses=True) + + def push(self, data): + self.redis_db.rpush(self.hashname, json.dumps(data)) + + def pop(self): + popped = None + if self.length() > 0: + popped = json.loads(self.redis_db.lpop(self.hashname)) + return popped + + def length(self): + return self.redis_db.llen(self.hashname) + + def list(self, start=0, end=-1): + data = self.redis_db.lrange(self.hashname, start, end) + output = [] + while len(data) > 0: + output.append(json.loads(data.pop(0))) + return output + + def flush(self): + self.redis_db.delete(self.hashname) \ No newline at end of file diff --git a/control.py b/control.py index 2489f787..0ed83a4a 100755 --- a/control.py +++ b/control.py @@ -23,13 +23,13 @@ import importlib from common import * # Common Module for WebUI and Control Program from common.process_mon import Process_Monitor +from common.redis_queue import RedisQueue from notify.notifications import * from file_mgmt.recipes import convert_recipe_units from file_mgmt.cookfile import create_cookfile from file_mgmt.common import read_json_file_data from os.path import exists - ''' ============================================================================== Read and initialize Settings, Control, History, Metrics, and Error Data @@ -65,7 +65,7 @@ GrillPlatModule = importlib.import_module(f'grillplat.{grill_platform}') except: - controlLogger.exception(f'Error occurred loading grillplatform module ({filename}). Trace dump: ') + controlLogger.exception(f'Error occurred loading grillplatform module ({settings["modules"]["grillplat"]}). Trace dump: ') GrillPlatModule = importlib.import_module('grillplat.prototype') error_event = f'An error occurred loading the [{settings["modules"]["grillplat"]}] platform module. The ' \ f'prototype module has been loaded instead. This sometimes means that the hardware is not connected ' \ @@ -94,7 +94,7 @@ else: grill_platform = GrillPlatModule.GrillPlatform(out_pins, in_pins, trigger_level) except: - controlLogger.exception(f'Error occurred configuring grillplatform module ({filename}). Trace dump: ') + controlLogger.exception(f'Error occurred configuring grillplatform module ({settings["modules"]["grillplat"]}). Trace dump: ') from grillplat.prototype import GrillPlatform # Simulated Library for controlling the grill platform grill_platform = GrillPlatform(out_pins, in_pins, trigger_level) error_event = f'An error occurred configuring the [{settings["modules"]["grillplat"]}] platform object. The ' \ @@ -134,6 +134,8 @@ try: display_name = settings['modules']['display'] DisplayModule = importlib.import_module(f'display.{display_name}') + display_config = settings['display']['config'][display_name] + display_config['probe_info'] = get_probe_info(settings['probe_settings']['probe_map']['probe_info']) except: controlLogger.exception(f'Error occurred loading the display module ({display_name}). Trace dump: ') @@ -150,11 +152,11 @@ try: display_device = DisplayModule.Display(dev_pins=dev_pins, buttonslevel=buttons_level, - rotation=disp_rotation, units=units) + rotation=disp_rotation, units=units, config=display_config) except: - controlLogger.exception(f'Error occurred configuring the display module ({filename}). Trace dump: ') + controlLogger.exception(f'Error occurred configuring the display module ({settings["modules"]["display"]}). Trace dump: ') from display.none import Display # Simulated Library for controlling the grill platform - display_device = Display(dev_pins=dev_pins, buttonslevel=buttons_level, rotation=disp_rotation, units=units) + display_device = Display(dev_pins=dev_pins, buttonslevel=buttons_level, rotation=disp_rotation, units=units, config={}) error_event = f'An error occurred configuring the [{settings["modules"]["display"]}] display object. The ' \ f'"display_none" module has been loaded instead. This sometimes means that the hardware is ' \ f'not connected properly, or the module is not configured. Please run the configuration wizard ' \ @@ -235,6 +237,31 @@ def _start_fan(settings, duty_cycle=None): else: grill_platform.fan_on() +def _process_system_commands(grill_platform): + # Setup access to the system command queue + system_commands = RedisQueue('control:systemq') + # Setup access to the system output queue + system_output = RedisQueue('control:systemo') + # Initialize variable for supported commands (only look for supported commands if we have something to process) + supported_cmds = [] + + while system_commands.length() > 0: + if supported_cmds == []: + # Get list of supported system commands + supported_cmds = grill_platform.supported_commands(None)['data']['supported_cmds'] + command = system_commands.pop() + if command[0] in supported_cmds: + command_method = getattr(grill_platform, command[0]) + result = command_method(command) + result['command'] = command + else: + result = { + 'command' : command, + 'result' : 'ERROR', + 'message' : f'ERROR: Command [{command[0]}] is not supported with the current platform.', + 'data' : {} + } + system_output.push(result) def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device): """ @@ -302,8 +329,8 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device # Set DC fan frequency if it has changed since init if dc_fan: pwm_frequency = settings['pwm']['frequency'] - status_data = grill_platform.get_output_status() - if not pwm_frequency == status_data['frequency']: + frequency_status = grill_platform.get_output_status() + if not pwm_frequency == frequency_status['frequency']: grill_platform.set_pwm_frequency(pwm_frequency) # Set Starting Configuration for Igniter, Fan , Auger @@ -392,6 +419,7 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device # Safety Controls if mode in ('Startup', 'Reignite'): + raw_startup_temp = ptemp # This value is needed for the case when the grill starts hot and exit temp has been exceeded control['safety']['startuptemp'] = int(max((ptemp * 0.9), settings['safety']['minstartuptemp'])) control['safety']['startuptemp'] = int( min(control['safety']['startuptemp'], settings['safety']['maxstartuptemp'])) @@ -418,40 +446,47 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device send_notifications("Grill_Error_03", control, settings, pelletdb) # Apply Smart Start Settings if Enabled - startup_timer = settings['globals']['startup_timer'] - if settings['smartstart']['enabled'] and mode in ('Startup', 'Reignite', 'Smoke'): + startup_timer = settings['startup']['duration'] + if settings['startup']['smartstart']['enabled'] and mode in ('Startup', 'Reignite', 'Smoke'): # If Startup, then save initial temperature & select the profile if mode in ('Startup', 'Reignite'): control['smartstart']['startuptemp'] = int(ptemp) # Cycle through profiles, and set profile if startup temperature falls below the minimum temperature - for profile_selected in range(0, len(settings['smartstart']['temp_range_list'])): - if control['smartstart']['startuptemp'] < settings['smartstart']['temp_range_list'][profile_selected]: + for profile_selected in range(0, len(settings['startup']['smartstart']['temp_range_list'])): + if control['smartstart']['startuptemp'] < settings['startup']['smartstart']['temp_range_list'][profile_selected]: control['smartstart']['profile_selected'] = profile_selected write_control(control, direct_write=True, origin='control') break # Break out of the loop - if profile_selected == len(settings['smartstart']['temp_range_list']) - 1: + if profile_selected == len(settings['startup']['smartstart']['temp_range_list']) - 1: control['smartstart']['profile_selected'] = profile_selected + 1 write_control(control, direct_write=True, origin='control') # Apply the profile profile_selected = control['smartstart']['profile_selected'] - OnTime = settings['smartstart']['profiles'][profile_selected]['augerontime'] # Auger On Time (Default 15s) - OffTime = settings['cycle_data']['SmokeOffCycleTime'] + (settings['smartstart']['profiles'][profile_selected]['p_mode'] * 10) # Auger Off Time + OnTime = settings['startup']['smartstart']['profiles'][profile_selected]['augerontime'] # Auger On Time (Default 15s) + OffTime = settings['cycle_data']['SmokeOffCycleTime'] + (settings['startup']['smartstart']['profiles'][profile_selected]['p_mode'] * 10) # Auger Off Time CycleTime = OnTime + OffTime # Total Cycle Time CycleRatio = RawCycleRatio = OnTime / CycleTime # Ratio of OnTime to CycleTime - startup_timer = settings['smartstart']['profiles'][profile_selected]['startuptime'] + startup_timer = settings['startup']['smartstart']['profiles'][profile_selected]['startuptime'] # Write Metrics metrics['smart_start_profile'] = profile_selected metrics['startup_temp'] = control['smartstart']['startuptemp'] - metrics['p_mode'] = settings['smartstart']['profiles'][profile_selected]['p_mode'] - metrics['auger_cycle_time'] = settings['smartstart']['profiles'][profile_selected]['augerontime'] + metrics['p_mode'] = settings['startup']['smartstart']['profiles'][profile_selected]['p_mode'] + metrics['auger_cycle_time'] = settings['startup']['smartstart']['profiles'][profile_selected]['augerontime'] write_metrics(metrics) # Set the start time start_time = time.time() + if mode == 'Startup': + control['startup_timestamp'] = start_time + write_control(control, direct_write=True, origin='control') + # Set time since toggle for temperature temp_toggle_time = start_time + # Set time since toggle for checking ETA + eta_toggle_time = start_time + # Set time since toggle for auger auger_toggle_time = start_time @@ -473,13 +508,19 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device # Set Fan Ramping Boolean pwm_fan_ramping = False + # Setup Display Data + status_data = {} + in_data = {} + # ============ Main Work Cycle ============ while status == 'Active': now = time.time() - execute_commands() + execute_control_writes() control = read_control() + _process_system_commands(grill_platform) + # Check if new mode has been requested if control['updated']: break @@ -489,6 +530,11 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device control['settings_update'] = False write_control(control, direct_write=True, origin='control') settings = read_settings() + # Change the log level if settings were updated + if settings['globals']['debug_mode']: + eventLogger.setLevel(logging.DEBUG) + else: + eventLogger.setLevel(logging.INFO) if mode in ('Startup', 'Reignite', 'Smoke'): OnTime = settings['cycle_data']['SmokeOnCycleTime'] # Auger On Time (Default 15s) OffTime = settings['cycle_data']['SmokeOffCycleTime'] + (settings['cycle_data']['PMode'] * 10) # Auger Off Time @@ -586,6 +632,12 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device eventLogger.debug('On Time = ' + str(OnTime) + ', OffTime = ' + str( OffTime) + ', CycleTime = ' + str(CycleTime) + ', CycleRatio = ' + str(CycleRatio)) + #publish pid info to mqtt if enabled + if settings['notify_services'].get('mqtt') != None and settings['notify_services']['mqtt']['enabled']: + pid_data = controllerCore.__dict__ + pid_data['cycle_ratio'] = round(CycleRatio, 2) + check_notify(settings, control, pid_data=pid_data) + # If Auger is ON and time since toggle is greater than On Time if current_output_status['auger'] and (now - auger_toggle_time) > (CycleTime * CycleRatio): grill_platform.auger_off() @@ -608,7 +660,6 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device sensor_data = probe_complex.read_probes() ptemp = list(sensor_data['primary'].values())[0] # Primary Temperature or the Pit Temperature - in_data = {} in_data['probe_history'] = sensor_data in_data['primary_setpoint'] = control['primary_setpoint'] if mode == 'Hold' else 0 in_data['notify_targets'] = get_notify_targets(control['notify_data']) @@ -626,12 +677,23 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device if control['tuning_mode']: write_tr(in_data['probe_history']['tr']) + # Every 20 seconds, update ETA for any pending notifications + if (now - eta_toggle_time) > 20: + eta_toggle_time = time.time() + update_eta = True + else: + update_eta = False # Check to see if there are any pending notifications (i.e. Timer / Temperature Settings) - control = check_notify(in_data, control, settings, pelletdb, grill_platform) + control = check_notify(settings, control, in_data=in_data, pelletdb=pelletdb, grill_platform=grill_platform, update_eta=update_eta) + + # Publish the cycle ratio to mqtt. Note if in HOLD mode it was already published. + if mode in ('Startup', 'Smoke') and 'CycleRatio' in locals(): + pid_data = {} + pid_data['cycle_ratio'] = round(CycleRatio, 2) + check_notify(settings, control, pid_data=pid_data) # Send Current Status / Temperature Data to Display Device every 0.5 second (Display Refresh) if (now - display_toggle_time) > 0.5: - status_data = {} status_data['notify_data'] = control['notify_data'] # Get any flagged notifications status_data['timer'] = control['timer'] # Get the timer information status_data['s_plus'] = control['s_plus'] @@ -641,12 +703,13 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device status_data['recipe'] = True if control['mode'] == 'Recipe' else False status_data['start_time'] = start_time status_data['start_duration'] = startup_timer - status_data['shutdown_duration'] = settings['globals']['shutdown_timer'] + status_data['shutdown_duration'] = settings['shutdown']['shutdown_duration'] status_data['prime_duration'] = prime_duration if mode == 'Prime' else 0 # Enable Timer for Prime Mode status_data['prime_amount'] = prime_amount if mode == 'Prime' else 0 # Enable Display of Prime Amount status_data['lid_open_detected'] = LidOpenDetect if mode == 'Hold' else False status_data['lid_open_endtime'] = LidOpenEventExpires if mode == 'Hold' else 0 status_data['p_mode'] = metrics.get('p_mode', None) + status_data['startup_timestamp'] = control['startup_timestamp'] if control['mode'] == 'Recipe': status_data['recipe_paused'] = True if control['recipe']['step_data']['triggered'] and control['recipe']['step_data']['pause'] else False else: @@ -789,18 +852,33 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device write_history(in_data, ext_data=ext_data) monitor.heartbeat() # Issue a heartbeat for the process monitor - # Check if startup time has elapsed since startup/reignite mode started + # Check if startup time has elapsed since startup/reignite mode started or if exit temperature has been achieved if mode in ('Startup', 'Reignite'): - if settings['smartstart']['enabled']: + if settings['startup']['smartstart']['enabled']: profile_selected = control['smartstart']['profile_selected'] - startup_timer = settings['smartstart']['profiles'][profile_selected]['startuptime'] + startup_timer = settings['startup']['smartstart']['profiles'][profile_selected]['startuptime'] + # Check case where the grill starts hot (perhaps due to previous failure) + if raw_startup_temp >= settings['startup']['smartstart']['exit_temp']: + exit_temp = 0 # Force ignite + else: + exit_temp = settings['startup']['smartstart']['exit_temp'] else: - startup_timer = settings['globals']['startup_timer'] + startup_timer = settings['startup']['duration'] + exit_temp = settings['startup']['startup_exit_temp'] + # Check case where the grill starts hot (perhaps due to previous failure) + if raw_startup_temp >= settings['startup']['startup_exit_temp']: + exit_temp = 0 # Force ignite + else: + exit_temp = settings['startup']['startup_exit_temp'] + if (now - start_time) > startup_timer: break + if (exit_temp != 0) and (ptemp >= exit_temp): + break + # Check if shutdown time has elapsed since shutdown mode started - if mode == 'Shutdown' and (now - start_time) > settings['globals']['shutdown_timer']: + if mode == 'Shutdown' and (now - start_time) > settings['shutdown']['shutdown_duration']: break # Check if prime time has elapsed @@ -868,10 +946,15 @@ def _work_cycle(mode, grill_platform, probe_complex, display_device, dist_device write_metrics(metrics) monitor.stop_monitor() + + if status_data != {}: + status_data['mode'] = control['mode'] + display_device.display_status(in_data, status_data) + return () def _next_mode(next_mode, setpoint=0): - execute_commands() + execute_control_writes() control = read_control() # If no other request, then transition to next mode, otherwise exit if not control['updated']: @@ -940,7 +1023,7 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta _work_cycle(recipe['steps'][step_num]['mode'], grill_platform, probe_complex, display_device, dist_device) # 4c. If reignite is required, run a reignite cycle and retry current step - execute_commands() + execute_control_writes() control = read_control() if control['mode'] == 'Reignite' and control['updated']: control['updated'] = False @@ -975,7 +1058,6 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta return() - # ***************************************** # Main Program Start / Init and Loop # ***************************************** @@ -1015,9 +1097,20 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta continue write_status(status) - # 1. Check control for commands - execute_commands() + # 1. Check control for changes + execute_control_writes() control = read_control() + # 2. Check for system commands + _process_system_commands(grill_platform) + + # Check if there were updates to any of the settings that were flagged + if control['settings_update']: + control['settings_update'] = False + write_control(control, direct_write=True, origin='control') + settings = read_settings() + + # Check if there are any notifications pending + check_notify(settings, control, pelletdb=pelletdb, grill_platform=grill_platform) # Check if there is a timer running, see if it has expired, send notification and reset for index, item in enumerate(control['notify_data']): @@ -1098,13 +1191,15 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta status['recipe_paused'] = False status['start_time'] = 0 status['lid_open_detected'] = False - status['lid_open_endtime'] = 0 + status['lid_open_endtime'] = 0 + status['startup_timestamp'] = 0 write_status(status) if control['status'] == 'monitor' and control['mode'] == 'Error': grill_platform.power_on() else: grill_platform.power_off() + if control['mode'] == 'Stop': eventLogger.info('Stop Mode Started.') display_device.clear_display() # When in error mode, leave the display showing ERROR @@ -1115,6 +1210,7 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta control['tuning_mode'] = False # Turn off Tuning Mode on Stop just in case it is on control['next_mode'] = 'Stop' control['safety']['reigniteretries'] = settings['safety']['reigniteretries'] # Reset retry counter to default + control['startup_timestamp'] = 0 # Reset the startup timestamp to 0 write_control(control, direct_write=True, origin='control') else: eventLogger.error('An error has occurred, Stop Mode enabled.') @@ -1141,7 +1237,7 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta _work_cycle('Prime', grill_platform, probe_complex, display_device, dist_device) # Select Next Mode settings = read_settings() - _next_mode(control['next_mode'], setpoint=settings['start_to_mode']['primary_setpoint']) + _next_mode(control['next_mode'], setpoint=settings['startup']['start_to_mode']['primary_setpoint']) # Startup (startup sequence) elif control['mode'] == 'Startup': @@ -1151,14 +1247,27 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta # Clear History (in the case it wasn't already cleared fromt he last run) eventLogger.debug('Clearing History and Current Log on Startup Mode.') read_history(0, flushhistory=True) # Clear all history - # Setup Next Mode (after startup mode) - control['next_mode'] = settings['start_to_mode']['after_startup_mode'] - write_control(control, direct_write=True, origin='control') - # Call Work Cycle for Startup Mode - _work_cycle('Startup', grill_platform, probe_complex, display_device, dist_device) - # Select Next Mode - settings = read_settings() - _next_mode(control['next_mode'], setpoint=settings['start_to_mode']['primary_setpoint']) + # Check if Prime on Startup is selected + if settings['startup']['prime_on_startup'] > 0: + control['prime_amount'] = settings['startup']['prime_on_startup'] + control['mode'] = 'Prime' + write_control(control, direct_write=True, origin='control') + # Call Work Cycle for Prime Mode + _work_cycle('Prime', grill_platform, probe_complex, display_device, dist_device) + control = read_control() # Refresh control in case any changes were made during the cycle + if control['mode'] in ['Prime', 'Startup']: + control['updated'] = False + control['mode'] = 'Startup' + # Check if there was a mode change during Priming + if control['mode'] == 'Startup': + # Setup Next Mode (after startup mode) + control['next_mode'] = settings['startup']['start_to_mode']['after_startup_mode'] + write_control(control, direct_write=True, origin='control') + # Call Work Cycle for Startup Mode + _work_cycle('Startup', grill_platform, probe_complex, display_device, dist_device) + # Select Next Mode + settings = read_settings() + _next_mode(control['next_mode'], setpoint=settings['startup']['start_to_mode']['primary_setpoint']) # Smoke (smoke cycle) elif control['mode'] == 'Smoke': @@ -1176,7 +1285,7 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta write_control(control, direct_write=True, origin='control') _work_cycle('Shutdown', grill_platform, probe_complex, display_device, dist_device) _next_mode(control['next_mode']) - if settings['globals']['auto_power_off']: + if settings['shutdown']['auto_power_off']: eventLogger.info('Shutdown mode ended powering off grill') os.system("sleep 3 && sudo shutdown -h now &") @@ -1204,6 +1313,9 @@ def _recipe_mode(grill_platform, probe_complex, display_device, dist_device, sta write_control(control, direct_write=True, origin='control') _work_cycle('Reignite', grill_platform, probe_complex, display_device, dist_device) _next_mode(control['next_mode'], setpoint=setpoint) + + if settings['notify_services'].get('mqtt') != None and settings['notify_services']['mqtt']['enabled']: + check_notify(settings, control, pelletdb=pelletdb) time.sleep(0.1) # =================== diff --git a/controller/controllers.json b/controller/controllers.json index 9bfd3619..bc4be5ca 100644 --- a/controller/controllers.json +++ b/controller/controllers.json @@ -20,7 +20,7 @@ { "option_name" : "PB", "option_friendly_name" : "Proportional Band(PB)", - "option_description" : "This is the temperature band centered around the set point, that the controller is active. If the error is greater than PB/2, the command is 0. If the error is less than PB/2, the command is 1.0", + "option_description" : "This is the temperature band centered around the set point, that the controller is active. If the error is greater than PB/2, the command is 0. If the error is less than PB/2, the command is 1.0. [Default=60.0]", "option_type" : "float", "option_default" : 60.0, "option_min" : null, @@ -31,7 +31,7 @@ { "option_name" : "Td", "option_friendly_name" : "Derivative Time (Td)", - "option_description" : "Time (in seconds) to predict the future value.", + "option_description" : "Time (in seconds) to predict the future value. [Default=45.0]", "option_type" : "float", "option_default" : 45.0, "option_min" : null, @@ -42,7 +42,7 @@ { "option_name" : "Ti", "option_friendly_name" : "Integral Time (Ti)", - "option_description" : "Time (in seconds) to eliminate the integral error.", + "option_description" : "Time (in seconds) to eliminate the integral error. [Default=180.0]", "option_type" : "float", "option_default" : 180.0, "option_min" : null, @@ -53,7 +53,7 @@ { "option_name" : "center", "option_friendly_name" : "Center Ratio", - "option_description" : "Center of Cycle Ratio, which is specific to the way this PID will behave.", + "option_description" : "Center of Cycle Ratio, which is specific to the way this PID will behave. [Default=0.5]", "option_type" : "float", "option_default" : 0.5, "option_min" : null, diff --git a/dashboard/basic.json b/dashboard/basic.json new file mode 100644 index 00000000..f9fa5f5e --- /dev/null +++ b/dashboard/basic.json @@ -0,0 +1,16 @@ +{ + "name" : "Basic", + "friendly_name" : "Basic Dashboard", + "description" : "The basic dashboard displays all food probes as values in cards. Includes a hopper card, and status card", + "author" : "Ben Parmeter", + "html_name" : "dash_basic.html", + "metadata" : "basic.json", + "link" : "https://github.com/nebhead/pifire", + "contributors" : ["Ben Parmeter"], + "attributions" : [], + "thumbnail" : "", + "custom" : { + "hidden_cards" : [] + }, + "config" : {} +} \ No newline at end of file diff --git a/dashboard/default.json b/dashboard/default.json new file mode 100644 index 00000000..45e973c9 --- /dev/null +++ b/dashboard/default.json @@ -0,0 +1,16 @@ +{ + "name" : "Default", + "friendly_name" : "Default Dashboard", + "description" : "The default PiFire dashboard displays all food probes as gauges, includes a hopper card, status card and elapsed time card.", + "author" : "Ben Parmeter", + "html_name" : "dash_default.html", + "metadata" : "default.json", + "link" : "https://github.com/nebhead/pifire", + "contributors" : ["Ben Parmeter"], + "attributions" : [], + "thumbnail" : "", + "custom" : { + "hidden_cards" : [] + }, + "config" : {} +} \ No newline at end of file diff --git a/display/base_240x320.py b/display/base_240x320.py index 451726de..4ec15177 100644 --- a/display/base_240x320.py +++ b/display/base_240x320.py @@ -28,7 +28,7 @@ ''' class DisplayBase: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): # Init Global Variables and Constants self.dev_pins = dev_pins self.buttonslevel = buttonslevel @@ -95,51 +95,67 @@ def _init_menu(self): # List of options for the 'inactive' menu. This is the initial menu when smoker is not running. 'Startup': { 'displaytext': 'Startup', - 'icon': '\uf04b' # FontAwesome Play Icon + 'icon': '\uf04b', # FontAwesome Play Icon + 'iconcolor': (255,255,255) }, 'Prime': { 'displaytext': 'Prime', - 'icon': '\uf101' # FontAwesome Double Arrow Right Icon + 'icon': '\uf101', # FontAwesome Double Arrow Right Icon + 'iconcolor': (255,255,255) }, 'Monitor': { 'displaytext': 'Monitor', - 'icon': '\uf530' # FontAwesome Glasses Icon + 'icon': '\uf530', # FontAwesome Glasses Icon + 'iconcolor': (255,255,255) }, 'Stop': { 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon + 'icon': '\uf04d', # FontAwesome Stop Icon + 'iconcolor': (255,255,255) }, 'Network': { 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon + 'icon': '\uf1eb', # FontAwesome Wifi Icon + 'iconcolor': (255,255,255) + }, + 'Power':{ + 'displaytext': 'Power Menu', + 'icon': '\uf0e7', #FontAwesome Power Icon + 'iconcolor' : (255,255,255) } } self.menu['active'] = { # List of options for the 'active' menu. This is the second level menu of options while running. - 'Shutdown': { - 'displaytext': 'Shutdown', - 'icon': '\uf11e' # FontAwesome Finish Icon - }, 'Hold': { 'displaytext': 'Hold', - 'icon': '\uf76b' # FontAwesome Temperature Low Icon + 'icon': '\uf76b', # FontAwesome Temperature Low Icon + 'iconcolor': (255,255,255) + }, + 'Shutdown': { + 'displaytext': 'Shutdown', + 'icon': '\uf11e', # FontAwesome Finish Icon + 'iconcolor' : (255,255,255) # White Orange }, 'Smoke': { 'displaytext': 'Smoke', - 'icon': '\uf0c2' # FontAwesome Cloud Icon + 'icon': '\uf0c2', # FontAwesome Cloud Icon + 'iconcolor': (255,255,255) }, + 'SmokePlus': { + 'displaytext': 'Smoke+', + 'icon': '\uf0c2', # FontAwesome Cloud Icon + 'iconcolor': (255,255,255) + }, 'Stop': { 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon - }, - 'SmokePlus': { - 'displaytext': 'Toggle Smoke+', - 'icon': '\uf0c2' # FontAwesome Cloud Icon + 'icon': '\uf04d', # FontAwesome Stop Icon + 'iconcolor': (255,255,255) }, 'Network': { 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon + 'icon': '\uf1eb', # FontAwesome Wifi Icon + 'iconcolor': (255,255,255) } } @@ -147,52 +163,86 @@ def _init_menu(self): # List of options for the 'active' menu. This is the second level menu of options while running. 'NextStep': { 'displaytext': 'Next Recipe Step', - 'icon': '\uf051' # FontAwesome Step Forward Icon + 'icon': '\uf051', # FontAwesome Step Forward Icon + 'iconcolor': (255,255,255) }, 'Shutdown': { 'displaytext': 'Shutdown', - 'icon': '\uf11e' # FontAwesome Finish Icon + 'icon': '\uf11e', # FontAwesome Finish Icon + 'iconcolor' : (255,255,255) # White Orange }, 'Stop': { 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon + 'icon': '\uf04d', # FontAwesome Stop Icon + 'iconcolor': (255,255,255) }, 'SmokePlus': { - 'displaytext': 'Toggle Smoke+', - 'icon': '\uf0c2' # FontAwesome Cloud Icon + 'displaytext': 'Smoke+', + 'icon': '\uf0c2', # FontAwesome Cloud Icon + 'iconcolor': (255,255,255) }, 'Network': { 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon + 'icon': '\uf1eb', # FontAwesome Wifi Icon + 'iconcolor': (255,255,255) } } self.menu['prime_selection'] = { 'Prime_10' : { 'displaytext': '\u00BB10g', - 'icon': '10' + 'icon': '10', + 'iconcolor': (255,255,255) }, 'Prime_25' : { 'displaytext': '\u00BB25g', - 'icon': '25' + 'icon': '25', + 'iconcolor': (255,255,255) }, 'Prime_50' : { 'displaytext': '\u00BB50g', - 'icon': '50' + 'icon': '50', + 'iconcolor': (255,255,255) }, 'Prime_10_Start' : { 'displaytext': '\u00BB10g & Start', - 'icon': '10' + 'icon': '10', + 'iconcolor': (255,255,255) }, 'Prime_25_Start' : { 'displaytext': '\u00BB25g & Start', - 'icon': '25' + 'icon': '25', + 'iconcolor': (255,255,255) }, 'Prime_50_Start' : { 'displaytext': '\u00BB50g & Start', - 'icon': '50' + 'icon': '50', + 'iconcolor': (255,255,255) + }, + 'Menu_Back' : { + 'displaytext' : 'Back', + 'icon' : '\uf060' # FontAwesome Back Arrow } } + + self.menu['power_menu'] = { + 'Power_Off' : { + 'displaytext' : 'Shutdown', + 'icon': '\uf011', # FontAwesome Power Button + 'iconcolor': (255,255,255) + }, + 'Power_Restart' : { + 'displaytext': 'Restart', + 'icon': '\uf2f9', # FontAwesome Circle Arrow + 'iconcolor': (255,255,255) + }, + 'Menu_Back' : { + 'displaytext' : 'Back', + 'icon' : '\uf060', # FontAwesome Back Arrow + 'iconcolor': (255,255,255) + } + } + self.menu['current'] = {} self.menu['current']['mode'] = 'none' # Current Menu Mode (inactive, active) self.menu['current']['option'] = 0 # Current option in current mode @@ -1088,6 +1138,21 @@ def _menu_display(self, action): control['updated'] = True control['mode'] = 'Stop' write_control(control, origin='display') + elif selected == 'Power': + self.menu['current']['mode'] = 'power_menu' + self.menu['current']['option'] = 0 + elif 'Power_' in selected: + control = read_control() + if 'Off' in selected: + os.system('sudo shutdown -h now &') + elif 'Restart' in selected: + os.system('sudo reboot &') + + # Master Menu Back Function + elif 'Menu_Back' in selected: + self.menu['current']['mode'] = 'inactive' + self.menu['current']['option'] = 0 + # Active Mode elif selected == 'Shutdown': self.display_active = True @@ -1194,17 +1259,17 @@ def _menu_display(self, action): img.paste(label_canvas, label_origin, label_canvas) # Current Mode (Bottom Center) - font_point_size = 36 + font_point_size = 40 text = "Grill Set Point" - label_canvas = self._draw_text(text, self.primary_font, font_point_size, (255,255,255)) + label_canvas = self._draw_text(text, self.primary_font, font_point_size, (0,0,0)) - # Draw Black Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(0, 0, 0)) + # Draw White Rectangle + draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(255, 255, 255)) # Draw White Line/Rectangle draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, ((self.HEIGHT // 8) * 6) + 2)], - fill=(255, 255, 255)) + fill=(130, 130, 130)) # Draw Text - label_origin = (int(self.WIDTH // 2 - label_canvas.width // 2), int((self.HEIGHT // 8) * 6.25)) + label_origin = (int(self.WIDTH // 2 - label_canvas.width // 2), int((self.HEIGHT // 8) * 6.35)) img.paste(label_canvas, label_origin, label_canvas) elif self.menu['current']['mode'] != 'none': @@ -1217,8 +1282,9 @@ def _menu_display(self, action): break index += 1 font_point_size = 80 if self.WIDTH == 240 else 120 + icon_color = self.menu[self.menu['current']['mode']][selected].get('iconcolor', (255,255,255)) # Get color from menu item, default to white if not defined text = self.menu[self.menu['current']['mode']][selected]['icon'] - label_canvas = self._draw_text(text, 'static/font/FA-Free-Solid.otf', font_point_size, (255,255,255)) + label_canvas = self._draw_text(text, 'static/font/FA-Free-Solid.otf', font_point_size, icon_color) label_origin = (int(self.WIDTH // 2 - label_canvas.width // 2), int(self.HEIGHT // 2.5 - label_canvas.height // 2)) img.paste(label_canvas, label_origin, label_canvas) @@ -1231,16 +1297,16 @@ def _menu_display(self, action): img.paste(label_canvas, label_origin, label_canvas) # Current Mode (Bottom Center) - # Draw Black Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(0, 0, 0)) - # Draw White Line/Rectangle + # Draw White Rectangle + draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(255, 255, 255)) + # Draw Gray Line/Rectangle draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, ((self.HEIGHT // 8) * 6) + 2)], - fill=(255, 255, 255)) + fill=(130, 130, 130)) # Draw Text - font_point_size = 36 + font_point_size = 40 text = self.menu[self.menu['current']['mode']][selected]['displaytext'] - label_canvas = self._draw_text(text, self.primary_font, font_point_size, (255,255,255)) - label_origin = (int(self.WIDTH // 2 - label_canvas.width // 2), int((self.HEIGHT // 8) * 6.25)) + label_canvas = self._draw_text(text, self.primary_font, font_point_size, (0,0,0)) + label_origin = (int(self.WIDTH // 2 - label_canvas.width // 2), int((self.HEIGHT // 8) * 6.35)) img.paste(label_canvas, label_origin, label_canvas) # Change color of Arrow for Up / Down when adjusting temperature @@ -1290,10 +1356,10 @@ def display_status(self, in_data, status_data): """ - Updates the current data for the display loop, if in a work mode """ - self.units = status_data['units'] + self.units = status_data.get('units', self.units) self.display_active = True self.in_data = in_data - self.status_data = status_data + self.status_data = status_data def display_splash(self): """ diff --git a/display/base_flex.py b/display/base_flex.py index 653002b7..00904b97 100644 --- a/display/base_flex.py +++ b/display/base_flex.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 ''' ***************************************** -PiFire Display Interface Library +PiFire Flexible Display Interface Library ***************************************** Description: - This is a base class for displays using - a resolutions greater than 320x240. + This is a base class for displays, with + a modular/flexible display size and layout. Other display libraries will inherit this base class and add device specific features. @@ -17,1300 +17,805 @@ Imported Libraries ''' import time -import socket -import qrcode import logging -from PIL import Image, ImageDraw, ImageFont -from common import read_control, write_control, is_raspberry_pi +import socket +import os +from PIL import Image +from common import read_control, write_control, is_real_hardware, read_generic_json, read_settings, write_settings, read_status, read_current +from display.flexobject_pil import FlexObject as FlexObjPIL ''' +================================================================================== Display base class definition +================================================================================== ''' class DisplayBase: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - # Init Global Variables and Constants - self.dev_pins = dev_pins - self.buttonslevel = buttonslevel - self.rotation = rotation - self.units = units - self.display_active = False - self.in_data = None - self.status_data = None - self.display_timeout = None - self.display_command = 'splash' - self.input_counter = 0 - self.input_enabled = False - self.raspberry_pi = True if is_raspberry_pi() else False - self.touch_pos = (0,0) - # Attempt to set the log level of PIL so that it does not pollute the logs - logging.getLogger('PIL').setLevel(logging.CRITICAL + 1) - # Init Display Device, Input Device, Assets - self._init_globals() - self._init_assets() - self._init_input() - self._init_display_device() - - def _init_globals(self): - # Init constants and variables - ''' - 0 = Zero Degrees Rotation - 90, 1 = 90 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) - 180, 2 = 180 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) - 270, 3 = 270 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) - ''' - if self.rotation in [90, 270, 1, 3]: - self.WIDTH = 480 - self.HEIGHT = 800 - else: - self.WIDTH = 800 - self.HEIGHT = 480 - - self.inc_pulse_color = True - self.icon_color = 100 - self.fan_rotation = 0 - self.auger_step = 0 - - def _init_display_device(self): - ''' + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + # Init Global Variables and Constants + self.config = config + + self.dev_pins = dev_pins + self.units = units + + self.in_data = None + self.last_in_data = {} + self.status_data = None + self.last_status_data = {} + + self.input_enabled = False + + self.display_active = None + self.display_timeout = None + self.TIMEOUT = 10 + self.command = 'splash' + self.command_data = None + self.input_origin = None + + self.real_hardware = True if is_real_hardware() else False + # Attempt to set the log level of PIL so that it does not pollute the logs + logging.getLogger('PIL').setLevel(logging.CRITICAL + 1) + + # Setup logger + self.eventLogger = logging.getLogger('control') + # Init Display Device, Input Device, Assets + self._init_globals() + self._init_framework() + self._init_input() + self._init_display_device() + + def _init_globals(self): + # Init constants and variables + ''' + 0 = Zero Degrees Rotation + 90, 1 = 90 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) + 180, 2 = 180 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) + 270, 3 = 270 Degrees Rotation (Pimoroni Libraries, Luma.LCD Libraries) + ''' + self.rotation = self.config.get('rotation', 0) + self.buttonslevel = self.config.get('buttonslevel', 'HIGH') + + ''' Get Local IP Address ''' + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(0) + try: + # doesn't even have to be reachable + s.connect(('10.254.254.254', 1)) + self.ip_address = s.getsockname()[0] + except Exception: + self.ip_address = '127.0.0.1' + self.eventLogger.error('Unable to get IP address of the system.') + finally: + s.close() + + def _init_framework(self): + ''' + Initialize the dash/home/menu framework + ''' + self.display_data = read_generic_json(self.config['display_data_filename']) + self.WIDTH = self.display_data['metadata'].get('screen_width', 800) + self.HEIGHT = self.display_data['metadata'].get('screen_height', 480) + self.SPLASH_DELAY = self.display_data['metadata'].get('splash_delay', 1000) + self.FRAMERATE = self.display_data['metadata'].get('framerate', 30) + if self.display_data.get('home', []) == []: + self.HOME_ENABLED = False + else: + self.HOME_ENABLED = True + + self.display_data['menus']['qrcode']['ip_address'] = self.ip_address + + self._fixup_display_data() + + self._init_assets() + + def _fixup_display_data(self): + for index, object in enumerate(self.display_data['home']): + for key in list(object.keys()): + if key in ['position', 'size', 'fg_color', 'bg_color', 'color', 'active_color', 'inactive_color']: + self.display_data['home'][index][key] = tuple(object[key]) + for index, object in enumerate(self.display_data['dash']): + #print(f'Object Name: {object["name"]}') + for key in list(object.keys()): + if key in ['position', 'size', 'fg_color', 'bg_color', 'color', 'active_color', 'inactive_color']: + #print(f'[{key}] = {object[key]}') + self.display_data['dash'][index][key] = tuple(object[key]) + #print(f'converted = {tuple(object[key])}') + if key in ['color_levels']: + color_level_list = [] + for item in self.display_data['dash'][index][key]: + color_level_list.append(tuple(item)) + self.display_data['dash'][index][key] = color_level_list + for menu, object in self.display_data['menus'].items(): + for key in list(object.keys()): + if key in ['position', 'size', 'fg_color', 'bg_color', 'color', 'active_color', 'inactive_color']: + self.display_data['menus'][menu][key] = tuple(object[key]) + for input, object in self.display_data['input'].items(): + for key in list(object.keys()): + if key in ['position', 'size', 'fg_color', 'bg_color', 'color', 'active_color', 'inactive_color']: + self.display_data['input'][input][key] = tuple(object[key]) + #print(f'Fixed Up: \n{self.display_data["menus"]}') + + + def _init_display_device(self): + ''' Inheriting classes will override this function to init the display device and start the display thread. ''' - pass - - def _init_input(self): - ''' - Inheriting classes will override this function to setup the inputs. - ''' - self.input_enabled = False # If the inheriting class does not implement input, then clear this flag - self.input_counter = 0 - - def _init_menu(self): - self.menu_active = False - self.menu_time = 0 - self.menu_item = '' - - self.menu = {} - - self.menu['inactive'] = { - # List of options for the 'inactive' menu. This is the initial menu when smoker is not running. - 'Startup': { - 'displaytext': 'Startup', - 'icon': '\uf04b' # FontAwesome Play Icon - }, - 'Prime': { - 'displaytext': 'Prime', - 'icon': '\uf101' # FontAwesome Double Arrow Right Icon - }, - 'Monitor': { - 'displaytext': 'Monitor', - 'icon': '\uf530' # FontAwesome Glasses Icon - }, - 'Stop': { - 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon - }, - 'Network': { - 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon - } - } - - self.menu['active'] = { - # List of options for the 'active' menu. This is the second level menu of options while running. - 'Shutdown': { - 'displaytext': 'Shutdown', - 'icon': '\uf11e' # FontAwesome Finish Icon - }, - 'Hold': { - 'displaytext': 'Hold', - 'icon': '\uf76b' # FontAwesome Temperature Low Icon - }, - 'Smoke': { - 'displaytext': 'Smoke', - 'icon': '\uf0c2' # FontAwesome Cloud Icon - }, - 'Stop': { - 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon - }, - 'SmokePlus': { - 'displaytext': 'Toggle Smoke+', - 'icon': '\uf0c2' # FontAwesome Cloud Icon - }, - 'Network': { - 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon - } - } - - self.menu['active_recipe'] = { - # List of options for the 'active' menu. This is the second level menu of options while running. - 'NextStep': { - 'displaytext': 'Next Recipe Step', - 'icon': '\uf051' # FontAwesome Step Forward Icon - }, - 'Shutdown': { - 'displaytext': 'Shutdown', - 'icon': '\uf11e' # FontAwesome Finish Icon - }, - 'Stop': { - 'displaytext': 'Stop', - 'icon': '\uf04d' # FontAwesome Stop Icon - }, - 'SmokePlus': { - 'displaytext': 'Toggle Smoke+', - 'icon': '\uf0c2' # FontAwesome Cloud Icon - }, - 'Network': { - 'displaytext': 'IP QR Code', - 'icon': '\uf1eb' # FontAwesome Wifi Icon - } - } - - self.menu['prime_selection'] = { - 'Prime_10' : { - 'displaytext': '\u00BB10g', - 'icon': '10' - }, - 'Prime_25' : { - 'displaytext': '\u00BB25g', - 'icon': '25' - }, - 'Prime_50' : { - 'displaytext': '\u00BB50g', - 'icon': '50' - }, - 'Prime_10_Start' : { - 'displaytext': '\u00BB10g & Start', - 'icon': '10' - }, - 'Prime_25_Start' : { - 'displaytext': '\u00BB25g & Start', - 'icon': '25' - }, - 'Prime_50_Start' : { - 'displaytext': '\u00BB50g & Start', - 'icon': '50' - } - } - self.menu['current'] = {} - self.menu['current']['mode'] = 'none' # Current Menu Mode (inactive, active) - self.menu['current']['option'] = 0 # Current option in current mode - - def _display_loop(self): - """ - Main display loop - """ - while True: - if self.input_enabled: - self._event_detect() - - if self.display_timeout: - if time.time() > self.display_timeout: - self.display_timeout = None - if not self.display_active: - self.display_command = 'clear' - - if self.display_command == 'clear': - self.display_active = False - self.display_timeout = None - self.display_command = None - self._display_clear() - - if self.display_command == 'splash': - self._display_splash() - self.display_timeout = time.time() + 3 - self.display_command = 'clear' - time.sleep(3) # Hold splash screen for 3 seconds - - if self.display_command == 'text': - self._display_text() - self.display_command = None - self.display_timeout = time.time() + 10 - - if self.display_command == 'network': - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - network_ip = s.getsockname()[0] - if network_ip != '': - self._display_network(network_ip) - self.display_timeout = time.time() + 30 - self.display_command = None - else: - self.display_text("No IP Found") - - if self.input_enabled: - if self.menu_active and not self.display_timeout: - if time.time() - self.menu_time > 5: - self.menu_active = False - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - if not self.display_active: - self.display_command = 'clear' - elif not self.display_timeout and self.display_active: - if self.in_data is not None and self.status_data is not None: - self._display_current(self.in_data, self.status_data) - - elif not self.display_timeout and self.display_active: - if self.in_data is not None and self.status_data is not None: - self._display_current(self.in_data, self.status_data) - - - time.sleep(0.1) - - ''' - ============== Input Callbacks ============= - - Inheriting classes will override these functions for all inputs. - ''' - def _enter_callback(self): - ''' - Inheriting classes will override this function. - ''' - pass - - def _up_callback(self, held=False): - ''' - Inheriting classes will override this function to clear the display device. - ''' - pass - - def _down_callback(self, held=False): - ''' - Inheriting classes will override this function to clear the display device. - ''' - pass - - ''' - ============== Graphics / Display / Draw Methods ============= - ''' - def _init_assets(self): - self._init_background() - self._init_splash() - - def _init_background(self): - self.background = Image.open('static/img/display/background.png') - self.background = self.background.resize((self.WIDTH, self.HEIGHT)) - - def _init_splash(self): - self.splash = Image.open('static/img/display/color-boot-splash.png') - (self.splash_width, self.splash_height) = self.splash.size - self.splash_width *= 2 - self.splash_height *= 2 - self.splash = self.splash.resize((self.splash_width, self.splash_height)) - - def _rounded_rectangle(self, draw, xy, rad, fill=None): - x0, y0, x1, y1 = xy - draw.rectangle([(x0, y0 + rad), (x1, y1 - rad)], fill=fill) - draw.rectangle([(x0 + rad, y0), (x1 - rad, y1)], fill=fill) - draw.pieslice([(x0, y0), (x0 + rad * 2, y0 + rad * 2)], 180, 270, fill=fill) - draw.pieslice([(x1 - rad * 2, y1 - rad * 2), (x1, y1)], 0, 90, fill=fill) - draw.pieslice([(x0, y1 - rad * 2), (x0 + rad * 2, y1)], 90, 180, fill=fill) - draw.pieslice([(x1 - rad * 2, y0), (x1, y0 + rad * 2)], 270, 360, fill=fill) - - def _text_circle(self, draw, position, size, text, fg_color=(255,255,255), bg_color=(0,0,0)): - # Draw outline with fg_color - coords = (position[0], position[1], position[0] + size[0], position[1] + size[1]) - draw.ellipse(coords, fill=fg_color) - # Fill circle with Center with bg_color - fill_coords = (coords[0]+2, coords[1]+2, coords[2]-2, coords[3]-2) - draw.ellipse(fill_coords, fill=bg_color) - # Place Text - font_point_size = round(size[1] * 0.6) # Convert size to height of circle * font point ratio 0.6 - font = ImageFont.truetype("trebuc.ttf", font_point_size) - (font_width, font_height) = font.getsize(text) # Grab the width of the text - label_x = position[0] + (size[0] // 2) - (font_width // 2) - label_y = position[1] + round((size[1] // 2) - (font_point_size // 2)) - label_origin = (label_x, label_y) - draw.text(label_origin, text, font=font, fill=fg_color) - - def _text_rectangle(self, draw, center_point, text, font_point_size, text_color, fill_color, outline_color): - # Create drawing object - vertical_margin = 5 - horizontal_margin = 10 - border = 2 - rad = 5 - - font = ImageFont.truetype("trebuc.ttf", font_point_size) - (font_width, font_height) = font.getsize(text) # Grab the width of the text - - size = (font_width + horizontal_margin + border, font_height + vertical_margin + border) - outside_coords = (center_point[0] - (size[0] // 2), center_point[1] - (size[1] // 2), center_point[0] + (size[0] // 2), center_point[1] + (size[1] // 2)) - inside_coords = (outside_coords[0] + border, outside_coords[1] + border, outside_coords[2] - border, outside_coords[3] - border) - self._rounded_rectangle(draw, outside_coords, rad, outline_color) - self._rounded_rectangle(draw, inside_coords, rad, fill_color) - draw.text((center_point[0] - (font_width // 2), center_point[1] - (font_height // 1.75)), text, font=font, fill=text_color) - - #return draw - - def _create_icon(self, charid, size, color): - # Get font and character size - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", size) - # Create canvas - icon_canvas = Image.new('RGBa', font.getsize(charid)) - # Create drawing object - draw = ImageDraw.Draw(icon_canvas) - draw.text((0, 0), charid, font=font, fill=color) - icon_canvas = icon_canvas.crop(icon_canvas.getbbox()) - return(icon_canvas) - - def _paste_icon(self, icon, canvas, position, rotation): - # Rotate the icon - icon = icon.rotate(rotation) - # Set the position & paste the icon onto the canvas - canvas.paste(icon, position, icon) - return(canvas) - - def _draw_fan_icon(self, canvas, position): - draw = ImageDraw.Draw(canvas) - # F = Fan (Upper Left) - icon_char = '\uf863' - icon_color = (0, self.icon_color, 255) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 36, icon_color) - icon_position = (position[0] + 4, position[1] + 4) - canvas = self._paste_icon(icon, canvas, icon_position, self.fan_rotation) - - # Increment Fan Rotation - self.fan_rotation += 30 - if self.fan_rotation >= 360: - self.fan_rotation = 0 - - return canvas - - def _draw_auger_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # A = Auger (Center Left) - icon_char = '\uf101' - icon_color = (0, self.icon_color, 0) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 36, icon_color) - icon_position = (position[0] + 7 + self.auger_step, position[1] + 10) - canvas = self._paste_icon(icon, canvas, icon_position, 0) - - self.auger_step += 1 - if self.auger_step >= 3: - self.auger_step = 0 - - return canvas - - def _draw_ignitor_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # I = Ignitor (Center Right) - icon_char = '\uf46a' - icon_color = (255, self.icon_color, 0) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 36, icon_color) - icon_position = (position[0] + 8, position[1] + 4) - canvas = self._paste_icon(icon, canvas, icon_position, 0) - - return canvas - - def _draw_notify_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # Notification Bell - icon_char = '\uf0f3' - icon_color = (255,255, 0) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 36, icon_color) - icon_position = (position[0] + 6, position[1] + 3) - canvas = self._paste_icon(icon, canvas, icon_position, 0) - - return canvas - - def _draw_recipe_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # Recipe Icon - icon_char = '\uf46d' - icon_color = (255,255, 0) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 32, icon_color) - icon_position = (position[0] + 9, position[1] + 5) - canvas = self._paste_icon(icon, canvas, icon_position, 0) - - return canvas - - def _draw_pause_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # Recipe Pause Icon - icon_char = '\uf04c' - icon_color = (255,self.icon_color, 0) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Icon Image - icon = self._create_icon(icon_char, 28, icon_color) - icon_position = (position[0] + 9, position[1] + 9) - canvas = self._paste_icon(icon, canvas, icon_position, 0) - - return canvas - - def _draw_splus_icon(self, canvas, position): - # Create a drawing object - draw = ImageDraw.Draw(canvas) - - # S = Smoke Plus (Center Right) - icon_color = (150, 0, 255) - - # Draw Rounded Rectangle Border - self._rounded_rectangle(draw, - (position[0], position[1], - position[0] + 42, position[1] + 42), - 5, icon_color) - - # Fill Rectangle with Black - self._rounded_rectangle(draw, - (position[0] + 2, position[1] + 2, - position[0] + 40, position[1] + 40), - 5, (0,0,0)) - - # Create Smoke Plus Icon Image (cloud + plus) - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", 32) - text = '\uf0c2' # FontAwesome Icon for Cloud (Smoke) - draw.text((position[0] + 2, position[1] + 6), text, - font=font, fill=(100, 0, 255)) - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", 24) - text = '\uf067' # FontAwesome Icon for PLUS - draw.text((position[0] + 10, position[1] + 10), text, - font=font, fill=(0, 0, 0)) - - return canvas - - def _draw_gauge(self, canvas, position, size, fg_color, bg_color, percents, temps, label, sp1_color=(0, 200, 255), sp2_color=(255, 255, 0)): - # Create drawing object - draw = ImageDraw.Draw(canvas) - # bgcolor = (50, 50, 50) # Grey - # fgcolor = (200, 0, 0) # Red - # percents = [temperature, setpoint1, setpoint2] - # temps = [current, setpoint1, setpoint2] - # sp1_color = (0, 200, 255) # Cyan - # sp2_color = (255, 255, 0) # Yellow - fill_color = (0, 0, 0) # Black - - # Draw Background Line - coords = (position[0], position[1], position[0] + size[0], position[1] + size[1]) - draw.ellipse(coords, fill=bg_color) - - # Draw Arc for Temperature (Percent) - if (percents[0] > 0) and (percents[0] < 100): - endpoint = (360 * (percents[0] / 100)) + 90 - elif percents[0] > 100: - endpoint = 360 + 90 - else: - endpoint = 90 - draw.pieslice(coords, start=90, end=endpoint, fill=fg_color) - - # Draw Tic for Setpoint[1] - if percents[1] > 0: - if percents[1] < 100: - setpoint = (360 * (percents[1] / 100)) + 90 - else: - setpoint = 360 + 90 - draw.pieslice(coords, start=setpoint - 2, end=setpoint + 2, fill=sp1_color) - - # Draw Tic for Setpoint[2] - if percents[2] > 0: - if percents[2] < 100: - setpoint = (360 * (percents[2] / 100)) + 90 - else: - setpoint = 360 + 90 - draw.pieslice(coords, start=setpoint - 2, end=setpoint + 2, fill=sp2_color) - - # Fill Circle with Center with black - fill_coords = (coords[0]+10, coords[1]+10, coords[2]-10, coords[3]-10) - draw.ellipse(fill_coords, fill=fill_color) - - # Gauge Label - if len(label) <= 5: - font_point_size = round((size[1] * 0.75) / 4) + 1 # Convert size to height of circle * font point ratio / 8 - elif len(label) <= 6: - font_point_size = round((size[1] * 0.60) / 4) + 1 # Convert size to height of circle * font point ratio / 8 - else: - font_point_size = round((size[1] * 0.40) / 4) + 1 # Convert size to height of circle * font point ratio / 8 - font = ImageFont.truetype("trebuc.ttf", font_point_size) - (font_width, font_height) = font.getsize(label) # Grab the width of the text - label_x = position[0] + (size[0] // 2) - (font_width // 2) - label_y = position[1] + (round(((size[1] * 0.75) / 8) * 6.6)) - label_origin = (label_x, label_y) - draw.text(label_origin, label, font=font, fill=(255, 255, 255)) - - # SetPoint1 Label - if percents[1] > 0: - sp1_label = f'>{temps[1]}<' - font_point_size = round((size[1] * 0.75) / 4) - 1 # Convert size to height of circle * font point ratio - font = ImageFont.truetype("trebuc.ttf", font_point_size) - (font_width, font_height) = font.getsize(sp1_label) # Grab the width of the text - label_x = position[0] + (size[0] // 2) - (font_width // 2) - label_y = position[1] + round((size[1] * 0.75) / 8) - label_origin = (label_x, label_y) - draw.text(label_origin, sp1_label, font=font, fill=sp1_color) - - # Current Temperature (Large Centered) - cur_temp = str(temps[0])[:5] - if self.units == 'F': - font_point_size = round(size[1] * 0.45) # Convert size to height of circle * font point ratio / 8 - else: - font_point_size = round(size[1] * 0.3) # Convert size to height of circle * font point ratio / 8 - font = ImageFont.truetype("trebuc.ttf", font_point_size) - (font_width, font_height) = font.getsize(cur_temp) # Grab the width of the text - label_x = position[0] + (size[0] // 2) - (font_width // 2) - label_y = position[1] + ((size[1] // 2) - (font_point_size // 1.5)) - label_origin = (label_x, label_y) - draw.text(label_origin, cur_temp, font=font, fill=(255,255,255)) - - return(canvas) - - def _display_clear(self): - ''' - Inheriting classes will override this function to clear the display device. - ''' - pass - - - def _display_canvas(self, canvas): - ''' - Inheriting classes will override this function to show the canvas on the display device. - ''' - pass - - def _display_splash(self): - # Create canvas - img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0)) - - # Set the position & paste the splash image onto the canvas - position = ((self.WIDTH - self.splash_width) // 2, (self.HEIGHT - self.splash_height) // 2) - img.paste(self.splash, position) - - self._display_canvas(img) - - def _display_text(self): - # Create canvas - img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0)) - - # Create drawing object - draw = ImageDraw.Draw(img) - - font = ImageFont.truetype("impact.ttf", 42) - (font_width, font_height) = font.getsize(self.display_data) - draw.text((self.WIDTH // 2 - font_width // 2, self.HEIGHT // 2 - font_height // 2), self.display_data, - font=font, fill=255) - - self._display_canvas(img) - - def _display_network(self, network_ip): - # Create canvas - img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(255, 255, 255)) - img_qr = qrcode.make('http://' + network_ip) - img_qr_width, img_qr_height = img_qr.size - img_qr_width *= 2 - img_qr_height *= 2 - w = min(self.WIDTH, self.HEIGHT) - new_image = img_qr.resize((w, w)) - position = (int((self.WIDTH/2)-(w/2)), 0) - img.paste(new_image, position) - - self._display_canvas(img) - - def _display_current(self, in_data, status_data): - # Create canvas - img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0)) - - # Set the position and paste the background image onto the canvas - position = (0, 0) - img.paste(self.background, position) - - # Create drawing object - draw = ImageDraw.Draw(img) - - # ======== Primary Temp Circle Gauge ======== - position = (self.WIDTH // 2 - 80, self.HEIGHT // 2 - 110) - size = (160, 160) - bg_color = (50, 50, 50) # Grey - fg_color = (200, 0, 0) # Red - - label = list(in_data['probe_history']['primary'].keys())[0] - - # percents = [temperature, setpoint1, setpoint2] - temps = [0,0,0] - percents = [0,0,0] - - temps[0] = in_data['probe_history']['primary'][label] - if temps[0] <= 0: - percents[0] = 0 - elif self.units == 'F': - percents[0] = round((temps[0] / 600) * 100) # F Temp Range [0 - 600F] for Grill - else: - percents[0] = round((temps[0] / 300) * 100) # C Temp Range [0 - 300C] for Grill - - temps[1] = in_data['primary_setpoint'] - if temps[1] <= 0: - percents[1] = 0 - elif self.units == 'F' and status_data['mode'] == 'Hold': - percents[1] = round((temps[1] / 600) * 100) # F Temp Range [0 - 600F] for Grill - elif self.units == 'C' and status_data['mode'] == 'Hold': - percents[1] = round((temps[1] / 300) * 100) # C Temp Range [0 - 300C] for Grill - - temps[2] = in_data['notify_targets'][label] - if temps[2] <= 0: - percents[2] = 0 - elif self.units == 'F': - percents[2] = round((temps[2] / 600) * 100) # F Temp Range [0 - 600F] for Grill - else: - percents[2] = round((temps[2] / 300) * 100) # C Temp Range [0 - 300C] for Grill - - # Draw the Grill Gauge w/Labels - img = self._draw_gauge(img, position, size, fg_color, bg_color, - percents, temps, label) - - # ======== Probe1 Temp Circle Gauge ======== - position = (10, self.HEIGHT - 110) - size = (100, 100) - bg_color = (50, 50, 50) # Grey - fg_color = (3, 161, 252) # Blue - - label = list(in_data['probe_history']['food'].keys())[0] - - # temp, percents = [current temperature, setpoint1, setpoint2] - temps = [0,0,0] - percents = [0,0,0] - - temps[0] = in_data['probe_history']['food'][label] - if temps[0] <= 0: - percents[0] = 0 - elif self.units == 'F': - percents[0] = round((temps[0] / 300) * 100) # F Temp Range [0 - 300F] for probe - else: - percents[0] = round((temps[0] / 150) * 100) # C Temp Range [0 - 150C] for probe - - temps[1] = in_data['notify_targets'][label] - if temps[1] <= 0: - percents[1] = 0 - elif self.units == 'F': - percents[1] = round((temps[1] / 300) * 100) # F Temp Range [0 - 300F] for probe - elif self.units == 'C': - percents[1] = round((temps[1] / 150) * 100) # C Temp Range [0 - 150C] for probe - - # No SetPoint2 on Probes - temps[2] = 0 - percents[2] = 0 - - # Draw the Probe1 Gauge w/Labels - Use Yellow as the SetPoint Color - img = self._draw_gauge(img, position, size, fg_color, bg_color, - percents, temps, label, sp1_color=(255, 255, 0)) - - # ======== Probe2 Temp Circle Gauge ======== - position = (self.WIDTH - 110, self.HEIGHT - 110) - size = (100, 100) - bg_color = (50, 50, 50) # Grey - fg_color = (3, 161, 252) # Blue - - label = list(in_data['probe_history']['food'].keys())[1] - - # temp, percents = [current temperature, setpoint1, setpoint2] - temps = [0,0,0] - percents = [0,0,0] - - temps[0] = in_data['probe_history']['food'][label] - if temps[0] <= 0: - percents[0] = 0 - elif self.units == 'F': - percents[0] = round((temps[0] / 300) * 100) # F Temp Range [0 - 300F] for probe - else: - percents[0] = round((temps[0] / 150) * 100) # C Temp Range [0 - 150C] for probe - - temps[1] = in_data['notify_targets'][label] - if temps[1] <= 0: - percents[1] = 0 - elif self.units == 'F': - percents[1] = round((temps[1] / 300) * 100) # F Temp Range [0 - 300F] for probe - elif self.units == 'C': - percents[1] = round((temps[1] / 150) * 100) # C Temp Range [0 - 150C] for probe - - # No SetPoint2 on Probes - temps[2] = 0 - percents[2] = 0 - - # Draw the Probe1 Gauge w/Labels - Use Yellow as the SetPoint Color - img = self._draw_gauge(img, position, size, fg_color, bg_color, - percents, temps, label, sp1_color=(255, 255, 0)) - - # Display Icons for Active Outputs - - # Pulse Color for some Icons - if self.inc_pulse_color: - if self.icon_color < 200: - self.icon_color += 20 - else: - self.inc_pulse_color = False - self.icon_color -= 20 - else: - if self.icon_color < 100: - self.inc_pulse_color = True - self.icon_color += 20 - else: - self.icon_color -= 20 - - if status_data['outpins']['fan']: - # F = Fan (Upper Left), position (10,10) - if self.WIDTH == 240: - self._draw_fan_icon(img, (10, 50)) - else: - self._draw_fan_icon(img, (10, 10)) - - if status_data['outpins']['igniter']: - # I = Igniter(Center Right) - if self.WIDTH == 240: - self._draw_ignitor_icon(img, (self.WIDTH - 52, 170)) - else: - self._draw_ignitor_icon(img, (self.WIDTH - 52, 60)) - - if status_data['outpins']['auger']: - # A = Auger (Center Left) - if self.WIDTH == 240: - self._draw_auger_icon(img, (10, 170)) - else: - self._draw_auger_icon(img, (10, 60)) - - # Notification Indicator (Right) - show_notify_indicator = False - notify_count = 0 - for index, item in enumerate(status_data['notify_data']): - if item['req'] and item['type'] != 'hopper': - show_notify_indicator = True - notify_count += 1 - - if status_data['recipe_paused']: - if self.WIDTH == 240: - self._draw_pause_icon(img, (self.WIDTH - 52, 50)) - else: - self._draw_pause_icon(img, (self.WIDTH - 52, 10)) - - elif status_data['recipe']: - if self.WIDTH == 240: - self._draw_recipe_icon(img, (self.WIDTH - 52, 50)) - else: - self._draw_recipe_icon(img, (self.WIDTH - 52, 10)) - - elif show_notify_indicator: - if self.WIDTH == 240: - self._draw_notify_icon(img, (self.WIDTH - 52, 50)) - else: - self._draw_notify_icon(img, (self.WIDTH - 52, 10)) - - if notify_count > 1: - self._text_circle(draw, (self.WIDTH - 24, 40), (22, 22), str(notify_count), fg_color=(255, 255, 255), bg_color=(200, 0, 0)) - - # Smoke Plus Indicator - if status_data['s_plus'] and (status_data['mode'] == 'Smoke' or status_data['mode'] == 'Hold'): - if self.WIDTH == 240: - self._draw_splus_icon(img, (self.WIDTH - 52, 170)) - else: - self._draw_splus_icon(img, (self.WIDTH - 52, 60)) - - # Grill Hopper Level (Lower Center) - text = "Hopper:" + str(status_data['hopper_level']) + "%" - if status_data['hopper_level'] > 70: - hopper_color = (0, 255, 0) - elif status_data['hopper_level'] > 30: - hopper_color = (255, 150, 0) - else: - hopper_color = (255, 0, 0) - if self.WIDTH == 240: - center_point = self.WIDTH // 2, self.HEIGHT - 14 - else: - center_point = self.WIDTH // 2, (self.HEIGHT // 2) + 64 - self._text_rectangle(draw, center_point, text, 16, hopper_color, (0,0,0), hopper_color) - - # Current Mode (Bottom Center) - text = status_data['mode'] # + ' Mode' - if self.WIDTH == 240: - center_point = (self.WIDTH // 2, 22) - else: - center_point = (self.WIDTH // 2, self.HEIGHT - 22) - self._text_rectangle(draw, center_point, text, 32, text_color=(0,0,0), fill_color=(255,255,255), outline_color=(3, 161, 252)) - - # Draw Units Circle - text = f'°{self.units}' - position = ((self.WIDTH // 2) - 13, (self.HEIGHT // 2) + 24) - size = (26, 26) - self._text_circle(draw, position, size, text) - - # Display Countdown for Startup / Reignite / Shutdown / Prime - if status_data['mode'] in ['Startup', 'Reignite', 'Shutdown', 'Prime']: - if status_data['mode'] in ['Startup', 'Reignite']: - duration = status_data['start_duration'] - elif status_data['mode'] in ['Prime']: - duration = status_data['prime_duration'] - else: - duration = status_data['shutdown_duration'] - - countdown = int(duration - (time.time() - status_data['start_time'])) if int(duration - (time.time() - status_data['start_time'])) > 0 else 0 - text = f'{countdown}s' - center_point = (self.WIDTH // 2, self.HEIGHT // 2 - 100) - self._text_rectangle(draw, center_point, text, 26, text_color=(0,200,0), fill_color=(0,0,0), outline_color=(0, 200, 0)) - - # Lid open detection timer display - if status_data['mode'] in ['Hold']: - if status_data['lid_open_detected']: - duration = int(status_data['lid_open_endtime'] - time.time()) if int(status_data['lid_open_endtime'] - time.time()) > 0 else 0 - text = f'Lid Pause {duration}s' - center_point = (self.WIDTH // 2, self.HEIGHT // 2 - 100) - self._text_rectangle(draw, center_point, text, 18, text_color=(0,200,0), fill_color=(0,0,0), outline_color=(0, 200, 0)) - - # Display Final Screen - self._display_canvas(img) - - ''' - ====================== Input & Menu Code ======================== - ''' - def _event_detect(self): - """ - Called to detect input events from buttons, encoder, touch, etc. - This function should be overriden by the inheriting class. - """ - pass - - def _menu_display(self, action): - # If menu is not currently being displayed, check mode and draw menu - if self.menu['current']['mode'] == 'none': - control = read_control() - # If in an inactive mode - if control['mode'] in ['Stop', 'Error', 'Monitor', 'Prime']: - self.menu['current']['mode'] = 'inactive' - elif control['mode'] in ['Recipe']: - self.menu['current']['mode'] = 'active_recipe' - else: # Use the active menu - self.menu['current']['mode'] = 'active' - - self.menu['current']['option'] = 0 # Set the menu option to the very first item in the list - # If selecting the 'grill_hold_value', take action based on button press was - elif self.menu['current']['mode'] == 'grill_hold_value': - if self.units == 'F': - stepValue = 5 # change in temp each time button pressed - minTemp = 120 # minimum temperature set for hold - maxTemp = 500 # maximum temperature set for hold - else: - stepValue = 2 # change in temp each time button pressed - minTemp = 50 # minimum temperature set for hold - maxTemp = 260 # maximum temperature set for hold - - # Speed up step count if input is faster - if self.input_counter < 3: - pass - elif self.input_counter < 7: - stepValue *= 4 - else: - stepValue *= 6 - - if action == 'DOWN': - self.menu['current']['option'] -= stepValue # Step down by stepValue degrees - if self.menu['current']['option'] <= minTemp: - self.menu['current']['option'] = maxTemp # Roll over to maxTemp if you go less than 120. - elif action == 'UP': - self.menu['current']['option'] += stepValue # Step up by stepValue degrees - if self.menu['current']['option'] > maxTemp: - self.menu['current']['option'] = minTemp # Roll over to minTemp if you go greater than 500. - elif action == 'ENTER': - control = read_control() - control['primary_setpoint'] = self.menu['current']['option'] - control['updated'] = True - control['mode'] = 'Hold' - write_control(control, origin='display') - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - # If selecting either active menu items or inactive menu items, take action based on what the button press was - else: - if action == 'DOWN': - self.menu['current']['option'] -= 1 - if self.menu['current']['option'] < 0: # Check to make sure we haven't gone past 0 - self.menu['current']['option'] = len(self.menu[self.menu['current']['mode']]) - 1 - temp_value = self.menu['current']['option'] - temp_mode = self.menu['current']['mode'] - index = 0 - selected = 'undefined' - for item in self.menu[temp_mode]: - if index == temp_value: - selected = item - break - index += 1 - elif action == 'UP': - self.menu['current']['option'] += 1 - # Check to make sure we haven't gone past the end of the menu - if self.menu['current']['option'] == len(self.menu[self.menu['current']['mode']]): - self.menu['current']['option'] = 0 - temp_value = self.menu['current']['option'] - temp_mode = self.menu['current']['mode'] - index = 0 - selected = 'undefined' - for item in self.menu[temp_mode]: - if index == temp_value: - selected = item - break - index += 1 - elif action == 'ENTER': - index = 0 - selected = 'undefined' - for item in self.menu[self.menu['current']['mode']]: - if (index == self.menu['current']['option']): - selected = item - break - index += 1 - # Inactive Mode Items - if selected == 'Startup': - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - control['updated'] = True - control['mode'] = 'Startup' - write_control(control, origin='display') - elif selected == 'Monitor': - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - control['updated'] = True - control['mode'] = 'Monitor' - write_control(control, origin='display') - elif selected == 'Stop': - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - self.clear_display() - control = read_control() - control['updated'] = True - control['mode'] = 'Stop' - write_control(control, origin='display') - # Active Mode - elif selected == 'Shutdown': - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - control['updated'] = True - control['mode'] = 'Shutdown' - write_control(control, origin='display') - elif selected == 'Hold': - self.display_active = True - self.menu['current']['mode'] = 'grill_hold_value' - if self.in_data['primary_setpoint'] == 0: - if self.units == 'F': - self.menu['current']['option'] = 200 # start at 200 for F - else: - self.menu['current']['option'] = 100 # start at 100 for C - else: - self.menu['current']['option'] = self.in_data['primary_setpoint'] - elif selected == 'Smoke': - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - control['updated'] = True - control['mode'] = 'Smoke' - write_control(control, origin='display') - elif selected == 'SmokePlus': - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - if control['s_plus']: - control['s_plus'] = False - else: - control['s_plus'] = True - write_control(control, origin='display') - elif selected == 'Network': - self.display_network() - elif selected == 'Prime': - self.menu['current']['mode'] = 'prime_selection' - self.menu['current']['option'] = 0 - elif 'Prime_' in selected: - control = read_control() - if '50' in selected: - control['prime_amount'] = 25 - elif '25' in selected: - control['prime_amount'] = 25 - else: - control['prime_amount'] = 10 - - if 'Start' in selected: - control['next_mode'] = 'Startup' - else: - control['next_mode'] = 'Stop' - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control['updated'] = True - control['mode'] = 'Prime' - write_control(control, origin='display') - elif 'NextStep' in selected: - self.display_active = True - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - self.menu_active = False - self.menu_time = 0 - control = read_control() - # Check if currently in 'Paused' Status - if 'triggered' in control['recipe']['step_data'] and 'pause' in control['recipe']['step_data']: - if control['recipe']['step_data']['triggered'] and control['recipe']['step_data']['pause']: - # 'Unpause' Recipe - control['recipe']['step_data']['pause'] = False - write_control(control, origin='display') - else: - # User is forcing next step - control['updated'] = True - write_control(control, origin='display') - else: - # User is forcing next step - control['updated'] = True - write_control(control, origin='display') - - # Create canvas - img = Image.new('RGB', (self.WIDTH, self.HEIGHT), color=(0, 0, 0)) - # Set the position & paste background image onto canvas - position = (0, 0) - img.paste(self.background, position) - # Create drawing object - draw = ImageDraw.Draw(img) - - if self.menu['current']['mode'] == 'grill_hold_value': - # Grill Temperature (Large Centered) - font_point_size = 80 if self.WIDTH == 240 else 120 - font = ImageFont.truetype("trebuc.ttf", font_point_size) - text = str(self.menu['current']['option']) - (font_width, font_height) = font.getsize(text) - if self.WIDTH == 240: - draw.text((self.WIDTH // 2 - font_width // 2 - 20, self.HEIGHT // 2.5 - font_height // 2), text, font=font, - fill=(255, 255, 255)) - else: - draw.text((self.WIDTH // 2 - font_width // 2, self.HEIGHT // 3 - font_height // 2), text, font=font, - fill=(255, 255, 255)) - - # Current Mode (Bottom Center) - font = ImageFont.truetype("trebuc.ttf", 36) - text = "Grill Set Point" - (font_width, font_height) = font.getsize(text) - # Draw Black Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(0, 0, 0)) - # Draw White Line/Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, ((self.HEIGHT // 8) * 6) + 2)], - fill=(255, 255, 255)) - # Draw Text - draw.text((self.WIDTH // 2 - font_width // 2, (self.HEIGHT // 8) * 6.25), text, font=font, - fill=(255, 255, 255)) - - elif self.menu['current']['mode'] != 'none': - # Menu Option (Large Top Center) - index = 0 - selected = 'undefined' - for item in self.menu[self.menu['current']['mode']]: - if index == self.menu['current']['option']: - selected = item - break - index += 1 - font_point_size = 80 if self.WIDTH == 240 else 120 - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", font_point_size) - text = self.menu[self.menu['current']['mode']][selected]['icon'] - (font_width, font_height) = font.getsize(text) - draw.text((self.WIDTH // 2 - font_width // 2, self.HEIGHT // 2.5 - font_height // 2), text, font=font, - fill=(255, 255, 255)) - # Draw a Plus Icon over the top of the Smoke Icon - if selected == 'SmokePlus': - font_point_size = 60 if self.WIDTH == 240 else 80 - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", font_point_size) - text = '\uf067' # FontAwesome Icon for PLUS - (font_width, font_height) = font.getsize(text) - draw.text((self.WIDTH // 2 - font_width // 2, self.HEIGHT // 2.5 - font_height // 2), text, font=font, - fill=(0, 0, 0)) - - # Current Mode (Bottom Center) - font = ImageFont.truetype("trebuc.ttf", 36) - text = self.menu[self.menu['current']['mode']][selected]['displaytext'] - (font_width, font_height) = font.getsize(text) - # Draw Black Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, self.HEIGHT)], fill=(0, 0, 0)) - # Draw White Line/Rectangle - draw.rectangle([(0, (self.HEIGHT // 8) * 6), (self.WIDTH, ((self.HEIGHT // 8) * 6) + 2)], - fill=(255, 255, 255)) - # Draw Text - draw.text((self.WIDTH // 2 - font_width // 2, (self.HEIGHT // 8) * 6.25), text, font=font, - fill=(255, 255, 255)) - - # Change color of Arrow for Up / Down when adjusting temperature - up_color = (255, 255, 255) - down_color = (255, 255, 255) - - if action == 'UP': - up_color = (255, 255, 0) - elif action == 'DOWN': - down_color = (255, 255, 0) - - # Up / Down Arrows (Middle Right) - font_point_size = 80 if self.WIDTH == 240 else 60 - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", font_point_size) - text = '\uf0de' # FontAwesome Icon Sort (Up Arrow) - (font_width, font_height) = font.getsize(text) - draw.text(((self.WIDTH - (font_width // 2) ** 1.3), (self.HEIGHT // 2.5 - font_height // 2)), text, - font=font, fill=up_color) - - text = '\uf0dd' # FontAwesome Icon Sort (Down Arrow) - (font_width, font_height) = font.getsize(text) - draw.text(((self.WIDTH - (font_width // 2) ** 1.3), (self.HEIGHT // 2.4 - font_height // 2)), text, - font=font, fill=down_color) - - self._display_canvas(img) - time.sleep(0.05) - - # Up / Down Arrows (Middle Right) - font_point_size = 80 if self.WIDTH == 240 else 60 - font = ImageFont.truetype("static/font/FA-Free-Solid.otf", font_point_size) - text = '\uf0de' # FontAwesome Icon Sort (Up Arrow) - (font_width, font_height) = font.getsize(text) - draw.text(((self.WIDTH - (font_width // 2) ** 1.3), (self.HEIGHT // 2.5 - font_height // 2)), text, - font=font, fill=(255, 255, 255)) - - text = '\uf0dd' # FontAwesome Icon Sort (Down Arrow) - (font_width, font_height) = font.getsize(text) - draw.text(((self.WIDTH - (font_width // 2) ** 1.3), (self.HEIGHT // 2.4 - font_height // 2)), text, - font=font, fill=(255, 255, 255)) - - self._display_canvas(img) - if not self.touch_held: - self.touch_pos = (0,0) - - - ''' - ================ Externally Available Methods ================ - ''' - - def display_status(self, in_data, status_data): - """ - - Updates the current data for the display loop, if in a work mode - """ - self.units = status_data['units'] - self.display_active = True - self.in_data = in_data - self.status_data = status_data - - def display_splash(self): - """ - - Calls Splash Screen - """ - self.display_command = 'splash' - - def clear_display(self): - """ - - Clear display and turn off backlight - """ - self.display_command = 'clear' - - def display_text(self, text): - """ - - Display some text - """ - self.display_command = 'text' - self.display_data = text - - def display_network(self): - """ - - Display Network IP QR Code - """ - self.display_command = 'network' + pass + + def _init_input(self): + ''' + Inheriting classes will override this function to setup the inputs. + ''' + self.input_enabled = False # If the inheriting class does not implement input, then clear this flag + self.input_counter = 0 + + def _display_loop(self): + """ + Main display loop + """ + while True: + time.sleep(0.1) + + def _zero_dash_data(self): + #self.last_in_data = {} + #self.last_status_data = {} + if self.status_data is not None or self.in_data is not None: + self.status_data['mode'] = 'Stop' + for outpin in self.status_data['outpins']: + if outpin != 'pwm': + self.status_data['outpins'][outpin] = False + for probe in self.in_data['P']: + self.in_data['P'][probe] = 0 + for probe in self.in_data['F']: + self.in_data['F'][probe] = 0 + for probe in self.in_data['AUX']: + self.in_data['AUX'][probe] = 0 + + self.in_data['PSP'] = 0 + + for probe in self.in_data['NT']: + self.in_data['NT'][probe] = 0 + + def _store_dash_objects(self): + ''' Store the dash object list so that it does not need to be rebuilt ''' + self.dash_object_list = self.display_object_list.copy() + + def _restore_dash_objects(self): + ''' Restore the dash object list to the main working display_object_list ''' + self.display_object_list = self.dash_object_list.copy() + + ''' + ============== Input Callbacks ============= + + Inheriting classes will override these functions for all inputs. + ''' + def _enter_callback(self): + ''' + Inheriting classes will override this function. + ''' + pass + + def _up_callback(self, held=False): + ''' + Inheriting classes will override this function to clear the display device. + ''' + pass + + def _down_callback(self, held=False): + ''' + Inheriting classes will override this function to clear the display device. + ''' + pass + + ''' + ============== Graphics / Display / Draw Methods ============= + ''' + def _init_assets(self): + self._init_background() + self._init_splash() + + def _init_background(self): + background_image_path = self.display_data['metadata']['dash_background'] + self.background = Image.open(background_image_path) + self.background = self.background.resize((self.WIDTH, self.HEIGHT)) + + def _init_splash(self): + splash_image_path = self.display_data['metadata']['splash_image'] + self.splash = Image.open(splash_image_path) + self.splash = self.splash.resize((self.WIDTH, self.HEIGHT)) + + def _wake_display(self): + ''' + Inheriting classes will override this function to wake the display device. + ''' + pass + + def _sleep_display(self): + ''' + Inheriting classes will override this function to sleep the display device. + ''' + pass + + def _display_clear(self): + ''' + Inheriting classes will override this function to clear the display device. + ''' + pass + + def _display_canvas(self, canvas): + ''' + Inheriting classes will override this function to show the canvas on the display device. + ''' + pass + + def _display_splash(self): + ''' + Inheriting classes will override this function to display the splash screen. + ''' + pass + + def _display_background(self): + ''' + Inheriting classes will override this function to display the stored background image. + ''' + pass + + def _display_menu_background(self): + ''' + Inheriting classes will override this function to display menu background + ''' + pass + + def _build_objects(self, background): + ''' + Inheriting classes may override this function to ensure the right object type is loaded + ''' + self.display_object_list = [] + + if self.display_active in ['home', 'dash']: + section_data = self.display_data[self.display_active] + elif 'menu_' in self.display_active: + section_data = [self.display_data['menus'][self.display_active.replace('menu_', '')]] + elif 'input_' in self.display_active: + section_data = [self.display_data['input'][self.display_active.replace('input_', '')]] + section_data[0]['data']['origin'] = self.input_origin + else: + return + + for object_data in section_data: + self.display_object_list.append(FlexObjPIL(object_data['type'], object_data, background)) + + def _configure_dash(self): + ''' Build Food Probe Map ''' + num_food_probes = min(len(self.config['probe_info']['food']), self.display_data['metadata']['max_food_probes']) + self.food_probe_label_map = {} + self.food_probe_name_map = {} + for index in range(num_food_probes): + self.food_probe_label_map[f'food_probe_gauge_{index}'] = self.config['probe_info']['food'][index]['label'] + self.food_probe_name_map[f'food_probe_gauge_{index}'] = self.config['probe_info']['food'][index]['name'] + + ''' Remove Unused Food Probes & Rename Used Food Probes''' + display_data_dash_list = [] + for object in self.display_data['dash']: + if 'food_probe_gauge_' in object['name'] and object['name'] not in list(self.food_probe_label_map.keys()): + pass + else: + if 'food_probe_gauge_' in object['name']: + ''' Rename Displayed Food Probes ''' + object['label'] = self.food_probe_name_map[object['name']] + object['units'] = self.units + object['button_value'] = [object['label']] + elif object['name'] == 'primary_gauge': + object['label'] = self.config['probe_info']['primary']['name'] + object['button_value'] = [object['label']] + + display_data_dash_list.append(object) + + self.display_data['dash'] = display_data_dash_list + + def _build_dash_map(self): + ''' Setup dash object mapping ''' + self.dash_map = {} + + #print('Setting up Dash Map:') + for index, object in enumerate(self.display_object_list): + objectData = object.get_object_data() + self.dash_map[objectData['name']] = index + #print(f' - Index: {index}, Maps to: {objectData["name"]}') + + def _update_dash_objects(self): + + if self.in_data is not None and self.status_data is not None: + ''' Update Mode Bar and Control Panel ''' + if (self.status_data['mode'] != self.last_status_data.get('mode', 'None')) or \ + (self.status_data['recipe_paused'] != self.last_status_data.get('recipe_paused', 'None')): + + ''' Disable Screen Timeout When not in Stop Mode ''' + if self.status_data['mode'] not in ['Stop']: + self.display_timeout = None + else: + self.display_timeout = time.time() + self.TIMEOUT + + ''' Mode Bar Update ''' + if 'mode_bar' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['mode_bar']].get_object_data() + + if self.status_data['recipe'] and self.status_data['mode'] != 'Shutdown': + object_data['text'] = 'Recipe: ' + self.status_data['mode'] + else: + object_data['text'] = self.status_data['mode'] + self.display_object_list[self.dash_map['mode_bar']].update_object_data(object_data) + ''' Control Panel Update ''' + if 'control_panel' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['control_panel']].get_object_data() + object_data['button_active'] = self.status_data['mode'] + if self.status_data['recipe']: + ''' Recipe Mode ''' + list_item = 'cmd_none' + type_item = 'Error' + if self.status_data['mode'] in ['Startup', 'Reignite']: + type_item = 'Startup' + elif self.status_data['mode'] == 'Smoke': + type_item = 'Smoke' + elif self.status_data['mode'] == 'Hold': + type_item = 'Hold' + elif self.status_data['mode'] == 'Shutdown': + type_item = 'None' + object_data['button_list'] = ['cmd_next_step', list_item, 'cmd_stop', 'cmd_shutdown'] + object_data['button_type'] = ['Next', type_item, 'Stop', 'Shutdown'] + if self.status_data['recipe_paused']: + object_data['button_active'] = 'Next' + elif self.status_data['mode'] in ['Startup', 'Reignite']: + ''' Startup Mode ''' + object_data['button_list'] = ['cmd_startup', 'cmd_smoke', 'input_hold', 'cmd_stop'] + object_data['button_type'] = ['Startup', 'Smoke', 'Hold', 'Stop'] + elif self.status_data['mode'] in ['Smoke', 'Hold', 'Shutdown']: + ''' Smoke, Hold or Shutdown Modes ''' + object_data['button_list'] = ['cmd_smoke', 'input_hold', 'cmd_stop', 'cmd_shutdown'] + object_data['button_type'] = ['Smoke', 'Hold', 'Stop', 'Shutdown'] + else: + ''' Stopped, Prime, Monitor Modes ''' + object_data['button_list'] = ['menu_prime', 'menu_startup', 'cmd_monitor', 'cmd_stop'] + object_data['button_type'] = ['Prime', 'Startup', 'Monitor', 'Stop'] + + self.display_object_list[self.dash_map['control_panel']].update_object_data(object_data) + + if self.last_in_data != {}: + ''' Update Primary Gauge Values ''' + primary_key = list(self.in_data['P'].keys())[0] # Get the key for the primary gauge + if (self.in_data['P'] != self.last_in_data['P']) or \ + (self.in_data['PSP'] != self.last_in_data['PSP']) or \ + (self.in_data['NT'][primary_key] != self.last_in_data['NT'].get(primary_key)): + + ''' Update the Primary Gauge ''' + object_data = self.display_object_list[self.dash_map['primary_gauge']].get_object_data() + object_data['temps'][0] = self.in_data['P'][primary_key] + object_data['temps'][1] = self.in_data['NT'][primary_key] + object_data['temps'][2] = self.in_data['PSP'] + object_data['units'] = self.units + #object_data['label'] = primary_key + self.display_object_list[self.dash_map['primary_gauge']].update_object_data(object_data) + + ''' Update Food Probe Gauges and Values ''' + food_gauge_keys = list(self.food_probe_label_map.keys()) + for gauge in food_gauge_keys: + key = self.food_probe_label_map[gauge] + if self.last_in_data['F'][key] != \ + self.in_data['F'][key] or \ + self.last_in_data['NT'][key] != \ + self.in_data['NT'][key]: + + ''' Update this food gauge ''' + object_data = self.display_object_list[self.dash_map[gauge]].get_object_data() + object_data['temps'][0] = self.in_data['F'][key] + object_data['temps'][1] = self.in_data['NT'][key] + object_data['temps'][2] = 0 # There is no set temp for food probes + object_data['units'] = self.units + self.display_object_list[self.dash_map[gauge]].update_object_data(object_data) + + ''' Update Output Status Icons ''' + if self.last_status_data.get('outpins') is None: + self.last_status_data['outpins'] = self.status_data['outpins'].copy() + for output in self.last_status_data['outpins']: + self.last_status_data['outpins'][output] = True if self.status_data['outpins'][output] == False else False + for output in self.status_data['outpins']: + if self.status_data['outpins'][output] != self.last_status_data['outpins'].get(output): + if output == 'auger' and 'auger_status' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['auger_status']].get_object_data() + object_data['animation_enabled'] = True if self.status_data['outpins'][output] else False + object_data['active'] = True if self.status_data['outpins'][output] else False + self.display_object_list[self.dash_map['auger_status']].update_object_data(object_data) + if output == 'fan' and 'fan_status' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['fan_status']].get_object_data() + object_data['animation_enabled'] = True if self.status_data['outpins'][output] else False + object_data['active'] = True if self.status_data['outpins'][output] else False + self.display_object_list[self.dash_map['fan_status']].update_object_data(object_data) + if output == 'igniter' and 'igniter_status' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['igniter_status']].get_object_data() + object_data['animation_enabled'] = True if self.status_data['outpins'][output] else False + object_data['active'] = True if self.status_data['outpins'][output] else False + self.display_object_list[self.dash_map['igniter_status']].update_object_data(object_data) + + ''' Update Timer Output ''' + if self.status_data['mode'] in ['Prime', 'Startup', 'Reignite', 'Shutdown']: + if self.status_data['mode'] in ['Startup', 'Reignite']: + duration = self.status_data['start_duration'] + elif self.status_data['mode'] in ['Prime']: + duration = self.status_data['prime_duration'] + else: + duration = self.status_data['shutdown_duration'] + + countdown = int(duration - (time.time() - self.status_data['start_time'])) if int(duration - (time.time() - self.status_data['start_time'])) > 0 else 0 + if 'timer' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['timer']].get_object_data() + + if countdown != object_data['data']['seconds']: + object_data['data']['seconds'] = countdown + object_data['label'] = 'Timer' + self.display_object_list[self.dash_map['timer']].update_object_data(object_data) + + elif self.status_data['mode'] in ['Hold'] and self.status_data['lid_open_detected']: + ''' In Hold Mode, use timer for lid open detection ''' + countdown = int(self.status_data['lid_open_endtime'] - time.time()) if int(self.status_data['lid_open_endtime'] - time.time()) > 0 else 0 + if 'timer' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['timer']].get_object_data() + if countdown != object_data['data']['seconds']: + object_data['data']['seconds'] = countdown + object_data['label'] = 'Lid Pause' + self.display_object_list[self.dash_map['timer']].update_object_data(object_data) + + else: + ''' Clear the timer in other modes. ''' + if 'timer' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['timer']].get_object_data() + if object_data['data']['seconds'] != 0: + object_data['data']['seconds'] = 0 + self.display_object_list[self.dash_map['timer']].update_object_data(object_data) + + ''' In Hold Mode, Check Lid Indicator ''' + if self.status_data['mode'] in ['Hold'] and self.last_status_data['lid_open_detected'] != self.status_data['lid_open_detected'] \ + and 'lid_indicator' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['lid_indicator']].get_object_data() + if self.status_data['lid_open_detected']: + object_data['active'] = True + else: + object_data['active'] = False + self.display_object_list[self.dash_map['lid_indicator']].update_object_data(object_data) + + ''' Update PMode ''' + if self.status_data['mode'] in ['Startup', 'Reignite', 'Smoke'] and \ + ((self.status_data['mode'] != self.last_status_data.get('mode', 'None')) or \ + (self.status_data['p_mode'] != self.last_status_data.get('p_mode', 'None'))) and \ + 'p_mode' in self.dash_map.keys(): + + object_data = self.display_object_list[self.dash_map['p_mode']].get_object_data() + object_data['active'] = True + object_data['data']['pmode'] = self.status_data['p_mode'] + self.display_object_list[self.dash_map['p_mode']].update_object_data(object_data) + + elif self.status_data['mode'] != self.last_status_data.get('mode', 'None') and 'p_mode' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['p_mode']].get_object_data() + object_data['active'] = False + self.display_object_list[self.dash_map['p_mode']].update_object_data(object_data) + + ''' Update Smoke Plus ''' + if self.status_data['s_plus'] != self.last_status_data.get('s_plus', None) and 'smoke_plus' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['smoke_plus']].get_object_data() + + object_data['active'] = self.status_data['s_plus'] + object_data['button_value'][0] = "off" if self.status_data['s_plus'] else "on" + + self.display_object_list[self.dash_map['smoke_plus']].update_object_data(object_data) + + ''' Update Hopper Info ''' + if self.status_data['hopper_level'] != self.last_status_data.get('hopper_level', None) and 'hopper' in self.dash_map.keys(): + object_data = self.display_object_list[self.dash_map['hopper']].get_object_data() + object_data['data']['level'] = max(self.status_data['hopper_level'], 0) + object_data['data']['level'] = min(object_data['data']['level'], 100) + + self.display_object_list[self.dash_map['hopper']].update_object_data(object_data) + + ''' After all the updates, update the last states/data ''' + self.last_in_data = self.in_data.copy() + self.last_status_data = self.status_data.copy() + + def _update_input_objects(self): + ''' + for index, object in enumerate(self.display_object_list): + objectData = object.get_object_data() + if objectData['data']['input'] != '': + self.display_object_list[index].update_object_data(objectData) + ''' + pass + + def _animate_objects(self): + for object in self.display_object_list: + objectData = object.get_object_data() + objectState = object.get_object_state() + + if objectData['animation_enabled'] and objectState['animation_active']: + object_image = object.update_object_data() + self.display_updated = True + self.display_surface.blit(object_image, objectData['position']) + else: + object_image = object.get_object_surface() + self.display_surface.blit(object_image, objectData['position']) + + + ''' + ====================== Input/Event Handling ======================== + ''' + def _fetch_data(self): + """ + - Updates the current data for the display loop, if in a work mode + """ + if self.in_data is None: + self.last_in_data = {} + self.in_data = read_current() + + if self.status_data is None: + self.last_status_data = {} + self.status_data = read_status() + + self.units = self.status_data['units'] + + ''' Wake the display to the dash if it's currently off ''' + if self.display_active == None and self.status_data['mode'] != 'Stop': + self.display_active = 'dash' + self.display_init = True + self._wake_display() + self.display_timeout = None + + def _event_detect(self): + """ + Called to detect input events from buttons, encoder, touch, etc. + This function should be overridden by the inheriting class. + """ + pass + + def _command_handler(self): + ''' + Called to handle commands + ''' + #print(' > Command Handler Called < ') + if 'monitor' in self.command: + data = { + 'updated' : True, + 'mode' : 'Monitor' + } + write_control(data, origin='display') + #print('Sent Monitor Mode Command!') + + if 'startup' in self.command: + data = { + 'updated' : True, + 'mode' : 'Startup' + } + write_control(data, origin='display') + #print('Sent Startup Mode Command!') + self.display_active = 'dash' + self.display_init = True + + if 'smoke' in self.command: + data = { + 'updated' : True, + 'mode' : 'Smoke' + } + write_control(data, origin='display') + + if 'hold' in self.command: + ''' Set hold target for primary probe ''' + primary_setpoint = 0 + for pointer, object in enumerate(self.display_object_list): + objectData = object.get_object_data() + if objectData['data'].get('value', False): + primary_setpoint = objectData['data'].get('value', False) + break + + if primary_setpoint: + data = { + 'updated' : True, + 'mode' : 'Hold', + 'primary_setpoint' : primary_setpoint + } + write_control(data, origin='display') + self.display_active = 'dash' + self.display_init = True + + if 'notify' in self.command: + ''' Set notification targets for probes/grill ''' + notify_target = 0 + for pointer, object in enumerate(self.display_object_list): + objectData = object.get_object_data() + if objectData['data'].get('value', False): + notify_target = objectData['data'].get('value', False) + break + + control = read_control() + for index, notify_source in enumerate(control['notify_data']): + if notify_source['name'] == self.input_origin: + control['notify_data'][index]['target'] = notify_target + control['notify_data'][index]['req'] = True if notify_target else False + break + + data = { + 'notify_data' : control['notify_data'], + } + write_control(data, origin='display') + + self.input_origin = None + self.display_active = 'dash' + self.display_init = True + + if 'shutdown' in self.command: + data = { + 'updated' : True, + 'mode' : 'Shutdown', + } + write_control(data, origin='display') + + if 'stop' in self.command: + data = { + 'updated' : True, + 'mode' : 'Stop', + } + write_control(data, origin='display') + + self._init_framework() + self._zero_dash_data() + self.display_active = 'dash' + self.display_init = True + self.display_timeout = time.time() + self.TIMEOUT + + if 'splus' in self.command: + enable = True if self.command_data == "on" else False + data = { + 's_plus' : enable, + } + write_control(data, origin='display') + + if 'primestartup' in self.command: + data = { + 'updated' : True, + 'mode' : 'Prime', + 'prime_amount' : self.command_data, + 'next_mode' : 'Startup' + } + write_control(data, origin='display') + self.display_active = 'dash' + self.display_init = True + + if 'primeonly' in self.command: + data = { + 'updated' : True, + 'mode' : 'Prime', + 'prime_amount' : self.command_data, + 'next_mode' : 'Stop' + } + write_control(data, origin='display') + self.display_active = 'dash' + self.display_init = True + + if 'pmode' in self.command: + # TODO : Change to API Call + settings = read_settings() + settings['cycle_data']['PMode'] = self.command_data + write_settings(settings) + data = { + 'settings_update' : True, + } + write_control(data, origin='display') + + self.display_active = 'dash' + self.display_init = True + + if 'next_step' in self.command: + data = read_control() + # Check if currently in 'Paused' Status + if 'triggered' in data['recipe']['step_data'] and 'pause' in data['recipe']['step_data']: + if data['recipe']['step_data']['triggered'] and data['recipe']['step_data']['pause']: + # 'Unpause' Recipe + data['recipe']['step_data']['pause'] = False + write_control(data, origin='display') + else: + # User is forcing next step + data['updated'] = True + write_control(data, origin='display') + else: + # User is forcing next step + data['updated'] = True + write_control(data, origin='display') + + if 'reboot' in self.command: + data = { + 'updated' : True, + 'mode' : 'Stop', + } + write_control(data, origin='display') + if self.real_hardware: + os.system('sleep 3 && sudo reboot &') + else: + pass + self.display_active = 'dash' + self.display_init = True + self.display_loop_active = False + + if 'poweroff' in self.command: + data = { + 'updated' : True, + 'mode' : 'Stop', + } + write_control(data, origin='display') + if self.real_hardware: + os.system('sleep 3 && sudo shutdown -h now &') + else: + pass + self.display_active = 'dash' + self.display_init = True + self.display_loop_active = False + + if 'restart' in self.command: + data = { + 'updated' : True, + 'mode' : 'Stop', + } + write_control(data, origin='display') + if self.real_hardware: + os.system('sleep 3 && sudo service supervisor restart &') + else: + pass + self.display_active = 'dash' + self.display_init = True + self.display_loop_active = False + + if 'hopper' in self.command: + data = { + 'hopper_check' : True + } + write_control(data, origin='display') + + if 'none' in self.command: + pass + + self.command = None + + + ''' + ================ Externally Available Methods ================ + ''' + + def display_status(self, in_data, status_data): + """ + Stub from legacy implementation + """ + pass + + def display_splash(self): + """ + - Calls Splash Screen + This function is currently unused and is only provided to maintain compatibility. + """ + pass + + def clear_display(self): + """ + - Clear display and turn off backlight + This function is currently unused and is only provided to maintain compatibility. + """ + #print('Clear Display Requested.') + pass + + def display_text(self, text): + """ + - Display some text + This function is currently unused and is only provided to maintain compatibility. + """ + #print(f'Display Text: {text}') + pass + + def display_network(self): + """ + - Display Network IP QR Code + This function is currently unused and is only provided to maintain compatibility. + """ + pass \ No newline at end of file diff --git a/display/dsi_800x480t.json b/display/dsi_800x480t.json new file mode 100644 index 00000000..8e1a3096 --- /dev/null +++ b/display/dsi_800x480t.json @@ -0,0 +1,518 @@ +{ + "metadata" : { + "name" : "dsi_800x480t", + "screen_width" : 800, + "screen_height" : 480, + "framerate" : 20, + "dash_background" : "./static/img/display/background.png", + "splash_image" : "./static/img/display/splash_800x480.png", + "splash_delay" : 500, + "max_food_probes" : 5 + }, + "home" : [], + "dash" : [ + { + "name" : "primary_gauge", + "type" : "gauge", + "position" : [225, 50], + "animation_enabled" : true, + "size" : [350, 350], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Primary", + "units" : "F", + "max_temp" : 600, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "control_panel", + "type" : "control_panel", + "position" : [200, 380], + "animation_enabled" : false, + "glow" : true, + "size" : [400, 100], + "label" : "control_panel", + "data" : {}, + "button_list" : ["menu_prime", "menu_startup", "cmd_monitor", "cmd_stop"], + "button_type" : ["Prime", "Startup", "Monitor", "Stop"], + "button_active" : "Stop", + "touch_areas" : [] + }, + { + "name" : "food_probe_gauge_0", + "type" : "gauge_compact", + "position" : [0, 55], + "animation_enabled" : false, + "size" : [220, 110], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Food Probe", + "units" : "F", + "max_temp" : 300, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "food_probe_gauge_1", + "type" : "gauge_compact", + "position" : [580, 55], + "animation_enabled" : false, + "size" : [220, 110], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Food Probe", + "units" : "F", + "max_temp" : 300, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "food_probe_gauge_2", + "type" : "gauge_compact", + "position" : [0, 160], + "animation_enabled" : false, + "size" : [220, 110], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Food Probe", + "units" : "F", + "max_temp" : 300, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "food_probe_gauge_3", + "type" : "gauge_compact", + "position" : [580, 160], + "animation_enabled" : false, + "size" : [220, 110], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Food Probe", + "units" : "F", + "max_temp" : 300, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "food_probe_gauge_4", + "type" : "gauge_compact", + "position" : [0, 275], + "animation_enabled" : false, + "size" : [220, 110], + "fg_color" : [255, 255, 255, 255], + "bg_color" : [25, 25, 25, 255], + "glow" : true, + "font" : "trebuc.ttf", + "temps" : [0, 0, 0], + "label" : "Food Probe", + "units" : "F", + "max_temp" : 300, + "data" : {}, + "button_list" : ["input_notify"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "mode_bar", + "type" : "mode_bar", + "position" : [200, 0], + "animation_enabled" : false, + "size" : [400, 60], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "mode_bar", + "text" : "Stop", + "data" : {}, + "touch_areas" : [] + }, + { + "name" : "fan_status", + "type" : "status_icon", + "position" : [10, 5], + "animation_enabled" : false, + "size" : [50, 50], + "glow" : true, + "icon" : "Fan", + "label" : "fan_status", + "active" : false, + "inactive_color" : [128,128,128,128], + "active_color" : [255,255,255,255], + "rotation" : 0, + "data" : {}, + "touch_areas" : [] + }, + { + "name" : "auger_status", + "type" : "status_icon", + "position" : [85, 5], + "animation_enabled" : false, + "size" : [50, 50], + "glow" : true, + "icon" : "Auger", + "label" : "auger_status", + "active" : false, + "inactive_color" : [128,128,128,128], + "active_color" : [255,255,255,255], + "rotation" : 0, + "data" : {}, + "touch_areas" : [] + }, + { + "name" : "igniter_status", + "type" : "status_icon", + "position" : [150, 5], + "animation_enabled" : false, + "size" : [50, 50], + "glow" : true, + "icon" : "Igniter", + "label" : "igniter_status", + "active" : false, + "inactive_color" : [128,128,128,128], + "active_color" : [255,255,255,255], + "rotation" : 0, + "data" : {}, + "touch_areas" : [] + }, + { + "name" : "menu_icon", + "type" : "menu_icon", + "position" : [740, 5], + "animation_enabled" : false, + "size" : [50, 50], + "glow" : true, + "icon" : "Hamburger", + "label" : "menu_icon", + "color" : [255,255,255,255], + "button_list" : ["menu_main"], + "button_value" : [], + "data" : {}, + "touch_areas" : [] + }, + { + "name" : "timer", + "type" : "timer", + "position" : [0, 380], + "animation_enabled" : false, + "size" : [200, 100], + "glow" : true, + "label" : "Timer", + "color" : [255,255,255,255], + "button_list" : [], + "button_value" : [], + "data" : { + "seconds" : 0 + }, + "touch_areas" : [] + }, + { + "name" : "lid_indicator", + "type" : "alert", + "position" : [600, 380], + "animation_enabled" : false, + "size" : [200, 100], + "glow" : true, + "label" : "Lid Open Detected", + "active" : false, + "color" : [0,200,0,255], + "data" : { + "text" : [ + "Lid Open", + "Detected" + ] + }, + "button_list" : [], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "p_mode", + "type" : "p_mode_control", + "position" : [600, 380], + "animation_enabled" : false, + "size" : [200, 100], + "glow" : true, + "label" : "P-Mode", + "active" : false, + "color" : [255,255,255,255], + "data" : { + "pmode" : 0 + }, + "button_list" : ["menu_pmode"], + "button_value" : [], + "touch_areas" : [] + }, + { + "name" : "smoke_plus", + "type" : "splus_control", + "position" : [650, 0], + "animation_enabled" : false, + "size" : [60, 60], + "glow" : false, + "label" : "P-Mode", + "active" : false, + "color" : [200,0,200,225], + "data" : {}, + "button_list" : ["cmd_splus"], + "touch_areas" : [], + "button_value" : ["on"] + }, + { + "name" : "hopper", + "type" : "hopper_status", + "position" : [580, 275], + "animation_enabled" : false, + "size" : [220, 110], + "glow" : true, + "label" : "Hopper Level", + "active" : false, + "color" : [255,255,255,255], + "color_levels" : [ + [225, 50, 50, 255], + [225, 150, 50, 255], + [225, 225, 50, 255], + [50, 225, 50, 255], + [255, 255, 255, 255] + ], + "data" : { + "level" : 100 + }, + "button_list" : ["cmd_hopper_level"], + "button_value" : [], + "touch_areas" : [] + } + ], + "menus" : { + "main" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "main_menu", + "title_text" : "Main Menu", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "menu_qrcode", "menu_main_reboot", "menu_main_power_off"], + "button_text" : ["Close Menu", "Show QR Code", "Reboot System", "Power Off System"], + "button_value" : [], + "touch_areas" : [] + }, + "qrcode" : { + "type" : "qrcode", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "qr_code", + "ip_address" : "0.0.0.0", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close"], + "button_value" : [], + "touch_areas" : [] + }, + "main_reboot" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "main_menu_reboot", + "title_text" : "Reboot the System?", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_reboot", "menu_close"], + "button_text" : ["Close Menu", "Yes", "No"], + "button_value" : [], + "touch_areas" : [] + }, + "main_power_off" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "main_menu_power_off", + "title_text" : "Power Off the System?", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_poweroff", "menu_close"], + "button_text" : ["Close Menu", "Yes", "No"], + "button_value" : [], + "touch_areas" : [] + }, + "prime" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "menu_prime_start", + "title_text" : "Startup after priming?", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "menu_prime_startup", "menu_prime_only"], + "button_text" : ["Close Menu", "Yes", "No"], + "button_value" : [], + "touch_areas" : [] + }, + "prime_startup" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "menu_prime", + "title_text" : "Select Amount to Prime:", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_primestartup", "cmd_primestartup", "cmd_primestartup"], + "button_text" : ["Close Menu", "10 grams", "25 grams", "50 grams"], + "button_value" : [0, 10, 25, 50], + "touch_areas" : [] + }, + "prime_only" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "menu_prime", + "title_text" : "Select Amount to Prime:", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_primeonly", "cmd_primeonly", "cmd_primeonly"], + "button_text" : ["Close Menu", "10 grams", "25 grams", "50 grams"], + "button_value" : [0, 10, 25, 50], + "touch_areas" : [] + }, + "startup" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "menu_startup", + "title_text" : "Do you want to start up?", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_startup", "menu_close"], + "button_text" : ["Close Menu", "Yes", "No"], + "button_value" : [], + "touch_areas" : [] + }, + "pmode" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "menu_startup", + "title_text" : "Select a PMode:", + "color" : [255,255,255,255], + "data" : {}, + "button_list" : ["menu_close", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode", "cmd_pmode"], + "button_text" : ["Close Menu", "PMode 0", "PMode 1", "PMode 2", "PMode 3", "PMode 4", "PMode 5", "PMode 6", "PMode 7", "PMode 8", "PMode 9"], + "button_value" : [0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + "touch_areas" : [] + }, + "message" : { + "type" : "menu", + "position" : [100, 40], + "animation_enabled" : false, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "message", + "title_text" : "Message", + "color" : [255,255,255,255], + "data" : { + "text" : "" + }, + "button_list" : ["menu_close", "menu_close"], + "button_text" : ["Close Menu", "OK"], + "button_value" : [], + "touch_areas" : [] + } + }, + "input" : { + "hold" : { + "type" : "input_number", + "position" : [100, 40], + "animation_enabled" : true, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "input_hold", + "title_text" : "Enter Hold Temperature:", + "color" : [255,255,255,255], + "command" : "cmd_hold", + "data" : { + "input": "", + "value" : 200, + "origin" : "" + }, + "step" : 5, + "button_list" : [], + "button_value" : [], + "touch_areas" : [] + }, + "notify" : { + "type" : "input_number", + "position" : [100, 40], + "animation_enabled" : true, + "size" : [600, 400], + "glow" : true, + "font" : "trebuc.ttf", + "label" : "input_notify", + "title_text" : "Enter Notify Temperature:", + "color" : [255,255,255,255], + "command" : "cmd_notify", + "data" : { + "input": "", + "value" : 165, + "origin" : "" + }, + "step" : 5, + "button_list" : [], + "button_value" : [], + "touch_areas" : [] + } + } +} diff --git a/display/dsi_800x480t.py b/display/dsi_800x480t.py new file mode 100644 index 00000000..2129d781 --- /dev/null +++ b/display/dsi_800x480t.py @@ -0,0 +1,357 @@ +#!/usr/bin/env python3 +''' +***************************************** +PiFire Display Interface Library +***************************************** + + Description: This library supports using pygame + with a DSI attached touch display like the official + Raspberry Pi 7 inch DSI attached display. + + This version supports mouse for development. + +***************************************** +''' + +''' + Imported Libraries +''' +import time +import multiprocessing +import pygame +from PIL import Image, ImageFilter +from display.base_flex import DisplayBase +from display.flexobject_pygame import FlexObjectPygame as FlexObjPygame + +''' +Dummy backlight class for prototyping +''' +class DummyBacklight(): + def __init__(self): + self.brightness = 100 + self.power = True + self.fade_duration = 1 + +''' +Display class definition +''' +class Display(DisplayBase): + + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + config['display_data_filename'] = "./display/dsi_800x480t.json" + super().__init__(dev_pins, buttonslevel, rotation, units, config) + + def _init_display_device(self): + ''' Init backlight ''' + if self.real_hardware: + # Use the rpi-backlight module if running on the RasPi + from rpi_backlight import Backlight + self.backlight = Backlight() + else: + # Else use a fake module class for backlight + self.backlight = DummyBacklight() + + self._wake_display() + + # Setup & Start Display Loop Worker + display_worker = multiprocessing.Process(target=self._display_loop) + display_worker.start() + + def _init_input(self): + self.input_enabled = True + self.input_event = None + self.touch_pos = (0,0) + + def _display_loop(self): + """ + Main display loop worker + """ + # Init Device + pygame.init() + # Set the pygame window name (for debug) + pygame.display.set_caption('PiFire Device Display') + # Create Display Surface + + if self.real_hardware: + flags = pygame.FULLSCREEN | pygame.DOUBLEBUF + self.display_surface = pygame.display.set_mode((0, 0), flags) + pygame.mouse.set_visible(False) # make mouse pointer invisible + else: + self.display_surface = pygame.display.set_mode(size=(self.WIDTH, self.HEIGHT), flags=pygame.SHOWN) + + self.clock = pygame.time.Clock() + + self.display_loop_active = True + + ''' Display the Splash Screen on Startup ''' + self._display_splash() + pygame.time.delay(self.SPLASH_DELAY) # Hold splash screen for designated time + self._display_clear() + + self.command = None + self.display_active = None + self.display_timeout = None + self.display_init = True + self.display_updated = False + + self.dash_object_list = [] + + refresh_data = 0 + + ''' Display Loop ''' + while self.display_loop_active: + ''' Fetch display data every 200ms ''' + now = time.time() + if now - refresh_data > 0.2: + self._fetch_data() + refresh_data = now + + ''' Poll for PyGame Events ''' + for event in pygame.event.get(): + if event.type == pygame.QUIT: + self.display_loop_active = False + break + # Check for mouse inputs + elif event.type == pygame.MOUSEBUTTONDOWN: + mouse_x, mouse_y = pygame.mouse.get_pos() + #print(f'{mouse_x}, {mouse_y}') + self.touch_pos = (mouse_x, mouse_y) + self.touch_held = True + elif event.type == pygame.MOUSEBUTTONUP: + self.touch_held = False + + # Check for touch inputs + elif event.type == pygame.FINGERDOWN: + touch_x = int(event.x * self.display_surface.get_width()) + touch_y = int(event.y * self.display_surface.get_height()) + #print(f'{touch_x}, {touch_y}') + self.touch_pos = (touch_x, touch_y) + self.touch_held = True + elif event.type == pygame.FINGERUP: + self.touch_held = False + + ''' Check for pressed keys ''' + keys = pygame.key.get_pressed() + if keys[pygame.K_UP]: + self.input_event = 'UP' + elif keys[pygame.K_DOWN]: + self.input_event = 'DOWN' + elif keys[pygame.K_RETURN]: + self.input_event = 'ENTER' + elif keys[pygame.K_x] or keys[pygame.K_q]: + self.display_loop_active = False + break + elif self.touch_pos != (0,0): + self.input_event = 'TOUCH' + + ''' Normal display loop''' + self._event_detect() + + if self.display_active != None: + + if self.display_timeout: + if time.time() > self.display_timeout: + self.display_timeout = None + self.display_active = None + self.display_init = True + + if self.display_active == 'home': + if self.display_init: + ''' Initialize Home Screen ''' + self._display_background() + self._build_objects(self.background) + self.display_init = False + self.display_updated = True + + elif self.display_active == 'dash': + if self.display_init: + ''' Initialize Dash Screen ''' + self._display_background() + if self.dash_object_list == []: + self._init_dash() + self._restore_dash_objects() + self._update_dash_objects() + self.display_init = False + self.display_updated = True + else: + self._update_dash_objects() + + elif self.display_active is not None: + if (('menu_' in self.display_active) or ('input_' in self.display_active)) and self.display_init: + ''' Initialize Menu / Input Dialog ''' + self._display_menu_background() + self._build_objects(self.menu_background) + self.display_init = False + self.display_updated = True + + ''' Perform any animations that need to be displayed. ''' + self._animate_objects() + + if self.display_updated: + self._display_canvas() + self.display_update = False + + else: + if self.display_init: + self._display_clear() + self.display_init = False + if not self.HOME_ENABLED: + self.display_active = 'dash' + self._init_dash() + self.display_active = None + + self.clock.tick(self.FRAMERATE) + + pygame.quit() + + ''' + ============== Graphics / Display / Draw Methods ============= + ''' + + def _init_background(self): + super()._init_background() + ''' Convert image to PyGame surface ''' + strFormat = self.background.mode + size = self.background.size + raw_str = self.background.tobytes("raw", strFormat) + self.background_surface = pygame.image.fromstring(raw_str, size, strFormat) + + def _init_splash(self): + super()._init_splash() + ''' Convert image to PyGame surface ''' + strFormat = self.splash.mode + size = self.splash.size + raw_str = self.splash.tobytes("raw", strFormat) + self.splash = pygame.image.fromstring(raw_str, size, strFormat) + + def _wake_display(self): + self.backlight.power = True + self.backlight.brightness = 100 + self.backlight.fade_duration = 1 + + def _sleep_display(self): + self.backlight.fade_duration = 1 + self.backlight.brightness = 0 + pygame.time.delay(1000) # give time for the screen to fade + self.backlight.power = False + + def _display_clear(self): + self.eventLogger.info('Screen Cleared.') + self._sleep_display() + self.display_surface.fill((0,0,0,255)) + pygame.display.update() + + def _display_canvas(self): + pygame.display.update() + + def _display_splash(self): + self.display_surface.blit(self.splash, (0,0)) + self._display_canvas() + + def _display_background(self): + self.display_surface.blit(self.background_surface, (0,0)) + self._display_canvas() + + def _capture_background(self): + pil_string_image = pygame.image.tostring(self.display_surface, 'RGBA', False) + pil_image = Image.frombytes('RGBA', (self.WIDTH, self.HEIGHT), pil_string_image) + self.menu_background = pil_image.filter(ImageFilter.GaussianBlur(radius = 5)) + + def _display_menu_background(self): + strFormat = self.menu_background.mode + size = self.menu_background.size + raw_str = self.menu_background.tobytes("raw", strFormat) + background_surface = pygame.image.fromstring(raw_str, size, strFormat) + self.display_surface.blit(background_surface, (0,0)) + self._display_canvas() + + def _build_objects(self, background): + self.display_object_list = [] + + if self.display_active in ['home', 'dash']: + section_data = self.display_data[self.display_active] + elif 'menu_' in self.display_active: + section_data = [self.display_data['menus'][self.display_active.replace('menu_', '')]] + elif 'input_' in self.display_active: + section_data = [self.display_data['input'][self.display_active.replace('input_', '')]] + section_data[0]['data']['origin'] = self.input_origin + else: + return + + for object_data in section_data: + self.display_object_list.append(FlexObjPygame(object_data['type'], object_data, background)) + + def _init_dash(self): + self._init_framework() + self._configure_dash() + self._build_objects(self.background) + self._build_dash_map() + self._store_dash_objects() + + ''' + ====================== Input & Menu Code ======================== + ''' + def _event_detect(self): + """ + Called to detect input events from buttons, encoder, touch, etc. + """ + user_input = self.input_event # Save to variable to prevent spurious changes + self.command = None + if user_input: + if self.display_timeout is not None: + self.display_timeout = time.time() + self.TIMEOUT + if user_input not in ['UP', 'DOWN', 'ENTER', 'TOUCH']: + self.input_event = None + self.touch_pos = (0,0) + return + elif user_input == 'TOUCH': + self._process_touch() + elif user_input in ['UP', 'DOWN', 'ENTER']: + ''' TODO ''' + pass + + # Clear the input event and touch_pos + self.input_event = None + self.touch_pos = (0,0) + + def _process_touch(self): + if self.display_active: + ''' + Loop through current displayed objects and check for touch collisions + ''' + for pointer, object in enumerate(self.display_object_list): + objectData = object.get_object_data() + for index, touch_area in enumerate(objectData['touch_areas']): + if touch_area.collidepoint(self.touch_pos): + #print(f'You touched {objectData["button_list"][index]}.') + if 'cmd_' in objectData['button_list'][index]: + self.command = objectData['button_list'][index] + if objectData.get('button_value', False): + self.command_data = objectData['button_value'][index] + else: + self.command_data = None + self._command_handler() + elif objectData['button_list'][index] == 'menu_close': + self.display_active = 'dash' + self.display_init = True + elif ('menu_' in objectData['button_list'][index]) or ('input_' in objectData['button_list'][index]): + if self.display_active == 'dash': + self._capture_background() + self._store_dash_objects() + if ('input_' in objectData['button_list'][index]) and ('button_value' in list(objectData.keys())): + self.input_origin = objectData['button_value'][index] + self.display_active = objectData['button_list'][index] + self.display_init = True + elif 'button_' in objectData['button_list'][index]: + objectData['data']['input'] = objectData['button_list'][index].replace('button_', '') + self.display_object_list[pointer].update_object_data(updated_objectData=objectData) + + else: + ''' + Wake the display & go to home/dash + ''' + self._wake_display() + self.display_active = 'home' if self.HOME_ENABLED else 'dash' + self.display_init = True + self.display_timeout = time.time() + self.TIMEOUT + \ No newline at end of file diff --git a/display/flexobject_pil.py b/display/flexobject_pil.py new file mode 100644 index 00000000..65f5b829 --- /dev/null +++ b/display/flexobject_pil.py @@ -0,0 +1,1227 @@ +''' + Imported Libraries +''' +import qrcode +from PIL import Image, ImageDraw, ImageFont, ImageFilter +from pygame import Rect # Needed for touch support + +''' +Display Flex Object Class Definition +''' +class FlexObject: + def __init__(self, objectType, objectData, background): + self.objectType = objectType + self.objectData = objectData + self.objectState = { + 'animation_active' : False, + 'animation_start' : False + } + self.background = background + self._init_surface() + self._init_background() + self.update_object_data(objectData) + + def _init_background(self): + ''' saves the slice of background image in PIL to be used for background ''' + crop_region = (self.objectData['position'][0], self.objectData['position'][1], self.objectData['position'][0] + self.objectData['size'][0], self.objectData['position'][1] + self.objectData['size'][0]) + self.objectBG = self.background.crop(crop_region) + + def update_object_data(self, updated_objectData=None): + if updated_objectData is not None: + if updated_objectData['animation_enabled']: + self.objectState['animation_active'] = True + self.objectState['animation_start'] = True + self.objectState['animation_lastData'] = {} + for key, value in self.objectData.items(): + self.objectState['animation_lastData'][key] = value + for key, value in updated_objectData.items(): + self.objectData[key] = value + + if self.objectType == 'gauge': + if self.objectState['animation_active']: + self.objectCanvas = self._animate_gauge() + else: + self.objectCanvas = self._draw_gauge(self.objectData['size'], self.objectData['fg_color'], self.objectData['bg_color'], + self.objectData['temps'], self.objectData['label'], units=self.objectData['units'], + max_temp=self.objectData['max_temp']) + self._define_generic_touch_area() + + if self.objectType == 'gauge_compact': + self.objectCanvas = self._draw_gauge_compact() + self._define_generic_touch_area() + + if self.objectType == 'mode_bar': + self.objectCanvas = self._draw_mode_bar(self.objectData['size'], self.objectData['text']) + + if self.objectType == 'control_panel': + self.objectCanvas = self._draw_control_panel(self.objectData['size'], self.objectData['button_list'], active=self.objectData['button_active']) + self._define_control_panel_touch_areas() + + if self.objectType == 'status_icon': + if self.objectState['animation_active']: + self.objectCanvas = self._animate_status_icon() + else: + self.objectCanvas = self._draw_status_icon() + + if self.objectType == 'menu_icon': + self.objectCanvas = self._draw_menu_icon(self.objectData['size']) + self._define_generic_touch_area() + + if self.objectType == 'menu': + self.objectCanvas = self._draw_menu() + + if self.objectType == 'qrcode': + self.objectCanvas = self._draw_qrcode() + self._define_generic_touch_area() + + if self.objectType == 'input_number': + self._process_number_input() + + if self.objectState['animation_active']: + self.objectCanvas = self._animate_input_number() + else: + self.objectCanvas = self._draw_input_number() + + if self.objectType == 'timer': + self.objectCanvas = self._draw_timer() + + if self.objectType == 'alert': + self.objectCanvas = self._draw_alert() + + if self.objectType == 'p_mode_control': + self.objectCanvas = self._draw_pmode_status() + self._define_generic_touch_area() + + if self.objectType == 'splus_control': + self.objectCanvas = self._draw_splus_status() + self._define_generic_touch_area() + + if self.objectType == 'hopper_status': + self.objectCanvas = self._draw_hopper_status() + self._define_generic_touch_area() + + return self.objectCanvas + + def get_object_data(self): + current_objectData = dict(self.objectData) + return current_objectData + + def get_object_canvas(self): + return self.objectCanvas + + def get_object_state(self): + current_objectState = self.objectState.copy() + return current_objectState + + def _animate_gauge(self): + if self.objectState['animation_start']: + self.objectState['animation_start'] = False # Run animation start only once + self.objectState['animation_temps'] = self.objectData['temps'].copy() + self.objectState['animation_temps'][0] = self.objectState['animation_lastData']['temps'][0] + target_temp = self.objectData['temps'][0] + last_temp = self.objectState['animation_lastData']['temps'][0] + self.delta = target_temp - last_temp + + if self.delta == 0: + self.objectState['animation_active'] = False + self.step_value = 0 + elif self.delta > 0: + self.step_value = int(self.delta / 3) if int(self.delta / 3) != 0 else 1 + else: + self.step_value = int(self.delta / 3) if int(self.delta / 3) != 0 else -1 + + if self.objectState['animation_temps'][0] != self.objectData['temps'][0]: + self.objectState['animation_temps'][0] += self.step_value + + if self.objectState['animation_temps'][0] <= 0: + self.objectState['animation_temps'][0] = self.objectData['temps'][0] + self.objectState['animation_active'] = False #if len(self.objectData['label']) <= 5 else True + + elif (self.delta >= 0) and (abs(self.objectState['animation_temps'][0]) >= abs(self.objectData['temps'][0])): + self.objectState['animation_temps'][0] = self.objectData['temps'][0] + self.objectState['animation_active'] = False #if len(self.objectData['label']) <= 5 else True + + elif (self.delta <= 0) and (abs(self.objectState['animation_temps'][0]) <= abs(self.objectData['temps'][0])): + self.objectState['animation_temps'][0] = self.objectData['temps'][0] + self.objectState['animation_active'] = False #if len(self.objectData['label']) <= 5 else True + ''' + if len(self.objectData['label']) > 5: + self.objectState['animation_label_position'] += 1 + if self.objectState['animation_label_position'] >= 100: + self.objectState['animation_label_position'] = 0 + ''' + + return self._draw_gauge(self.objectData['size'], self.objectData['fg_color'], self.objectData['bg_color'], + self.objectState['animation_temps'], self.objectData['label'], units=self.objectData['units'], + max_temp=self.objectData['max_temp']) + + def _draw_gauge(self, size, fg_color, bg_color, temps, label, set_point_color=(0, 200, 255, 255), notify_point_color=(255, 255, 0, 255), units='F', max_temp=600): + ''' + Draw a gauge and return an Image canvas with that gauge. + + :param size: The size of the gauge to produce in (width, height) format. + :type size: tuple(int, int) + :param fg_color: The foreground color of the gauge in (r,g,b,a) format. + :type fg_color: tuple(int, int, int, int) + :param bg_color: The background color of the gauge in (r,g,b,a) format. + :type bg_color: tuple(int, int, int, int) + :param temps: The list of temperatures to display. [current, notify_point, set_point] + :type temps: list[float, float, float] + :param set_point_color: The set point color of the gauge in (r,g,b,a) format. This is used mainly for the primary gauge. + :type set_point_color: tuple(int, int, int, int) + :param notify_point_color: The notify point color of the gauge in (r,g,b,a) format. + :type notify_point_color: tuple(int, int, int, int) + :param str units: Either 'F' for fahrenheit or 'C' for celcius. + :param int max_temp: Maximum temperature to display on the gauge arc. + :return: Image object. + ''' + + output_size = size + + size = (400,400) + + # Create drawing object + gauge = Image.new("RGBA", (size[0], size[1])) + draw = ImageDraw.Draw(gauge) + + # Get coordinates for the gauge arcs + coords = (0 + int(size[0] * 0.05), 0 + int(size[1] * 0.05), size[0] - int(size[0] * 0.05), size[1] - int(size[0] * 0.05)) + + # Draw Arc for Temperature (Percent) + start_rad = 135 + + # Determine the radian (0-270) for the current temperature + temp_rad = 270 * min(temps[0]/max_temp, 1) + end_rad = start_rad + temp_rad + + # Draw Temperature Arc + draw.arc(coords, start=start_rad, end=end_rad, fill=fg_color, width=30) + + # Draw Background Arc + draw.arc(coords, start=end_rad, end=45, fill=bg_color, width=30) + + # Current Temperature (Large Centered) + cur_temp = str(temps[0])[:5] + if len(cur_temp) < 5: + font_point_size = round(size[1] * 0.3) # Font size as a ratio of the object size + else: + font_point_size = round(size[1] * 0.25) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(cur_temp) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + label_x = (size[0] // 2) - (font_width // 2) + label_y = (size[1] // 2) - (font_height // 1.1) + label_origin = (label_x, label_y) + + draw.text(label_origin, cur_temp, font=font, fill=fg_color) + + # Units Label (Small Centered) + unit_label = f'{units}°' + font_point_size = font_point_size = round((size[1] * 0.35) / 4) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(units) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + + label_x = (size[0] // 2) - (font_width // 2) + label_y = round((size[1] * 0.60)) + label_origin = (label_x, label_y) + draw.text(label_origin, unit_label, font=font, fill=(255, 255, 255)) + + # Gauge Label + + # Gauge Label Text + if len(label) > 7: + label_displayed = label[0:7] + else: + label_displayed = label + font_point_size = round((size[1] * 0.55) / 4) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(label_displayed) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + #print(f'Font bbox= {font_bbox}') + + label_x = (size[0] // 2) - (font_width // 2) + label_y = round((size[1] * 0.75)) + label_origin = (label_x, label_y) + draw.text(label_origin, label_displayed, font=font, fill=(255, 255, 255)) + # Gauge Label Rectangle + # rounded_rectangle = (label_x-6, label_y+4, label_x + font_width + 8, label_y + font_height + 16) + rounded_rectangle = (label_x-8, label_y+(font_bbox[1] - 8), label_x + font_width + 8, label_y + font_bbox[1] + font_height + 8) + draw.rounded_rectangle(rounded_rectangle, radius=8, outline=(255,255,255), width=3) + + + # Set Points Labels + if temps[1] > 0 and temps[2] > 0: + dual_label = 1 + else: + dual_label = 0 + + # Notify Point Label + if temps[1] > 0: + notify_point_label = f'{temps[1]}' + font_point_size = round((size[1] * (0.5 - (dual_label * 0.15))) / 4) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(notify_point_label) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + + label_x = (size[0] // 2) - (font_width // 2) - (dual_label * ((font_width // 2) + 10)) + label_y = round((size[1] * (0.20 + (dual_label * 0.05)))) + label_origin = (label_x, label_y) + draw.text(label_origin, notify_point_label, font=font, fill=notify_point_color) + # Notify Point Label Rectangle + rounded_rectangle = (label_x-8, label_y+(font_bbox[1] - 8), label_x + font_width + 8, label_y + font_bbox[1] + font_height + 8) + # (label_x-6, label_y+2, label_x + font_width + 6, label_y + font_height + 4) + draw.rounded_rectangle(rounded_rectangle, radius=8, outline=notify_point_color, width=3) + + # Draw Tic for notify point + setpoint = 270 * min(temps[1]/max_temp, 1) + setpoint += start_rad + draw.arc(coords, start=setpoint - 1, end=setpoint + 1, fill=notify_point_color, width=30) + + # Set Point Label + if temps[2] > 0: + set_point_label = f'{temps[2]}' + font_point_size = round((size[1] * (0.5 - (dual_label * 0.15))) / 4) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(set_point_label) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + + label_x = (size[0] // 2) - (font_width // 2) + (dual_label * ((font_width // 2) + 10)) + label_y = round((size[1] * (0.20 + (dual_label * 0.05)))) + label_origin = (label_x, label_y) + draw.text(label_origin, set_point_label, font=font, fill=set_point_color) + # Set Point Label Rectangle + rounded_rectangle = (label_x-8, label_y+(font_bbox[1] - 8), label_x + font_width + 8, label_y + font_bbox[1] + font_height + 8) + draw.rounded_rectangle(rounded_rectangle, radius=8, outline=set_point_color, width=3) + + # Draw Tic for set point + setpoint = 270 * min(temps[2]/max_temp, 1) + setpoint += start_rad + draw.arc(coords, start=setpoint - 1, end=setpoint + 1, fill=set_point_color, width=30) + + # Create drawing object + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + gauge = gauge.resize(output_size) + canvas.paste(gauge, (0, 0), gauge) + + return canvas + + def _draw_gauge_compact(self): + output_size = self.objectData['size'] + size = (400,200) # Working Canvas Size + + # Create drawing object + gauge = Image.new("RGBA", size) + draw = ImageDraw.Draw(gauge) + + # Gauge Background + draw.rounded_rectangle((15, 15, size[0]-15, size[1]-15), radius=20, fill=(255,255,255,100)) + + # Draw Gauge Label on Top Portion of Box + if len(self.objectData['label']) > 11: + label_displayed = self.objectData['label'][0:11] + else: + label_displayed = self.objectData['label'] + + gauge_label = self._draw_text(label_displayed, 'trebuc.ttf', 50, (255,255,255)) + gauge.paste(gauge_label, (40,30), gauge_label) + + # Draw Temperature Value + current_temp = self._draw_text(self.objectData['temps'][0], 'trebuc.ttf', 100, (255,255,255)) + gauge.paste(current_temp, (40, 75), current_temp) + + # Determine if Displaying Notify Point AND Set Point + dual_temp = True if self.objectData['temps'][1] != 0 and self.objectData['temps'][2] != 0 else False + + if dual_temp: + font_size = 30 + y_position_offset = 0 + else: + font_size = 50 + y_position_offset = 15 + + + # Draw Notify Point Value + if self.objectData['temps'][1]: + notify_point_temp = self._draw_text(self.objectData['temps'][1], 'trebuc.ttf', font_size, (255, 255, 0), rect=True) + gauge.paste(notify_point_temp, (215, 75 + y_position_offset), notify_point_temp) + + # Draw Set Point Value + if self.objectData['temps'][2]: + set_point_temp = self._draw_text(self.objectData['temps'][2], 'trebuc.ttf', font_size, (0, 255, 255), rect=True) + if dual_temp: + y_position_offset = notify_point_temp.size[1] + 2 + gauge.paste(set_point_temp, (215, 75 + y_position_offset), set_point_temp) + + # Draw Units + text = f'{self.objectData["units"]}°' + units_label = self._draw_text(text, 'Trebuchet_MS_Bold.ttf', 50, (255,255,255)) + #units_label_size = units_label.size() + units_label_position = (330, (size[1] // 2)) + gauge.paste(units_label, units_label_position, units_label) + + # Draw Bar + temp_bar = (40, 160, 360, 170) + max_temp = self.objectData['max_temp'] + current_temp_adjusted = int((self.objectData['temps'][0] / max_temp) * 320) + 40 if self.objectData['temps'][0] > 0 else 40 + if current_temp_adjusted > 360: + current_temp_adjusted = 360 + current_temp_bar = (40, 160, current_temp_adjusted, 170) + draw.rounded_rectangle(temp_bar, radius=10, fill=(0,0,0,200)) + draw.rounded_rectangle(current_temp_bar, radius=10, fill=(255,255,255,255)) + + # Draw Notify Point Polygon + if self.objectData['temps'][1]: + notify_temp_adjusted = int((self.objectData['temps'][1] / max_temp) * 320) + 40 if self.objectData['temps'][1] > 0 else 0 + if notify_temp_adjusted > 360: + notify_temp_adjusted = 360 + triangle_coords = [(notify_temp_adjusted, 168), (notify_temp_adjusted + 10, 150), (notify_temp_adjusted - 10, 150)] + draw.polygon(triangle_coords, fill=(255,255,0,255)) + + # Draw Set Point Polygon + if self.objectData['temps'][2]: + set_temp_adjusted = int((self.objectData['temps'][2] / max_temp) * 320) + 40 if self.objectData['temps'][2] > 0 else 0 + if set_temp_adjusted > 360: + set_temp_adjusted = 360 + triangle_coords = [(set_temp_adjusted, 168), (set_temp_adjusted + 10, 150), (set_temp_adjusted - 10, 150)] + draw.polygon(triangle_coords, fill=(0,255,255,255)) + + # Create drawing object + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + gauge = gauge.resize(output_size) + canvas.paste(gauge, (0, 0), gauge) + + return canvas + + def _draw_text(self, text, font_name, font_point_size, color, rect=False, bg_fill=None): + font = ImageFont.truetype(font_name, font_point_size) + font_bbox = font.getbbox(str(text)) # Grab the width of the text + font_canvas_size = (font_bbox[2], font_bbox[3]) + font_canvas = Image.new('RGBA', font_canvas_size) + font_draw = ImageDraw.Draw(font_canvas) + font_draw.text((0,0), str(text), font=font, fill=color) + font_canvas = font_canvas.crop(font_canvas.getbbox()) + if rect: + font_canvas_size = font_canvas.size + rect_canvas_size = (font_canvas_size[0] + 16, font_canvas_size[1] + 16) + rect_canvas = Image.new('RGBA', rect_canvas_size) + if bg_fill is not None: + rect_canvas.paste(bg_fill, (0,0) + rect_canvas.size) + rect_draw = ImageDraw.Draw(rect_canvas) + rect_draw.rounded_rectangle((0, 0, rect_canvas_size[0], rect_canvas_size[1]), radius=8, outline=color, width=3) + rect_canvas.paste(font_canvas, (8,8), font_canvas) + return rect_canvas + elif bg_fill is not None: + output_canvas = Image.new('RGBA', font_canvas.size) + output_canvas.paste(bg_fill, (0,0) + font_canvas.size) + output_canvas.paste(font_canvas, (0,0), font_canvas) + return output_canvas + else: + return font_canvas + + def _draw_mode_bar(self, size, text): + output_size = size + + size = (400,60) + + # Create drawing object + mode_bar = Image.new("RGBA", (size[0], size[1])) + draw = ImageDraw.Draw(mode_bar) + + # Text Rectangle from top + draw.rounded_rectangle((10, -20, size[0]-10, size[1]-10), radius=8, outline=(255,255,255,255), width=2, fill=(0, 0, 0, 100)) + + # Mode Text + if len(text) > 16: + label_displayed = text[0:16] + else: + label_displayed = text + font_point_size = round(size[1] * 0.80) # Font size as a ratio of the object size + font = ImageFont.truetype("trebuc.ttf", font_point_size) + font_bbox = font.getbbox(label_displayed) # Grab the width of the text + font_width = font_bbox[2] - font_bbox[0] + font_height = font_bbox[3] - font_bbox[1] + + label_x = (size[0] // 2) - (font_width // 2) + label_y = (size[1] // 2) - (font_height // 2) - 18 + label_origin = (label_x, label_y) + draw.text(label_origin, label_displayed, font=font, fill=(255, 255, 255)) + + # Create drawing object + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + mode_bar = mode_bar.resize(output_size) + canvas.paste(mode_bar, (0, 0), mode_bar) + + return canvas + + def _create_icon(self, charid, font_size, color, bg_fill=None): + # Get font and character size + font = ImageFont.truetype("./static/font/FA-Free-Solid.otf", font_size) + # Create canvas + font_bbox = font.getbbox(charid) # Grab the width of the text + font_width = font_bbox[2] + font_height = font_bbox[3] + + icon_canvas = Image.new('RGBA', (font_width, font_height)) + if bg_fill is not None: + icon_canvas.paste(bg_fill, (0,0) + icon_canvas.size) + + # Create drawing object + draw = ImageDraw.Draw(icon_canvas) + draw.text((0, 0), charid, font=font, fill=color) + icon_canvas = icon_canvas.crop(icon_canvas.getbbox()) + return icon_canvas + + def _draw_control_panel(self, size, button_type, active='Stop'): + output_size = self.objectData['size'] + button_type = self.objectData['button_type'] + active = self.objectData['button_active'] + + # Establish working size + size = (400,100) + padding = 10 + + # Create drawing object + control_panel = Image.new("RGBA", (size[0], size[1])) + draw = ImageDraw.Draw(control_panel) + + # Text Rectangle from top + draw.rounded_rectangle((padding, padding, size[0]-padding, size[1]-padding), radius=8, outline=(255,255,255,255), width=2, fill=(0, 0, 0, 100)) + + spacing = int((size[0] - 20) / (len(button_type))) + # Draw Dividing Lines + for index in range(1, len(button_type) + 1): + x_position = (index * spacing) + 10 + + # Draw vertical dividing line unless on the last icon space + if index < len(button_type): + coords = (x_position, padding, x_position, size[1]-padding) + draw.line(coords, fill=(255,255,255,255), width=2) + + # Draw icon + font_size = 40 + if button_type[index - 1] == active: + font_color = (255, 255, 255, 255) # Color for active button + else: + font_color = (255, 255, 255, 200) # Color for inactive button + + if button_type[index - 1] == 'Startup': + char_id = '\uf04b' # FontAwesome Play Icon + elif button_type[index - 1] == 'Prime': + char_id = '\uf101' # FontAwesome Double Arrow Right Icon + elif button_type[index - 1] == 'Monitor': + char_id = '\uf530' # FontAwesome Glasses Icon + elif button_type[index - 1] == 'Stop': + char_id = '\uf04d' # FontAwesome Stop Icon + elif button_type[index - 1] == 'Smoke': + char_id = '\uf0c2' # FontAwesome Cloud Icon + elif button_type[index - 1] == 'Hold': + char_id = '\uf05b' # FontAwesome Crosshairs Icon + elif button_type[index - 1] == 'Shutdown': + char_id = '\uf11e' # FontAwesome Finish Flag Icon + elif button_type[index - 1] == 'Next': + char_id = '\uf051' # FontAwesome Step Icon + elif button_type[index - 1] == 'None': + char_id = '\uf068' # FontAwesome Minus Icon + else: + char_id = '\uf071' # FontAwesome Error Triangle Icon + icon_canvas = self._create_icon(char_id, font_size, font_color) + icon_size = icon_canvas.getbbox() + control_panel.paste(icon_canvas, (x_position - (spacing // 2) - (icon_size[2] // 2), (size[1] // 2) - (icon_size[3] // 2) ), icon_canvas) + + # Create final canvas output object + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + control_panel = control_panel.resize(output_size) + canvas.paste(control_panel, (0, 0), control_panel) + + return canvas + + def _define_control_panel_touch_areas(self): + spacing = int((self.objectData['size'][0]) / (len(self.objectData['button_list']))) + # Draw Dividing Lines + self.objectData['touch_areas'] = [] + for index in range(0, len(self.objectData['button_list'])): + x_left = self.objectData['position'][0] + (index * spacing) + y_top = self.objectData['position'][1] + width = spacing + height = self.objectData['size'][1] + touch_area = Rect(x_left, y_top, width, height) + # Create button rectangle / touch area and append to list + self.objectData['touch_areas'].append(touch_area) + #print(f'Index: {index} Button: {self.objectData["button_list"][index]} Touch Area: {touch_area}') + + def _draw_status_icon(self, rotation=0, breath_step=0): + # Save output size + output_size = self.objectData['size'] + type = self.objectData['icon'] + icon_color = self.objectData['active_color'] if self.objectData['active'] else self.objectData['inactive_color'] + animation_breath_steps = [1, 0.95, 0.90, 0.80, 0.70, 0.80, 0.90, 0.95, 1] + + # Working Size + size = (100,100) + + if type == 'Fan': + char_id = '\uf863' # Font Awesome Fan Icon + + elif type == 'Auger': + char_id = '\uf101' # Font Awesome Right Chevron Arrows Icon + + elif type == 'Igniter': + char_id = '\uf46a' # Font Awesome Flame Icon + + elif type == 'SmokePlus': + char_id = '\uf0c2' # Font Awesome Icon for Cloud (Smoke) + text = '\uf067' # Font Awesome Icon for PLUS + + elif type == 'Notify': + char_id = '\uf0f3' # Font Awesome Bell Icon + + elif type == 'Recipe': + char_id = '\uf46d' # Font Awesome Clipboard Icon + + elif type == 'Pause': + char_id = '\uf04c' # Font Awesome Pause Icon + + else: + char_id = '\uf071' # FontAwesome Error Triangle Icon + + if 'animation_breathe' in self.objectState.keys(): + if self.objectState['animation_breathe'] >= len(animation_breath_steps): + self.objectState['animation_breathe'] = breath_step = 0 + + font_size = int(animation_breath_steps[breath_step] * 80) + + icon = self._create_icon(char_id, font_size, icon_color) + + # Determine Bounding Box of Icon + icon_size = icon.getbbox() + + if rotation: + icon = icon.rotate(rotation) + + icon = icon.crop(icon_size) + # Upper Left Corner of Centered Icon + center = ((size[0] // 2) - (icon_size[2] // 2), (size[1] // 2) - (icon_size[3] // 2)) + + # Create final canvas output object + canvas = Image.new("RGBA", size) + canvas.paste(icon, center, icon) + + canvas = canvas.resize(output_size) + + return canvas + + def _animate_status_icon(self): + if self.objectState['animation_start']: + self.objectState['animation_start'] = False # Run animation start only once + self.objectState['animation_rotation'] = 0 # Set initial rotation + self.objectState['animation_breathe'] = 0 # Set initial animation breath step + + # Fans Rotate, so increase rotation by 15 degrees on each step + if self.objectData['icon'] == 'Fan': + self.objectState['animation_rotation'] += 15 + if self.objectState['animation_rotation'] > 360: + self.objectState['animation_rotation'] = 0 + + # Some Icons Breathe + if self.objectData['icon'] in ['Auger', 'Igniter', 'Recipe']: + self.objectState['animation_breathe'] += 1 + + return self._draw_status_icon(rotation=self.objectState['animation_rotation'], breath_step=self.objectState['animation_breathe']) + + + def _draw_menu_icon(self, size): + # Save output size + output_size = size + + # Working Size + size = (40,40) + if self.objectData['icon'] == 'Hamburger': + char_id = '\uf0c9' # Font Awesome Hamburger Menu + else: + char_id = '\uf00d' # Font Awesome Times for closing the window + + font_size = 30 + color = (255,255,255,255) + + menu_icon = self._create_icon(char_id, font_size, color) + + menu_icon_size = menu_icon.getbbox() + + center_offset = (size[0] // 2) - (menu_icon_size[2] // 2), (size[1] // 2) - (menu_icon_size[3] // 2) + + # Create final canvas output object + canvas = Image.new("RGBA", size) + canvas.paste(menu_icon, center_offset, menu_icon) + + canvas = canvas.resize(output_size) + + return canvas + + def _define_generic_touch_area(self): + touch_area = Rect((self.objectData['position'], self.objectData['size'])) + # Create button rectangle / touch area and append to list + self.objectData['touch_areas'] = [touch_area] + + def _scale_touch_area(self, rectangle, screen_size_old, screen_size_new): + """Scales a rectangle size and position according to the screen size change. + + Args: + rectangle: A tuple of (x, y, width, height). + screen_size_old: The old screen size. + screen_size_new: The new screen size. + + Returns: + A tuple of (x, y, width, height) of the scaled rectangle. + """ + x, y, width, height = rectangle + scaled_width = int(width * (screen_size_new[0] / screen_size_old[0])) + scaled_height = int(height * (screen_size_new[1] / screen_size_old[1])) + xlated_x = int(x * (screen_size_new[0] / screen_size_old[0])) + xlated_y = int(y * (screen_size_new[1] / screen_size_old[1])) + return (xlated_x, xlated_y, scaled_width, scaled_height) + + def _transform_touch_area(self, touch_area, origin): + """ Transforms the touch area to the correct place on the screen. """ + return (touch_area[0] + origin[0], touch_area[1] + origin[1], touch_area[2], touch_area[3]) + + def _draw_menu(self): + size = (600, 400) # Define working size + canvas = Image.new("RGBA", size) # Create canvas output object + draw = ImageDraw.Draw(canvas) # Create drawing object + fg_color = self.objectData['color'] + + # Clear any touch areas that might have been defined before + self.objectData['touch_areas'] = [] + + # Rounded rectangle that fills the canvas size + menu_padding = 10 # Define padding around outside of the menu rectangle + draw.rounded_rectangle((menu_padding, menu_padding, size[0]-menu_padding, size[1]-menu_padding), radius=8, outline=(0,0,0,225), fill=(0, 0, 0, 250)) + + # Menu Title + title = self._draw_text(self.objectData['title_text'], 'trebuc.ttf', 35, fg_color) + title_position = ((size[0] // 2) - (title.width // 2), 20) + canvas.paste(title, title_position, title) + + # Index through button_list to create menu items + + number_of_buttons = len(self.objectData['button_list']) + + two_column_mode = True if number_of_buttons > 5 else False + + if two_column_mode: + button_height = 50 + button_padding = 10 + button_width = size[0] // 2 - menu_padding - (button_padding * 2) + column = 0 + button_area_position = (menu_padding + button_padding, 80) + button_area_size = (size[0] - (menu_padding * 2) - (button_padding * 2), size[1] - button_area_position[1] - menu_padding - button_padding) + row_height = 60 + else: + button_height = 50 + button_padding = 10 + button_width = size[0] - (menu_padding * 2) - (button_padding * 2) + button_area_position = (menu_padding + button_padding, 60) + button_area_size = (size[0] - (menu_padding * 2) - (button_padding * 2), size[1] - button_area_position[1] - menu_padding - button_padding) + row_height = button_area_size[1] // (number_of_buttons - 1) + + button_count = 0 + row = 0 + + for index, button in enumerate(self.objectData['button_list']): + if '_close' in button and self.objectData['button_text'][index] == 'Close Menu': + # Close Icon Upper Right + close_icon = self._create_icon('\uf00d', 34, (255,255,255)) + close_position = (size[0] - (menu_padding * 4), (menu_padding * 2)) + canvas.paste(close_icon, close_position, close_icon) + close_touch_area = (close_position[0]+self.objectData['position'][0], close_position[1]+self.objectData['position'][1], close_icon.width, close_icon.height) + close_touch_area = self._scale_touch_area(close_touch_area, size, self.objectData['size']) + scaled_touch_area = self._scale_touch_area(close_touch_area, size, self.objectData['size']) + self.objectData['touch_areas'].append(Rect(scaled_touch_area)) + else: + if button_count > 10: + break # Stop if at 11 items + + if two_column_mode: + if button_count in [0, 2, 4, 6, 8, 10]: + rect_position = (button_area_position[0], button_area_position[1] + (row * row_height)) + else: + rect_position = (button_area_position[0] + button_width + (button_padding * 2), button_area_position[1] + (row * row_height)) + row += 1 + else: + rect_position = (button_area_position[0], button_area_position[1] + (button_count * row_height) + ((row_height // 2) - (button_height // 2))) + + rect_size = (button_width, button_height) + rect_coords = (rect_position[0], rect_position[1], rect_position[0] + rect_size[0], rect_position[1] + rect_size[1]) + + draw.rounded_rectangle(rect_coords, radius=8, outline=(255,255,255,255), fill=(0, 0, 0, 250)) + + # Put button text inside rectangle + if len(self.objectData['button_text'][index]) > 25: + label_displayed = self.objectData['button_text'][index][0:25] + else: + label_displayed = self.objectData['button_text'][index] + + label = self._draw_text(label_displayed, self.objectData['font'], 35, fg_color) + label_x = rect_position[0] + (rect_size[0] // 2) - (label.width // 2) + label_y = rect_position[1] + (rect_size[1] // 2) - (label.height // 2) + label_position = (label_x, label_y) + canvas.paste(label, label_position, label) + + # Define touch area for button + button_touch_area = rect_position + rect_size + scaled_touch_area = self._scale_touch_area(button_touch_area, size, self.objectData['size']) + transformed_touch_area = self._transform_touch_area(scaled_touch_area, self.objectData['position']) + touch_area = Rect(transformed_touch_area) + + # Create button rectangle / touch area and append to list + self.objectData['touch_areas'].append(touch_area) + + button_count += 1 + + # Resize for output + canvas = canvas.resize(self.objectData['size']) + + return canvas + + def _draw_qrcode(self): + size = (600, 400) # Define working size + canvas = Image.new("RGBA", size) # Create canvas output object + draw = ImageDraw.Draw(canvas) # Create drawing object + fg_color = self.objectData['color'] + + # Rounded rectangle that fills the canvas size + menu_padding = 10 # Define padding around outside of the menu rectangle + draw.rounded_rectangle((menu_padding, menu_padding, size[0]-menu_padding, size[1]-menu_padding), radius=8, outline=(0,0,0,225), fill=(0, 0, 0, 250)) + + # Draw Close Icon in upper right + close_icon = self._create_icon('\uf00d', 34, (255,255,255)) + close_position = (size[0] - (menu_padding * 4), (menu_padding * 2)) + canvas.paste(close_icon, close_position, close_icon) + + # Menu Title + title = self._draw_text(self.objectData['ip_address'], 'trebuc.ttf', 35, fg_color) + title_position = ((size[0] // 2) - (title.width // 2), 20) + canvas.paste(title, title_position, title) + + # Draw QR Code + img_qr = qrcode.make(f'http://{self.objectData["ip_address"]}') + img_qr = img_qr.resize((300,300)) + position = (150, 60) + canvas.paste(img_qr, position) + + return canvas + + def _draw_input_number(self): + size = (600, 400) # Define working size + canvas = Image.new("RGBA", size) # Create canvas output object + draw = ImageDraw.Draw(canvas) # Create drawing object + button_pushed = self.objectState.get('animation_input', '') + self.objectData['touch_areas'] = [] + self.objectData['button_list'] = [] + + # Rounded rectangle that fills the canvas size + menu_padding = 10 # Define padding around outside of the menu rectangle + draw.rounded_rectangle((menu_padding, menu_padding, size[0]-menu_padding, size[1]-menu_padding), radius=8, outline=(0,0,0,225), fill=(0, 0, 0, 250)) + + # Close Icon Upper Right + close_icon = self._create_icon('\uf00d', 34, (255,255,255)) + close_position = (size[0] - (menu_padding * 4), (menu_padding * 2)) + canvas.paste(close_icon, close_position, close_icon) + close_touch_area = (close_position[0]+self.objectData['position'][0], close_position[1]+self.objectData['position'][1], close_icon.width, close_icon.height) + close_touch_area = self._scale_touch_area(close_touch_area, size, self.objectData['size']) + scaled_touch_area = self._scale_touch_area(close_touch_area, size, self.objectData['size']) + self.objectData['touch_areas'].append(Rect(scaled_touch_area)) + self.objectData['button_list'].append('menu_close') + + # Menu Title + title = self._draw_text(self.objectData['title_text'], self.objectData['font'], 35, self.objectData['color'], rect=False, bg_fill=(0,0,0,250)) + title_x = (size[0] // 2) - (title.width // 2) + title_y = 15 + canvas.paste(title, (title_x, title_y)) + + # Number Display + number_entry_position = (60, 75) + number_entry_size = (240, 100) + number_entry_coords = number_entry_position + (number_entry_position[0] + number_entry_size[0], number_entry_position[1] + number_entry_size[1]) + number_entry_bg_color = (50,50,50) + draw.rounded_rectangle(number_entry_coords, radius=8, fill=number_entry_bg_color) + number_digits = self._draw_text(self.objectData['data']['value'], self.objectData['font'], 80, self.objectData['color'], bg_fill=number_entry_bg_color) + number_digits_position = (number_entry_position[0] + ((number_entry_size[0] // 2) - (number_digits.width // 2)), number_entry_position[1] + (number_entry_size[1] // 2) - (number_digits.height // 2)) + canvas.paste(number_digits, number_digits_position) + + # Up Arrow + if button_pushed == 'up': + bg_fill = (255,255,255,255) + fg_fill = (0,0,0,255) + else: + bg_fill = (0,0,0,255) + fg_fill = self.objectData['color'] + button_position = (60, 195) + button_size = (110, 70) + button_coords = button_position + (button_position[0] + button_size[0], button_position[1] + button_size[1]) + draw.rounded_rectangle(button_coords, radius=8, outline=fg_fill, fill=bg_fill, width=3) + button_icon = self._create_icon('\uf077', 35, fg_fill, bg_fill=bg_fill) + button_icon_position = (button_position[0] + ((button_size[0] // 2) - (button_icon.width // 2)), button_position[1] + (button_size[1] // 2) - (button_icon.height // 2)) + canvas.paste(button_icon, button_icon_position) + # Scale and Store Touch Area + button_touch_area = button_position + button_size + scaled_touch_area = self._scale_touch_area(button_touch_area, size, self.objectData['size']) + transform_touch_area = self._transform_touch_area(scaled_touch_area, self.objectData['position']) + self.objectData['touch_areas'].append(Rect(transform_touch_area)) + self.objectData['button_list'].append('button_up') + + # Down Arrow + if button_pushed == 'down': + bg_fill = (255,255,255,255) + fg_fill = (0,0,0,255) + else: + bg_fill = (0,0,0,255) + fg_fill = self.objectData['color'] + button_position = (190, 195) + button_size = (110, 70) + button_coords = button_position + (button_position[0] + button_size[0], button_position[1] + button_size[1]) + draw.rounded_rectangle(button_coords, radius=8, outline=fg_fill, fill=bg_fill, width=3) + button_icon = self._create_icon('\uf078', 35, fg_fill, bg_fill=bg_fill) + button_icon_position = (button_position[0] + ((button_size[0] // 2) - (button_icon.width // 2)), button_position[1] + (button_size[1] // 2) - (button_icon.height // 2)) + canvas.paste(button_icon, button_icon_position) + # Scale and Store Touch Area + button_touch_area = button_position + button_size + scaled_touch_area = self._scale_touch_area(button_touch_area, size, self.objectData['size']) + transform_touch_area = self._transform_touch_area(scaled_touch_area, self.objectData['position']) + self.objectData['touch_areas'].append(Rect(transform_touch_area)) + self.objectData['button_list'].append('button_down') + + # Enter Button + button_position = (60, 290) + button_size = (240, 80) + button_coords = button_position + (button_position[0] + button_size[0], button_position[1] + button_size[1]) + draw.rounded_rectangle(button_coords, radius=8, outline=self.objectData['color'], width=3) + button_text = self._draw_text('ENTER', self.objectData['font'], 60, self.objectData['color'], bg_fill=(0,0,0)) + button_text_position = (button_position[0] + ((button_size[0] // 2) - (button_text.width // 2)), button_position[1] + (button_size[1] // 2) - (button_text.height // 2)) + canvas.paste(button_text, button_text_position) + # Scale and Store Touch Area + button_touch_area = button_position + button_size + scaled_touch_area = self._scale_touch_area(button_touch_area, size, self.objectData['size']) + transform_touch_area = self._transform_touch_area(scaled_touch_area, self.objectData['position']) + self.objectData['touch_areas'].append(Rect(transform_touch_area)) + self.objectData['button_list'].append(self.objectData['command']) + + # Draw Number Pad + pad_position = (250, 75) + button_size = (70,70) + button_padding = 5 + button_position = [pad_position[0] - button_size[0] - button_padding, pad_position[1] - button_size[0] - button_padding] + + pad_button_list = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['DEL', '0', '.']] + for row in pad_button_list: + button_position[1] += button_size[1] + button_padding + button_position[0] = pad_position[0] + for col in row: + if button_pushed == col: + bg_fill = (255,255,255,255) + fg_fill = (0,0,0,255) + else: + bg_fill = (0,0,0,255) + fg_fill = self.objectData['color'] + button_position[0] += button_size[0] + button_padding + if col == 'DEL': + button_text = self._create_icon('\uf55a', 35, fg_fill, bg_fill=bg_fill) + else: + button_text = self._draw_text(col, self.objectData['font'], 35, fg_fill, rect=False, bg_fill=bg_fill) + # Draw Rectangle + button_coords = tuple(button_position) + (button_position[0] + button_size[0], button_position[1] + button_size[1]) + draw.rounded_rectangle(button_coords, radius=8, outline=fg_fill, fill=bg_fill, width=3) + text_position = (button_position[0] + (button_size[0] // 2) - (button_text.width // 2), button_position[1] + (button_size[1] // 2) - (button_text.height // 2)) + canvas.paste(button_text, text_position) + button_touch_area = (button_position[0]+self.objectData['position'][0], button_position[1]+self.objectData['position'][1]) + button_size + scaled_touch_area = self._scale_touch_area(button_touch_area, size, self.objectData['size']) + self.objectData['touch_areas'].append(Rect(scaled_touch_area)) + self.objectData['button_list'].append(f'button_{col}') + + return canvas + + def _animate_input_number(self): + if self.objectState['animation_start']: + self.objectState['animation_start'] = False # Run animation start only once + self.objectState['animation_counter'] = 0 # Setup a counter for number of frames to produce + self.objectState['animation_input'] = self.objectData['data']['input'] # Save input from user + self.objectData['data']['input'] = '' # Clear user input + + if self.objectState['animation_counter'] > 1: + self.objectState['animation_active'] = False # Disable animation after one frame + self.objectState['animation_input'] = '' + + canvas = self._draw_input_number() + + self.objectState['animation_counter'] += 1 # Increment the frame counter + + return canvas + + def _process_number_input(self): + + if self.objectData['data']['input'] != '': + ''' Check first for up / down input ''' + if self.objectData['data']['input'] == 'up': + self.objectData['data']['value'] += self.objectData['step'] + + if self.objectData['data']['input'] == 'down': + self.objectData['data']['value'] -= self.objectData['step'] + if self.objectData['data']['value'] < 0: + self.objectData['data']['value'] = 0 + + ''' Convert value to list of characters ''' + temp_string = str(self.objectData['data']['value']) + self.objectState['value'] = [char for char in temp_string] + + if self.objectData['data']['input'] == 'DEL': + + if len(self.objectState['value']) > 1: + ''' If a float, delete back to the decimal value ''' + if '.' in self.objectState['value'] and self.objectState['value'][-2] == '.': + self.objectState['value'].pop() + self.objectState['value'].pop() + else: + self.objectState['value'].pop() + else: + self.objectState['value'] = ['0'] + + if '.' in self.objectState['value'] and self.objectData['data']['input'] == '.': + pass + elif self.objectData['data']['input'] in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.']: + + if len(self.objectState['value']) > 5: + pass + if '.' in self.objectState['value']: + self.objectState['value'].pop() + self.objectState['value'].append(self.objectData['data']['input']) + elif len(self.objectState['value']) == 3 and self.objectData['data']['input'] == '.': + self.objectState['value'].append(self.objectData['data']['input']) + elif len(self.objectState['value']) < 3: + self.objectState['value'].append(self.objectData['data']['input']) + + ''' Combine list of characters back to string and then back to a float or int ''' + temp_string = "".join([str(i) for i in self.objectState['value']]) + + if '.' in temp_string: + self.objectData['data']['value'] = float(temp_string) + else: + self.objectData['data']['value'] = int(temp_string) + + def _draw_timer(self): + output_size = self.objectData['size'] + size = (400,200) # Working Canvas Size + fg_color = self.objectData['color'] + + # Create drawing object + canvas = Image.new("RGBA", size) + draw = ImageDraw.Draw(canvas) + + # If not in use, display empty box + if self.objectData['data']['seconds'] > 0: + # Timer Background + draw.rounded_rectangle((15, 15, size[0]-15, size[1]-15), radius=20, fill=(255,255,255,100)) + + # Draw Stopwatch Icon + timer_icon = self._create_icon('\uf2f2', 35, fg_color) + canvas.paste(timer_icon, (40,30), timer_icon) + + # Draw Timer Label on Top Portion of Box + if len(self.objectData['label']) > 11: + label_displayed = self.objectData['label'][0:11] + else: + label_displayed = self.objectData['label'] + + timer_label = self._draw_text(label_displayed, 'trebuc.ttf', 50, fg_color) + canvas.paste(timer_label, (80,30), timer_label) + + # Draw Seconds Remaining + seconds_remaining = f'{self.objectData["data"]["seconds"]}s' + timer_text = self._draw_text(seconds_remaining, 'trebuc.ttf', 100, fg_color) + timer_text_position = ((size[0] // 2) - (timer_text.width // 2), 90) + canvas.paste(timer_text, timer_text_position, timer_text) + + # Resize and Prepare Output + resized = canvas.resize(output_size) + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + + canvas.paste(resized, (0, 0), resized) + + return canvas + + def _draw_alert(self): + output_size = self.objectData['size'] + size = (400,200) # Working Canvas Size + fg_color = self.objectData['color'] + + # Create canvas & drawing object + canvas = Image.new("RGBA", size) + draw = ImageDraw.Draw(canvas) + + if self.objectData['active']: + # Draw Rectangle + draw.rounded_rectangle((15, 15, size[0]-15, size[1]-15), radius=20, outline=fg_color, width=6) + + # Draw Alert Icon + fg_color_alpha = list(fg_color) + fg_color_alpha[3] = 125 + fg_color_alpha = tuple(fg_color_alpha) + + alert_icon = self._create_icon('\uf071', 100, fg_color_alpha) + alert_icon_pos = ((size[0] // 2) - (alert_icon.width // 2), (size[1] // 2) - (alert_icon.height // 2)) + canvas.paste(alert_icon, alert_icon_pos) + + text_lines = self.objectData['data']['text'] + num_lines = len(text_lines) + padding = 50 + line_height = (size[1] - padding) // num_lines + font_size = int(line_height * 0.8) + for index, text in enumerate(text_lines): + if len(text) > 11: + text_displayed = text[0:11] + else: + text_displayed = text + + text_line = self._draw_text(text_displayed, 'trebuc.ttf', font_size, fg_color) + text_pos = ((size[0] // 2) - (text_line.width // 2), (padding // 2) + (line_height * index) + (line_height // 2) - (text_line.height // 2)) + canvas.paste(text_line, text_pos, text_line) + + # Resize and Prepare Output + resized = canvas.resize(output_size) + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + canvas.paste(resized, (0, 0), resized) + + return canvas + + def _draw_pmode_status(self): + output_size = self.objectData['size'] + size = (400,200) # Working Canvas Size + fg_color = self.objectData['color'] + + # Create canvas & drawing object + canvas = Image.new("RGBA", size) + + if self.objectData['active']: + draw = ImageDraw.Draw(canvas) + # Draw Rectangle + draw.rounded_rectangle((15, 15, size[0]-15, size[1]-15), radius=20, outline=fg_color, width=6) + + # Draw PMode Icon + fg_color_alpha = list(fg_color) + fg_color_alpha[3] = 125 + fg_color_alpha = tuple(fg_color_alpha) + + pmode_icon = self._create_icon('\uf83e', 100, fg_color_alpha) + pmode_icon_pos = ((size[0] // 2) - (pmode_icon.width // 2), (size[1] // 2) - 25) + canvas.paste(pmode_icon, pmode_icon_pos) + + # Draw Title + text_displayed = self.objectData['label'] + font_size = 40 + text_line = self._draw_text(text_displayed, 'trebuc.ttf', font_size, fg_color) + text_pos = ((size[0] // 2) - (text_line.width // 2), 25) + canvas.paste(text_line, text_pos, text_line) + + # Draw PMode Number + text_displayed = self.objectData['data']['pmode'] + font_size = 100 + text_line = self._draw_text(text_displayed, 'trebuc.ttf', font_size, fg_color) + text_pos = ((size[0] // 2) - (text_line.width // 2), (size[1] // 2) - 25) + canvas.paste(text_line, text_pos, text_line) + + # Resize and Prepare Output + resized = canvas.resize(output_size) + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + canvas.paste(resized, (0, 0), resized) + + return canvas + + def _draw_splus_status(self): + output_size = self.objectData['size'] + size = (200,200) # Working Canvas Size + + + # Create canvas & drawing object + canvas = Image.new("RGBA", size) + draw = ImageDraw.Draw(canvas) + + fg_color = self.objectData['color'] + + if not self.objectData['active']: + fg_color = (255,255,255,125) + + # Draw Rectangle + padding = 25 + draw.rounded_rectangle((padding, padding, size[0]-padding, size[1]-padding), radius=20, outline=fg_color, width=6) + + # Draw Smoke Plus Icon(s) + cloud_icon = self._create_icon('\uf0c2', 85, fg_color) + cloud_icon_pos = ((size[0] // 2) - (cloud_icon.width // 2) - 8, (size[1] // 2) - (cloud_icon.height // 2)) + canvas.paste(cloud_icon, cloud_icon_pos, cloud_icon) + + plus_icon = self._create_icon('\uf067', 50, fg_color) + plus_icon_pos = (120, 50) + #plus_icon_pos = ((size[0] // 2) - (plus_icon.width // 2), (size[1] // 2) - (plus_icon.height // 2)) + canvas.paste(plus_icon, plus_icon_pos, plus_icon) + + # Resize and Prepare Output + resized = canvas.resize(output_size) + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + canvas.paste(resized, (0, 0), resized) + + return canvas + + def _draw_hopper_status(self): + output_size = self.objectData['size'] + size = (400,200) # Working Canvas Size + #fg_color = self.objectData['color'] + + color_index = int(self.objectData['data']['level'] // (100 / len(self.objectData['color_levels']))) + #print(f'color_index = {color_index-1} level={self.objectData["data"]["level"]}') + fg_color = self.objectData['color_levels'][max(color_index-1, 0)] + + # Create canvas & drawing object + canvas = Image.new("RGBA", size) + draw = ImageDraw.Draw(canvas) + + # Draw Transparent Rectangle + bg_color = (255,255,255,100) if color_index != 0 else fg_color + bg_color = list(bg_color) + bg_color[3] = 100 + bg_color = tuple(bg_color) + draw.rounded_rectangle((15, 15, size[0]-15, size[1]-15), radius=20, fill=bg_color) + + # Draw Title + text_displayed = self.objectData['label'] + font_size = 40 + text_line = self._draw_text(text_displayed, 'trebuc.ttf', font_size, fg_color) + text_pos = ((size[0] // 2) - (text_line.width // 2), 25) + canvas.paste(text_line, text_pos, text_line) + + # Draw Hopper Percentage + text_displayed = str(self.objectData['data']['level']) + '%' + font_size = 100 + text_line = self._draw_text(text_displayed, 'trebuc.ttf', font_size, fg_color) + text_pos = ((size[0] // 2) - (text_line.width // 2), (size[1] // 2) - 25) + canvas.paste(text_line, text_pos, text_line) + + # Draw Bar + level_bar = (40, 160, 360, 170) + current_level_adjusted = int((self.objectData['data']['level'] / 100) * 320) + 40 if self.objectData['data']['level'] > 0 else 40 + if current_level_adjusted > 360: + current_level_adjusted = 360 + current_level_bar = (40, 160, current_level_adjusted, 170) + draw.rounded_rectangle(level_bar, radius=10, fill=(0,0,0,200)) + draw.rounded_rectangle(current_level_bar, radius=10, fill=fg_color) + + # Resize and Prepare Output + resized = canvas.resize(output_size) + canvas = Image.new("RGBA", (output_size[0], output_size[1])) + canvas.paste(resized, (0, 0), resized) + + return canvas diff --git a/display/flexobject_pygame.py b/display/flexobject_pygame.py new file mode 100644 index 00000000..337f33e3 --- /dev/null +++ b/display/flexobject_pygame.py @@ -0,0 +1,48 @@ +''' + Imported Libraries +''' + +from pygame import Surface, SRCALPHA +from pygame import image as PyImage +from PIL import Image, ImageFilter +from display.flexobject_pil import FlexObject + +''' +Display Flex Object Class Definition +''' +class FlexObjectPygame(FlexObject): + def __init__(self, objectType, objectData, background): + super().__init__(objectType, objectData, background) + + def _init_surface(self): + ''' pygame surface for output ''' + self.objectSurface = Surface(self.objectData['size'], SRCALPHA, 32) # Foreground Surface + + def _canvas_to_surface(self): + # Create Temporary Canvas + canvas = Image.new("RGBA", self.objectData['size']) + # Paste Background Chunk onto canvas + canvas.paste(self.objectBG, (0,0)) + # Paste Working Canvas onto canvas + if self.objectData['glow']: + glow = self.objectCanvas.filter(ImageFilter.GaussianBlur(radius = 5)) + canvas.paste(glow, (0,0), glow) + canvas.paste(self.objectCanvas, (0,0), self.objectCanvas) + + # Convert temporary canvas to PyGame surface + strFormat = canvas.mode + size = canvas.size + raw_str = canvas.tobytes("raw", strFormat) + + self.objectSurface = PyImage.fromstring(raw_str, size, strFormat) + + def update_object_data(self, updated_objectData=None): + super().update_object_data(updated_objectData) + + ''' Convert the PIL Canvas to Pygame Surface ''' + self._canvas_to_surface() + + return self.objectSurface + + def get_object_surface(self): + return self.objectSurface diff --git a/display/ili9341.py b/display/ili9341.py index 03c691e1..7841cfcc 100644 --- a/display/ili9341.py +++ b/display/ili9341.py @@ -26,8 +26,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Init Device diff --git a/display/ili9341b.py b/display/ili9341b.py index 53bbf1f4..46f270ee 100644 --- a/display/ili9341b.py +++ b/display/ili9341b.py @@ -28,8 +28,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Init Device diff --git a/display/ili9341e.py b/display/ili9341e.py index 75b1e9e6..250046b3 100644 --- a/display/ili9341e.py +++ b/display/ili9341e.py @@ -28,8 +28,9 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) + self.last_direction = None def _init_display_device(self): # Init Device @@ -54,6 +55,9 @@ def _init_input(self): sw_pin = self.dev_pins['input']['enter_sw'] # Switch - GPIO21 self.input_event = None self.input_counter = 0 + self.last_direction = None + self.last_movement_time = 0 + self.enter_received = False # Init Menu Structures self._init_menu() @@ -71,15 +75,35 @@ def _init_input(self): ============== Input Callbacks ============= ''' def _click_callback(self): - self.input_event='ENTER' + self.input_event = 'ENTER' + self.enter_received = True def _inc_callback(self, v): - self.input_event='UP' - self.input_counter += 1 + current_time = time.time() + if self.last_direction is None or self.last_direction == 'UP' or current_time - self.last_movement_time > 0.5: + if not self.enter_received: + self.input_event = 'UP' + self.input_counter += 1 + self.last_direction = 'UP' + self.last_movement_time = current_time + if time.time() - self.last_movement_time < 0.3: + if self.enter_received: + self.enter_received = False + return # if enter command is received during this time, execute the enter command and not the up def _dec_callback(self, v): - self.input_event='DOWN' - self.input_counter += 1 + current_time = time.time() + if self.last_direction is None or self.last_direction == 'DOWN' or current_time - self.last_movement_time > 0.5: + if not self.enter_received: + self.input_event = 'DOWN' + self.input_counter += 1 + self.last_direction = 'DOWN' + self.last_movement_time = current_time + if time.time() - self.last_movement_time < 0.3: + if self.enter_received: + self.enter_received = False + return # if enter command is received during this time, execute the enter command and not the down + ''' ============== Graphics / Display / Draw Methods ============= diff --git a/display/ili9341em.py b/display/ili9341em.py index 25851082..e785cee5 100644 --- a/display/ili9341em.py +++ b/display/ili9341em.py @@ -29,8 +29,9 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) + self.last_direction = None def _init_display_device(self): # Init Device @@ -55,6 +56,9 @@ def _init_input(self): sw_pin = self.dev_pins['input']['enter_sw'] # Switch - GPIO21 self.input_event = None self.input_counter = 0 + self.last_direction = None + self.last_movement_time = 0 + self.enter_received = False # Init Menu Structures self._init_menu() @@ -72,15 +76,35 @@ def _init_input(self): ============== Input Callbacks ============= ''' def _click_callback(self): - self.input_event='ENTER' + self.input_event = 'ENTER' + self.enter_received = True def _inc_callback(self, v): - self.input_event='UP' - self.input_counter += 1 + current_time = time.time() + if self.last_direction is None or self.last_direction == 'UP' or current_time - self.last_movement_time > 0.5: + if not self.enter_received: + self.input_event = 'UP' + self.input_counter += 1 + self.last_direction = 'UP' + self.last_movement_time = current_time + if time.time() - self.last_movement_time < 0.3: + if self.enter_received: + self.enter_received = False + return # if enter command is received during this time, execute the enter command and not the up def _dec_callback(self, v): - self.input_event='DOWN' - self.input_counter += 1 + current_time = time.time() + if self.last_direction is None or self.last_direction == 'DOWN' or current_time - self.last_movement_time > 0.5: + if not self.enter_received: + self.input_event = 'DOWN' + self.input_counter += 1 + self.last_direction = 'DOWN' + self.last_movement_time = current_time + if time.time() - self.last_movement_time < 0.3: + if self.enter_received: + self.enter_received = False + return # if enter command is received during this time, execute the enter command and not the down + ''' ============== Graphics / Display / Draw Methods ============= diff --git a/display/none.py b/display/none.py index 27b24aa2..dc0118f7 100644 --- a/display/none.py +++ b/display/none.py @@ -15,7 +15,7 @@ class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): self.display_splash() def display_status(self, in_data, status_data): diff --git a/display/prototype.py b/display/prototype.py index d3813bd4..e3f9b2e1 100644 --- a/display/prototype.py +++ b/display/prototype.py @@ -17,7 +17,7 @@ class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): self.units = units curses.wrapper(self._curses_main) curses.curs_set(0) # Invisible Cursor diff --git a/display/pygame_240x320.py b/display/pygame_240x320.py index c2ce55c4..43e06bc8 100644 --- a/display/pygame_240x320.py +++ b/display/pygame_240x320.py @@ -28,8 +28,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Setup & Start Display Loop Thread diff --git a/display/pygame_240x320b.py b/display/pygame_240x320b.py index ad38ae7d..232448f3 100644 --- a/display/pygame_240x320b.py +++ b/display/pygame_240x320b.py @@ -29,8 +29,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) self.eventLogger = logging.getLogger('events') def _init_display_device(self): diff --git a/display/pygame_64x128.py b/display/pygame_64x128.py index 36240f78..ab2b0144 100644 --- a/display/pygame_64x128.py +++ b/display/pygame_64x128.py @@ -27,7 +27,7 @@ ''' class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): # Init Global Variables and Constants self.units = units self.display_active = False diff --git a/display/pygame_dsi_touch.py b/display/pygame_dsi_touch.py deleted file mode 100644 index 8837a733..00000000 --- a/display/pygame_dsi_touch.py +++ /dev/null @@ -1,216 +0,0 @@ -#!/usr/bin/env python3 -''' -***************************************** -PiFire Display Interface Library -***************************************** - - Description: This library supports using pygame - with a DSI attached touch display like the official - Raspberry Pi 7 inch DSI attached display. - - This version supports mouse for development. - -***************************************** -''' - -''' - Imported Libraries -''' -import logging -import time -import threading -import socket -import pygame -from display.base_flex import DisplayBase - -''' -Display class definition -''' -class Display(DisplayBase): - - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) - self.eventLogger = logging.getLogger('events') - - def _init_display_device(self): - # Setup & Start Display Loop Thread - display_thread = threading.Thread(target=self._display_loop) - display_thread.start() - - def _init_input(self): - self.input_enabled = True - self.input_event = None - # Init Menu Structures - self._init_menu() - - def _display_loop(self): - """ - Main display loop - """ - # Init Device - pygame.init() - # set the pygame window name - pygame.display.set_caption('PiFire Device Display') - # Create Display Surface - - if self.raspberry_pi: - self.display_surface = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) - pygame.mouse.set_visible(False) # make mouse pointer invisible - else: - self.display_surface = pygame.display.set_mode(size=(self.WIDTH, self.HEIGHT), flags=pygame.SHOWN) - - self.display_command = 'splash' - - self.touch_pos = (0,0) - - self.clock = pygame.time.Clock() - - # Create Touch Zones - self.touch_enter = pygame.Rect(0, 0, self.WIDTH * 0.75, self.HEIGHT) - self.touch_up = pygame.Rect(self.WIDTH * 0.75, 0, self.WIDTH - (self.WIDTH * 0.25), self.HEIGHT / 2) - self.touch_down = pygame.Rect(self.WIDTH * 0.75, self.HEIGHT / 2, self.WIDTH - (self.WIDTH * 0.25), self.HEIGHT / 2) - - while True: - ''' Add pygame key test here. ''' - pygame.time.delay(250) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - break - # Check for mouse inputs - elif event.type == pygame.MOUSEBUTTONDOWN: - mouse_x, mouse_y = pygame.mouse.get_pos() - #print(f'{mouse_x}, {mouse_y}') - self.touch_pos = (mouse_x, mouse_y) - self.touch_held = True - elif event.type == pygame.MOUSEBUTTONUP: - self.touch_held = False - - # Check for touch inputs - elif event.type == pygame.FINGERDOWN: - touch_x = int(event.x * self.display_surface.get_width()) - touch_y = int(event.y * self.display_surface.get_height()) - #print(f'{touch_x}, {touch_y}') - self.touch_pos = (touch_x, touch_y) - self.touch_held = True - elif event.type == pygame.FINGERUP: - self.touch_held = False - - keys = pygame.key.get_pressed() - if keys[pygame.K_UP]: - self.input_event = 'UP' - elif keys[pygame.K_DOWN]: - self.input_event = 'DOWN' - elif keys[pygame.K_RETURN]: - self.input_event = 'ENTER' - elif keys[pygame.K_x]: - break - elif self.touch_pos != (0,0): - self.input_event = 'TOUCH' - - ''' Normal display loop''' - self._event_detect() - - if self.display_timeout: - if time.time() > self.display_timeout: - self.display_timeout = None - if not self.display_active: - self.display_command = 'clear' - - if self.display_command == 'clear': - self.display_active = False - self.display_timeout = None - self.display_command = None - self._display_clear() - - if self.display_command == 'splash': - self._display_splash() - self.display_timeout = time.time() + 3 - self.display_command = 'clear' - pygame.time.delay(3000) # Hold splash screen for 3 seconds - - if self.display_command == 'text': - self._display_text() - self.display_command = None - self.display_timeout = time.time() + 10 - - if self.display_command == 'network': - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) - network_ip = s.getsockname()[0] - if network_ip != '': - self._display_network(network_ip) - self.display_timeout = time.time() + 30 - self.display_command = None - else: - self.display_text("No IP Found") - - if self.menu_active and not self.display_timeout: - if time.time() - self.menu_time > 5: - self.menu_active = False - self.menu['current']['mode'] = 'none' - self.menu['current']['option'] = 0 - if not self.display_active: - self.display_command = 'clear' - elif not self.display_timeout and self.display_active: - if self.in_data is not None and self.status_data is not None: - self._display_current(self.in_data, self.status_data) - - self.clock.tick(30) - - pygame.quit() - - ''' - ============== Graphics / Display / Draw Methods ============= - ''' - def _display_clear(self): - self.eventLogger.info('Screen Cleared.') - self.display_surface.fill((0,0,0)) - pygame.display.update() - - def _display_canvas(self, canvas): - # Convert to PyGame and Display - strFormat = canvas.mode - size = canvas.size - raw_str = canvas.tobytes("raw", strFormat) - - self.display_image = pygame.image.fromstring(raw_str, size, strFormat) - - self.display_surface.fill((255,255,255)) - self.display_surface.blit(self.display_image, (0, 0)) - - pygame.display.update() - - ''' - ====================== Input & Menu Code ======================== - ''' - def _event_detect(self): - """ - Called to detect input events from buttons, encoder, touch, etc. - """ - command = self.input_event # Save to variable to prevent spurious changes - if command: - self.display_timeout = None # If something is being displayed i.e. text, network, splash then override this - - if command not in ['UP', 'DOWN', 'ENTER', 'TOUCH']: - self.touch_pos = (0,0) - return - elif command == 'TOUCH': - if self.touch_enter.collidepoint(self.touch_pos): - command = 'ENTER' - self.touch_pos = (0,0) - elif self.touch_up.collidepoint(self.touch_pos): - command = 'UP' - if not self.touch_held: - self.touch_pos = (0,0) - elif self.touch_down.collidepoint(self.touch_pos): - command = 'DOWN' - if not self.touch_held: - self.touch_pos = (0,0) - - self.display_command = None - self.display_data = None - self.input_event=None - self.menu_active = True - self.menu_time = time.time() - self._menu_display(command) - diff --git a/display/ssd1306.py b/display/ssd1306.py index f12b4086..5c1fac41 100644 --- a/display/ssd1306.py +++ b/display/ssd1306.py @@ -29,7 +29,7 @@ ''' class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): # Init Global Variables and Constants self.units = units self.display_active = False diff --git a/display/ssd1306b.py b/display/ssd1306b.py index 5a048374..69416828 100644 --- a/display/ssd1306b.py +++ b/display/ssd1306b.py @@ -33,7 +33,7 @@ ''' class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): # Init Global Variables and Constants self.dev_pins = dev_pins self.buttonslevel = buttonslevel diff --git a/display/st7789_240x320.py b/display/st7789_240x320.py index ff7cb728..b734ea0c 100644 --- a/display/st7789_240x320.py +++ b/display/st7789_240x320.py @@ -26,8 +26,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Init Device diff --git a/display/st7789_240x320b.py b/display/st7789_240x320b.py index 3b81f6f3..eb0a46e8 100644 --- a/display/st7789_240x320b.py +++ b/display/st7789_240x320b.py @@ -28,8 +28,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Init Device diff --git a/display/st7789_240x320e.py b/display/st7789_240x320e.py index 5ce69ca7..0aab1d54 100644 --- a/display/st7789_240x320e.py +++ b/display/st7789_240x320e.py @@ -28,8 +28,8 @@ ''' class Display(DisplayBase): - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): - super().__init__(dev_pins, buttonslevel, rotation, units) + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): + super().__init__(dev_pins, buttonslevel, rotation, units, config) def _init_display_device(self): # Init Device diff --git a/display/st7789p.py b/display/st7789p.py index 766cddbe..c648e61e 100644 --- a/display/st7789p.py +++ b/display/st7789p.py @@ -26,7 +26,7 @@ ''' class Display: - def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F'): + def __init__(self, dev_pins, buttonslevel='HIGH', rotation=0, units='F', config={}): # Init Global Variables and Constants self.dev_pins = dev_pins self.rotation = rotation diff --git a/file_mgmt/cookfile.py b/file_mgmt/cookfile.py index 75c1654e..de88d1c5 100644 --- a/file_mgmt/cookfile.py +++ b/file_mgmt/cookfile.py @@ -17,7 +17,7 @@ import zipfile import pathlib -from common import read_settings, read_history, generate_uuid, read_metrics, write_metrics, process_metrics, semantic_ver_to_list, epoch_to_time, unpack_history, default_probe_config +from common import read_settings, read_history, generate_uuid, read_metrics, write_metrics, process_metrics, semantic_ver_to_list, epoch_to_time, unpack_history, default_probe_config, create_logger from file_mgmt.common import read_json_file_data, update_json_file_data HISTORY_FOLDER = './history/' # Path to historical cook files @@ -66,6 +66,8 @@ def create_cookfile(): #global cmdsts global HISTORY_FOLDER + eventLogger = create_logger('events', filename='/tmp/events.log', messageformat='%(asctime)s [%(levelname)s] %(message)s') + settings = read_settings() cook_file_struct = {} @@ -104,29 +106,40 @@ def create_cookfile(): files_list = ['metadata', 'graph_data', 'raw_data', 'graph_labels', 'events', 'comments', 'assets'] if not os.path.exists(HISTORY_FOLDER): os.mkdir(HISTORY_FOLDER) - os.mkdir(f'{HISTORY_FOLDER}{title}') # Make temporary folder for all files + cook_file_path = f'{HISTORY_FOLDER}{title}' + cook_file_name = f'{cook_file_path}.pifire' + cook_file_duplicate = 0 + while(os.path.exists(cook_file_name)): + # If file path exists, attempt to add a new path + cook_file_duplicate += 1 + eventLogger.debug(f'{cook_file_name} exists, attempting to use {cook_file_path}-{cook_file_duplicate}.pifire') + cook_file_name = f'{cook_file_path}-{cook_file_duplicate}.pifire' + + os.mkdir(cook_file_path) # Make temporary folder for all files for item in files_list: json_data_string = json.dumps(cook_file_struct[item], indent=2, sort_keys=True) - filename = f'{HISTORY_FOLDER}{title}/{item}.json' + filename = f'{cook_file_path}/{item}.json' with open(filename, 'w+') as cook_file: cook_file.write(json_data_string) # 2. Create empty data folder(s) & add default data - os.mkdir(f'{HISTORY_FOLDER}{title}/assets') - os.mkdir(f'{HISTORY_FOLDER}{title}/assets/thumbs') + os.mkdir(f'{cook_file_path}/assets') + os.mkdir(f'{cook_file_path}/assets/thumbs') #shutil.copy2('./static/img/pifire-cf-thumb.png', f'{HISTORY_FOLDER}{title}/assets/{thumbnail_UUID}.png') #shutil.copy2('./static/img/pifire-cf-thumb.png', f'{HISTORY_FOLDER}{title}/assets/thumbs/{thumbnail_UUID}.png') # 3. Create ZIP file of the folder - directory = pathlib.Path(f'{HISTORY_FOLDER}{title}/') - filename = f'{HISTORY_FOLDER}{title}.pifire' + directory = pathlib.Path(f'{cook_file_path}/') + filename = cook_file_name with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as archive: for file_path in directory.rglob("*"): archive.write(file_path, arcname=file_path.relative_to(directory)) + eventLogger.debug(f'Wrote {cook_file_name} to {HISTORY_FOLDER}.') + # 4. Cleanup temporary files - command = f'rm -rf {HISTORY_FOLDER}{title}' + command = f'rm -rf {cook_file_path}' os.system(command) # Delete Redis DB for history / current @@ -394,13 +407,13 @@ def prepare_chartdata(probe_config, chart_info={}, num_items=10, reduce=True, da # Build all lists from file data for index in range(list_length - num_items, list_length, step): for key, value in history['P'].items(): - chart_data[probe_mapper['probes'][key]]['data'].append(history['P'][key][index]) + chart_data[probe_mapper['probes'][key]]['data'].append({'x':history['T'][index], 'y':history['P'][key][index]}) for key, value in history['F'].items(): - chart_data[probe_mapper['probes'][key]]['data'].append(history['F'][key][index]) + chart_data[probe_mapper['probes'][key]]['data'].append({'x':history['T'][index], 'y':history['F'][key][index]}) for key, value in history['NT'].items(): - chart_data[probe_mapper['targets'][key]]['data'].append(history['NT'][key][index]) + chart_data[probe_mapper['targets'][key]]['data'].append({'x':history['T'][index], 'y':history['NT'][key][index]}) for key in probe_mapper['primarysp']: - chart_data[probe_mapper['primarysp'][key]]['data'].append(history['PSP'][index]) + chart_data[probe_mapper['primarysp'][key]]['data'].append({'x':history['T'][index], 'y':history['PSP'][index]}) break time_labels.append(history['T'][index]) diff --git a/grillplat/pifire.py b/grillplat/pifire.py index 5e2468e2..a1d79e6d 100644 --- a/grillplat/pifire.py +++ b/grillplat/pifire.py @@ -15,12 +15,15 @@ # Imported Libraries # ***************************************** +import subprocess +from common import is_float, create_logger from gpiozero import OutputDevice from gpiozero import Button class GrillPlatform: def __init__(self, outpins, inpins, triggerlevel='LOW'): + self.logger = create_logger('control') self.outpins = outpins # { 'power' : 4, 'auger' : 14, 'fan' : 15, 'igniter' : 18 } self.inpins = inpins # { 'selector' : 17 } self.current = {} @@ -71,3 +74,132 @@ def get_output_status(self): self.current['power'] = self.power.is_active self.current['fan'] = self.fan.is_active return self.current + + """ + ============================== + System / Platform Commands + ============================== + + Commands callable by outside processes to get status or information, for the platform. + + """ + + def supported_commands(self, arglist): + supported_commands = [ + 'check_throttled', + 'check_wifi_quality', + 'check_cpu_temp', + 'supported_commands', + 'check_alive' + ] + + data = { + 'result' : 'OK', + 'message' : 'Supported commands listed in "data".', + 'data' : { + 'supported_cmds' : supported_commands + } + } + return data + + def check_throttled(self, arglist): + """Checks for under-voltage and throttling using vcgencmd. + + Returns: + (bool, bool): A tuple of (under_voltage, throttled) indicating their status. + """ + + output = subprocess.check_output(["vcgencmd", "get_throttled"]) + status_str = output.decode("utf-8").strip()[10:] # Extract the numerical value + status_int = int(status_str, 16) # Convert from hex to decimal + + under_voltage = bool(status_int & 0x10000) # Check bit 16 for under-voltage + throttled = bool(status_int & 0x5) # Check bits 0 and 2 for active throttling + + if under_voltage or throttled: + message = 'WARNING: Under-voltage or throttled situation detected' + else: + message = 'No under-voltage or throttling detected.' + + data = { + 'result' : 'OK', + 'message' : message, + 'data' : { + 'cpu_under_voltage' : under_voltage, + 'cpu_throttled' : throttled + } + } + self.logger.debug(f'Check Throttled Called. [data = {data}]') + return data + + + def check_wifi_quality(self, arglist): + """Checks the Wi-Fi signal quality on a Raspberry Pi and returns the percentage value (or None if not connected).""" + data = { + 'result' : 'ERROR', + 'message' : 'Unable to obtain wifi quality data.', + 'data' : {} + } + + try: + # Use iwconfig to get the signal quality + output = subprocess.check_output(["iwconfig", "wlan0"]) + lines = output.decode("utf-8").splitlines() + + # Find the line containing "Link Quality" and extract the relevant part + for line in lines: + if "Link Quality=" in line: + quality_str = line.split("=")[1].strip() # Isolate the part after "=" + quality_parts = quality_str.split(" ")[0] # Extract only the first part before spaces + + try: + quality_value, quality_max = quality_parts.split("/") # Split for numerical values + percentage = (int(quality_value) / int(quality_max)) * 100 + data['result'] = 'OK' + data['message'] = 'Successfully obtained wifi quality data.' + data['data']['wifi_quality_value'] = int(quality_value) + data['data']['wifi_quality_max'] = int(quality_max) + data['data']['wifi_quality_percentage'] = round(percentage, 2) # Round to two decimal places + + except ValueError: + # Handle cases where the value might not be directly convertible to an integer + pass + + except subprocess.CalledProcessError: + # Handle errors, such as iwconfig not being found or wlan0 not existing + self.logger.debug(f'Check Throttled had a subprocess error') + pass + + self.logger.debug(f'Check Throttled Called. [data = {data}]') + return data + + def check_cpu_temp(self, arglist): + output = subprocess.check_output(["vcgencmd", "measure_temp"]) + temp = output.decode("utf-8").replace("temp=","").replace("'C", "").replace("\n", "") + + if is_float(temp): + temp = float(temp) + else: + temp = 0.0 + + data = { + 'result' : 'OK', + 'message' : 'Success.', + 'data' : { + 'cpu_temp' : float(temp) + } + } + self.logger.debug(f'Check CPU Temp Called. [data = {data}]') + return data + + def check_alive(self, arglist): + ''' + Simple check to see if the platform is up and running. + ''' + + data = { + 'result' : 'OK', + 'message' : 'The control script is running.', + 'data' : {} + } + return data \ No newline at end of file diff --git a/grillplat/pifire_pwm.py b/grillplat/pifire_pwm.py index f88f943c..1c46babc 100644 --- a/grillplat/pifire_pwm.py +++ b/grillplat/pifire_pwm.py @@ -7,7 +7,7 @@ # Description: This library supports controlling the PiFire PWM PCB, which # integrates solid state relays and PWM amplifier circuitry to drive a 12V DC PWM fan. # -# Relays (augur, igniter) are controlled via Raspberry Pi GPIO pins. +# Relays (augur, igniter) are controlled via GPIO pins. # # 12V power to the fan is controlled via GPIO, which controls a power transistor. # @@ -31,16 +31,17 @@ # Imported Libraries # ***************************************** +import subprocess +from common import is_float, create_logger from gpiozero import OutputDevice from gpiozero import Button from gpiozero.threads import GPIOThread from rpi_hardware_pwm import HardwarePWM -import logging class GrillPlatform: def __init__(self, out_pins, in_pins, trigger_level='LOW', dc_fan=False, frequency=100): - self.logger = logging.getLogger("events") + self.logger = create_logger('control') self.logger.info('grillplat pifire_pwm __init__: ************************************************') self.logger.info('grillplat pifire_pwm __init__: **** Starting Grill Platform Initialization ****') self.logger.info('grillplat pifire_pwm __init__: ************************************************') @@ -184,3 +185,132 @@ def _ramp_device(self, on_time, min_duty_cycle, max_duty_cycle, fps=25): self.current_fan_speed_percent = fan_speed_percent # Keep track of our current fan percent speed if self._ramp_thread.stopping.wait(delay): break + + """ + ============================== + System / Platform Commands + ============================== + + Commands callable by outside processes to get status or information, for the platform. + + """ + + def supported_commands(self, arglist): + supported_commands = [ + 'check_throttled', + 'check_wifi_quality', + 'check_cpu_temp', + 'supported_commands', + 'check_alive' + ] + + data = { + 'result' : 'OK', + 'message' : 'Supported commands listed in "data".', + 'data' : { + 'supported_cmds' : supported_commands + } + } + return data + + def check_throttled(self, arglist): + """Checks for under-voltage and throttling using vcgencmd. + + Returns: + (bool, bool): A tuple of (under_voltage, throttled) indicating their status. + """ + + output = subprocess.check_output(["vcgencmd", "get_throttled"]) + status_str = output.decode("utf-8").strip()[10:] # Extract the numerical value + status_int = int(status_str, 16) # Convert from hex to decimal + + under_voltage = bool(status_int & 0x10000) # Check bit 16 for under-voltage + throttled = bool(status_int & 0x5) # Check bits 0 and 2 for active throttling + + if under_voltage or throttled: + message = 'WARNING: Under-voltage or throttled situation detected' + else: + message = 'No under-voltage or throttling detected.' + + data = { + 'result' : 'OK', + 'message' : message, + 'data' : { + 'cpu_under_voltage' : under_voltage, + 'cpu_throttled' : throttled + } + } + self.logger.debug(f'Check Throttled Called. [data = {data}]') + return data + + + def check_wifi_quality(self, arglist): + """Checks the Wi-Fi signal quality on a Raspberry Pi and returns the percentage value (or None if not connected).""" + data = { + 'result' : 'ERROR', + 'message' : 'Unable to obtain wifi quality data.', + 'data' : {} + } + + try: + # Use iwconfig to get the signal quality + output = subprocess.check_output(["iwconfig", "wlan0"]) + lines = output.decode("utf-8").splitlines() + + # Find the line containing "Link Quality" and extract the relevant part + for line in lines: + if "Link Quality=" in line: + quality_str = line.split("=")[1].strip() # Isolate the part after "=" + quality_parts = quality_str.split(" ")[0] # Extract only the first part before spaces + + try: + quality_value, quality_max = quality_parts.split("/") # Split for numerical values + percentage = (int(quality_value) / int(quality_max)) * 100 + data['result'] = 'OK' + data['message'] = 'Successfully obtained wifi quality data.' + data['data']['wifi_quality_value'] = int(quality_value) + data['data']['wifi_quality_max'] = int(quality_max) + data['data']['wifi_quality_percentage'] = round(percentage, 2) # Round to two decimal places + + except ValueError: + # Handle cases where the value might not be directly convertible to an integer + pass + + except subprocess.CalledProcessError: + # Handle errors, such as iwconfig not being found or wlan0 not existing + self.logger.debug(f'Check Throttled had a subprocess error') + pass + + self.logger.debug(f'Check Throttled Called. [data = {data}]') + return data + + def check_cpu_temp(self, arglist): + output = subprocess.check_output(["vcgencmd", "measure_temp"]) + temp = output.decode("utf-8").replace("temp=","").replace("'C", "").replace("\n", "") + + if is_float(temp): + temp = float(temp) + else: + temp = 0.0 + + data = { + 'result' : 'OK', + 'message' : 'Success.', + 'data' : { + 'cpu_temp' : float(temp) + } + } + self.logger.debug(f'Check CPU Temp Called. [data = {data}]') + return data + + def check_alive(self, arglist): + ''' + Simple check to see if the platform is up and running. + ''' + + data = { + 'result' : 'OK', + 'message' : 'The control script is running.', + 'data' : {} + } + return data \ No newline at end of file diff --git a/grillplat/prototype.py b/grillplat/prototype.py index 4a60335a..e5ffed9b 100644 --- a/grillplat/prototype.py +++ b/grillplat/prototype.py @@ -6,12 +6,12 @@ # # Description: This library simulates # controlling the Grill outputs via -# Raspberry Pi GPIOs, to a 4-channel -# relay +# GPIOs, to a 4-channel relay # # ***************************************** from gpiozero.threads import GPIOThread +from common import is_float class GrillPlatform: @@ -124,3 +124,98 @@ def _ramp_device(self, on_time, min_duty_cycle, max_duty_cycle, fps=25): #print('Set PWM Speed ' + str(percent)) if self._ramp_thread.stopping.wait(delay): break + + """ + ============================== + System / Platform Commands + ============================== + + Commands callable by outside processes to get status or information, for the platform. + + """ + + def supported_commands(self, arglist): + supported_commands = [ + 'check_throttled', + 'check_wifi_quality', + 'check_cpu_temp', + 'supported_commands', + 'check_alive' + ] + + data = { + 'result' : 'OK', + 'message' : 'Supported commands listed in "data".', + 'data' : { + 'supported_cmds' : supported_commands + } + } + return data + + def check_throttled(self, arglist): + """Checks for under-voltage and throttling using vcgencmd. + + Returns: + (bool, bool): A tuple of (under_voltage, throttled) indicating their status. + """ + under_voltage = False + throttled = False + + if under_voltage or throttled: + message = 'WARNING: Under-voltage or throttled situation detected' + else: + message = 'No under-voltage or throttling detected.' + + data = { + 'result' : 'OK', + 'message' : message, + 'data' : { + 'cpu_under_voltage' : under_voltage, + 'cpu_throttled' : throttled + } + } + return data + + def check_wifi_quality(self, arglist): + """Checks the Wi-Fi signal quality on a Raspberry Pi and returns the value (or None if not connected).""" + # Return None if not connected or if there was an error + + data = { + 'result' : 'OK', + 'message' : 'Success.', + 'data' : { + 'wifi_quality_value' : 60, + 'wifi_quality_max' : 70, + 'wifi_quality_percentage' : 80 + } + } + return data + + def check_cpu_temp(self, arglist): + temp = '40.0' + + if is_float(temp): + temp = float(temp) + else: + temp = 0.0 + + data = { + 'result' : 'OK', + 'message' : 'Success.', + 'data' : { + 'cpu_temp' : temp + } + } + return data + + def check_alive(self, arglist): + ''' + Simple check to see if the platform is up and running. + ''' + + data = { + 'result' : 'OK', + 'message' : 'The control script is running.', + 'data' : {} + } + return data diff --git a/installer/debian/build.xml b/installer/debian/build.xml deleted file mode 100644 index 092232f7..00000000 --- a/installer/debian/build.xml +++ /dev/null @@ -1,189 +0,0 @@ - - - Build Debian binary packages for PiFire. - - The binary archives are constructed "by hand", so the task can be - accomplished on platforms that don't provide Debian package tooling. --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2.0 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 2.0 - - - - - - - - - - - - - - - - - diff --git a/installer/debian/other/conffiles b/installer/debian/other/conffiles deleted file mode 100644 index f1fe5875..00000000 --- a/installer/debian/other/conffiles +++ /dev/null @@ -1,2 +0,0 @@ -/usr/local/bin/pifire/settings.json -/usr/local/bin/pifire/pelletdb.json diff --git a/installer/debian/other/control b/installer/debian/other/control deleted file mode 100644 index 37c5e65e..00000000 --- a/installer/debian/other/control +++ /dev/null @@ -1,22 +0,0 @@ -Package: pifire -Maintainer: Ben Parmeter -Version: @VERSION@-@BUILD@ -Priority: optional -Architecture: all -Depends: nginx, - python3-dev, - python3-pip, - python3-pil, - libfreetype6-dev, - libjpeg-dev, - build-essential, - libopenjp2-7, - libtiff5, - gunicorn3, - supervisor, - git, - ttf-mscorefonts-installer, - redis-server -Replaces: pifire (<= 1.0-1) -Homepage: https://github.com/nebhead/PiFire -Description: This project is a WiFi enabled controller for your pellet smoker / grill. This example uses a Raspberry Pi Zero W with Flask WebUI. Based off of PiSmoker by DBorello diff --git a/installer/debian/other/postinst b/installer/debian/other/postinst deleted file mode 100644 index f6d581e1..00000000 --- a/installer/debian/other/postinst +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/bash - -### Check if this is a fresh install or upgrade -if [ -f "/usr/local/bin/pifire/upgradecheck" ]; then - ## Update settings server version - cd /usr/local/bin/pifire || exit 1 # Change dir to where the settings.py application is (and common.py) - python3 settings.py -v @VERSION@ - echo "PiFire has been upgraded" - echo "Starting Supervisor" - service supervisor start - exit 0 -fi - -# Find the rows and columns. Will default to 80x24 if it can not be detected. -screen_size=$(stty size 2>/dev/null || echo 24 80) -rows=$(echo $screen_size | awk '{print $1}') -columns=$(echo $screen_size | awk '{print $2}') - -# Divide by two so the dialogs take up half of the screen. -r=$((rows / 2)) -c=$((columns / 2)) -# If the screen is small, modify defaults -r=$((r < 20 ? 20 : r)) -c=$((c < 70 ? 70 : c)) - -# Display the welcome dialog -whiptail --msgbox --backtitle "Welcome" --title "PiFire Automated Installer" "This installer will transform your Raspberry Pi into a connected Smoker Controller. NOTE: This installer is intended to be run on a fresh install of Raspbian Lite Stretch +. This script is currently in Alpha testing so there may be bugs." ${r} ${c} - -# Install dependencies -sudo -H pip3 install flask -sudo -H pip3 install pushbullet.py -sudo -H pip3 install flask-mobility -sudo -H pip3 install flask-qrcode -sudo -H pip3 install flask-socketio -sudo -H pip3 install eventlet==0.30.2 -sudo -H pip3 install redis -sudo -H pip3 install uuid - -### Setup nginx to proxy to gunicorn -# Delete default configuration -rm /etc/nginx/sites-enabled/default - -# Create link in sites-enabled -ln -s /etc/nginx/sites-available/pifire /etc/nginx/sites-enabled - -# Restart nginx -service nginx restart - -### Setup Supervisor to Start Apps on Boot / Restart on Failures - -SVISOR=$(whiptail --title "Would you like to enable the supervisor WebUI?" --radiolist "This allows you to check the status of the supervised processes via a web browser, and also allows those processes to be restarted directly from this interface. (Recommended)" 20 78 2 "ENABLE_SVISOR" "Enable the WebUI" ON "DISABLE_SVISOR" "Disable the WebUI" OFF 3>&1 1>&2 2>&3) - -if [[ $SVISOR == "ENABLE_SVISOR" ]]; then - if grep -q 'inet_http_server' /etc/supervisor/supervisord.conf; then - whiptail --msgbox --backtitle "Supervisor WebUI Setup" --title "Already Setup" "Appears the WebUI has already been setup reusing exiting settings" ${r} ${c} - else - echo " " | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - echo "[inet_http_server]" | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - echo "port = 9001" | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - USERNAME=$(whiptail --inputbox "Choose a username [default: user]" 8 78 user --title "Choose Username" 3>&1 1>&2 2>&3) - echo "username = " $USERNAME | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - PASSWORD=$(whiptail --passwordbox "Enter your password" 8 78 --title "Choose Password" 3>&1 1>&2 2>&3) - echo "password = " $PASSWORD | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - whiptail --msgbox --backtitle "Supervisor WebUI Setup" --title "Setup Completed" "You now should be able to access the Supervisor WebUI at http://your.ip.address.here:9001 with the username and password you have chosen." ${r} ${c} - fi -else - echo "No WebUI Setup." -fi - -# If supervisor isn't already running, startup Supervisor -service supervisor restart - -## Update settings server version -cd /usr/local/bin/pifire || exit 1 # Change dir to where the settings.py application is (and common.py) -python3 settings.py -v @VERSION@ - -echo " " | sudo tee -a upgradecheck >/dev/null - -# Finish -whiptail --msgbox --backtitle "Install Complete / Reboot Required" --title "Installation Completed" "Congratulations, the installation is complete. At this time, you should reboot your system to complete the installation. On first boot, the wizard will guide you through the remaining setup steps. You should be able to access your application by opening a browser on your PC or other device and using the IP address for this device. Enjoy!" ${r} ${c} - -exit 0 diff --git a/installer/debian/other/postrm b/installer/debian/other/postrm deleted file mode 100644 index 15bb8bf7..00000000 --- a/installer/debian/other/postrm +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -if [ $1 = "remove" ];then - echo "Cleaning up tmp files" - if [ -f "/tmp/events.log" ];then - rm /tmp/events.log - fi - if [ -f "/tmp/tr.log" ]; then - rm /tmp/tr.log - fi - - # Remove upgradecheck file on remove command - if [ -f "/usr/local/bin/pifire/upgradecheck" ]; then - rm /usr/local/bin/pifire/upgradecheck - fi - - # Remove python packages - echo "Removing python packages" - sudo -H pip3 uninstall flask -y - sudo -H pip3 uninstall pushbullet.py -y - sudo -H pip3 uninstall flask_qrcode -y - sudo -H pip3 uninstall flask-socketio -y - sudo -H pip3 uninstall eventlet -y - sudo -H pip3 uninstall redis -y - - echo "Removing pifire site from nginx and restoring default site" - # Remove PiFire site link - rm /etc/nginx/sites-enabled/pifire - - # Return original link in sites-enabled - ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled - - echo "PiFire has been removed" -fi - -exit 0 \ No newline at end of file diff --git a/installer/debian/other/preinst b/installer/debian/other/preinst deleted file mode 100644 index 4d9518ef..00000000 --- a/installer/debian/other/preinst +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# Stop supervisor if it is installed -if [ -f "/etc/supervisor/supervisord.conf" ]; then - service supervisor stop -fi - -# Setting /tmp to RAM based storage in /etc/fstab -if ! grep -q 'tmpfs /tmp tmpfs defaults,noatime 0 0' /etc/fstab ; then - echo "tmpfs /tmp tmpfs defaults,noatime 0 0" | sudo tee -a /etc/fstab > /dev/null -fi - -exit 0 \ No newline at end of file diff --git a/installer/debian/other/prerm b/installer/debian/other/prerm deleted file mode 100755 index f1331ebe..00000000 --- a/installer/debian/other/prerm +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -echo "Stopping Supervisor" -# Stop Supervisor so configs can be removed -service supervisor stop - -if [ $1 = "remove" ] ; then - echo "Cleaning up logs and removing pycache" - rm /usr/local/bin/pifire/logs/*.log - if [ -d "/usr/local/bin/pifire/__pycache__" ] ; then - rm -R /usr/local/bin/pifire/__pycache__ - fi -fi - -exit 0 \ No newline at end of file diff --git a/installer/debian/rpi/conffiles b/installer/debian/rpi/conffiles deleted file mode 100644 index f1fe5875..00000000 --- a/installer/debian/rpi/conffiles +++ /dev/null @@ -1,2 +0,0 @@ -/usr/local/bin/pifire/settings.json -/usr/local/bin/pifire/pelletdb.json diff --git a/installer/debian/rpi/control b/installer/debian/rpi/control deleted file mode 100644 index 57826a59..00000000 --- a/installer/debian/rpi/control +++ /dev/null @@ -1,26 +0,0 @@ -Package: pifire -Maintainer: Ben Parmeter -Version: @VERSION@-@BUILD@ -Priority: optional -Architecture: all -Depends: nginx, - python3-dev, - python3-pip, - python3-pil, - python3-numpy, - python3-spidev, - python3-smbus, - python3-rpi.gpio, - libfreetype6-dev, - libjpeg-dev, - build-essential, - libopenjp2-7, - libtiff5, - gunicorn3, - supervisor, - git, - ttf-mscorefonts-installer, - redis-server -Replaces: pifire (<= 1.0-1) -Homepage: https://github.com/nebhead/PiFire -Description: This project is a WiFi enabled controller for your pellet smoker / grill. This example uses a Raspberry Pi Zero W with Flask WebUI. Based off of PiSmoker by DBorello diff --git a/installer/debian/rpi/postinst b/installer/debian/rpi/postinst deleted file mode 100644 index 29aaa616..00000000 --- a/installer/debian/rpi/postinst +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/bash - -### Check if this is a fresh install or upgrade -if [ -f "/usr/local/bin/pifire/upgradecheck" ]; then - ## Update settings server version - cd /usr/local/bin/pifire || exit 1 # Change dir to where the settings.py application is (and common.py) - python3 settings.py -v @VERSION@ - echo "PiFire has been upgraded" - echo "Starting Supervisor" - service supervisor start - exit 0 -fi - -# Find the rows and columns. Will default to 80x24 if it can not be detected. -screen_size=$(stty size 2>/dev/null || echo 24 80) -rows=$(echo $screen_size | awk '{print $1}') -columns=$(echo $screen_size | awk '{print $2}') - -# Divide by two so the dialogs take up half of the screen. -r=$((rows / 2)) -c=$((columns / 2)) -# If the screen is small, modify defaults -r=$((r < 20 ? 20 : r)) -c=$((c < 70 ? 70 : c)) - -# Display the welcome dialog -whiptail --msgbox --backtitle "Welcome" --title "PiFire Automated Installer" "This installer will transform your Raspberry Pi into a connected Smoker Controller. NOTE: This installer is intended to be run on a fresh install of Raspbian Lite Stretch +. This script is currently in Alpha testing so there may be bugs." ${r} ${c} - -# Install dependencies -sudo -H pip3 install flask -sudo -H pip3 install pushbullet.py -sudo -H pip3 install flask-mobility -sudo -H pip3 install flask-qrcode -sudo -H pip3 install flask-socketio -sudo -H pip3 install eventlet==0.30.2 -sudo -H pip3 install gpiozero -sudo -H pip3 install redis -sudo -H pip3 install uuid - -### Setup nginx to proxy to gunicorn -# Delete default configuration -rm /etc/nginx/sites-enabled/default - -# Create link in sites-enabled -ln -s /etc/nginx/sites-available/pifire /etc/nginx/sites-enabled - -# Restart nginx -service nginx restart - -### Setup Supervisor to Start Apps on Boot / Restart on Failures - -SVISOR=$(whiptail --title "Would you like to enable the supervisor WebUI?" --radiolist "This allows you to check the status of the supervised processes via a web browser, and also allows those processes to be restarted directly from this interface. (Recommended)" 20 78 2 "ENABLE_SVISOR" "Enable the WebUI" ON "DISABLE_SVISOR" "Disable the WebUI" OFF 3>&1 1>&2 2>&3) - -if [[ $SVISOR == "ENABLE_SVISOR" ]]; then - if grep -q 'inet_http_server' /etc/supervisor/supervisord.conf ; then - whiptail --msgbox --backtitle "Supervisor WebUI Setup" --title "Already Setup" "Appears the WebUI has already been setup reusing exiting settings" ${r} ${c} - else - echo " " | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - echo "[inet_http_server]" | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - echo "port = 9001" | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - USERNAME=$(whiptail --inputbox "Choose a username [default: user]" 8 78 user --title "Choose Username" 3>&1 1>&2 2>&3) - echo "username = " $USERNAME | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - PASSWORD=$(whiptail --passwordbox "Enter your password" 8 78 --title "Choose Password" 3>&1 1>&2 2>&3) - echo "password = " $PASSWORD | sudo tee -a /etc/supervisor/supervisord.conf >/dev/null - whiptail --msgbox --backtitle "Supervisor WebUI Setup" --title "Setup Completed" "You now should be able to access the Supervisor WebUI at http://your.ip.address.here:9001 with the username and password you have chosen." ${r} ${c} - fi -else - echo "No WebUI Setup." -fi - -# If supervisor isn't already running, startup Supervisor -service supervisor restart - -## Update settings server version -python3 settings.py -v @VERSION@ - -echo " " | sudo tee -a upgradecheck >/dev/null - -# Finish -whiptail --msgbox --backtitle "Install Complete / Reboot Required" --title "Installation Completed" "Congratulations, the installation is complete. At this time, you should reboot your system to complete the installation. On first boot, the wizard will guide you through the remaining setup steps. You should be able to access your application by opening a browser on your PC or other device and using the IP address for this Pi. Enjoy!" ${r} ${c} - -exit 0 diff --git a/installer/debian/rpi/postrm b/installer/debian/rpi/postrm deleted file mode 100644 index e45b7e4b..00000000 --- a/installer/debian/rpi/postrm +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -if [ $1 = "remove" ];then - echo "Cleaning up tmp files" - if [ -f "/tmp/events.log" ];then - rm /tmp/events.log - fi - if [ -f "/tmp/tr.log" ]; then - rm /tmp/tr.log - fi - - # Remove upgradecheck file on remove command - if [ -f "/usr/local/bin/pifire/upgradecheck" ]; then - rm /usr/local/bin/pifire/upgradecheck - fi - - # Remove python packages - echo "Removing python packages" - sudo -H pip3 uninstall flask -y - sudo -H pip3 uninstall pushbullet.py -y - sudo -H pip3 uninstall flask_qrcode -y - sudo -H pip3 uninstall flask-socketio -y - sudo -H pip3 uninstall eventlet -y - sudo -H pip3 uninstall gpiozero -y - sudo -H pip3 uninstall redis -y - - echo "Removing pifire site from nginx and restoring default site" - # Remove PiFire site link - rm /etc/nginx/sites-enabled/pifire - - # Return original link in sites-enabled - ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled - - echo "PiFire has been removed" -fi - -exit 0 \ No newline at end of file diff --git a/installer/debian/rpi/preinst b/installer/debian/rpi/preinst deleted file mode 100644 index 4d9518ef..00000000 --- a/installer/debian/rpi/preinst +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# Stop supervisor if it is installed -if [ -f "/etc/supervisor/supervisord.conf" ]; then - service supervisor stop -fi - -# Setting /tmp to RAM based storage in /etc/fstab -if ! grep -q 'tmpfs /tmp tmpfs defaults,noatime 0 0' /etc/fstab ; then - echo "tmpfs /tmp tmpfs defaults,noatime 0 0" | sudo tee -a /etc/fstab > /dev/null -fi - -exit 0 \ No newline at end of file diff --git a/installer/debian/rpi/prerm b/installer/debian/rpi/prerm deleted file mode 100755 index f1331ebe..00000000 --- a/installer/debian/rpi/prerm +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/bash - -echo "Stopping Supervisor" -# Stop Supervisor so configs can be removed -service supervisor stop - -if [ $1 = "remove" ] ; then - echo "Cleaning up logs and removing pycache" - rm /usr/local/bin/pifire/logs/*.log - if [ -d "/usr/local/bin/pifire/__pycache__" ] ; then - rm -R /usr/local/bin/pifire/__pycache__ - fi -fi - -exit 0 \ No newline at end of file diff --git a/installer/nginx-configs/pifire.nginx b/installer/nginx-configs/pifire.nginx deleted file mode 100644 index ae4e06c4..00000000 --- a/installer/nginx-configs/pifire.nginx +++ /dev/null @@ -1,22 +0,0 @@ -server { - location / { - proxy_pass http://127.0.0.1:8000; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - - } - - location /socket.io { - proxy_pass http://127.0.0.1:8000/socket.io; - - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "Upgrade"; - - } -} \ No newline at end of file diff --git a/installer/supervisor/control.conf b/installer/supervisor/control.conf deleted file mode 100755 index 80d5877a..00000000 --- a/installer/supervisor/control.conf +++ /dev/null @@ -1,9 +0,0 @@ -[program:control] -command=/usr/bin/python3 /usr/local/bin/pifire/control.py -directory=/usr/local/bin/pifire -autostart=true -autorestart=true -startretries=3 -stderr_logfile=/usr/local/bin/pifire/logs/control.err.log -stdout_logfile=/usr/local/bin/pifire/logs/control.out.log -user=root diff --git a/installer/supervisor/webapp.conf b/installer/supervisor/webapp.conf deleted file mode 100755 index bab2a93a..00000000 --- a/installer/supervisor/webapp.conf +++ /dev/null @@ -1,9 +0,0 @@ -[program:webapp] -command=/usr/bin/gunicorn3 -k eventlet -b 0.0.0.0:8000 -w 1 app:app -directory=/usr/local/bin/pifire -autostart=true -autorestart=true -startretries=3 -stderr_logfile=/usr/local/bin/pifire/logs/webapp.err.log -stdout_logfile=/usr/local/bin/pifire/logs/webapp.out.log -user=root diff --git a/license.txt b/license.txt index a287d551..cc7c6e48 100755 --- a/license.txt +++ b/license.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Ben Parmeter +Copyright (c) 2020-2024 Ben Parmeter Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/notify/mqtt_handler.py b/notify/mqtt_handler.py new file mode 100644 index 00000000..aafaaaad --- /dev/null +++ b/notify/mqtt_handler.py @@ -0,0 +1,473 @@ +#!/usr/bin/env python3 + +# ***************************************** +# PiFire Mqtt Interface Library +# ***************************************** +# +# Description: This library supports publishing data to an mqtt +# broker, including metadata for homeassistant +# sensor autodiscovery. +# +# Install Dependencies: +# +# sudo pip3 install paho-mqtt +# +# ***************************************** + +import paho.mqtt.client as mqtt +import json +import logging +import time +from socket import getfqdn +from common import create_logger +import psutil +#from common import write_control + + +class MqttNotificationHandler: + + def __init__(self, settings) -> None: + """ Initialize an mqtt client + Home Assistant mqtt sensor metadata: https://www.home-assistant.io/integrations/sensor.mqtt/ + Home Assistant auto-discovery description: https://www.home-assistant.io/integrations/mqtt/ + + Arguments: + settings: PiFire settings dict + """ + + try: + self.client = None # Initialize to none so we can check its existance later + self.initialized_topics = [] # Topics that have already sent auto-discover data + self.last = {} # Last published values so we can send by exception + self.last_mode = None # Last mode we were in + self.last_conn_time = 0 # Last time we tried to connect to the mqtt broker + self.subscriptions = [] # Topics we have subscribed to so we can be remotely controlled + self.control = None # Link to the control structure so we can send controls to the Control app + + # Keep track of the last time we published different types of MQTT data so that we can + # throttle our updates to the configured rate + self.pub_times = {'base': 0, 'pellet': 0, 'pid': 0} + self.pub_rate = float(settings['notify_services']['mqtt']['update_sec']) + + # Create shortcuts to settings we will use frequently + self._global_settings = settings['globals'] + self._probe_settings = settings['probe_settings']['probe_map']['probe_info'] + self._mqtt_settings = settings['notify_services']['mqtt'] + + # This lists contexts that will be published to mqtt + self.CONTEXTS = ['control','devices', + 'probe_data_primary','probe_data_food','probe_data_aux','probe_data_tr', + 'pid', 'pid_config','pid_cycle_data','pellet','notify_event','system'] + + # These list the devices aka attributes that will be published to mqtt + self.DEVICE_SENSORS = ['auger','igniter','power','fan','notify_event'] + self.CONTROL_SENSORS = ['mode','next_mode','s_plus','pwm_control','duty_cycle','status','primary_setpoint'] + self.PID_CONFIG_SENSORS = ['PB','Td','Ti','center'] + self.PID_CYCLE_TIME_SENSORS = ['HoldCycleTime','LidOpenPauseTime','LidOpenThreshold','PMode','SmokeCycleTime','u_max','u_min'] + self.PID_SENSORS = ['kp','ki','kd','p','i','d','u','error','derv','inter','inter_max','cycle_ratio'] + self.HOPPER_SENSORS = ['hopper_level'] + self.CONTROL_NOTIFY_SENSORS = ['target'] + #self.LAST_NOTIFICATION = ['msg'] + self.SYSTEM_SENSORS = ['cpu','available_memory','free_memory','cpu_temp'] + + # Setup logging + log_level = logging.DEBUG if settings['globals']['debug_mode'] else logging.INFO + self._mqttLogger = create_logger('mqtt', filename='./logs/mqtt.log', level=log_level, + messageformat='%(asctime)s | %(levelname)s | %(thread)s | %(message)s') + + # Default payload for setting up home assistant auto discovery messagage. + self.discovery_topic = self._mqtt_settings['homeassistant_autodiscovery_topic'] + self.pifire_id = self._mqtt_settings['id'] + grill_name = "PiFire" if len(self._global_settings['grill_name']) == 0 else self._global_settings['grill_name'] + self.default_payload = { + 'availability': [{ + 'topic': f"{self.pifire_id}/availability", + }], + 'availability_mode': 'all', + 'device': { + 'identifiers': [ self.pifire_id ], + 'manufacturer': "PiFire", + 'model': settings['modules']['grillplat'], + 'name': grill_name, + 'configuration_url': f"http://{getfqdn()}:5000/settings" + }, + 'enabled_by_default': True, + } + + # Connect to the broker + self._check_connection() + + except: + self._mqttLogger.exception(f'Error initializing the mqtt class object: ') + + def __del__(self): + try: + self._publish_data(topic=f"{self._mqtt_settings['id']}/availability",payload="offline") + self.client.loop_stop() + self.client.disconnect() + except: + self._mqttLogger.exception(f'Error occurred closing mqtt client: ') + + def _connect(self): + try: + settings = self._mqtt_settings + + # Initialize our client if not already intialized + if self.client == None: + + # Future: may want to make these configurable + self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, transport='tcp', protocol=mqtt.MQTTv5) + self.client.on_connect = self._on_connect + self.client.on_disconnect = self._on_disconnect + self.client.on_connect_fail = self._on_connect_fail + self.client.on_message = self._on_message + self.client.will_set(topic=f"{settings['id']}/availability",payload="offline") + + if len(settings['username']) > 0: + self.client.username_pw_set(settings['username'], settings['password']) + + # Future: may want to support tls + #self.client.tls_set( certfile=None, keyfile=None, cert_reqs=None) + + ret = self.client.connect(host=settings['broker'], port=int(settings['port']), + keepalive=10) + + if ret == mqtt.MQTT_ERR_SUCCESS: + self.client.loop_start() #Will run async forever, monitoring mqtt and sending keepalives + else: + self._mqttLogger.error(f"Error {mqtt.connack_string(ret)} connecting to {settings['broker']}.") + self.client = None + except: + self._mqttLogger.exception(f'Error occurred connecting to the mqtt broker: ') + + def _on_connect(self, client, userdata, flags, rc, properties): + self._mqttLogger.info(f"Connection to '{self._mqtt_settings['broker']}' returned result: '{mqtt.connack_string(rc)}'") + self.last_conn_time = 0 + self._publish_data(topic=f"{self._mqtt_settings['id']}/availability",payload="online", qos=1) + + # Restore any subscriptions that we have + for sub in self.subscriptions: + self._subscribe(sub) + + def _on_message(self, client, userdata, msg): + + # Ignore if we haven't finished intializing or haven't enabled control + #if self.control == None: return + #if self._mqtt_settings['control'] == False: return + + # element = msg.topic.split('/')[-1] + # payload = msg.payload.decode('utf-8') + + # self._mqttLogger.debug(f"Recieved message {payload} for {element}") + + # if element == "mode": + # # If going into Hold mode we need to set the setpoint as well. Default to 150 if not set. + # if payload == 'Hold': + # self.control['primary_setpoint'] = max(self.control['primary_setpoint'], 150) + + # self.control[element] = payload + # self.control['updated'] = 'yes' + # write_control(self.control, direct_write=False, origin='mqtt') + + # #TODO when switching to HOLD mode we also need to send a setpoint + + # elif element == "primary_setpoint": + # #Only adjust the setpoint if we are in Hold mode + # if (self.control['mode']) == 'Hold': + # self.control[element] = int(payload) + # self.control['updated'] = 'yes' + # write_control(self.control, direct_write=False, origin='mqtt') + + # else: + # pass + pass + + def _on_disconnect(self, client, userdata, rc, properties): + if rc != 0: + self._mqttLogger.error(f"Disconnect returned result: {mqtt.connack_string(rc)}") + else: + self._mqttLogger.info(f"Disconnect returned result: {mqtt.connack_string(rc)}") + + def _on_connect_fail(self, client, userdata, rc): + self._mqttLogger.error(f"Connection failure: {mqtt.connack_string(rc)}") + + def _check_connection(self): + + # Connection process is async, so give it some time + if time.time() < self.last_conn_time + 5: return False + + # Connect if not already connected + if self.client is None or not self.client.is_connected(): + self._mqttLogger.error(f"Need to connect to the broker") + self._connect() + self.last_conn_time = time.time() + + return self.client.is_connected() + + def _check_homeassistant(self): + return len(self._mqtt_settings['homeassistant_autodiscovery_topic']) > 0 + + def _publish_data(self, topic, payload, qos=0, retain=False, properties=None): + + if not self._check_connection(): + return False + + ret= self.client.publish(topic, payload, qos, retain, properties) + + # Check the return + if ret.rc == mqtt.MQTT_ERR_SUCCESS: + return True + elif ret.rc == mqtt.MQTT_ERR_CONN_LOST: + self._mqttLogger.error(f"Cannot publish data for {topic} because the mqtt connection is lost.") + return False + elif ret.rc == mqtt.MQTT_ERR_AUTH: + self._mqttLogger.error(f"Cannot publish data for {topic} because of error {mqtt.connack_string(ret.rc)}") + self.client = None + else: + self._mqttLogger.error(f"Cannot publish data for {topic} because of error {mqtt.connack_string(ret.rc)}") + return False + + def _publish_autodiscover(self, category, topic, payload, qos=2, retain=True, properties=None): + + ret= self.client.publish(topic, payload, qos, retain, properties) + + # Check the return + if ret.rc == mqtt.MQTT_ERR_SUCCESS: + if not category in self.initialized_topics: + self.initialized_topics.append(category) + elif ret.rc == mqtt.MQTT_ERR_CONN_LOST: + self._mqttLogger.error(f"Cannot publish autodiscover data for {topic} because the mqtt connection is lost.") + else: + self._mqttLogger.error(f"Cannot publish autodiscover data for {topic} because of error {mqtt.connack_string(ret.rc)}") + + def _publish(self, context, data): + + # Extract the supported attributes and verify there is a change + change_detected = False + payload = {} + for device in data: + if (context == 'devices' and device in self.DEVICE_SENSORS) or \ + (context == 'control' and device in self.CONTROL_SENSORS) or \ + (context == 'pid_config' and device in self.PID_CONFIG_SENSORS) or \ + (context == 'pid_cycle_data' and device in self.PID_CYCLE_TIME_SENSORS) or \ + (context == 'pellet') and device in self.HOPPER_SENSORS or \ + (context == 'pid' and device in self.PID_SENSORS) or \ + (context == 'notify_event') or \ + (context == 'system') or \ + (context.startswith('control_notify_data') and device in self.CONTROL_NOTIFY_SENSORS) or \ + (context.startswith("probe_data")): + + device_name = context + '_' + device + last_val = self.last.get(device_name) + payload[device] = data[device] + new_val = data[device] + if new_val != last_val: + change_detected = True + self.last[device_name] = new_val + + if not change_detected: return + + if self._publish_data(topic=f"{self._mqtt_settings['id']}/{context}", payload=json.dumps(payload)): + + # Publish home assitant auto-discovery info + if self._check_homeassistant: + self._create_autodiscover(context, data) + + def _subscribe(self,topic): + self.client.subscribe(topic) + if topic not in self.subscriptions: + self.subscriptions.append(topic) + + def _create_autodiscover(self, context, data): + for device in data: + device_name = context + '_' + device + topic_name = device_name + if device_name in self.last: + if not device_name in set(self.initialized_topics): + + discovery = self.default_payload.copy() + discovery['state_topic'] = f"{self.pifire_id}/{context}" + discovery['object_id'] = f"{self.pifire_id}_{device_name}".lower() + discovery['unique_id'] = f"{self.pifire_id}_{device_name}".lower() + discovery['value_template'] = f"{{{{ value_json.{device} }}}}" + discovery['name'] = device.title().replace('_',' ') + + datatype = type(data[device]) + + if datatype == bool: + component = "binary_sensor" + discovery['payload_on'] = True + discovery['payload_off'] = False + + if device not in {'auger','igniter','power','fan'}: + discovery['enabled_by_default'] = False + + elif datatype == str: + component = "sensor" + + elif datatype == int or datatype == float: + component = "sensor" + discovery['state_class'] = "measurement" + + if context in ['probe_data_primary', 'probe_data_food','probe_data_aux']: + discovery['device_class'] = "temperature" + discovery['unit_of_measurement'] = f"°{self._global_settings['units']}" + suffix = 'Temp' + + elif context == 'probe_data_tr': + discovery['unit_of_measurement'] = "ohms" + discovery['enabled_by_default'] = False + discovery['entity_category'] = "diagnostic" + suffix = 'RTD Ohms' + + elif context.startswith('probe_data'): + # Find this probes name in the settings + for probe in self._probe_settings: + if probe['label'] == device: + discovery['name'] = f"{discovery['name']} {suffix}" + topic_name = context + '_' + probe['port'] + break + + elif context.startswith('control_notify'): + discovery['device_class'] = "temperature" + discovery['unit_of_measurement'] = f"°{self._global_settings['units']}" + suffix = 'Target' + for probe in self._probe_settings: + if probe['label'] == data['label']: + discovery['name'] = f"{data['name']} {suffix}" + topic_name = context + break + + elif context.startswith('pid'): + discovery['entity_category'] = "diagnostic" + discovery['enabled_by_default'] = False + + if device in ['u_min','u_max','center','p','i','d','u','cycle_ratio']: + discovery['unit_of_measurement'] = "%" + discovery['enabled_by_default'] = False + discovery['value_template'] = f"{{{{ value_json.{device} | round(2)}}}}" + + elif device in ['available_memory', 'free_memory']: + discovery['unit_of_measurement'] = "b" + + elif device in ['duty_cycle', 'hopper_level','cpu']: + discovery['unit_of_measurement'] = "%" + + elif device in ['PB', 'Td', 'Ti', 'HoldCycleTime', 'LidOpenPauseTime']: + discovery['unit_of_measurement'] = "s" + discovery['enabled_by_default'] = False + + elif device == 'cpu_temp': + discovery['device_class'] = "temperature" + discovery['unit_of_measurement'] = "°C" + + elif device in ['primary_setpoint']: + discovery['device_class'] = "temperature" + discovery['unit_of_measurement'] = f"°{self._global_settings['units']}" + + #if self._mqtt_settings['control']: + #component = "number" + + # Make setpoint subscribeable and subscribe to it + #discovery['command_topic'] = f"{discovery['state_topic']}/set/{device}" + #discovery['min'] = 100 + #discovery['max'] = 700 + #discovery['mode'] = 'auto' + #discovery['optimistic'] = 'false' + #discovery['step'] = 1 + #self._subscribe(discovery['command_topic']) + + self._publish_autodiscover( + device, + f"{self.discovery_topic}/{component}/{self.pifire_id}/{topic_name}/config", + json.dumps(discovery)) + + self.initialized_topics.append(device_name) + + def notify(self, context: str, data: dict): + """ Publish changed data to the mqtt broker + + Parameters: + context (str): Description of the data (devices, probes, control, cycle) + data (dict): Key-value pairs of data to publish + """ + try: + # Split out nested dictionaries + for key in data: + if key == 'notify_data': + for device in data[key]: + #new_context = context + '_' + key + '_' + device['label'] + for probe in self._probe_settings: + if probe['label'] == device['label']: + new_context = context + '_' + key + '_' + probe['port'] + break + self.notify(new_context, device ) + elif isinstance(data[key], dict): + new_context = context + '_' + key + self.notify(new_context, data[key]) + + # Save the latest control info + if context == 'control' and self.control == None: + self.control = data + + # Get system info if requested + if context == 'system': + data = {} + data['cpu'] = psutil.cpu_percent(interval=None) + data['available_memory'] = psutil.virtual_memory().available + data['free_memory'] = psutil.virtual_memory().free + if 'sensors_temperatures' in dir(psutil): + # This is for raspberry PI. Other hardware may be different + temps = psutil.sensors_temperatures() + if 'cpu_thermal' in temps: + cpu_temps = temps['cpu_thermal'] + data['cpu_temp'] = cpu_temps[0].current + + # Publish the data we are interested in + if context in self.CONTEXTS or context.startswith('control_notify_data'): + self._publish(context, data) + + # Check to see if we have changed operating mode + if context != 'control' or data['mode'] == self.last_mode: return + + new_mode = data['mode'] + + # Update the message state if we moved to a more advanced state (ignore Stop and Error) + # all modes ['Stop','Recipe','Error','Reignite','Monitor','Prime','Startup','Smoke','Hold','Shutdown','Manual'] + #if new_mode in ['Recipe', 'Reignite','Monitor','Prime','Startup','Smoke','Hold','Shutdown','Manual']: + payload = {'msg': f"Entered {new_mode} mode" } + self.notify("notify_event", payload) + + self.last_mode = data['mode'] + + # Zero the PID data if not controlling + if data['mode'] not in ['Hold']: + payload = {} + for key in self.PID_SENSORS: + payload[key] = 0 + self._publish('pid', payload) + + # Zero the temps if stopped + if data['mode'] == "Stop": + primary_data = {} + food_data = {} + aux_data = {} + tr_data = {} + for probe in self._probe_settings: + if probe['type'] == 'Food': + food_data[probe['label']] = 0 + elif probe['type'] == 'Primary': + primary_data[probe['label']] = 0 + elif probe['type'] == 'aux': + aux_data[probe['label']] = 0 + else: + pass + + tr_data[probe['label']] = 0 + self._publish('probe_data_primary', primary_data) + self._publish('probe_data_food', food_data) + self._publish('probe_data_aux', aux_data) + self._publish('probe_data_tr', tr_data) + + except: + self._mqttLogger.exception(f'Error occurred publishing device data: ') diff --git a/notify/notifications.py b/notify/notifications.py index 20280b5b..e4b7bc4a 100644 --- a/notify/notifications.py +++ b/notify/notifications.py @@ -22,7 +22,9 @@ import json import apprise import logging -from common import write_settings, write_control, create_logger +import math +from common import write_settings, write_control, create_logger, read_history +from scipy.interpolate import interp1d ''' ============================================================================== @@ -30,7 +32,8 @@ ============================================================================== ''' -def check_notify(in_data, control, settings, pelletdb, grill_platform): + +def check_notify(settings, control, in_data=None, pelletdb=None, grill_platform=None, pid_data=None, update_eta=False): """ Check for any pending notifications @@ -40,20 +43,50 @@ def check_notify(in_data, control, settings, pelletdb, grill_platform): :param pelletdb: Pellet DB :param grill_platform: Grill Platform """ + + # If pelletdb or grill_platform is not populated, exit + if not pelletdb or not grill_platform: + return + + # Forward to mqtt if enabled. + if settings['notify_services'].get('mqtt') != None and \ + settings['notify_services']['mqtt']['enabled'] == True: + _send_mqtt_notification(control, settings, pelletdb, in_data, grill_platform, pid_data) + if settings['notify_services']['influxdb']['url'] != '' and settings['notify_services']['influxdb']['enabled']: _send_influxdb_notification('GRILL_STATE', control, settings, pelletdb, in_data, grill_platform) ''' Get simple list of temperatures key:value pairs ''' probe_temp_list = {} - for group in in_data['probe_history']: - if group != 'tr': - for probe in in_data['probe_history'][group]: - probe_temp_list[probe] = in_data['probe_history'][group][probe] + if in_data is not None: + for group in in_data['probe_history']: + if group != 'tr': + for probe in in_data['probe_history'][group]: + probe_temp_list[probe] = in_data['probe_history'][group][probe] ''' Process all registered notification items ''' for index, item in enumerate(control['notify_data']): if item['req']: - if item['type'] == 'probe': + if item['type'] == 'probe' and in_data is not None: + # Update the ETA, if requested for any active probe + if update_eta: + num_minutes = 20 # Number of minutes of history to grab + num_seconds = num_minutes * 60 + time_interval = 3 # 3-Second Time Intervals + # Get temperature history for this probe + temperatures = [] + history = read_history(num_items=(num_seconds // time_interval)) + for datapoint in history: + if item['label'] in datapoint['F']: + temperatures.append(datapoint['F'][item['label']]) + elif item['label'] in datapoint['P']: + temperatures.append(datapoint['P'][item['label']]) + # Call extrapolate ETA + #print(f'DEBUG: ETA: Interpolating {item["name"]}') + eta_seconds = _estimate_eta(temperatures, item['target'], interval_seconds=time_interval, max_history_minutes=num_minutes) + # Write to control + control['notify_data'][index]['eta'] = eta_seconds + # If target temperature achieved, send notification and clear request/data if probe_temp_list[item['label']] >= in_data['notify_targets'][item['label']]: send_notifications("Probe_Temp_Achieved", control, settings, pelletdb, label=item['label'], target=in_data['notify_targets'][item['label']]) if control['mode'] == 'Recipe': @@ -61,6 +94,7 @@ def check_notify(in_data, control, settings, pelletdb, grill_platform): control['recipe']['step_data']['triggered'] = True control['notify_data'][index]['req'] = False control['notify_data'][index]['target'] = 0 + control['notify_data'][index]['eta'] = None elif item['type'] == 'timer': if time.time() >= control['timer']['end']: @@ -79,6 +113,11 @@ def check_notify(in_data, control, settings, pelletdb, grill_platform): send_notifications("Pellet_Level_Low", control, settings, pelletdb) control['notify_data'][index]['last_check'] = time.time() + elif item['type'] == 'test': + send_notifications("Test_Notify", control, settings, pelletdb) + control['notify_data'][index]['last_check'] = time.time() + control['notify_data'][index]['req'] = False + ''' Do Shutdown or Keep Warm if Requested ''' if item['shutdown'] and control['mode'] in ('Reignite', 'Startup', 'Smoke', 'Hold') and not control['notify_data'][index]['req']: control['mode'] = 'Shutdown' @@ -170,6 +209,12 @@ def send_notifications(notify_event, control, settings, pelletdb, label='Probe', channel = 'pifire_recipe_message' query_args = {"value1": control['recipe']['step_data']['message']} eventLogger.info(body_message) + elif "Test_Notify" in notify_event: + title_message = "Test Notification" + body_message = "This is a test notification from PiFire." + channel = 'pifire_test_message' + query_args = {"value1": "This is a test notification from PiFire."} + eventLogger.info(body_message) else: title_message = "PiFire: Unknown Notification issue" body_message = "Whoops! PiFire had the following unhandled notify event: " + notify_event + " at " + str(now) @@ -188,7 +233,8 @@ def send_notifications(notify_event, control, settings, pelletdb, label='Probe', _send_pushover_notification(settings, title_message, body_message) if settings['notify_services']['onesignal']['app_id'] != '' and settings['notify_services']['onesignal']['enabled']: _send_onesignal_notification(settings, title_message, body_message, channel) - + if settings['notify_services']['mqtt']['broker'] != '' and settings['notify_services']['mqtt']['enabled']: + _send_mqtt_notification(control, settings, notify_event=title_message) def _send_apprise_notifications(settings, title_message, body_message): """ @@ -222,28 +268,37 @@ def _send_pushover_notification(settings, title_message, body_message): :param title_message: Message Title :param body_message: Message Body """ - log_level = logging.DEBUG if settings['globals']['debug_mode'] else logging.INFO - eventLogger = create_logger('events', filename='/tmp/events.log', messageformat='%(asctime)s [%(levelname)s] %(message)s', level=log_level) - url = 'https://api.pushover.net/1/messages.json' - for user in settings['notify_services']['pushover']['UserKeys'].split(','): - try: - response = requests.post(url, data={ - "token": settings['notify_services']['pushover']['APIKey'], - "user": user.strip(), - "message": body_message, - "title": title_message, - "url": settings['notify_services']['pushover']['PublicURL'] - }) + eventLogger = create_logger('events', filename='/tmp/events.log', messageformat='%(asctime)s [%(levelname)s] %(message)s') + if settings['globals']['debug_mode']: + eventLogger.setLevel(logging.DEBUG) + else: + eventLogger.setLevel(logging.INFO) - if not response.status_code == 200: - eventLogger.warning("Pushover Notification Failed: " + title_message) + appriseHandler = apprise.Apprise() - eventLogger.debug("Pushover Response: " + response.text) + token = settings["notify_services"]["pushover"]["APIKey"] + public_url = settings["notify_services"]["pushover"]["PublicURL"] - except Exception as e: - eventLogger.warning("Pushover Notification to %s failed: %s" % (user, e)) - except: - eventLogger.warning("Pushover Notification to %s failed for unknown reason." % (user)) + for user in settings['notify_services']['pushover']['UserKeys'].split(','): + user_id = user.strip() + apprise_url = f'pover://{user_id}@{token}?url={public_url}' + appriseHandler.add(apprise_url) + + try: + result = appriseHandler.notify( + title=title_message, + body=body_message, + ) + + if result: + eventLogger.debug(f"Pushover Notification to {user} was a success!") + else: + eventLogger.warning(f"Pushover Notification to {user} failed!") + + except Exception as e: + eventLogger.warning(f"Pushover Notification to {user} failed: {e}") + except: + eventLogger.warning(f"Pushover Notification to {user} failed for unknown reason.") def _send_pushbullet_notification(settings, title_message, body_message): @@ -255,27 +310,35 @@ def _send_pushbullet_notification(settings, title_message, body_message): :param body_message: Message Body :return: """ - log_level = logging.DEBUG if settings['globals']['debug_mode'] else logging.INFO - eventLogger = create_logger('events', filename='/tmp/events.log', messageformat='%(asctime)s [%(levelname)s] %(message)s', level=log_level) - api_key = settings['notify_services']['pushbullet']['APIKey'] - pushbullet_link = settings['notify_services']['pushbullet']['PublicURL'] - url = "https://api.pushbullet.com/v2/pushes" + eventLogger = create_logger('events', filename='/tmp/events.log', messageformat='%(asctime)s [%(levelname)s] %(message)s') + if settings['globals']['debug_mode']: + eventLogger.setLevel(logging.DEBUG) + else: + eventLogger.setLevel(logging.INFO) - headers = {"content-type": "application/json", "Authorization": 'Bearer ' + api_key} - payload = {"type": "link", "title": title_message, "url": pushbullet_link, "body": body_message} + appriseHandler = apprise.Apprise() - try: - response = requests.post(url, headers=headers, data=json.dumps(payload)) + api_key = settings['notify_services']['pushbullet']['APIKey'] + public_url = settings['notify_services']['pushbullet']['PublicURL'] - if not response.status_code == 200: - eventLogger.warning("PushBullet Notification Failed: " + title_message) + apprise_url = f'pbul://{api_key}@{api_key}?url={public_url}' + appriseHandler.add(apprise_url) + + try: + result = appriseHandler.notify( + title=title_message, + body=body_message, + ) - eventLogger.debug("PushBullet Response: " + response.text) + if result: + eventLogger.debug(f'Push Bullet Notification to {api_key} was a success!') + else: + eventLogger.warning(f'Push Bullet Notification to {api_key} failed!') except Exception as e: - eventLogger.warning("PushBullet Notification failed: %s" % (e)) + eventLogger.warning(f'Push Bullet Notification to {api_key} failed: {e}') except: - eventLogger.warning("PushBullet Notification failed for unknown reason.") + eventLogger.warning(f'Push Bullet Notification to {api_key} failed for unknown reason.') def _send_onesignal_notification(settings, title_message, body_message, channel): @@ -371,3 +434,135 @@ def _send_influxdb_notification(notify_event, control, settings, pelletdb, in_da from notify.influxdb_handler import InfluxNotificationHandler influx_handler = InfluxNotificationHandler(settings) influx_handler.notify(notify_event, control, settings, pelletdb, in_data, grill_platform) + +def _estimate_eta(temperatures, target_temperature, interval_seconds=3, max_history_minutes=5, min_history_minutes=1): + """ + Estimates the ETA (Estimated Time of Arrival) for the food probe to reach a specific target temperature using + Linear Interpolation from the SciPy library module. + + Args: + temperatures: A list of temperatures measured by the food probe over time. + target_temperature: The desired target temperature. Value should be larger than the temperatures in the list. + interval: Time between temperature readings. Value between 1 and 60. + max_history_minutes: Maximum minutes of history to use for calculating ETA + min_history_minutes: Minimum minutes of history to use for calculating ETA + + Returns: + The estimated time (in seconds) it will take for the food probe to reach the target temperature. + None if the target temperature is already reached or the probe data is insufficient. + """ + eventLogger = create_logger('events', filename='/tmp/events.log') + + # Ensure target temperature is not already reached + if target_temperature <= max(temperatures): + #print('DEBUG: ETA: Target temperature already achieved.') + eventLogger.debug(f'ETA: Target temperature already achieved.') + return None + + # Ensure that interval is between 1 and 60 seconds + if interval_seconds > 60 or interval_seconds < 1: + #print('DEBUG: ETA: History data interval not between 1 and 60 seconds.') + eventLogger.debug(f'ETA: History data interval not between 1 and 60 seconds.') + return None + + # If there is more data than needed, shorten the list + readings_per_minute = (60 // interval_seconds) + minutes_of_data = len(temperatures) // readings_per_minute + if minutes_of_data > max_history_minutes: + while len(temperatures) // readings_per_minute > max_history_minutes: + temperatures.pop(0) + # If there is less data than needed, return None + elif minutes_of_data < min_history_minutes: + #print('DEBUG: Not enough history data to make estimate.') + eventLogger.debug(f'ETA: Not enough history data to make estimate.') + return None + + # Build times list + times = [] + for index in range(0, len(temperatures) * interval_seconds, interval_seconds): + times.append(index) + + #print(f'===========================================') + #print(f'DEBUG: ETA: times = {times}') + #print(f'DEBUG: ETA: temps = {temperatures}') + #print(f'===========================================') + + try: + # Create an interpolation function from the temperature data + interpolator = interp1d(times, temperatures, axis=0, bounds_error=False, kind="linear", fill_value="extrapolate") + + # Estimate the time to reach the target temperature + estimated_time = interpolator(target_temperature) + # If estimated time is over 24 hours or less than 0, it's likely to be a bad guess + if estimated_time > 86400 or estimated_time <= 0: + #print(f'DEBUG: ETA: Estimated time outside of bounds. [{estimated_time}]') + eventLogger.debug(f'ETA: Estimated time outside of bounds. [{estimated_time}]') + return None + eta = math.ceil(int(estimated_time) - times[-1]) + if eta <= 0: + # Additional bounds testing + #print(f'DEBUG: ETA: Estimated time outside of bounds. [{eta}]') + eventLogger.debug(f'ETA: Estimated time outside of bounds. [{eta}]') + return None + #print(f'===========================================') + #print(f'DEBUG: ETA: {eta}s') + #print(f'===========================================') + eventLogger.debug(f'Calculated ETA: {eta}s') + + except: + # Something failed, return None + #print('DEBUG: ETA: An exception occurred.') + eventLogger.debug(f'ETA: An exception occurred.') + #raise + return None + + return eta + +mqtt = None +def _send_mqtt_notification(control, settings, + pelletdb=None, in_data=None, grill_platform=None, pid_data=None, notify_event=None): + """ + Send mqtt Notifications + + :param notify_event: String Event + :param control: mode info + :param settings: Settings + :param pelletdb: Pellet level + :param in_data: In Data (Probe Temps) + :param grill_platform: Device status + "param pid_data: pid configuration, and actual values + """ + global mqtt + + if not mqtt: + from notify.mqtt_handler import MqttNotificationHandler + mqtt = MqttNotificationHandler(settings) + + # Send a notify_event immidiately + if notify_event != None: + payload = {'msg': notify_event } + mqtt.notify("notify_event", payload) + + # Write other data if we changed modes or we've exceeded the configured + # update rate + + mode_changed = (control['mode'] != mqtt.last_mode) + current_time = time.time() + + if pid_data and \ + (mode_changed or current_time > mqtt.pub_times['pid'] + mqtt.pub_rate): + mqtt.pub_times['pid'] = current_time + mqtt.notify("pid", pid_data) + + if pelletdb and \ + (mode_changed or current_time > mqtt.pub_times['pellet'] + mqtt.pub_rate): + mqtt.pub_times['pellet'] = current_time + mqtt.notify("pellet", pelletdb['current']) + + if mode_changed or \ + current_time > mqtt.pub_times['base'] + mqtt.pub_rate: + mqtt.pub_times['base'] = current_time + mqtt.notify("control", control) + mqtt.notify("system", control) + if grill_platform: mqtt.notify("devices", grill_platform.current) + if in_data: mqtt.notify("probe_data", in_data['probe_history']) diff --git a/probes/ds18b20.py b/probes/ds18b20.py new file mode 100644 index 00000000..6f9432b3 --- /dev/null +++ b/probes/ds18b20.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +''' +***************************************** +PiFire Probes DS18B20 Module +***************************************** + +Description: + This module utilizes the DS18B20 hardware and returns temperature data. + Depends on: pip install w1thermsensor + + Note: Still experimental. Expects this device to be connected to GPIO 6 (Pin 31) + Edit /boot/config.txt to add: + dtoverlay=w1-gpio,gpiopin=6,pullup="y" + + Ex Device Definition: + + device = { + 'device' : 'your_device_name', # Unique name for the device + 'module' : 'ds18b20', # Must be populated for this module to load properly + 'ports' : ['DS0'], # This is defined in the module, so this does not need to be defined. + 'config' : {} + } + +''' + +''' +***************************************** + Imported Libraries +***************************************** +''' +import logging +import time +from w1thermsensor import W1ThermSensor, Unit +from probes.base import ProbeInterface +import RPi.GPIO as GPIO + +''' +***************************************** + Class Definitions +***************************************** +''' + +class DS18B20_Device(): + ''' DS18B20 Device Utilizing the w1thermsensor module ''' + def __init__(self): + self.logger = logging.getLogger("control") + # Setup software pull-up which will wake the device connected + GPIO.setmode(GPIO.BCM) + GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP) + + time.sleep(4) # Give time for the kernel to connect to the 1-wire bus and get the device IDs + self.sensor = W1ThermSensor() + + @property + def temperature(self): + return self.sensor.get_temperature() + +class ReadProbes(ProbeInterface): + + def __init__(self, probe_info, device_info, units): + super().__init__(probe_info, device_info, units) + self.logger = logging.getLogger("control") + + def _init_device(self): + self.time_delay = 0 + self.device_info['ports'] = ['DS0'] + try: + self.device = DS18B20_Device() + except: + self.logger.error('Something went wrong when trying to initialize the DS18B20 device.') + raise + + def read_all_ports(self, output_data): + ''' Read temperature from device ''' + tempC = round(self.device.temperature, 1) + tempF = int(tempC * (9/5) + 32) # Celsius to Fahrenheit + port = self.device_info['ports'][0] + + ''' Read resistance from device ''' + self.output_data['tr'][self.port_map[port]] = 0 # resistance NA + + ''' Get average temperature from the queue and store it in the output data structure''' + if port == self.primary_port: + self.output_data['primary'][self.port_map[port]] = tempF if self.units == 'F' else tempC + elif port in self.food_ports: + self.output_data['food'][self.port_map[port]] = tempF if self.units == 'F' else tempC + elif port in self.aux_ports: + self.output_data['aux'][self.port_map[port]] = tempF if self.units == 'F' else tempC + + return self.output_data \ No newline at end of file diff --git a/static/css/base.css b/static/css/base.css new file mode 100755 index 00000000..8897d003 --- /dev/null +++ b/static/css/base.css @@ -0,0 +1,41 @@ + body { + padding-top: 35px; + } + .navbar { + padding-top: 0.3rem; + padding-bottom: 0.3rem; + } + .navbar-brand, .navbar-nav .nav-link { + padding-top: 0.3rem; + padding-bottom: 0.3rem; + } + .timer-bar-config { + top: 15px; + } + @media (max-width: 992px) { /* Small devices */ + .timer-offset .timer-bar-config { + top: 50px; + } + } + @media (max-width: 992px) { /* Large devices (landscape tablets, less than 1200px) */ + .fas.fa-bars{ + padding: 0.1em 0; + } + .navbar-nav { + text-align: center; + } + .nav-item { + display: inline-block; + float: none; + } + .navbar-collapse { + width: 100%; + background-color: rgba(44, 44, 44); /* Semi-transparent black */ + } + } + @media (min-width: 992px) { /* Large devices (landscape tablets, 1200px and up) */ + .navbar-collapse { + display: flex; + justify-content: center; + } + } diff --git a/static/css/dash_basic.css b/static/css/dash_basic.css new file mode 100644 index 00000000..ba6a5317 --- /dev/null +++ b/static/css/dash_basic.css @@ -0,0 +1,32 @@ + .gear-icon { + position: fixed; + bottom: 85px; + left: 20px; + width: 50px; + height: 50px; + color: #ffffff; + background-color: #80808083; + border-radius: 25px; + display: flex; + justify-content: center; + align-items: center; + transition: all 0.3s ease-in-out; + } + + .gear-icon:hover { + width: 200px; + background-color: #007bff; + } + + .gear-icon:hover::after { + content: "Dash Settings"; + white-space: nowrap; + color: #fff; + font-size: 1.25rem; + margin-left: 10px; + transition: all 0.3s ease-in-out; + } + + .temperature-input { + font-size: 64px; + } \ No newline at end of file diff --git a/static/css/dash_default.css b/static/css/dash_default.css old mode 100644 new mode 100755 index 37426b67..8ae419ea --- a/static/css/dash_default.css +++ b/static/css/dash_default.css @@ -1,4 +1,4 @@ -.gauge-container { + .gauge-container { width: 250px; height: 250px; display: inline-block; @@ -29,4 +29,37 @@ right: 48%; display: inline-block; font-size: 1.4em; - } \ No newline at end of file + } + + .gear-icon { + position: fixed; + bottom: 85px; + left: 20px; + width: 50px; + height: 50px; + color: #ffffff; + background-color: #80808083; + border-radius: 25px; + display: flex; + justify-content: center; + align-items: center; + transition: all 0.3s ease-in-out; + } + + .gear-icon:hover { + width: 200px; + background-color: #007bff; + } + + .gear-icon:hover::after { + content: "Dash Settings"; + white-space: nowrap; + color: #fff; + font-size: 1.25rem; + margin-left: 10px; + transition: all 0.3s ease-in-out; + } + + .temperature-input { + font-size: 64px; + } \ No newline at end of file diff --git a/static/css/settings.css b/static/css/settings.css index a1d568b1..4bd7c637 100755 --- a/static/css/settings.css +++ b/static/css/settings.css @@ -4,3 +4,46 @@ .input-group .input-group-text { width: 100%; } + +.power-modal{ + margin-top:35px; + z-index:9999; +} +@media (max-width: 992px) { /* Adjust this value as needed */ + .nav-settings-fixed { + position: relative; /* Changed from fixed to relative */ + left: 0; + top: 0; /* Changed from 40px to 0 */ + width: 100%; /* Changed from 20% to 100% */ + z-index: 2; + display: flex; + justify-content: center; + align-items: flex-start; + } + + .main-content { + position: relative; + top: 40px; + margin-left: 0; /* Changed from 20% to 0 */ + width:100%; + } + .power-modal{ + margin-top:90px; + } + .settings-button { + width: 100%; /* Make the button full width */ + position: fixed; /* Fix the position of the button */ + top: 50px; /* Position it at the top of the screen */ + left: 0; /* Position it at the left of the screen */ + z-index: 2; /* Ensure it's on top of other elements */ + } + #settingsNav { + position: fixed; + top: 90px; + left: 50%; /* Added this line */ + transform: translateX(-50%); /* Added this line */ + width: auto; /* Changed from 100% to auto */ + z-index: 3; + } + +} \ No newline at end of file diff --git a/static/img/display/splash_800x480.png b/static/img/display/splash_800x480.png new file mode 100644 index 00000000..d99ea983 Binary files /dev/null and b/static/img/display/splash_800x480.png differ diff --git a/static/img/flame-flat.svg b/static/img/flame-flat.svg new file mode 100644 index 00000000..3ebe3182 --- /dev/null +++ b/static/img/flame-flat.svg @@ -0,0 +1,50 @@ + + + + + + + + diff --git a/static/img/splash.svg b/static/img/splash.svg new file mode 100644 index 00000000..ea15ca9d --- /dev/null +++ b/static/img/splash.svg @@ -0,0 +1,170 @@ + + + +PiFire diff --git a/static/img/wizard/ds18b20.png b/static/img/wizard/ds18b20.png new file mode 100644 index 00000000..f8da25b8 Binary files /dev/null and b/static/img/wizard/ds18b20.png differ diff --git a/static/img/wizard/ds18b20.xcf b/static/img/wizard/ds18b20.xcf new file mode 100644 index 00000000..458ef182 Binary files /dev/null and b/static/img/wizard/ds18b20.xcf differ diff --git a/static/img/wizard/dsi_touch.png b/static/img/wizard/dsi_touch.png new file mode 100644 index 00000000..86007aa9 Binary files /dev/null and b/static/img/wizard/dsi_touch.png differ diff --git a/static/img/wizard/dsi_touch.xcf b/static/img/wizard/dsi_touch.xcf new file mode 100644 index 00000000..6f91e0d3 Binary files /dev/null and b/static/img/wizard/dsi_touch.xcf differ diff --git a/static/js/chart.esm.js b/static/js/chart.esm.js index 2306fa88..20729e36 100644 --- a/static/js/chart.esm.js +++ b/static/js/chart.esm.js @@ -1,10 +1,10 @@ /*! - * Chart.js v3.7.1 + * Chart.js v3.9.1 * https://www.chartjs.org * (c) 2022 Chart.js Contributors * Released under the MIT License */ -import { r as requestAnimFrame, a as resolve, e as effects, c as color, d as defaults, i as isObject, b as isArray, v as valueOrDefault, u as unlistenArrayEvents, l as listenArrayEvents, f as resolveObjectKey, g as isNumberFinite, h as createContext, j as defined, s as sign, k as isNullOrUndef, _ as _arrayUnique, t as toRadians, m as toPercentage, n as toDimension, T as TAU, o as formatNumber, p as _angleBetween, H as HALF_PI, P as PI, q as isNumber, w as _limitValue, x as _lookupByKey, y as getRelativePosition$1, z as _isPointInArea, A as _rlookupByKey, B as getAngleFromPoint, C as toPadding, D as each, E as getMaximumSize, F as _getParentNode, G as readUsedSize, I as throttled, J as supportsEventListenerOptions, K as _isDomSupported, L as log10, M as _factorize, N as finiteOrDefault, O as callback, Q as _addGrace, R as toDegrees, S as _measureText, U as _int16Range, V as _alignPixel, W as clipArea, X as renderText, Y as unclipArea, Z as toFont, $ as _toLeftRightCenter, a0 as _alignStartEnd, a1 as overrides, a2 as merge, a3 as _capitalize, a4 as descriptors, a5 as isFunction, a6 as _attachContext, a7 as _createResolver, a8 as _descriptors, a9 as mergeIf, aa as uid, ab as debounce, ac as retinaScale, ad as clearCanvas, ae as setsEqual, af as _elementsEqual, ag as _isClickEvent, ah as _isBetween, ai as _readValueToProps, aj as _updateBezierControlPoints, ak as _computeSegments, al as _boundSegments, am as _steppedInterpolation, an as _bezierInterpolation, ao as _pointInLine, ap as _steppedLineTo, aq as _bezierCurveTo, ar as drawPoint, as as addRoundedRectPath, at as toTRBL, au as toTRBLCorners, av as _boundSegment, aw as _normalizeAngle, ax as getRtlAdapter, ay as overrideTextDirection, az as _textX, aA as restoreTextDirection, aB as noop, aC as distanceBetweenPoints, aD as _setMinAndMaxByKey, aE as niceNum, aF as almostWhole, aG as almostEquals, aH as _decimalPlaces, aI as _longestText, aJ as _filterBetween, aK as _lookup } from './chunks/helpers.segment.js'; +import { r as requestAnimFrame, a as resolve, e as effects, c as color, d as defaults, i as isObject, b as isArray, v as valueOrDefault, u as unlistenArrayEvents, l as listenArrayEvents, f as resolveObjectKey, g as isNumberFinite, h as createContext, j as defined, s as sign, k as isNullOrUndef, _ as _arrayUnique, t as toRadians, m as toPercentage, n as toDimension, T as TAU, o as formatNumber, p as _angleBetween, H as HALF_PI, P as PI, q as _getStartAndCountOfVisiblePoints, w as _scaleRangesChanged, x as isNumber, y as _parseObjectDataRadialScale, z as log10, A as _factorize, B as finiteOrDefault, C as callback, D as _addGrace, E as _limitValue, F as toDegrees, G as _measureText, I as _int16Range, J as _alignPixel, K as toPadding, L as clipArea, M as renderText, N as unclipArea, O as toFont, Q as each, R as _toLeftRightCenter, S as _alignStartEnd, U as overrides, V as merge, W as _capitalize, X as getRelativePosition, Y as _rlookupByKey, Z as _lookupByKey, $ as _isPointInArea, a0 as getAngleFromPoint, a1 as getMaximumSize, a2 as _getParentNode, a3 as readUsedSize, a4 as throttled, a5 as supportsEventListenerOptions, a6 as _isDomSupported, a7 as descriptors, a8 as isFunction, a9 as _attachContext, aa as _createResolver, ab as _descriptors, ac as mergeIf, ad as uid, ae as debounce, af as retinaScale, ag as clearCanvas, ah as setsEqual, ai as _elementsEqual, aj as _isClickEvent, ak as _isBetween, al as _readValueToProps, am as _updateBezierControlPoints, an as _computeSegments, ao as _boundSegments, ap as _steppedInterpolation, aq as _bezierInterpolation, ar as _pointInLine, as as _steppedLineTo, at as _bezierCurveTo, au as drawPoint, av as addRoundedRectPath, aw as toTRBL, ax as toTRBLCorners, ay as _boundSegment, az as _normalizeAngle, aA as getRtlAdapter, aB as overrideTextDirection, aC as _textX, aD as restoreTextDirection, aE as drawPointLegend, aF as noop, aG as distanceBetweenPoints, aH as _setMinAndMaxByKey, aI as niceNum, aJ as almostWhole, aK as almostEquals, aL as _decimalPlaces, aM as _longestText, aN as _filterBetween, aO as _lookup } from './chunks/helpers.segment.js'; export { d as defaults } from './chunks/helpers.segment.js'; class Animator { @@ -615,6 +615,7 @@ class DatasetController { this._drawStart = undefined; this._drawCount = undefined; this.enableOptionSharing = false; + this.supportsDecimation = false; this.$context = undefined; this._syncList = []; this.initialize(); @@ -1015,6 +1016,14 @@ class DatasetController { includeOptions(mode, sharedOptions) { return !sharedOptions || isDirectUpdateMode(mode) || this.chart._animationsDisabled; } + _getSharedOptions(start, mode) { + const firstOpts = this.resolveDataElementOptions(start, mode); + const previouslySharedOptions = this._sharedOptions; + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions) || (sharedOptions !== previouslySharedOptions); + this.updateSharedOptions(sharedOptions, mode, firstOpts); + return {sharedOptions, includeOptions}; + } updateElement(element, index, properties, mode) { if (isDirectUpdateMode(mode)) { Object.assign(element, properties); @@ -1293,6 +1302,10 @@ function setBorderSkipped(properties, options, stack, index) { properties.borderSkipped = res; return; } + if (edge === true) { + properties.borderSkipped = {top: true, right: true, bottom: true, left: true}; + return; + } const {start, end, reverse, top, bottom} = borderProps(properties); if (edge === 'middle' && stack) { properties.enableBorderRadius = true; @@ -1390,10 +1403,7 @@ class BarController extends DatasetController { const base = vScale.getBasePixel(); const horizontal = vScale.isHorizontal(); const ruler = this._getRuler(); - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); - this.updateSharedOptions(sharedOptions, mode, firstOpts); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); for (let i = start; i < start + count; i++) { const parsed = this.getParsed(i); const vpixels = reset || isNullOrUndef(parsed[vScale.axis]) ? {base, head: base} : this._calculateBarValuePixels(i); @@ -1418,31 +1428,27 @@ class BarController extends DatasetController { } } _getStacks(last, dataIndex) { - const meta = this._cachedMeta; - const iScale = meta.iScale; - const metasets = iScale.getMatchingVisibleMetas(this._type); + const {iScale} = this._cachedMeta; + const metasets = iScale.getMatchingVisibleMetas(this._type) + .filter(meta => meta.controller.options.grouped); const stacked = iScale.options.stacked; - const ilen = metasets.length; const stacks = []; - let i, item; - for (i = 0; i < ilen; ++i) { - item = metasets[i]; - if (!item.controller.options.grouped) { - continue; + const skipNull = (meta) => { + const parsed = meta.controller.getParsed(dataIndex); + const val = parsed && parsed[meta.vScale.axis]; + if (isNullOrUndef(val) || isNaN(val)) { + return true; } - if (typeof dataIndex !== 'undefined') { - const val = item.controller.getParsed(dataIndex)[ - item.controller._cachedMeta.vScale.axis - ]; - if (isNullOrUndef(val) || isNaN(val)) { - continue; - } + }; + for (const meta of metasets) { + if (dataIndex !== undefined && skipNull(meta)) { + continue; } - if (stacked === false || stacks.indexOf(item.stack) === -1 || - (stacked === undefined && item.stack === undefined)) { - stacks.push(item.stack); + if (stacked === false || stacks.indexOf(meta.stack) === -1 || + (stacked === undefined && meta.stack === undefined)) { + stacks.push(meta.stack); } - if (item.index === last) { + if (meta.index === last) { break; } } @@ -1520,6 +1526,11 @@ class BarController extends DatasetController { if (value === actualBase) { base -= size / 2; } + const startPixel = vScale.getPixelForDecimal(0); + const endPixel = vScale.getPixelForDecimal(1); + const min = Math.min(startPixel, endPixel); + const max = Math.max(startPixel, endPixel); + base = Math.max(Math.min(base, max), min); head = base + size; } if (base === vScale.getPixelForValue(actualBase)) { @@ -1657,9 +1668,7 @@ class BubbleController extends DatasetController { updateElements(points, start, count, mode) { const reset = mode === 'reset'; const {iScale, vScale} = this._cachedMeta; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); const iAxis = iScale.axis; const vAxis = vScale.axis; for (let i = start; i < start + count; i++) { @@ -1670,14 +1679,13 @@ class BubbleController extends DatasetController { const vPixel = properties[vAxis] = reset ? vScale.getBasePixel() : vScale.getPixelForValue(parsed[vAxis]); properties.skip = isNaN(iPixel) || isNaN(vPixel); if (includeOptions) { - properties.options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); if (reset) { properties.options.radius = 0; } } this.updateElement(point, i, properties, mode); } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } resolveDataElementOptions(index, mode) { const parsed = this.getParsed(index); @@ -1843,9 +1851,7 @@ class DoughnutController extends DatasetController { const animateScale = reset && animationOpts.animateScale; const innerRadius = animateScale ? 0 : this.innerRadius; const outerRadius = animateScale ? 0 : this.outerRadius; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); let startAngle = this._getRotation(); let i; for (i = 0; i < start; ++i) { @@ -1869,7 +1875,6 @@ class DoughnutController extends DatasetController { startAngle += circumference; this.updateElement(arc, i, properties, mode); } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } calculateTotal() { const meta = this._cachedMeta; @@ -2030,16 +2035,17 @@ DoughnutController.overrides = { class LineController extends DatasetController { initialize() { this.enableOptionSharing = true; + this.supportsDecimation = true; super.initialize(); } update(mode) { const meta = this._cachedMeta; const {dataset: line, data: points = [], _dataset} = meta; const animationsDisabled = this.chart._animationsDisabled; - let {start, count} = getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); this._drawStart = start; this._drawCount = count; - if (scaleRangesChanged(meta)) { + if (_scaleRangesChanged(meta)) { start = 0; count = points.length; } @@ -2061,9 +2067,7 @@ class LineController extends DatasetController { updateElements(points, start, count, mode) { const reset = mode === 'reset'; const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); const iAxis = iScale.axis; const vAxis = vScale.axis; const {spanGaps, segment} = this.options; @@ -2078,7 +2082,7 @@ class LineController extends DatasetController { const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; - properties.stop = i > 0 && (parsed[iAxis] - prevParsed[iAxis]) > maxGapLength; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; if (segment) { properties.parsed = parsed; properties.raw = _dataset.data[i]; @@ -2091,7 +2095,6 @@ class LineController extends DatasetController { } prevParsed = parsed; } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } getMaxOverflow() { const meta = this._cachedMeta; @@ -2128,50 +2131,6 @@ LineController.overrides = { }, } }; -function getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { - const pointCount = points.length; - let start = 0; - let count = pointCount; - if (meta._sorted) { - const {iScale, _parsed} = meta; - const axis = iScale.axis; - const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); - if (minDefined) { - start = _limitValue(Math.min( - _lookupByKey(_parsed, iScale.axis, min).lo, - animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo), - 0, pointCount - 1); - } - if (maxDefined) { - count = _limitValue(Math.max( - _lookupByKey(_parsed, iScale.axis, max).hi + 1, - animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max)).hi + 1), - start, pointCount) - start; - } else { - count = pointCount - start; - } - } - return {start, count}; -} -function scaleRangesChanged(meta) { - const {xScale, yScale, _scaleRanges} = meta; - const newRanges = { - xmin: xScale.min, - xmax: xScale.max, - ymin: yScale.min, - ymax: yScale.max - }; - if (!_scaleRanges) { - meta._scaleRanges = newRanges; - return true; - } - const changed = _scaleRanges.xmin !== xScale.min - || _scaleRanges.xmax !== xScale.max - || _scaleRanges.ymin !== yScale.min - || _scaleRanges.ymax !== yScale.max; - Object.assign(_scaleRanges, newRanges); - return changed; -} class PolarAreaController extends DatasetController { constructor(chart, datasetIndex) { @@ -2189,11 +2148,30 @@ class PolarAreaController extends DatasetController { value, }; } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } update(mode) { const arcs = this._cachedMeta.data; this._updateRadius(); this.updateElements(arcs, 0, arcs.length, mode); } + getMinMax() { + const meta = this._cachedMeta; + const range = {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY}; + meta.data.forEach((element, index) => { + const parsed = this.getParsed(index).r; + if (!isNaN(parsed) && this.chart.getDataVisibility(index)) { + if (parsed < range.min) { + range.min = parsed; + } + if (parsed > range.max) { + range.max = parsed; + } + } + }); + return range; + } _updateRadius() { const chart = this.chart; const chartArea = chart.chartArea; @@ -2208,7 +2186,6 @@ class PolarAreaController extends DatasetController { updateElements(arcs, start, count, mode) { const reset = mode === 'reset'; const chart = this.chart; - const dataset = this.getDataset(); const opts = chart.options; const animationOpts = opts.animation; const scale = this._cachedMeta.rScale; @@ -2225,7 +2202,7 @@ class PolarAreaController extends DatasetController { const arc = arcs[i]; let startAngle = angle; let endAngle = angle + this._computeAngle(i, mode, defaultAngle); - let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0; + let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(this.getParsed(i).r) : 0; angle = endAngle; if (reset) { if (animationOpts.animateScale) { @@ -2248,11 +2225,10 @@ class PolarAreaController extends DatasetController { } } countVisibleElements() { - const dataset = this.getDataset(); const meta = this._cachedMeta; let count = 0; meta.data.forEach((element, index) => { - if (!isNaN(dataset.data[index]) && this.chart.getDataVisibility(index)) { + if (!isNaN(this.getParsed(index).r) && this.chart.getDataVisibility(index)) { count++; } }); @@ -2359,6 +2335,9 @@ class RadarController extends DatasetController { value: '' + vScale.getLabelForValue(parsed[vScale.axis]) }; } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } update(mode) { const meta = this._cachedMeta; const line = meta.dataset; @@ -2380,13 +2359,12 @@ class RadarController extends DatasetController { this.updateElements(points, 0, points.length, mode); } updateElements(points, start, count, mode) { - const dataset = this.getDataset(); const scale = this._cachedMeta.rScale; const reset = mode === 'reset'; for (let i = start; i < start + count; i++) { const point = points[i]; const options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); - const pointPosition = scale.getPointPositionForValue(i, dataset.data[i]); + const pointPosition = scale.getPointPositionForValue(i, this.getParsed(i).r); const x = reset ? scale.xCenter : pointPosition.x; const y = reset ? scale.yCenter : pointPosition.y; const properties = { @@ -2421,2480 +2399,2568 @@ RadarController.overrides = { } }; -class ScatterController extends LineController { -} -ScatterController.id = 'scatter'; -ScatterController.defaults = { - showLine: false, - fill: false -}; -ScatterController.overrides = { - interaction: { - mode: 'point' - }, - plugins: { - tooltip: { - callbacks: { - title() { - return ''; - }, - label(item) { - return '(' + item.label + ', ' + item.formattedValue + ')'; - } - } - } - }, - scales: { - x: { - type: 'linear' - }, - y: { - type: 'linear' - } - } -}; - -var controllers = /*#__PURE__*/Object.freeze({ -__proto__: null, -BarController: BarController, -BubbleController: BubbleController, -DoughnutController: DoughnutController, -LineController: LineController, -PolarAreaController: PolarAreaController, -PieController: PieController, -RadarController: RadarController, -ScatterController: ScatterController -}); - -function abstract() { - throw new Error('This method is not implemented: Check that a complete date adapter is provided.'); -} -class DateAdapter { - constructor(options) { - this.options = options || {}; - } - formats() { - return abstract(); - } - parse(value, format) { - return abstract(); - } - format(timestamp, format) { - return abstract(); - } - add(timestamp, amount, unit) { - return abstract(); +class Element { + constructor() { + this.x = undefined; + this.y = undefined; + this.active = false; + this.options = undefined; + this.$animations = undefined; } - diff(a, b, unit) { - return abstract(); + tooltipPosition(useFinalPosition) { + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + return {x, y}; } - startOf(timestamp, unit, weekday) { - return abstract(); + hasValue() { + return isNumber(this.x) && isNumber(this.y); } - endOf(timestamp, unit) { - return abstract(); + getProps(props, final) { + const anims = this.$animations; + if (!final || !anims) { + return this; + } + const ret = {}; + props.forEach(prop => { + ret[prop] = anims[prop] && anims[prop].active() ? anims[prop]._to : this[prop]; + }); + return ret; } } -DateAdapter.override = function(members) { - Object.assign(DateAdapter.prototype, members); -}; -var adapters = { - _date: DateAdapter -}; +Element.defaults = {}; +Element.defaultRoutes = undefined; -function getRelativePosition(e, chart) { - if ('native' in e) { - return { - x: e.x, - y: e.y - }; - } - return getRelativePosition$1(e, chart); -} -function evaluateAllVisibleItems(chart, handler) { - const metasets = chart.getSortedVisibleDatasetMetas(); - let index, data, element; - for (let i = 0, ilen = metasets.length; i < ilen; ++i) { - ({index, data} = metasets[i]); - for (let j = 0, jlen = data.length; j < jlen; ++j) { - element = data[j]; - if (!element.skip) { - handler(element, index, j); - } +const formatters = { + values(value) { + return isArray(value) ? value : '' + value; + }, + numeric(tickValue, index, ticks) { + if (tickValue === 0) { + return '0'; } - } -} -function binarySearch(metaset, axis, value, intersect) { - const {controller, data, _sorted} = metaset; - const iScale = controller._cachedMeta.iScale; - if (iScale && axis === iScale.axis && axis !== 'r' && _sorted && data.length) { - const lookupMethod = iScale._reversePixels ? _rlookupByKey : _lookupByKey; - if (!intersect) { - return lookupMethod(data, axis, value); - } else if (controller._sharedOptions) { - const el = data[0]; - const range = typeof el.getRange === 'function' && el.getRange(axis); - if (range) { - const start = lookupMethod(data, axis, value - range); - const end = lookupMethod(data, axis, value + range); - return {lo: start.lo, hi: end.hi}; + const locale = this.chart.options.locale; + let notation; + let delta = tickValue; + if (ticks.length > 1) { + const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); + if (maxTick < 1e-4 || maxTick > 1e+15) { + notation = 'scientific'; } + delta = calculateDelta(tickValue, ticks); } - } - return {lo: 0, hi: data.length - 1}; -} -function optimizedEvaluateItems(chart, axis, position, handler, intersect) { - const metasets = chart.getSortedVisibleDatasetMetas(); - const value = position[axis]; - for (let i = 0, ilen = metasets.length; i < ilen; ++i) { - const {index, data} = metasets[i]; - const {lo, hi} = binarySearch(metasets[i], axis, value, intersect); - for (let j = lo; j <= hi; ++j) { - const element = data[j]; - if (!element.skip) { - handler(element, index, j); - } + const logDelta = log10(Math.abs(delta)); + const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); + const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; + Object.assign(options, this.options.ticks.format); + return formatNumber(tickValue, locale, options); + }, + logarithmic(tickValue, index, ticks) { + if (tickValue === 0) { + return '0'; + } + const remain = tickValue / (Math.pow(10, Math.floor(log10(tickValue)))); + if (remain === 1 || remain === 2 || remain === 5) { + return formatters.numeric.call(this, tickValue, index, ticks); } + return ''; } -} -function getDistanceMetricForAxis(axis) { - const useX = axis.indexOf('x') !== -1; - const useY = axis.indexOf('y') !== -1; - return function(pt1, pt2) { - const deltaX = useX ? Math.abs(pt1.x - pt2.x) : 0; - const deltaY = useY ? Math.abs(pt1.y - pt2.y) : 0; - return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); - }; -} -function getIntersectItems(chart, position, axis, useFinalPosition) { - const items = []; - if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) { - return items; +}; +function calculateDelta(tickValue, ticks) { + let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value; + if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) { + delta = tickValue - Math.floor(tickValue); } - const evaluationFunc = function(element, datasetIndex, index) { - if (element.inRange(position.x, position.y, useFinalPosition)) { - items.push({element, datasetIndex, index}); - } - }; - optimizedEvaluateItems(chart, axis, position, evaluationFunc, true); - return items; + return delta; } -function getNearestRadialItems(chart, position, axis, useFinalPosition) { - let items = []; - function evaluationFunc(element, datasetIndex, index) { - const {startAngle, endAngle} = element.getProps(['startAngle', 'endAngle'], useFinalPosition); - const {angle} = getAngleFromPoint(element, {x: position.x, y: position.y}); - if (_angleBetween(angle, startAngle, endAngle)) { - items.push({element, datasetIndex, index}); - } - } - optimizedEvaluateItems(chart, axis, position, evaluationFunc); - return items; -} -function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition) { - let items = []; - const distanceMetric = getDistanceMetricForAxis(axis); - let minDistance = Number.POSITIVE_INFINITY; - function evaluationFunc(element, datasetIndex, index) { - const inRange = element.inRange(position.x, position.y, useFinalPosition); - if (intersect && !inRange) { - return; +var Ticks = {formatters}; + +defaults.set('scale', { + display: true, + offset: false, + reverse: false, + beginAtZero: false, + bounds: 'ticks', + grace: 0, + grid: { + display: true, + lineWidth: 1, + drawBorder: true, + drawOnChartArea: true, + drawTicks: true, + tickLength: 8, + tickWidth: (_ctx, options) => options.lineWidth, + tickColor: (_ctx, options) => options.color, + offset: false, + borderDash: [], + borderDashOffset: 0.0, + borderWidth: 1 + }, + title: { + display: false, + text: '', + padding: { + top: 4, + bottom: 4 } - const center = element.getCenterPoint(useFinalPosition); - const pointInArea = _isPointInArea(center, chart.chartArea, chart._minPadding); - if (!pointInArea && !inRange) { - return; + }, + ticks: { + minRotation: 0, + maxRotation: 50, + mirror: false, + textStrokeWidth: 0, + textStrokeColor: '', + padding: 3, + display: true, + autoSkip: true, + autoSkipPadding: 3, + labelOffset: 0, + callback: Ticks.formatters.values, + minor: {}, + major: {}, + align: 'center', + crossAlign: 'near', + showLabelBackdrop: false, + backdropColor: 'rgba(255, 255, 255, 0.75)', + backdropPadding: 2, + } +}); +defaults.route('scale.ticks', 'color', '', 'color'); +defaults.route('scale.grid', 'color', '', 'borderColor'); +defaults.route('scale.grid', 'borderColor', '', 'borderColor'); +defaults.route('scale.title', 'color', '', 'color'); +defaults.describe('scale', { + _fallback: false, + _scriptable: (name) => !name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser', + _indexable: (name) => name !== 'borderDash' && name !== 'tickBorderDash', +}); +defaults.describe('scales', { + _fallback: 'scale', +}); +defaults.describe('scale.ticks', { + _scriptable: (name) => name !== 'backdropPadding' && name !== 'callback', + _indexable: (name) => name !== 'backdropPadding', +}); + +function autoSkip(scale, ticks) { + const tickOpts = scale.options.ticks; + const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); + const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; + const numMajorIndices = majorIndices.length; + const first = majorIndices[0]; + const last = majorIndices[numMajorIndices - 1]; + const newTicks = []; + if (numMajorIndices > ticksLimit) { + skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit); + return newTicks; + } + const spacing = calculateSpacing(majorIndices, ticks, ticksLimit); + if (numMajorIndices > 0) { + let i, ilen; + const avgMajorSpacing = numMajorIndices > 1 ? Math.round((last - first) / (numMajorIndices - 1)) : null; + skip(ticks, newTicks, spacing, isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first); + for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) { + skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]); } - const distance = distanceMetric(position, center); - if (distance < minDistance) { - items = [{element, datasetIndex, index}]; - minDistance = distance; - } else if (distance === minDistance) { - items.push({element, datasetIndex, index}); + skip(ticks, newTicks, spacing, last, isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing); + return newTicks; + } + skip(ticks, newTicks, spacing); + return newTicks; +} +function determineMaxTicks(scale) { + const offset = scale.options.offset; + const tickLength = scale._tickSize(); + const maxScale = scale._length / tickLength + (offset ? 0 : 1); + const maxChart = scale._maxLength / tickLength; + return Math.floor(Math.min(maxScale, maxChart)); +} +function calculateSpacing(majorIndices, ticks, ticksLimit) { + const evenMajorSpacing = getEvenSpacing(majorIndices); + const spacing = ticks.length / ticksLimit; + if (!evenMajorSpacing) { + return Math.max(spacing, 1); + } + const factors = _factorize(evenMajorSpacing); + for (let i = 0, ilen = factors.length - 1; i < ilen; i++) { + const factor = factors[i]; + if (factor > spacing) { + return factor; } } - optimizedEvaluateItems(chart, axis, position, evaluationFunc); - return items; + return Math.max(spacing, 1); } -function getNearestItems(chart, position, axis, intersect, useFinalPosition) { - if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) { - return []; +function getMajorIndices(ticks) { + const result = []; + let i, ilen; + for (i = 0, ilen = ticks.length; i < ilen; i++) { + if (ticks[i].major) { + result.push(i); + } } - return axis === 'r' && !intersect - ? getNearestRadialItems(chart, position, axis, useFinalPosition) - : getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition); + return result; } -function getAxisItems(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const items = []; - const axis = options.axis; - const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; - let intersectsItem = false; - evaluateAllVisibleItems(chart, (element, datasetIndex, index) => { - if (element[rangeMethod](position[axis], useFinalPosition)) { - items.push({element, datasetIndex, index}); +function skipMajors(ticks, newTicks, majorIndices, spacing) { + let count = 0; + let next = majorIndices[0]; + let i; + spacing = Math.ceil(spacing); + for (i = 0; i < ticks.length; i++) { + if (i === next) { + newTicks.push(ticks[i]); + count++; + next = majorIndices[count * spacing]; } - if (element.inRange(position.x, position.y, useFinalPosition)) { - intersectsItem = true; + } +} +function skip(ticks, newTicks, spacing, majorStart, majorEnd) { + const start = valueOrDefault(majorStart, 0); + const end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length); + let count = 0; + let length, i, next; + spacing = Math.ceil(spacing); + if (majorEnd) { + length = majorEnd - majorStart; + spacing = length / Math.floor(length / spacing); + } + next = start; + while (next < 0) { + count++; + next = Math.round(start + count * spacing); + } + for (i = Math.max(start, 0); i < end; i++) { + if (i === next) { + newTicks.push(ticks[i]); + count++; + next = Math.round(start + count * spacing); } - }); - if (options.intersect && !intersectsItem) { - return []; } - return items; } -var Interaction = { - modes: { - index(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'x'; - const items = options.intersect - ? getIntersectItems(chart, position, axis, useFinalPosition) - : getNearestItems(chart, position, axis, false, useFinalPosition); - const elements = []; - if (!items.length) { - return []; - } - chart.getSortedVisibleDatasetMetas().forEach((meta) => { - const index = items[0].index; - const element = meta.data[index]; - if (element && !element.skip) { - elements.push({element, datasetIndex: meta.index, index}); - } - }); - return elements; - }, - dataset(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - let items = options.intersect - ? getIntersectItems(chart, position, axis, useFinalPosition) : - getNearestItems(chart, position, axis, false, useFinalPosition); - if (items.length > 0) { - const datasetIndex = items[0].datasetIndex; - const data = chart.getDatasetMeta(datasetIndex).data; - items = []; - for (let i = 0; i < data.length; ++i) { - items.push({element: data[i], datasetIndex, index: i}); - } - } - return items; - }, - point(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - return getIntersectItems(chart, position, axis, useFinalPosition); - }, - nearest(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - return getNearestItems(chart, position, axis, options.intersect, useFinalPosition); - }, - x(chart, e, options, useFinalPosition) { - return getAxisItems(chart, e, {axis: 'x', intersect: options.intersect}, useFinalPosition); - }, - y(chart, e, options, useFinalPosition) { - return getAxisItems(chart, e, {axis: 'y', intersect: options.intersect}, useFinalPosition); +function getEvenSpacing(arr) { + const len = arr.length; + let i, diff; + if (len < 2) { + return false; + } + for (diff = arr[0], i = 1; i < len; ++i) { + if (arr[i] - arr[i - 1] !== diff) { + return false; } } -}; - -const STATIC_POSITIONS = ['left', 'top', 'right', 'bottom']; -function filterByPosition(array, position) { - return array.filter(v => v.pos === position); + return diff; } -function filterDynamicPositionByAxis(array, axis) { - return array.filter(v => STATIC_POSITIONS.indexOf(v.pos) === -1 && v.box.axis === axis); + +const reverseAlign = (align) => align === 'left' ? 'right' : align === 'right' ? 'left' : align; +const offsetFromEdge = (scale, edge, offset) => edge === 'top' || edge === 'left' ? scale[edge] + offset : scale[edge] - offset; +function sample(arr, numItems) { + const result = []; + const increment = arr.length / numItems; + const len = arr.length; + let i = 0; + for (; i < len; i += increment) { + result.push(arr[Math.floor(i)]); + } + return result; } -function sortByWeight(array, reverse) { - return array.sort((a, b) => { - const v0 = reverse ? b : a; - const v1 = reverse ? a : b; - return v0.weight === v1.weight ? - v0.index - v1.index : - v0.weight - v1.weight; - }); -} -function wrapBoxes(boxes) { - const layoutBoxes = []; - let i, ilen, box, pos, stack, stackWeight; - for (i = 0, ilen = (boxes || []).length; i < ilen; ++i) { - box = boxes[i]; - ({position: pos, options: {stack, stackWeight = 1}} = box); - layoutBoxes.push({ - index: i, - box, - pos, - horizontal: box.isHorizontal(), - weight: box.weight, - stack: stack && (pos + stack), - stackWeight - }); - } - return layoutBoxes; -} -function buildStacks(layouts) { - const stacks = {}; - for (const wrap of layouts) { - const {stack, pos, stackWeight} = wrap; - if (!stack || !STATIC_POSITIONS.includes(pos)) { - continue; - } - const _stack = stacks[stack] || (stacks[stack] = {count: 0, placed: 0, weight: 0, size: 0}); - _stack.count++; - _stack.weight += stackWeight; - } - return stacks; -} -function setLayoutDims(layouts, params) { - const stacks = buildStacks(layouts); - const {vBoxMaxWidth, hBoxMaxHeight} = params; - let i, ilen, layout; - for (i = 0, ilen = layouts.length; i < ilen; ++i) { - layout = layouts[i]; - const {fullSize} = layout.box; - const stack = stacks[layout.stack]; - const factor = stack && layout.stackWeight / stack.weight; - if (layout.horizontal) { - layout.width = factor ? factor * vBoxMaxWidth : fullSize && params.availableWidth; - layout.height = hBoxMaxHeight; +function getPixelForGridLine(scale, index, offsetGridLines) { + const length = scale.ticks.length; + const validIndex = Math.min(index, length - 1); + const start = scale._startPixel; + const end = scale._endPixel; + const epsilon = 1e-6; + let lineValue = scale.getPixelForTick(validIndex); + let offset; + if (offsetGridLines) { + if (length === 1) { + offset = Math.max(lineValue - start, end - lineValue); + } else if (index === 0) { + offset = (scale.getPixelForTick(1) - lineValue) / 2; } else { - layout.width = vBoxMaxWidth; - layout.height = factor ? factor * hBoxMaxHeight : fullSize && params.availableHeight; + offset = (lineValue - scale.getPixelForTick(validIndex - 1)) / 2; + } + lineValue += validIndex < index ? offset : -offset; + if (lineValue < start - epsilon || lineValue > end + epsilon) { + return; } } - return stacks; -} -function buildLayoutBoxes(boxes) { - const layoutBoxes = wrapBoxes(boxes); - const fullSize = sortByWeight(layoutBoxes.filter(wrap => wrap.box.fullSize), true); - const left = sortByWeight(filterByPosition(layoutBoxes, 'left'), true); - const right = sortByWeight(filterByPosition(layoutBoxes, 'right')); - const top = sortByWeight(filterByPosition(layoutBoxes, 'top'), true); - const bottom = sortByWeight(filterByPosition(layoutBoxes, 'bottom')); - const centerHorizontal = filterDynamicPositionByAxis(layoutBoxes, 'x'); - const centerVertical = filterDynamicPositionByAxis(layoutBoxes, 'y'); - return { - fullSize, - leftAndTop: left.concat(top), - rightAndBottom: right.concat(centerVertical).concat(bottom).concat(centerHorizontal), - chartArea: filterByPosition(layoutBoxes, 'chartArea'), - vertical: left.concat(right).concat(centerVertical), - horizontal: top.concat(bottom).concat(centerHorizontal) - }; + return lineValue; } -function getCombinedMax(maxPadding, chartArea, a, b) { - return Math.max(maxPadding[a], chartArea[a]) + Math.max(maxPadding[b], chartArea[b]); +function garbageCollect(caches, length) { + each(caches, (cache) => { + const gc = cache.gc; + const gcLen = gc.length / 2; + let i; + if (gcLen > length) { + for (i = 0; i < gcLen; ++i) { + delete cache.data[gc[i]]; + } + gc.splice(0, gcLen); + } + }); } -function updateMaxPadding(maxPadding, boxPadding) { - maxPadding.top = Math.max(maxPadding.top, boxPadding.top); - maxPadding.left = Math.max(maxPadding.left, boxPadding.left); - maxPadding.bottom = Math.max(maxPadding.bottom, boxPadding.bottom); - maxPadding.right = Math.max(maxPadding.right, boxPadding.right); +function getTickMarkLength(options) { + return options.drawTicks ? options.tickLength : 0; } -function updateDims(chartArea, params, layout, stacks) { - const {pos, box} = layout; - const maxPadding = chartArea.maxPadding; - if (!isObject(pos)) { - if (layout.size) { - chartArea[pos] -= layout.size; - } - const stack = stacks[layout.stack] || {size: 0, count: 1}; - stack.size = Math.max(stack.size, layout.horizontal ? box.height : box.width); - layout.size = stack.size / stack.count; - chartArea[pos] += layout.size; - } - if (box.getPadding) { - updateMaxPadding(maxPadding, box.getPadding()); +function getTitleHeight(options, fallback) { + if (!options.display) { + return 0; } - const newWidth = Math.max(0, params.outerWidth - getCombinedMax(maxPadding, chartArea, 'left', 'right')); - const newHeight = Math.max(0, params.outerHeight - getCombinedMax(maxPadding, chartArea, 'top', 'bottom')); - const widthChanged = newWidth !== chartArea.w; - const heightChanged = newHeight !== chartArea.h; - chartArea.w = newWidth; - chartArea.h = newHeight; - return layout.horizontal - ? {same: widthChanged, other: heightChanged} - : {same: heightChanged, other: widthChanged}; + const font = toFont(options.font, fallback); + const padding = toPadding(options.padding); + const lines = isArray(options.text) ? options.text.length : 1; + return (lines * font.lineHeight) + padding.height; } -function handleMaxPadding(chartArea) { - const maxPadding = chartArea.maxPadding; - function updatePos(pos) { - const change = Math.max(maxPadding[pos] - chartArea[pos], 0); - chartArea[pos] += change; - return change; - } - chartArea.y += updatePos('top'); - chartArea.x += updatePos('left'); - updatePos('right'); - updatePos('bottom'); +function createScaleContext(parent, scale) { + return createContext(parent, { + scale, + type: 'scale' + }); } -function getMargins(horizontal, chartArea) { - const maxPadding = chartArea.maxPadding; - function marginForPositions(positions) { - const margin = {left: 0, top: 0, right: 0, bottom: 0}; - positions.forEach((pos) => { - margin[pos] = Math.max(chartArea[pos], maxPadding[pos]); - }); - return margin; +function createTickContext(parent, index, tick) { + return createContext(parent, { + tick, + index, + type: 'tick' + }); +} +function titleAlign(align, position, reverse) { + let ret = _toLeftRightCenter(align); + if ((reverse && position !== 'right') || (!reverse && position === 'right')) { + ret = reverseAlign(ret); } - return horizontal - ? marginForPositions(['left', 'right']) - : marginForPositions(['top', 'bottom']); + return ret; } -function fitBoxes(boxes, chartArea, params, stacks) { - const refitBoxes = []; - let i, ilen, layout, box, refit, changed; - for (i = 0, ilen = boxes.length, refit = 0; i < ilen; ++i) { - layout = boxes[i]; - box = layout.box; - box.update( - layout.width || chartArea.w, - layout.height || chartArea.h, - getMargins(layout.horizontal, chartArea) - ); - const {same, other} = updateDims(chartArea, params, layout, stacks); - refit |= same && refitBoxes.length; - changed = changed || other; - if (!box.fullSize) { - refitBoxes.push(layout); +function titleArgs(scale, offset, position, align) { + const {top, left, bottom, right, chart} = scale; + const {chartArea, scales} = chart; + let rotation = 0; + let maxWidth, titleX, titleY; + const height = bottom - top; + const width = right - left; + if (scale.isHorizontal()) { + titleX = _alignStartEnd(align, left, right); + if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + titleY = scales[positionAxisID].getPixelForValue(value) + height - offset; + } else if (position === 'center') { + titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset; + } else { + titleY = offsetFromEdge(scale, position, offset); + } + maxWidth = right - left; + } else { + if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + titleX = scales[positionAxisID].getPixelForValue(value) - width + offset; + } else if (position === 'center') { + titleX = (chartArea.left + chartArea.right) / 2 - width + offset; + } else { + titleX = offsetFromEdge(scale, position, offset); } + titleY = _alignStartEnd(align, bottom, top); + rotation = position === 'left' ? -HALF_PI : HALF_PI; } - return refit && fitBoxes(refitBoxes, chartArea, params, stacks) || changed; -} -function setBoxDims(box, left, top, width, height) { - box.top = top; - box.left = left; - box.right = left + width; - box.bottom = top + height; - box.width = width; - box.height = height; + return {titleX, titleY, maxWidth, rotation}; } -function placeBoxes(boxes, chartArea, params, stacks) { - const userPadding = params.padding; - let {x, y} = chartArea; - for (const layout of boxes) { - const box = layout.box; - const stack = stacks[layout.stack] || {count: 1, placed: 0, weight: 1}; - const weight = (layout.stackWeight / stack.weight) || 1; - if (layout.horizontal) { - const width = chartArea.w * weight; - const height = stack.size || box.height; - if (defined(stack.start)) { - y = stack.start; - } - if (box.fullSize) { - setBoxDims(box, userPadding.left, y, params.outerWidth - userPadding.right - userPadding.left, height); - } else { - setBoxDims(box, chartArea.left + stack.placed, y, width, height); - } - stack.start = y; - stack.placed += width; - y = box.bottom; - } else { - const height = chartArea.h * weight; - const width = stack.size || box.width; - if (defined(stack.start)) { - x = stack.start; - } - if (box.fullSize) { - setBoxDims(box, x, userPadding.top, width, params.outerHeight - userPadding.bottom - userPadding.top); - } else { - setBoxDims(box, x, chartArea.top + stack.placed, width, height); - } - stack.start = x; - stack.placed += height; - x = box.right; - } +class Scale extends Element { + constructor(cfg) { + super(); + this.id = cfg.id; + this.type = cfg.type; + this.options = undefined; + this.ctx = cfg.ctx; + this.chart = cfg.chart; + this.top = undefined; + this.bottom = undefined; + this.left = undefined; + this.right = undefined; + this.width = undefined; + this.height = undefined; + this._margins = { + left: 0, + right: 0, + top: 0, + bottom: 0 + }; + this.maxWidth = undefined; + this.maxHeight = undefined; + this.paddingTop = undefined; + this.paddingBottom = undefined; + this.paddingLeft = undefined; + this.paddingRight = undefined; + this.axis = undefined; + this.labelRotation = undefined; + this.min = undefined; + this.max = undefined; + this._range = undefined; + this.ticks = []; + this._gridLineItems = null; + this._labelItems = null; + this._labelSizes = null; + this._length = 0; + this._maxLength = 0; + this._longestTextCache = {}; + this._startPixel = undefined; + this._endPixel = undefined; + this._reversePixels = false; + this._userMax = undefined; + this._userMin = undefined; + this._suggestedMax = undefined; + this._suggestedMin = undefined; + this._ticksLength = 0; + this._borderValue = 0; + this._cache = {}; + this._dataLimitsCached = false; + this.$context = undefined; } - chartArea.x = x; - chartArea.y = y; -} -defaults.set('layout', { - autoPadding: true, - padding: { - top: 0, - right: 0, - bottom: 0, - left: 0 + init(options) { + this.options = options.setContext(this.getContext()); + this.axis = options.axis; + this._userMin = this.parse(options.min); + this._userMax = this.parse(options.max); + this._suggestedMin = this.parse(options.suggestedMin); + this._suggestedMax = this.parse(options.suggestedMax); } -}); -var layouts = { - addBox(chart, item) { - if (!chart.boxes) { - chart.boxes = []; - } - item.fullSize = item.fullSize || false; - item.position = item.position || 'top'; - item.weight = item.weight || 0; - item._layers = item._layers || function() { - return [{ - z: 0, - draw(chartArea) { - item.draw(chartArea); - } - }]; + parse(raw, index) { + return raw; + } + getUserBounds() { + let {_userMin, _userMax, _suggestedMin, _suggestedMax} = this; + _userMin = finiteOrDefault(_userMin, Number.POSITIVE_INFINITY); + _userMax = finiteOrDefault(_userMax, Number.NEGATIVE_INFINITY); + _suggestedMin = finiteOrDefault(_suggestedMin, Number.POSITIVE_INFINITY); + _suggestedMax = finiteOrDefault(_suggestedMax, Number.NEGATIVE_INFINITY); + return { + min: finiteOrDefault(_userMin, _suggestedMin), + max: finiteOrDefault(_userMax, _suggestedMax), + minDefined: isNumberFinite(_userMin), + maxDefined: isNumberFinite(_userMax) }; - chart.boxes.push(item); - }, - removeBox(chart, layoutItem) { - const index = chart.boxes ? chart.boxes.indexOf(layoutItem) : -1; - if (index !== -1) { - chart.boxes.splice(index, 1); - } - }, - configure(chart, item, options) { - item.fullSize = options.fullSize; - item.position = options.position; - item.weight = options.weight; - }, - update(chart, width, height, minPadding) { - if (!chart) { - return; + } + getMinMax(canStack) { + let {min, max, minDefined, maxDefined} = this.getUserBounds(); + let range; + if (minDefined && maxDefined) { + return {min, max}; } - const padding = toPadding(chart.options.layout.padding); - const availableWidth = Math.max(width - padding.width, 0); - const availableHeight = Math.max(height - padding.height, 0); - const boxes = buildLayoutBoxes(chart.boxes); - const verticalBoxes = boxes.vertical; - const horizontalBoxes = boxes.horizontal; - each(chart.boxes, box => { - if (typeof box.beforeLayout === 'function') { - box.beforeLayout(); + const metas = this.getMatchingVisibleMetas(); + for (let i = 0, ilen = metas.length; i < ilen; ++i) { + range = metas[i].controller.getMinMax(this, canStack); + if (!minDefined) { + min = Math.min(min, range.min); + } + if (!maxDefined) { + max = Math.max(max, range.max); } - }); - const visibleVerticalBoxCount = verticalBoxes.reduce((total, wrap) => - wrap.box.options && wrap.box.options.display === false ? total : total + 1, 0) || 1; - const params = Object.freeze({ - outerWidth: width, - outerHeight: height, - padding, - availableWidth, - availableHeight, - vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount, - hBoxMaxHeight: availableHeight / 2 - }); - const maxPadding = Object.assign({}, padding); - updateMaxPadding(maxPadding, toPadding(minPadding)); - const chartArea = Object.assign({ - maxPadding, - w: availableWidth, - h: availableHeight, - x: padding.left, - y: padding.top - }, padding); - const stacks = setLayoutDims(verticalBoxes.concat(horizontalBoxes), params); - fitBoxes(boxes.fullSize, chartArea, params, stacks); - fitBoxes(verticalBoxes, chartArea, params, stacks); - if (fitBoxes(horizontalBoxes, chartArea, params, stacks)) { - fitBoxes(verticalBoxes, chartArea, params, stacks); } - handleMaxPadding(chartArea); - placeBoxes(boxes.leftAndTop, chartArea, params, stacks); - chartArea.x += chartArea.w; - chartArea.y += chartArea.h; - placeBoxes(boxes.rightAndBottom, chartArea, params, stacks); - chart.chartArea = { - left: chartArea.left, - top: chartArea.top, - right: chartArea.left + chartArea.w, - bottom: chartArea.top + chartArea.h, - height: chartArea.h, - width: chartArea.w, + min = maxDefined && min > max ? max : min; + max = minDefined && min > max ? min : max; + return { + min: finiteOrDefault(min, finiteOrDefault(max, min)), + max: finiteOrDefault(max, finiteOrDefault(min, max)) }; - each(boxes.chartArea, (layout) => { - const box = layout.box; - Object.assign(box, chart.chartArea); - box.update(chartArea.w, chartArea.h, {left: 0, top: 0, right: 0, bottom: 0}); - }); - } -}; - -class BasePlatform { - acquireContext(canvas, aspectRatio) {} - releaseContext(context) { - return false; - } - addEventListener(chart, type, listener) {} - removeEventListener(chart, type, listener) {} - getDevicePixelRatio() { - return 1; } - getMaximumSize(element, width, height, aspectRatio) { - width = Math.max(0, width || element.width); - height = height || element.height; + getPadding() { return { - width, - height: Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height) + left: this.paddingLeft || 0, + top: this.paddingTop || 0, + right: this.paddingRight || 0, + bottom: this.paddingBottom || 0 }; } - isAttached(canvas) { - return true; + getTicks() { + return this.ticks; } - updateConfig(config) { + getLabels() { + const data = this.chart.data; + return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || []; } -} - -class BasicPlatform extends BasePlatform { - acquireContext(item) { - return item && item.getContext && item.getContext('2d') || null; + beforeLayout() { + this._cache = {}; + this._dataLimitsCached = false; } - updateConfig(config) { - config.options.animation = false; + beforeUpdate() { + callback(this.options.beforeUpdate, [this]); } -} - -const EXPANDO_KEY = '$chartjs'; -const EVENT_TYPES = { - touchstart: 'mousedown', - touchmove: 'mousemove', - touchend: 'mouseup', - pointerenter: 'mouseenter', - pointerdown: 'mousedown', - pointermove: 'mousemove', - pointerup: 'mouseup', - pointerleave: 'mouseout', - pointerout: 'mouseout' -}; -const isNullOrEmpty = value => value === null || value === ''; -function initCanvas(canvas, aspectRatio) { - const style = canvas.style; - const renderHeight = canvas.getAttribute('height'); - const renderWidth = canvas.getAttribute('width'); - canvas[EXPANDO_KEY] = { - initial: { - height: renderHeight, - width: renderWidth, - style: { - display: style.display, - height: style.height, - width: style.width - } + update(maxWidth, maxHeight, margins) { + const {beginAtZero, grace, ticks: tickOpts} = this.options; + const sampleSize = tickOpts.sampleSize; + this.beforeUpdate(); + this.maxWidth = maxWidth; + this.maxHeight = maxHeight; + this._margins = margins = Object.assign({ + left: 0, + right: 0, + top: 0, + bottom: 0 + }, margins); + this.ticks = null; + this._labelSizes = null; + this._gridLineItems = null; + this._labelItems = null; + this.beforeSetDimensions(); + this.setDimensions(); + this.afterSetDimensions(); + this._maxLength = this.isHorizontal() + ? this.width + margins.left + margins.right + : this.height + margins.top + margins.bottom; + if (!this._dataLimitsCached) { + this.beforeDataLimits(); + this.determineDataLimits(); + this.afterDataLimits(); + this._range = _addGrace(this, grace, beginAtZero); + this._dataLimitsCached = true; } - }; - style.display = style.display || 'block'; - style.boxSizing = style.boxSizing || 'border-box'; - if (isNullOrEmpty(renderWidth)) { - const displayWidth = readUsedSize(canvas, 'width'); - if (displayWidth !== undefined) { - canvas.width = displayWidth; + this.beforeBuildTicks(); + this.ticks = this.buildTicks() || []; + this.afterBuildTicks(); + const samplingEnabled = sampleSize < this.ticks.length; + this._convertTicksToLabels(samplingEnabled ? sample(this.ticks, sampleSize) : this.ticks); + this.configure(); + this.beforeCalculateLabelRotation(); + this.calculateLabelRotation(); + this.afterCalculateLabelRotation(); + if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { + this.ticks = autoSkip(this, this.ticks); + this._labelSizes = null; + this.afterAutoSkip(); + } + if (samplingEnabled) { + this._convertTicksToLabels(this.ticks); } + this.beforeFit(); + this.fit(); + this.afterFit(); + this.afterUpdate(); } - if (isNullOrEmpty(renderHeight)) { - if (canvas.style.height === '') { - canvas.height = canvas.width / (aspectRatio || 2); + configure() { + let reversePixels = this.options.reverse; + let startPixel, endPixel; + if (this.isHorizontal()) { + startPixel = this.left; + endPixel = this.right; } else { - const displayHeight = readUsedSize(canvas, 'height'); - if (displayHeight !== undefined) { - canvas.height = displayHeight; - } + startPixel = this.top; + endPixel = this.bottom; + reversePixels = !reversePixels; } + this._startPixel = startPixel; + this._endPixel = endPixel; + this._reversePixels = reversePixels; + this._length = endPixel - startPixel; + this._alignToPixels = this.options.alignToPixels; } - return canvas; -} -const eventListenerOptions = supportsEventListenerOptions ? {passive: true} : false; -function addListener(node, type, listener) { - node.addEventListener(type, listener, eventListenerOptions); -} -function removeListener(chart, type, listener) { - chart.canvas.removeEventListener(type, listener, eventListenerOptions); -} -function fromNativeEvent(event, chart) { - const type = EVENT_TYPES[event.type] || event.type; - const {x, y} = getRelativePosition$1(event, chart); - return { - type, - chart, - native: event, - x: x !== undefined ? x : null, - y: y !== undefined ? y : null, - }; -} -function nodeListContains(nodeList, canvas) { - for (const node of nodeList) { - if (node === canvas || node.contains(canvas)) { - return true; - } + afterUpdate() { + callback(this.options.afterUpdate, [this]); } -} -function createAttachObserver(chart, type, listener) { - const canvas = chart.canvas; - const observer = new MutationObserver(entries => { - let trigger = false; - for (const entry of entries) { - trigger = trigger || nodeListContains(entry.addedNodes, canvas); - trigger = trigger && !nodeListContains(entry.removedNodes, canvas); - } - if (trigger) { - listener(); - } - }); - observer.observe(document, {childList: true, subtree: true}); - return observer; -} -function createDetachObserver(chart, type, listener) { - const canvas = chart.canvas; - const observer = new MutationObserver(entries => { - let trigger = false; - for (const entry of entries) { - trigger = trigger || nodeListContains(entry.removedNodes, canvas); - trigger = trigger && !nodeListContains(entry.addedNodes, canvas); - } - if (trigger) { - listener(); - } - }); - observer.observe(document, {childList: true, subtree: true}); - return observer; -} -const drpListeningCharts = new Map(); -let oldDevicePixelRatio = 0; -function onWindowResize() { - const dpr = window.devicePixelRatio; - if (dpr === oldDevicePixelRatio) { - return; + beforeSetDimensions() { + callback(this.options.beforeSetDimensions, [this]); } - oldDevicePixelRatio = dpr; - drpListeningCharts.forEach((resize, chart) => { - if (chart.currentDevicePixelRatio !== dpr) { - resize(); + setDimensions() { + if (this.isHorizontal()) { + this.width = this.maxWidth; + this.left = 0; + this.right = this.width; + } else { + this.height = this.maxHeight; + this.top = 0; + this.bottom = this.height; } - }); -} -function listenDevicePixelRatioChanges(chart, resize) { - if (!drpListeningCharts.size) { - window.addEventListener('resize', onWindowResize); + this.paddingLeft = 0; + this.paddingTop = 0; + this.paddingRight = 0; + this.paddingBottom = 0; } - drpListeningCharts.set(chart, resize); -} -function unlistenDevicePixelRatioChanges(chart) { - drpListeningCharts.delete(chart); - if (!drpListeningCharts.size) { - window.removeEventListener('resize', onWindowResize); + afterSetDimensions() { + callback(this.options.afterSetDimensions, [this]); } -} -function createResizeObserver(chart, type, listener) { - const canvas = chart.canvas; - const container = canvas && _getParentNode(canvas); - if (!container) { - return; + _callHooks(name) { + this.chart.notifyPlugins(name, this.getContext()); + callback(this.options[name], [this]); } - const resize = throttled((width, height) => { - const w = container.clientWidth; - listener(width, height); - if (w < container.clientWidth) { - listener(); - } - }, window); - const observer = new ResizeObserver(entries => { - const entry = entries[0]; - const width = entry.contentRect.width; - const height = entry.contentRect.height; - if (width === 0 && height === 0) { - return; - } - resize(width, height); - }); - observer.observe(container); - listenDevicePixelRatioChanges(chart, resize); - return observer; -} -function releaseObserver(chart, type, observer) { - if (observer) { - observer.disconnect(); + beforeDataLimits() { + this._callHooks('beforeDataLimits'); } - if (type === 'resize') { - unlistenDevicePixelRatioChanges(chart); + determineDataLimits() {} + afterDataLimits() { + this._callHooks('afterDataLimits'); } -} -function createProxyAndListen(chart, type, listener) { - const canvas = chart.canvas; - const proxy = throttled((event) => { - if (chart.ctx !== null) { - listener(fromNativeEvent(event, chart)); - } - }, chart, (args) => { - const event = args[0]; - return [event, event.offsetX, event.offsetY]; - }); - addListener(canvas, type, proxy); - return proxy; -} -class DomPlatform extends BasePlatform { - acquireContext(canvas, aspectRatio) { - const context = canvas && canvas.getContext && canvas.getContext('2d'); - if (context && context.canvas === canvas) { - initCanvas(canvas, aspectRatio); - return context; - } - return null; + beforeBuildTicks() { + this._callHooks('beforeBuildTicks'); } - releaseContext(context) { - const canvas = context.canvas; - if (!canvas[EXPANDO_KEY]) { - return false; - } - const initial = canvas[EXPANDO_KEY].initial; - ['height', 'width'].forEach((prop) => { - const value = initial[prop]; - if (isNullOrUndef(value)) { - canvas.removeAttribute(prop); - } else { - canvas.setAttribute(prop, value); - } - }); - const style = initial.style || {}; - Object.keys(style).forEach((key) => { - canvas.style[key] = style[key]; - }); - canvas.width = canvas.width; - delete canvas[EXPANDO_KEY]; - return true; - } - addEventListener(chart, type, listener) { - this.removeEventListener(chart, type); - const proxies = chart.$proxies || (chart.$proxies = {}); - const handlers = { - attach: createAttachObserver, - detach: createDetachObserver, - resize: createResizeObserver - }; - const handler = handlers[type] || createProxyAndListen; - proxies[type] = handler(chart, type, listener); + buildTicks() { + return []; } - removeEventListener(chart, type) { - const proxies = chart.$proxies || (chart.$proxies = {}); - const proxy = proxies[type]; - if (!proxy) { - return; - } - const handlers = { - attach: releaseObserver, - detach: releaseObserver, - resize: releaseObserver - }; - const handler = handlers[type] || removeListener; - handler(chart, type, proxy); - proxies[type] = undefined; + afterBuildTicks() { + this._callHooks('afterBuildTicks'); } - getDevicePixelRatio() { - return window.devicePixelRatio; + beforeTickToLabelConversion() { + callback(this.options.beforeTickToLabelConversion, [this]); } - getMaximumSize(canvas, width, height, aspectRatio) { - return getMaximumSize(canvas, width, height, aspectRatio); + generateTickLabels(ticks) { + const tickOpts = this.options.ticks; + let i, ilen, tick; + for (i = 0, ilen = ticks.length; i < ilen; i++) { + tick = ticks[i]; + tick.label = callback(tickOpts.callback, [tick.value, i, ticks], this); + } } - isAttached(canvas) { - const container = _getParentNode(canvas); - return !!(container && container.isConnected); + afterTickToLabelConversion() { + callback(this.options.afterTickToLabelConversion, [this]); } -} - -function _detectPlatform(canvas) { - if (!_isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) { - return BasicPlatform; + beforeCalculateLabelRotation() { + callback(this.options.beforeCalculateLabelRotation, [this]); } - return DomPlatform; -} - -class Element { - constructor() { - this.x = undefined; - this.y = undefined; - this.active = false; - this.options = undefined; - this.$animations = undefined; + calculateLabelRotation() { + const options = this.options; + const tickOpts = options.ticks; + const numTicks = this.ticks.length; + const minRotation = tickOpts.minRotation || 0; + const maxRotation = tickOpts.maxRotation; + let labelRotation = minRotation; + let tickWidth, maxHeight, maxLabelDiagonal; + if (!this._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !this.isHorizontal()) { + this.labelRotation = minRotation; + return; + } + const labelSizes = this._getLabelSizes(); + const maxLabelWidth = labelSizes.widest.width; + const maxLabelHeight = labelSizes.highest.height; + const maxWidth = _limitValue(this.chart.width - maxLabelWidth, 0, this.maxWidth); + tickWidth = options.offset ? this.maxWidth / numTicks : maxWidth / (numTicks - 1); + if (maxLabelWidth + 6 > tickWidth) { + tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1)); + maxHeight = this.maxHeight - getTickMarkLength(options.grid) + - tickOpts.padding - getTitleHeight(options.title, this.chart.options.font); + maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight); + labelRotation = toDegrees(Math.min( + Math.asin(_limitValue((labelSizes.highest.height + 6) / tickWidth, -1, 1)), + Math.asin(_limitValue(maxHeight / maxLabelDiagonal, -1, 1)) - Math.asin(_limitValue(maxLabelHeight / maxLabelDiagonal, -1, 1)) + )); + labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation)); + } + this.labelRotation = labelRotation; } - tooltipPosition(useFinalPosition) { - const {x, y} = this.getProps(['x', 'y'], useFinalPosition); - return {x, y}; + afterCalculateLabelRotation() { + callback(this.options.afterCalculateLabelRotation, [this]); } - hasValue() { - return isNumber(this.x) && isNumber(this.y); + afterAutoSkip() {} + beforeFit() { + callback(this.options.beforeFit, [this]); } - getProps(props, final) { - const anims = this.$animations; - if (!final || !anims) { - return this; + fit() { + const minSize = { + width: 0, + height: 0 + }; + const {chart, options: {ticks: tickOpts, title: titleOpts, grid: gridOpts}} = this; + const display = this._isVisible(); + const isHorizontal = this.isHorizontal(); + if (display) { + const titleHeight = getTitleHeight(titleOpts, chart.options.font); + if (isHorizontal) { + minSize.width = this.maxWidth; + minSize.height = getTickMarkLength(gridOpts) + titleHeight; + } else { + minSize.height = this.maxHeight; + minSize.width = getTickMarkLength(gridOpts) + titleHeight; + } + if (tickOpts.display && this.ticks.length) { + const {first, last, widest, highest} = this._getLabelSizes(); + const tickPadding = tickOpts.padding * 2; + const angleRadians = toRadians(this.labelRotation); + const cos = Math.cos(angleRadians); + const sin = Math.sin(angleRadians); + if (isHorizontal) { + const labelHeight = tickOpts.mirror ? 0 : sin * widest.width + cos * highest.height; + minSize.height = Math.min(this.maxHeight, minSize.height + labelHeight + tickPadding); + } else { + const labelWidth = tickOpts.mirror ? 0 : cos * widest.width + sin * highest.height; + minSize.width = Math.min(this.maxWidth, minSize.width + labelWidth + tickPadding); + } + this._calculatePadding(first, last, sin, cos); + } } - const ret = {}; - props.forEach(prop => { - ret[prop] = anims[prop] && anims[prop].active() ? anims[prop]._to : this[prop]; - }); - return ret; - } -} -Element.defaults = {}; -Element.defaultRoutes = undefined; - -const formatters = { - values(value) { - return isArray(value) ? value : '' + value; - }, - numeric(tickValue, index, ticks) { - if (tickValue === 0) { - return '0'; + this._handleMargins(); + if (isHorizontal) { + this.width = this._length = chart.width - this._margins.left - this._margins.right; + this.height = minSize.height; + } else { + this.width = minSize.width; + this.height = this._length = chart.height - this._margins.top - this._margins.bottom; } - const locale = this.chart.options.locale; - let notation; - let delta = tickValue; - if (ticks.length > 1) { - const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); - if (maxTick < 1e-4 || maxTick > 1e+15) { - notation = 'scientific'; + } + _calculatePadding(first, last, sin, cos) { + const {ticks: {align, padding}, position} = this.options; + const isRotated = this.labelRotation !== 0; + const labelsBelowTicks = position !== 'top' && this.axis === 'x'; + if (this.isHorizontal()) { + const offsetLeft = this.getPixelForTick(0) - this.left; + const offsetRight = this.right - this.getPixelForTick(this.ticks.length - 1); + let paddingLeft = 0; + let paddingRight = 0; + if (isRotated) { + if (labelsBelowTicks) { + paddingLeft = cos * first.width; + paddingRight = sin * last.height; + } else { + paddingLeft = sin * first.height; + paddingRight = cos * last.width; + } + } else if (align === 'start') { + paddingRight = last.width; + } else if (align === 'end') { + paddingLeft = first.width; + } else if (align !== 'inner') { + paddingLeft = first.width / 2; + paddingRight = last.width / 2; } - delta = calculateDelta(tickValue, ticks); - } - const logDelta = log10(Math.abs(delta)); - const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); - const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; - Object.assign(options, this.options.ticks.format); - return formatNumber(tickValue, locale, options); - }, - logarithmic(tickValue, index, ticks) { - if (tickValue === 0) { - return '0'; + this.paddingLeft = Math.max((paddingLeft - offsetLeft + padding) * this.width / (this.width - offsetLeft), 0); + this.paddingRight = Math.max((paddingRight - offsetRight + padding) * this.width / (this.width - offsetRight), 0); + } else { + let paddingTop = last.height / 2; + let paddingBottom = first.height / 2; + if (align === 'start') { + paddingTop = 0; + paddingBottom = first.height; + } else if (align === 'end') { + paddingTop = last.height; + paddingBottom = 0; + } + this.paddingTop = paddingTop + padding; + this.paddingBottom = paddingBottom + padding; } - const remain = tickValue / (Math.pow(10, Math.floor(log10(tickValue)))); - if (remain === 1 || remain === 2 || remain === 5) { - return formatters.numeric.call(this, tickValue, index, ticks); + } + _handleMargins() { + if (this._margins) { + this._margins.left = Math.max(this.paddingLeft, this._margins.left); + this._margins.top = Math.max(this.paddingTop, this._margins.top); + this._margins.right = Math.max(this.paddingRight, this._margins.right); + this._margins.bottom = Math.max(this.paddingBottom, this._margins.bottom); } - return ''; } -}; -function calculateDelta(tickValue, ticks) { - let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value; - if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) { - delta = tickValue - Math.floor(tickValue); + afterFit() { + callback(this.options.afterFit, [this]); } - return delta; -} -var Ticks = {formatters}; - -defaults.set('scale', { - display: true, - offset: false, - reverse: false, - beginAtZero: false, - bounds: 'ticks', - grace: 0, - grid: { - display: true, - lineWidth: 1, - drawBorder: true, - drawOnChartArea: true, - drawTicks: true, - tickLength: 8, - tickWidth: (_ctx, options) => options.lineWidth, - tickColor: (_ctx, options) => options.color, - offset: false, - borderDash: [], - borderDashOffset: 0.0, - borderWidth: 1 - }, - title: { - display: false, - text: '', - padding: { - top: 4, - bottom: 4 - } - }, - ticks: { - minRotation: 0, - maxRotation: 50, - mirror: false, - textStrokeWidth: 0, - textStrokeColor: '', - padding: 3, - display: true, - autoSkip: true, - autoSkipPadding: 3, - labelOffset: 0, - callback: Ticks.formatters.values, - minor: {}, - major: {}, - align: 'center', - crossAlign: 'near', - showLabelBackdrop: false, - backdropColor: 'rgba(255, 255, 255, 0.75)', - backdropPadding: 2, + isHorizontal() { + const {axis, position} = this.options; + return position === 'top' || position === 'bottom' || axis === 'x'; } -}); -defaults.route('scale.ticks', 'color', '', 'color'); -defaults.route('scale.grid', 'color', '', 'borderColor'); -defaults.route('scale.grid', 'borderColor', '', 'borderColor'); -defaults.route('scale.title', 'color', '', 'color'); -defaults.describe('scale', { - _fallback: false, - _scriptable: (name) => !name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser', - _indexable: (name) => name !== 'borderDash' && name !== 'tickBorderDash', -}); -defaults.describe('scales', { - _fallback: 'scale', -}); -defaults.describe('scale.ticks', { - _scriptable: (name) => name !== 'backdropPadding' && name !== 'callback', - _indexable: (name) => name !== 'backdropPadding', -}); - -function autoSkip(scale, ticks) { - const tickOpts = scale.options.ticks; - const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); - const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; - const numMajorIndices = majorIndices.length; - const first = majorIndices[0]; - const last = majorIndices[numMajorIndices - 1]; - const newTicks = []; - if (numMajorIndices > ticksLimit) { - skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit); - return newTicks; + isFullSize() { + return this.options.fullSize; } - const spacing = calculateSpacing(majorIndices, ticks, ticksLimit); - if (numMajorIndices > 0) { + _convertTicksToLabels(ticks) { + this.beforeTickToLabelConversion(); + this.generateTickLabels(ticks); let i, ilen; - const avgMajorSpacing = numMajorIndices > 1 ? Math.round((last - first) / (numMajorIndices - 1)) : null; - skip(ticks, newTicks, spacing, isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first); - for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) { - skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]); + for (i = 0, ilen = ticks.length; i < ilen; i++) { + if (isNullOrUndef(ticks[i].label)) { + ticks.splice(i, 1); + ilen--; + i--; + } } - skip(ticks, newTicks, spacing, last, isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing); - return newTicks; - } - skip(ticks, newTicks, spacing); - return newTicks; -} -function determineMaxTicks(scale) { - const offset = scale.options.offset; - const tickLength = scale._tickSize(); - const maxScale = scale._length / tickLength + (offset ? 0 : 1); - const maxChart = scale._maxLength / tickLength; - return Math.floor(Math.min(maxScale, maxChart)); -} -function calculateSpacing(majorIndices, ticks, ticksLimit) { - const evenMajorSpacing = getEvenSpacing(majorIndices); - const spacing = ticks.length / ticksLimit; - if (!evenMajorSpacing) { - return Math.max(spacing, 1); + this.afterTickToLabelConversion(); } - const factors = _factorize(evenMajorSpacing); - for (let i = 0, ilen = factors.length - 1; i < ilen; i++) { - const factor = factors[i]; - if (factor > spacing) { - return factor; + _getLabelSizes() { + let labelSizes = this._labelSizes; + if (!labelSizes) { + const sampleSize = this.options.ticks.sampleSize; + let ticks = this.ticks; + if (sampleSize < ticks.length) { + ticks = sample(ticks, sampleSize); + } + this._labelSizes = labelSizes = this._computeLabelSizes(ticks, ticks.length); } + return labelSizes; } - return Math.max(spacing, 1); -} -function getMajorIndices(ticks) { - const result = []; - let i, ilen; - for (i = 0, ilen = ticks.length; i < ilen; i++) { - if (ticks[i].major) { - result.push(i); + _computeLabelSizes(ticks, length) { + const {ctx, _longestTextCache: caches} = this; + const widths = []; + const heights = []; + let widestLabelSize = 0; + let highestLabelSize = 0; + let i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel; + for (i = 0; i < length; ++i) { + label = ticks[i].label; + tickFont = this._resolveTickFontOptions(i); + ctx.font = fontString = tickFont.string; + cache = caches[fontString] = caches[fontString] || {data: {}, gc: []}; + lineHeight = tickFont.lineHeight; + width = height = 0; + if (!isNullOrUndef(label) && !isArray(label)) { + width = _measureText(ctx, cache.data, cache.gc, width, label); + height = lineHeight; + } else if (isArray(label)) { + for (j = 0, jlen = label.length; j < jlen; ++j) { + nestedLabel = label[j]; + if (!isNullOrUndef(nestedLabel) && !isArray(nestedLabel)) { + width = _measureText(ctx, cache.data, cache.gc, width, nestedLabel); + height += lineHeight; + } + } + } + widths.push(width); + heights.push(height); + widestLabelSize = Math.max(width, widestLabelSize); + highestLabelSize = Math.max(height, highestLabelSize); } + garbageCollect(caches, length); + const widest = widths.indexOf(widestLabelSize); + const highest = heights.indexOf(highestLabelSize); + const valueAt = (idx) => ({width: widths[idx] || 0, height: heights[idx] || 0}); + return { + first: valueAt(0), + last: valueAt(length - 1), + widest: valueAt(widest), + highest: valueAt(highest), + widths, + heights, + }; } - return result; -} -function skipMajors(ticks, newTicks, majorIndices, spacing) { - let count = 0; - let next = majorIndices[0]; - let i; - spacing = Math.ceil(spacing); - for (i = 0; i < ticks.length; i++) { - if (i === next) { - newTicks.push(ticks[i]); - count++; - next = majorIndices[count * spacing]; - } + getLabelForValue(value) { + return value; } -} -function skip(ticks, newTicks, spacing, majorStart, majorEnd) { - const start = valueOrDefault(majorStart, 0); - const end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length); - let count = 0; - let length, i, next; - spacing = Math.ceil(spacing); - if (majorEnd) { - length = majorEnd - majorStart; - spacing = length / Math.floor(length / spacing); + getPixelForValue(value, index) { + return NaN; } - next = start; - while (next < 0) { - count++; - next = Math.round(start + count * spacing); + getValueForPixel(pixel) {} + getPixelForTick(index) { + const ticks = this.ticks; + if (index < 0 || index > ticks.length - 1) { + return null; + } + return this.getPixelForValue(ticks[index].value); } - for (i = Math.max(start, 0); i < end; i++) { - if (i === next) { - newTicks.push(ticks[i]); - count++; - next = Math.round(start + count * spacing); + getPixelForDecimal(decimal) { + if (this._reversePixels) { + decimal = 1 - decimal; } + const pixel = this._startPixel + decimal * this._length; + return _int16Range(this._alignToPixels ? _alignPixel(this.chart, pixel, 0) : pixel); } -} -function getEvenSpacing(arr) { - const len = arr.length; - let i, diff; - if (len < 2) { - return false; + getDecimalForPixel(pixel) { + const decimal = (pixel - this._startPixel) / this._length; + return this._reversePixels ? 1 - decimal : decimal; } - for (diff = arr[0], i = 1; i < len; ++i) { - if (arr[i] - arr[i - 1] !== diff) { - return false; + getBasePixel() { + return this.getPixelForValue(this.getBaseValue()); + } + getBaseValue() { + const {min, max} = this; + return min < 0 && max < 0 ? max : + min > 0 && max > 0 ? min : + 0; + } + getContext(index) { + const ticks = this.ticks || []; + if (index >= 0 && index < ticks.length) { + const tick = ticks[index]; + return tick.$context || + (tick.$context = createTickContext(this.getContext(), index, tick)); } + return this.$context || + (this.$context = createScaleContext(this.chart.getContext(), this)); } - return diff; -} - -const reverseAlign = (align) => align === 'left' ? 'right' : align === 'right' ? 'left' : align; -const offsetFromEdge = (scale, edge, offset) => edge === 'top' || edge === 'left' ? scale[edge] + offset : scale[edge] - offset; -function sample(arr, numItems) { - const result = []; - const increment = arr.length / numItems; - const len = arr.length; - let i = 0; - for (; i < len; i += increment) { - result.push(arr[Math.floor(i)]); + _tickSize() { + const optionTicks = this.options.ticks; + const rot = toRadians(this.labelRotation); + const cos = Math.abs(Math.cos(rot)); + const sin = Math.abs(Math.sin(rot)); + const labelSizes = this._getLabelSizes(); + const padding = optionTicks.autoSkipPadding || 0; + const w = labelSizes ? labelSizes.widest.width + padding : 0; + const h = labelSizes ? labelSizes.highest.height + padding : 0; + return this.isHorizontal() + ? h * cos > w * sin ? w / cos : h / sin + : h * sin < w * cos ? h / cos : w / sin; } - return result; -} -function getPixelForGridLine(scale, index, offsetGridLines) { - const length = scale.ticks.length; - const validIndex = Math.min(index, length - 1); - const start = scale._startPixel; - const end = scale._endPixel; - const epsilon = 1e-6; - let lineValue = scale.getPixelForTick(validIndex); - let offset; - if (offsetGridLines) { - if (length === 1) { - offset = Math.max(lineValue - start, end - lineValue); - } else if (index === 0) { - offset = (scale.getPixelForTick(1) - lineValue) / 2; - } else { - offset = (lineValue - scale.getPixelForTick(validIndex - 1)) / 2; - } - lineValue += validIndex < index ? offset : -offset; - if (lineValue < start - epsilon || lineValue > end + epsilon) { - return; + _isVisible() { + const display = this.options.display; + if (display !== 'auto') { + return !!display; } + return this.getMatchingVisibleMetas().length > 0; } - return lineValue; -} -function garbageCollect(caches, length) { - each(caches, (cache) => { - const gc = cache.gc; - const gcLen = gc.length / 2; - let i; - if (gcLen > length) { - for (i = 0; i < gcLen; ++i) { - delete cache.data[gc[i]]; + _computeGridLineItems(chartArea) { + const axis = this.axis; + const chart = this.chart; + const options = this.options; + const {grid, position} = options; + const offset = grid.offset; + const isHorizontal = this.isHorizontal(); + const ticks = this.ticks; + const ticksLength = ticks.length + (offset ? 1 : 0); + const tl = getTickMarkLength(grid); + const items = []; + const borderOpts = grid.setContext(this.getContext()); + const axisWidth = borderOpts.drawBorder ? borderOpts.borderWidth : 0; + const axisHalfWidth = axisWidth / 2; + const alignBorderValue = function(pixel) { + return _alignPixel(chart, pixel, axisWidth); + }; + let borderValue, i, lineValue, alignedLineValue; + let tx1, ty1, tx2, ty2, x1, y1, x2, y2; + if (position === 'top') { + borderValue = alignBorderValue(this.bottom); + ty1 = this.bottom - tl; + ty2 = borderValue - axisHalfWidth; + y1 = alignBorderValue(chartArea.top) + axisHalfWidth; + y2 = chartArea.bottom; + } else if (position === 'bottom') { + borderValue = alignBorderValue(this.top); + y1 = chartArea.top; + y2 = alignBorderValue(chartArea.bottom) - axisHalfWidth; + ty1 = borderValue + axisHalfWidth; + ty2 = this.top + tl; + } else if (position === 'left') { + borderValue = alignBorderValue(this.right); + tx1 = this.right - tl; + tx2 = borderValue - axisHalfWidth; + x1 = alignBorderValue(chartArea.left) + axisHalfWidth; + x2 = chartArea.right; + } else if (position === 'right') { + borderValue = alignBorderValue(this.left); + x1 = chartArea.left; + x2 = alignBorderValue(chartArea.right) - axisHalfWidth; + tx1 = borderValue + axisHalfWidth; + tx2 = this.left + tl; + } else if (axis === 'x') { + if (position === 'center') { + borderValue = alignBorderValue((chartArea.top + chartArea.bottom) / 2 + 0.5); + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); } - gc.splice(0, gcLen); - } - }); -} -function getTickMarkLength(options) { - return options.drawTicks ? options.tickLength : 0; -} -function getTitleHeight(options, fallback) { - if (!options.display) { - return 0; - } - const font = toFont(options.font, fallback); - const padding = toPadding(options.padding); - const lines = isArray(options.text) ? options.text.length : 1; - return (lines * font.lineHeight) + padding.height; -} -function createScaleContext(parent, scale) { - return createContext(parent, { - scale, - type: 'scale' - }); -} -function createTickContext(parent, index, tick) { - return createContext(parent, { - tick, - index, - type: 'tick' - }); -} -function titleAlign(align, position, reverse) { - let ret = _toLeftRightCenter(align); - if ((reverse && position !== 'right') || (!reverse && position === 'right')) { - ret = reverseAlign(ret); - } - return ret; -} -function titleArgs(scale, offset, position, align) { - const {top, left, bottom, right, chart} = scale; - const {chartArea, scales} = chart; - let rotation = 0; - let maxWidth, titleX, titleY; - const height = bottom - top; - const width = right - left; - if (scale.isHorizontal()) { - titleX = _alignStartEnd(align, left, right); - if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - titleY = scales[positionAxisID].getPixelForValue(value) + height - offset; - } else if (position === 'center') { - titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset; - } else { - titleY = offsetFromEdge(scale, position, offset); + y1 = chartArea.top; + y2 = chartArea.bottom; + ty1 = borderValue + axisHalfWidth; + ty2 = ty1 + tl; + } else if (axis === 'y') { + if (position === 'center') { + borderValue = alignBorderValue((chartArea.left + chartArea.right) / 2); + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); + } + tx1 = borderValue - axisHalfWidth; + tx2 = tx1 - tl; + x1 = chartArea.left; + x2 = chartArea.right; } - maxWidth = right - left; - } else { - if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - titleX = scales[positionAxisID].getPixelForValue(value) - width + offset; - } else if (position === 'center') { - titleX = (chartArea.left + chartArea.right) / 2 - width + offset; - } else { - titleX = offsetFromEdge(scale, position, offset); + const limit = valueOrDefault(options.ticks.maxTicksLimit, ticksLength); + const step = Math.max(1, Math.ceil(ticksLength / limit)); + for (i = 0; i < ticksLength; i += step) { + const optsAtIndex = grid.setContext(this.getContext(i)); + const lineWidth = optsAtIndex.lineWidth; + const lineColor = optsAtIndex.color; + const borderDash = optsAtIndex.borderDash || []; + const borderDashOffset = optsAtIndex.borderDashOffset; + const tickWidth = optsAtIndex.tickWidth; + const tickColor = optsAtIndex.tickColor; + const tickBorderDash = optsAtIndex.tickBorderDash || []; + const tickBorderDashOffset = optsAtIndex.tickBorderDashOffset; + lineValue = getPixelForGridLine(this, i, offset); + if (lineValue === undefined) { + continue; + } + alignedLineValue = _alignPixel(chart, lineValue, lineWidth); + if (isHorizontal) { + tx1 = tx2 = x1 = x2 = alignedLineValue; + } else { + ty1 = ty2 = y1 = y2 = alignedLineValue; + } + items.push({ + tx1, + ty1, + tx2, + ty2, + x1, + y1, + x2, + y2, + width: lineWidth, + color: lineColor, + borderDash, + borderDashOffset, + tickWidth, + tickColor, + tickBorderDash, + tickBorderDashOffset, + }); } - titleY = _alignStartEnd(align, bottom, top); - rotation = position === 'left' ? -HALF_PI : HALF_PI; + this._ticksLength = ticksLength; + this._borderValue = borderValue; + return items; } - return {titleX, titleY, maxWidth, rotation}; -} -class Scale extends Element { - constructor(cfg) { - super(); - this.id = cfg.id; - this.type = cfg.type; - this.options = undefined; - this.ctx = cfg.ctx; - this.chart = cfg.chart; - this.top = undefined; - this.bottom = undefined; - this.left = undefined; - this.right = undefined; - this.width = undefined; - this.height = undefined; - this._margins = { - left: 0, - right: 0, - top: 0, - bottom: 0 - }; - this.maxWidth = undefined; - this.maxHeight = undefined; - this.paddingTop = undefined; - this.paddingBottom = undefined; - this.paddingLeft = undefined; - this.paddingRight = undefined; - this.axis = undefined; - this.labelRotation = undefined; - this.min = undefined; - this.max = undefined; - this._range = undefined; - this.ticks = []; - this._gridLineItems = null; - this._labelItems = null; - this._labelSizes = null; - this._length = 0; - this._maxLength = 0; - this._longestTextCache = {}; - this._startPixel = undefined; - this._endPixel = undefined; - this._reversePixels = false; - this._userMax = undefined; - this._userMin = undefined; - this._suggestedMax = undefined; - this._suggestedMin = undefined; - this._ticksLength = 0; - this._borderValue = 0; - this._cache = {}; - this._dataLimitsCached = false; - this.$context = undefined; - } - init(options) { - this.options = options.setContext(this.getContext()); - this.axis = options.axis; - this._userMin = this.parse(options.min); - this._userMax = this.parse(options.max); - this._suggestedMin = this.parse(options.suggestedMin); - this._suggestedMax = this.parse(options.suggestedMax); - } - parse(raw, index) { - return raw; - } - getUserBounds() { - let {_userMin, _userMax, _suggestedMin, _suggestedMax} = this; - _userMin = finiteOrDefault(_userMin, Number.POSITIVE_INFINITY); - _userMax = finiteOrDefault(_userMax, Number.NEGATIVE_INFINITY); - _suggestedMin = finiteOrDefault(_suggestedMin, Number.POSITIVE_INFINITY); - _suggestedMax = finiteOrDefault(_suggestedMax, Number.NEGATIVE_INFINITY); - return { - min: finiteOrDefault(_userMin, _suggestedMin), - max: finiteOrDefault(_userMax, _suggestedMax), - minDefined: isNumberFinite(_userMin), - maxDefined: isNumberFinite(_userMax) - }; - } - getMinMax(canStack) { - let {min, max, minDefined, maxDefined} = this.getUserBounds(); - let range; - if (minDefined && maxDefined) { - return {min, max}; + _computeLabelItems(chartArea) { + const axis = this.axis; + const options = this.options; + const {position, ticks: optionTicks} = options; + const isHorizontal = this.isHorizontal(); + const ticks = this.ticks; + const {align, crossAlign, padding, mirror} = optionTicks; + const tl = getTickMarkLength(options.grid); + const tickAndPadding = tl + padding; + const hTickAndPadding = mirror ? -padding : tickAndPadding; + const rotation = -toRadians(this.labelRotation); + const items = []; + let i, ilen, tick, label, x, y, textAlign, pixel, font, lineHeight, lineCount, textOffset; + let textBaseline = 'middle'; + if (position === 'top') { + y = this.bottom - hTickAndPadding; + textAlign = this._getXAxisLabelAlignment(); + } else if (position === 'bottom') { + y = this.top + hTickAndPadding; + textAlign = this._getXAxisLabelAlignment(); + } else if (position === 'left') { + const ret = this._getYAxisLabelAlignment(tl); + textAlign = ret.textAlign; + x = ret.x; + } else if (position === 'right') { + const ret = this._getYAxisLabelAlignment(tl); + textAlign = ret.textAlign; + x = ret.x; + } else if (axis === 'x') { + if (position === 'center') { + y = ((chartArea.top + chartArea.bottom) / 2) + tickAndPadding; + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + y = this.chart.scales[positionAxisID].getPixelForValue(value) + tickAndPadding; + } + textAlign = this._getXAxisLabelAlignment(); + } else if (axis === 'y') { + if (position === 'center') { + x = ((chartArea.left + chartArea.right) / 2) - tickAndPadding; + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + x = this.chart.scales[positionAxisID].getPixelForValue(value); + } + textAlign = this._getYAxisLabelAlignment(tl).textAlign; } - const metas = this.getMatchingVisibleMetas(); - for (let i = 0, ilen = metas.length; i < ilen; ++i) { - range = metas[i].controller.getMinMax(this, canStack); - if (!minDefined) { - min = Math.min(min, range.min); + if (axis === 'y') { + if (align === 'start') { + textBaseline = 'top'; + } else if (align === 'end') { + textBaseline = 'bottom'; } - if (!maxDefined) { - max = Math.max(max, range.max); + } + const labelSizes = this._getLabelSizes(); + for (i = 0, ilen = ticks.length; i < ilen; ++i) { + tick = ticks[i]; + label = tick.label; + const optsAtIndex = optionTicks.setContext(this.getContext(i)); + pixel = this.getPixelForTick(i) + optionTicks.labelOffset; + font = this._resolveTickFontOptions(i); + lineHeight = font.lineHeight; + lineCount = isArray(label) ? label.length : 1; + const halfCount = lineCount / 2; + const color = optsAtIndex.color; + const strokeColor = optsAtIndex.textStrokeColor; + const strokeWidth = optsAtIndex.textStrokeWidth; + let tickTextAlign = textAlign; + if (isHorizontal) { + x = pixel; + if (textAlign === 'inner') { + if (i === ilen - 1) { + tickTextAlign = !this.options.reverse ? 'right' : 'left'; + } else if (i === 0) { + tickTextAlign = !this.options.reverse ? 'left' : 'right'; + } else { + tickTextAlign = 'center'; + } + } + if (position === 'top') { + if (crossAlign === 'near' || rotation !== 0) { + textOffset = -lineCount * lineHeight + lineHeight / 2; + } else if (crossAlign === 'center') { + textOffset = -labelSizes.highest.height / 2 - halfCount * lineHeight + lineHeight; + } else { + textOffset = -labelSizes.highest.height + lineHeight / 2; + } + } else { + if (crossAlign === 'near' || rotation !== 0) { + textOffset = lineHeight / 2; + } else if (crossAlign === 'center') { + textOffset = labelSizes.highest.height / 2 - halfCount * lineHeight; + } else { + textOffset = labelSizes.highest.height - lineCount * lineHeight; + } + } + if (mirror) { + textOffset *= -1; + } + } else { + y = pixel; + textOffset = (1 - lineCount) * lineHeight / 2; } + let backdrop; + if (optsAtIndex.showLabelBackdrop) { + const labelPadding = toPadding(optsAtIndex.backdropPadding); + const height = labelSizes.heights[i]; + const width = labelSizes.widths[i]; + let top = y + textOffset - labelPadding.top; + let left = x - labelPadding.left; + switch (textBaseline) { + case 'middle': + top -= height / 2; + break; + case 'bottom': + top -= height; + break; + } + switch (textAlign) { + case 'center': + left -= width / 2; + break; + case 'right': + left -= width; + break; + } + backdrop = { + left, + top, + width: width + labelPadding.width, + height: height + labelPadding.height, + color: optsAtIndex.backdropColor, + }; + } + items.push({ + rotation, + label, + font, + color, + strokeColor, + strokeWidth, + textOffset, + textAlign: tickTextAlign, + textBaseline, + translation: [x, y], + backdrop, + }); } - min = maxDefined && min > max ? max : min; - max = minDefined && min > max ? min : max; - return { - min: finiteOrDefault(min, finiteOrDefault(max, min)), - max: finiteOrDefault(max, finiteOrDefault(min, max)) - }; - } - getPadding() { - return { - left: this.paddingLeft || 0, - top: this.paddingTop || 0, - right: this.paddingRight || 0, - bottom: this.paddingBottom || 0 - }; - } - getTicks() { - return this.ticks; - } - getLabels() { - const data = this.chart.data; - return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || []; - } - beforeLayout() { - this._cache = {}; - this._dataLimitsCached = false; - } - beforeUpdate() { - callback(this.options.beforeUpdate, [this]); + return items; } - update(maxWidth, maxHeight, margins) { - const {beginAtZero, grace, ticks: tickOpts} = this.options; - const sampleSize = tickOpts.sampleSize; - this.beforeUpdate(); - this.maxWidth = maxWidth; - this.maxHeight = maxHeight; - this._margins = margins = Object.assign({ - left: 0, - right: 0, - top: 0, - bottom: 0 - }, margins); - this.ticks = null; - this._labelSizes = null; - this._gridLineItems = null; - this._labelItems = null; - this.beforeSetDimensions(); - this.setDimensions(); - this.afterSetDimensions(); - this._maxLength = this.isHorizontal() - ? this.width + margins.left + margins.right - : this.height + margins.top + margins.bottom; - if (!this._dataLimitsCached) { - this.beforeDataLimits(); - this.determineDataLimits(); - this.afterDataLimits(); - this._range = _addGrace(this, grace, beginAtZero); - this._dataLimitsCached = true; + _getXAxisLabelAlignment() { + const {position, ticks} = this.options; + const rotation = -toRadians(this.labelRotation); + if (rotation) { + return position === 'top' ? 'left' : 'right'; } - this.beforeBuildTicks(); - this.ticks = this.buildTicks() || []; - this.afterBuildTicks(); - const samplingEnabled = sampleSize < this.ticks.length; - this._convertTicksToLabels(samplingEnabled ? sample(this.ticks, sampleSize) : this.ticks); - this.configure(); - this.beforeCalculateLabelRotation(); - this.calculateLabelRotation(); - this.afterCalculateLabelRotation(); - if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { - this.ticks = autoSkip(this, this.ticks); - this._labelSizes = null; + let align = 'center'; + if (ticks.align === 'start') { + align = 'left'; + } else if (ticks.align === 'end') { + align = 'right'; + } else if (ticks.align === 'inner') { + align = 'inner'; + } + return align; + } + _getYAxisLabelAlignment(tl) { + const {position, ticks: {crossAlign, mirror, padding}} = this.options; + const labelSizes = this._getLabelSizes(); + const tickAndPadding = tl + padding; + const widest = labelSizes.widest.width; + let textAlign; + let x; + if (position === 'left') { + if (mirror) { + x = this.right + padding; + if (crossAlign === 'near') { + textAlign = 'left'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x += (widest / 2); + } else { + textAlign = 'right'; + x += widest; + } + } else { + x = this.right - tickAndPadding; + if (crossAlign === 'near') { + textAlign = 'right'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x -= (widest / 2); + } else { + textAlign = 'left'; + x = this.left; + } + } + } else if (position === 'right') { + if (mirror) { + x = this.left + padding; + if (crossAlign === 'near') { + textAlign = 'right'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x -= (widest / 2); + } else { + textAlign = 'left'; + x -= widest; + } + } else { + x = this.left + tickAndPadding; + if (crossAlign === 'near') { + textAlign = 'left'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x += widest / 2; + } else { + textAlign = 'right'; + x = this.right; + } + } + } else { + textAlign = 'right'; } - if (samplingEnabled) { - this._convertTicksToLabels(this.ticks); + return {textAlign, x}; + } + _computeLabelArea() { + if (this.options.ticks.mirror) { + return; + } + const chart = this.chart; + const position = this.options.position; + if (position === 'left' || position === 'right') { + return {top: 0, left: this.left, bottom: chart.height, right: this.right}; + } if (position === 'top' || position === 'bottom') { + return {top: this.top, left: 0, bottom: this.bottom, right: chart.width}; } - this.beforeFit(); - this.fit(); - this.afterFit(); - this.afterUpdate(); } - configure() { - let reversePixels = this.options.reverse; - let startPixel, endPixel; - if (this.isHorizontal()) { - startPixel = this.left; - endPixel = this.right; - } else { - startPixel = this.top; - endPixel = this.bottom; - reversePixels = !reversePixels; + drawBackground() { + const {ctx, options: {backgroundColor}, left, top, width, height} = this; + if (backgroundColor) { + ctx.save(); + ctx.fillStyle = backgroundColor; + ctx.fillRect(left, top, width, height); + ctx.restore(); } - this._startPixel = startPixel; - this._endPixel = endPixel; - this._reversePixels = reversePixels; - this._length = endPixel - startPixel; - this._alignToPixels = this.options.alignToPixels; } - afterUpdate() { - callback(this.options.afterUpdate, [this]); + getLineWidthForValue(value) { + const grid = this.options.grid; + if (!this._isVisible() || !grid.display) { + return 0; + } + const ticks = this.ticks; + const index = ticks.findIndex(t => t.value === value); + if (index >= 0) { + const opts = grid.setContext(this.getContext(index)); + return opts.lineWidth; + } + return 0; } - beforeSetDimensions() { - callback(this.options.beforeSetDimensions, [this]); + drawGrid(chartArea) { + const grid = this.options.grid; + const ctx = this.ctx; + const items = this._gridLineItems || (this._gridLineItems = this._computeGridLineItems(chartArea)); + let i, ilen; + const drawLine = (p1, p2, style) => { + if (!style.width || !style.color) { + return; + } + ctx.save(); + ctx.lineWidth = style.width; + ctx.strokeStyle = style.color; + ctx.setLineDash(style.borderDash || []); + ctx.lineDashOffset = style.borderDashOffset; + ctx.beginPath(); + ctx.moveTo(p1.x, p1.y); + ctx.lineTo(p2.x, p2.y); + ctx.stroke(); + ctx.restore(); + }; + if (grid.display) { + for (i = 0, ilen = items.length; i < ilen; ++i) { + const item = items[i]; + if (grid.drawOnChartArea) { + drawLine( + {x: item.x1, y: item.y1}, + {x: item.x2, y: item.y2}, + item + ); + } + if (grid.drawTicks) { + drawLine( + {x: item.tx1, y: item.ty1}, + {x: item.tx2, y: item.ty2}, + { + color: item.tickColor, + width: item.tickWidth, + borderDash: item.tickBorderDash, + borderDashOffset: item.tickBorderDashOffset + } + ); + } + } + } } - setDimensions() { + drawBorder() { + const {chart, ctx, options: {grid}} = this; + const borderOpts = grid.setContext(this.getContext()); + const axisWidth = grid.drawBorder ? borderOpts.borderWidth : 0; + if (!axisWidth) { + return; + } + const lastLineWidth = grid.setContext(this.getContext(0)).lineWidth; + const borderValue = this._borderValue; + let x1, x2, y1, y2; if (this.isHorizontal()) { - this.width = this.maxWidth; - this.left = 0; - this.right = this.width; + x1 = _alignPixel(chart, this.left, axisWidth) - axisWidth / 2; + x2 = _alignPixel(chart, this.right, lastLineWidth) + lastLineWidth / 2; + y1 = y2 = borderValue; } else { - this.height = this.maxHeight; - this.top = 0; - this.bottom = this.height; - } - this.paddingLeft = 0; - this.paddingTop = 0; - this.paddingRight = 0; - this.paddingBottom = 0; - } - afterSetDimensions() { - callback(this.options.afterSetDimensions, [this]); - } - _callHooks(name) { - this.chart.notifyPlugins(name, this.getContext()); - callback(this.options[name], [this]); - } - beforeDataLimits() { - this._callHooks('beforeDataLimits'); - } - determineDataLimits() {} - afterDataLimits() { - this._callHooks('afterDataLimits'); - } - beforeBuildTicks() { - this._callHooks('beforeBuildTicks'); - } - buildTicks() { - return []; - } - afterBuildTicks() { - this._callHooks('afterBuildTicks'); - } - beforeTickToLabelConversion() { - callback(this.options.beforeTickToLabelConversion, [this]); - } - generateTickLabels(ticks) { - const tickOpts = this.options.ticks; - let i, ilen, tick; - for (i = 0, ilen = ticks.length; i < ilen; i++) { - tick = ticks[i]; - tick.label = callback(tickOpts.callback, [tick.value, i, ticks], this); + y1 = _alignPixel(chart, this.top, axisWidth) - axisWidth / 2; + y2 = _alignPixel(chart, this.bottom, lastLineWidth) + lastLineWidth / 2; + x1 = x2 = borderValue; } + ctx.save(); + ctx.lineWidth = borderOpts.borderWidth; + ctx.strokeStyle = borderOpts.borderColor; + ctx.beginPath(); + ctx.moveTo(x1, y1); + ctx.lineTo(x2, y2); + ctx.stroke(); + ctx.restore(); } - afterTickToLabelConversion() { - callback(this.options.afterTickToLabelConversion, [this]); - } - beforeCalculateLabelRotation() { - callback(this.options.beforeCalculateLabelRotation, [this]); - } - calculateLabelRotation() { - const options = this.options; - const tickOpts = options.ticks; - const numTicks = this.ticks.length; - const minRotation = tickOpts.minRotation || 0; - const maxRotation = tickOpts.maxRotation; - let labelRotation = minRotation; - let tickWidth, maxHeight, maxLabelDiagonal; - if (!this._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !this.isHorizontal()) { - this.labelRotation = minRotation; + drawLabels(chartArea) { + const optionTicks = this.options.ticks; + if (!optionTicks.display) { return; } - const labelSizes = this._getLabelSizes(); - const maxLabelWidth = labelSizes.widest.width; - const maxLabelHeight = labelSizes.highest.height; - const maxWidth = _limitValue(this.chart.width - maxLabelWidth, 0, this.maxWidth); - tickWidth = options.offset ? this.maxWidth / numTicks : maxWidth / (numTicks - 1); - if (maxLabelWidth + 6 > tickWidth) { - tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1)); - maxHeight = this.maxHeight - getTickMarkLength(options.grid) - - tickOpts.padding - getTitleHeight(options.title, this.chart.options.font); - maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight); - labelRotation = toDegrees(Math.min( - Math.asin(_limitValue((labelSizes.highest.height + 6) / tickWidth, -1, 1)), - Math.asin(_limitValue(maxHeight / maxLabelDiagonal, -1, 1)) - Math.asin(_limitValue(maxLabelHeight / maxLabelDiagonal, -1, 1)) - )); - labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation)); + const ctx = this.ctx; + const area = this._computeLabelArea(); + if (area) { + clipArea(ctx, area); } - this.labelRotation = labelRotation; - } - afterCalculateLabelRotation() { - callback(this.options.afterCalculateLabelRotation, [this]); - } - beforeFit() { - callback(this.options.beforeFit, [this]); - } - fit() { - const minSize = { - width: 0, - height: 0 - }; - const {chart, options: {ticks: tickOpts, title: titleOpts, grid: gridOpts}} = this; - const display = this._isVisible(); - const isHorizontal = this.isHorizontal(); - if (display) { - const titleHeight = getTitleHeight(titleOpts, chart.options.font); - if (isHorizontal) { - minSize.width = this.maxWidth; - minSize.height = getTickMarkLength(gridOpts) + titleHeight; - } else { - minSize.height = this.maxHeight; - minSize.width = getTickMarkLength(gridOpts) + titleHeight; - } - if (tickOpts.display && this.ticks.length) { - const {first, last, widest, highest} = this._getLabelSizes(); - const tickPadding = tickOpts.padding * 2; - const angleRadians = toRadians(this.labelRotation); - const cos = Math.cos(angleRadians); - const sin = Math.sin(angleRadians); - if (isHorizontal) { - const labelHeight = tickOpts.mirror ? 0 : sin * widest.width + cos * highest.height; - minSize.height = Math.min(this.maxHeight, minSize.height + labelHeight + tickPadding); - } else { - const labelWidth = tickOpts.mirror ? 0 : cos * widest.width + sin * highest.height; - minSize.width = Math.min(this.maxWidth, minSize.width + labelWidth + tickPadding); - } - this._calculatePadding(first, last, sin, cos); + const items = this._labelItems || (this._labelItems = this._computeLabelItems(chartArea)); + let i, ilen; + for (i = 0, ilen = items.length; i < ilen; ++i) { + const item = items[i]; + const tickFont = item.font; + const label = item.label; + if (item.backdrop) { + ctx.fillStyle = item.backdrop.color; + ctx.fillRect(item.backdrop.left, item.backdrop.top, item.backdrop.width, item.backdrop.height); } + let y = item.textOffset; + renderText(ctx, label, 0, y, tickFont, item); } - this._handleMargins(); - if (isHorizontal) { - this.width = this._length = chart.width - this._margins.left - this._margins.right; - this.height = minSize.height; - } else { - this.width = minSize.width; - this.height = this._length = chart.height - this._margins.top - this._margins.bottom; + if (area) { + unclipArea(ctx); } } - _calculatePadding(first, last, sin, cos) { - const {ticks: {align, padding}, position} = this.options; - const isRotated = this.labelRotation !== 0; - const labelsBelowTicks = position !== 'top' && this.axis === 'x'; - if (this.isHorizontal()) { - const offsetLeft = this.getPixelForTick(0) - this.left; - const offsetRight = this.right - this.getPixelForTick(this.ticks.length - 1); - let paddingLeft = 0; - let paddingRight = 0; - if (isRotated) { - if (labelsBelowTicks) { - paddingLeft = cos * first.width; - paddingRight = sin * last.height; - } else { - paddingLeft = sin * first.height; - paddingRight = cos * last.width; - } - } else if (align === 'start') { - paddingRight = last.width; - } else if (align === 'end') { - paddingLeft = first.width; - } else { - paddingLeft = first.width / 2; - paddingRight = last.width / 2; + drawTitle() { + const {ctx, options: {position, title, reverse}} = this; + if (!title.display) { + return; + } + const font = toFont(title.font); + const padding = toPadding(title.padding); + const align = title.align; + let offset = font.lineHeight / 2; + if (position === 'bottom' || position === 'center' || isObject(position)) { + offset += padding.bottom; + if (isArray(title.text)) { + offset += font.lineHeight * (title.text.length - 1); } - this.paddingLeft = Math.max((paddingLeft - offsetLeft + padding) * this.width / (this.width - offsetLeft), 0); - this.paddingRight = Math.max((paddingRight - offsetRight + padding) * this.width / (this.width - offsetRight), 0); } else { - let paddingTop = last.height / 2; - let paddingBottom = first.height / 2; - if (align === 'start') { - paddingTop = 0; - paddingBottom = first.height; - } else if (align === 'end') { - paddingTop = last.height; - paddingBottom = 0; - } - this.paddingTop = paddingTop + padding; - this.paddingBottom = paddingBottom + padding; + offset += padding.top; } + const {titleX, titleY, maxWidth, rotation} = titleArgs(this, offset, position, align); + renderText(ctx, title.text, 0, 0, font, { + color: title.color, + maxWidth, + rotation, + textAlign: titleAlign(align, position, reverse), + textBaseline: 'middle', + translation: [titleX, titleY], + }); } - _handleMargins() { - if (this._margins) { - this._margins.left = Math.max(this.paddingLeft, this._margins.left); - this._margins.top = Math.max(this.paddingTop, this._margins.top); - this._margins.right = Math.max(this.paddingRight, this._margins.right); - this._margins.bottom = Math.max(this.paddingBottom, this._margins.bottom); + draw(chartArea) { + if (!this._isVisible()) { + return; } + this.drawBackground(); + this.drawGrid(chartArea); + this.drawBorder(); + this.drawTitle(); + this.drawLabels(chartArea); } - afterFit() { - callback(this.options.afterFit, [this]); - } - isHorizontal() { - const {axis, position} = this.options; - return position === 'top' || position === 'bottom' || axis === 'x'; - } - isFullSize() { - return this.options.fullSize; + _layers() { + const opts = this.options; + const tz = opts.ticks && opts.ticks.z || 0; + const gz = valueOrDefault(opts.grid && opts.grid.z, -1); + if (!this._isVisible() || this.draw !== Scale.prototype.draw) { + return [{ + z: tz, + draw: (chartArea) => { + this.draw(chartArea); + } + }]; + } + return [{ + z: gz, + draw: (chartArea) => { + this.drawBackground(); + this.drawGrid(chartArea); + this.drawTitle(); + } + }, { + z: gz + 1, + draw: () => { + this.drawBorder(); + } + }, { + z: tz, + draw: (chartArea) => { + this.drawLabels(chartArea); + } + }]; } - _convertTicksToLabels(ticks) { - this.beforeTickToLabelConversion(); - this.generateTickLabels(ticks); + getMatchingVisibleMetas(type) { + const metas = this.chart.getSortedVisibleDatasetMetas(); + const axisID = this.axis + 'AxisID'; + const result = []; let i, ilen; - for (i = 0, ilen = ticks.length; i < ilen; i++) { - if (isNullOrUndef(ticks[i].label)) { - ticks.splice(i, 1); - ilen--; - i--; + for (i = 0, ilen = metas.length; i < ilen; ++i) { + const meta = metas[i]; + if (meta[axisID] === this.id && (!type || meta.type === type)) { + result.push(meta); } } - this.afterTickToLabelConversion(); + return result; } - _getLabelSizes() { - let labelSizes = this._labelSizes; - if (!labelSizes) { - const sampleSize = this.options.ticks.sampleSize; - let ticks = this.ticks; - if (sampleSize < ticks.length) { - ticks = sample(ticks, sampleSize); - } - this._labelSizes = labelSizes = this._computeLabelSizes(ticks, ticks.length); + _resolveTickFontOptions(index) { + const opts = this.options.ticks.setContext(this.getContext(index)); + return toFont(opts.font); + } + _maxDigits() { + const fontSize = this._resolveTickFontOptions(0).lineHeight; + return (this.isHorizontal() ? this.width : this.height) / fontSize; + } +} + +class TypedRegistry { + constructor(type, scope, override) { + this.type = type; + this.scope = scope; + this.override = override; + this.items = Object.create(null); + } + isForType(type) { + return Object.prototype.isPrototypeOf.call(this.type.prototype, type.prototype); + } + register(item) { + const proto = Object.getPrototypeOf(item); + let parentScope; + if (isIChartComponent(proto)) { + parentScope = this.register(proto); } - return labelSizes; + const items = this.items; + const id = item.id; + const scope = this.scope + '.' + id; + if (!id) { + throw new Error('class does not have id: ' + item); + } + if (id in items) { + return scope; + } + items[id] = item; + registerDefaults(item, scope, parentScope); + if (this.override) { + defaults.override(item.id, item.overrides); + } + return scope; } - _computeLabelSizes(ticks, length) { - const {ctx, _longestTextCache: caches} = this; - const widths = []; - const heights = []; - let widestLabelSize = 0; - let highestLabelSize = 0; - let i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel; - for (i = 0; i < length; ++i) { - label = ticks[i].label; - tickFont = this._resolveTickFontOptions(i); - ctx.font = fontString = tickFont.string; - cache = caches[fontString] = caches[fontString] || {data: {}, gc: []}; - lineHeight = tickFont.lineHeight; - width = height = 0; - if (!isNullOrUndef(label) && !isArray(label)) { - width = _measureText(ctx, cache.data, cache.gc, width, label); - height = lineHeight; - } else if (isArray(label)) { - for (j = 0, jlen = label.length; j < jlen; ++j) { - nestedLabel = label[j]; - if (!isNullOrUndef(nestedLabel) && !isArray(nestedLabel)) { - width = _measureText(ctx, cache.data, cache.gc, width, nestedLabel); - height += lineHeight; - } - } + get(id) { + return this.items[id]; + } + unregister(item) { + const items = this.items; + const id = item.id; + const scope = this.scope; + if (id in items) { + delete items[id]; + } + if (scope && id in defaults[scope]) { + delete defaults[scope][id]; + if (this.override) { + delete overrides[id]; } - widths.push(width); - heights.push(height); - widestLabelSize = Math.max(width, widestLabelSize); - highestLabelSize = Math.max(height, highestLabelSize); } - garbageCollect(caches, length); - const widest = widths.indexOf(widestLabelSize); - const highest = heights.indexOf(highestLabelSize); - const valueAt = (idx) => ({width: widths[idx] || 0, height: heights[idx] || 0}); - return { - first: valueAt(0), - last: valueAt(length - 1), - widest: valueAt(widest), - highest: valueAt(highest), - widths, - heights, - }; } - getLabelForValue(value) { - return value; +} +function registerDefaults(item, scope, parentScope) { + const itemDefaults = merge(Object.create(null), [ + parentScope ? defaults.get(parentScope) : {}, + defaults.get(scope), + item.defaults + ]); + defaults.set(scope, itemDefaults); + if (item.defaultRoutes) { + routeDefaults(scope, item.defaultRoutes); + } + if (item.descriptors) { + defaults.describe(scope, item.descriptors); } - getPixelForValue(value, index) { - return NaN; +} +function routeDefaults(scope, routes) { + Object.keys(routes).forEach(property => { + const propertyParts = property.split('.'); + const sourceName = propertyParts.pop(); + const sourceScope = [scope].concat(propertyParts).join('.'); + const parts = routes[property].split('.'); + const targetName = parts.pop(); + const targetScope = parts.join('.'); + defaults.route(sourceScope, sourceName, targetScope, targetName); + }); +} +function isIChartComponent(proto) { + return 'id' in proto && 'defaults' in proto; +} + +class Registry { + constructor() { + this.controllers = new TypedRegistry(DatasetController, 'datasets', true); + this.elements = new TypedRegistry(Element, 'elements'); + this.plugins = new TypedRegistry(Object, 'plugins'); + this.scales = new TypedRegistry(Scale, 'scales'); + this._typedRegistries = [this.controllers, this.scales, this.elements]; } - getValueForPixel(pixel) {} - getPixelForTick(index) { - const ticks = this.ticks; - if (index < 0 || index > ticks.length - 1) { - return null; - } - return this.getPixelForValue(ticks[index].value); + add(...args) { + this._each('register', args); } - getPixelForDecimal(decimal) { - if (this._reversePixels) { - decimal = 1 - decimal; - } - const pixel = this._startPixel + decimal * this._length; - return _int16Range(this._alignToPixels ? _alignPixel(this.chart, pixel, 0) : pixel); + remove(...args) { + this._each('unregister', args); } - getDecimalForPixel(pixel) { - const decimal = (pixel - this._startPixel) / this._length; - return this._reversePixels ? 1 - decimal : decimal; + addControllers(...args) { + this._each('register', args, this.controllers); } - getBasePixel() { - return this.getPixelForValue(this.getBaseValue()); + addElements(...args) { + this._each('register', args, this.elements); } - getBaseValue() { - const {min, max} = this; - return min < 0 && max < 0 ? max : - min > 0 && max > 0 ? min : - 0; + addPlugins(...args) { + this._each('register', args, this.plugins); } - getContext(index) { - const ticks = this.ticks || []; - if (index >= 0 && index < ticks.length) { - const tick = ticks[index]; - return tick.$context || - (tick.$context = createTickContext(this.getContext(), index, tick)); - } - return this.$context || - (this.$context = createScaleContext(this.chart.getContext(), this)); + addScales(...args) { + this._each('register', args, this.scales); } - _tickSize() { - const optionTicks = this.options.ticks; - const rot = toRadians(this.labelRotation); - const cos = Math.abs(Math.cos(rot)); - const sin = Math.abs(Math.sin(rot)); - const labelSizes = this._getLabelSizes(); - const padding = optionTicks.autoSkipPadding || 0; - const w = labelSizes ? labelSizes.widest.width + padding : 0; - const h = labelSizes ? labelSizes.highest.height + padding : 0; - return this.isHorizontal() - ? h * cos > w * sin ? w / cos : h / sin - : h * sin < w * cos ? h / cos : w / sin; + getController(id) { + return this._get(id, this.controllers, 'controller'); } - _isVisible() { - const display = this.options.display; - if (display !== 'auto') { - return !!display; - } - return this.getMatchingVisibleMetas().length > 0; + getElement(id) { + return this._get(id, this.elements, 'element'); } - _computeGridLineItems(chartArea) { - const axis = this.axis; - const chart = this.chart; - const options = this.options; - const {grid, position} = options; - const offset = grid.offset; - const isHorizontal = this.isHorizontal(); - const ticks = this.ticks; - const ticksLength = ticks.length + (offset ? 1 : 0); - const tl = getTickMarkLength(grid); - const items = []; - const borderOpts = grid.setContext(this.getContext()); - const axisWidth = borderOpts.drawBorder ? borderOpts.borderWidth : 0; - const axisHalfWidth = axisWidth / 2; - const alignBorderValue = function(pixel) { - return _alignPixel(chart, pixel, axisWidth); - }; - let borderValue, i, lineValue, alignedLineValue; - let tx1, ty1, tx2, ty2, x1, y1, x2, y2; - if (position === 'top') { - borderValue = alignBorderValue(this.bottom); - ty1 = this.bottom - tl; - ty2 = borderValue - axisHalfWidth; - y1 = alignBorderValue(chartArea.top) + axisHalfWidth; - y2 = chartArea.bottom; - } else if (position === 'bottom') { - borderValue = alignBorderValue(this.top); - y1 = chartArea.top; - y2 = alignBorderValue(chartArea.bottom) - axisHalfWidth; - ty1 = borderValue + axisHalfWidth; - ty2 = this.top + tl; - } else if (position === 'left') { - borderValue = alignBorderValue(this.right); - tx1 = this.right - tl; - tx2 = borderValue - axisHalfWidth; - x1 = alignBorderValue(chartArea.left) + axisHalfWidth; - x2 = chartArea.right; - } else if (position === 'right') { - borderValue = alignBorderValue(this.left); - x1 = chartArea.left; - x2 = alignBorderValue(chartArea.right) - axisHalfWidth; - tx1 = borderValue + axisHalfWidth; - tx2 = this.left + tl; - } else if (axis === 'x') { - if (position === 'center') { - borderValue = alignBorderValue((chartArea.top + chartArea.bottom) / 2 + 0.5); - } else if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); + getPlugin(id) { + return this._get(id, this.plugins, 'plugin'); + } + getScale(id) { + return this._get(id, this.scales, 'scale'); + } + removeControllers(...args) { + this._each('unregister', args, this.controllers); + } + removeElements(...args) { + this._each('unregister', args, this.elements); + } + removePlugins(...args) { + this._each('unregister', args, this.plugins); + } + removeScales(...args) { + this._each('unregister', args, this.scales); + } + _each(method, args, typedRegistry) { + [...args].forEach(arg => { + const reg = typedRegistry || this._getRegistryForType(arg); + if (typedRegistry || reg.isForType(arg) || (reg === this.plugins && arg.id)) { + this._exec(method, reg, arg); + } else { + each(arg, item => { + const itemReg = typedRegistry || this._getRegistryForType(item); + this._exec(method, itemReg, item); + }); } - y1 = chartArea.top; - y2 = chartArea.bottom; - ty1 = borderValue + axisHalfWidth; - ty2 = ty1 + tl; - } else if (axis === 'y') { - if (position === 'center') { - borderValue = alignBorderValue((chartArea.left + chartArea.right) / 2); - } else if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); + }); + } + _exec(method, registry, component) { + const camelMethod = _capitalize(method); + callback(component['before' + camelMethod], [], component); + registry[method](component); + callback(component['after' + camelMethod], [], component); + } + _getRegistryForType(type) { + for (let i = 0; i < this._typedRegistries.length; i++) { + const reg = this._typedRegistries[i]; + if (reg.isForType(type)) { + return reg; } - tx1 = borderValue - axisHalfWidth; - tx2 = tx1 - tl; - x1 = chartArea.left; - x2 = chartArea.right; } - const limit = valueOrDefault(options.ticks.maxTicksLimit, ticksLength); - const step = Math.max(1, Math.ceil(ticksLength / limit)); - for (i = 0; i < ticksLength; i += step) { - const optsAtIndex = grid.setContext(this.getContext(i)); - const lineWidth = optsAtIndex.lineWidth; - const lineColor = optsAtIndex.color; - const borderDash = grid.borderDash || []; - const borderDashOffset = optsAtIndex.borderDashOffset; - const tickWidth = optsAtIndex.tickWidth; - const tickColor = optsAtIndex.tickColor; - const tickBorderDash = optsAtIndex.tickBorderDash || []; - const tickBorderDashOffset = optsAtIndex.tickBorderDashOffset; - lineValue = getPixelForGridLine(this, i, offset); - if (lineValue === undefined) { - continue; - } - alignedLineValue = _alignPixel(chart, lineValue, lineWidth); - if (isHorizontal) { - tx1 = tx2 = x1 = x2 = alignedLineValue; - } else { - ty1 = ty2 = y1 = y2 = alignedLineValue; - } - items.push({ - tx1, - ty1, - tx2, - ty2, - x1, - y1, - x2, - y2, - width: lineWidth, - color: lineColor, - borderDash, - borderDashOffset, - tickWidth, - tickColor, - tickBorderDash, - tickBorderDashOffset, - }); + return this.plugins; + } + _get(id, typedRegistry, type) { + const item = typedRegistry.get(id); + if (item === undefined) { + throw new Error('"' + id + '" is not a registered ' + type + '.'); + } + return item; + } +} +var registry = new Registry(); + +class ScatterController extends DatasetController { + update(mode) { + const meta = this._cachedMeta; + const {data: points = []} = meta; + const animationsDisabled = this.chart._animationsDisabled; + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + this._drawStart = start; + this._drawCount = count; + if (_scaleRangesChanged(meta)) { + start = 0; + count = points.length; } - this._ticksLength = ticksLength; - this._borderValue = borderValue; - return items; + if (this.options.showLine) { + const {dataset: line, _dataset} = meta; + line._chart = this.chart; + line._datasetIndex = this.index; + line._decimated = !!_dataset._decimated; + line.points = points; + const options = this.resolveDatasetElementOptions(mode); + options.segment = this.options.segment; + this.updateElement(line, undefined, { + animated: !animationsDisabled, + options + }, mode); + } + this.updateElements(points, start, count, mode); } - _computeLabelItems(chartArea) { - const axis = this.axis; - const options = this.options; - const {position, ticks: optionTicks} = options; - const isHorizontal = this.isHorizontal(); - const ticks = this.ticks; - const {align, crossAlign, padding, mirror} = optionTicks; - const tl = getTickMarkLength(options.grid); - const tickAndPadding = tl + padding; - const hTickAndPadding = mirror ? -padding : tickAndPadding; - const rotation = -toRadians(this.labelRotation); - const items = []; - let i, ilen, tick, label, x, y, textAlign, pixel, font, lineHeight, lineCount, textOffset; - let textBaseline = 'middle'; - if (position === 'top') { - y = this.bottom - hTickAndPadding; - textAlign = this._getXAxisLabelAlignment(); - } else if (position === 'bottom') { - y = this.top + hTickAndPadding; - textAlign = this._getXAxisLabelAlignment(); - } else if (position === 'left') { - const ret = this._getYAxisLabelAlignment(tl); - textAlign = ret.textAlign; - x = ret.x; - } else if (position === 'right') { - const ret = this._getYAxisLabelAlignment(tl); - textAlign = ret.textAlign; - x = ret.x; - } else if (axis === 'x') { - if (position === 'center') { - y = ((chartArea.top + chartArea.bottom) / 2) + tickAndPadding; - } else if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - y = this.chart.scales[positionAxisID].getPixelForValue(value) + tickAndPadding; + addElements() { + const {showLine} = this.options; + if (!this.datasetElementType && showLine) { + this.datasetElementType = registry.getElement('line'); + } + super.addElements(); + } + updateElements(points, start, count, mode) { + const reset = mode === 'reset'; + const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; + const firstOpts = this.resolveDataElementOptions(start, mode); + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions); + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const {spanGaps, segment} = this.options; + const maxGapLength = isNumber(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY; + const directUpdate = this.chart._animationsDisabled || reset || mode === 'none'; + let prevParsed = start > 0 && this.getParsed(start - 1); + for (let i = start; i < start + count; ++i) { + const point = points[i]; + const parsed = this.getParsed(i); + const properties = directUpdate ? point : {}; + const nullData = isNullOrUndef(parsed[vAxis]); + const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); + const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); + properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; + if (segment) { + properties.parsed = parsed; + properties.raw = _dataset.data[i]; } - textAlign = this._getXAxisLabelAlignment(); - } else if (axis === 'y') { - if (position === 'center') { - x = ((chartArea.left + chartArea.right) / 2) - tickAndPadding; - } else if (isObject(position)) { - const positionAxisID = Object.keys(position)[0]; - const value = position[positionAxisID]; - x = this.chart.scales[positionAxisID].getPixelForValue(value); + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); } - textAlign = this._getYAxisLabelAlignment(tl).textAlign; + if (!directUpdate) { + this.updateElement(point, i, properties, mode); + } + prevParsed = parsed; } - if (axis === 'y') { - if (align === 'start') { - textBaseline = 'top'; - } else if (align === 'end') { - textBaseline = 'bottom'; + this.updateSharedOptions(sharedOptions, mode, firstOpts); + } + getMaxOverflow() { + const meta = this._cachedMeta; + const data = meta.data || []; + if (!this.options.showLine) { + let max = 0; + for (let i = data.length - 1; i >= 0; --i) { + max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2); } + return max > 0 && max; } - const labelSizes = this._getLabelSizes(); - for (i = 0, ilen = ticks.length; i < ilen; ++i) { - tick = ticks[i]; - label = tick.label; - const optsAtIndex = optionTicks.setContext(this.getContext(i)); - pixel = this.getPixelForTick(i) + optionTicks.labelOffset; - font = this._resolveTickFontOptions(i); - lineHeight = font.lineHeight; - lineCount = isArray(label) ? label.length : 1; - const halfCount = lineCount / 2; - const color = optsAtIndex.color; - const strokeColor = optsAtIndex.textStrokeColor; - const strokeWidth = optsAtIndex.textStrokeWidth; - if (isHorizontal) { - x = pixel; - if (position === 'top') { - if (crossAlign === 'near' || rotation !== 0) { - textOffset = -lineCount * lineHeight + lineHeight / 2; - } else if (crossAlign === 'center') { - textOffset = -labelSizes.highest.height / 2 - halfCount * lineHeight + lineHeight; - } else { - textOffset = -labelSizes.highest.height + lineHeight / 2; - } - } else { - if (crossAlign === 'near' || rotation !== 0) { - textOffset = lineHeight / 2; - } else if (crossAlign === 'center') { - textOffset = labelSizes.highest.height / 2 - halfCount * lineHeight; - } else { - textOffset = labelSizes.highest.height - lineCount * lineHeight; - } - } - if (mirror) { - textOffset *= -1; + const dataset = meta.dataset; + const border = dataset.options && dataset.options.borderWidth || 0; + if (!data.length) { + return border; + } + const firstPoint = data[0].size(this.resolveDataElementOptions(0)); + const lastPoint = data[data.length - 1].size(this.resolveDataElementOptions(data.length - 1)); + return Math.max(border, firstPoint, lastPoint) / 2; + } +} +ScatterController.id = 'scatter'; +ScatterController.defaults = { + datasetElementType: false, + dataElementType: 'point', + showLine: false, + fill: false +}; +ScatterController.overrides = { + interaction: { + mode: 'point' + }, + plugins: { + tooltip: { + callbacks: { + title() { + return ''; + }, + label(item) { + return '(' + item.label + ', ' + item.formattedValue + ')'; } - } else { - y = pixel; - textOffset = (1 - lineCount) * lineHeight / 2; } - let backdrop; - if (optsAtIndex.showLabelBackdrop) { - const labelPadding = toPadding(optsAtIndex.backdropPadding); - const height = labelSizes.heights[i]; - const width = labelSizes.widths[i]; - let top = y + textOffset - labelPadding.top; - let left = x - labelPadding.left; - switch (textBaseline) { - case 'middle': - top -= height / 2; - break; - case 'bottom': - top -= height; - break; - } - switch (textAlign) { - case 'center': - left -= width / 2; - break; - case 'right': - left -= width; - break; - } - backdrop = { - left, - top, - width: width + labelPadding.width, - height: height + labelPadding.height, - color: optsAtIndex.backdropColor, - }; + } + }, + scales: { + x: { + type: 'linear' + }, + y: { + type: 'linear' + } + } +}; + +var controllers = /*#__PURE__*/Object.freeze({ +__proto__: null, +BarController: BarController, +BubbleController: BubbleController, +DoughnutController: DoughnutController, +LineController: LineController, +PolarAreaController: PolarAreaController, +PieController: PieController, +RadarController: RadarController, +ScatterController: ScatterController +}); + +function abstract() { + throw new Error('This method is not implemented: Check that a complete date adapter is provided.'); +} +class DateAdapter { + constructor(options) { + this.options = options || {}; + } + init(chartOptions) {} + formats() { + return abstract(); + } + parse(value, format) { + return abstract(); + } + format(timestamp, format) { + return abstract(); + } + add(timestamp, amount, unit) { + return abstract(); + } + diff(a, b, unit) { + return abstract(); + } + startOf(timestamp, unit, weekday) { + return abstract(); + } + endOf(timestamp, unit) { + return abstract(); + } +} +DateAdapter.override = function(members) { + Object.assign(DateAdapter.prototype, members); +}; +var adapters = { + _date: DateAdapter +}; + +function binarySearch(metaset, axis, value, intersect) { + const {controller, data, _sorted} = metaset; + const iScale = controller._cachedMeta.iScale; + if (iScale && axis === iScale.axis && axis !== 'r' && _sorted && data.length) { + const lookupMethod = iScale._reversePixels ? _rlookupByKey : _lookupByKey; + if (!intersect) { + return lookupMethod(data, axis, value); + } else if (controller._sharedOptions) { + const el = data[0]; + const range = typeof el.getRange === 'function' && el.getRange(axis); + if (range) { + const start = lookupMethod(data, axis, value - range); + const end = lookupMethod(data, axis, value + range); + return {lo: start.lo, hi: end.hi}; + } + } + } + return {lo: 0, hi: data.length - 1}; +} +function evaluateInteractionItems(chart, axis, position, handler, intersect) { + const metasets = chart.getSortedVisibleDatasetMetas(); + const value = position[axis]; + for (let i = 0, ilen = metasets.length; i < ilen; ++i) { + const {index, data} = metasets[i]; + const {lo, hi} = binarySearch(metasets[i], axis, value, intersect); + for (let j = lo; j <= hi; ++j) { + const element = data[j]; + if (!element.skip) { + handler(element, index, j); } - items.push({ - rotation, - label, - font, - color, - strokeColor, - strokeWidth, - textOffset, - textAlign, - textBaseline, - translation: [x, y], - backdrop, - }); } + } +} +function getDistanceMetricForAxis(axis) { + const useX = axis.indexOf('x') !== -1; + const useY = axis.indexOf('y') !== -1; + return function(pt1, pt2) { + const deltaX = useX ? Math.abs(pt1.x - pt2.x) : 0; + const deltaY = useY ? Math.abs(pt1.y - pt2.y) : 0; + return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); + }; +} +function getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) { + const items = []; + if (!includeInvisible && !chart.isPointInArea(position)) { return items; } - _getXAxisLabelAlignment() { - const {position, ticks} = this.options; - const rotation = -toRadians(this.labelRotation); - if (rotation) { - return position === 'top' ? 'left' : 'right'; + const evaluationFunc = function(element, datasetIndex, index) { + if (!includeInvisible && !_isPointInArea(element, chart.chartArea, 0)) { + return; } - let align = 'center'; - if (ticks.align === 'start') { - align = 'left'; - } else if (ticks.align === 'end') { - align = 'right'; + if (element.inRange(position.x, position.y, useFinalPosition)) { + items.push({element, datasetIndex, index}); } - return align; - } - _getYAxisLabelAlignment(tl) { - const {position, ticks: {crossAlign, mirror, padding}} = this.options; - const labelSizes = this._getLabelSizes(); - const tickAndPadding = tl + padding; - const widest = labelSizes.widest.width; - let textAlign; - let x; - if (position === 'left') { - if (mirror) { - x = this.right + padding; - if (crossAlign === 'near') { - textAlign = 'left'; - } else if (crossAlign === 'center') { - textAlign = 'center'; - x += (widest / 2); - } else { - textAlign = 'right'; - x += widest; - } - } else { - x = this.right - tickAndPadding; - if (crossAlign === 'near') { - textAlign = 'right'; - } else if (crossAlign === 'center') { - textAlign = 'center'; - x -= (widest / 2); - } else { - textAlign = 'left'; - x = this.left; - } - } - } else if (position === 'right') { - if (mirror) { - x = this.left + padding; - if (crossAlign === 'near') { - textAlign = 'right'; - } else if (crossAlign === 'center') { - textAlign = 'center'; - x -= (widest / 2); - } else { - textAlign = 'left'; - x -= widest; - } - } else { - x = this.left + tickAndPadding; - if (crossAlign === 'near') { - textAlign = 'left'; - } else if (crossAlign === 'center') { - textAlign = 'center'; - x += widest / 2; - } else { - textAlign = 'right'; - x = this.right; - } - } - } else { - textAlign = 'right'; + }; + evaluateInteractionItems(chart, axis, position, evaluationFunc, true); + return items; +} +function getNearestRadialItems(chart, position, axis, useFinalPosition) { + let items = []; + function evaluationFunc(element, datasetIndex, index) { + const {startAngle, endAngle} = element.getProps(['startAngle', 'endAngle'], useFinalPosition); + const {angle} = getAngleFromPoint(element, {x: position.x, y: position.y}); + if (_angleBetween(angle, startAngle, endAngle)) { + items.push({element, datasetIndex, index}); } - return {textAlign, x}; } - _computeLabelArea() { - if (this.options.ticks.mirror) { + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + let items = []; + const distanceMetric = getDistanceMetricForAxis(axis); + let minDistance = Number.POSITIVE_INFINITY; + function evaluationFunc(element, datasetIndex, index) { + const inRange = element.inRange(position.x, position.y, useFinalPosition); + if (intersect && !inRange) { return; } - const chart = this.chart; - const position = this.options.position; - if (position === 'left' || position === 'right') { - return {top: 0, left: this.left, bottom: chart.height, right: this.right}; - } if (position === 'top' || position === 'bottom') { - return {top: this.top, left: 0, bottom: this.bottom, right: chart.width}; + const center = element.getCenterPoint(useFinalPosition); + const pointInArea = !!includeInvisible || chart.isPointInArea(center); + if (!pointInArea && !inRange) { + return; } - } - drawBackground() { - const {ctx, options: {backgroundColor}, left, top, width, height} = this; - if (backgroundColor) { - ctx.save(); - ctx.fillStyle = backgroundColor; - ctx.fillRect(left, top, width, height); - ctx.restore(); + const distance = distanceMetric(position, center); + if (distance < minDistance) { + items = [{element, datasetIndex, index}]; + minDistance = distance; + } else if (distance === minDistance) { + items.push({element, datasetIndex, index}); } } - getLineWidthForValue(value) { - const grid = this.options.grid; - if (!this._isVisible() || !grid.display) { - return 0; - } - const ticks = this.ticks; - const index = ticks.findIndex(t => t.value === value); - if (index >= 0) { - const opts = grid.setContext(this.getContext(index)); - return opts.lineWidth; + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + if (!includeInvisible && !chart.isPointInArea(position)) { + return []; + } + return axis === 'r' && !intersect + ? getNearestRadialItems(chart, position, axis, useFinalPosition) + : getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible); +} +function getAxisItems(chart, position, axis, intersect, useFinalPosition) { + const items = []; + const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; + let intersectsItem = false; + evaluateInteractionItems(chart, axis, position, (element, datasetIndex, index) => { + if (element[rangeMethod](position[axis], useFinalPosition)) { + items.push({element, datasetIndex, index}); + intersectsItem = intersectsItem || element.inRange(position.x, position.y, useFinalPosition); } - return 0; + }); + if (intersect && !intersectsItem) { + return []; } - drawGrid(chartArea) { - const grid = this.options.grid; - const ctx = this.ctx; - const items = this._gridLineItems || (this._gridLineItems = this._computeGridLineItems(chartArea)); - let i, ilen; - const drawLine = (p1, p2, style) => { - if (!style.width || !style.color) { - return; + return items; +} +var Interaction = { + evaluateInteractionItems, + modes: { + index(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'x'; + const includeInvisible = options.includeInvisible || false; + const items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) + : getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + const elements = []; + if (!items.length) { + return []; } - ctx.save(); - ctx.lineWidth = style.width; - ctx.strokeStyle = style.color; - ctx.setLineDash(style.borderDash || []); - ctx.lineDashOffset = style.borderDashOffset; - ctx.beginPath(); - ctx.moveTo(p1.x, p1.y); - ctx.lineTo(p2.x, p2.y); - ctx.stroke(); - ctx.restore(); - }; - if (grid.display) { - for (i = 0, ilen = items.length; i < ilen; ++i) { - const item = items[i]; - if (grid.drawOnChartArea) { - drawLine( - {x: item.x1, y: item.y1}, - {x: item.x2, y: item.y2}, - item - ); + chart.getSortedVisibleDatasetMetas().forEach((meta) => { + const index = items[0].index; + const element = meta.data[index]; + if (element && !element.skip) { + elements.push({element, datasetIndex: meta.index, index}); } - if (grid.drawTicks) { - drawLine( - {x: item.tx1, y: item.ty1}, - {x: item.tx2, y: item.ty2}, - { - color: item.tickColor, - width: item.tickWidth, - borderDash: item.tickBorderDash, - borderDashOffset: item.tickBorderDashOffset - } - ); + }); + return elements; + }, + dataset(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + let items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) : + getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + if (items.length > 0) { + const datasetIndex = items[0].datasetIndex; + const data = chart.getDatasetMeta(datasetIndex).data; + items = []; + for (let i = 0; i < data.length; ++i) { + items.push({element: data[i], datasetIndex, index: i}); } } + return items; + }, + point(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible); + }, + nearest(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getNearestItems(chart, position, axis, options.intersect, useFinalPosition, includeInvisible); + }, + x(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'x', options.intersect, useFinalPosition); + }, + y(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'y', options.intersect, useFinalPosition); } } - drawBorder() { - const {chart, ctx, options: {grid}} = this; - const borderOpts = grid.setContext(this.getContext()); - const axisWidth = grid.drawBorder ? borderOpts.borderWidth : 0; - if (!axisWidth) { - return; +}; + +const STATIC_POSITIONS = ['left', 'top', 'right', 'bottom']; +function filterByPosition(array, position) { + return array.filter(v => v.pos === position); +} +function filterDynamicPositionByAxis(array, axis) { + return array.filter(v => STATIC_POSITIONS.indexOf(v.pos) === -1 && v.box.axis === axis); +} +function sortByWeight(array, reverse) { + return array.sort((a, b) => { + const v0 = reverse ? b : a; + const v1 = reverse ? a : b; + return v0.weight === v1.weight ? + v0.index - v1.index : + v0.weight - v1.weight; + }); +} +function wrapBoxes(boxes) { + const layoutBoxes = []; + let i, ilen, box, pos, stack, stackWeight; + for (i = 0, ilen = (boxes || []).length; i < ilen; ++i) { + box = boxes[i]; + ({position: pos, options: {stack, stackWeight = 1}} = box); + layoutBoxes.push({ + index: i, + box, + pos, + horizontal: box.isHorizontal(), + weight: box.weight, + stack: stack && (pos + stack), + stackWeight + }); + } + return layoutBoxes; +} +function buildStacks(layouts) { + const stacks = {}; + for (const wrap of layouts) { + const {stack, pos, stackWeight} = wrap; + if (!stack || !STATIC_POSITIONS.includes(pos)) { + continue; } - const lastLineWidth = grid.setContext(this.getContext(0)).lineWidth; - const borderValue = this._borderValue; - let x1, x2, y1, y2; - if (this.isHorizontal()) { - x1 = _alignPixel(chart, this.left, axisWidth) - axisWidth / 2; - x2 = _alignPixel(chart, this.right, lastLineWidth) + lastLineWidth / 2; - y1 = y2 = borderValue; + const _stack = stacks[stack] || (stacks[stack] = {count: 0, placed: 0, weight: 0, size: 0}); + _stack.count++; + _stack.weight += stackWeight; + } + return stacks; +} +function setLayoutDims(layouts, params) { + const stacks = buildStacks(layouts); + const {vBoxMaxWidth, hBoxMaxHeight} = params; + let i, ilen, layout; + for (i = 0, ilen = layouts.length; i < ilen; ++i) { + layout = layouts[i]; + const {fullSize} = layout.box; + const stack = stacks[layout.stack]; + const factor = stack && layout.stackWeight / stack.weight; + if (layout.horizontal) { + layout.width = factor ? factor * vBoxMaxWidth : fullSize && params.availableWidth; + layout.height = hBoxMaxHeight; } else { - y1 = _alignPixel(chart, this.top, axisWidth) - axisWidth / 2; - y2 = _alignPixel(chart, this.bottom, lastLineWidth) + lastLineWidth / 2; - x1 = x2 = borderValue; + layout.width = vBoxMaxWidth; + layout.height = factor ? factor * hBoxMaxHeight : fullSize && params.availableHeight; } - ctx.save(); - ctx.lineWidth = borderOpts.borderWidth; - ctx.strokeStyle = borderOpts.borderColor; - ctx.beginPath(); - ctx.moveTo(x1, y1); - ctx.lineTo(x2, y2); - ctx.stroke(); - ctx.restore(); } - drawLabels(chartArea) { - const optionTicks = this.options.ticks; - if (!optionTicks.display) { - return; - } - const ctx = this.ctx; - const area = this._computeLabelArea(); - if (area) { - clipArea(ctx, area); - } - const items = this._labelItems || (this._labelItems = this._computeLabelItems(chartArea)); - let i, ilen; - for (i = 0, ilen = items.length; i < ilen; ++i) { - const item = items[i]; - const tickFont = item.font; - const label = item.label; - if (item.backdrop) { - ctx.fillStyle = item.backdrop.color; - ctx.fillRect(item.backdrop.left, item.backdrop.top, item.backdrop.width, item.backdrop.height); - } - let y = item.textOffset; - renderText(ctx, label, 0, y, tickFont, item); - } - if (area) { - unclipArea(ctx); + return stacks; +} +function buildLayoutBoxes(boxes) { + const layoutBoxes = wrapBoxes(boxes); + const fullSize = sortByWeight(layoutBoxes.filter(wrap => wrap.box.fullSize), true); + const left = sortByWeight(filterByPosition(layoutBoxes, 'left'), true); + const right = sortByWeight(filterByPosition(layoutBoxes, 'right')); + const top = sortByWeight(filterByPosition(layoutBoxes, 'top'), true); + const bottom = sortByWeight(filterByPosition(layoutBoxes, 'bottom')); + const centerHorizontal = filterDynamicPositionByAxis(layoutBoxes, 'x'); + const centerVertical = filterDynamicPositionByAxis(layoutBoxes, 'y'); + return { + fullSize, + leftAndTop: left.concat(top), + rightAndBottom: right.concat(centerVertical).concat(bottom).concat(centerHorizontal), + chartArea: filterByPosition(layoutBoxes, 'chartArea'), + vertical: left.concat(right).concat(centerVertical), + horizontal: top.concat(bottom).concat(centerHorizontal) + }; +} +function getCombinedMax(maxPadding, chartArea, a, b) { + return Math.max(maxPadding[a], chartArea[a]) + Math.max(maxPadding[b], chartArea[b]); +} +function updateMaxPadding(maxPadding, boxPadding) { + maxPadding.top = Math.max(maxPadding.top, boxPadding.top); + maxPadding.left = Math.max(maxPadding.left, boxPadding.left); + maxPadding.bottom = Math.max(maxPadding.bottom, boxPadding.bottom); + maxPadding.right = Math.max(maxPadding.right, boxPadding.right); +} +function updateDims(chartArea, params, layout, stacks) { + const {pos, box} = layout; + const maxPadding = chartArea.maxPadding; + if (!isObject(pos)) { + if (layout.size) { + chartArea[pos] -= layout.size; } + const stack = stacks[layout.stack] || {size: 0, count: 1}; + stack.size = Math.max(stack.size, layout.horizontal ? box.height : box.width); + layout.size = stack.size / stack.count; + chartArea[pos] += layout.size; } - drawTitle() { - const {ctx, options: {position, title, reverse}} = this; - if (!title.display) { - return; + if (box.getPadding) { + updateMaxPadding(maxPadding, box.getPadding()); + } + const newWidth = Math.max(0, params.outerWidth - getCombinedMax(maxPadding, chartArea, 'left', 'right')); + const newHeight = Math.max(0, params.outerHeight - getCombinedMax(maxPadding, chartArea, 'top', 'bottom')); + const widthChanged = newWidth !== chartArea.w; + const heightChanged = newHeight !== chartArea.h; + chartArea.w = newWidth; + chartArea.h = newHeight; + return layout.horizontal + ? {same: widthChanged, other: heightChanged} + : {same: heightChanged, other: widthChanged}; +} +function handleMaxPadding(chartArea) { + const maxPadding = chartArea.maxPadding; + function updatePos(pos) { + const change = Math.max(maxPadding[pos] - chartArea[pos], 0); + chartArea[pos] += change; + return change; + } + chartArea.y += updatePos('top'); + chartArea.x += updatePos('left'); + updatePos('right'); + updatePos('bottom'); +} +function getMargins(horizontal, chartArea) { + const maxPadding = chartArea.maxPadding; + function marginForPositions(positions) { + const margin = {left: 0, top: 0, right: 0, bottom: 0}; + positions.forEach((pos) => { + margin[pos] = Math.max(chartArea[pos], maxPadding[pos]); + }); + return margin; + } + return horizontal + ? marginForPositions(['left', 'right']) + : marginForPositions(['top', 'bottom']); +} +function fitBoxes(boxes, chartArea, params, stacks) { + const refitBoxes = []; + let i, ilen, layout, box, refit, changed; + for (i = 0, ilen = boxes.length, refit = 0; i < ilen; ++i) { + layout = boxes[i]; + box = layout.box; + box.update( + layout.width || chartArea.w, + layout.height || chartArea.h, + getMargins(layout.horizontal, chartArea) + ); + const {same, other} = updateDims(chartArea, params, layout, stacks); + refit |= same && refitBoxes.length; + changed = changed || other; + if (!box.fullSize) { + refitBoxes.push(layout); } - const font = toFont(title.font); - const padding = toPadding(title.padding); - const align = title.align; - let offset = font.lineHeight / 2; - if (position === 'bottom' || position === 'center' || isObject(position)) { - offset += padding.bottom; - if (isArray(title.text)) { - offset += font.lineHeight * (title.text.length - 1); + } + return refit && fitBoxes(refitBoxes, chartArea, params, stacks) || changed; +} +function setBoxDims(box, left, top, width, height) { + box.top = top; + box.left = left; + box.right = left + width; + box.bottom = top + height; + box.width = width; + box.height = height; +} +function placeBoxes(boxes, chartArea, params, stacks) { + const userPadding = params.padding; + let {x, y} = chartArea; + for (const layout of boxes) { + const box = layout.box; + const stack = stacks[layout.stack] || {count: 1, placed: 0, weight: 1}; + const weight = (layout.stackWeight / stack.weight) || 1; + if (layout.horizontal) { + const width = chartArea.w * weight; + const height = stack.size || box.height; + if (defined(stack.start)) { + y = stack.start; + } + if (box.fullSize) { + setBoxDims(box, userPadding.left, y, params.outerWidth - userPadding.right - userPadding.left, height); + } else { + setBoxDims(box, chartArea.left + stack.placed, y, width, height); } + stack.start = y; + stack.placed += width; + y = box.bottom; } else { - offset += padding.top; + const height = chartArea.h * weight; + const width = stack.size || box.width; + if (defined(stack.start)) { + x = stack.start; + } + if (box.fullSize) { + setBoxDims(box, x, userPadding.top, width, params.outerHeight - userPadding.bottom - userPadding.top); + } else { + setBoxDims(box, x, chartArea.top + stack.placed, width, height); + } + stack.start = x; + stack.placed += height; + x = box.right; } - const {titleX, titleY, maxWidth, rotation} = titleArgs(this, offset, position, align); - renderText(ctx, title.text, 0, 0, font, { - color: title.color, - maxWidth, - rotation, - textAlign: titleAlign(align, position, reverse), - textBaseline: 'middle', - translation: [titleX, titleY], - }); } - draw(chartArea) { - if (!this._isVisible()) { - return; - } - this.drawBackground(); - this.drawGrid(chartArea); - this.drawBorder(); - this.drawTitle(); - this.drawLabels(chartArea); + chartArea.x = x; + chartArea.y = y; +} +defaults.set('layout', { + autoPadding: true, + padding: { + top: 0, + right: 0, + bottom: 0, + left: 0 } - _layers() { - const opts = this.options; - const tz = opts.ticks && opts.ticks.z || 0; - const gz = valueOrDefault(opts.grid && opts.grid.z, -1); - if (!this._isVisible() || this.draw !== Scale.prototype.draw) { +}); +var layouts = { + addBox(chart, item) { + if (!chart.boxes) { + chart.boxes = []; + } + item.fullSize = item.fullSize || false; + item.position = item.position || 'top'; + item.weight = item.weight || 0; + item._layers = item._layers || function() { return [{ - z: tz, - draw: (chartArea) => { - this.draw(chartArea); + z: 0, + draw(chartArea) { + item.draw(chartArea); } }]; + }; + chart.boxes.push(item); + }, + removeBox(chart, layoutItem) { + const index = chart.boxes ? chart.boxes.indexOf(layoutItem) : -1; + if (index !== -1) { + chart.boxes.splice(index, 1); } - return [{ - z: gz, - draw: (chartArea) => { - this.drawBackground(); - this.drawGrid(chartArea); - this.drawTitle(); - } - }, { - z: gz + 1, - draw: () => { - this.drawBorder(); - } - }, { - z: tz, - draw: (chartArea) => { - this.drawLabels(chartArea); - } - }]; - } - getMatchingVisibleMetas(type) { - const metas = this.chart.getSortedVisibleDatasetMetas(); - const axisID = this.axis + 'AxisID'; - const result = []; - let i, ilen; - for (i = 0, ilen = metas.length; i < ilen; ++i) { - const meta = metas[i]; - if (meta[axisID] === this.id && (!type || meta.type === type)) { - result.push(meta); + }, + configure(chart, item, options) { + item.fullSize = options.fullSize; + item.position = options.position; + item.weight = options.weight; + }, + update(chart, width, height, minPadding) { + if (!chart) { + return; + } + const padding = toPadding(chart.options.layout.padding); + const availableWidth = Math.max(width - padding.width, 0); + const availableHeight = Math.max(height - padding.height, 0); + const boxes = buildLayoutBoxes(chart.boxes); + const verticalBoxes = boxes.vertical; + const horizontalBoxes = boxes.horizontal; + each(chart.boxes, box => { + if (typeof box.beforeLayout === 'function') { + box.beforeLayout(); } + }); + const visibleVerticalBoxCount = verticalBoxes.reduce((total, wrap) => + wrap.box.options && wrap.box.options.display === false ? total : total + 1, 0) || 1; + const params = Object.freeze({ + outerWidth: width, + outerHeight: height, + padding, + availableWidth, + availableHeight, + vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount, + hBoxMaxHeight: availableHeight / 2 + }); + const maxPadding = Object.assign({}, padding); + updateMaxPadding(maxPadding, toPadding(minPadding)); + const chartArea = Object.assign({ + maxPadding, + w: availableWidth, + h: availableHeight, + x: padding.left, + y: padding.top + }, padding); + const stacks = setLayoutDims(verticalBoxes.concat(horizontalBoxes), params); + fitBoxes(boxes.fullSize, chartArea, params, stacks); + fitBoxes(verticalBoxes, chartArea, params, stacks); + if (fitBoxes(horizontalBoxes, chartArea, params, stacks)) { + fitBoxes(verticalBoxes, chartArea, params, stacks); } - return result; + handleMaxPadding(chartArea); + placeBoxes(boxes.leftAndTop, chartArea, params, stacks); + chartArea.x += chartArea.w; + chartArea.y += chartArea.h; + placeBoxes(boxes.rightAndBottom, chartArea, params, stacks); + chart.chartArea = { + left: chartArea.left, + top: chartArea.top, + right: chartArea.left + chartArea.w, + bottom: chartArea.top + chartArea.h, + height: chartArea.h, + width: chartArea.w, + }; + each(boxes.chartArea, (layout) => { + const box = layout.box; + Object.assign(box, chart.chartArea); + box.update(chartArea.w, chartArea.h, {left: 0, top: 0, right: 0, bottom: 0}); + }); } - _resolveTickFontOptions(index) { - const opts = this.options.ticks.setContext(this.getContext(index)); - return toFont(opts.font); +}; + +class BasePlatform { + acquireContext(canvas, aspectRatio) {} + releaseContext(context) { + return false; } - _maxDigits() { - const fontSize = this._resolveTickFontOptions(0).lineHeight; - return (this.isHorizontal() ? this.width : this.height) / fontSize; + addEventListener(chart, type, listener) {} + removeEventListener(chart, type, listener) {} + getDevicePixelRatio() { + return 1; + } + getMaximumSize(element, width, height, aspectRatio) { + width = Math.max(0, width || element.width); + height = height || element.height; + return { + width, + height: Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height) + }; + } + isAttached(canvas) { + return true; + } + updateConfig(config) { } } -class TypedRegistry { - constructor(type, scope, override) { - this.type = type; - this.scope = scope; - this.override = override; - this.items = Object.create(null); +class BasicPlatform extends BasePlatform { + acquireContext(item) { + return item && item.getContext && item.getContext('2d') || null; } - isForType(type) { - return Object.prototype.isPrototypeOf.call(this.type.prototype, type.prototype); + updateConfig(config) { + config.options.animation = false; } - register(item) { - const proto = Object.getPrototypeOf(item); - let parentScope; - if (isIChartComponent(proto)) { - parentScope = this.register(proto); - } - const items = this.items; - const id = item.id; - const scope = this.scope + '.' + id; - if (!id) { - throw new Error('class does not have id: ' + item); - } - if (id in items) { - return scope; +} + +const EXPANDO_KEY = '$chartjs'; +const EVENT_TYPES = { + touchstart: 'mousedown', + touchmove: 'mousemove', + touchend: 'mouseup', + pointerenter: 'mouseenter', + pointerdown: 'mousedown', + pointermove: 'mousemove', + pointerup: 'mouseup', + pointerleave: 'mouseout', + pointerout: 'mouseout' +}; +const isNullOrEmpty = value => value === null || value === ''; +function initCanvas(canvas, aspectRatio) { + const style = canvas.style; + const renderHeight = canvas.getAttribute('height'); + const renderWidth = canvas.getAttribute('width'); + canvas[EXPANDO_KEY] = { + initial: { + height: renderHeight, + width: renderWidth, + style: { + display: style.display, + height: style.height, + width: style.width + } } - items[id] = item; - registerDefaults(item, scope, parentScope); - if (this.override) { - defaults.override(item.id, item.overrides); + }; + style.display = style.display || 'block'; + style.boxSizing = style.boxSizing || 'border-box'; + if (isNullOrEmpty(renderWidth)) { + const displayWidth = readUsedSize(canvas, 'width'); + if (displayWidth !== undefined) { + canvas.width = displayWidth; } - return scope; } - get(id) { - return this.items[id]; - } - unregister(item) { - const items = this.items; - const id = item.id; - const scope = this.scope; - if (id in items) { - delete items[id]; - } - if (scope && id in defaults[scope]) { - delete defaults[scope][id]; - if (this.override) { - delete overrides[id]; + if (isNullOrEmpty(renderHeight)) { + if (canvas.style.height === '') { + canvas.height = canvas.width / (aspectRatio || 2); + } else { + const displayHeight = readUsedSize(canvas, 'height'); + if (displayHeight !== undefined) { + canvas.height = displayHeight; } } } + return canvas; } -function registerDefaults(item, scope, parentScope) { - const itemDefaults = merge(Object.create(null), [ - parentScope ? defaults.get(parentScope) : {}, - defaults.get(scope), - item.defaults - ]); - defaults.set(scope, itemDefaults); - if (item.defaultRoutes) { - routeDefaults(scope, item.defaultRoutes); - } - if (item.descriptors) { - defaults.describe(scope, item.descriptors); +const eventListenerOptions = supportsEventListenerOptions ? {passive: true} : false; +function addListener(node, type, listener) { + node.addEventListener(type, listener, eventListenerOptions); +} +function removeListener(chart, type, listener) { + chart.canvas.removeEventListener(type, listener, eventListenerOptions); +} +function fromNativeEvent(event, chart) { + const type = EVENT_TYPES[event.type] || event.type; + const {x, y} = getRelativePosition(event, chart); + return { + type, + chart, + native: event, + x: x !== undefined ? x : null, + y: y !== undefined ? y : null, + }; +} +function nodeListContains(nodeList, canvas) { + for (const node of nodeList) { + if (node === canvas || node.contains(canvas)) { + return true; + } } } -function routeDefaults(scope, routes) { - Object.keys(routes).forEach(property => { - const propertyParts = property.split('.'); - const sourceName = propertyParts.pop(); - const sourceScope = [scope].concat(propertyParts).join('.'); - const parts = routes[property].split('.'); - const targetName = parts.pop(); - const targetScope = parts.join('.'); - defaults.route(sourceScope, sourceName, targetScope, targetName); +function createAttachObserver(chart, type, listener) { + const canvas = chart.canvas; + const observer = new MutationObserver(entries => { + let trigger = false; + for (const entry of entries) { + trigger = trigger || nodeListContains(entry.addedNodes, canvas); + trigger = trigger && !nodeListContains(entry.removedNodes, canvas); + } + if (trigger) { + listener(); + } }); + observer.observe(document, {childList: true, subtree: true}); + return observer; } -function isIChartComponent(proto) { - return 'id' in proto && 'defaults' in proto; +function createDetachObserver(chart, type, listener) { + const canvas = chart.canvas; + const observer = new MutationObserver(entries => { + let trigger = false; + for (const entry of entries) { + trigger = trigger || nodeListContains(entry.removedNodes, canvas); + trigger = trigger && !nodeListContains(entry.addedNodes, canvas); + } + if (trigger) { + listener(); + } + }); + observer.observe(document, {childList: true, subtree: true}); + return observer; } - -class Registry { - constructor() { - this.controllers = new TypedRegistry(DatasetController, 'datasets', true); - this.elements = new TypedRegistry(Element, 'elements'); - this.plugins = new TypedRegistry(Object, 'plugins'); - this.scales = new TypedRegistry(Scale, 'scales'); - this._typedRegistries = [this.controllers, this.scales, this.elements]; - } - add(...args) { - this._each('register', args); - } - remove(...args) { - this._each('unregister', args); - } - addControllers(...args) { - this._each('register', args, this.controllers); - } - addElements(...args) { - this._each('register', args, this.elements); - } - addPlugins(...args) { - this._each('register', args, this.plugins); - } - addScales(...args) { - this._each('register', args, this.scales); - } - getController(id) { - return this._get(id, this.controllers, 'controller'); - } - getElement(id) { - return this._get(id, this.elements, 'element'); +const drpListeningCharts = new Map(); +let oldDevicePixelRatio = 0; +function onWindowResize() { + const dpr = window.devicePixelRatio; + if (dpr === oldDevicePixelRatio) { + return; } - getPlugin(id) { - return this._get(id, this.plugins, 'plugin'); + oldDevicePixelRatio = dpr; + drpListeningCharts.forEach((resize, chart) => { + if (chart.currentDevicePixelRatio !== dpr) { + resize(); + } + }); +} +function listenDevicePixelRatioChanges(chart, resize) { + if (!drpListeningCharts.size) { + window.addEventListener('resize', onWindowResize); } - getScale(id) { - return this._get(id, this.scales, 'scale'); + drpListeningCharts.set(chart, resize); +} +function unlistenDevicePixelRatioChanges(chart) { + drpListeningCharts.delete(chart); + if (!drpListeningCharts.size) { + window.removeEventListener('resize', onWindowResize); } - removeControllers(...args) { - this._each('unregister', args, this.controllers); +} +function createResizeObserver(chart, type, listener) { + const canvas = chart.canvas; + const container = canvas && _getParentNode(canvas); + if (!container) { + return; } - removeElements(...args) { - this._each('unregister', args, this.elements); + const resize = throttled((width, height) => { + const w = container.clientWidth; + listener(width, height); + if (w < container.clientWidth) { + listener(); + } + }, window); + const observer = new ResizeObserver(entries => { + const entry = entries[0]; + const width = entry.contentRect.width; + const height = entry.contentRect.height; + if (width === 0 && height === 0) { + return; + } + resize(width, height); + }); + observer.observe(container); + listenDevicePixelRatioChanges(chart, resize); + return observer; +} +function releaseObserver(chart, type, observer) { + if (observer) { + observer.disconnect(); } - removePlugins(...args) { - this._each('unregister', args, this.plugins); + if (type === 'resize') { + unlistenDevicePixelRatioChanges(chart); } - removeScales(...args) { - this._each('unregister', args, this.scales); +} +function createProxyAndListen(chart, type, listener) { + const canvas = chart.canvas; + const proxy = throttled((event) => { + if (chart.ctx !== null) { + listener(fromNativeEvent(event, chart)); + } + }, chart, (args) => { + const event = args[0]; + return [event, event.offsetX, event.offsetY]; + }); + addListener(canvas, type, proxy); + return proxy; +} +class DomPlatform extends BasePlatform { + acquireContext(canvas, aspectRatio) { + const context = canvas && canvas.getContext && canvas.getContext('2d'); + if (context && context.canvas === canvas) { + initCanvas(canvas, aspectRatio); + return context; + } + return null; } - _each(method, args, typedRegistry) { - [...args].forEach(arg => { - const reg = typedRegistry || this._getRegistryForType(arg); - if (typedRegistry || reg.isForType(arg) || (reg === this.plugins && arg.id)) { - this._exec(method, reg, arg); + releaseContext(context) { + const canvas = context.canvas; + if (!canvas[EXPANDO_KEY]) { + return false; + } + const initial = canvas[EXPANDO_KEY].initial; + ['height', 'width'].forEach((prop) => { + const value = initial[prop]; + if (isNullOrUndef(value)) { + canvas.removeAttribute(prop); } else { - each(arg, item => { - const itemReg = typedRegistry || this._getRegistryForType(item); - this._exec(method, itemReg, item); - }); + canvas.setAttribute(prop, value); } }); + const style = initial.style || {}; + Object.keys(style).forEach((key) => { + canvas.style[key] = style[key]; + }); + canvas.width = canvas.width; + delete canvas[EXPANDO_KEY]; + return true; } - _exec(method, registry, component) { - const camelMethod = _capitalize(method); - callback(component['before' + camelMethod], [], component); - registry[method](component); - callback(component['after' + camelMethod], [], component); + addEventListener(chart, type, listener) { + this.removeEventListener(chart, type); + const proxies = chart.$proxies || (chart.$proxies = {}); + const handlers = { + attach: createAttachObserver, + detach: createDetachObserver, + resize: createResizeObserver + }; + const handler = handlers[type] || createProxyAndListen; + proxies[type] = handler(chart, type, listener); } - _getRegistryForType(type) { - for (let i = 0; i < this._typedRegistries.length; i++) { - const reg = this._typedRegistries[i]; - if (reg.isForType(type)) { - return reg; - } + removeEventListener(chart, type) { + const proxies = chart.$proxies || (chart.$proxies = {}); + const proxy = proxies[type]; + if (!proxy) { + return; } - return this.plugins; + const handlers = { + attach: releaseObserver, + detach: releaseObserver, + resize: releaseObserver + }; + const handler = handlers[type] || removeListener; + handler(chart, type, proxy); + proxies[type] = undefined; } - _get(id, typedRegistry, type) { - const item = typedRegistry.get(id); - if (item === undefined) { - throw new Error('"' + id + '" is not a registered ' + type + '.'); - } - return item; + getDevicePixelRatio() { + return window.devicePixelRatio; + } + getMaximumSize(canvas, width, height, aspectRatio) { + return getMaximumSize(canvas, width, height, aspectRatio); + } + isAttached(canvas) { + const container = _getParentNode(canvas); + return !!(container && container.isConnected); } } -var registry = new Registry(); + +function _detectPlatform(canvas) { + if (!_isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) { + return BasicPlatform; + } + return DomPlatform; +} class PluginService { constructor() { @@ -4954,6 +5020,7 @@ class PluginService { } } function allPlugins(config) { + const localIds = {}; const plugins = []; const keys = Object.keys(registry.plugins.items); for (let i = 0; i < keys.length; i++) { @@ -4964,9 +5031,10 @@ function allPlugins(config) { const plugin = local[i]; if (plugins.indexOf(plugin) === -1) { plugins.push(plugin); + localIds[plugin.id] = true; } } - return plugins; + return {plugins, localIds}; } function getOpts(options, all) { if (!all && options === false) { @@ -4977,11 +5045,10 @@ function getOpts(options, all) { } return options; } -function createDescriptors(chart, plugins, options, all) { +function createDescriptors(chart, {plugins, localIds}, options, all) { const result = []; const context = chart.getContext(); - for (let i = 0; i < plugins.length; i++) { - const plugin = plugins[i]; + for (const plugin of plugins) { const id = plugin.id; const opts = getOpts(options[id], all); if (opts === null) { @@ -4989,15 +5056,22 @@ function createDescriptors(chart, plugins, options, all) { } result.push({ plugin, - options: pluginOpts(chart.config, plugin, opts, context) + options: pluginOpts(chart.config, {plugin, local: localIds[id]}, opts, context) }); } return result; } -function pluginOpts(config, plugin, opts, context) { +function pluginOpts(config, {plugin, local}, opts, context) { const keys = config.pluginScopeKeys(plugin); const scopes = config.getOptionScopes(opts, keys); - return config.createResolver(scopes, context, [''], {scriptable: false, indexable: false, allKeys: true}); + if (local && plugin.defaults) { + scopes.push(plugin.defaults); + } + return config.createResolver(scopes, context, [''], { + scriptable: false, + indexable: false, + allKeys: true + }); } function getIndexAxis(type, options) { @@ -5283,7 +5357,7 @@ function needContext(proxy, names) { return false; } -var version = "3.7.1"; +var version = "3.9.1"; const KNOWN_POSITIONS = ['top', 'bottom', 'left', 'right', 'chartArea']; function positionIsHorizontal(position, axis) { @@ -5353,7 +5427,7 @@ class Chart { if (existingChart) { throw new Error( 'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' + - ' must be destroyed before the canvas can be reused.' + ' must be destroyed before the canvas with ID \'' + existingChart.canvas.id + '\' can be reused.' ); } const options = config.createResolver(config.chartOptionScopes(), this.getContext()); @@ -5824,6 +5898,9 @@ class Chart { args.cancelable = false; this.notifyPlugins('afterDatasetDraw', args); } + isPointInArea(point) { + return _isPointInArea(point, this.chartArea, this._minPadding); + } getElementsAtEventForMode(e, mode, options, useFinalPosition) { const method = Interaction.modes[mode]; if (typeof method === 'function') { @@ -6063,7 +6140,7 @@ class Chart { event: e, replay, cancelable: true, - inChartArea: _isPointInArea(e, this.chartArea, this._minPadding) + inChartArea: this.isPointInArea(e) }; const eventFilter = (plugin) => (plugin.options.events || this.options.events).includes(e.native.type); if (this.notifyPlugins('beforeEvent', args, eventFilter) === false) { @@ -6190,7 +6267,7 @@ function rThetaToXY(r, theta, x, y) { y: y + r * Math.sin(theta), }; } -function pathArc(ctx, element, offset, spacing, end) { +function pathArc(ctx, element, offset, spacing, end, circular) { const {x, y, startAngle: start, pixelMargin, innerRadius: innerR} = element; const outerRadius = Math.max(element.outerRadius + spacing + offset - pixelMargin, 0); const innerRadius = innerR > 0 ? innerR + spacing + offset + pixelMargin : 0; @@ -6217,35 +6294,45 @@ function pathArc(ctx, element, offset, spacing, end) { const innerStartAdjustedAngle = startAngle + innerStart / innerStartAdjustedRadius; const innerEndAdjustedAngle = endAngle - innerEnd / innerEndAdjustedRadius; ctx.beginPath(); - ctx.arc(x, y, outerRadius, outerStartAdjustedAngle, outerEndAdjustedAngle); - if (outerEnd > 0) { - const pCenter = rThetaToXY(outerEndAdjustedRadius, outerEndAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, outerEnd, outerEndAdjustedAngle, endAngle + HALF_PI); - } - const p4 = rThetaToXY(innerEndAdjustedRadius, endAngle, x, y); - ctx.lineTo(p4.x, p4.y); - if (innerEnd > 0) { - const pCenter = rThetaToXY(innerEndAdjustedRadius, innerEndAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, innerEnd, endAngle + HALF_PI, innerEndAdjustedAngle + Math.PI); - } - ctx.arc(x, y, innerRadius, endAngle - (innerEnd / innerRadius), startAngle + (innerStart / innerRadius), true); - if (innerStart > 0) { - const pCenter = rThetaToXY(innerStartAdjustedRadius, innerStartAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, innerStart, innerStartAdjustedAngle + Math.PI, startAngle - HALF_PI); - } - const p8 = rThetaToXY(outerStartAdjustedRadius, startAngle, x, y); - ctx.lineTo(p8.x, p8.y); - if (outerStart > 0) { - const pCenter = rThetaToXY(outerStartAdjustedRadius, outerStartAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, outerStart, startAngle - HALF_PI, outerStartAdjustedAngle); + if (circular) { + ctx.arc(x, y, outerRadius, outerStartAdjustedAngle, outerEndAdjustedAngle); + if (outerEnd > 0) { + const pCenter = rThetaToXY(outerEndAdjustedRadius, outerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerEnd, outerEndAdjustedAngle, endAngle + HALF_PI); + } + const p4 = rThetaToXY(innerEndAdjustedRadius, endAngle, x, y); + ctx.lineTo(p4.x, p4.y); + if (innerEnd > 0) { + const pCenter = rThetaToXY(innerEndAdjustedRadius, innerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerEnd, endAngle + HALF_PI, innerEndAdjustedAngle + Math.PI); + } + ctx.arc(x, y, innerRadius, endAngle - (innerEnd / innerRadius), startAngle + (innerStart / innerRadius), true); + if (innerStart > 0) { + const pCenter = rThetaToXY(innerStartAdjustedRadius, innerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerStart, innerStartAdjustedAngle + Math.PI, startAngle - HALF_PI); + } + const p8 = rThetaToXY(outerStartAdjustedRadius, startAngle, x, y); + ctx.lineTo(p8.x, p8.y); + if (outerStart > 0) { + const pCenter = rThetaToXY(outerStartAdjustedRadius, outerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerStart, startAngle - HALF_PI, outerStartAdjustedAngle); + } + } else { + ctx.moveTo(x, y); + const outerStartX = Math.cos(outerStartAdjustedAngle) * outerRadius + x; + const outerStartY = Math.sin(outerStartAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerStartX, outerStartY); + const outerEndX = Math.cos(outerEndAdjustedAngle) * outerRadius + x; + const outerEndY = Math.sin(outerEndAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerEndX, outerEndY); } ctx.closePath(); } -function drawArc(ctx, element, offset, spacing) { +function drawArc(ctx, element, offset, spacing, circular) { const {fullCircles, startAngle, circumference} = element; let endAngle = element.endAngle; if (fullCircles) { - pathArc(ctx, element, offset, spacing, startAngle + TAU); + pathArc(ctx, element, offset, spacing, startAngle + TAU, circular); for (let i = 0; i < fullCircles; ++i) { ctx.fill(); } @@ -6256,7 +6343,7 @@ function drawArc(ctx, element, offset, spacing) { } } } - pathArc(ctx, element, offset, spacing, endAngle); + pathArc(ctx, element, offset, spacing, endAngle, circular); ctx.fill(); return endAngle; } @@ -6279,7 +6366,7 @@ function drawFullCircleBorders(ctx, element, inner) { ctx.stroke(); } } -function drawBorder(ctx, element, offset, spacing, endAngle) { +function drawBorder(ctx, element, offset, spacing, endAngle, circular) { const {options} = element; const {borderWidth, borderJoinStyle} = options; const inner = options.borderAlign === 'inner'; @@ -6299,7 +6386,7 @@ function drawBorder(ctx, element, offset, spacing, endAngle) { if (inner) { clipArc(ctx, element, endAngle); } - pathArc(ctx, element, offset, spacing, endAngle); + pathArc(ctx, element, offset, spacing, endAngle, circular); ctx.stroke(); } class ArcElement extends Element { @@ -6358,6 +6445,7 @@ class ArcElement extends Element { const {options, circumference} = this; const offset = (options.offset || 0) / 2; const spacing = (options.spacing || 0) / 2; + const circular = options.circular; this.pixelMargin = (options.borderAlign === 'inner') ? 0.33 : 0; this.fullCircles = circumference > TAU ? Math.floor(circumference / TAU) : 0; if (circumference === 0 || this.innerRadius < 0 || this.outerRadius < 0) { @@ -6375,8 +6463,8 @@ class ArcElement extends Element { } ctx.fillStyle = options.backgroundColor; ctx.strokeStyle = options.borderColor; - const endAngle = drawArc(ctx, this, radiusOffset, spacing); - drawBorder(ctx, this, radiusOffset, spacing, endAngle); + const endAngle = drawArc(ctx, this, radiusOffset, spacing, circular); + drawBorder(ctx, this, radiusOffset, spacing, endAngle, circular); ctx.restore(); } } @@ -6390,6 +6478,7 @@ ArcElement.defaults = { offset: 0, spacing: 0, angle: undefined, + circular: true, }; ArcElement.defaultRoutes = { backgroundColor: 'backgroundColor' @@ -7061,7 +7150,7 @@ var plugin_decimation = { if (resolve([indexAxis, chart.options.indexAxis]) === 'y') { return; } - if (meta.type !== 'line') { + if (!meta.controller.supportsDecimation) { return; } const xAxis = chart.scales[meta.xAxisID]; @@ -7110,158 +7199,203 @@ var plugin_decimation = { } }; -function getLineByIndex(chart, index) { - const meta = chart.getDatasetMeta(index); - const visible = meta && chart.isDatasetVisible(index); - return visible ? meta.dataset : null; +function _segments(line, target, property) { + const segments = line.segments; + const points = line.points; + const tpoints = target.points; + const parts = []; + for (const segment of segments) { + let {start, end} = segment; + end = _findSegmentEnd(start, end, points); + const bounds = _getBounds(property, points[start], points[end], segment.loop); + if (!target.segments) { + parts.push({ + source: segment, + target: bounds, + start: points[start], + end: points[end] + }); + continue; + } + const targetSegments = _boundSegments(target, bounds); + for (const tgt of targetSegments) { + const subBounds = _getBounds(property, tpoints[tgt.start], tpoints[tgt.end], tgt.loop); + const fillSources = _boundSegment(segment, points, subBounds); + for (const fillSource of fillSources) { + parts.push({ + source: fillSource, + target: tgt, + start: { + [property]: _getEdge(bounds, subBounds, 'start', Math.max) + }, + end: { + [property]: _getEdge(bounds, subBounds, 'end', Math.min) + } + }); + } + } + } + return parts; } -function parseFillOption(line) { - const options = line.options; - const fillOption = options.fill; - let fill = valueOrDefault(fillOption && fillOption.target, fillOption); - if (fill === undefined) { - fill = !!options.backgroundColor; +function _getBounds(property, first, last, loop) { + if (loop) { + return; } - if (fill === false || fill === null) { - return false; + let start = first[property]; + let end = last[property]; + if (property === 'angle') { + start = _normalizeAngle(start); + end = _normalizeAngle(end); } - if (fill === true) { - return 'origin'; + return {property, start, end}; +} +function _pointsFromSegments(boundary, line) { + const {x = null, y = null} = boundary || {}; + const linePoints = line.points; + const points = []; + line.segments.forEach(({start, end}) => { + end = _findSegmentEnd(start, end, linePoints); + const first = linePoints[start]; + const last = linePoints[end]; + if (y !== null) { + points.push({x: first.x, y}); + points.push({x: last.x, y}); + } else if (x !== null) { + points.push({x, y: first.y}); + points.push({x, y: last.y}); + } + }); + return points; +} +function _findSegmentEnd(start, end, points) { + for (;end > start; end--) { + const point = points[end]; + if (!isNaN(point.x) && !isNaN(point.y)) { + break; + } } - return fill; + return end; +} +function _getEdge(a, b, prop, fn) { + if (a && b) { + return fn(a[prop], b[prop]); + } + return a ? a[prop] : b ? b[prop] : 0; +} + +function _createBoundaryLine(boundary, line) { + let points = []; + let _loop = false; + if (isArray(boundary)) { + _loop = true; + points = boundary; + } else { + points = _pointsFromSegments(boundary, line); + } + return points.length ? new LineElement({ + points, + options: {tension: 0}, + _loop, + _fullLoop: _loop + }) : null; +} +function _shouldApplyFill(source) { + return source && source.fill !== false; +} + +function _resolveTarget(sources, index, propagate) { + const source = sources[index]; + let fill = source.fill; + const visited = [index]; + let target; + if (!propagate) { + return fill; + } + while (fill !== false && visited.indexOf(fill) === -1) { + if (!isNumberFinite(fill)) { + return fill; + } + target = sources[fill]; + if (!target) { + return false; + } + if (target.visible) { + return fill; + } + visited.push(fill); + fill = target.fill; + } + return false; } -function decodeFill(line, index, count) { +function _decodeFill(line, index, count) { const fill = parseFillOption(line); if (isObject(fill)) { return isNaN(fill.value) ? false : fill; } let target = parseFloat(fill); if (isNumberFinite(target) && Math.floor(target) === target) { - if (fill[0] === '-' || fill[0] === '+') { - target = index + target; - } - if (target === index || target < 0 || target >= count) { - return false; - } - return target; + return decodeTargetIndex(fill[0], index, target, count); } return ['origin', 'start', 'end', 'stack', 'shape'].indexOf(fill) >= 0 && fill; } -function computeLinearBoundary(source) { - const {scale = {}, fill} = source; - let target = null; - let horizontal; +function decodeTargetIndex(firstCh, index, target, count) { + if (firstCh === '-' || firstCh === '+') { + target = index + target; + } + if (target === index || target < 0 || target >= count) { + return false; + } + return target; +} +function _getTargetPixel(fill, scale) { + let pixel = null; if (fill === 'start') { - target = scale.bottom; + pixel = scale.bottom; } else if (fill === 'end') { - target = scale.top; + pixel = scale.top; } else if (isObject(fill)) { - target = scale.getPixelForValue(fill.value); + pixel = scale.getPixelForValue(fill.value); } else if (scale.getBasePixel) { - target = scale.getBasePixel(); - } - if (isNumberFinite(target)) { - horizontal = scale.isHorizontal(); - return { - x: horizontal ? target : null, - y: horizontal ? null : target - }; - } - return null; -} -class simpleArc { - constructor(opts) { - this.x = opts.x; - this.y = opts.y; - this.radius = opts.radius; - } - pathSegment(ctx, bounds, opts) { - const {x, y, radius} = this; - bounds = bounds || {start: 0, end: TAU}; - ctx.arc(x, y, radius, bounds.end, bounds.start, true); - return !opts.bounds; - } - interpolate(point) { - const {x, y, radius} = this; - const angle = point.angle; - return { - x: x + Math.cos(angle) * radius, - y: y + Math.sin(angle) * radius, - angle - }; + pixel = scale.getBasePixel(); } + return pixel; } -function computeCircularBoundary(source) { - const {scale, fill} = source; - const options = scale.options; - const length = scale.getLabels().length; - const target = []; - const start = options.reverse ? scale.max : scale.min; - const end = options.reverse ? scale.min : scale.max; - let i, center, value; +function _getTargetValue(fill, scale, startValue) { + let value; if (fill === 'start') { - value = start; + value = startValue; } else if (fill === 'end') { - value = end; + value = scale.options.reverse ? scale.min : scale.max; } else if (isObject(fill)) { value = fill.value; } else { value = scale.getBaseValue(); } - if (options.grid.circular) { - center = scale.getPointPositionForValue(0, start); - return new simpleArc({ - x: center.x, - y: center.y, - radius: scale.getDistanceFromCenterForValue(value) - }); - } - for (i = 0; i < length; ++i) { - target.push(scale.getPointPositionForValue(i, value)); - } - return target; -} -function computeBoundary(source) { - const scale = source.scale || {}; - if (scale.getPointPositionForValue) { - return computeCircularBoundary(source); - } - return computeLinearBoundary(source); -} -function findSegmentEnd(start, end, points) { - for (;end > start; end--) { - const point = points[end]; - if (!isNaN(point.x) && !isNaN(point.y)) { - break; - } - } - return end; + return value; } -function pointsFromSegments(boundary, line) { - const {x = null, y = null} = boundary || {}; - const linePoints = line.points; - const points = []; - line.segments.forEach(({start, end}) => { - end = findSegmentEnd(start, end, linePoints); - const first = linePoints[start]; - const last = linePoints[end]; - if (y !== null) { - points.push({x: first.x, y}); - points.push({x: last.x, y}); - } else if (x !== null) { - points.push({x, y: first.y}); - points.push({x, y: last.y}); - } - }); - return points; +function parseFillOption(line) { + const options = line.options; + const fillOption = options.fill; + let fill = valueOrDefault(fillOption && fillOption.target, fillOption); + if (fill === undefined) { + fill = !!options.backgroundColor; + } + if (fill === false || fill === null) { + return false; + } + if (fill === true) { + return 'origin'; + } + return fill; } -function buildStackLine(source) { + +function _buildStackLine(source) { const {scale, index, line} = source; const points = []; const segments = line.segments; const sourcePoints = line.points; const linesBelow = getLinesBelow(scale, index); - linesBelow.push(createBoundaryLine({x: null, y: scale.bottom}, line)); + linesBelow.push(_createBoundaryLine({x: null, y: scale.bottom}, line)); for (let i = 0; i < segments.length; i++) { const segment = segments[i]; for (let j = segment.start; j <= segment.end; j++) { @@ -7325,13 +7459,37 @@ function findPoint(line, sourcePoint, property) { } return {first, last, point}; } -function getTarget(source) { + +class simpleArc { + constructor(opts) { + this.x = opts.x; + this.y = opts.y; + this.radius = opts.radius; + } + pathSegment(ctx, bounds, opts) { + const {x, y, radius} = this; + bounds = bounds || {start: 0, end: TAU}; + ctx.arc(x, y, radius, bounds.end, bounds.start, true); + return !opts.bounds; + } + interpolate(point) { + const {x, y, radius} = this; + const angle = point.angle; + return { + x: x + Math.cos(angle) * radius, + y: y + Math.sin(angle) * radius, + angle + }; + } +} + +function _getTarget(source) { const {chart, fill, line} = source; if (isNumberFinite(fill)) { return getLineByIndex(chart, fill); } if (fill === 'stack') { - return buildStackLine(source); + return _buildStackLine(source); } if (fill === 'shape') { return true; @@ -7340,49 +7498,81 @@ function getTarget(source) { if (boundary instanceof simpleArc) { return boundary; } - return createBoundaryLine(boundary, line); + return _createBoundaryLine(boundary, line); } -function createBoundaryLine(boundary, line) { - let points = []; - let _loop = false; - if (isArray(boundary)) { - _loop = true; - points = boundary; - } else { - points = pointsFromSegments(boundary, line); +function getLineByIndex(chart, index) { + const meta = chart.getDatasetMeta(index); + const visible = meta && chart.isDatasetVisible(index); + return visible ? meta.dataset : null; +} +function computeBoundary(source) { + const scale = source.scale || {}; + if (scale.getPointPositionForValue) { + return computeCircularBoundary(source); } - return points.length ? new LineElement({ - points, - options: {tension: 0}, - _loop, - _fullLoop: _loop - }) : null; + return computeLinearBoundary(source); } -function resolveTarget(sources, index, propagate) { - const source = sources[index]; - let fill = source.fill; - const visited = [index]; - let target; - if (!propagate) { - return fill; +function computeLinearBoundary(source) { + const {scale = {}, fill} = source; + const pixel = _getTargetPixel(fill, scale); + if (isNumberFinite(pixel)) { + const horizontal = scale.isHorizontal(); + return { + x: horizontal ? pixel : null, + y: horizontal ? null : pixel + }; } - while (fill !== false && visited.indexOf(fill) === -1) { - if (!isNumberFinite(fill)) { - return fill; - } - target = sources[fill]; - if (!target) { - return false; - } - if (target.visible) { - return fill; - } - visited.push(fill); - fill = target.fill; + return null; +} +function computeCircularBoundary(source) { + const {scale, fill} = source; + const options = scale.options; + const length = scale.getLabels().length; + const start = options.reverse ? scale.max : scale.min; + const value = _getTargetValue(fill, scale, start); + const target = []; + if (options.grid.circular) { + const center = scale.getPointPositionForValue(0, start); + return new simpleArc({ + x: center.x, + y: center.y, + radius: scale.getDistanceFromCenterForValue(value) + }); } - return false; + for (let i = 0; i < length; ++i) { + target.push(scale.getPointPositionForValue(i, value)); + } + return target; +} + +function _drawfill(ctx, source, area) { + const target = _getTarget(source); + const {line, scale, axis} = source; + const lineOpts = line.options; + const fillOption = lineOpts.fill; + const color = lineOpts.backgroundColor; + const {above = color, below = color} = fillOption || {}; + if (target && line.points.length) { + clipArea(ctx, area); + doFill(ctx, {line, target, above, below, area, scale, axis}); + unclipArea(ctx); + } +} +function doFill(ctx, cfg) { + const {line, target, above, below, area, scale} = cfg; + const property = line._loop ? 'angle' : cfg.axis; + ctx.save(); + if (property === 'x' && below !== above) { + clipVertical(ctx, target, area.top); + fill(ctx, {line, target, color: above, scale, property}); + ctx.restore(); + ctx.save(); + clipVertical(ctx, target, area.bottom); + } + fill(ctx, {line, target, color: below, scale, property}); + ctx.restore(); } -function _clip(ctx, target, clipY) { +function clipVertical(ctx, target, clipY) { const {segments, points} = target; let first = true; let lineLoop = false; @@ -7390,7 +7580,7 @@ function _clip(ctx, target, clipY) { for (const segment of segments) { const {start, end} = segment; const firstPoint = points[start]; - const lastPoint = points[findSegmentEnd(start, end, points)]; + const lastPoint = points[_findSegmentEnd(start, end, points)]; if (first) { ctx.moveTo(firstPoint.x, firstPoint.y); first = false; @@ -7409,78 +7599,7 @@ function _clip(ctx, target, clipY) { ctx.closePath(); ctx.clip(); } -function getBounds(property, first, last, loop) { - if (loop) { - return; - } - let start = first[property]; - let end = last[property]; - if (property === 'angle') { - start = _normalizeAngle(start); - end = _normalizeAngle(end); - } - return {property, start, end}; -} -function _getEdge(a, b, prop, fn) { - if (a && b) { - return fn(a[prop], b[prop]); - } - return a ? a[prop] : b ? b[prop] : 0; -} -function _segments(line, target, property) { - const segments = line.segments; - const points = line.points; - const tpoints = target.points; - const parts = []; - for (const segment of segments) { - let {start, end} = segment; - end = findSegmentEnd(start, end, points); - const bounds = getBounds(property, points[start], points[end], segment.loop); - if (!target.segments) { - parts.push({ - source: segment, - target: bounds, - start: points[start], - end: points[end] - }); - continue; - } - const targetSegments = _boundSegments(target, bounds); - for (const tgt of targetSegments) { - const subBounds = getBounds(property, tpoints[tgt.start], tpoints[tgt.end], tgt.loop); - const fillSources = _boundSegment(segment, points, subBounds); - for (const fillSource of fillSources) { - parts.push({ - source: fillSource, - target: tgt, - start: { - [property]: _getEdge(bounds, subBounds, 'start', Math.max) - }, - end: { - [property]: _getEdge(bounds, subBounds, 'end', Math.min) - } - }); - } - } - } - return parts; -} -function clipBounds(ctx, scale, bounds) { - const {top, bottom} = scale.chart.chartArea; - const {property, start, end} = bounds || {}; - if (property === 'x') { - ctx.beginPath(); - ctx.rect(start, top, end - start, bottom - top); - ctx.clip(); - } -} -function interpolatedLineTo(ctx, target, point, property) { - const interpolatedPoint = target.interpolate(point, property); - if (interpolatedPoint) { - ctx.lineTo(interpolatedPoint.x, interpolatedPoint.y); - } -} -function _fill(ctx, cfg) { +function fill(ctx, cfg) { const {line, target, property, color, scale} = cfg; const segments = _segments(line, target, property); for (const {source: src, target: tgt, start, end} of segments) { @@ -7488,7 +7607,7 @@ function _fill(ctx, cfg) { const notShape = target !== true; ctx.save(); ctx.fillStyle = backgroundColor; - clipBounds(ctx, scale, notShape && getBounds(property, start, end)); + clipBounds(ctx, scale, notShape && _getBounds(property, start, end)); ctx.beginPath(); const lineLoop = !!line.pathSegment(ctx, src); let loop; @@ -7509,34 +7628,23 @@ function _fill(ctx, cfg) { ctx.restore(); } } -function doFill(ctx, cfg) { - const {line, target, above, below, area, scale} = cfg; - const property = line._loop ? 'angle' : cfg.axis; - ctx.save(); - if (property === 'x' && below !== above) { - _clip(ctx, target, area.top); - _fill(ctx, {line, target, color: above, scale, property}); - ctx.restore(); - ctx.save(); - _clip(ctx, target, area.bottom); +function clipBounds(ctx, scale, bounds) { + const {top, bottom} = scale.chart.chartArea; + const {property, start, end} = bounds || {}; + if (property === 'x') { + ctx.beginPath(); + ctx.rect(start, top, end - start, bottom - top); + ctx.clip(); } - _fill(ctx, {line, target, color: below, scale, property}); - ctx.restore(); } -function drawfill(ctx, source, area) { - const target = getTarget(source); - const {line, scale, axis} = source; - const lineOpts = line.options; - const fillOption = lineOpts.fill; - const color = lineOpts.backgroundColor; - const {above = color, below = color} = fillOption || {}; - if (target && line.points.length) { - clipArea(ctx, area); - doFill(ctx, {line, target, above, below, area, scale, axis}); - unclipArea(ctx); +function interpolatedLineTo(ctx, target, point, property) { + const interpolatedPoint = target.interpolate(point, property); + if (interpolatedPoint) { + ctx.lineTo(interpolatedPoint.x, interpolatedPoint.y); } } -var plugin_filler = { + +var index = { id: 'filler', afterDatasetsUpdate(chart, _args, options) { const count = (chart.data.datasets || []).length; @@ -7550,7 +7658,7 @@ var plugin_filler = { source = { visible: chart.isDatasetVisible(i), index: i, - fill: decodeFill(line, i, count), + fill: _decodeFill(line, i, count), chart, axis: meta.controller.options.indexAxis, scale: meta.vScale, @@ -7565,7 +7673,7 @@ var plugin_filler = { if (!source || source.fill === false) { continue; } - source.fill = resolveTarget(sources, i, options.propagate); + source.fill = _resolveTarget(sources, i, options.propagate); } }, beforeDraw(chart, _args, options) { @@ -7578,8 +7686,8 @@ var plugin_filler = { continue; } source.line.updateControlPoints(area, source.axis); - if (draw) { - drawfill(chart.ctx, source, area); + if (draw && source.fill) { + _drawfill(chart.ctx, source, area); } } }, @@ -7590,17 +7698,17 @@ var plugin_filler = { const metasets = chart.getSortedVisibleDatasetMetas(); for (let i = metasets.length - 1; i >= 0; --i) { const source = metasets[i].$filler; - if (source) { - drawfill(chart.ctx, source, chart.chartArea); + if (_shouldApplyFill(source)) { + _drawfill(chart.ctx, source, chart.chartArea); } } }, beforeDatasetDraw(chart, args, options) { const source = args.meta.$filler; - if (!source || source.fill === false || options.drawTime !== 'beforeDatasetDraw') { + if (!_shouldApplyFill(source) || options.drawTime !== 'beforeDatasetDraw') { return; } - drawfill(chart.ctx, source, chart.chartArea); + _drawfill(chart.ctx, source, chart.chartArea); }, defaults: { propagate: true, @@ -7612,7 +7720,7 @@ const getBoxSize = (labelOpts, fontSize) => { let {boxHeight = fontSize, boxWidth = fontSize} = labelOpts; if (labelOpts.usePointStyle) { boxHeight = Math.min(boxHeight, fontSize); - boxWidth = Math.min(boxWidth, fontSize); + boxWidth = labelOpts.pointStyleWidth || Math.min(boxWidth, fontSize); } return { boxWidth, @@ -7829,14 +7937,14 @@ class Legend extends Element { ctx.setLineDash(valueOrDefault(legendItem.lineDash, [])); if (labelOpts.usePointStyle) { const drawOptions = { - radius: boxWidth * Math.SQRT2 / 2, + radius: boxHeight * Math.SQRT2 / 2, pointStyle: legendItem.pointStyle, rotation: legendItem.rotation, borderWidth: lineWidth }; const centerX = rtlHelper.xPlus(x, boxWidth / 2); const centerY = y + halfFontSize; - drawPoint(ctx, drawOptions, centerX, centerY); + drawPointLegend(ctx, drawOptions, centerX, centerY, labelOpts.pointStyleWidth && boxWidth); } else { const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0); const xBoxLeft = rtlHelper.leftForLtr(x, boxWidth); @@ -7974,7 +8082,7 @@ class Legend extends Element { return; } const hoveredItem = this._getLegendItemAt(e.x, e.y); - if (e.type === 'mousemove') { + if (e.type === 'mousemove' || e.type === 'mouseout') { const previous = this._hoveredItem; const sameItem = itemsEqual(previous, hoveredItem); if (previous && !sameItem) { @@ -7990,7 +8098,7 @@ class Legend extends Element { } } function isListened(type, opts) { - if (type === 'mousemove' && (opts.onHover || opts.onLeave)) { + if ((type === 'mousemove' || type === 'mouseout') && (opts.onHover || opts.onLeave)) { return true; } if (opts.onClick && (type === 'click' || type === 'mouseup')) { @@ -8778,7 +8886,7 @@ class Tooltip extends Element { ctx.fillStyle = labelColors.backgroundColor; drawPoint(ctx, drawOptions, centerX, centerY); } else { - ctx.lineWidth = labelColors.borderWidth || 1; + ctx.lineWidth = isObject(labelColors.borderWidth) ? Math.max(...Object.values(labelColors.borderWidth)) : (labelColors.borderWidth || 1); ctx.strokeStyle = labelColors.borderColor; ctx.setLineDash(labelColors.borderDash || []); ctx.lineDashOffset = labelColors.borderDashOffset || 0; @@ -8940,6 +9048,9 @@ class Tooltip extends Element { } } } + _willRender() { + return !!this.opacity; + } draw(ctx) { const options = this.options.setContext(this.getContext()); let opacity = this.opacity; @@ -9060,16 +9171,16 @@ var plugin_tooltip = { }, afterDraw(chart) { const tooltip = chart.tooltip; - const args = { - tooltip - }; - if (chart.notifyPlugins('beforeTooltipDraw', args) === false) { - return; - } - if (tooltip) { + if (tooltip && tooltip._willRender()) { + const args = { + tooltip + }; + if (chart.notifyPlugins('beforeTooltipDraw', args) === false) { + return; + } tooltip.draw(chart.ctx); + chart.notifyPlugins('afterTooltipDraw', args); } - chart.notifyPlugins('afterTooltipDraw', args); }, afterEvent(chart, args) { if (chart.tooltip) { @@ -9217,7 +9328,7 @@ var plugin_tooltip = { var plugins = /*#__PURE__*/Object.freeze({ __proto__: null, Decimation: plugin_decimation, -Filler: plugin_filler, +Filler: index, Legend: plugin_legend, SubTitle: plugin_subtitle, Title: plugin_title, @@ -9858,9 +9969,26 @@ function drawPointLabels(scale, labelCount) { const {x, y, textAlign, left, top, right, bottom} = scale._pointLabelItems[i]; const {backdropColor} = optsAtIndex; if (!isNullOrUndef(backdropColor)) { + const borderRadius = toTRBLCorners(optsAtIndex.borderRadius); const padding = toPadding(optsAtIndex.backdropPadding); ctx.fillStyle = backdropColor; - ctx.fillRect(left - padding.left, top - padding.top, right - left + padding.width, bottom - top + padding.height); + const backdropLeft = left - padding.left; + const backdropTop = top - padding.top; + const backdropWidth = right - left + padding.width; + const backdropHeight = bottom - top + padding.height; + if (Object.values(borderRadius).some(v => v !== 0)) { + ctx.beginPath(); + addRoundedRectPath(ctx, { + x: backdropLeft, + y: backdropTop, + w: backdropWidth, + h: backdropHeight, + radius: borderRadius, + }); + ctx.fill(); + } else { + ctx.fillRect(backdropLeft, backdropTop, backdropWidth, backdropHeight); + } } renderText( ctx, @@ -10274,6 +10402,7 @@ class TimeScale extends Scale { init(scaleOpts, opts) { const time = scaleOpts.time || (scaleOpts.time = {}); const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date); + adapter.init(opts); mergeIf(time.displayFormats, adapter.formats()); this._parseOpts = { parser: time.parser, @@ -10354,6 +10483,11 @@ class TimeScale extends Scale { } return ticksFromTimestamps(this, ticks, this._majorUnit); } + afterAutoSkip() { + if (this.options.offsetAfterAutoskip) { + this.initOffsets(this.ticks.map(tick => +tick.value)); + } + } initOffsets(timestamps) { let start = 0; let end = 0; @@ -10624,4 +10758,4 @@ const registerables = [ scales, ]; -export { Animation, Animations, ArcElement, BarController, BarElement, BasePlatform, BasicPlatform, BubbleController, CategoryScale, Chart, DatasetController, plugin_decimation as Decimation, DomPlatform, DoughnutController, Element, plugin_filler as Filler, Interaction, plugin_legend as Legend, LineController, LineElement, LinearScale, LogarithmicScale, PieController, PointElement, PolarAreaController, RadarController, RadialLinearScale, Scale, ScatterController, plugin_subtitle as SubTitle, Ticks, TimeScale, TimeSeriesScale, plugin_title as Title, plugin_tooltip as Tooltip, adapters as _adapters, _detectPlatform, animator, controllers, elements, layouts, plugins, registerables, registry, scales }; +export { Animation, Animations, ArcElement, BarController, BarElement, BasePlatform, BasicPlatform, BubbleController, CategoryScale, Chart, DatasetController, plugin_decimation as Decimation, DomPlatform, DoughnutController, Element, index as Filler, Interaction, plugin_legend as Legend, LineController, LineElement, LinearScale, LogarithmicScale, PieController, PointElement, PolarAreaController, RadarController, RadialLinearScale, Scale, ScatterController, plugin_subtitle as SubTitle, Ticks, TimeScale, TimeSeriesScale, plugin_title as Title, plugin_tooltip as Tooltip, adapters as _adapters, _detectPlatform, animator, controllers, elements, layouts, plugins, registerables, registry, scales }; diff --git a/static/js/chart.js b/static/js/chart.js index 58990616..9ea0b0a6 100644 --- a/static/js/chart.js +++ b/static/js/chart.js @@ -1,5 +1,5 @@ /*! - * Chart.js v3.7.1 + * Chart.js v3.9.1 * https://www.chartjs.org * (c) 2022 Chart.js Contributors * Released under the MIT License @@ -10,861 +10,114 @@ typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Chart = factory()); })(this, (function () { 'use strict'; -function fontString(pixelSize, fontStyle, fontFamily) { - return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; -} -const requestAnimFrame = (function() { - if (typeof window === 'undefined') { - return function(callback) { - return callback(); - }; - } - return window.requestAnimationFrame; -}()); -function throttled(fn, thisArg, updateFn) { - const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args)); - let ticking = false; - let args = []; - return function(...rest) { - args = updateArgs(rest); - if (!ticking) { - ticking = true; - requestAnimFrame.call(window, () => { - ticking = false; - fn.apply(thisArg, args); - }); - } - }; -} -function debounce(fn, delay) { - let timeout; - return function(...args) { - if (delay) { - clearTimeout(timeout); - timeout = setTimeout(fn, delay, args); - } else { - fn.apply(this, args); - } - return delay; +function noop() {} +const uid = (function() { + let id = 0; + return function() { + return id++; }; +}()); +function isNullOrUndef(value) { + return value === null || typeof value === 'undefined'; } -const _toLeftRightCenter = (align) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; -const _alignStartEnd = (align, start, end) => align === 'start' ? start : align === 'end' ? end : (start + end) / 2; -const _textX = (align, left, right, rtl) => { - const check = rtl ? 'left' : 'right'; - return align === check ? right : align === 'center' ? (left + right) / 2 : left; -}; - -class Animator { - constructor() { - this._request = null; - this._charts = new Map(); - this._running = false; - this._lastDate = undefined; +function isArray(value) { + if (Array.isArray && Array.isArray(value)) { + return true; } - _notify(chart, anims, date, type) { - const callbacks = anims.listeners[type]; - const numSteps = anims.duration; - callbacks.forEach(fn => fn({ - chart, - initial: anims.initial, - numSteps, - currentStep: Math.min(date - anims.start, numSteps) - })); + const type = Object.prototype.toString.call(value); + if (type.slice(0, 7) === '[object' && type.slice(-6) === 'Array]') { + return true; } - _refresh() { - if (this._request) { - return; - } - this._running = true; - this._request = requestAnimFrame.call(window, () => { - this._update(); - this._request = null; - if (this._running) { - this._refresh(); - } - }); + return false; +} +function isObject(value) { + return value !== null && Object.prototype.toString.call(value) === '[object Object]'; +} +const isNumberFinite = (value) => (typeof value === 'number' || value instanceof Number) && isFinite(+value); +function finiteOrDefault(value, defaultValue) { + return isNumberFinite(value) ? value : defaultValue; +} +function valueOrDefault(value, defaultValue) { + return typeof value === 'undefined' ? defaultValue : value; +} +const toPercentage = (value, dimension) => + typeof value === 'string' && value.endsWith('%') ? + parseFloat(value) / 100 + : value / dimension; +const toDimension = (value, dimension) => + typeof value === 'string' && value.endsWith('%') ? + parseFloat(value) / 100 * dimension + : +value; +function callback(fn, args, thisArg) { + if (fn && typeof fn.call === 'function') { + return fn.apply(thisArg, args); } - _update(date = Date.now()) { - let remaining = 0; - this._charts.forEach((anims, chart) => { - if (!anims.running || !anims.items.length) { - return; - } - const items = anims.items; - let i = items.length - 1; - let draw = false; - let item; - for (; i >= 0; --i) { - item = items[i]; - if (item._active) { - if (item._total > anims.duration) { - anims.duration = item._total; - } - item.tick(date); - draw = true; - } else { - items[i] = items[items.length - 1]; - items.pop(); - } - } - if (draw) { - chart.draw(); - this._notify(chart, anims, date, 'progress'); +} +function each(loopable, fn, thisArg, reverse) { + let i, len, keys; + if (isArray(loopable)) { + len = loopable.length; + if (reverse) { + for (i = len - 1; i >= 0; i--) { + fn.call(thisArg, loopable[i], i); } - if (!items.length) { - anims.running = false; - this._notify(chart, anims, date, 'complete'); - anims.initial = false; + } else { + for (i = 0; i < len; i++) { + fn.call(thisArg, loopable[i], i); } - remaining += items.length; - }); - this._lastDate = date; - if (remaining === 0) { - this._running = false; - } - } - _getAnims(chart) { - const charts = this._charts; - let anims = charts.get(chart); - if (!anims) { - anims = { - running: false, - initial: true, - items: [], - listeners: { - complete: [], - progress: [] - } - }; - charts.set(chart, anims); } - return anims; - } - listen(chart, event, cb) { - this._getAnims(chart).listeners[event].push(cb); - } - add(chart, items) { - if (!items || !items.length) { - return; + } else if (isObject(loopable)) { + keys = Object.keys(loopable); + len = keys.length; + for (i = 0; i < len; i++) { + fn.call(thisArg, loopable[keys[i]], keys[i]); } - this._getAnims(chart).items.push(...items); - } - has(chart) { - return this._getAnims(chart).items.length > 0; } - start(chart) { - const anims = this._charts.get(chart); - if (!anims) { - return; - } - anims.running = true; - anims.start = Date.now(); - anims.duration = anims.items.reduce((acc, cur) => Math.max(acc, cur._duration), 0); - this._refresh(); +} +function _elementsEqual(a0, a1) { + let i, ilen, v0, v1; + if (!a0 || !a1 || a0.length !== a1.length) { + return false; } - running(chart) { - if (!this._running) { - return false; - } - const anims = this._charts.get(chart); - if (!anims || !anims.running || !anims.items.length) { + for (i = 0, ilen = a0.length; i < ilen; ++i) { + v0 = a0[i]; + v1 = a1[i]; + if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) { return false; } - return true; } - stop(chart) { - const anims = this._charts.get(chart); - if (!anims || !anims.items.length) { - return; - } - const items = anims.items; - let i = items.length - 1; - for (; i >= 0; --i) { - items[i].cancel(); - } - anims.items = []; - this._notify(chart, anims, Date.now(), 'complete'); + return true; +} +function clone$1(source) { + if (isArray(source)) { + return source.map(clone$1); } - remove(chart) { - return this._charts.delete(chart); + if (isObject(source)) { + const target = Object.create(null); + const keys = Object.keys(source); + const klen = keys.length; + let k = 0; + for (; k < klen; ++k) { + target[keys[k]] = clone$1(source[keys[k]]); + } + return target; } + return source; } -var animator = new Animator(); - -/*! - * @kurkle/color v0.1.9 - * https://github.com/kurkle/color#readme - * (c) 2020 Jukka Kurkela - * Released under the MIT License - */ -const map$1 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15}; -const hex = '0123456789ABCDEF'; -const h1 = (b) => hex[b & 0xF]; -const h2 = (b) => hex[(b & 0xF0) >> 4] + hex[b & 0xF]; -const eq = (b) => (((b & 0xF0) >> 4) === (b & 0xF)); -function isShort(v) { - return eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a); +function isValidKey(key) { + return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1; } -function hexParse(str) { - var len = str.length; - var ret; - if (str[0] === '#') { - if (len === 4 || len === 5) { - ret = { - r: 255 & map$1[str[1]] * 17, - g: 255 & map$1[str[2]] * 17, - b: 255 & map$1[str[3]] * 17, - a: len === 5 ? map$1[str[4]] * 17 : 255 - }; - } else if (len === 7 || len === 9) { - ret = { - r: map$1[str[1]] << 4 | map$1[str[2]], - g: map$1[str[3]] << 4 | map$1[str[4]], - b: map$1[str[5]] << 4 | map$1[str[6]], - a: len === 9 ? (map$1[str[7]] << 4 | map$1[str[8]]) : 255 - }; - } - } - return ret; -} -function hexString(v) { - var f = isShort(v) ? h1 : h2; - return v - ? '#' + f(v.r) + f(v.g) + f(v.b) + (v.a < 255 ? f(v.a) : '') - : v; -} -function round(v) { - return v + 0.5 | 0; -} -const lim = (v, l, h) => Math.max(Math.min(v, h), l); -function p2b(v) { - return lim(round(v * 2.55), 0, 255); -} -function n2b(v) { - return lim(round(v * 255), 0, 255); -} -function b2n(v) { - return lim(round(v / 2.55) / 100, 0, 1); -} -function n2p(v) { - return lim(round(v * 100), 0, 100); -} -const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/; -function rgbParse(str) { - const m = RGB_RE.exec(str); - let a = 255; - let r, g, b; - if (!m) { - return; - } - if (m[7] !== r) { - const v = +m[7]; - a = 255 & (m[8] ? p2b(v) : v * 255); - } - r = +m[1]; - g = +m[3]; - b = +m[5]; - r = 255 & (m[2] ? p2b(r) : r); - g = 255 & (m[4] ? p2b(g) : g); - b = 255 & (m[6] ? p2b(b) : b); - return { - r: r, - g: g, - b: b, - a: a - }; -} -function rgbString(v) { - return v && ( - v.a < 255 - ? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})` - : `rgb(${v.r}, ${v.g}, ${v.b})` - ); -} -const HUE_RE = /^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/; -function hsl2rgbn(h, s, l) { - const a = s * Math.min(l, 1 - l); - const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); - return [f(0), f(8), f(4)]; -} -function hsv2rgbn(h, s, v) { - const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0); - return [f(5), f(3), f(1)]; -} -function hwb2rgbn(h, w, b) { - const rgb = hsl2rgbn(h, 1, 0.5); - let i; - if (w + b > 1) { - i = 1 / (w + b); - w *= i; - b *= i; - } - for (i = 0; i < 3; i++) { - rgb[i] *= 1 - w - b; - rgb[i] += w; - } - return rgb; -} -function rgb2hsl(v) { - const range = 255; - const r = v.r / range; - const g = v.g / range; - const b = v.b / range; - const max = Math.max(r, g, b); - const min = Math.min(r, g, b); - const l = (max + min) / 2; - let h, s, d; - if (max !== min) { - d = max - min; - s = l > 0.5 ? d / (2 - max - min) : d / (max + min); - h = max === r - ? ((g - b) / d) + (g < b ? 6 : 0) - : max === g - ? (b - r) / d + 2 - : (r - g) / d + 4; - h = h * 60 + 0.5; - } - return [h | 0, s || 0, l]; -} -function calln(f, a, b, c) { - return ( - Array.isArray(a) - ? f(a[0], a[1], a[2]) - : f(a, b, c) - ).map(n2b); -} -function hsl2rgb(h, s, l) { - return calln(hsl2rgbn, h, s, l); -} -function hwb2rgb(h, w, b) { - return calln(hwb2rgbn, h, w, b); -} -function hsv2rgb(h, s, v) { - return calln(hsv2rgbn, h, s, v); -} -function hue(h) { - return (h % 360 + 360) % 360; -} -function hueParse(str) { - const m = HUE_RE.exec(str); - let a = 255; - let v; - if (!m) { - return; - } - if (m[5] !== v) { - a = m[6] ? p2b(+m[5]) : n2b(+m[5]); - } - const h = hue(+m[2]); - const p1 = +m[3] / 100; - const p2 = +m[4] / 100; - if (m[1] === 'hwb') { - v = hwb2rgb(h, p1, p2); - } else if (m[1] === 'hsv') { - v = hsv2rgb(h, p1, p2); - } else { - v = hsl2rgb(h, p1, p2); - } - return { - r: v[0], - g: v[1], - b: v[2], - a: a - }; -} -function rotate(v, deg) { - var h = rgb2hsl(v); - h[0] = hue(h[0] + deg); - h = hsl2rgb(h); - v.r = h[0]; - v.g = h[1]; - v.b = h[2]; -} -function hslString(v) { - if (!v) { - return; - } - const a = rgb2hsl(v); - const h = a[0]; - const s = n2p(a[1]); - const l = n2p(a[2]); - return v.a < 255 - ? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})` - : `hsl(${h}, ${s}%, ${l}%)`; -} -const map$1$1 = { - x: 'dark', - Z: 'light', - Y: 're', - X: 'blu', - W: 'gr', - V: 'medium', - U: 'slate', - A: 'ee', - T: 'ol', - S: 'or', - B: 'ra', - C: 'lateg', - D: 'ights', - R: 'in', - Q: 'turquois', - E: 'hi', - P: 'ro', - O: 'al', - N: 'le', - M: 'de', - L: 'yello', - F: 'en', - K: 'ch', - G: 'arks', - H: 'ea', - I: 'ightg', - J: 'wh' -}; -const names = { - OiceXe: 'f0f8ff', - antiquewEte: 'faebd7', - aqua: 'ffff', - aquamarRe: '7fffd4', - azuY: 'f0ffff', - beige: 'f5f5dc', - bisque: 'ffe4c4', - black: '0', - blanKedOmond: 'ffebcd', - Xe: 'ff', - XeviTet: '8a2be2', - bPwn: 'a52a2a', - burlywood: 'deb887', - caMtXe: '5f9ea0', - KartYuse: '7fff00', - KocTate: 'd2691e', - cSO: 'ff7f50', - cSnflowerXe: '6495ed', - cSnsilk: 'fff8dc', - crimson: 'dc143c', - cyan: 'ffff', - xXe: '8b', - xcyan: '8b8b', - xgTMnPd: 'b8860b', - xWay: 'a9a9a9', - xgYF: '6400', - xgYy: 'a9a9a9', - xkhaki: 'bdb76b', - xmagFta: '8b008b', - xTivegYF: '556b2f', - xSange: 'ff8c00', - xScEd: '9932cc', - xYd: '8b0000', - xsOmon: 'e9967a', - xsHgYF: '8fbc8f', - xUXe: '483d8b', - xUWay: '2f4f4f', - xUgYy: '2f4f4f', - xQe: 'ced1', - xviTet: '9400d3', - dAppRk: 'ff1493', - dApskyXe: 'bfff', - dimWay: '696969', - dimgYy: '696969', - dodgerXe: '1e90ff', - fiYbrick: 'b22222', - flSOwEte: 'fffaf0', - foYstWAn: '228b22', - fuKsia: 'ff00ff', - gaRsbSo: 'dcdcdc', - ghostwEte: 'f8f8ff', - gTd: 'ffd700', - gTMnPd: 'daa520', - Way: '808080', - gYF: '8000', - gYFLw: 'adff2f', - gYy: '808080', - honeyMw: 'f0fff0', - hotpRk: 'ff69b4', - RdianYd: 'cd5c5c', - Rdigo: '4b0082', - ivSy: 'fffff0', - khaki: 'f0e68c', - lavFMr: 'e6e6fa', - lavFMrXsh: 'fff0f5', - lawngYF: '7cfc00', - NmoncEffon: 'fffacd', - ZXe: 'add8e6', - ZcSO: 'f08080', - Zcyan: 'e0ffff', - ZgTMnPdLw: 'fafad2', - ZWay: 'd3d3d3', - ZgYF: '90ee90', - ZgYy: 'd3d3d3', - ZpRk: 'ffb6c1', - ZsOmon: 'ffa07a', - ZsHgYF: '20b2aa', - ZskyXe: '87cefa', - ZUWay: '778899', - ZUgYy: '778899', - ZstAlXe: 'b0c4de', - ZLw: 'ffffe0', - lime: 'ff00', - limegYF: '32cd32', - lRF: 'faf0e6', - magFta: 'ff00ff', - maPon: '800000', - VaquamarRe: '66cdaa', - VXe: 'cd', - VScEd: 'ba55d3', - VpurpN: '9370db', - VsHgYF: '3cb371', - VUXe: '7b68ee', - VsprRggYF: 'fa9a', - VQe: '48d1cc', - VviTetYd: 'c71585', - midnightXe: '191970', - mRtcYam: 'f5fffa', - mistyPse: 'ffe4e1', - moccasR: 'ffe4b5', - navajowEte: 'ffdead', - navy: '80', - Tdlace: 'fdf5e6', - Tive: '808000', - TivedBb: '6b8e23', - Sange: 'ffa500', - SangeYd: 'ff4500', - ScEd: 'da70d6', - pOegTMnPd: 'eee8aa', - pOegYF: '98fb98', - pOeQe: 'afeeee', - pOeviTetYd: 'db7093', - papayawEp: 'ffefd5', - pHKpuff: 'ffdab9', - peru: 'cd853f', - pRk: 'ffc0cb', - plum: 'dda0dd', - powMrXe: 'b0e0e6', - purpN: '800080', - YbeccapurpN: '663399', - Yd: 'ff0000', - Psybrown: 'bc8f8f', - PyOXe: '4169e1', - saddNbPwn: '8b4513', - sOmon: 'fa8072', - sandybPwn: 'f4a460', - sHgYF: '2e8b57', - sHshell: 'fff5ee', - siFna: 'a0522d', - silver: 'c0c0c0', - skyXe: '87ceeb', - UXe: '6a5acd', - UWay: '708090', - UgYy: '708090', - snow: 'fffafa', - sprRggYF: 'ff7f', - stAlXe: '4682b4', - tan: 'd2b48c', - teO: '8080', - tEstN: 'd8bfd8', - tomato: 'ff6347', - Qe: '40e0d0', - viTet: 'ee82ee', - JHt: 'f5deb3', - wEte: 'ffffff', - wEtesmoke: 'f5f5f5', - Lw: 'ffff00', - LwgYF: '9acd32' -}; -function unpack() { - const unpacked = {}; - const keys = Object.keys(names); - const tkeys = Object.keys(map$1$1); - let i, j, k, ok, nk; - for (i = 0; i < keys.length; i++) { - ok = nk = keys[i]; - for (j = 0; j < tkeys.length; j++) { - k = tkeys[j]; - nk = nk.replace(k, map$1$1[k]); - } - k = parseInt(names[ok], 16); - unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF]; - } - return unpacked; -} -let names$1; -function nameParse(str) { - if (!names$1) { - names$1 = unpack(); - names$1.transparent = [0, 0, 0, 0]; - } - const a = names$1[str.toLowerCase()]; - return a && { - r: a[0], - g: a[1], - b: a[2], - a: a.length === 4 ? a[3] : 255 - }; -} -function modHSL(v, i, ratio) { - if (v) { - let tmp = rgb2hsl(v); - tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1)); - tmp = hsl2rgb(tmp); - v.r = tmp[0]; - v.g = tmp[1]; - v.b = tmp[2]; - } -} -function clone$1(v, proto) { - return v ? Object.assign(proto || {}, v) : v; -} -function fromObject(input) { - var v = {r: 0, g: 0, b: 0, a: 255}; - if (Array.isArray(input)) { - if (input.length >= 3) { - v = {r: input[0], g: input[1], b: input[2], a: 255}; - if (input.length > 3) { - v.a = n2b(input[3]); - } - } - } else { - v = clone$1(input, {r: 0, g: 0, b: 0, a: 1}); - v.a = n2b(v.a); - } - return v; -} -function functionParse(str) { - if (str.charAt(0) === 'r') { - return rgbParse(str); - } - return hueParse(str); -} -class Color { - constructor(input) { - if (input instanceof Color) { - return input; - } - const type = typeof input; - let v; - if (type === 'object') { - v = fromObject(input); - } else if (type === 'string') { - v = hexParse(input) || nameParse(input) || functionParse(input); - } - this._rgb = v; - this._valid = !!v; - } - get valid() { - return this._valid; - } - get rgb() { - var v = clone$1(this._rgb); - if (v) { - v.a = b2n(v.a); - } - return v; - } - set rgb(obj) { - this._rgb = fromObject(obj); - } - rgbString() { - return this._valid ? rgbString(this._rgb) : this._rgb; - } - hexString() { - return this._valid ? hexString(this._rgb) : this._rgb; - } - hslString() { - return this._valid ? hslString(this._rgb) : this._rgb; - } - mix(color, weight) { - const me = this; - if (color) { - const c1 = me.rgb; - const c2 = color.rgb; - let w2; - const p = weight === w2 ? 0.5 : weight; - const w = 2 * p - 1; - const a = c1.a - c2.a; - const w1 = ((w * a === -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0; - w2 = 1 - w1; - c1.r = 0xFF & w1 * c1.r + w2 * c2.r + 0.5; - c1.g = 0xFF & w1 * c1.g + w2 * c2.g + 0.5; - c1.b = 0xFF & w1 * c1.b + w2 * c2.b + 0.5; - c1.a = p * c1.a + (1 - p) * c2.a; - me.rgb = c1; - } - return me; - } - clone() { - return new Color(this.rgb); - } - alpha(a) { - this._rgb.a = n2b(a); - return this; - } - clearer(ratio) { - const rgb = this._rgb; - rgb.a *= 1 - ratio; - return this; - } - greyscale() { - const rgb = this._rgb; - const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11); - rgb.r = rgb.g = rgb.b = val; - return this; - } - opaquer(ratio) { - const rgb = this._rgb; - rgb.a *= 1 + ratio; - return this; - } - negate() { - const v = this._rgb; - v.r = 255 - v.r; - v.g = 255 - v.g; - v.b = 255 - v.b; - return this; - } - lighten(ratio) { - modHSL(this._rgb, 2, ratio); - return this; - } - darken(ratio) { - modHSL(this._rgb, 2, -ratio); - return this; - } - saturate(ratio) { - modHSL(this._rgb, 1, ratio); - return this; - } - desaturate(ratio) { - modHSL(this._rgb, 1, -ratio); - return this; - } - rotate(deg) { - rotate(this._rgb, deg); - return this; - } -} -function index_esm(input) { - return new Color(input); -} - -const isPatternOrGradient = (value) => value instanceof CanvasGradient || value instanceof CanvasPattern; -function color(value) { - return isPatternOrGradient(value) ? value : index_esm(value); -} -function getHoverColor(value) { - return isPatternOrGradient(value) - ? value - : index_esm(value).saturate(0.5).darken(0.1).hexString(); -} - -function noop() {} -const uid = (function() { - let id = 0; - return function() { - return id++; - }; -}()); -function isNullOrUndef(value) { - return value === null || typeof value === 'undefined'; -} -function isArray(value) { - if (Array.isArray && Array.isArray(value)) { - return true; - } - const type = Object.prototype.toString.call(value); - if (type.substr(0, 7) === '[object' && type.substr(-6) === 'Array]') { - return true; - } - return false; -} -function isObject(value) { - return value !== null && Object.prototype.toString.call(value) === '[object Object]'; -} -const isNumberFinite = (value) => (typeof value === 'number' || value instanceof Number) && isFinite(+value); -function finiteOrDefault(value, defaultValue) { - return isNumberFinite(value) ? value : defaultValue; -} -function valueOrDefault(value, defaultValue) { - return typeof value === 'undefined' ? defaultValue : value; -} -const toPercentage = (value, dimension) => - typeof value === 'string' && value.endsWith('%') ? - parseFloat(value) / 100 - : value / dimension; -const toDimension = (value, dimension) => - typeof value === 'string' && value.endsWith('%') ? - parseFloat(value) / 100 * dimension - : +value; -function callback(fn, args, thisArg) { - if (fn && typeof fn.call === 'function') { - return fn.apply(thisArg, args); - } -} -function each(loopable, fn, thisArg, reverse) { - let i, len, keys; - if (isArray(loopable)) { - len = loopable.length; - if (reverse) { - for (i = len - 1; i >= 0; i--) { - fn.call(thisArg, loopable[i], i); - } - } else { - for (i = 0; i < len; i++) { - fn.call(thisArg, loopable[i], i); - } - } - } else if (isObject(loopable)) { - keys = Object.keys(loopable); - len = keys.length; - for (i = 0; i < len; i++) { - fn.call(thisArg, loopable[keys[i]], keys[i]); - } - } -} -function _elementsEqual(a0, a1) { - let i, ilen, v0, v1; - if (!a0 || !a1 || a0.length !== a1.length) { - return false; - } - for (i = 0, ilen = a0.length; i < ilen; ++i) { - v0 = a0[i]; - v1 = a1[i]; - if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) { - return false; - } - } - return true; -} -function clone(source) { - if (isArray(source)) { - return source.map(clone); - } - if (isObject(source)) { - const target = Object.create(null); - const keys = Object.keys(source); - const klen = keys.length; - let k = 0; - for (; k < klen; ++k) { - target[keys[k]] = clone(source[keys[k]]); - } - return target; - } - return source; -} -function isValidKey(key) { - return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1; -} -function _merger(key, target, source, options) { - if (!isValidKey(key)) { - return; - } - const tval = target[key]; - const sval = source[key]; - if (isObject(tval) && isObject(sval)) { - merge(tval, sval, options); - } else { - target[key] = clone(sval); - } +function _merger(key, target, source, options) { + if (!isValidKey(key)) { + return; + } + const tval = target[key]; + const sval = source[key]; + if (isObject(tval) && isObject(sval)) { + merge(tval, sval, options); + } else { + target[key] = clone$1(sval); + } } function merge(target, source, options) { const sources = isArray(source) ? source : [source]; @@ -898,7 +151,7 @@ function _mergerIf(key, target, source) { if (isObject(tval) && isObject(sval)) { mergeIf(tval, sval); } else if (!Object.prototype.hasOwnProperty.call(target, key)) { - target[key] = clone(sval); + target[key] = clone$1(sval); } } function _deprecated(scope, value, previous, current) { @@ -907,24 +160,41 @@ function _deprecated(scope, value, previous, current) { '" is deprecated. Please use "' + current + '" instead'); } } -const emptyString = ''; -const dot = '.'; -function indexOfDotOrLength(key, start) { - const idx = key.indexOf(dot, start); - return idx === -1 ? key.length : idx; -} +const keyResolvers = { + '': v => v, + x: o => o.x, + y: o => o.y +}; function resolveObjectKey(obj, key) { - if (key === emptyString) { + const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); + return resolver(obj); +} +function _getKeyResolver(key) { + const keys = _splitKey(key); + return obj => { + for (const k of keys) { + if (k === '') { + break; + } + obj = obj && obj[k]; + } return obj; + }; +} +function _splitKey(key) { + const parts = key.split('.'); + const keys = []; + let tmp = ''; + for (const part of parts) { + tmp += part; + if (tmp.endsWith('\\')) { + tmp = tmp.slice(0, -1) + '.'; + } else { + keys.push(tmp); + tmp = ''; + } } - let pos = 0; - let idx = indexOfDotOrLength(key, pos); - while (obj && idx > pos) { - obj = obj[key.substr(pos, idx - pos)]; - pos = idx + 1; - idx = indexOfDotOrLength(key, pos); - } - return obj; + return keys; } function _capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); @@ -946,171 +216,58 @@ function _isClickEvent(e) { return e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu'; } -const overrides = Object.create(null); -const descriptors = Object.create(null); -function getScope$1(node, key) { - if (!key) { - return node; +const PI = Math.PI; +const TAU = 2 * PI; +const PITAU = TAU + PI; +const INFINITY = Number.POSITIVE_INFINITY; +const RAD_PER_DEG = PI / 180; +const HALF_PI = PI / 2; +const QUARTER_PI = PI / 4; +const TWO_THIRDS_PI = PI * 2 / 3; +const log10 = Math.log10; +const sign = Math.sign; +function niceNum(range) { + const roundedRange = Math.round(range); + range = almostEquals(range, roundedRange, range / 1000) ? roundedRange : range; + const niceRange = Math.pow(10, Math.floor(log10(range))); + const fraction = range / niceRange; + const niceFraction = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10; + return niceFraction * niceRange; +} +function _factorize(value) { + const result = []; + const sqrt = Math.sqrt(value); + let i; + for (i = 1; i < sqrt; i++) { + if (value % i === 0) { + result.push(i); + result.push(value / i); + } } - const keys = key.split('.'); - for (let i = 0, n = keys.length; i < n; ++i) { - const k = keys[i]; - node = node[k] || (node[k] = Object.create(null)); + if (sqrt === (sqrt | 0)) { + result.push(sqrt); } - return node; + result.sort((a, b) => a - b).pop(); + return result; } -function set(root, scope, values) { - if (typeof scope === 'string') { - return merge(getScope$1(root, scope), values); - } - return merge(getScope$1(root, ''), scope); -} -class Defaults { - constructor(_descriptors) { - this.animation = undefined; - this.backgroundColor = 'rgba(0,0,0,0.1)'; - this.borderColor = 'rgba(0,0,0,0.1)'; - this.color = '#666'; - this.datasets = {}; - this.devicePixelRatio = (context) => context.chart.platform.getDevicePixelRatio(); - this.elements = {}; - this.events = [ - 'mousemove', - 'mouseout', - 'click', - 'touchstart', - 'touchmove' - ]; - this.font = { - family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", - size: 12, - style: 'normal', - lineHeight: 1.2, - weight: null - }; - this.hover = {}; - this.hoverBackgroundColor = (ctx, options) => getHoverColor(options.backgroundColor); - this.hoverBorderColor = (ctx, options) => getHoverColor(options.borderColor); - this.hoverColor = (ctx, options) => getHoverColor(options.color); - this.indexAxis = 'x'; - this.interaction = { - mode: 'nearest', - intersect: true - }; - this.maintainAspectRatio = true; - this.onHover = null; - this.onClick = null; - this.parsing = true; - this.plugins = {}; - this.responsive = true; - this.scale = undefined; - this.scales = {}; - this.showLine = true; - this.drawActiveElementsOnTop = true; - this.describe(_descriptors); - } - set(scope, values) { - return set(this, scope, values); - } - get(scope) { - return getScope$1(this, scope); - } - describe(scope, values) { - return set(descriptors, scope, values); - } - override(scope, values) { - return set(overrides, scope, values); - } - route(scope, name, targetScope, targetName) { - const scopeObject = getScope$1(this, scope); - const targetScopeObject = getScope$1(this, targetScope); - const privateName = '_' + name; - Object.defineProperties(scopeObject, { - [privateName]: { - value: scopeObject[name], - writable: true - }, - [name]: { - enumerable: true, - get() { - const local = this[privateName]; - const target = targetScopeObject[targetName]; - if (isObject(local)) { - return Object.assign({}, target, local); - } - return valueOrDefault(local, target); - }, - set(value) { - this[privateName] = value; - } - } - }); - } -} -var defaults = new Defaults({ - _scriptable: (name) => !name.startsWith('on'), - _indexable: (name) => name !== 'events', - hover: { - _fallback: 'interaction' - }, - interaction: { - _scriptable: false, - _indexable: false, - } -}); - -const PI = Math.PI; -const TAU = 2 * PI; -const PITAU = TAU + PI; -const INFINITY = Number.POSITIVE_INFINITY; -const RAD_PER_DEG = PI / 180; -const HALF_PI = PI / 2; -const QUARTER_PI = PI / 4; -const TWO_THIRDS_PI = PI * 2 / 3; -const log10 = Math.log10; -const sign = Math.sign; -function niceNum(range) { - const roundedRange = Math.round(range); - range = almostEquals(range, roundedRange, range / 1000) ? roundedRange : range; - const niceRange = Math.pow(10, Math.floor(log10(range))); - const fraction = range / niceRange; - const niceFraction = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10; - return niceFraction * niceRange; -} -function _factorize(value) { - const result = []; - const sqrt = Math.sqrt(value); - let i; - for (i = 1; i < sqrt; i++) { - if (value % i === 0) { - result.push(i); - result.push(value / i); - } - } - if (sqrt === (sqrt | 0)) { - result.push(sqrt); - } - result.sort((a, b) => a - b).pop(); - return result; -} -function isNumber(n) { - return !isNaN(parseFloat(n)) && isFinite(n); -} -function almostEquals(x, y, epsilon) { - return Math.abs(x - y) < epsilon; -} -function almostWhole(x, epsilon) { - const rounded = Math.round(x); - return ((rounded - epsilon) <= x) && ((rounded + epsilon) >= x); -} -function _setMinAndMaxByKey(array, target, property) { - let i, ilen, value; - for (i = 0, ilen = array.length; i < ilen; i++) { - value = array[i][property]; - if (!isNaN(value)) { - target.min = Math.min(target.min, value); - target.max = Math.max(target.max, value); - } +function isNumber(n) { + return !isNaN(parseFloat(n)) && isFinite(n); +} +function almostEquals(x, y, epsilon) { + return Math.abs(x - y) < epsilon; +} +function almostWhole(x, epsilon) { + const rounded = Math.round(x); + return ((rounded - epsilon) <= x) && ((rounded + epsilon) >= x); +} +function _setMinAndMaxByKey(array, target, property) { + let i, ilen, value; + for (i = 0, ilen = array.length; i < ilen; i++) { + value = array[i][property]; + if (!isNaN(value)) { + target.min = Math.min(target.min, value); + target.max = Math.max(target.max, value); + } } } function toRadians(degrees) { @@ -1174,2019 +331,2473 @@ function _isBetween(value, start, end, epsilon = 1e-6) { return value >= Math.min(start, end) - epsilon && value <= Math.max(start, end) + epsilon; } -function toFontString(font) { - if (!font || isNullOrUndef(font.size) || isNullOrUndef(font.family)) { - return null; +function _lookup(table, value, cmp) { + cmp = cmp || ((index) => table[index] < value); + let hi = table.length - 1; + let lo = 0; + let mid; + while (hi - lo > 1) { + mid = (lo + hi) >> 1; + if (cmp(mid)) { + lo = mid; + } else { + hi = mid; + } } - return (font.style ? font.style + ' ' : '') - + (font.weight ? font.weight + ' ' : '') - + font.size + 'px ' - + font.family; + return {lo, hi}; } -function _measureText(ctx, data, gc, longest, string) { - let textWidth = data[string]; - if (!textWidth) { - textWidth = data[string] = ctx.measureText(string).width; - gc.push(string); +const _lookupByKey = (table, key, value, last) => + _lookup(table, value, last + ? index => table[index][key] <= value + : index => table[index][key] < value); +const _rlookupByKey = (table, key, value) => + _lookup(table, value, index => table[index][key] >= value); +function _filterBetween(values, min, max) { + let start = 0; + let end = values.length; + while (start < end && values[start] < min) { + start++; } - if (textWidth > longest) { - longest = textWidth; + while (end > start && values[end - 1] > max) { + end--; } - return longest; + return start > 0 || end < values.length + ? values.slice(start, end) + : values; } -function _longestText(ctx, font, arrayOfThings, cache) { - cache = cache || {}; - let data = cache.data = cache.data || {}; - let gc = cache.garbageCollect = cache.garbageCollect || []; - if (cache.font !== font) { - data = cache.data = {}; - gc = cache.garbageCollect = []; - cache.font = font; +const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift']; +function listenArrayEvents(array, listener) { + if (array._chartjs) { + array._chartjs.listeners.push(listener); + return; } - ctx.save(); - ctx.font = font; - let longest = 0; - const ilen = arrayOfThings.length; - let i, j, jlen, thing, nestedThing; - for (i = 0; i < ilen; i++) { - thing = arrayOfThings[i]; - if (thing !== undefined && thing !== null && isArray(thing) !== true) { - longest = _measureText(ctx, data, gc, longest, thing); - } else if (isArray(thing)) { - for (j = 0, jlen = thing.length; j < jlen; j++) { - nestedThing = thing[j]; - if (nestedThing !== undefined && nestedThing !== null && !isArray(nestedThing)) { - longest = _measureText(ctx, data, gc, longest, nestedThing); - } - } + Object.defineProperty(array, '_chartjs', { + configurable: true, + enumerable: false, + value: { + listeners: [listener] } + }); + arrayEvents.forEach((key) => { + const method = '_onData' + _capitalize(key); + const base = array[key]; + Object.defineProperty(array, key, { + configurable: true, + enumerable: false, + value(...args) { + const res = base.apply(this, args); + array._chartjs.listeners.forEach((object) => { + if (typeof object[method] === 'function') { + object[method](...args); + } + }); + return res; + } + }); + }); +} +function unlistenArrayEvents(array, listener) { + const stub = array._chartjs; + if (!stub) { + return; } - ctx.restore(); - const gcLen = gc.length / 2; - if (gcLen > arrayOfThings.length) { - for (i = 0; i < gcLen; i++) { - delete data[gc[i]]; - } - gc.splice(0, gcLen); + const listeners = stub.listeners; + const index = listeners.indexOf(listener); + if (index !== -1) { + listeners.splice(index, 1); } - return longest; + if (listeners.length > 0) { + return; + } + arrayEvents.forEach((key) => { + delete array[key]; + }); + delete array._chartjs; } -function _alignPixel(chart, pixel, width) { - const devicePixelRatio = chart.currentDevicePixelRatio; - const halfWidth = width !== 0 ? Math.max(width / 2, 0.5) : 0; - return Math.round((pixel - halfWidth) * devicePixelRatio) / devicePixelRatio + halfWidth; -} -function clearCanvas(canvas, ctx) { - ctx = ctx || canvas.getContext('2d'); - ctx.save(); - ctx.resetTransform(); - ctx.clearRect(0, 0, canvas.width, canvas.height); - ctx.restore(); -} -function drawPoint(ctx, options, x, y) { - let type, xOffset, yOffset, size, cornerRadius; - const style = options.pointStyle; - const rotation = options.rotation; - const radius = options.radius; - let rad = (rotation || 0) * RAD_PER_DEG; - if (style && typeof style === 'object') { - type = style.toString(); - if (type === '[object HTMLImageElement]' || type === '[object HTMLCanvasElement]') { - ctx.save(); - ctx.translate(x, y); - ctx.rotate(rad); - ctx.drawImage(style, -style.width / 2, -style.height / 2, style.width, style.height); - ctx.restore(); - return; - } - } - if (isNaN(radius) || radius <= 0) { - return; - } - ctx.beginPath(); - switch (style) { - default: - ctx.arc(x, y, radius, 0, TAU); - ctx.closePath(); - break; - case 'triangle': - ctx.moveTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); - rad += TWO_THIRDS_PI; - ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); - rad += TWO_THIRDS_PI; - ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); - ctx.closePath(); - break; - case 'rectRounded': - cornerRadius = radius * 0.516; - size = radius - cornerRadius; - xOffset = Math.cos(rad + QUARTER_PI) * size; - yOffset = Math.sin(rad + QUARTER_PI) * size; - ctx.arc(x - xOffset, y - yOffset, cornerRadius, rad - PI, rad - HALF_PI); - ctx.arc(x + yOffset, y - xOffset, cornerRadius, rad - HALF_PI, rad); - ctx.arc(x + xOffset, y + yOffset, cornerRadius, rad, rad + HALF_PI); - ctx.arc(x - yOffset, y + xOffset, cornerRadius, rad + HALF_PI, rad + PI); - ctx.closePath(); - break; - case 'rect': - if (!rotation) { - size = Math.SQRT1_2 * radius; - ctx.rect(x - size, y - size, 2 * size, 2 * size); - break; - } - rad += QUARTER_PI; - case 'rectRot': - xOffset = Math.cos(rad) * radius; - yOffset = Math.sin(rad) * radius; - ctx.moveTo(x - xOffset, y - yOffset); - ctx.lineTo(x + yOffset, y - xOffset); - ctx.lineTo(x + xOffset, y + yOffset); - ctx.lineTo(x - yOffset, y + xOffset); - ctx.closePath(); - break; - case 'crossRot': - rad += QUARTER_PI; - case 'cross': - xOffset = Math.cos(rad) * radius; - yOffset = Math.sin(rad) * radius; - ctx.moveTo(x - xOffset, y - yOffset); - ctx.lineTo(x + xOffset, y + yOffset); - ctx.moveTo(x + yOffset, y - xOffset); - ctx.lineTo(x - yOffset, y + xOffset); - break; - case 'star': - xOffset = Math.cos(rad) * radius; - yOffset = Math.sin(rad) * radius; - ctx.moveTo(x - xOffset, y - yOffset); - ctx.lineTo(x + xOffset, y + yOffset); - ctx.moveTo(x + yOffset, y - xOffset); - ctx.lineTo(x - yOffset, y + xOffset); - rad += QUARTER_PI; - xOffset = Math.cos(rad) * radius; - yOffset = Math.sin(rad) * radius; - ctx.moveTo(x - xOffset, y - yOffset); - ctx.lineTo(x + xOffset, y + yOffset); - ctx.moveTo(x + yOffset, y - xOffset); - ctx.lineTo(x - yOffset, y + xOffset); - break; - case 'line': - xOffset = Math.cos(rad) * radius; - yOffset = Math.sin(rad) * radius; - ctx.moveTo(x - xOffset, y - yOffset); - ctx.lineTo(x + xOffset, y + yOffset); - break; - case 'dash': - ctx.moveTo(x, y); - ctx.lineTo(x + Math.cos(rad) * radius, y + Math.sin(rad) * radius); - break; +function _arrayUnique(items) { + const set = new Set(); + let i, ilen; + for (i = 0, ilen = items.length; i < ilen; ++i) { + set.add(items[i]); } - ctx.fill(); - if (options.borderWidth > 0) { - ctx.stroke(); + if (set.size === ilen) { + return items; } + return Array.from(set); } -function _isPointInArea(point, area, margin) { - margin = margin || 0.5; - return !area || (point && point.x > area.left - margin && point.x < area.right + margin && - point.y > area.top - margin && point.y < area.bottom + margin); -} -function clipArea(ctx, area) { - ctx.save(); - ctx.beginPath(); - ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); - ctx.clip(); -} -function unclipArea(ctx) { - ctx.restore(); + +function fontString(pixelSize, fontStyle, fontFamily) { + return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; } -function _steppedLineTo(ctx, previous, target, flip, mode) { - if (!previous) { - return ctx.lineTo(target.x, target.y); - } - if (mode === 'middle') { - const midpoint = (previous.x + target.x) / 2.0; - ctx.lineTo(midpoint, previous.y); - ctx.lineTo(midpoint, target.y); - } else if (mode === 'after' !== !!flip) { - ctx.lineTo(previous.x, target.y); - } else { - ctx.lineTo(target.x, previous.y); +const requestAnimFrame = (function() { + if (typeof window === 'undefined') { + return function(callback) { + return callback(); + }; } - ctx.lineTo(target.x, target.y); + return window.requestAnimationFrame; +}()); +function throttled(fn, thisArg, updateFn) { + const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args)); + let ticking = false; + let args = []; + return function(...rest) { + args = updateArgs(rest); + if (!ticking) { + ticking = true; + requestAnimFrame.call(window, () => { + ticking = false; + fn.apply(thisArg, args); + }); + } + }; } -function _bezierCurveTo(ctx, previous, target, flip) { - if (!previous) { - return ctx.lineTo(target.x, target.y); - } - ctx.bezierCurveTo( - flip ? previous.cp1x : previous.cp2x, - flip ? previous.cp1y : previous.cp2y, - flip ? target.cp2x : target.cp1x, - flip ? target.cp2y : target.cp1y, - target.x, - target.y); +function debounce(fn, delay) { + let timeout; + return function(...args) { + if (delay) { + clearTimeout(timeout); + timeout = setTimeout(fn, delay, args); + } else { + fn.apply(this, args); + } + return delay; + }; } -function renderText(ctx, text, x, y, font, opts = {}) { - const lines = isArray(text) ? text : [text]; - const stroke = opts.strokeWidth > 0 && opts.strokeColor !== ''; - let i, line; - ctx.save(); - ctx.font = font.string; - setRenderOpts(ctx, opts); - for (i = 0; i < lines.length; ++i) { - line = lines[i]; - if (stroke) { - if (opts.strokeColor) { - ctx.strokeStyle = opts.strokeColor; - } - if (!isNullOrUndef(opts.strokeWidth)) { - ctx.lineWidth = opts.strokeWidth; - } - ctx.strokeText(line, x, y, opts.maxWidth); +const _toLeftRightCenter = (align) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; +const _alignStartEnd = (align, start, end) => align === 'start' ? start : align === 'end' ? end : (start + end) / 2; +const _textX = (align, left, right, rtl) => { + const check = rtl ? 'left' : 'right'; + return align === check ? right : align === 'center' ? (left + right) / 2 : left; +}; +function _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { + const pointCount = points.length; + let start = 0; + let count = pointCount; + if (meta._sorted) { + const {iScale, _parsed} = meta; + const axis = iScale.axis; + const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); + if (minDefined) { + start = _limitValue(Math.min( + _lookupByKey(_parsed, iScale.axis, min).lo, + animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo), + 0, pointCount - 1); + } + if (maxDefined) { + count = _limitValue(Math.max( + _lookupByKey(_parsed, iScale.axis, max, true).hi + 1, + animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max), true).hi + 1), + start, pointCount) - start; + } else { + count = pointCount - start; } - ctx.fillText(line, x, y, opts.maxWidth); - decorateText(ctx, x, y, line, opts); - y += font.lineHeight; } - ctx.restore(); + return {start, count}; } -function setRenderOpts(ctx, opts) { - if (opts.translation) { - ctx.translate(opts.translation[0], opts.translation[1]); - } - if (!isNullOrUndef(opts.rotation)) { - ctx.rotate(opts.rotation); - } - if (opts.color) { - ctx.fillStyle = opts.color; - } - if (opts.textAlign) { - ctx.textAlign = opts.textAlign; +function _scaleRangesChanged(meta) { + const {xScale, yScale, _scaleRanges} = meta; + const newRanges = { + xmin: xScale.min, + xmax: xScale.max, + ymin: yScale.min, + ymax: yScale.max + }; + if (!_scaleRanges) { + meta._scaleRanges = newRanges; + return true; } - if (opts.textBaseline) { - ctx.textBaseline = opts.textBaseline; - } -} -function decorateText(ctx, x, y, line, opts) { - if (opts.strikethrough || opts.underline) { - const metrics = ctx.measureText(line); - const left = x - metrics.actualBoundingBoxLeft; - const right = x + metrics.actualBoundingBoxRight; - const top = y - metrics.actualBoundingBoxAscent; - const bottom = y + metrics.actualBoundingBoxDescent; - const yDecoration = opts.strikethrough ? (top + bottom) / 2 : bottom; - ctx.strokeStyle = ctx.fillStyle; - ctx.beginPath(); - ctx.lineWidth = opts.decorationWidth || 2; - ctx.moveTo(left, yDecoration); - ctx.lineTo(right, yDecoration); - ctx.stroke(); - } -} -function addRoundedRectPath(ctx, rect) { - const {x, y, w, h, radius} = rect; - ctx.arc(x + radius.topLeft, y + radius.topLeft, radius.topLeft, -HALF_PI, PI, true); - ctx.lineTo(x, y + h - radius.bottomLeft); - ctx.arc(x + radius.bottomLeft, y + h - radius.bottomLeft, radius.bottomLeft, PI, HALF_PI, true); - ctx.lineTo(x + w - radius.bottomRight, y + h); - ctx.arc(x + w - radius.bottomRight, y + h - radius.bottomRight, radius.bottomRight, HALF_PI, 0, true); - ctx.lineTo(x + w, y + radius.topRight); - ctx.arc(x + w - radius.topRight, y + radius.topRight, radius.topRight, 0, -HALF_PI, true); - ctx.lineTo(x + radius.topLeft, y); + const changed = _scaleRanges.xmin !== xScale.min + || _scaleRanges.xmax !== xScale.max + || _scaleRanges.ymin !== yScale.min + || _scaleRanges.ymax !== yScale.max; + Object.assign(_scaleRanges, newRanges); + return changed; } -function _lookup(table, value, cmp) { - cmp = cmp || ((index) => table[index] < value); - let hi = table.length - 1; - let lo = 0; - let mid; - while (hi - lo > 1) { - mid = (lo + hi) >> 1; - if (cmp(mid)) { - lo = mid; - } else { - hi = mid; - } - } - return {lo, hi}; -} -const _lookupByKey = (table, key, value) => - _lookup(table, value, index => table[index][key] < value); -const _rlookupByKey = (table, key, value) => - _lookup(table, value, index => table[index][key] >= value); -function _filterBetween(values, min, max) { - let start = 0; - let end = values.length; - while (start < end && values[start] < min) { - start++; - } - while (end > start && values[end - 1] > max) { - end--; +class Animator { + constructor() { + this._request = null; + this._charts = new Map(); + this._running = false; + this._lastDate = undefined; } - return start > 0 || end < values.length - ? values.slice(start, end) - : values; -} -const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift']; -function listenArrayEvents(array, listener) { - if (array._chartjs) { - array._chartjs.listeners.push(listener); - return; + _notify(chart, anims, date, type) { + const callbacks = anims.listeners[type]; + const numSteps = anims.duration; + callbacks.forEach(fn => fn({ + chart, + initial: anims.initial, + numSteps, + currentStep: Math.min(date - anims.start, numSteps) + })); } - Object.defineProperty(array, '_chartjs', { - configurable: true, - enumerable: false, - value: { - listeners: [listener] + _refresh() { + if (this._request) { + return; } - }); - arrayEvents.forEach((key) => { - const method = '_onData' + _capitalize(key); - const base = array[key]; - Object.defineProperty(array, key, { - configurable: true, - enumerable: false, - value(...args) { - const res = base.apply(this, args); - array._chartjs.listeners.forEach((object) => { - if (typeof object[method] === 'function') { - object[method](...args); + this._running = true; + this._request = requestAnimFrame.call(window, () => { + this._update(); + this._request = null; + if (this._running) { + this._refresh(); + } + }); + } + _update(date = Date.now()) { + let remaining = 0; + this._charts.forEach((anims, chart) => { + if (!anims.running || !anims.items.length) { + return; + } + const items = anims.items; + let i = items.length - 1; + let draw = false; + let item; + for (; i >= 0; --i) { + item = items[i]; + if (item._active) { + if (item._total > anims.duration) { + anims.duration = item._total; } - }); - return res; + item.tick(date); + draw = true; + } else { + items[i] = items[items.length - 1]; + items.pop(); + } + } + if (draw) { + chart.draw(); + this._notify(chart, anims, date, 'progress'); + } + if (!items.length) { + anims.running = false; + this._notify(chart, anims, date, 'complete'); + anims.initial = false; } + remaining += items.length; }); - }); -} -function unlistenArrayEvents(array, listener) { - const stub = array._chartjs; - if (!stub) { - return; + this._lastDate = date; + if (remaining === 0) { + this._running = false; + } } - const listeners = stub.listeners; - const index = listeners.indexOf(listener); - if (index !== -1) { - listeners.splice(index, 1); + _getAnims(chart) { + const charts = this._charts; + let anims = charts.get(chart); + if (!anims) { + anims = { + running: false, + initial: true, + items: [], + listeners: { + complete: [], + progress: [] + } + }; + charts.set(chart, anims); + } + return anims; } - if (listeners.length > 0) { - return; + listen(chart, event, cb) { + this._getAnims(chart).listeners[event].push(cb); } - arrayEvents.forEach((key) => { - delete array[key]; - }); - delete array._chartjs; -} -function _arrayUnique(items) { - const set = new Set(); - let i, ilen; - for (i = 0, ilen = items.length; i < ilen; ++i) { - set.add(items[i]); + add(chart, items) { + if (!items || !items.length) { + return; + } + this._getAnims(chart).items.push(...items); } - if (set.size === ilen) { - return items; + has(chart) { + return this._getAnims(chart).items.length > 0; } - return Array.from(set); -} - -function _isDomSupported() { - return typeof window !== 'undefined' && typeof document !== 'undefined'; -} -function _getParentNode(domNode) { - let parent = domNode.parentNode; - if (parent && parent.toString() === '[object ShadowRoot]') { - parent = parent.host; + start(chart) { + const anims = this._charts.get(chart); + if (!anims) { + return; + } + anims.running = true; + anims.start = Date.now(); + anims.duration = anims.items.reduce((acc, cur) => Math.max(acc, cur._duration), 0); + this._refresh(); } - return parent; -} -function parseMaxStyle(styleValue, node, parentProperty) { - let valueInPixels; - if (typeof styleValue === 'string') { - valueInPixels = parseInt(styleValue, 10); - if (styleValue.indexOf('%') !== -1) { - valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty]; + running(chart) { + if (!this._running) { + return false; } - } else { - valueInPixels = styleValue; + const anims = this._charts.get(chart); + if (!anims || !anims.running || !anims.items.length) { + return false; + } + return true; + } + stop(chart) { + const anims = this._charts.get(chart); + if (!anims || !anims.items.length) { + return; + } + const items = anims.items; + let i = items.length - 1; + for (; i >= 0; --i) { + items[i].cancel(); + } + anims.items = []; + this._notify(chart, anims, Date.now(), 'complete'); + } + remove(chart) { + return this._charts.delete(chart); } - return valueInPixels; -} -const getComputedStyle = (element) => window.getComputedStyle(element, null); -function getStyle(el, property) { - return getComputedStyle(el).getPropertyValue(property); } -const positions = ['top', 'right', 'bottom', 'left']; -function getPositionedStyle(styles, style, suffix) { - const result = {}; - suffix = suffix ? '-' + suffix : ''; - for (let i = 0; i < 4; i++) { - const pos = positions[i]; - result[pos] = parseFloat(styles[style + '-' + pos + suffix]) || 0; +var animator = new Animator(); + +/*! + * @kurkle/color v0.2.1 + * https://github.com/kurkle/color#readme + * (c) 2022 Jukka Kurkela + * Released under the MIT License + */ +function round(v) { + return v + 0.5 | 0; +} +const lim = (v, l, h) => Math.max(Math.min(v, h), l); +function p2b(v) { + return lim(round(v * 2.55), 0, 255); +} +function n2b(v) { + return lim(round(v * 255), 0, 255); +} +function b2n(v) { + return lim(round(v / 2.55) / 100, 0, 1); +} +function n2p(v) { + return lim(round(v * 100), 0, 100); +} +const map$1 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15}; +const hex = [...'0123456789ABCDEF']; +const h1 = b => hex[b & 0xF]; +const h2 = b => hex[(b & 0xF0) >> 4] + hex[b & 0xF]; +const eq = b => ((b & 0xF0) >> 4) === (b & 0xF); +const isShort = v => eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a); +function hexParse(str) { + var len = str.length; + var ret; + if (str[0] === '#') { + if (len === 4 || len === 5) { + ret = { + r: 255 & map$1[str[1]] * 17, + g: 255 & map$1[str[2]] * 17, + b: 255 & map$1[str[3]] * 17, + a: len === 5 ? map$1[str[4]] * 17 : 255 + }; + } else if (len === 7 || len === 9) { + ret = { + r: map$1[str[1]] << 4 | map$1[str[2]], + g: map$1[str[3]] << 4 | map$1[str[4]], + b: map$1[str[5]] << 4 | map$1[str[6]], + a: len === 9 ? (map$1[str[7]] << 4 | map$1[str[8]]) : 255 + }; + } } - result.width = result.left + result.right; - result.height = result.top + result.bottom; - return result; + return ret; } -const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot); -function getCanvasPosition(evt, canvas) { - const e = evt.native || evt; - const touches = e.touches; - const source = touches && touches.length ? touches[0] : e; - const {offsetX, offsetY} = source; - let box = false; - let x, y; - if (useOffsetPos(offsetX, offsetY, e.target)) { - x = offsetX; - y = offsetY; - } else { - const rect = canvas.getBoundingClientRect(); - x = source.clientX - rect.left; - y = source.clientY - rect.top; - box = true; +const alpha = (a, f) => a < 255 ? f(a) : ''; +function hexString(v) { + var f = isShort(v) ? h1 : h2; + return v + ? '#' + f(v.r) + f(v.g) + f(v.b) + alpha(v.a, f) + : undefined; +} +const HUE_RE = /^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/; +function hsl2rgbn(h, s, l) { + const a = s * Math.min(l, 1 - l); + const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return [f(0), f(8), f(4)]; +} +function hsv2rgbn(h, s, v) { + const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0); + return [f(5), f(3), f(1)]; +} +function hwb2rgbn(h, w, b) { + const rgb = hsl2rgbn(h, 1, 0.5); + let i; + if (w + b > 1) { + i = 1 / (w + b); + w *= i; + b *= i; } - return {x, y, box}; + for (i = 0; i < 3; i++) { + rgb[i] *= 1 - w - b; + rgb[i] += w; + } + return rgb; } -function getRelativePosition$1(evt, chart) { - const {canvas, currentDevicePixelRatio} = chart; - const style = getComputedStyle(canvas); - const borderBox = style.boxSizing === 'border-box'; - const paddings = getPositionedStyle(style, 'padding'); - const borders = getPositionedStyle(style, 'border', 'width'); - const {x, y, box} = getCanvasPosition(evt, canvas); - const xOffset = paddings.left + (box && borders.left); - const yOffset = paddings.top + (box && borders.top); - let {width, height} = chart; - if (borderBox) { - width -= paddings.width + borders.width; - height -= paddings.height + borders.height; +function hueValue(r, g, b, d, max) { + if (r === max) { + return ((g - b) / d) + (g < b ? 6 : 0); + } + if (g === max) { + return (b - r) / d + 2; + } + return (r - g) / d + 4; +} +function rgb2hsl(v) { + const range = 255; + const r = v.r / range; + const g = v.g / range; + const b = v.b / range; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const l = (max + min) / 2; + let h, s, d; + if (max !== min) { + d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + h = hueValue(r, g, b, d, max); + h = h * 60 + 0.5; + } + return [h | 0, s || 0, l]; +} +function calln(f, a, b, c) { + return ( + Array.isArray(a) + ? f(a[0], a[1], a[2]) + : f(a, b, c) + ).map(n2b); +} +function hsl2rgb(h, s, l) { + return calln(hsl2rgbn, h, s, l); +} +function hwb2rgb(h, w, b) { + return calln(hwb2rgbn, h, w, b); +} +function hsv2rgb(h, s, v) { + return calln(hsv2rgbn, h, s, v); +} +function hue(h) { + return (h % 360 + 360) % 360; +} +function hueParse(str) { + const m = HUE_RE.exec(str); + let a = 255; + let v; + if (!m) { + return; + } + if (m[5] !== v) { + a = m[6] ? p2b(+m[5]) : n2b(+m[5]); + } + const h = hue(+m[2]); + const p1 = +m[3] / 100; + const p2 = +m[4] / 100; + if (m[1] === 'hwb') { + v = hwb2rgb(h, p1, p2); + } else if (m[1] === 'hsv') { + v = hsv2rgb(h, p1, p2); + } else { + v = hsl2rgb(h, p1, p2); } return { - x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio), - y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio) + r: v[0], + g: v[1], + b: v[2], + a: a }; } -function getContainerSize(canvas, width, height) { - let maxWidth, maxHeight; - if (width === undefined || height === undefined) { - const container = _getParentNode(canvas); - if (!container) { - width = canvas.clientWidth; - height = canvas.clientHeight; - } else { - const rect = container.getBoundingClientRect(); - const containerStyle = getComputedStyle(container); - const containerBorder = getPositionedStyle(containerStyle, 'border', 'width'); - const containerPadding = getPositionedStyle(containerStyle, 'padding'); - width = rect.width - containerPadding.width - containerBorder.width; - height = rect.height - containerPadding.height - containerBorder.height; - maxWidth = parseMaxStyle(containerStyle.maxWidth, container, 'clientWidth'); - maxHeight = parseMaxStyle(containerStyle.maxHeight, container, 'clientHeight'); - } +function rotate(v, deg) { + var h = rgb2hsl(v); + h[0] = hue(h[0] + deg); + h = hsl2rgb(h); + v.r = h[0]; + v.g = h[1]; + v.b = h[2]; +} +function hslString(v) { + if (!v) { + return; } - return { - width, - height, - maxWidth: maxWidth || INFINITY, - maxHeight: maxHeight || INFINITY + const a = rgb2hsl(v); + const h = a[0]; + const s = n2p(a[1]); + const l = n2p(a[2]); + return v.a < 255 + ? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})` + : `hsl(${h}, ${s}%, ${l}%)`; +} +const map$2 = { + x: 'dark', + Z: 'light', + Y: 're', + X: 'blu', + W: 'gr', + V: 'medium', + U: 'slate', + A: 'ee', + T: 'ol', + S: 'or', + B: 'ra', + C: 'lateg', + D: 'ights', + R: 'in', + Q: 'turquois', + E: 'hi', + P: 'ro', + O: 'al', + N: 'le', + M: 'de', + L: 'yello', + F: 'en', + K: 'ch', + G: 'arks', + H: 'ea', + I: 'ightg', + J: 'wh' +}; +const names$1 = { + OiceXe: 'f0f8ff', + antiquewEte: 'faebd7', + aqua: 'ffff', + aquamarRe: '7fffd4', + azuY: 'f0ffff', + beige: 'f5f5dc', + bisque: 'ffe4c4', + black: '0', + blanKedOmond: 'ffebcd', + Xe: 'ff', + XeviTet: '8a2be2', + bPwn: 'a52a2a', + burlywood: 'deb887', + caMtXe: '5f9ea0', + KartYuse: '7fff00', + KocTate: 'd2691e', + cSO: 'ff7f50', + cSnflowerXe: '6495ed', + cSnsilk: 'fff8dc', + crimson: 'dc143c', + cyan: 'ffff', + xXe: '8b', + xcyan: '8b8b', + xgTMnPd: 'b8860b', + xWay: 'a9a9a9', + xgYF: '6400', + xgYy: 'a9a9a9', + xkhaki: 'bdb76b', + xmagFta: '8b008b', + xTivegYF: '556b2f', + xSange: 'ff8c00', + xScEd: '9932cc', + xYd: '8b0000', + xsOmon: 'e9967a', + xsHgYF: '8fbc8f', + xUXe: '483d8b', + xUWay: '2f4f4f', + xUgYy: '2f4f4f', + xQe: 'ced1', + xviTet: '9400d3', + dAppRk: 'ff1493', + dApskyXe: 'bfff', + dimWay: '696969', + dimgYy: '696969', + dodgerXe: '1e90ff', + fiYbrick: 'b22222', + flSOwEte: 'fffaf0', + foYstWAn: '228b22', + fuKsia: 'ff00ff', + gaRsbSo: 'dcdcdc', + ghostwEte: 'f8f8ff', + gTd: 'ffd700', + gTMnPd: 'daa520', + Way: '808080', + gYF: '8000', + gYFLw: 'adff2f', + gYy: '808080', + honeyMw: 'f0fff0', + hotpRk: 'ff69b4', + RdianYd: 'cd5c5c', + Rdigo: '4b0082', + ivSy: 'fffff0', + khaki: 'f0e68c', + lavFMr: 'e6e6fa', + lavFMrXsh: 'fff0f5', + lawngYF: '7cfc00', + NmoncEffon: 'fffacd', + ZXe: 'add8e6', + ZcSO: 'f08080', + Zcyan: 'e0ffff', + ZgTMnPdLw: 'fafad2', + ZWay: 'd3d3d3', + ZgYF: '90ee90', + ZgYy: 'd3d3d3', + ZpRk: 'ffb6c1', + ZsOmon: 'ffa07a', + ZsHgYF: '20b2aa', + ZskyXe: '87cefa', + ZUWay: '778899', + ZUgYy: '778899', + ZstAlXe: 'b0c4de', + ZLw: 'ffffe0', + lime: 'ff00', + limegYF: '32cd32', + lRF: 'faf0e6', + magFta: 'ff00ff', + maPon: '800000', + VaquamarRe: '66cdaa', + VXe: 'cd', + VScEd: 'ba55d3', + VpurpN: '9370db', + VsHgYF: '3cb371', + VUXe: '7b68ee', + VsprRggYF: 'fa9a', + VQe: '48d1cc', + VviTetYd: 'c71585', + midnightXe: '191970', + mRtcYam: 'f5fffa', + mistyPse: 'ffe4e1', + moccasR: 'ffe4b5', + navajowEte: 'ffdead', + navy: '80', + Tdlace: 'fdf5e6', + Tive: '808000', + TivedBb: '6b8e23', + Sange: 'ffa500', + SangeYd: 'ff4500', + ScEd: 'da70d6', + pOegTMnPd: 'eee8aa', + pOegYF: '98fb98', + pOeQe: 'afeeee', + pOeviTetYd: 'db7093', + papayawEp: 'ffefd5', + pHKpuff: 'ffdab9', + peru: 'cd853f', + pRk: 'ffc0cb', + plum: 'dda0dd', + powMrXe: 'b0e0e6', + purpN: '800080', + YbeccapurpN: '663399', + Yd: 'ff0000', + Psybrown: 'bc8f8f', + PyOXe: '4169e1', + saddNbPwn: '8b4513', + sOmon: 'fa8072', + sandybPwn: 'f4a460', + sHgYF: '2e8b57', + sHshell: 'fff5ee', + siFna: 'a0522d', + silver: 'c0c0c0', + skyXe: '87ceeb', + UXe: '6a5acd', + UWay: '708090', + UgYy: '708090', + snow: 'fffafa', + sprRggYF: 'ff7f', + stAlXe: '4682b4', + tan: 'd2b48c', + teO: '8080', + tEstN: 'd8bfd8', + tomato: 'ff6347', + Qe: '40e0d0', + viTet: 'ee82ee', + JHt: 'f5deb3', + wEte: 'ffffff', + wEtesmoke: 'f5f5f5', + Lw: 'ffff00', + LwgYF: '9acd32' +}; +function unpack() { + const unpacked = {}; + const keys = Object.keys(names$1); + const tkeys = Object.keys(map$2); + let i, j, k, ok, nk; + for (i = 0; i < keys.length; i++) { + ok = nk = keys[i]; + for (j = 0; j < tkeys.length; j++) { + k = tkeys[j]; + nk = nk.replace(k, map$2[k]); + } + k = parseInt(names$1[ok], 16); + unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF]; + } + return unpacked; +} +let names; +function nameParse(str) { + if (!names) { + names = unpack(); + names.transparent = [0, 0, 0, 0]; + } + const a = names[str.toLowerCase()]; + return a && { + r: a[0], + g: a[1], + b: a[2], + a: a.length === 4 ? a[3] : 255 }; } -const round1 = v => Math.round(v * 10) / 10; -function getMaximumSize(canvas, bbWidth, bbHeight, aspectRatio) { - const style = getComputedStyle(canvas); - const margins = getPositionedStyle(style, 'margin'); - const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY; - const maxHeight = parseMaxStyle(style.maxHeight, canvas, 'clientHeight') || INFINITY; - const containerSize = getContainerSize(canvas, bbWidth, bbHeight); - let {width, height} = containerSize; - if (style.boxSizing === 'content-box') { - const borders = getPositionedStyle(style, 'border', 'width'); - const paddings = getPositionedStyle(style, 'padding'); - width -= paddings.width + borders.width; - height -= paddings.height + borders.height; +const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/; +function rgbParse(str) { + const m = RGB_RE.exec(str); + let a = 255; + let r, g, b; + if (!m) { + return; } - width = Math.max(0, width - margins.width); - height = Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height - margins.height); - width = round1(Math.min(width, maxWidth, containerSize.maxWidth)); - height = round1(Math.min(height, maxHeight, containerSize.maxHeight)); - if (width && !height) { - height = round1(width / 2); + if (m[7] !== r) { + const v = +m[7]; + a = m[8] ? p2b(v) : lim(v * 255, 0, 255); } + r = +m[1]; + g = +m[3]; + b = +m[5]; + r = 255 & (m[2] ? p2b(r) : lim(r, 0, 255)); + g = 255 & (m[4] ? p2b(g) : lim(g, 0, 255)); + b = 255 & (m[6] ? p2b(b) : lim(b, 0, 255)); return { - width, - height + r: r, + g: g, + b: b, + a: a }; } -function retinaScale(chart, forceRatio, forceStyle) { - const pixelRatio = forceRatio || 1; - const deviceHeight = Math.floor(chart.height * pixelRatio); - const deviceWidth = Math.floor(chart.width * pixelRatio); - chart.height = deviceHeight / pixelRatio; - chart.width = deviceWidth / pixelRatio; - const canvas = chart.canvas; - if (canvas.style && (forceStyle || (!canvas.style.height && !canvas.style.width))) { - canvas.style.height = `${chart.height}px`; - canvas.style.width = `${chart.width}px`; - } - if (chart.currentDevicePixelRatio !== pixelRatio - || canvas.height !== deviceHeight - || canvas.width !== deviceWidth) { - chart.currentDevicePixelRatio = pixelRatio; - canvas.height = deviceHeight; - canvas.width = deviceWidth; - chart.ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); - return true; - } - return false; +function rgbString(v) { + return v && ( + v.a < 255 + ? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})` + : `rgb(${v.r}, ${v.g}, ${v.b})` + ); } -const supportsEventListenerOptions = (function() { - let passiveSupported = false; - try { - const options = { - get passive() { - passiveSupported = true; - return false; - } - }; - window.addEventListener('test', null, options); - window.removeEventListener('test', null, options); - } catch (e) { - } - return passiveSupported; -}()); -function readUsedSize(element, property) { - const value = getStyle(element, property); - const matches = value && value.match(/^(\d+)(\.\d+)?px$/); - return matches ? +matches[1] : undefined; +const to = v => v <= 0.0031308 ? v * 12.92 : Math.pow(v, 1.0 / 2.4) * 1.055 - 0.055; +const from = v => v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); +function interpolate$1(rgb1, rgb2, t) { + const r = from(b2n(rgb1.r)); + const g = from(b2n(rgb1.g)); + const b = from(b2n(rgb1.b)); + return { + r: n2b(to(r + t * (from(b2n(rgb2.r)) - r))), + g: n2b(to(g + t * (from(b2n(rgb2.g)) - g))), + b: n2b(to(b + t * (from(b2n(rgb2.b)) - b))), + a: rgb1.a + t * (rgb2.a - rgb1.a) + }; } - -function getRelativePosition(e, chart) { - if ('native' in e) { - return { - x: e.x, - y: e.y - }; +function modHSL(v, i, ratio) { + if (v) { + let tmp = rgb2hsl(v); + tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1)); + tmp = hsl2rgb(tmp); + v.r = tmp[0]; + v.g = tmp[1]; + v.b = tmp[2]; } - return getRelativePosition$1(e, chart); } -function evaluateAllVisibleItems(chart, handler) { - const metasets = chart.getSortedVisibleDatasetMetas(); - let index, data, element; - for (let i = 0, ilen = metasets.length; i < ilen; ++i) { - ({index, data} = metasets[i]); - for (let j = 0, jlen = data.length; j < jlen; ++j) { - element = data[j]; - if (!element.skip) { - handler(element, index, j); - } - } - } +function clone(v, proto) { + return v ? Object.assign(proto || {}, v) : v; } -function binarySearch(metaset, axis, value, intersect) { - const {controller, data, _sorted} = metaset; - const iScale = controller._cachedMeta.iScale; - if (iScale && axis === iScale.axis && axis !== 'r' && _sorted && data.length) { - const lookupMethod = iScale._reversePixels ? _rlookupByKey : _lookupByKey; - if (!intersect) { - return lookupMethod(data, axis, value); - } else if (controller._sharedOptions) { - const el = data[0]; - const range = typeof el.getRange === 'function' && el.getRange(axis); - if (range) { - const start = lookupMethod(data, axis, value - range); - const end = lookupMethod(data, axis, value + range); - return {lo: start.lo, hi: end.hi}; +function fromObject(input) { + var v = {r: 0, g: 0, b: 0, a: 255}; + if (Array.isArray(input)) { + if (input.length >= 3) { + v = {r: input[0], g: input[1], b: input[2], a: 255}; + if (input.length > 3) { + v.a = n2b(input[3]); } } + } else { + v = clone(input, {r: 0, g: 0, b: 0, a: 1}); + v.a = n2b(v.a); } - return {lo: 0, hi: data.length - 1}; + return v; } -function optimizedEvaluateItems(chart, axis, position, handler, intersect) { - const metasets = chart.getSortedVisibleDatasetMetas(); - const value = position[axis]; - for (let i = 0, ilen = metasets.length; i < ilen; ++i) { - const {index, data} = metasets[i]; - const {lo, hi} = binarySearch(metasets[i], axis, value, intersect); - for (let j = lo; j <= hi; ++j) { - const element = data[j]; - if (!element.skip) { - handler(element, index, j); - } - } +function functionParse(str) { + if (str.charAt(0) === 'r') { + return rgbParse(str); } + return hueParse(str); } -function getDistanceMetricForAxis(axis) { - const useX = axis.indexOf('x') !== -1; - const useY = axis.indexOf('y') !== -1; - return function(pt1, pt2) { - const deltaX = useX ? Math.abs(pt1.x - pt2.x) : 0; - const deltaY = useY ? Math.abs(pt1.y - pt2.y) : 0; - return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); - }; -} -function getIntersectItems(chart, position, axis, useFinalPosition) { - const items = []; - if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) { - return items; +class Color { + constructor(input) { + if (input instanceof Color) { + return input; + } + const type = typeof input; + let v; + if (type === 'object') { + v = fromObject(input); + } else if (type === 'string') { + v = hexParse(input) || nameParse(input) || functionParse(input); + } + this._rgb = v; + this._valid = !!v; + } + get valid() { + return this._valid; + } + get rgb() { + var v = clone(this._rgb); + if (v) { + v.a = b2n(v.a); + } + return v; + } + set rgb(obj) { + this._rgb = fromObject(obj); + } + rgbString() { + return this._valid ? rgbString(this._rgb) : undefined; + } + hexString() { + return this._valid ? hexString(this._rgb) : undefined; + } + hslString() { + return this._valid ? hslString(this._rgb) : undefined; + } + mix(color, weight) { + if (color) { + const c1 = this.rgb; + const c2 = color.rgb; + let w2; + const p = weight === w2 ? 0.5 : weight; + const w = 2 * p - 1; + const a = c1.a - c2.a; + const w1 = ((w * a === -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0; + w2 = 1 - w1; + c1.r = 0xFF & w1 * c1.r + w2 * c2.r + 0.5; + c1.g = 0xFF & w1 * c1.g + w2 * c2.g + 0.5; + c1.b = 0xFF & w1 * c1.b + w2 * c2.b + 0.5; + c1.a = p * c1.a + (1 - p) * c2.a; + this.rgb = c1; + } + return this; } - const evaluationFunc = function(element, datasetIndex, index) { - if (element.inRange(position.x, position.y, useFinalPosition)) { - items.push({element, datasetIndex, index}); - } - }; - optimizedEvaluateItems(chart, axis, position, evaluationFunc, true); - return items; -} -function getNearestRadialItems(chart, position, axis, useFinalPosition) { - let items = []; - function evaluationFunc(element, datasetIndex, index) { - const {startAngle, endAngle} = element.getProps(['startAngle', 'endAngle'], useFinalPosition); - const {angle} = getAngleFromPoint(element, {x: position.x, y: position.y}); - if (_angleBetween(angle, startAngle, endAngle)) { - items.push({element, datasetIndex, index}); + interpolate(color, t) { + if (color) { + this._rgb = interpolate$1(this._rgb, color._rgb, t); } + return this; } - optimizedEvaluateItems(chart, axis, position, evaluationFunc); - return items; -} -function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition) { - let items = []; - const distanceMetric = getDistanceMetricForAxis(axis); - let minDistance = Number.POSITIVE_INFINITY; - function evaluationFunc(element, datasetIndex, index) { - const inRange = element.inRange(position.x, position.y, useFinalPosition); - if (intersect && !inRange) { - return; - } - const center = element.getCenterPoint(useFinalPosition); - const pointInArea = _isPointInArea(center, chart.chartArea, chart._minPadding); - if (!pointInArea && !inRange) { - return; - } - const distance = distanceMetric(position, center); - if (distance < minDistance) { - items = [{element, datasetIndex, index}]; - minDistance = distance; - } else if (distance === minDistance) { - items.push({element, datasetIndex, index}); - } + clone() { + return new Color(this.rgb); } - optimizedEvaluateItems(chart, axis, position, evaluationFunc); - return items; -} -function getNearestItems(chart, position, axis, intersect, useFinalPosition) { - if (!_isPointInArea(position, chart.chartArea, chart._minPadding)) { - return []; + alpha(a) { + this._rgb.a = n2b(a); + return this; } - return axis === 'r' && !intersect - ? getNearestRadialItems(chart, position, axis, useFinalPosition) - : getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition); -} -function getAxisItems(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const items = []; - const axis = options.axis; - const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; - let intersectsItem = false; - evaluateAllVisibleItems(chart, (element, datasetIndex, index) => { - if (element[rangeMethod](position[axis], useFinalPosition)) { - items.push({element, datasetIndex, index}); - } - if (element.inRange(position.x, position.y, useFinalPosition)) { - intersectsItem = true; - } - }); - if (options.intersect && !intersectsItem) { - return []; + clearer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 - ratio; + return this; } - return items; -} -var Interaction = { - modes: { - index(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'x'; - const items = options.intersect - ? getIntersectItems(chart, position, axis, useFinalPosition) - : getNearestItems(chart, position, axis, false, useFinalPosition); - const elements = []; - if (!items.length) { - return []; - } - chart.getSortedVisibleDatasetMetas().forEach((meta) => { - const index = items[0].index; - const element = meta.data[index]; - if (element && !element.skip) { - elements.push({element, datasetIndex: meta.index, index}); - } - }); - return elements; - }, - dataset(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - let items = options.intersect - ? getIntersectItems(chart, position, axis, useFinalPosition) : - getNearestItems(chart, position, axis, false, useFinalPosition); - if (items.length > 0) { - const datasetIndex = items[0].datasetIndex; - const data = chart.getDatasetMeta(datasetIndex).data; - items = []; - for (let i = 0; i < data.length; ++i) { - items.push({element: data[i], datasetIndex, index: i}); - } - } - return items; - }, - point(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - return getIntersectItems(chart, position, axis, useFinalPosition); - }, - nearest(chart, e, options, useFinalPosition) { - const position = getRelativePosition(e, chart); - const axis = options.axis || 'xy'; - return getNearestItems(chart, position, axis, options.intersect, useFinalPosition); - }, - x(chart, e, options, useFinalPosition) { - return getAxisItems(chart, e, {axis: 'x', intersect: options.intersect}, useFinalPosition); - }, - y(chart, e, options, useFinalPosition) { - return getAxisItems(chart, e, {axis: 'y', intersect: options.intersect}, useFinalPosition); - } + greyscale() { + const rgb = this._rgb; + const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11); + rgb.r = rgb.g = rgb.b = val; + return this; } -}; - -const LINE_HEIGHT = new RegExp(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/); -const FONT_STYLE = new RegExp(/^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/); -function toLineHeight(value, size) { - const matches = ('' + value).match(LINE_HEIGHT); - if (!matches || matches[1] === 'normal') { - return size * 1.2; + opaquer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 + ratio; + return this; } - value = +matches[2]; - switch (matches[3]) { - case 'px': - return value; - case '%': - value /= 100; - break; + negate() { + const v = this._rgb; + v.r = 255 - v.r; + v.g = 255 - v.g; + v.b = 255 - v.b; + return this; } - return size * value; -} -const numberOrZero = v => +v || 0; -function _readValueToProps(value, props) { - const ret = {}; - const objProps = isObject(props); - const keys = objProps ? Object.keys(props) : props; - const read = isObject(value) - ? objProps - ? prop => valueOrDefault(value[prop], value[props[prop]]) - : prop => value[prop] - : () => value; - for (const prop of keys) { - ret[prop] = numberOrZero(read(prop)); + lighten(ratio) { + modHSL(this._rgb, 2, ratio); + return this; } - return ret; -} -function toTRBL(value) { - return _readValueToProps(value, {top: 'y', right: 'x', bottom: 'y', left: 'x'}); -} -function toTRBLCorners(value) { - return _readValueToProps(value, ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']); -} -function toPadding(value) { - const obj = toTRBL(value); - obj.width = obj.left + obj.right; - obj.height = obj.top + obj.bottom; - return obj; -} -function toFont(options, fallback) { - options = options || {}; - fallback = fallback || defaults.font; - let size = valueOrDefault(options.size, fallback.size); - if (typeof size === 'string') { - size = parseInt(size, 10); + darken(ratio) { + modHSL(this._rgb, 2, -ratio); + return this; } - let style = valueOrDefault(options.style, fallback.style); - if (style && !('' + style).match(FONT_STYLE)) { - console.warn('Invalid font style specified: "' + style + '"'); - style = ''; + saturate(ratio) { + modHSL(this._rgb, 1, ratio); + return this; } - const font = { - family: valueOrDefault(options.family, fallback.family), - lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size), - size, - style, - weight: valueOrDefault(options.weight, fallback.weight), - string: '' - }; - font.string = toFontString(font); - return font; -} -function resolve(inputs, context, index, info) { - let cacheable = true; - let i, ilen, value; - for (i = 0, ilen = inputs.length; i < ilen; ++i) { - value = inputs[i]; - if (value === undefined) { - continue; - } - if (context !== undefined && typeof value === 'function') { - value = value(context); - cacheable = false; - } - if (index !== undefined && isArray(value)) { - value = value[index % value.length]; - cacheable = false; - } - if (value !== undefined) { - if (info && !cacheable) { - info.cacheable = false; - } - return value; - } + desaturate(ratio) { + modHSL(this._rgb, 1, -ratio); + return this; + } + rotate(deg) { + rotate(this._rgb, deg); + return this; } } -function _addGrace(minmax, grace, beginAtZero) { - const {min, max} = minmax; - const change = toDimension(grace, (max - min) / 2); - const keepZero = (value, add) => beginAtZero && value === 0 ? 0 : value + add; - return { - min: keepZero(min, -Math.abs(change)), - max: keepZero(max, change) - }; -} -function createContext(parentContext, context) { - return Object.assign(Object.create(parentContext), context); +function index_esm(input) { + return new Color(input); } -const STATIC_POSITIONS = ['left', 'top', 'right', 'bottom']; -function filterByPosition(array, position) { - return array.filter(v => v.pos === position); +function isPatternOrGradient(value) { + if (value && typeof value === 'object') { + const type = value.toString(); + return type === '[object CanvasPattern]' || type === '[object CanvasGradient]'; + } + return false; } -function filterDynamicPositionByAxis(array, axis) { - return array.filter(v => STATIC_POSITIONS.indexOf(v.pos) === -1 && v.box.axis === axis); +function color(value) { + return isPatternOrGradient(value) ? value : index_esm(value); } -function sortByWeight(array, reverse) { - return array.sort((a, b) => { - const v0 = reverse ? b : a; - const v1 = reverse ? a : b; - return v0.weight === v1.weight ? - v0.index - v1.index : - v0.weight - v1.weight; - }); +function getHoverColor(value) { + return isPatternOrGradient(value) + ? value + : index_esm(value).saturate(0.5).darken(0.1).hexString(); } -function wrapBoxes(boxes) { - const layoutBoxes = []; - let i, ilen, box, pos, stack, stackWeight; - for (i = 0, ilen = (boxes || []).length; i < ilen; ++i) { - box = boxes[i]; - ({position: pos, options: {stack, stackWeight = 1}} = box); - layoutBoxes.push({ - index: i, - box, - pos, - horizontal: box.isHorizontal(), - weight: box.weight, - stack: stack && (pos + stack), - stackWeight - }); + +const overrides = Object.create(null); +const descriptors = Object.create(null); +function getScope$1(node, key) { + if (!key) { + return node; } - return layoutBoxes; -} -function buildStacks(layouts) { - const stacks = {}; - for (const wrap of layouts) { - const {stack, pos, stackWeight} = wrap; - if (!stack || !STATIC_POSITIONS.includes(pos)) { - continue; - } - const _stack = stacks[stack] || (stacks[stack] = {count: 0, placed: 0, weight: 0, size: 0}); - _stack.count++; - _stack.weight += stackWeight; + const keys = key.split('.'); + for (let i = 0, n = keys.length; i < n; ++i) { + const k = keys[i]; + node = node[k] || (node[k] = Object.create(null)); } - return stacks; + return node; } -function setLayoutDims(layouts, params) { - const stacks = buildStacks(layouts); - const {vBoxMaxWidth, hBoxMaxHeight} = params; - let i, ilen, layout; - for (i = 0, ilen = layouts.length; i < ilen; ++i) { - layout = layouts[i]; - const {fullSize} = layout.box; - const stack = stacks[layout.stack]; - const factor = stack && layout.stackWeight / stack.weight; - if (layout.horizontal) { - layout.width = factor ? factor * vBoxMaxWidth : fullSize && params.availableWidth; - layout.height = hBoxMaxHeight; - } else { - layout.width = vBoxMaxWidth; - layout.height = factor ? factor * hBoxMaxHeight : fullSize && params.availableHeight; - } +function set(root, scope, values) { + if (typeof scope === 'string') { + return merge(getScope$1(root, scope), values); } - return stacks; -} -function buildLayoutBoxes(boxes) { - const layoutBoxes = wrapBoxes(boxes); - const fullSize = sortByWeight(layoutBoxes.filter(wrap => wrap.box.fullSize), true); - const left = sortByWeight(filterByPosition(layoutBoxes, 'left'), true); - const right = sortByWeight(filterByPosition(layoutBoxes, 'right')); - const top = sortByWeight(filterByPosition(layoutBoxes, 'top'), true); - const bottom = sortByWeight(filterByPosition(layoutBoxes, 'bottom')); - const centerHorizontal = filterDynamicPositionByAxis(layoutBoxes, 'x'); - const centerVertical = filterDynamicPositionByAxis(layoutBoxes, 'y'); - return { - fullSize, - leftAndTop: left.concat(top), - rightAndBottom: right.concat(centerVertical).concat(bottom).concat(centerHorizontal), - chartArea: filterByPosition(layoutBoxes, 'chartArea'), - vertical: left.concat(right).concat(centerVertical), - horizontal: top.concat(bottom).concat(centerHorizontal) - }; -} -function getCombinedMax(maxPadding, chartArea, a, b) { - return Math.max(maxPadding[a], chartArea[a]) + Math.max(maxPadding[b], chartArea[b]); -} -function updateMaxPadding(maxPadding, boxPadding) { - maxPadding.top = Math.max(maxPadding.top, boxPadding.top); - maxPadding.left = Math.max(maxPadding.left, boxPadding.left); - maxPadding.bottom = Math.max(maxPadding.bottom, boxPadding.bottom); - maxPadding.right = Math.max(maxPadding.right, boxPadding.right); + return merge(getScope$1(root, ''), scope); } -function updateDims(chartArea, params, layout, stacks) { - const {pos, box} = layout; - const maxPadding = chartArea.maxPadding; - if (!isObject(pos)) { - if (layout.size) { - chartArea[pos] -= layout.size; - } - const stack = stacks[layout.stack] || {size: 0, count: 1}; - stack.size = Math.max(stack.size, layout.horizontal ? box.height : box.width); - layout.size = stack.size / stack.count; - chartArea[pos] += layout.size; +class Defaults { + constructor(_descriptors) { + this.animation = undefined; + this.backgroundColor = 'rgba(0,0,0,0.1)'; + this.borderColor = 'rgba(0,0,0,0.1)'; + this.color = '#666'; + this.datasets = {}; + this.devicePixelRatio = (context) => context.chart.platform.getDevicePixelRatio(); + this.elements = {}; + this.events = [ + 'mousemove', + 'mouseout', + 'click', + 'touchstart', + 'touchmove' + ]; + this.font = { + family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", + size: 12, + style: 'normal', + lineHeight: 1.2, + weight: null + }; + this.hover = {}; + this.hoverBackgroundColor = (ctx, options) => getHoverColor(options.backgroundColor); + this.hoverBorderColor = (ctx, options) => getHoverColor(options.borderColor); + this.hoverColor = (ctx, options) => getHoverColor(options.color); + this.indexAxis = 'x'; + this.interaction = { + mode: 'nearest', + intersect: true, + includeInvisible: false + }; + this.maintainAspectRatio = true; + this.onHover = null; + this.onClick = null; + this.parsing = true; + this.plugins = {}; + this.responsive = true; + this.scale = undefined; + this.scales = {}; + this.showLine = true; + this.drawActiveElementsOnTop = true; + this.describe(_descriptors); } - if (box.getPadding) { - updateMaxPadding(maxPadding, box.getPadding()); + set(scope, values) { + return set(this, scope, values); } - const newWidth = Math.max(0, params.outerWidth - getCombinedMax(maxPadding, chartArea, 'left', 'right')); - const newHeight = Math.max(0, params.outerHeight - getCombinedMax(maxPadding, chartArea, 'top', 'bottom')); - const widthChanged = newWidth !== chartArea.w; - const heightChanged = newHeight !== chartArea.h; - chartArea.w = newWidth; - chartArea.h = newHeight; - return layout.horizontal - ? {same: widthChanged, other: heightChanged} - : {same: heightChanged, other: widthChanged}; -} -function handleMaxPadding(chartArea) { - const maxPadding = chartArea.maxPadding; - function updatePos(pos) { - const change = Math.max(maxPadding[pos] - chartArea[pos], 0); - chartArea[pos] += change; - return change; + get(scope) { + return getScope$1(this, scope); } - chartArea.y += updatePos('top'); - chartArea.x += updatePos('left'); - updatePos('right'); - updatePos('bottom'); -} -function getMargins(horizontal, chartArea) { - const maxPadding = chartArea.maxPadding; - function marginForPositions(positions) { - const margin = {left: 0, top: 0, right: 0, bottom: 0}; - positions.forEach((pos) => { - margin[pos] = Math.max(chartArea[pos], maxPadding[pos]); + describe(scope, values) { + return set(descriptors, scope, values); + } + override(scope, values) { + return set(overrides, scope, values); + } + route(scope, name, targetScope, targetName) { + const scopeObject = getScope$1(this, scope); + const targetScopeObject = getScope$1(this, targetScope); + const privateName = '_' + name; + Object.defineProperties(scopeObject, { + [privateName]: { + value: scopeObject[name], + writable: true + }, + [name]: { + enumerable: true, + get() { + const local = this[privateName]; + const target = targetScopeObject[targetName]; + if (isObject(local)) { + return Object.assign({}, target, local); + } + return valueOrDefault(local, target); + }, + set(value) { + this[privateName] = value; + } + } }); - return margin; } - return horizontal - ? marginForPositions(['left', 'right']) - : marginForPositions(['top', 'bottom']); } -function fitBoxes(boxes, chartArea, params, stacks) { - const refitBoxes = []; - let i, ilen, layout, box, refit, changed; - for (i = 0, ilen = boxes.length, refit = 0; i < ilen; ++i) { - layout = boxes[i]; - box = layout.box; - box.update( - layout.width || chartArea.w, - layout.height || chartArea.h, - getMargins(layout.horizontal, chartArea) - ); - const {same, other} = updateDims(chartArea, params, layout, stacks); - refit |= same && refitBoxes.length; - changed = changed || other; - if (!box.fullSize) { - refitBoxes.push(layout); - } +var defaults = new Defaults({ + _scriptable: (name) => !name.startsWith('on'), + _indexable: (name) => name !== 'events', + hover: { + _fallback: 'interaction' + }, + interaction: { + _scriptable: false, + _indexable: false, } - return refit && fitBoxes(refitBoxes, chartArea, params, stacks) || changed; +}); + +function _isDomSupported() { + return typeof window !== 'undefined' && typeof document !== 'undefined'; } -function setBoxDims(box, left, top, width, height) { - box.top = top; - box.left = left; - box.right = left + width; - box.bottom = top + height; - box.width = width; - box.height = height; +function _getParentNode(domNode) { + let parent = domNode.parentNode; + if (parent && parent.toString() === '[object ShadowRoot]') { + parent = parent.host; + } + return parent; } -function placeBoxes(boxes, chartArea, params, stacks) { - const userPadding = params.padding; - let {x, y} = chartArea; - for (const layout of boxes) { - const box = layout.box; - const stack = stacks[layout.stack] || {count: 1, placed: 0, weight: 1}; - const weight = (layout.stackWeight / stack.weight) || 1; - if (layout.horizontal) { - const width = chartArea.w * weight; - const height = stack.size || box.height; - if (defined(stack.start)) { - y = stack.start; - } - if (box.fullSize) { - setBoxDims(box, userPadding.left, y, params.outerWidth - userPadding.right - userPadding.left, height); - } else { - setBoxDims(box, chartArea.left + stack.placed, y, width, height); - } - stack.start = y; - stack.placed += width; - y = box.bottom; - } else { - const height = chartArea.h * weight; - const width = stack.size || box.width; - if (defined(stack.start)) { - x = stack.start; - } - if (box.fullSize) { - setBoxDims(box, x, userPadding.top, width, params.outerHeight - userPadding.bottom - userPadding.top); - } else { - setBoxDims(box, x, chartArea.top + stack.placed, width, height); - } - stack.start = x; - stack.placed += height; - x = box.right; +function parseMaxStyle(styleValue, node, parentProperty) { + let valueInPixels; + if (typeof styleValue === 'string') { + valueInPixels = parseInt(styleValue, 10); + if (styleValue.indexOf('%') !== -1) { + valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty]; } + } else { + valueInPixels = styleValue; } - chartArea.x = x; - chartArea.y = y; + return valueInPixels; } -defaults.set('layout', { - autoPadding: true, - padding: { - top: 0, - right: 0, - bottom: 0, - left: 0 +const getComputedStyle = (element) => window.getComputedStyle(element, null); +function getStyle(el, property) { + return getComputedStyle(el).getPropertyValue(property); +} +const positions = ['top', 'right', 'bottom', 'left']; +function getPositionedStyle(styles, style, suffix) { + const result = {}; + suffix = suffix ? '-' + suffix : ''; + for (let i = 0; i < 4; i++) { + const pos = positions[i]; + result[pos] = parseFloat(styles[style + '-' + pos + suffix]) || 0; } -}); -var layouts = { - addBox(chart, item) { - if (!chart.boxes) { - chart.boxes = []; - } - item.fullSize = item.fullSize || false; - item.position = item.position || 'top'; - item.weight = item.weight || 0; - item._layers = item._layers || function() { - return [{ - z: 0, - draw(chartArea) { - item.draw(chartArea); - } - }]; - }; - chart.boxes.push(item); - }, - removeBox(chart, layoutItem) { - const index = chart.boxes ? chart.boxes.indexOf(layoutItem) : -1; - if (index !== -1) { - chart.boxes.splice(index, 1); - } - }, - configure(chart, item, options) { - item.fullSize = options.fullSize; - item.position = options.position; - item.weight = options.weight; - }, - update(chart, width, height, minPadding) { - if (!chart) { - return; - } - const padding = toPadding(chart.options.layout.padding); - const availableWidth = Math.max(width - padding.width, 0); - const availableHeight = Math.max(height - padding.height, 0); - const boxes = buildLayoutBoxes(chart.boxes); - const verticalBoxes = boxes.vertical; - const horizontalBoxes = boxes.horizontal; - each(chart.boxes, box => { - if (typeof box.beforeLayout === 'function') { - box.beforeLayout(); - } - }); - const visibleVerticalBoxCount = verticalBoxes.reduce((total, wrap) => - wrap.box.options && wrap.box.options.display === false ? total : total + 1, 0) || 1; - const params = Object.freeze({ - outerWidth: width, - outerHeight: height, - padding, - availableWidth, - availableHeight, - vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount, - hBoxMaxHeight: availableHeight / 2 - }); - const maxPadding = Object.assign({}, padding); - updateMaxPadding(maxPadding, toPadding(minPadding)); - const chartArea = Object.assign({ - maxPadding, - w: availableWidth, - h: availableHeight, - x: padding.left, - y: padding.top - }, padding); - const stacks = setLayoutDims(verticalBoxes.concat(horizontalBoxes), params); - fitBoxes(boxes.fullSize, chartArea, params, stacks); - fitBoxes(verticalBoxes, chartArea, params, stacks); - if (fitBoxes(horizontalBoxes, chartArea, params, stacks)) { - fitBoxes(verticalBoxes, chartArea, params, stacks); - } - handleMaxPadding(chartArea); - placeBoxes(boxes.leftAndTop, chartArea, params, stacks); - chartArea.x += chartArea.w; - chartArea.y += chartArea.h; - placeBoxes(boxes.rightAndBottom, chartArea, params, stacks); - chart.chartArea = { - left: chartArea.left, - top: chartArea.top, - right: chartArea.left + chartArea.w, - bottom: chartArea.top + chartArea.h, - height: chartArea.h, - width: chartArea.w, - }; - each(boxes.chartArea, (layout) => { - const box = layout.box; - Object.assign(box, chart.chartArea); - box.update(chartArea.w, chartArea.h, {left: 0, top: 0, right: 0, bottom: 0}); - }); + result.width = result.left + result.right; + result.height = result.top + result.bottom; + return result; +} +const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot); +function getCanvasPosition(e, canvas) { + const touches = e.touches; + const source = touches && touches.length ? touches[0] : e; + const {offsetX, offsetY} = source; + let box = false; + let x, y; + if (useOffsetPos(offsetX, offsetY, e.target)) { + x = offsetX; + y = offsetY; + } else { + const rect = canvas.getBoundingClientRect(); + x = source.clientX - rect.left; + y = source.clientY - rect.top; + box = true; } -}; - -function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fallback, getTarget = () => scopes[0]) { - if (!defined(fallback)) { - fallback = _resolve('_fallback', scopes); + return {x, y, box}; +} +function getRelativePosition(evt, chart) { + if ('native' in evt) { + return evt; } - const cache = { - [Symbol.toStringTag]: 'Object', - _cacheable: true, - _scopes: scopes, - _rootScopes: rootScopes, - _fallback: fallback, - _getTarget: getTarget, - override: (scope) => _createResolver([scope, ...scopes], prefixes, rootScopes, fallback), + const {canvas, currentDevicePixelRatio} = chart; + const style = getComputedStyle(canvas); + const borderBox = style.boxSizing === 'border-box'; + const paddings = getPositionedStyle(style, 'padding'); + const borders = getPositionedStyle(style, 'border', 'width'); + const {x, y, box} = getCanvasPosition(evt, canvas); + const xOffset = paddings.left + (box && borders.left); + const yOffset = paddings.top + (box && borders.top); + let {width, height} = chart; + if (borderBox) { + width -= paddings.width + borders.width; + height -= paddings.height + borders.height; + } + return { + x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio), + y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio) }; - return new Proxy(cache, { - deleteProperty(target, prop) { - delete target[prop]; - delete target._keys; - delete scopes[0][prop]; - return true; - }, - get(target, prop) { - return _cached(target, prop, - () => _resolveWithPrefixes(prop, prefixes, scopes, target)); - }, - getOwnPropertyDescriptor(target, prop) { - return Reflect.getOwnPropertyDescriptor(target._scopes[0], prop); - }, - getPrototypeOf() { - return Reflect.getPrototypeOf(scopes[0]); - }, - has(target, prop) { - return getKeysFromAllScopes(target).includes(prop); - }, - ownKeys(target) { - return getKeysFromAllScopes(target); - }, - set(target, prop, value) { - const storage = target._storage || (target._storage = getTarget()); - target[prop] = storage[prop] = value; - delete target._keys; - return true; - } - }); } -function _attachContext(proxy, context, subProxy, descriptorDefaults) { - const cache = { - _cacheable: false, - _proxy: proxy, - _context: context, - _subProxy: subProxy, - _stack: new Set(), - _descriptors: _descriptors(proxy, descriptorDefaults), - setContext: (ctx) => _attachContext(proxy, ctx, subProxy, descriptorDefaults), - override: (scope) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults) - }; - return new Proxy(cache, { - deleteProperty(target, prop) { - delete target[prop]; - delete proxy[prop]; - return true; - }, - get(target, prop, receiver) { - return _cached(target, prop, - () => _resolveWithContext(target, prop, receiver)); - }, - getOwnPropertyDescriptor(target, prop) { - return target._descriptors.allKeys - ? Reflect.has(proxy, prop) ? {enumerable: true, configurable: true} : undefined - : Reflect.getOwnPropertyDescriptor(proxy, prop); - }, - getPrototypeOf() { - return Reflect.getPrototypeOf(proxy); - }, - has(target, prop) { - return Reflect.has(proxy, prop); - }, - ownKeys() { - return Reflect.ownKeys(proxy); - }, - set(target, prop, value) { - proxy[prop] = value; - delete target[prop]; - return true; +function getContainerSize(canvas, width, height) { + let maxWidth, maxHeight; + if (width === undefined || height === undefined) { + const container = _getParentNode(canvas); + if (!container) { + width = canvas.clientWidth; + height = canvas.clientHeight; + } else { + const rect = container.getBoundingClientRect(); + const containerStyle = getComputedStyle(container); + const containerBorder = getPositionedStyle(containerStyle, 'border', 'width'); + const containerPadding = getPositionedStyle(containerStyle, 'padding'); + width = rect.width - containerPadding.width - containerBorder.width; + height = rect.height - containerPadding.height - containerBorder.height; + maxWidth = parseMaxStyle(containerStyle.maxWidth, container, 'clientWidth'); + maxHeight = parseMaxStyle(containerStyle.maxHeight, container, 'clientHeight'); } - }); -} -function _descriptors(proxy, defaults = {scriptable: true, indexable: true}) { - const {_scriptable = defaults.scriptable, _indexable = defaults.indexable, _allKeys = defaults.allKeys} = proxy; + } return { - allKeys: _allKeys, - scriptable: _scriptable, - indexable: _indexable, - isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable, - isIndexable: isFunction(_indexable) ? _indexable : () => _indexable + width, + height, + maxWidth: maxWidth || INFINITY, + maxHeight: maxHeight || INFINITY }; } -const readKey = (prefix, name) => prefix ? prefix + _capitalize(name) : name; -const needsSubResolver = (prop, value) => isObject(value) && prop !== 'adapters' && - (Object.getPrototypeOf(value) === null || value.constructor === Object); -function _cached(target, prop, resolve) { - if (Object.prototype.hasOwnProperty.call(target, prop)) { - return target[prop]; - } - const value = resolve(); - target[prop] = value; - return value; -} -function _resolveWithContext(target, prop, receiver) { - const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; - let value = _proxy[prop]; - if (isFunction(value) && descriptors.isScriptable(prop)) { - value = _resolveScriptable(prop, value, target, receiver); - } - if (isArray(value) && value.length) { - value = _resolveArray(prop, value, target, descriptors.isIndexable); - } - if (needsSubResolver(prop, value)) { - value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors); - } - return value; -} -function _resolveScriptable(prop, value, target, receiver) { - const {_proxy, _context, _subProxy, _stack} = target; - if (_stack.has(prop)) { - throw new Error('Recursion detected: ' + Array.from(_stack).join('->') + '->' + prop); +const round1 = v => Math.round(v * 10) / 10; +function getMaximumSize(canvas, bbWidth, bbHeight, aspectRatio) { + const style = getComputedStyle(canvas); + const margins = getPositionedStyle(style, 'margin'); + const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY; + const maxHeight = parseMaxStyle(style.maxHeight, canvas, 'clientHeight') || INFINITY; + const containerSize = getContainerSize(canvas, bbWidth, bbHeight); + let {width, height} = containerSize; + if (style.boxSizing === 'content-box') { + const borders = getPositionedStyle(style, 'border', 'width'); + const paddings = getPositionedStyle(style, 'padding'); + width -= paddings.width + borders.width; + height -= paddings.height + borders.height; } - _stack.add(prop); - value = value(_context, _subProxy || receiver); - _stack.delete(prop); - if (needsSubResolver(prop, value)) { - value = createSubResolver(_proxy._scopes, _proxy, prop, value); + width = Math.max(0, width - margins.width); + height = Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height - margins.height); + width = round1(Math.min(width, maxWidth, containerSize.maxWidth)); + height = round1(Math.min(height, maxHeight, containerSize.maxHeight)); + if (width && !height) { + height = round1(width / 2); } - return value; + return { + width, + height + }; } -function _resolveArray(prop, value, target, isIndexable) { - const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; - if (defined(_context.index) && isIndexable(prop)) { - value = value[_context.index % value.length]; - } else if (isObject(value[0])) { - const arr = value; - const scopes = _proxy._scopes.filter(s => s !== arr); - value = []; - for (const item of arr) { - const resolver = createSubResolver(scopes, _proxy, prop, item); - value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors)); - } +function retinaScale(chart, forceRatio, forceStyle) { + const pixelRatio = forceRatio || 1; + const deviceHeight = Math.floor(chart.height * pixelRatio); + const deviceWidth = Math.floor(chart.width * pixelRatio); + chart.height = deviceHeight / pixelRatio; + chart.width = deviceWidth / pixelRatio; + const canvas = chart.canvas; + if (canvas.style && (forceStyle || (!canvas.style.height && !canvas.style.width))) { + canvas.style.height = `${chart.height}px`; + canvas.style.width = `${chart.width}px`; } - return value; -} -function resolveFallback(fallback, prop, value) { - return isFunction(fallback) ? fallback(prop, value) : fallback; -} -const getScope = (key, parent) => key === true ? parent - : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; -function addScopes(set, parentScopes, key, parentFallback, value) { - for (const parent of parentScopes) { - const scope = getScope(key, parent); - if (scope) { - set.add(scope); - const fallback = resolveFallback(scope._fallback, key, value); - if (defined(fallback) && fallback !== key && fallback !== parentFallback) { - return fallback; - } - } else if (scope === false && defined(parentFallback) && key !== parentFallback) { - return null; - } + if (chart.currentDevicePixelRatio !== pixelRatio + || canvas.height !== deviceHeight + || canvas.width !== deviceWidth) { + chart.currentDevicePixelRatio = pixelRatio; + canvas.height = deviceHeight; + canvas.width = deviceWidth; + chart.ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); + return true; } return false; } -function createSubResolver(parentScopes, resolver, prop, value) { - const rootScopes = resolver._rootScopes; - const fallback = resolveFallback(resolver._fallback, prop, value); - const allScopes = [...parentScopes, ...rootScopes]; - const set = new Set(); - set.add(value); - let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); - if (key === null) { - return false; - } - if (defined(fallback) && fallback !== prop) { - key = addScopesFromKey(set, allScopes, fallback, key, value); - if (key === null) { - return false; - } +const supportsEventListenerOptions = (function() { + let passiveSupported = false; + try { + const options = { + get passive() { + passiveSupported = true; + return false; + } + }; + window.addEventListener('test', null, options); + window.removeEventListener('test', null, options); + } catch (e) { } - return _createResolver(Array.from(set), [''], rootScopes, fallback, - () => subGetTarget(resolver, prop, value)); + return passiveSupported; +}()); +function readUsedSize(element, property) { + const value = getStyle(element, property); + const matches = value && value.match(/^(\d+)(\.\d+)?px$/); + return matches ? +matches[1] : undefined; } -function addScopesFromKey(set, allScopes, key, fallback, item) { - while (key) { - key = addScopes(set, allScopes, key, fallback, item); + +function toFontString(font) { + if (!font || isNullOrUndef(font.size) || isNullOrUndef(font.family)) { + return null; } - return key; + return (font.style ? font.style + ' ' : '') + + (font.weight ? font.weight + ' ' : '') + + font.size + 'px ' + + font.family; } -function subGetTarget(resolver, prop, value) { - const parent = resolver._getTarget(); - if (!(prop in parent)) { - parent[prop] = {}; +function _measureText(ctx, data, gc, longest, string) { + let textWidth = data[string]; + if (!textWidth) { + textWidth = data[string] = ctx.measureText(string).width; + gc.push(string); } - const target = parent[prop]; - if (isArray(target) && isObject(value)) { - return value; + if (textWidth > longest) { + longest = textWidth; } - return target; + return longest; } -function _resolveWithPrefixes(prop, prefixes, scopes, proxy) { - let value; - for (const prefix of prefixes) { - value = _resolve(readKey(prefix, prop), scopes); - if (defined(value)) { - return needsSubResolver(prop, value) - ? createSubResolver(scopes, proxy, prop, value) - : value; - } +function _longestText(ctx, font, arrayOfThings, cache) { + cache = cache || {}; + let data = cache.data = cache.data || {}; + let gc = cache.garbageCollect = cache.garbageCollect || []; + if (cache.font !== font) { + data = cache.data = {}; + gc = cache.garbageCollect = []; + cache.font = font; } -} -function _resolve(key, scopes) { - for (const scope of scopes) { - if (!scope) { - continue; + ctx.save(); + ctx.font = font; + let longest = 0; + const ilen = arrayOfThings.length; + let i, j, jlen, thing, nestedThing; + for (i = 0; i < ilen; i++) { + thing = arrayOfThings[i]; + if (thing !== undefined && thing !== null && isArray(thing) !== true) { + longest = _measureText(ctx, data, gc, longest, thing); + } else if (isArray(thing)) { + for (j = 0, jlen = thing.length; j < jlen; j++) { + nestedThing = thing[j]; + if (nestedThing !== undefined && nestedThing !== null && !isArray(nestedThing)) { + longest = _measureText(ctx, data, gc, longest, nestedThing); + } + } } - const value = scope[key]; - if (defined(value)) { - return value; + } + ctx.restore(); + const gcLen = gc.length / 2; + if (gcLen > arrayOfThings.length) { + for (i = 0; i < gcLen; i++) { + delete data[gc[i]]; } + gc.splice(0, gcLen); } + return longest; } -function getKeysFromAllScopes(target) { - let keys = target._keys; - if (!keys) { - keys = target._keys = resolveKeysFromAllScopes(target._scopes); - } - return keys; +function _alignPixel(chart, pixel, width) { + const devicePixelRatio = chart.currentDevicePixelRatio; + const halfWidth = width !== 0 ? Math.max(width / 2, 0.5) : 0; + return Math.round((pixel - halfWidth) * devicePixelRatio) / devicePixelRatio + halfWidth; } -function resolveKeysFromAllScopes(scopes) { - const set = new Set(); - for (const scope of scopes) { - for (const key of Object.keys(scope).filter(k => !k.startsWith('_'))) { - set.add(key); +function clearCanvas(canvas, ctx) { + ctx = ctx || canvas.getContext('2d'); + ctx.save(); + ctx.resetTransform(); + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.restore(); +} +function drawPoint(ctx, options, x, y) { + drawPointLegend(ctx, options, x, y, null); +} +function drawPointLegend(ctx, options, x, y, w) { + let type, xOffset, yOffset, size, cornerRadius, width; + const style = options.pointStyle; + const rotation = options.rotation; + const radius = options.radius; + let rad = (rotation || 0) * RAD_PER_DEG; + if (style && typeof style === 'object') { + type = style.toString(); + if (type === '[object HTMLImageElement]' || type === '[object HTMLCanvasElement]') { + ctx.save(); + ctx.translate(x, y); + ctx.rotate(rad); + ctx.drawImage(style, -style.width / 2, -style.height / 2, style.width, style.height); + ctx.restore(); + return; } } - return Array.from(set); -} - -const EPSILON = Number.EPSILON || 1e-14; -const getPoint = (points, i) => i < points.length && !points[i].skip && points[i]; -const getValueAxis = (indexAxis) => indexAxis === 'x' ? 'y' : 'x'; -function splineCurve(firstPoint, middlePoint, afterPoint, t) { - const previous = firstPoint.skip ? middlePoint : firstPoint; - const current = middlePoint; - const next = afterPoint.skip ? middlePoint : afterPoint; - const d01 = distanceBetweenPoints(current, previous); - const d12 = distanceBetweenPoints(next, current); - let s01 = d01 / (d01 + d12); - let s12 = d12 / (d01 + d12); - s01 = isNaN(s01) ? 0 : s01; - s12 = isNaN(s12) ? 0 : s12; - const fa = t * s01; - const fb = t * s12; - return { - previous: { - x: current.x - fa * (next.x - previous.x), - y: current.y - fa * (next.y - previous.y) - }, - next: { - x: current.x + fb * (next.x - previous.x), - y: current.y + fb * (next.y - previous.y) - } - }; -} -function monotoneAdjust(points, deltaK, mK) { - const pointsLen = points.length; - let alphaK, betaK, tauK, squaredMagnitude, pointCurrent; - let pointAfter = getPoint(points, 0); - for (let i = 0; i < pointsLen - 1; ++i) { - pointCurrent = pointAfter; - pointAfter = getPoint(points, i + 1); - if (!pointCurrent || !pointAfter) { - continue; - } - if (almostEquals(deltaK[i], 0, EPSILON)) { - mK[i] = mK[i + 1] = 0; - continue; - } - alphaK = mK[i] / deltaK[i]; - betaK = mK[i + 1] / deltaK[i]; - squaredMagnitude = Math.pow(alphaK, 2) + Math.pow(betaK, 2); - if (squaredMagnitude <= 9) { - continue; - } - tauK = 3 / Math.sqrt(squaredMagnitude); - mK[i] = alphaK * tauK * deltaK[i]; - mK[i + 1] = betaK * tauK * deltaK[i]; + if (isNaN(radius) || radius <= 0) { + return; } -} -function monotoneCompute(points, mK, indexAxis = 'x') { - const valueAxis = getValueAxis(indexAxis); - const pointsLen = points.length; - let delta, pointBefore, pointCurrent; - let pointAfter = getPoint(points, 0); - for (let i = 0; i < pointsLen; ++i) { - pointBefore = pointCurrent; - pointCurrent = pointAfter; - pointAfter = getPoint(points, i + 1); - if (!pointCurrent) { - continue; - } - const iPixel = pointCurrent[indexAxis]; - const vPixel = pointCurrent[valueAxis]; - if (pointBefore) { - delta = (iPixel - pointBefore[indexAxis]) / 3; - pointCurrent[`cp1${indexAxis}`] = iPixel - delta; - pointCurrent[`cp1${valueAxis}`] = vPixel - delta * mK[i]; + ctx.beginPath(); + switch (style) { + default: + if (w) { + ctx.ellipse(x, y, w / 2, radius, 0, 0, TAU); + } else { + ctx.arc(x, y, radius, 0, TAU); } - if (pointAfter) { - delta = (pointAfter[indexAxis] - iPixel) / 3; - pointCurrent[`cp2${indexAxis}`] = iPixel + delta; - pointCurrent[`cp2${valueAxis}`] = vPixel + delta * mK[i]; + ctx.closePath(); + break; + case 'triangle': + ctx.moveTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + rad += TWO_THIRDS_PI; + ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + rad += TWO_THIRDS_PI; + ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + ctx.closePath(); + break; + case 'rectRounded': + cornerRadius = radius * 0.516; + size = radius - cornerRadius; + xOffset = Math.cos(rad + QUARTER_PI) * size; + yOffset = Math.sin(rad + QUARTER_PI) * size; + ctx.arc(x - xOffset, y - yOffset, cornerRadius, rad - PI, rad - HALF_PI); + ctx.arc(x + yOffset, y - xOffset, cornerRadius, rad - HALF_PI, rad); + ctx.arc(x + xOffset, y + yOffset, cornerRadius, rad, rad + HALF_PI); + ctx.arc(x - yOffset, y + xOffset, cornerRadius, rad + HALF_PI, rad + PI); + ctx.closePath(); + break; + case 'rect': + if (!rotation) { + size = Math.SQRT1_2 * radius; + width = w ? w / 2 : size; + ctx.rect(x - width, y - size, 2 * width, 2 * size); + break; } + rad += QUARTER_PI; + case 'rectRot': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + yOffset, y - xOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.lineTo(x - yOffset, y + xOffset); + ctx.closePath(); + break; + case 'crossRot': + rad += QUARTER_PI; + case 'cross': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + break; + case 'star': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + rad += QUARTER_PI; + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + break; + case 'line': + xOffset = w ? w / 2 : Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + break; + case 'dash': + ctx.moveTo(x, y); + ctx.lineTo(x + Math.cos(rad) * radius, y + Math.sin(rad) * radius); + break; } -} -function splineCurveMonotone(points, indexAxis = 'x') { - const valueAxis = getValueAxis(indexAxis); - const pointsLen = points.length; - const deltaK = Array(pointsLen).fill(0); - const mK = Array(pointsLen); - let i, pointBefore, pointCurrent; - let pointAfter = getPoint(points, 0); - for (i = 0; i < pointsLen; ++i) { - pointBefore = pointCurrent; - pointCurrent = pointAfter; - pointAfter = getPoint(points, i + 1); - if (!pointCurrent) { - continue; - } - if (pointAfter) { - const slopeDelta = pointAfter[indexAxis] - pointCurrent[indexAxis]; - deltaK[i] = slopeDelta !== 0 ? (pointAfter[valueAxis] - pointCurrent[valueAxis]) / slopeDelta : 0; - } - mK[i] = !pointBefore ? deltaK[i] - : !pointAfter ? deltaK[i - 1] - : (sign(deltaK[i - 1]) !== sign(deltaK[i])) ? 0 - : (deltaK[i - 1] + deltaK[i]) / 2; + ctx.fill(); + if (options.borderWidth > 0) { + ctx.stroke(); } - monotoneAdjust(points, deltaK, mK); - monotoneCompute(points, mK, indexAxis); } -function capControlPoint(pt, min, max) { - return Math.max(Math.min(pt, max), min); +function _isPointInArea(point, area, margin) { + margin = margin || 0.5; + return !area || (point && point.x > area.left - margin && point.x < area.right + margin && + point.y > area.top - margin && point.y < area.bottom + margin); } -function capBezierPoints(points, area) { - let i, ilen, point, inArea, inAreaPrev; - let inAreaNext = _isPointInArea(points[0], area); - for (i = 0, ilen = points.length; i < ilen; ++i) { - inAreaPrev = inArea; - inArea = inAreaNext; - inAreaNext = i < ilen - 1 && _isPointInArea(points[i + 1], area); - if (!inArea) { - continue; - } - point = points[i]; - if (inAreaPrev) { - point.cp1x = capControlPoint(point.cp1x, area.left, area.right); - point.cp1y = capControlPoint(point.cp1y, area.top, area.bottom); - } - if (inAreaNext) { - point.cp2x = capControlPoint(point.cp2x, area.left, area.right); - point.cp2y = capControlPoint(point.cp2y, area.top, area.bottom); - } - } +function clipArea(ctx, area) { + ctx.save(); + ctx.beginPath(); + ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); + ctx.clip(); } -function _updateBezierControlPoints(points, options, area, loop, indexAxis) { - let i, ilen, point, controlPoints; - if (options.spanGaps) { - points = points.filter((pt) => !pt.skip); +function unclipArea(ctx) { + ctx.restore(); +} +function _steppedLineTo(ctx, previous, target, flip, mode) { + if (!previous) { + return ctx.lineTo(target.x, target.y); } - if (options.cubicInterpolationMode === 'monotone') { - splineCurveMonotone(points, indexAxis); + if (mode === 'middle') { + const midpoint = (previous.x + target.x) / 2.0; + ctx.lineTo(midpoint, previous.y); + ctx.lineTo(midpoint, target.y); + } else if (mode === 'after' !== !!flip) { + ctx.lineTo(previous.x, target.y); } else { - let prev = loop ? points[points.length - 1] : points[0]; - for (i = 0, ilen = points.length; i < ilen; ++i) { - point = points[i]; - controlPoints = splineCurve( - prev, - point, - points[Math.min(i + 1, ilen - (loop ? 0 : 1)) % ilen], - options.tension - ); - point.cp1x = controlPoints.previous.x; - point.cp1y = controlPoints.previous.y; - point.cp2x = controlPoints.next.x; - point.cp2y = controlPoints.next.y; - prev = point; - } - } - if (options.capBezierPoints) { - capBezierPoints(points, area); + ctx.lineTo(target.x, previous.y); } + ctx.lineTo(target.x, target.y); } - -const atEdge = (t) => t === 0 || t === 1; -const elasticIn = (t, s, p) => -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p)); -const elasticOut = (t, s, p) => Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1; -const effects = { - linear: t => t, - easeInQuad: t => t * t, - easeOutQuad: t => -t * (t - 2), - easeInOutQuad: t => ((t /= 0.5) < 1) - ? 0.5 * t * t - : -0.5 * ((--t) * (t - 2) - 1), - easeInCubic: t => t * t * t, - easeOutCubic: t => (t -= 1) * t * t + 1, - easeInOutCubic: t => ((t /= 0.5) < 1) - ? 0.5 * t * t * t - : 0.5 * ((t -= 2) * t * t + 2), - easeInQuart: t => t * t * t * t, - easeOutQuart: t => -((t -= 1) * t * t * t - 1), - easeInOutQuart: t => ((t /= 0.5) < 1) - ? 0.5 * t * t * t * t - : -0.5 * ((t -= 2) * t * t * t - 2), - easeInQuint: t => t * t * t * t * t, - easeOutQuint: t => (t -= 1) * t * t * t * t + 1, - easeInOutQuint: t => ((t /= 0.5) < 1) - ? 0.5 * t * t * t * t * t - : 0.5 * ((t -= 2) * t * t * t * t + 2), - easeInSine: t => -Math.cos(t * HALF_PI) + 1, - easeOutSine: t => Math.sin(t * HALF_PI), - easeInOutSine: t => -0.5 * (Math.cos(PI * t) - 1), - easeInExpo: t => (t === 0) ? 0 : Math.pow(2, 10 * (t - 1)), - easeOutExpo: t => (t === 1) ? 1 : -Math.pow(2, -10 * t) + 1, - easeInOutExpo: t => atEdge(t) ? t : t < 0.5 - ? 0.5 * Math.pow(2, 10 * (t * 2 - 1)) - : 0.5 * (-Math.pow(2, -10 * (t * 2 - 1)) + 2), - easeInCirc: t => (t >= 1) ? t : -(Math.sqrt(1 - t * t) - 1), - easeOutCirc: t => Math.sqrt(1 - (t -= 1) * t), - easeInOutCirc: t => ((t /= 0.5) < 1) - ? -0.5 * (Math.sqrt(1 - t * t) - 1) - : 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1), - easeInElastic: t => atEdge(t) ? t : elasticIn(t, 0.075, 0.3), - easeOutElastic: t => atEdge(t) ? t : elasticOut(t, 0.075, 0.3), - easeInOutElastic(t) { - const s = 0.1125; - const p = 0.45; - return atEdge(t) ? t : - t < 0.5 - ? 0.5 * elasticIn(t * 2, s, p) - : 0.5 + 0.5 * elasticOut(t * 2 - 1, s, p); - }, - easeInBack(t) { - const s = 1.70158; - return t * t * ((s + 1) * t - s); - }, - easeOutBack(t) { - const s = 1.70158; - return (t -= 1) * t * ((s + 1) * t + s) + 1; - }, - easeInOutBack(t) { - let s = 1.70158; - if ((t /= 0.5) < 1) { - return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)); - } - return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2); - }, - easeInBounce: t => 1 - effects.easeOutBounce(1 - t), - easeOutBounce(t) { - const m = 7.5625; - const d = 2.75; - if (t < (1 / d)) { - return m * t * t; - } - if (t < (2 / d)) { - return m * (t -= (1.5 / d)) * t + 0.75; - } - if (t < (2.5 / d)) { - return m * (t -= (2.25 / d)) * t + 0.9375; - } - return m * (t -= (2.625 / d)) * t + 0.984375; - }, - easeInOutBounce: t => (t < 0.5) - ? effects.easeInBounce(t * 2) * 0.5 - : effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5, -}; - -function _pointInLine(p1, p2, t, mode) { - return { - x: p1.x + t * (p2.x - p1.x), - y: p1.y + t * (p2.y - p1.y) - }; +function _bezierCurveTo(ctx, previous, target, flip) { + if (!previous) { + return ctx.lineTo(target.x, target.y); + } + ctx.bezierCurveTo( + flip ? previous.cp1x : previous.cp2x, + flip ? previous.cp1y : previous.cp2y, + flip ? target.cp2x : target.cp1x, + flip ? target.cp2y : target.cp1y, + target.x, + target.y); } -function _steppedInterpolation(p1, p2, t, mode) { - return { - x: p1.x + t * (p2.x - p1.x), - y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y - : mode === 'after' ? t < 1 ? p1.y : p2.y - : t > 0 ? p2.y : p1.y - }; +function renderText(ctx, text, x, y, font, opts = {}) { + const lines = isArray(text) ? text : [text]; + const stroke = opts.strokeWidth > 0 && opts.strokeColor !== ''; + let i, line; + ctx.save(); + ctx.font = font.string; + setRenderOpts(ctx, opts); + for (i = 0; i < lines.length; ++i) { + line = lines[i]; + if (stroke) { + if (opts.strokeColor) { + ctx.strokeStyle = opts.strokeColor; + } + if (!isNullOrUndef(opts.strokeWidth)) { + ctx.lineWidth = opts.strokeWidth; + } + ctx.strokeText(line, x, y, opts.maxWidth); + } + ctx.fillText(line, x, y, opts.maxWidth); + decorateText(ctx, x, y, line, opts); + y += font.lineHeight; + } + ctx.restore(); } -function _bezierInterpolation(p1, p2, t, mode) { - const cp1 = {x: p1.cp2x, y: p1.cp2y}; - const cp2 = {x: p2.cp1x, y: p2.cp1y}; - const a = _pointInLine(p1, cp1, t); - const b = _pointInLine(cp1, cp2, t); - const c = _pointInLine(cp2, p2, t); - const d = _pointInLine(a, b, t); - const e = _pointInLine(b, c, t); - return _pointInLine(d, e, t); +function setRenderOpts(ctx, opts) { + if (opts.translation) { + ctx.translate(opts.translation[0], opts.translation[1]); + } + if (!isNullOrUndef(opts.rotation)) { + ctx.rotate(opts.rotation); + } + if (opts.color) { + ctx.fillStyle = opts.color; + } + if (opts.textAlign) { + ctx.textAlign = opts.textAlign; + } + if (opts.textBaseline) { + ctx.textBaseline = opts.textBaseline; + } } - -const intlCache = new Map(); -function getNumberFormat(locale, options) { - options = options || {}; - const cacheKey = locale + JSON.stringify(options); - let formatter = intlCache.get(cacheKey); - if (!formatter) { - formatter = new Intl.NumberFormat(locale, options); - intlCache.set(cacheKey, formatter); +function decorateText(ctx, x, y, line, opts) { + if (opts.strikethrough || opts.underline) { + const metrics = ctx.measureText(line); + const left = x - metrics.actualBoundingBoxLeft; + const right = x + metrics.actualBoundingBoxRight; + const top = y - metrics.actualBoundingBoxAscent; + const bottom = y + metrics.actualBoundingBoxDescent; + const yDecoration = opts.strikethrough ? (top + bottom) / 2 : bottom; + ctx.strokeStyle = ctx.fillStyle; + ctx.beginPath(); + ctx.lineWidth = opts.decorationWidth || 2; + ctx.moveTo(left, yDecoration); + ctx.lineTo(right, yDecoration); + ctx.stroke(); } - return formatter; } -function formatNumber(num, locale, options) { - return getNumberFormat(locale, options).format(num); +function addRoundedRectPath(ctx, rect) { + const {x, y, w, h, radius} = rect; + ctx.arc(x + radius.topLeft, y + radius.topLeft, radius.topLeft, -HALF_PI, PI, true); + ctx.lineTo(x, y + h - radius.bottomLeft); + ctx.arc(x + radius.bottomLeft, y + h - radius.bottomLeft, radius.bottomLeft, PI, HALF_PI, true); + ctx.lineTo(x + w - radius.bottomRight, y + h); + ctx.arc(x + w - radius.bottomRight, y + h - radius.bottomRight, radius.bottomRight, HALF_PI, 0, true); + ctx.lineTo(x + w, y + radius.topRight); + ctx.arc(x + w - radius.topRight, y + radius.topRight, radius.topRight, 0, -HALF_PI, true); + ctx.lineTo(x + radius.topLeft, y); } -const getRightToLeftAdapter = function(rectX, width) { - return { - x(x) { - return rectX + rectX + width - x; - }, - setWidth(w) { - width = w; - }, - textAlign(align) { - if (align === 'center') { - return align; - } - return align === 'right' ? 'left' : 'right'; - }, - xPlus(x, value) { - return x - value; - }, - leftForLtr(x, itemWidth) { - return x - itemWidth; - }, +function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fallback, getTarget = () => scopes[0]) { + if (!defined(fallback)) { + fallback = _resolve('_fallback', scopes); + } + const cache = { + [Symbol.toStringTag]: 'Object', + _cacheable: true, + _scopes: scopes, + _rootScopes: rootScopes, + _fallback: fallback, + _getTarget: getTarget, + override: (scope) => _createResolver([scope, ...scopes], prefixes, rootScopes, fallback), }; -}; -const getLeftToRightAdapter = function() { - return { - x(x) { - return x; + return new Proxy(cache, { + deleteProperty(target, prop) { + delete target[prop]; + delete target._keys; + delete scopes[0][prop]; + return true; }, - setWidth(w) { + get(target, prop) { + return _cached(target, prop, + () => _resolveWithPrefixes(prop, prefixes, scopes, target)); }, - textAlign(align) { - return align; + getOwnPropertyDescriptor(target, prop) { + return Reflect.getOwnPropertyDescriptor(target._scopes[0], prop); }, - xPlus(x, value) { - return x + value; + getPrototypeOf() { + return Reflect.getPrototypeOf(scopes[0]); }, - leftForLtr(x, _itemWidth) { - return x; + has(target, prop) { + return getKeysFromAllScopes(target).includes(prop); }, - }; -}; -function getRtlAdapter(rtl, rectX, width) { - return rtl ? getRightToLeftAdapter(rectX, width) : getLeftToRightAdapter(); + ownKeys(target) { + return getKeysFromAllScopes(target); + }, + set(target, prop, value) { + const storage = target._storage || (target._storage = getTarget()); + target[prop] = storage[prop] = value; + delete target._keys; + return true; + } + }); } -function overrideTextDirection(ctx, direction) { - let style, original; - if (direction === 'ltr' || direction === 'rtl') { - style = ctx.canvas.style; - original = [ - style.getPropertyValue('direction'), - style.getPropertyPriority('direction'), - ]; - style.setProperty('direction', direction, 'important'); - ctx.prevTextDirection = original; - } -} -function restoreTextDirection(ctx, original) { - if (original !== undefined) { - delete ctx.prevTextDirection; - ctx.canvas.style.setProperty('direction', original[0], original[1]); - } -} - -function propertyFn(property) { - if (property === 'angle') { - return { - between: _angleBetween, - compare: _angleDiff, - normalize: _normalizeAngle, - }; - } - return { - between: _isBetween, - compare: (a, b) => a - b, - normalize: x => x +function _attachContext(proxy, context, subProxy, descriptorDefaults) { + const cache = { + _cacheable: false, + _proxy: proxy, + _context: context, + _subProxy: subProxy, + _stack: new Set(), + _descriptors: _descriptors(proxy, descriptorDefaults), + setContext: (ctx) => _attachContext(proxy, ctx, subProxy, descriptorDefaults), + override: (scope) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults) }; + return new Proxy(cache, { + deleteProperty(target, prop) { + delete target[prop]; + delete proxy[prop]; + return true; + }, + get(target, prop, receiver) { + return _cached(target, prop, + () => _resolveWithContext(target, prop, receiver)); + }, + getOwnPropertyDescriptor(target, prop) { + return target._descriptors.allKeys + ? Reflect.has(proxy, prop) ? {enumerable: true, configurable: true} : undefined + : Reflect.getOwnPropertyDescriptor(proxy, prop); + }, + getPrototypeOf() { + return Reflect.getPrototypeOf(proxy); + }, + has(target, prop) { + return Reflect.has(proxy, prop); + }, + ownKeys() { + return Reflect.ownKeys(proxy); + }, + set(target, prop, value) { + proxy[prop] = value; + delete target[prop]; + return true; + } + }); } -function normalizeSegment({start, end, count, loop, style}) { +function _descriptors(proxy, defaults = {scriptable: true, indexable: true}) { + const {_scriptable = defaults.scriptable, _indexable = defaults.indexable, _allKeys = defaults.allKeys} = proxy; return { - start: start % count, - end: end % count, - loop: loop && (end - start + 1) % count === 0, - style + allKeys: _allKeys, + scriptable: _scriptable, + indexable: _indexable, + isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable, + isIndexable: isFunction(_indexable) ? _indexable : () => _indexable }; } -function getSegment(segment, points, bounds) { - const {property, start: startBound, end: endBound} = bounds; - const {between, normalize} = propertyFn(property); - const count = points.length; - let {start, end, loop} = segment; - let i, ilen; - if (loop) { - start += count; - end += count; - for (i = 0, ilen = count; i < ilen; ++i) { - if (!between(normalize(points[start % count][property]), startBound, endBound)) { - break; - } - start--; - end--; - } - start %= count; - end %= count; +const readKey = (prefix, name) => prefix ? prefix + _capitalize(name) : name; +const needsSubResolver = (prop, value) => isObject(value) && prop !== 'adapters' && + (Object.getPrototypeOf(value) === null || value.constructor === Object); +function _cached(target, prop, resolve) { + if (Object.prototype.hasOwnProperty.call(target, prop)) { + return target[prop]; } - if (end < start) { - end += count; + const value = resolve(); + target[prop] = value; + return value; +} +function _resolveWithContext(target, prop, receiver) { + const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; + let value = _proxy[prop]; + if (isFunction(value) && descriptors.isScriptable(prop)) { + value = _resolveScriptable(prop, value, target, receiver); } - return {start, end, loop, style: segment.style}; + if (isArray(value) && value.length) { + value = _resolveArray(prop, value, target, descriptors.isIndexable); + } + if (needsSubResolver(prop, value)) { + value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors); + } + return value; } -function _boundSegment(segment, points, bounds) { - if (!bounds) { - return [segment]; +function _resolveScriptable(prop, value, target, receiver) { + const {_proxy, _context, _subProxy, _stack} = target; + if (_stack.has(prop)) { + throw new Error('Recursion detected: ' + Array.from(_stack).join('->') + '->' + prop); } - const {property, start: startBound, end: endBound} = bounds; - const count = points.length; - const {compare, between, normalize} = propertyFn(property); - const {start, end, loop, style} = getSegment(segment, points, bounds); - const result = []; - let inside = false; - let subStart = null; - let value, point, prevValue; - const startIsBefore = () => between(startBound, prevValue, value) && compare(startBound, prevValue) !== 0; - const endIsBefore = () => compare(endBound, value) === 0 || between(endBound, prevValue, value); - const shouldStart = () => inside || startIsBefore(); - const shouldStop = () => !inside || endIsBefore(); - for (let i = start, prev = start; i <= end; ++i) { - point = points[i % count]; - if (point.skip) { - continue; - } - value = normalize(point[property]); - if (value === prevValue) { - continue; - } - inside = between(value, startBound, endBound); - if (subStart === null && shouldStart()) { - subStart = compare(value, startBound) === 0 ? i : prev; - } - if (subStart !== null && shouldStop()) { - result.push(normalizeSegment({start: subStart, end: i, loop, count, style})); - subStart = null; - } - prev = i; - prevValue = value; + _stack.add(prop); + value = value(_context, _subProxy || receiver); + _stack.delete(prop); + if (needsSubResolver(prop, value)) { + value = createSubResolver(_proxy._scopes, _proxy, prop, value); } - if (subStart !== null) { - result.push(normalizeSegment({start: subStart, end, loop, count, style})); + return value; +} +function _resolveArray(prop, value, target, isIndexable) { + const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; + if (defined(_context.index) && isIndexable(prop)) { + value = value[_context.index % value.length]; + } else if (isObject(value[0])) { + const arr = value; + const scopes = _proxy._scopes.filter(s => s !== arr); + value = []; + for (const item of arr) { + const resolver = createSubResolver(scopes, _proxy, prop, item); + value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors)); + } } - return result; + return value; } -function _boundSegments(line, bounds) { - const result = []; - const segments = line.segments; - for (let i = 0; i < segments.length; i++) { - const sub = _boundSegment(segments[i], line.points, bounds); - if (sub.length) { - result.push(...sub); +function resolveFallback(fallback, prop, value) { + return isFunction(fallback) ? fallback(prop, value) : fallback; +} +const getScope = (key, parent) => key === true ? parent + : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; +function addScopes(set, parentScopes, key, parentFallback, value) { + for (const parent of parentScopes) { + const scope = getScope(key, parent); + if (scope) { + set.add(scope); + const fallback = resolveFallback(scope._fallback, key, value); + if (defined(fallback) && fallback !== key && fallback !== parentFallback) { + return fallback; + } + } else if (scope === false && defined(parentFallback) && key !== parentFallback) { + return null; } } - return result; + return false; } -function findStartAndEnd(points, count, loop, spanGaps) { - let start = 0; - let end = count - 1; - if (loop && !spanGaps) { - while (start < count && !points[start].skip) { - start++; +function createSubResolver(parentScopes, resolver, prop, value) { + const rootScopes = resolver._rootScopes; + const fallback = resolveFallback(resolver._fallback, prop, value); + const allScopes = [...parentScopes, ...rootScopes]; + const set = new Set(); + set.add(value); + let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); + if (key === null) { + return false; + } + if (defined(fallback) && fallback !== prop) { + key = addScopesFromKey(set, allScopes, fallback, key, value); + if (key === null) { + return false; } } - while (start < count && points[start].skip) { - start++; + return _createResolver(Array.from(set), [''], rootScopes, fallback, + () => subGetTarget(resolver, prop, value)); +} +function addScopesFromKey(set, allScopes, key, fallback, item) { + while (key) { + key = addScopes(set, allScopes, key, fallback, item); } - start %= count; - if (loop) { - end += start; + return key; +} +function subGetTarget(resolver, prop, value) { + const parent = resolver._getTarget(); + if (!(prop in parent)) { + parent[prop] = {}; } - while (end > start && points[end % count].skip) { - end--; + const target = parent[prop]; + if (isArray(target) && isObject(value)) { + return value; } - end %= count; - return {start, end}; + return target; } -function solidSegments(points, start, max, loop) { - const count = points.length; - const result = []; - let last = start; - let prev = points[start]; - let end; - for (end = start + 1; end <= max; ++end) { - const cur = points[end % count]; - if (cur.skip || cur.stop) { - if (!prev.skip) { - loop = false; - result.push({start: start % count, end: (end - 1) % count, loop}); - start = last = cur.stop ? end : null; - } - } else { - last = end; - if (prev.skip) { - start = end; - } +function _resolveWithPrefixes(prop, prefixes, scopes, proxy) { + let value; + for (const prefix of prefixes) { + value = _resolve(readKey(prefix, prop), scopes); + if (defined(value)) { + return needsSubResolver(prop, value) + ? createSubResolver(scopes, proxy, prop, value) + : value; } - prev = cur; } - if (last !== null) { - result.push({start: start % count, end: last % count, loop}); +} +function _resolve(key, scopes) { + for (const scope of scopes) { + if (!scope) { + continue; + } + const value = scope[key]; + if (defined(value)) { + return value; + } } - return result; } -function _computeSegments(line, segmentOptions) { - const points = line.points; - const spanGaps = line.options.spanGaps; - const count = points.length; - if (!count) { - return []; +function getKeysFromAllScopes(target) { + let keys = target._keys; + if (!keys) { + keys = target._keys = resolveKeysFromAllScopes(target._scopes); } - const loop = !!line._loop; - const {start, end} = findStartAndEnd(points, count, loop, spanGaps); - if (spanGaps === true) { - return splitByStyles(line, [{start, end, loop}], points, segmentOptions); + return keys; +} +function resolveKeysFromAllScopes(scopes) { + const set = new Set(); + for (const scope of scopes) { + for (const key of Object.keys(scope).filter(k => !k.startsWith('_'))) { + set.add(key); + } } - const max = end < start ? end + count : end; - const completeLoop = !!line._fullLoop && start === 0 && end === count - 1; - return splitByStyles(line, solidSegments(points, start, max, completeLoop), points, segmentOptions); + return Array.from(set); } -function splitByStyles(line, segments, points, segmentOptions) { - if (!segmentOptions || !segmentOptions.setContext || !points) { - return segments; +function _parseObjectDataRadialScale(meta, data, start, count) { + const {iScale} = meta; + const {key = 'r'} = this._parsing; + const parsed = new Array(count); + let i, ilen, index, item; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + item = data[index]; + parsed[i] = { + r: iScale.parse(resolveObjectKey(item, key), index) + }; } - return doSplitByStyles(line, segments, points, segmentOptions); + return parsed; } -function doSplitByStyles(line, segments, points, segmentOptions) { - const chartContext = line._chart.getContext(); - const baseStyle = readStyle(line.options); - const {_datasetIndex: datasetIndex, options: {spanGaps}} = line; - const count = points.length; - const result = []; - let prevStyle = baseStyle; - let start = segments[0].start; - let i = start; - function addStyle(s, e, l, st) { - const dir = spanGaps ? -1 : 1; - if (s === e) { - return; + +const EPSILON = Number.EPSILON || 1e-14; +const getPoint = (points, i) => i < points.length && !points[i].skip && points[i]; +const getValueAxis = (indexAxis) => indexAxis === 'x' ? 'y' : 'x'; +function splineCurve(firstPoint, middlePoint, afterPoint, t) { + const previous = firstPoint.skip ? middlePoint : firstPoint; + const current = middlePoint; + const next = afterPoint.skip ? middlePoint : afterPoint; + const d01 = distanceBetweenPoints(current, previous); + const d12 = distanceBetweenPoints(next, current); + let s01 = d01 / (d01 + d12); + let s12 = d12 / (d01 + d12); + s01 = isNaN(s01) ? 0 : s01; + s12 = isNaN(s12) ? 0 : s12; + const fa = t * s01; + const fb = t * s12; + return { + previous: { + x: current.x - fa * (next.x - previous.x), + y: current.y - fa * (next.y - previous.y) + }, + next: { + x: current.x + fb * (next.x - previous.x), + y: current.y + fb * (next.y - previous.y) } - s += count; - while (points[s % count].skip) { - s -= dir; + }; +} +function monotoneAdjust(points, deltaK, mK) { + const pointsLen = points.length; + let alphaK, betaK, tauK, squaredMagnitude, pointCurrent; + let pointAfter = getPoint(points, 0); + for (let i = 0; i < pointsLen - 1; ++i) { + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent || !pointAfter) { + continue; } - while (points[e % count].skip) { - e += dir; + if (almostEquals(deltaK[i], 0, EPSILON)) { + mK[i] = mK[i + 1] = 0; + continue; } - if (s % count !== e % count) { - result.push({start: s % count, end: e % count, loop: l, style: st}); - prevStyle = st; - start = e % count; + alphaK = mK[i] / deltaK[i]; + betaK = mK[i + 1] / deltaK[i]; + squaredMagnitude = Math.pow(alphaK, 2) + Math.pow(betaK, 2); + if (squaredMagnitude <= 9) { + continue; } + tauK = 3 / Math.sqrt(squaredMagnitude); + mK[i] = alphaK * tauK * deltaK[i]; + mK[i + 1] = betaK * tauK * deltaK[i]; } - for (const segment of segments) { - start = spanGaps ? start : segment.start; - let prev = points[start % count]; - let style; - for (i = start + 1; i <= segment.end; i++) { - const pt = points[i % count]; - style = readStyle(segmentOptions.setContext(createContext(chartContext, { - type: 'segment', - p0: prev, - p1: pt, - p0DataIndex: (i - 1) % count, - p1DataIndex: i % count, - datasetIndex - }))); - if (styleChanged(style, prevStyle)) { - addStyle(start, i - 1, segment.loop, prevStyle); - } - prev = pt; - prevStyle = style; +} +function monotoneCompute(points, mK, indexAxis = 'x') { + const valueAxis = getValueAxis(indexAxis); + const pointsLen = points.length; + let delta, pointBefore, pointCurrent; + let pointAfter = getPoint(points, 0); + for (let i = 0; i < pointsLen; ++i) { + pointBefore = pointCurrent; + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent) { + continue; } - if (start < i - 1) { - addStyle(start, i - 1, segment.loop, prevStyle); + const iPixel = pointCurrent[indexAxis]; + const vPixel = pointCurrent[valueAxis]; + if (pointBefore) { + delta = (iPixel - pointBefore[indexAxis]) / 3; + pointCurrent[`cp1${indexAxis}`] = iPixel - delta; + pointCurrent[`cp1${valueAxis}`] = vPixel - delta * mK[i]; + } + if (pointAfter) { + delta = (pointAfter[indexAxis] - iPixel) / 3; + pointCurrent[`cp2${indexAxis}`] = iPixel + delta; + pointCurrent[`cp2${valueAxis}`] = vPixel + delta * mK[i]; } } - return result; } -function readStyle(options) { - return { - backgroundColor: options.backgroundColor, - borderCapStyle: options.borderCapStyle, - borderDash: options.borderDash, - borderDashOffset: options.borderDashOffset, - borderJoinStyle: options.borderJoinStyle, - borderWidth: options.borderWidth, - borderColor: options.borderColor - }; +function splineCurveMonotone(points, indexAxis = 'x') { + const valueAxis = getValueAxis(indexAxis); + const pointsLen = points.length; + const deltaK = Array(pointsLen).fill(0); + const mK = Array(pointsLen); + let i, pointBefore, pointCurrent; + let pointAfter = getPoint(points, 0); + for (i = 0; i < pointsLen; ++i) { + pointBefore = pointCurrent; + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent) { + continue; + } + if (pointAfter) { + const slopeDelta = pointAfter[indexAxis] - pointCurrent[indexAxis]; + deltaK[i] = slopeDelta !== 0 ? (pointAfter[valueAxis] - pointCurrent[valueAxis]) / slopeDelta : 0; + } + mK[i] = !pointBefore ? deltaK[i] + : !pointAfter ? deltaK[i - 1] + : (sign(deltaK[i - 1]) !== sign(deltaK[i])) ? 0 + : (deltaK[i - 1] + deltaK[i]) / 2; + } + monotoneAdjust(points, deltaK, mK); + monotoneCompute(points, mK, indexAxis); } -function styleChanged(style, prevStyle) { - return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle); +function capControlPoint(pt, min, max) { + return Math.max(Math.min(pt, max), min); } - -var helpers = /*#__PURE__*/Object.freeze({ -__proto__: null, -easingEffects: effects, -color: color, -getHoverColor: getHoverColor, -noop: noop, -uid: uid, -isNullOrUndef: isNullOrUndef, -isArray: isArray, -isObject: isObject, -isFinite: isNumberFinite, -finiteOrDefault: finiteOrDefault, -valueOrDefault: valueOrDefault, -toPercentage: toPercentage, -toDimension: toDimension, -callback: callback, -each: each, -_elementsEqual: _elementsEqual, -clone: clone, -_merger: _merger, -merge: merge, -mergeIf: mergeIf, -_mergerIf: _mergerIf, -_deprecated: _deprecated, -resolveObjectKey: resolveObjectKey, -_capitalize: _capitalize, -defined: defined, -isFunction: isFunction, -setsEqual: setsEqual, -_isClickEvent: _isClickEvent, +function capBezierPoints(points, area) { + let i, ilen, point, inArea, inAreaPrev; + let inAreaNext = _isPointInArea(points[0], area); + for (i = 0, ilen = points.length; i < ilen; ++i) { + inAreaPrev = inArea; + inArea = inAreaNext; + inAreaNext = i < ilen - 1 && _isPointInArea(points[i + 1], area); + if (!inArea) { + continue; + } + point = points[i]; + if (inAreaPrev) { + point.cp1x = capControlPoint(point.cp1x, area.left, area.right); + point.cp1y = capControlPoint(point.cp1y, area.top, area.bottom); + } + if (inAreaNext) { + point.cp2x = capControlPoint(point.cp2x, area.left, area.right); + point.cp2y = capControlPoint(point.cp2y, area.top, area.bottom); + } + } +} +function _updateBezierControlPoints(points, options, area, loop, indexAxis) { + let i, ilen, point, controlPoints; + if (options.spanGaps) { + points = points.filter((pt) => !pt.skip); + } + if (options.cubicInterpolationMode === 'monotone') { + splineCurveMonotone(points, indexAxis); + } else { + let prev = loop ? points[points.length - 1] : points[0]; + for (i = 0, ilen = points.length; i < ilen; ++i) { + point = points[i]; + controlPoints = splineCurve( + prev, + point, + points[Math.min(i + 1, ilen - (loop ? 0 : 1)) % ilen], + options.tension + ); + point.cp1x = controlPoints.previous.x; + point.cp1y = controlPoints.previous.y; + point.cp2x = controlPoints.next.x; + point.cp2y = controlPoints.next.y; + prev = point; + } + } + if (options.capBezierPoints) { + capBezierPoints(points, area); + } +} + +const atEdge = (t) => t === 0 || t === 1; +const elasticIn = (t, s, p) => -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p)); +const elasticOut = (t, s, p) => Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1; +const effects = { + linear: t => t, + easeInQuad: t => t * t, + easeOutQuad: t => -t * (t - 2), + easeInOutQuad: t => ((t /= 0.5) < 1) + ? 0.5 * t * t + : -0.5 * ((--t) * (t - 2) - 1), + easeInCubic: t => t * t * t, + easeOutCubic: t => (t -= 1) * t * t + 1, + easeInOutCubic: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t + : 0.5 * ((t -= 2) * t * t + 2), + easeInQuart: t => t * t * t * t, + easeOutQuart: t => -((t -= 1) * t * t * t - 1), + easeInOutQuart: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t * t + : -0.5 * ((t -= 2) * t * t * t - 2), + easeInQuint: t => t * t * t * t * t, + easeOutQuint: t => (t -= 1) * t * t * t * t + 1, + easeInOutQuint: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t * t * t + : 0.5 * ((t -= 2) * t * t * t * t + 2), + easeInSine: t => -Math.cos(t * HALF_PI) + 1, + easeOutSine: t => Math.sin(t * HALF_PI), + easeInOutSine: t => -0.5 * (Math.cos(PI * t) - 1), + easeInExpo: t => (t === 0) ? 0 : Math.pow(2, 10 * (t - 1)), + easeOutExpo: t => (t === 1) ? 1 : -Math.pow(2, -10 * t) + 1, + easeInOutExpo: t => atEdge(t) ? t : t < 0.5 + ? 0.5 * Math.pow(2, 10 * (t * 2 - 1)) + : 0.5 * (-Math.pow(2, -10 * (t * 2 - 1)) + 2), + easeInCirc: t => (t >= 1) ? t : -(Math.sqrt(1 - t * t) - 1), + easeOutCirc: t => Math.sqrt(1 - (t -= 1) * t), + easeInOutCirc: t => ((t /= 0.5) < 1) + ? -0.5 * (Math.sqrt(1 - t * t) - 1) + : 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1), + easeInElastic: t => atEdge(t) ? t : elasticIn(t, 0.075, 0.3), + easeOutElastic: t => atEdge(t) ? t : elasticOut(t, 0.075, 0.3), + easeInOutElastic(t) { + const s = 0.1125; + const p = 0.45; + return atEdge(t) ? t : + t < 0.5 + ? 0.5 * elasticIn(t * 2, s, p) + : 0.5 + 0.5 * elasticOut(t * 2 - 1, s, p); + }, + easeInBack(t) { + const s = 1.70158; + return t * t * ((s + 1) * t - s); + }, + easeOutBack(t) { + const s = 1.70158; + return (t -= 1) * t * ((s + 1) * t + s) + 1; + }, + easeInOutBack(t) { + let s = 1.70158; + if ((t /= 0.5) < 1) { + return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)); + } + return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2); + }, + easeInBounce: t => 1 - effects.easeOutBounce(1 - t), + easeOutBounce(t) { + const m = 7.5625; + const d = 2.75; + if (t < (1 / d)) { + return m * t * t; + } + if (t < (2 / d)) { + return m * (t -= (1.5 / d)) * t + 0.75; + } + if (t < (2.5 / d)) { + return m * (t -= (2.25 / d)) * t + 0.9375; + } + return m * (t -= (2.625 / d)) * t + 0.984375; + }, + easeInOutBounce: t => (t < 0.5) + ? effects.easeInBounce(t * 2) * 0.5 + : effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5, +}; + +function _pointInLine(p1, p2, t, mode) { + return { + x: p1.x + t * (p2.x - p1.x), + y: p1.y + t * (p2.y - p1.y) + }; +} +function _steppedInterpolation(p1, p2, t, mode) { + return { + x: p1.x + t * (p2.x - p1.x), + y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y + : mode === 'after' ? t < 1 ? p1.y : p2.y + : t > 0 ? p2.y : p1.y + }; +} +function _bezierInterpolation(p1, p2, t, mode) { + const cp1 = {x: p1.cp2x, y: p1.cp2y}; + const cp2 = {x: p2.cp1x, y: p2.cp1y}; + const a = _pointInLine(p1, cp1, t); + const b = _pointInLine(cp1, cp2, t); + const c = _pointInLine(cp2, p2, t); + const d = _pointInLine(a, b, t); + const e = _pointInLine(b, c, t); + return _pointInLine(d, e, t); +} + +const intlCache = new Map(); +function getNumberFormat(locale, options) { + options = options || {}; + const cacheKey = locale + JSON.stringify(options); + let formatter = intlCache.get(cacheKey); + if (!formatter) { + formatter = new Intl.NumberFormat(locale, options); + intlCache.set(cacheKey, formatter); + } + return formatter; +} +function formatNumber(num, locale, options) { + return getNumberFormat(locale, options).format(num); +} + +const LINE_HEIGHT = new RegExp(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/); +const FONT_STYLE = new RegExp(/^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/); +function toLineHeight(value, size) { + const matches = ('' + value).match(LINE_HEIGHT); + if (!matches || matches[1] === 'normal') { + return size * 1.2; + } + value = +matches[2]; + switch (matches[3]) { + case 'px': + return value; + case '%': + value /= 100; + break; + } + return size * value; +} +const numberOrZero = v => +v || 0; +function _readValueToProps(value, props) { + const ret = {}; + const objProps = isObject(props); + const keys = objProps ? Object.keys(props) : props; + const read = isObject(value) + ? objProps + ? prop => valueOrDefault(value[prop], value[props[prop]]) + : prop => value[prop] + : () => value; + for (const prop of keys) { + ret[prop] = numberOrZero(read(prop)); + } + return ret; +} +function toTRBL(value) { + return _readValueToProps(value, {top: 'y', right: 'x', bottom: 'y', left: 'x'}); +} +function toTRBLCorners(value) { + return _readValueToProps(value, ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']); +} +function toPadding(value) { + const obj = toTRBL(value); + obj.width = obj.left + obj.right; + obj.height = obj.top + obj.bottom; + return obj; +} +function toFont(options, fallback) { + options = options || {}; + fallback = fallback || defaults.font; + let size = valueOrDefault(options.size, fallback.size); + if (typeof size === 'string') { + size = parseInt(size, 10); + } + let style = valueOrDefault(options.style, fallback.style); + if (style && !('' + style).match(FONT_STYLE)) { + console.warn('Invalid font style specified: "' + style + '"'); + style = ''; + } + const font = { + family: valueOrDefault(options.family, fallback.family), + lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size), + size, + style, + weight: valueOrDefault(options.weight, fallback.weight), + string: '' + }; + font.string = toFontString(font); + return font; +} +function resolve(inputs, context, index, info) { + let cacheable = true; + let i, ilen, value; + for (i = 0, ilen = inputs.length; i < ilen; ++i) { + value = inputs[i]; + if (value === undefined) { + continue; + } + if (context !== undefined && typeof value === 'function') { + value = value(context); + cacheable = false; + } + if (index !== undefined && isArray(value)) { + value = value[index % value.length]; + cacheable = false; + } + if (value !== undefined) { + if (info && !cacheable) { + info.cacheable = false; + } + return value; + } + } +} +function _addGrace(minmax, grace, beginAtZero) { + const {min, max} = minmax; + const change = toDimension(grace, (max - min) / 2); + const keepZero = (value, add) => beginAtZero && value === 0 ? 0 : value + add; + return { + min: keepZero(min, -Math.abs(change)), + max: keepZero(max, change) + }; +} +function createContext(parentContext, context) { + return Object.assign(Object.create(parentContext), context); +} + +const getRightToLeftAdapter = function(rectX, width) { + return { + x(x) { + return rectX + rectX + width - x; + }, + setWidth(w) { + width = w; + }, + textAlign(align) { + if (align === 'center') { + return align; + } + return align === 'right' ? 'left' : 'right'; + }, + xPlus(x, value) { + return x - value; + }, + leftForLtr(x, itemWidth) { + return x - itemWidth; + }, + }; +}; +const getLeftToRightAdapter = function() { + return { + x(x) { + return x; + }, + setWidth(w) { + }, + textAlign(align) { + return align; + }, + xPlus(x, value) { + return x + value; + }, + leftForLtr(x, _itemWidth) { + return x; + }, + }; +}; +function getRtlAdapter(rtl, rectX, width) { + return rtl ? getRightToLeftAdapter(rectX, width) : getLeftToRightAdapter(); +} +function overrideTextDirection(ctx, direction) { + let style, original; + if (direction === 'ltr' || direction === 'rtl') { + style = ctx.canvas.style; + original = [ + style.getPropertyValue('direction'), + style.getPropertyPriority('direction'), + ]; + style.setProperty('direction', direction, 'important'); + ctx.prevTextDirection = original; + } +} +function restoreTextDirection(ctx, original) { + if (original !== undefined) { + delete ctx.prevTextDirection; + ctx.canvas.style.setProperty('direction', original[0], original[1]); + } +} + +function propertyFn(property) { + if (property === 'angle') { + return { + between: _angleBetween, + compare: _angleDiff, + normalize: _normalizeAngle, + }; + } + return { + between: _isBetween, + compare: (a, b) => a - b, + normalize: x => x + }; +} +function normalizeSegment({start, end, count, loop, style}) { + return { + start: start % count, + end: end % count, + loop: loop && (end - start + 1) % count === 0, + style + }; +} +function getSegment(segment, points, bounds) { + const {property, start: startBound, end: endBound} = bounds; + const {between, normalize} = propertyFn(property); + const count = points.length; + let {start, end, loop} = segment; + let i, ilen; + if (loop) { + start += count; + end += count; + for (i = 0, ilen = count; i < ilen; ++i) { + if (!between(normalize(points[start % count][property]), startBound, endBound)) { + break; + } + start--; + end--; + } + start %= count; + end %= count; + } + if (end < start) { + end += count; + } + return {start, end, loop, style: segment.style}; +} +function _boundSegment(segment, points, bounds) { + if (!bounds) { + return [segment]; + } + const {property, start: startBound, end: endBound} = bounds; + const count = points.length; + const {compare, between, normalize} = propertyFn(property); + const {start, end, loop, style} = getSegment(segment, points, bounds); + const result = []; + let inside = false; + let subStart = null; + let value, point, prevValue; + const startIsBefore = () => between(startBound, prevValue, value) && compare(startBound, prevValue) !== 0; + const endIsBefore = () => compare(endBound, value) === 0 || between(endBound, prevValue, value); + const shouldStart = () => inside || startIsBefore(); + const shouldStop = () => !inside || endIsBefore(); + for (let i = start, prev = start; i <= end; ++i) { + point = points[i % count]; + if (point.skip) { + continue; + } + value = normalize(point[property]); + if (value === prevValue) { + continue; + } + inside = between(value, startBound, endBound); + if (subStart === null && shouldStart()) { + subStart = compare(value, startBound) === 0 ? i : prev; + } + if (subStart !== null && shouldStop()) { + result.push(normalizeSegment({start: subStart, end: i, loop, count, style})); + subStart = null; + } + prev = i; + prevValue = value; + } + if (subStart !== null) { + result.push(normalizeSegment({start: subStart, end, loop, count, style})); + } + return result; +} +function _boundSegments(line, bounds) { + const result = []; + const segments = line.segments; + for (let i = 0; i < segments.length; i++) { + const sub = _boundSegment(segments[i], line.points, bounds); + if (sub.length) { + result.push(...sub); + } + } + return result; +} +function findStartAndEnd(points, count, loop, spanGaps) { + let start = 0; + let end = count - 1; + if (loop && !spanGaps) { + while (start < count && !points[start].skip) { + start++; + } + } + while (start < count && points[start].skip) { + start++; + } + start %= count; + if (loop) { + end += start; + } + while (end > start && points[end % count].skip) { + end--; + } + end %= count; + return {start, end}; +} +function solidSegments(points, start, max, loop) { + const count = points.length; + const result = []; + let last = start; + let prev = points[start]; + let end; + for (end = start + 1; end <= max; ++end) { + const cur = points[end % count]; + if (cur.skip || cur.stop) { + if (!prev.skip) { + loop = false; + result.push({start: start % count, end: (end - 1) % count, loop}); + start = last = cur.stop ? end : null; + } + } else { + last = end; + if (prev.skip) { + start = end; + } + } + prev = cur; + } + if (last !== null) { + result.push({start: start % count, end: last % count, loop}); + } + return result; +} +function _computeSegments(line, segmentOptions) { + const points = line.points; + const spanGaps = line.options.spanGaps; + const count = points.length; + if (!count) { + return []; + } + const loop = !!line._loop; + const {start, end} = findStartAndEnd(points, count, loop, spanGaps); + if (spanGaps === true) { + return splitByStyles(line, [{start, end, loop}], points, segmentOptions); + } + const max = end < start ? end + count : end; + const completeLoop = !!line._fullLoop && start === 0 && end === count - 1; + return splitByStyles(line, solidSegments(points, start, max, completeLoop), points, segmentOptions); +} +function splitByStyles(line, segments, points, segmentOptions) { + if (!segmentOptions || !segmentOptions.setContext || !points) { + return segments; + } + return doSplitByStyles(line, segments, points, segmentOptions); +} +function doSplitByStyles(line, segments, points, segmentOptions) { + const chartContext = line._chart.getContext(); + const baseStyle = readStyle(line.options); + const {_datasetIndex: datasetIndex, options: {spanGaps}} = line; + const count = points.length; + const result = []; + let prevStyle = baseStyle; + let start = segments[0].start; + let i = start; + function addStyle(s, e, l, st) { + const dir = spanGaps ? -1 : 1; + if (s === e) { + return; + } + s += count; + while (points[s % count].skip) { + s -= dir; + } + while (points[e % count].skip) { + e += dir; + } + if (s % count !== e % count) { + result.push({start: s % count, end: e % count, loop: l, style: st}); + prevStyle = st; + start = e % count; + } + } + for (const segment of segments) { + start = spanGaps ? start : segment.start; + let prev = points[start % count]; + let style; + for (i = start + 1; i <= segment.end; i++) { + const pt = points[i % count]; + style = readStyle(segmentOptions.setContext(createContext(chartContext, { + type: 'segment', + p0: prev, + p1: pt, + p0DataIndex: (i - 1) % count, + p1DataIndex: i % count, + datasetIndex + }))); + if (styleChanged(style, prevStyle)) { + addStyle(start, i - 1, segment.loop, prevStyle); + } + prev = pt; + prevStyle = style; + } + if (start < i - 1) { + addStyle(start, i - 1, segment.loop, prevStyle); + } + } + return result; +} +function readStyle(options) { + return { + backgroundColor: options.backgroundColor, + borderCapStyle: options.borderCapStyle, + borderDash: options.borderDash, + borderDashOffset: options.borderDashOffset, + borderJoinStyle: options.borderJoinStyle, + borderWidth: options.borderWidth, + borderColor: options.borderColor + }; +} +function styleChanged(style, prevStyle) { + return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle); +} + +var helpers = /*#__PURE__*/Object.freeze({ +__proto__: null, +easingEffects: effects, +isPatternOrGradient: isPatternOrGradient, +color: color, +getHoverColor: getHoverColor, +noop: noop, +uid: uid, +isNullOrUndef: isNullOrUndef, +isArray: isArray, +isObject: isObject, +isFinite: isNumberFinite, +finiteOrDefault: finiteOrDefault, +valueOrDefault: valueOrDefault, +toPercentage: toPercentage, +toDimension: toDimension, +callback: callback, +each: each, +_elementsEqual: _elementsEqual, +clone: clone$1, +_merger: _merger, +merge: merge, +mergeIf: mergeIf, +_mergerIf: _mergerIf, +_deprecated: _deprecated, +resolveObjectKey: resolveObjectKey, +_splitKey: _splitKey, +_capitalize: _capitalize, +defined: defined, +isFunction: isFunction, +setsEqual: setsEqual, +_isClickEvent: _isClickEvent, toFontString: toFontString, _measureText: _measureText, _longestText: _longestText, _alignPixel: _alignPixel, clearCanvas: clearCanvas, drawPoint: drawPoint, +drawPointLegend: drawPointLegend, _isPointInArea: _isPointInArea, clipArea: clipArea, unclipArea: unclipArea, @@ -3204,13 +2815,14 @@ _arrayUnique: _arrayUnique, _createResolver: _createResolver, _attachContext: _attachContext, _descriptors: _descriptors, +_parseObjectDataRadialScale: _parseObjectDataRadialScale, splineCurve: splineCurve, splineCurveMonotone: splineCurveMonotone, _updateBezierControlPoints: _updateBezierControlPoints, _isDomSupported: _isDomSupported, _getParentNode: _getParentNode, getStyle: getStyle, -getRelativePosition: getRelativePosition$1, +getRelativePosition: getRelativePosition, getMaximumSize: getMaximumSize, retinaScale: retinaScale, supportsEventListenerOptions: supportsEventListenerOptions, @@ -3222,6 +2834,8 @@ debounce: debounce, _toLeftRightCenter: _toLeftRightCenter, _alignStartEnd: _alignStartEnd, _textX: _textX, +_getStartAndCountOfVisiblePoints: _getStartAndCountOfVisiblePoints, +_scaleRangesChanged: _scaleRangesChanged, _pointInLine: _pointInLine, _steppedInterpolation: _steppedInterpolation, _bezierInterpolation: _bezierInterpolation, @@ -3270,6 +2884,498 @@ _boundSegments: _boundSegments, _computeSegments: _computeSegments }); +function binarySearch(metaset, axis, value, intersect) { + const {controller, data, _sorted} = metaset; + const iScale = controller._cachedMeta.iScale; + if (iScale && axis === iScale.axis && axis !== 'r' && _sorted && data.length) { + const lookupMethod = iScale._reversePixels ? _rlookupByKey : _lookupByKey; + if (!intersect) { + return lookupMethod(data, axis, value); + } else if (controller._sharedOptions) { + const el = data[0]; + const range = typeof el.getRange === 'function' && el.getRange(axis); + if (range) { + const start = lookupMethod(data, axis, value - range); + const end = lookupMethod(data, axis, value + range); + return {lo: start.lo, hi: end.hi}; + } + } + } + return {lo: 0, hi: data.length - 1}; +} +function evaluateInteractionItems(chart, axis, position, handler, intersect) { + const metasets = chart.getSortedVisibleDatasetMetas(); + const value = position[axis]; + for (let i = 0, ilen = metasets.length; i < ilen; ++i) { + const {index, data} = metasets[i]; + const {lo, hi} = binarySearch(metasets[i], axis, value, intersect); + for (let j = lo; j <= hi; ++j) { + const element = data[j]; + if (!element.skip) { + handler(element, index, j); + } + } + } +} +function getDistanceMetricForAxis(axis) { + const useX = axis.indexOf('x') !== -1; + const useY = axis.indexOf('y') !== -1; + return function(pt1, pt2) { + const deltaX = useX ? Math.abs(pt1.x - pt2.x) : 0; + const deltaY = useY ? Math.abs(pt1.y - pt2.y) : 0; + return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); + }; +} +function getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) { + const items = []; + if (!includeInvisible && !chart.isPointInArea(position)) { + return items; + } + const evaluationFunc = function(element, datasetIndex, index) { + if (!includeInvisible && !_isPointInArea(element, chart.chartArea, 0)) { + return; + } + if (element.inRange(position.x, position.y, useFinalPosition)) { + items.push({element, datasetIndex, index}); + } + }; + evaluateInteractionItems(chart, axis, position, evaluationFunc, true); + return items; +} +function getNearestRadialItems(chart, position, axis, useFinalPosition) { + let items = []; + function evaluationFunc(element, datasetIndex, index) { + const {startAngle, endAngle} = element.getProps(['startAngle', 'endAngle'], useFinalPosition); + const {angle} = getAngleFromPoint(element, {x: position.x, y: position.y}); + if (_angleBetween(angle, startAngle, endAngle)) { + items.push({element, datasetIndex, index}); + } + } + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + let items = []; + const distanceMetric = getDistanceMetricForAxis(axis); + let minDistance = Number.POSITIVE_INFINITY; + function evaluationFunc(element, datasetIndex, index) { + const inRange = element.inRange(position.x, position.y, useFinalPosition); + if (intersect && !inRange) { + return; + } + const center = element.getCenterPoint(useFinalPosition); + const pointInArea = !!includeInvisible || chart.isPointInArea(center); + if (!pointInArea && !inRange) { + return; + } + const distance = distanceMetric(position, center); + if (distance < minDistance) { + items = [{element, datasetIndex, index}]; + minDistance = distance; + } else if (distance === minDistance) { + items.push({element, datasetIndex, index}); + } + } + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + if (!includeInvisible && !chart.isPointInArea(position)) { + return []; + } + return axis === 'r' && !intersect + ? getNearestRadialItems(chart, position, axis, useFinalPosition) + : getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible); +} +function getAxisItems(chart, position, axis, intersect, useFinalPosition) { + const items = []; + const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; + let intersectsItem = false; + evaluateInteractionItems(chart, axis, position, (element, datasetIndex, index) => { + if (element[rangeMethod](position[axis], useFinalPosition)) { + items.push({element, datasetIndex, index}); + intersectsItem = intersectsItem || element.inRange(position.x, position.y, useFinalPosition); + } + }); + if (intersect && !intersectsItem) { + return []; + } + return items; +} +var Interaction = { + evaluateInteractionItems, + modes: { + index(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'x'; + const includeInvisible = options.includeInvisible || false; + const items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) + : getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + const elements = []; + if (!items.length) { + return []; + } + chart.getSortedVisibleDatasetMetas().forEach((meta) => { + const index = items[0].index; + const element = meta.data[index]; + if (element && !element.skip) { + elements.push({element, datasetIndex: meta.index, index}); + } + }); + return elements; + }, + dataset(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + let items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) : + getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + if (items.length > 0) { + const datasetIndex = items[0].datasetIndex; + const data = chart.getDatasetMeta(datasetIndex).data; + items = []; + for (let i = 0; i < data.length; ++i) { + items.push({element: data[i], datasetIndex, index: i}); + } + } + return items; + }, + point(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible); + }, + nearest(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getNearestItems(chart, position, axis, options.intersect, useFinalPosition, includeInvisible); + }, + x(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'x', options.intersect, useFinalPosition); + }, + y(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'y', options.intersect, useFinalPosition); + } + } +}; + +const STATIC_POSITIONS = ['left', 'top', 'right', 'bottom']; +function filterByPosition(array, position) { + return array.filter(v => v.pos === position); +} +function filterDynamicPositionByAxis(array, axis) { + return array.filter(v => STATIC_POSITIONS.indexOf(v.pos) === -1 && v.box.axis === axis); +} +function sortByWeight(array, reverse) { + return array.sort((a, b) => { + const v0 = reverse ? b : a; + const v1 = reverse ? a : b; + return v0.weight === v1.weight ? + v0.index - v1.index : + v0.weight - v1.weight; + }); +} +function wrapBoxes(boxes) { + const layoutBoxes = []; + let i, ilen, box, pos, stack, stackWeight; + for (i = 0, ilen = (boxes || []).length; i < ilen; ++i) { + box = boxes[i]; + ({position: pos, options: {stack, stackWeight = 1}} = box); + layoutBoxes.push({ + index: i, + box, + pos, + horizontal: box.isHorizontal(), + weight: box.weight, + stack: stack && (pos + stack), + stackWeight + }); + } + return layoutBoxes; +} +function buildStacks(layouts) { + const stacks = {}; + for (const wrap of layouts) { + const {stack, pos, stackWeight} = wrap; + if (!stack || !STATIC_POSITIONS.includes(pos)) { + continue; + } + const _stack = stacks[stack] || (stacks[stack] = {count: 0, placed: 0, weight: 0, size: 0}); + _stack.count++; + _stack.weight += stackWeight; + } + return stacks; +} +function setLayoutDims(layouts, params) { + const stacks = buildStacks(layouts); + const {vBoxMaxWidth, hBoxMaxHeight} = params; + let i, ilen, layout; + for (i = 0, ilen = layouts.length; i < ilen; ++i) { + layout = layouts[i]; + const {fullSize} = layout.box; + const stack = stacks[layout.stack]; + const factor = stack && layout.stackWeight / stack.weight; + if (layout.horizontal) { + layout.width = factor ? factor * vBoxMaxWidth : fullSize && params.availableWidth; + layout.height = hBoxMaxHeight; + } else { + layout.width = vBoxMaxWidth; + layout.height = factor ? factor * hBoxMaxHeight : fullSize && params.availableHeight; + } + } + return stacks; +} +function buildLayoutBoxes(boxes) { + const layoutBoxes = wrapBoxes(boxes); + const fullSize = sortByWeight(layoutBoxes.filter(wrap => wrap.box.fullSize), true); + const left = sortByWeight(filterByPosition(layoutBoxes, 'left'), true); + const right = sortByWeight(filterByPosition(layoutBoxes, 'right')); + const top = sortByWeight(filterByPosition(layoutBoxes, 'top'), true); + const bottom = sortByWeight(filterByPosition(layoutBoxes, 'bottom')); + const centerHorizontal = filterDynamicPositionByAxis(layoutBoxes, 'x'); + const centerVertical = filterDynamicPositionByAxis(layoutBoxes, 'y'); + return { + fullSize, + leftAndTop: left.concat(top), + rightAndBottom: right.concat(centerVertical).concat(bottom).concat(centerHorizontal), + chartArea: filterByPosition(layoutBoxes, 'chartArea'), + vertical: left.concat(right).concat(centerVertical), + horizontal: top.concat(bottom).concat(centerHorizontal) + }; +} +function getCombinedMax(maxPadding, chartArea, a, b) { + return Math.max(maxPadding[a], chartArea[a]) + Math.max(maxPadding[b], chartArea[b]); +} +function updateMaxPadding(maxPadding, boxPadding) { + maxPadding.top = Math.max(maxPadding.top, boxPadding.top); + maxPadding.left = Math.max(maxPadding.left, boxPadding.left); + maxPadding.bottom = Math.max(maxPadding.bottom, boxPadding.bottom); + maxPadding.right = Math.max(maxPadding.right, boxPadding.right); +} +function updateDims(chartArea, params, layout, stacks) { + const {pos, box} = layout; + const maxPadding = chartArea.maxPadding; + if (!isObject(pos)) { + if (layout.size) { + chartArea[pos] -= layout.size; + } + const stack = stacks[layout.stack] || {size: 0, count: 1}; + stack.size = Math.max(stack.size, layout.horizontal ? box.height : box.width); + layout.size = stack.size / stack.count; + chartArea[pos] += layout.size; + } + if (box.getPadding) { + updateMaxPadding(maxPadding, box.getPadding()); + } + const newWidth = Math.max(0, params.outerWidth - getCombinedMax(maxPadding, chartArea, 'left', 'right')); + const newHeight = Math.max(0, params.outerHeight - getCombinedMax(maxPadding, chartArea, 'top', 'bottom')); + const widthChanged = newWidth !== chartArea.w; + const heightChanged = newHeight !== chartArea.h; + chartArea.w = newWidth; + chartArea.h = newHeight; + return layout.horizontal + ? {same: widthChanged, other: heightChanged} + : {same: heightChanged, other: widthChanged}; +} +function handleMaxPadding(chartArea) { + const maxPadding = chartArea.maxPadding; + function updatePos(pos) { + const change = Math.max(maxPadding[pos] - chartArea[pos], 0); + chartArea[pos] += change; + return change; + } + chartArea.y += updatePos('top'); + chartArea.x += updatePos('left'); + updatePos('right'); + updatePos('bottom'); +} +function getMargins(horizontal, chartArea) { + const maxPadding = chartArea.maxPadding; + function marginForPositions(positions) { + const margin = {left: 0, top: 0, right: 0, bottom: 0}; + positions.forEach((pos) => { + margin[pos] = Math.max(chartArea[pos], maxPadding[pos]); + }); + return margin; + } + return horizontal + ? marginForPositions(['left', 'right']) + : marginForPositions(['top', 'bottom']); +} +function fitBoxes(boxes, chartArea, params, stacks) { + const refitBoxes = []; + let i, ilen, layout, box, refit, changed; + for (i = 0, ilen = boxes.length, refit = 0; i < ilen; ++i) { + layout = boxes[i]; + box = layout.box; + box.update( + layout.width || chartArea.w, + layout.height || chartArea.h, + getMargins(layout.horizontal, chartArea) + ); + const {same, other} = updateDims(chartArea, params, layout, stacks); + refit |= same && refitBoxes.length; + changed = changed || other; + if (!box.fullSize) { + refitBoxes.push(layout); + } + } + return refit && fitBoxes(refitBoxes, chartArea, params, stacks) || changed; +} +function setBoxDims(box, left, top, width, height) { + box.top = top; + box.left = left; + box.right = left + width; + box.bottom = top + height; + box.width = width; + box.height = height; +} +function placeBoxes(boxes, chartArea, params, stacks) { + const userPadding = params.padding; + let {x, y} = chartArea; + for (const layout of boxes) { + const box = layout.box; + const stack = stacks[layout.stack] || {count: 1, placed: 0, weight: 1}; + const weight = (layout.stackWeight / stack.weight) || 1; + if (layout.horizontal) { + const width = chartArea.w * weight; + const height = stack.size || box.height; + if (defined(stack.start)) { + y = stack.start; + } + if (box.fullSize) { + setBoxDims(box, userPadding.left, y, params.outerWidth - userPadding.right - userPadding.left, height); + } else { + setBoxDims(box, chartArea.left + stack.placed, y, width, height); + } + stack.start = y; + stack.placed += width; + y = box.bottom; + } else { + const height = chartArea.h * weight; + const width = stack.size || box.width; + if (defined(stack.start)) { + x = stack.start; + } + if (box.fullSize) { + setBoxDims(box, x, userPadding.top, width, params.outerHeight - userPadding.bottom - userPadding.top); + } else { + setBoxDims(box, x, chartArea.top + stack.placed, width, height); + } + stack.start = x; + stack.placed += height; + x = box.right; + } + } + chartArea.x = x; + chartArea.y = y; +} +defaults.set('layout', { + autoPadding: true, + padding: { + top: 0, + right: 0, + bottom: 0, + left: 0 + } +}); +var layouts = { + addBox(chart, item) { + if (!chart.boxes) { + chart.boxes = []; + } + item.fullSize = item.fullSize || false; + item.position = item.position || 'top'; + item.weight = item.weight || 0; + item._layers = item._layers || function() { + return [{ + z: 0, + draw(chartArea) { + item.draw(chartArea); + } + }]; + }; + chart.boxes.push(item); + }, + removeBox(chart, layoutItem) { + const index = chart.boxes ? chart.boxes.indexOf(layoutItem) : -1; + if (index !== -1) { + chart.boxes.splice(index, 1); + } + }, + configure(chart, item, options) { + item.fullSize = options.fullSize; + item.position = options.position; + item.weight = options.weight; + }, + update(chart, width, height, minPadding) { + if (!chart) { + return; + } + const padding = toPadding(chart.options.layout.padding); + const availableWidth = Math.max(width - padding.width, 0); + const availableHeight = Math.max(height - padding.height, 0); + const boxes = buildLayoutBoxes(chart.boxes); + const verticalBoxes = boxes.vertical; + const horizontalBoxes = boxes.horizontal; + each(chart.boxes, box => { + if (typeof box.beforeLayout === 'function') { + box.beforeLayout(); + } + }); + const visibleVerticalBoxCount = verticalBoxes.reduce((total, wrap) => + wrap.box.options && wrap.box.options.display === false ? total : total + 1, 0) || 1; + const params = Object.freeze({ + outerWidth: width, + outerHeight: height, + padding, + availableWidth, + availableHeight, + vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount, + hBoxMaxHeight: availableHeight / 2 + }); + const maxPadding = Object.assign({}, padding); + updateMaxPadding(maxPadding, toPadding(minPadding)); + const chartArea = Object.assign({ + maxPadding, + w: availableWidth, + h: availableHeight, + x: padding.left, + y: padding.top + }, padding); + const stacks = setLayoutDims(verticalBoxes.concat(horizontalBoxes), params); + fitBoxes(boxes.fullSize, chartArea, params, stacks); + fitBoxes(verticalBoxes, chartArea, params, stacks); + if (fitBoxes(horizontalBoxes, chartArea, params, stacks)) { + fitBoxes(verticalBoxes, chartArea, params, stacks); + } + handleMaxPadding(chartArea); + placeBoxes(boxes.leftAndTop, chartArea, params, stacks); + chartArea.x += chartArea.w; + chartArea.y += chartArea.h; + placeBoxes(boxes.rightAndBottom, chartArea, params, stacks); + chart.chartArea = { + left: chartArea.left, + top: chartArea.top, + right: chartArea.left + chartArea.w, + bottom: chartArea.top + chartArea.h, + height: chartArea.h, + width: chartArea.w, + }; + each(boxes.chartArea, (layout) => { + const box = layout.box; + Object.assign(box, chart.chartArea); + box.update(chartArea.w, chartArea.h, {left: 0, top: 0, right: 0, bottom: 0}); + }); + } +}; + class BasePlatform { acquireContext(canvas, aspectRatio) {} releaseContext(context) { @@ -3361,7 +3467,7 @@ function removeListener(chart, type, listener) { } function fromNativeEvent(event, chart) { const type = EVENT_TYPES[event.type] || event.type; - const {x, y} = getRelativePosition$1(event, chart); + const {x, y} = getRelativePosition(event, chart); return { type, chart, @@ -4035,6 +4141,7 @@ class DatasetController { this._drawStart = undefined; this._drawCount = undefined; this.enableOptionSharing = false; + this.supportsDecimation = false; this.$context = undefined; this._syncList = []; this.initialize(); @@ -4435,6 +4542,14 @@ class DatasetController { includeOptions(mode, sharedOptions) { return !sharedOptions || isDirectUpdateMode(mode) || this.chart._animationsDisabled; } + _getSharedOptions(start, mode) { + const firstOpts = this.resolveDataElementOptions(start, mode); + const previouslySharedOptions = this._sharedOptions; + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions) || (sharedOptions !== previouslySharedOptions); + this.updateSharedOptions(sharedOptions, mode, firstOpts); + return {sharedOptions, includeOptions}; + } updateElement(element, index, properties, mode) { if (isDirectUpdateMode(mode)) { Object.assign(element, properties); @@ -5085,6 +5200,7 @@ class Scale extends Element { if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { this.ticks = autoSkip(this, this.ticks); this._labelSizes = null; + this.afterAutoSkip(); } if (samplingEnabled) { this._convertTicksToLabels(this.ticks); @@ -5205,6 +5321,7 @@ class Scale extends Element { afterCalculateLabelRotation() { callback(this.options.afterCalculateLabelRotation, [this]); } + afterAutoSkip() {} beforeFit() { callback(this.options.beforeFit, [this]); } @@ -5271,7 +5388,7 @@ class Scale extends Element { paddingRight = last.width; } else if (align === 'end') { paddingLeft = first.width; - } else { + } else if (align !== 'inner') { paddingLeft = first.width / 2; paddingRight = last.width / 2; } @@ -5516,7 +5633,7 @@ class Scale extends Element { const optsAtIndex = grid.setContext(this.getContext(i)); const lineWidth = optsAtIndex.lineWidth; const lineColor = optsAtIndex.color; - const borderDash = grid.borderDash || []; + const borderDash = optsAtIndex.borderDash || []; const borderDashOffset = optsAtIndex.borderDashOffset; const tickWidth = optsAtIndex.tickWidth; const tickColor = optsAtIndex.tickColor; @@ -5622,8 +5739,18 @@ class Scale extends Element { const color = optsAtIndex.color; const strokeColor = optsAtIndex.textStrokeColor; const strokeWidth = optsAtIndex.textStrokeWidth; + let tickTextAlign = textAlign; if (isHorizontal) { x = pixel; + if (textAlign === 'inner') { + if (i === ilen - 1) { + tickTextAlign = !this.options.reverse ? 'right' : 'left'; + } else if (i === 0) { + tickTextAlign = !this.options.reverse ? 'left' : 'right'; + } else { + tickTextAlign = 'center'; + } + } if (position === 'top') { if (crossAlign === 'near' || rotation !== 0) { textOffset = -lineCount * lineHeight + lineHeight / 2; @@ -5687,7 +5814,7 @@ class Scale extends Element { strokeColor, strokeWidth, textOffset, - textAlign, + textAlign: tickTextAlign, textBaseline, translation: [x, y], backdrop, @@ -5706,6 +5833,8 @@ class Scale extends Element { align = 'left'; } else if (ticks.align === 'end') { align = 'right'; + } else if (ticks.align === 'inner') { + align = 'inner'; } return align; } @@ -6219,6 +6348,7 @@ class PluginService { } } function allPlugins(config) { + const localIds = {}; const plugins = []; const keys = Object.keys(registry.plugins.items); for (let i = 0; i < keys.length; i++) { @@ -6229,9 +6359,10 @@ function allPlugins(config) { const plugin = local[i]; if (plugins.indexOf(plugin) === -1) { plugins.push(plugin); + localIds[plugin.id] = true; } } - return plugins; + return {plugins, localIds}; } function getOpts(options, all) { if (!all && options === false) { @@ -6242,11 +6373,10 @@ function getOpts(options, all) { } return options; } -function createDescriptors(chart, plugins, options, all) { +function createDescriptors(chart, {plugins, localIds}, options, all) { const result = []; const context = chart.getContext(); - for (let i = 0; i < plugins.length; i++) { - const plugin = plugins[i]; + for (const plugin of plugins) { const id = plugin.id; const opts = getOpts(options[id], all); if (opts === null) { @@ -6254,15 +6384,22 @@ function createDescriptors(chart, plugins, options, all) { } result.push({ plugin, - options: pluginOpts(chart.config, plugin, opts, context) + options: pluginOpts(chart.config, {plugin, local: localIds[id]}, opts, context) }); } return result; } -function pluginOpts(config, plugin, opts, context) { +function pluginOpts(config, {plugin, local}, opts, context) { const keys = config.pluginScopeKeys(plugin); const scopes = config.getOptionScopes(opts, keys); - return config.createResolver(scopes, context, [''], {scriptable: false, indexable: false, allKeys: true}); + if (local && plugin.defaults) { + scopes.push(plugin.defaults); + } + return config.createResolver(scopes, context, [''], { + scriptable: false, + indexable: false, + allKeys: true + }); } function getIndexAxis(type, options) { @@ -6548,7 +6685,7 @@ function needContext(proxy, names) { return false; } -var version = "3.7.1"; +var version = "3.9.1"; const KNOWN_POSITIONS = ['top', 'bottom', 'left', 'right', 'chartArea']; function positionIsHorizontal(position, axis) { @@ -6618,7 +6755,7 @@ class Chart { if (existingChart) { throw new Error( 'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' + - ' must be destroyed before the canvas can be reused.' + ' must be destroyed before the canvas with ID \'' + existingChart.canvas.id + '\' can be reused.' ); } const options = config.createResolver(config.chartOptionScopes(), this.getContext()); @@ -7089,6 +7226,9 @@ class Chart { args.cancelable = false; this.notifyPlugins('afterDatasetDraw', args); } + isPointInArea(point) { + return _isPointInArea(point, this.chartArea, this._minPadding); + } getElementsAtEventForMode(e, mode, options, useFinalPosition) { const method = Interaction.modes[mode]; if (typeof method === 'function') { @@ -7328,7 +7468,7 @@ class Chart { event: e, replay, cancelable: true, - inChartArea: _isPointInArea(e, this.chartArea, this._minPadding) + inChartArea: this.isPointInArea(e) }; const eventFilter = (plugin) => (plugin.options.events || this.options.events).includes(e.native.type); if (this.notifyPlugins('beforeEvent', args, eventFilter) === false) { @@ -7424,6 +7564,7 @@ class DateAdapter { constructor(options) { this.options = options || {}; } + init(chartOptions) {} formats() { return abstract(); } @@ -7605,6 +7746,10 @@ function setBorderSkipped(properties, options, stack, index) { properties.borderSkipped = res; return; } + if (edge === true) { + properties.borderSkipped = {top: true, right: true, bottom: true, left: true}; + return; + } const {start, end, reverse, top, bottom} = borderProps(properties); if (edge === 'middle' && stack) { properties.enableBorderRadius = true; @@ -7702,10 +7847,7 @@ class BarController extends DatasetController { const base = vScale.getBasePixel(); const horizontal = vScale.isHorizontal(); const ruler = this._getRuler(); - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); - this.updateSharedOptions(sharedOptions, mode, firstOpts); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); for (let i = start; i < start + count; i++) { const parsed = this.getParsed(i); const vpixels = reset || isNullOrUndef(parsed[vScale.axis]) ? {base, head: base} : this._calculateBarValuePixels(i); @@ -7730,31 +7872,27 @@ class BarController extends DatasetController { } } _getStacks(last, dataIndex) { - const meta = this._cachedMeta; - const iScale = meta.iScale; - const metasets = iScale.getMatchingVisibleMetas(this._type); + const {iScale} = this._cachedMeta; + const metasets = iScale.getMatchingVisibleMetas(this._type) + .filter(meta => meta.controller.options.grouped); const stacked = iScale.options.stacked; - const ilen = metasets.length; const stacks = []; - let i, item; - for (i = 0; i < ilen; ++i) { - item = metasets[i]; - if (!item.controller.options.grouped) { - continue; + const skipNull = (meta) => { + const parsed = meta.controller.getParsed(dataIndex); + const val = parsed && parsed[meta.vScale.axis]; + if (isNullOrUndef(val) || isNaN(val)) { + return true; } - if (typeof dataIndex !== 'undefined') { - const val = item.controller.getParsed(dataIndex)[ - item.controller._cachedMeta.vScale.axis - ]; - if (isNullOrUndef(val) || isNaN(val)) { - continue; - } + }; + for (const meta of metasets) { + if (dataIndex !== undefined && skipNull(meta)) { + continue; } - if (stacked === false || stacks.indexOf(item.stack) === -1 || - (stacked === undefined && item.stack === undefined)) { - stacks.push(item.stack); + if (stacked === false || stacks.indexOf(meta.stack) === -1 || + (stacked === undefined && meta.stack === undefined)) { + stacks.push(meta.stack); } - if (item.index === last) { + if (meta.index === last) { break; } } @@ -7832,6 +7970,11 @@ class BarController extends DatasetController { if (value === actualBase) { base -= size / 2; } + const startPixel = vScale.getPixelForDecimal(0); + const endPixel = vScale.getPixelForDecimal(1); + const min = Math.min(startPixel, endPixel); + const max = Math.max(startPixel, endPixel); + base = Math.max(Math.min(base, max), min); head = base + size; } if (base === vScale.getPixelForValue(actualBase)) { @@ -7969,9 +8112,7 @@ class BubbleController extends DatasetController { updateElements(points, start, count, mode) { const reset = mode === 'reset'; const {iScale, vScale} = this._cachedMeta; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); const iAxis = iScale.axis; const vAxis = vScale.axis; for (let i = start; i < start + count; i++) { @@ -7982,14 +8123,13 @@ class BubbleController extends DatasetController { const vPixel = properties[vAxis] = reset ? vScale.getBasePixel() : vScale.getPixelForValue(parsed[vAxis]); properties.skip = isNaN(iPixel) || isNaN(vPixel); if (includeOptions) { - properties.options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); if (reset) { properties.options.radius = 0; } } this.updateElement(point, i, properties, mode); } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } resolveDataElementOptions(index, mode) { const parsed = this.getParsed(index); @@ -8155,9 +8295,7 @@ class DoughnutController extends DatasetController { const animateScale = reset && animationOpts.animateScale; const innerRadius = animateScale ? 0 : this.innerRadius; const outerRadius = animateScale ? 0 : this.outerRadius; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); let startAngle = this._getRotation(); let i; for (i = 0; i < start; ++i) { @@ -8181,7 +8319,6 @@ class DoughnutController extends DatasetController { startAngle += circumference; this.updateElement(arc, i, properties, mode); } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } calculateTotal() { const meta = this._cachedMeta; @@ -8342,16 +8479,17 @@ DoughnutController.overrides = { class LineController extends DatasetController { initialize() { this.enableOptionSharing = true; + this.supportsDecimation = true; super.initialize(); } update(mode) { const meta = this._cachedMeta; const {dataset: line, data: points = [], _dataset} = meta; const animationsDisabled = this.chart._animationsDisabled; - let {start, count} = getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); this._drawStart = start; this._drawCount = count; - if (scaleRangesChanged(meta)) { + if (_scaleRangesChanged(meta)) { start = 0; count = points.length; } @@ -8373,9 +8511,7 @@ class LineController extends DatasetController { updateElements(points, start, count, mode) { const reset = mode === 'reset'; const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; - const firstOpts = this.resolveDataElementOptions(start, mode); - const sharedOptions = this.getSharedOptions(firstOpts); - const includeOptions = this.includeOptions(mode, sharedOptions); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); const iAxis = iScale.axis; const vAxis = vScale.axis; const {spanGaps, segment} = this.options; @@ -8390,7 +8526,7 @@ class LineController extends DatasetController { const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; - properties.stop = i > 0 && (parsed[iAxis] - prevParsed[iAxis]) > maxGapLength; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; if (segment) { properties.parsed = parsed; properties.raw = _dataset.data[i]; @@ -8403,7 +8539,6 @@ class LineController extends DatasetController { } prevParsed = parsed; } - this.updateSharedOptions(sharedOptions, mode, firstOpts); } getMaxOverflow() { const meta = this._cachedMeta; @@ -8440,50 +8575,6 @@ LineController.overrides = { }, } }; -function getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { - const pointCount = points.length; - let start = 0; - let count = pointCount; - if (meta._sorted) { - const {iScale, _parsed} = meta; - const axis = iScale.axis; - const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); - if (minDefined) { - start = _limitValue(Math.min( - _lookupByKey(_parsed, iScale.axis, min).lo, - animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo), - 0, pointCount - 1); - } - if (maxDefined) { - count = _limitValue(Math.max( - _lookupByKey(_parsed, iScale.axis, max).hi + 1, - animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max)).hi + 1), - start, pointCount) - start; - } else { - count = pointCount - start; - } - } - return {start, count}; -} -function scaleRangesChanged(meta) { - const {xScale, yScale, _scaleRanges} = meta; - const newRanges = { - xmin: xScale.min, - xmax: xScale.max, - ymin: yScale.min, - ymax: yScale.max - }; - if (!_scaleRanges) { - meta._scaleRanges = newRanges; - return true; - } - const changed = _scaleRanges.xmin !== xScale.min - || _scaleRanges.xmax !== xScale.max - || _scaleRanges.ymin !== yScale.min - || _scaleRanges.ymax !== yScale.max; - Object.assign(_scaleRanges, newRanges); - return changed; -} class PolarAreaController extends DatasetController { constructor(chart, datasetIndex) { @@ -8501,11 +8592,30 @@ class PolarAreaController extends DatasetController { value, }; } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } update(mode) { const arcs = this._cachedMeta.data; this._updateRadius(); this.updateElements(arcs, 0, arcs.length, mode); } + getMinMax() { + const meta = this._cachedMeta; + const range = {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY}; + meta.data.forEach((element, index) => { + const parsed = this.getParsed(index).r; + if (!isNaN(parsed) && this.chart.getDataVisibility(index)) { + if (parsed < range.min) { + range.min = parsed; + } + if (parsed > range.max) { + range.max = parsed; + } + } + }); + return range; + } _updateRadius() { const chart = this.chart; const chartArea = chart.chartArea; @@ -8520,7 +8630,6 @@ class PolarAreaController extends DatasetController { updateElements(arcs, start, count, mode) { const reset = mode === 'reset'; const chart = this.chart; - const dataset = this.getDataset(); const opts = chart.options; const animationOpts = opts.animation; const scale = this._cachedMeta.rScale; @@ -8537,7 +8646,7 @@ class PolarAreaController extends DatasetController { const arc = arcs[i]; let startAngle = angle; let endAngle = angle + this._computeAngle(i, mode, defaultAngle); - let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0; + let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(this.getParsed(i).r) : 0; angle = endAngle; if (reset) { if (animationOpts.animateScale) { @@ -8560,11 +8669,10 @@ class PolarAreaController extends DatasetController { } } countVisibleElements() { - const dataset = this.getDataset(); const meta = this._cachedMeta; let count = 0; meta.data.forEach((element, index) => { - if (!isNaN(dataset.data[index]) && this.chart.getDataVisibility(index)) { + if (!isNaN(this.getParsed(index).r) && this.chart.getDataVisibility(index)) { count++; } }); @@ -8671,6 +8779,9 @@ class RadarController extends DatasetController { value: '' + vScale.getLabelForValue(parsed[vScale.axis]) }; } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } update(mode) { const meta = this._cachedMeta; const line = meta.dataset; @@ -8692,13 +8803,12 @@ class RadarController extends DatasetController { this.updateElements(points, 0, points.length, mode); } updateElements(points, start, count, mode) { - const dataset = this.getDataset(); const scale = this._cachedMeta.rScale; const reset = mode === 'reset'; for (let i = start; i < start + count; i++) { const point = points[i]; const options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); - const pointPosition = scale.getPointPositionForValue(i, dataset.data[i]); + const pointPosition = scale.getPointPositionForValue(i, this.getParsed(i).r); const x = reset ? scale.xCenter : pointPosition.x; const y = reset ? scale.yCenter : pointPosition.y; const properties = { @@ -8711,32 +8821,121 @@ class RadarController extends DatasetController { this.updateElement(point, i, properties, mode); } } -} -RadarController.id = 'radar'; -RadarController.defaults = { - datasetElementType: 'line', - dataElementType: 'point', - indexAxis: 'r', - showLine: true, - elements: { - line: { - fill: 'start' +} +RadarController.id = 'radar'; +RadarController.defaults = { + datasetElementType: 'line', + dataElementType: 'point', + indexAxis: 'r', + showLine: true, + elements: { + line: { + fill: 'start' + } + }, +}; +RadarController.overrides = { + aspectRatio: 1, + scales: { + r: { + type: 'radialLinear', + } + } +}; + +class ScatterController extends DatasetController { + update(mode) { + const meta = this._cachedMeta; + const {data: points = []} = meta; + const animationsDisabled = this.chart._animationsDisabled; + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + this._drawStart = start; + this._drawCount = count; + if (_scaleRangesChanged(meta)) { + start = 0; + count = points.length; + } + if (this.options.showLine) { + const {dataset: line, _dataset} = meta; + line._chart = this.chart; + line._datasetIndex = this.index; + line._decimated = !!_dataset._decimated; + line.points = points; + const options = this.resolveDatasetElementOptions(mode); + options.segment = this.options.segment; + this.updateElement(line, undefined, { + animated: !animationsDisabled, + options + }, mode); + } + this.updateElements(points, start, count, mode); + } + addElements() { + const {showLine} = this.options; + if (!this.datasetElementType && showLine) { + this.datasetElementType = registry.getElement('line'); + } + super.addElements(); + } + updateElements(points, start, count, mode) { + const reset = mode === 'reset'; + const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; + const firstOpts = this.resolveDataElementOptions(start, mode); + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions); + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const {spanGaps, segment} = this.options; + const maxGapLength = isNumber(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY; + const directUpdate = this.chart._animationsDisabled || reset || mode === 'none'; + let prevParsed = start > 0 && this.getParsed(start - 1); + for (let i = start; i < start + count; ++i) { + const point = points[i]; + const parsed = this.getParsed(i); + const properties = directUpdate ? point : {}; + const nullData = isNullOrUndef(parsed[vAxis]); + const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); + const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); + properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; + if (segment) { + properties.parsed = parsed; + properties.raw = _dataset.data[i]; + } + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); + } + if (!directUpdate) { + this.updateElement(point, i, properties, mode); + } + prevParsed = parsed; + } + this.updateSharedOptions(sharedOptions, mode, firstOpts); + } + getMaxOverflow() { + const meta = this._cachedMeta; + const data = meta.data || []; + if (!this.options.showLine) { + let max = 0; + for (let i = data.length - 1; i >= 0; --i) { + max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2); + } + return max > 0 && max; } - }, -}; -RadarController.overrides = { - aspectRatio: 1, - scales: { - r: { - type: 'radialLinear', + const dataset = meta.dataset; + const border = dataset.options && dataset.options.borderWidth || 0; + if (!data.length) { + return border; } + const firstPoint = data[0].size(this.resolveDataElementOptions(0)); + const lastPoint = data[data.length - 1].size(this.resolveDataElementOptions(data.length - 1)); + return Math.max(border, firstPoint, lastPoint) / 2; } -}; - -class ScatterController extends LineController { } ScatterController.id = 'scatter'; ScatterController.defaults = { + datasetElementType: false, + dataElementType: 'point', showLine: false, fill: false }; @@ -8816,7 +9015,7 @@ function rThetaToXY(r, theta, x, y) { y: y + r * Math.sin(theta), }; } -function pathArc(ctx, element, offset, spacing, end) { +function pathArc(ctx, element, offset, spacing, end, circular) { const {x, y, startAngle: start, pixelMargin, innerRadius: innerR} = element; const outerRadius = Math.max(element.outerRadius + spacing + offset - pixelMargin, 0); const innerRadius = innerR > 0 ? innerR + spacing + offset + pixelMargin : 0; @@ -8843,35 +9042,45 @@ function pathArc(ctx, element, offset, spacing, end) { const innerStartAdjustedAngle = startAngle + innerStart / innerStartAdjustedRadius; const innerEndAdjustedAngle = endAngle - innerEnd / innerEndAdjustedRadius; ctx.beginPath(); - ctx.arc(x, y, outerRadius, outerStartAdjustedAngle, outerEndAdjustedAngle); - if (outerEnd > 0) { - const pCenter = rThetaToXY(outerEndAdjustedRadius, outerEndAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, outerEnd, outerEndAdjustedAngle, endAngle + HALF_PI); - } - const p4 = rThetaToXY(innerEndAdjustedRadius, endAngle, x, y); - ctx.lineTo(p4.x, p4.y); - if (innerEnd > 0) { - const pCenter = rThetaToXY(innerEndAdjustedRadius, innerEndAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, innerEnd, endAngle + HALF_PI, innerEndAdjustedAngle + Math.PI); - } - ctx.arc(x, y, innerRadius, endAngle - (innerEnd / innerRadius), startAngle + (innerStart / innerRadius), true); - if (innerStart > 0) { - const pCenter = rThetaToXY(innerStartAdjustedRadius, innerStartAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, innerStart, innerStartAdjustedAngle + Math.PI, startAngle - HALF_PI); - } - const p8 = rThetaToXY(outerStartAdjustedRadius, startAngle, x, y); - ctx.lineTo(p8.x, p8.y); - if (outerStart > 0) { - const pCenter = rThetaToXY(outerStartAdjustedRadius, outerStartAdjustedAngle, x, y); - ctx.arc(pCenter.x, pCenter.y, outerStart, startAngle - HALF_PI, outerStartAdjustedAngle); + if (circular) { + ctx.arc(x, y, outerRadius, outerStartAdjustedAngle, outerEndAdjustedAngle); + if (outerEnd > 0) { + const pCenter = rThetaToXY(outerEndAdjustedRadius, outerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerEnd, outerEndAdjustedAngle, endAngle + HALF_PI); + } + const p4 = rThetaToXY(innerEndAdjustedRadius, endAngle, x, y); + ctx.lineTo(p4.x, p4.y); + if (innerEnd > 0) { + const pCenter = rThetaToXY(innerEndAdjustedRadius, innerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerEnd, endAngle + HALF_PI, innerEndAdjustedAngle + Math.PI); + } + ctx.arc(x, y, innerRadius, endAngle - (innerEnd / innerRadius), startAngle + (innerStart / innerRadius), true); + if (innerStart > 0) { + const pCenter = rThetaToXY(innerStartAdjustedRadius, innerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerStart, innerStartAdjustedAngle + Math.PI, startAngle - HALF_PI); + } + const p8 = rThetaToXY(outerStartAdjustedRadius, startAngle, x, y); + ctx.lineTo(p8.x, p8.y); + if (outerStart > 0) { + const pCenter = rThetaToXY(outerStartAdjustedRadius, outerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerStart, startAngle - HALF_PI, outerStartAdjustedAngle); + } + } else { + ctx.moveTo(x, y); + const outerStartX = Math.cos(outerStartAdjustedAngle) * outerRadius + x; + const outerStartY = Math.sin(outerStartAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerStartX, outerStartY); + const outerEndX = Math.cos(outerEndAdjustedAngle) * outerRadius + x; + const outerEndY = Math.sin(outerEndAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerEndX, outerEndY); } ctx.closePath(); } -function drawArc(ctx, element, offset, spacing) { +function drawArc(ctx, element, offset, spacing, circular) { const {fullCircles, startAngle, circumference} = element; let endAngle = element.endAngle; if (fullCircles) { - pathArc(ctx, element, offset, spacing, startAngle + TAU); + pathArc(ctx, element, offset, spacing, startAngle + TAU, circular); for (let i = 0; i < fullCircles; ++i) { ctx.fill(); } @@ -8882,7 +9091,7 @@ function drawArc(ctx, element, offset, spacing) { } } } - pathArc(ctx, element, offset, spacing, endAngle); + pathArc(ctx, element, offset, spacing, endAngle, circular); ctx.fill(); return endAngle; } @@ -8905,7 +9114,7 @@ function drawFullCircleBorders(ctx, element, inner) { ctx.stroke(); } } -function drawBorder(ctx, element, offset, spacing, endAngle) { +function drawBorder(ctx, element, offset, spacing, endAngle, circular) { const {options} = element; const {borderWidth, borderJoinStyle} = options; const inner = options.borderAlign === 'inner'; @@ -8925,7 +9134,7 @@ function drawBorder(ctx, element, offset, spacing, endAngle) { if (inner) { clipArc(ctx, element, endAngle); } - pathArc(ctx, element, offset, spacing, endAngle); + pathArc(ctx, element, offset, spacing, endAngle, circular); ctx.stroke(); } class ArcElement extends Element { @@ -8984,6 +9193,7 @@ class ArcElement extends Element { const {options, circumference} = this; const offset = (options.offset || 0) / 2; const spacing = (options.spacing || 0) / 2; + const circular = options.circular; this.pixelMargin = (options.borderAlign === 'inner') ? 0.33 : 0; this.fullCircles = circumference > TAU ? Math.floor(circumference / TAU) : 0; if (circumference === 0 || this.innerRadius < 0 || this.outerRadius < 0) { @@ -9001,8 +9211,8 @@ class ArcElement extends Element { } ctx.fillStyle = options.backgroundColor; ctx.strokeStyle = options.borderColor; - const endAngle = drawArc(ctx, this, radiusOffset, spacing); - drawBorder(ctx, this, radiusOffset, spacing, endAngle); + const endAngle = drawArc(ctx, this, radiusOffset, spacing, circular); + drawBorder(ctx, this, radiusOffset, spacing, endAngle, circular); ctx.restore(); } } @@ -9016,6 +9226,7 @@ ArcElement.defaults = { offset: 0, spacing: 0, angle: undefined, + circular: true, }; ArcElement.defaultRoutes = { backgroundColor: 'backgroundColor' @@ -9687,7 +9898,7 @@ var plugin_decimation = { if (resolve([indexAxis, chart.options.indexAxis]) === 'y') { return; } - if (meta.type !== 'line') { + if (!meta.controller.supportsDecimation) { return; } const xAxis = chart.scales[meta.xAxisID]; @@ -9736,158 +9947,203 @@ var plugin_decimation = { } }; -function getLineByIndex(chart, index) { - const meta = chart.getDatasetMeta(index); - const visible = meta && chart.isDatasetVisible(index); - return visible ? meta.dataset : null; +function _segments(line, target, property) { + const segments = line.segments; + const points = line.points; + const tpoints = target.points; + const parts = []; + for (const segment of segments) { + let {start, end} = segment; + end = _findSegmentEnd(start, end, points); + const bounds = _getBounds(property, points[start], points[end], segment.loop); + if (!target.segments) { + parts.push({ + source: segment, + target: bounds, + start: points[start], + end: points[end] + }); + continue; + } + const targetSegments = _boundSegments(target, bounds); + for (const tgt of targetSegments) { + const subBounds = _getBounds(property, tpoints[tgt.start], tpoints[tgt.end], tgt.loop); + const fillSources = _boundSegment(segment, points, subBounds); + for (const fillSource of fillSources) { + parts.push({ + source: fillSource, + target: tgt, + start: { + [property]: _getEdge(bounds, subBounds, 'start', Math.max) + }, + end: { + [property]: _getEdge(bounds, subBounds, 'end', Math.min) + } + }); + } + } + } + return parts; } -function parseFillOption(line) { - const options = line.options; - const fillOption = options.fill; - let fill = valueOrDefault(fillOption && fillOption.target, fillOption); - if (fill === undefined) { - fill = !!options.backgroundColor; +function _getBounds(property, first, last, loop) { + if (loop) { + return; } - if (fill === false || fill === null) { - return false; + let start = first[property]; + let end = last[property]; + if (property === 'angle') { + start = _normalizeAngle(start); + end = _normalizeAngle(end); } - if (fill === true) { - return 'origin'; + return {property, start, end}; +} +function _pointsFromSegments(boundary, line) { + const {x = null, y = null} = boundary || {}; + const linePoints = line.points; + const points = []; + line.segments.forEach(({start, end}) => { + end = _findSegmentEnd(start, end, linePoints); + const first = linePoints[start]; + const last = linePoints[end]; + if (y !== null) { + points.push({x: first.x, y}); + points.push({x: last.x, y}); + } else if (x !== null) { + points.push({x, y: first.y}); + points.push({x, y: last.y}); + } + }); + return points; +} +function _findSegmentEnd(start, end, points) { + for (;end > start; end--) { + const point = points[end]; + if (!isNaN(point.x) && !isNaN(point.y)) { + break; + } } - return fill; + return end; +} +function _getEdge(a, b, prop, fn) { + if (a && b) { + return fn(a[prop], b[prop]); + } + return a ? a[prop] : b ? b[prop] : 0; +} + +function _createBoundaryLine(boundary, line) { + let points = []; + let _loop = false; + if (isArray(boundary)) { + _loop = true; + points = boundary; + } else { + points = _pointsFromSegments(boundary, line); + } + return points.length ? new LineElement({ + points, + options: {tension: 0}, + _loop, + _fullLoop: _loop + }) : null; +} +function _shouldApplyFill(source) { + return source && source.fill !== false; +} + +function _resolveTarget(sources, index, propagate) { + const source = sources[index]; + let fill = source.fill; + const visited = [index]; + let target; + if (!propagate) { + return fill; + } + while (fill !== false && visited.indexOf(fill) === -1) { + if (!isNumberFinite(fill)) { + return fill; + } + target = sources[fill]; + if (!target) { + return false; + } + if (target.visible) { + return fill; + } + visited.push(fill); + fill = target.fill; + } + return false; } -function decodeFill(line, index, count) { +function _decodeFill(line, index, count) { const fill = parseFillOption(line); if (isObject(fill)) { return isNaN(fill.value) ? false : fill; } let target = parseFloat(fill); if (isNumberFinite(target) && Math.floor(target) === target) { - if (fill[0] === '-' || fill[0] === '+') { - target = index + target; - } - if (target === index || target < 0 || target >= count) { - return false; - } - return target; + return decodeTargetIndex(fill[0], index, target, count); } return ['origin', 'start', 'end', 'stack', 'shape'].indexOf(fill) >= 0 && fill; } -function computeLinearBoundary(source) { - const {scale = {}, fill} = source; - let target = null; - let horizontal; +function decodeTargetIndex(firstCh, index, target, count) { + if (firstCh === '-' || firstCh === '+') { + target = index + target; + } + if (target === index || target < 0 || target >= count) { + return false; + } + return target; +} +function _getTargetPixel(fill, scale) { + let pixel = null; if (fill === 'start') { - target = scale.bottom; + pixel = scale.bottom; } else if (fill === 'end') { - target = scale.top; + pixel = scale.top; } else if (isObject(fill)) { - target = scale.getPixelForValue(fill.value); + pixel = scale.getPixelForValue(fill.value); } else if (scale.getBasePixel) { - target = scale.getBasePixel(); - } - if (isNumberFinite(target)) { - horizontal = scale.isHorizontal(); - return { - x: horizontal ? target : null, - y: horizontal ? null : target - }; - } - return null; -} -class simpleArc { - constructor(opts) { - this.x = opts.x; - this.y = opts.y; - this.radius = opts.radius; - } - pathSegment(ctx, bounds, opts) { - const {x, y, radius} = this; - bounds = bounds || {start: 0, end: TAU}; - ctx.arc(x, y, radius, bounds.end, bounds.start, true); - return !opts.bounds; - } - interpolate(point) { - const {x, y, radius} = this; - const angle = point.angle; - return { - x: x + Math.cos(angle) * radius, - y: y + Math.sin(angle) * radius, - angle - }; + pixel = scale.getBasePixel(); } + return pixel; } -function computeCircularBoundary(source) { - const {scale, fill} = source; - const options = scale.options; - const length = scale.getLabels().length; - const target = []; - const start = options.reverse ? scale.max : scale.min; - const end = options.reverse ? scale.min : scale.max; - let i, center, value; +function _getTargetValue(fill, scale, startValue) { + let value; if (fill === 'start') { - value = start; + value = startValue; } else if (fill === 'end') { - value = end; + value = scale.options.reverse ? scale.min : scale.max; } else if (isObject(fill)) { value = fill.value; } else { value = scale.getBaseValue(); } - if (options.grid.circular) { - center = scale.getPointPositionForValue(0, start); - return new simpleArc({ - x: center.x, - y: center.y, - radius: scale.getDistanceFromCenterForValue(value) - }); - } - for (i = 0; i < length; ++i) { - target.push(scale.getPointPositionForValue(i, value)); - } - return target; -} -function computeBoundary(source) { - const scale = source.scale || {}; - if (scale.getPointPositionForValue) { - return computeCircularBoundary(source); - } - return computeLinearBoundary(source); -} -function findSegmentEnd(start, end, points) { - for (;end > start; end--) { - const point = points[end]; - if (!isNaN(point.x) && !isNaN(point.y)) { - break; - } - } - return end; + return value; } -function pointsFromSegments(boundary, line) { - const {x = null, y = null} = boundary || {}; - const linePoints = line.points; - const points = []; - line.segments.forEach(({start, end}) => { - end = findSegmentEnd(start, end, linePoints); - const first = linePoints[start]; - const last = linePoints[end]; - if (y !== null) { - points.push({x: first.x, y}); - points.push({x: last.x, y}); - } else if (x !== null) { - points.push({x, y: first.y}); - points.push({x, y: last.y}); - } - }); - return points; +function parseFillOption(line) { + const options = line.options; + const fillOption = options.fill; + let fill = valueOrDefault(fillOption && fillOption.target, fillOption); + if (fill === undefined) { + fill = !!options.backgroundColor; + } + if (fill === false || fill === null) { + return false; + } + if (fill === true) { + return 'origin'; + } + return fill; } -function buildStackLine(source) { + +function _buildStackLine(source) { const {scale, index, line} = source; const points = []; const segments = line.segments; const sourcePoints = line.points; const linesBelow = getLinesBelow(scale, index); - linesBelow.push(createBoundaryLine({x: null, y: scale.bottom}, line)); + linesBelow.push(_createBoundaryLine({x: null, y: scale.bottom}, line)); for (let i = 0; i < segments.length; i++) { const segment = segments[i]; for (let j = segment.start; j <= segment.end; j++) { @@ -9951,13 +10207,37 @@ function findPoint(line, sourcePoint, property) { } return {first, last, point}; } -function getTarget(source) { + +class simpleArc { + constructor(opts) { + this.x = opts.x; + this.y = opts.y; + this.radius = opts.radius; + } + pathSegment(ctx, bounds, opts) { + const {x, y, radius} = this; + bounds = bounds || {start: 0, end: TAU}; + ctx.arc(x, y, radius, bounds.end, bounds.start, true); + return !opts.bounds; + } + interpolate(point) { + const {x, y, radius} = this; + const angle = point.angle; + return { + x: x + Math.cos(angle) * radius, + y: y + Math.sin(angle) * radius, + angle + }; + } +} + +function _getTarget(source) { const {chart, fill, line} = source; if (isNumberFinite(fill)) { return getLineByIndex(chart, fill); } if (fill === 'stack') { - return buildStackLine(source); + return _buildStackLine(source); } if (fill === 'shape') { return true; @@ -9966,49 +10246,81 @@ function getTarget(source) { if (boundary instanceof simpleArc) { return boundary; } - return createBoundaryLine(boundary, line); + return _createBoundaryLine(boundary, line); } -function createBoundaryLine(boundary, line) { - let points = []; - let _loop = false; - if (isArray(boundary)) { - _loop = true; - points = boundary; - } else { - points = pointsFromSegments(boundary, line); +function getLineByIndex(chart, index) { + const meta = chart.getDatasetMeta(index); + const visible = meta && chart.isDatasetVisible(index); + return visible ? meta.dataset : null; +} +function computeBoundary(source) { + const scale = source.scale || {}; + if (scale.getPointPositionForValue) { + return computeCircularBoundary(source); } - return points.length ? new LineElement({ - points, - options: {tension: 0}, - _loop, - _fullLoop: _loop - }) : null; + return computeLinearBoundary(source); } -function resolveTarget(sources, index, propagate) { - const source = sources[index]; - let fill = source.fill; - const visited = [index]; - let target; - if (!propagate) { - return fill; +function computeLinearBoundary(source) { + const {scale = {}, fill} = source; + const pixel = _getTargetPixel(fill, scale); + if (isNumberFinite(pixel)) { + const horizontal = scale.isHorizontal(); + return { + x: horizontal ? pixel : null, + y: horizontal ? null : pixel + }; } - while (fill !== false && visited.indexOf(fill) === -1) { - if (!isNumberFinite(fill)) { - return fill; - } - target = sources[fill]; - if (!target) { - return false; - } - if (target.visible) { - return fill; - } - visited.push(fill); - fill = target.fill; + return null; +} +function computeCircularBoundary(source) { + const {scale, fill} = source; + const options = scale.options; + const length = scale.getLabels().length; + const start = options.reverse ? scale.max : scale.min; + const value = _getTargetValue(fill, scale, start); + const target = []; + if (options.grid.circular) { + const center = scale.getPointPositionForValue(0, start); + return new simpleArc({ + x: center.x, + y: center.y, + radius: scale.getDistanceFromCenterForValue(value) + }); } - return false; + for (let i = 0; i < length; ++i) { + target.push(scale.getPointPositionForValue(i, value)); + } + return target; +} + +function _drawfill(ctx, source, area) { + const target = _getTarget(source); + const {line, scale, axis} = source; + const lineOpts = line.options; + const fillOption = lineOpts.fill; + const color = lineOpts.backgroundColor; + const {above = color, below = color} = fillOption || {}; + if (target && line.points.length) { + clipArea(ctx, area); + doFill(ctx, {line, target, above, below, area, scale, axis}); + unclipArea(ctx); + } +} +function doFill(ctx, cfg) { + const {line, target, above, below, area, scale} = cfg; + const property = line._loop ? 'angle' : cfg.axis; + ctx.save(); + if (property === 'x' && below !== above) { + clipVertical(ctx, target, area.top); + fill(ctx, {line, target, color: above, scale, property}); + ctx.restore(); + ctx.save(); + clipVertical(ctx, target, area.bottom); + } + fill(ctx, {line, target, color: below, scale, property}); + ctx.restore(); } -function _clip(ctx, target, clipY) { +function clipVertical(ctx, target, clipY) { const {segments, points} = target; let first = true; let lineLoop = false; @@ -10016,7 +10328,7 @@ function _clip(ctx, target, clipY) { for (const segment of segments) { const {start, end} = segment; const firstPoint = points[start]; - const lastPoint = points[findSegmentEnd(start, end, points)]; + const lastPoint = points[_findSegmentEnd(start, end, points)]; if (first) { ctx.moveTo(firstPoint.x, firstPoint.y); first = false; @@ -10035,78 +10347,7 @@ function _clip(ctx, target, clipY) { ctx.closePath(); ctx.clip(); } -function getBounds(property, first, last, loop) { - if (loop) { - return; - } - let start = first[property]; - let end = last[property]; - if (property === 'angle') { - start = _normalizeAngle(start); - end = _normalizeAngle(end); - } - return {property, start, end}; -} -function _getEdge(a, b, prop, fn) { - if (a && b) { - return fn(a[prop], b[prop]); - } - return a ? a[prop] : b ? b[prop] : 0; -} -function _segments(line, target, property) { - const segments = line.segments; - const points = line.points; - const tpoints = target.points; - const parts = []; - for (const segment of segments) { - let {start, end} = segment; - end = findSegmentEnd(start, end, points); - const bounds = getBounds(property, points[start], points[end], segment.loop); - if (!target.segments) { - parts.push({ - source: segment, - target: bounds, - start: points[start], - end: points[end] - }); - continue; - } - const targetSegments = _boundSegments(target, bounds); - for (const tgt of targetSegments) { - const subBounds = getBounds(property, tpoints[tgt.start], tpoints[tgt.end], tgt.loop); - const fillSources = _boundSegment(segment, points, subBounds); - for (const fillSource of fillSources) { - parts.push({ - source: fillSource, - target: tgt, - start: { - [property]: _getEdge(bounds, subBounds, 'start', Math.max) - }, - end: { - [property]: _getEdge(bounds, subBounds, 'end', Math.min) - } - }); - } - } - } - return parts; -} -function clipBounds(ctx, scale, bounds) { - const {top, bottom} = scale.chart.chartArea; - const {property, start, end} = bounds || {}; - if (property === 'x') { - ctx.beginPath(); - ctx.rect(start, top, end - start, bottom - top); - ctx.clip(); - } -} -function interpolatedLineTo(ctx, target, point, property) { - const interpolatedPoint = target.interpolate(point, property); - if (interpolatedPoint) { - ctx.lineTo(interpolatedPoint.x, interpolatedPoint.y); - } -} -function _fill(ctx, cfg) { +function fill(ctx, cfg) { const {line, target, property, color, scale} = cfg; const segments = _segments(line, target, property); for (const {source: src, target: tgt, start, end} of segments) { @@ -10114,7 +10355,7 @@ function _fill(ctx, cfg) { const notShape = target !== true; ctx.save(); ctx.fillStyle = backgroundColor; - clipBounds(ctx, scale, notShape && getBounds(property, start, end)); + clipBounds(ctx, scale, notShape && _getBounds(property, start, end)); ctx.beginPath(); const lineLoop = !!line.pathSegment(ctx, src); let loop; @@ -10135,34 +10376,23 @@ function _fill(ctx, cfg) { ctx.restore(); } } -function doFill(ctx, cfg) { - const {line, target, above, below, area, scale} = cfg; - const property = line._loop ? 'angle' : cfg.axis; - ctx.save(); - if (property === 'x' && below !== above) { - _clip(ctx, target, area.top); - _fill(ctx, {line, target, color: above, scale, property}); - ctx.restore(); - ctx.save(); - _clip(ctx, target, area.bottom); +function clipBounds(ctx, scale, bounds) { + const {top, bottom} = scale.chart.chartArea; + const {property, start, end} = bounds || {}; + if (property === 'x') { + ctx.beginPath(); + ctx.rect(start, top, end - start, bottom - top); + ctx.clip(); } - _fill(ctx, {line, target, color: below, scale, property}); - ctx.restore(); } -function drawfill(ctx, source, area) { - const target = getTarget(source); - const {line, scale, axis} = source; - const lineOpts = line.options; - const fillOption = lineOpts.fill; - const color = lineOpts.backgroundColor; - const {above = color, below = color} = fillOption || {}; - if (target && line.points.length) { - clipArea(ctx, area); - doFill(ctx, {line, target, above, below, area, scale, axis}); - unclipArea(ctx); +function interpolatedLineTo(ctx, target, point, property) { + const interpolatedPoint = target.interpolate(point, property); + if (interpolatedPoint) { + ctx.lineTo(interpolatedPoint.x, interpolatedPoint.y); } } -var plugin_filler = { + +var index = { id: 'filler', afterDatasetsUpdate(chart, _args, options) { const count = (chart.data.datasets || []).length; @@ -10176,7 +10406,7 @@ var plugin_filler = { source = { visible: chart.isDatasetVisible(i), index: i, - fill: decodeFill(line, i, count), + fill: _decodeFill(line, i, count), chart, axis: meta.controller.options.indexAxis, scale: meta.vScale, @@ -10191,7 +10421,7 @@ var plugin_filler = { if (!source || source.fill === false) { continue; } - source.fill = resolveTarget(sources, i, options.propagate); + source.fill = _resolveTarget(sources, i, options.propagate); } }, beforeDraw(chart, _args, options) { @@ -10204,8 +10434,8 @@ var plugin_filler = { continue; } source.line.updateControlPoints(area, source.axis); - if (draw) { - drawfill(chart.ctx, source, area); + if (draw && source.fill) { + _drawfill(chart.ctx, source, area); } } }, @@ -10216,17 +10446,17 @@ var plugin_filler = { const metasets = chart.getSortedVisibleDatasetMetas(); for (let i = metasets.length - 1; i >= 0; --i) { const source = metasets[i].$filler; - if (source) { - drawfill(chart.ctx, source, chart.chartArea); + if (_shouldApplyFill(source)) { + _drawfill(chart.ctx, source, chart.chartArea); } } }, beforeDatasetDraw(chart, args, options) { const source = args.meta.$filler; - if (!source || source.fill === false || options.drawTime !== 'beforeDatasetDraw') { + if (!_shouldApplyFill(source) || options.drawTime !== 'beforeDatasetDraw') { return; } - drawfill(chart.ctx, source, chart.chartArea); + _drawfill(chart.ctx, source, chart.chartArea); }, defaults: { propagate: true, @@ -10238,7 +10468,7 @@ const getBoxSize = (labelOpts, fontSize) => { let {boxHeight = fontSize, boxWidth = fontSize} = labelOpts; if (labelOpts.usePointStyle) { boxHeight = Math.min(boxHeight, fontSize); - boxWidth = Math.min(boxWidth, fontSize); + boxWidth = labelOpts.pointStyleWidth || Math.min(boxWidth, fontSize); } return { boxWidth, @@ -10455,14 +10685,14 @@ class Legend extends Element { ctx.setLineDash(valueOrDefault(legendItem.lineDash, [])); if (labelOpts.usePointStyle) { const drawOptions = { - radius: boxWidth * Math.SQRT2 / 2, + radius: boxHeight * Math.SQRT2 / 2, pointStyle: legendItem.pointStyle, rotation: legendItem.rotation, borderWidth: lineWidth }; const centerX = rtlHelper.xPlus(x, boxWidth / 2); const centerY = y + halfFontSize; - drawPoint(ctx, drawOptions, centerX, centerY); + drawPointLegend(ctx, drawOptions, centerX, centerY, labelOpts.pointStyleWidth && boxWidth); } else { const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0); const xBoxLeft = rtlHelper.leftForLtr(x, boxWidth); @@ -10600,7 +10830,7 @@ class Legend extends Element { return; } const hoveredItem = this._getLegendItemAt(e.x, e.y); - if (e.type === 'mousemove') { + if (e.type === 'mousemove' || e.type === 'mouseout') { const previous = this._hoveredItem; const sameItem = itemsEqual(previous, hoveredItem); if (previous && !sameItem) { @@ -10616,7 +10846,7 @@ class Legend extends Element { } } function isListened(type, opts) { - if (type === 'mousemove' && (opts.onHover || opts.onLeave)) { + if ((type === 'mousemove' || type === 'mouseout') && (opts.onHover || opts.onLeave)) { return true; } if (opts.onClick && (type === 'click' || type === 'mouseup')) { @@ -11404,7 +11634,7 @@ class Tooltip extends Element { ctx.fillStyle = labelColors.backgroundColor; drawPoint(ctx, drawOptions, centerX, centerY); } else { - ctx.lineWidth = labelColors.borderWidth || 1; + ctx.lineWidth = isObject(labelColors.borderWidth) ? Math.max(...Object.values(labelColors.borderWidth)) : (labelColors.borderWidth || 1); ctx.strokeStyle = labelColors.borderColor; ctx.setLineDash(labelColors.borderDash || []); ctx.lineDashOffset = labelColors.borderDashOffset || 0; @@ -11566,6 +11796,9 @@ class Tooltip extends Element { } } } + _willRender() { + return !!this.opacity; + } draw(ctx) { const options = this.options.setContext(this.getContext()); let opacity = this.opacity; @@ -11686,16 +11919,16 @@ var plugin_tooltip = { }, afterDraw(chart) { const tooltip = chart.tooltip; - const args = { - tooltip - }; - if (chart.notifyPlugins('beforeTooltipDraw', args) === false) { - return; - } - if (tooltip) { + if (tooltip && tooltip._willRender()) { + const args = { + tooltip + }; + if (chart.notifyPlugins('beforeTooltipDraw', args) === false) { + return; + } tooltip.draw(chart.ctx); + chart.notifyPlugins('afterTooltipDraw', args); } - chart.notifyPlugins('afterTooltipDraw', args); }, afterEvent(chart, args) { if (chart.tooltip) { @@ -11843,7 +12076,7 @@ var plugin_tooltip = { var plugins = /*#__PURE__*/Object.freeze({ __proto__: null, Decimation: plugin_decimation, -Filler: plugin_filler, +Filler: index, Legend: plugin_legend, SubTitle: plugin_subtitle, Title: plugin_title, @@ -12484,9 +12717,26 @@ function drawPointLabels(scale, labelCount) { const {x, y, textAlign, left, top, right, bottom} = scale._pointLabelItems[i]; const {backdropColor} = optsAtIndex; if (!isNullOrUndef(backdropColor)) { + const borderRadius = toTRBLCorners(optsAtIndex.borderRadius); const padding = toPadding(optsAtIndex.backdropPadding); ctx.fillStyle = backdropColor; - ctx.fillRect(left - padding.left, top - padding.top, right - left + padding.width, bottom - top + padding.height); + const backdropLeft = left - padding.left; + const backdropTop = top - padding.top; + const backdropWidth = right - left + padding.width; + const backdropHeight = bottom - top + padding.height; + if (Object.values(borderRadius).some(v => v !== 0)) { + ctx.beginPath(); + addRoundedRectPath(ctx, { + x: backdropLeft, + y: backdropTop, + w: backdropWidth, + h: backdropHeight, + radius: borderRadius, + }); + ctx.fill(); + } else { + ctx.fillRect(backdropLeft, backdropTop, backdropWidth, backdropHeight); + } } renderText( ctx, @@ -12900,6 +13150,7 @@ class TimeScale extends Scale { init(scaleOpts, opts) { const time = scaleOpts.time || (scaleOpts.time = {}); const adapter = this._adapter = new _adapters._date(scaleOpts.adapters.date); + adapter.init(opts); mergeIf(time.displayFormats, adapter.formats()); this._parseOpts = { parser: time.parser, @@ -12980,6 +13231,11 @@ class TimeScale extends Scale { } return ticksFromTimestamps(this, ticks, this._majorUnit); } + afterAutoSkip() { + if (this.options.offsetAfterAutoskip) { + this.initOffsets(this.ticks.map(tick => +tick.value)); + } + } initOffsets(timestamps) { let start = 0; let end = 0; diff --git a/static/js/chart.min.js b/static/js/chart.min.js index 2b3e9984..8f69759e 100644 --- a/static/js/chart.min.js +++ b/static/js/chart.min.js @@ -1,13 +1,13 @@ /*! - * Chart.js v3.7.1 + * Chart.js v3.9.1 * https://www.chartjs.org * (c) 2022 Chart.js Contributors * Released under the MIT License */ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Chart=e()}(this,(function(){"use strict";const t="undefined"==typeof window?function(t){return t()}:window.requestAnimationFrame;function e(e,i,s){const n=s||(t=>Array.prototype.slice.call(t));let o=!1,a=[];return function(...s){a=n(s),o||(o=!0,t.call(window,(()=>{o=!1,e.apply(i,a)})))}}function i(t,e){let i;return function(...s){return e?(clearTimeout(i),i=setTimeout(t,e,s)):t.apply(this,s),e}}const s=t=>"start"===t?"left":"end"===t?"right":"center",n=(t,e,i)=>"start"===t?e:"end"===t?i:(e+i)/2,o=(t,e,i,s)=>t===(s?"left":"right")?i:"center"===t?(e+i)/2:e;var a=new class{constructor(){this._request=null,this._charts=new Map,this._running=!1,this._lastDate=void 0}_notify(t,e,i,s){const n=e.listeners[s],o=e.duration;n.forEach((s=>s({chart:t,initial:e.initial,numSteps:o,currentStep:Math.min(i-e.start,o)})))}_refresh(){this._request||(this._running=!0,this._request=t.call(window,(()=>{this._update(),this._request=null,this._running&&this._refresh()})))}_update(t=Date.now()){let e=0;this._charts.forEach(((i,s)=>{if(!i.running||!i.items.length)return;const n=i.items;let o,a=n.length-1,r=!1;for(;a>=0;--a)o=n[a],o._active?(o._total>i.duration&&(i.duration=o._total),o.tick(t),r=!0):(n[a]=n[n.length-1],n.pop());r&&(s.draw(),this._notify(s,i,t,"progress")),n.length||(i.running=!1,this._notify(s,i,t,"complete"),i.initial=!1),e+=n.length})),this._lastDate=t,0===e&&(this._running=!1)}_getAnims(t){const e=this._charts;let i=e.get(t);return i||(i={running:!1,initial:!0,items:[],listeners:{complete:[],progress:[]}},e.set(t,i)),i}listen(t,e,i){this._getAnims(t).listeners[e].push(i)}add(t,e){e&&e.length&&this._getAnims(t).items.push(...e)}has(t){return this._getAnims(t).items.length>0}start(t){const e=this._charts.get(t);e&&(e.running=!0,e.start=Date.now(),e.duration=e.items.reduce(((t,e)=>Math.max(t,e._duration)),0),this._refresh())}running(t){if(!this._running)return!1;const e=this._charts.get(t);return!!(e&&e.running&&e.items.length)}stop(t){const e=this._charts.get(t);if(!e||!e.items.length)return;const i=e.items;let s=i.length-1;for(;s>=0;--s)i[s].cancel();e.items=[],this._notify(t,e,Date.now(),"complete")}remove(t){return this._charts.delete(t)}}; +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Chart=e()}(this,(function(){"use strict";function t(){}const e=function(){let t=0;return function(){return t++}}();function i(t){return null==t}function s(t){if(Array.isArray&&Array.isArray(t))return!0;const e=Object.prototype.toString.call(t);return"[object"===e.slice(0,7)&&"Array]"===e.slice(-6)}function n(t){return null!==t&&"[object Object]"===Object.prototype.toString.call(t)}const o=t=>("number"==typeof t||t instanceof Number)&&isFinite(+t);function a(t,e){return o(t)?t:e}function r(t,e){return void 0===t?e:t}const l=(t,e)=>"string"==typeof t&&t.endsWith("%")?parseFloat(t)/100:t/e,h=(t,e)=>"string"==typeof t&&t.endsWith("%")?parseFloat(t)/100*e:+t;function c(t,e,i){if(t&&"function"==typeof t.call)return t.apply(i,e)}function d(t,e,i,o){let a,r,l;if(s(t))if(r=t.length,o)for(a=r-1;a>=0;a--)e.call(i,t[a],a);else for(a=0;at,x:t=>t.x,y:t=>t.y};function y(t,e){const i=_[e]||(_[e]=function(t){const e=v(t);return t=>{for(const i of e){if(""===i)break;t=t&&t[i]}return t}}(e));return i(t)}function v(t){const e=t.split("."),i=[];let s="";for(const t of e)s+=t,s.endsWith("\\")?s=s.slice(0,-1)+".":(i.push(s),s="");return i}function w(t){return t.charAt(0).toUpperCase()+t.slice(1)}const M=t=>void 0!==t,k=t=>"function"==typeof t,S=(t,e)=>{if(t.size!==e.size)return!1;for(const i of t)if(!e.has(i))return!1;return!0};function P(t){return"mouseup"===t.type||"click"===t.type||"contextmenu"===t.type}const D=Math.PI,O=2*D,C=O+D,A=Number.POSITIVE_INFINITY,T=D/180,L=D/2,E=D/4,R=2*D/3,I=Math.log10,z=Math.sign;function F(t){const e=Math.round(t);t=N(t,e,t/1e3)?e:t;const i=Math.pow(10,Math.floor(I(t))),s=t/i;return(s<=1?1:s<=2?2:s<=5?5:10)*i}function V(t){const e=[],i=Math.sqrt(t);let s;for(s=1;st-e)).pop(),e}function B(t){return!isNaN(parseFloat(t))&&isFinite(t)}function N(t,e,i){return Math.abs(t-e)=t}function j(t,e,i){let s,n,o;for(s=0,n=t.length;sl&&h=Math.min(e,i)-s&&t<=Math.max(e,i)+s}function tt(t,e,i){i=i||(i=>t[i]1;)s=o+n>>1,i(s)?o=s:n=s;return{lo:o,hi:n}}const et=(t,e,i,s)=>tt(t,i,s?s=>t[s][e]<=i:s=>t[s][e]tt(t,i,(s=>t[s][e]>=i));function st(t,e,i){let s=0,n=t.length;for(;ss&&t[n-1]>i;)n--;return s>0||n{const i="_onData"+w(e),s=t[e];Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value(...e){const n=s.apply(this,e);return t._chartjs.listeners.forEach((t=>{"function"==typeof t[i]&&t[i](...e)})),n}})})))}function at(t,e){const i=t._chartjs;if(!i)return;const s=i.listeners,n=s.indexOf(e);-1!==n&&s.splice(n,1),s.length>0||(nt.forEach((e=>{delete t[e]})),delete t._chartjs)}function rt(t){const e=new Set;let i,s;for(i=0,s=t.length;iArray.prototype.slice.call(t));let n=!1,o=[];return function(...i){o=s(i),n||(n=!0,lt.call(window,(()=>{n=!1,t.apply(e,o)})))}}function ct(t,e){let i;return function(...s){return e?(clearTimeout(i),i=setTimeout(t,e,s)):t.apply(this,s),e}}const dt=t=>"start"===t?"left":"end"===t?"right":"center",ut=(t,e,i)=>"start"===t?e:"end"===t?i:(e+i)/2,ft=(t,e,i,s)=>t===(s?"left":"right")?i:"center"===t?(e+i)/2:e;function gt(t,e,i){const s=e.length;let n=0,o=s;if(t._sorted){const{iScale:a,_parsed:r}=t,l=a.axis,{min:h,max:c,minDefined:d,maxDefined:u}=a.getUserBounds();d&&(n=Z(Math.min(et(r,a.axis,h).lo,i?s:et(e,l,a.getPixelForValue(h)).lo),0,s-1)),o=u?Z(Math.max(et(r,a.axis,c,!0).hi+1,i?0:et(e,l,a.getPixelForValue(c),!0).hi+1),n,s)-n:s-n}return{start:n,count:o}}function pt(t){const{xScale:e,yScale:i,_scaleRanges:s}=t,n={xmin:e.min,xmax:e.max,ymin:i.min,ymax:i.max};if(!s)return t._scaleRanges=n,!0;const o=s.xmin!==e.min||s.xmax!==e.max||s.ymin!==i.min||s.ymax!==i.max;return Object.assign(s,n),o}var mt=new class{constructor(){this._request=null,this._charts=new Map,this._running=!1,this._lastDate=void 0}_notify(t,e,i,s){const n=e.listeners[s],o=e.duration;n.forEach((s=>s({chart:t,initial:e.initial,numSteps:o,currentStep:Math.min(i-e.start,o)})))}_refresh(){this._request||(this._running=!0,this._request=lt.call(window,(()=>{this._update(),this._request=null,this._running&&this._refresh()})))}_update(t=Date.now()){let e=0;this._charts.forEach(((i,s)=>{if(!i.running||!i.items.length)return;const n=i.items;let o,a=n.length-1,r=!1;for(;a>=0;--a)o=n[a],o._active?(o._total>i.duration&&(i.duration=o._total),o.tick(t),r=!0):(n[a]=n[n.length-1],n.pop());r&&(s.draw(),this._notify(s,i,t,"progress")),n.length||(i.running=!1,this._notify(s,i,t,"complete"),i.initial=!1),e+=n.length})),this._lastDate=t,0===e&&(this._running=!1)}_getAnims(t){const e=this._charts;let i=e.get(t);return i||(i={running:!1,initial:!0,items:[],listeners:{complete:[],progress:[]}},e.set(t,i)),i}listen(t,e,i){this._getAnims(t).listeners[e].push(i)}add(t,e){e&&e.length&&this._getAnims(t).items.push(...e)}has(t){return this._getAnims(t).items.length>0}start(t){const e=this._charts.get(t);e&&(e.running=!0,e.start=Date.now(),e.duration=e.items.reduce(((t,e)=>Math.max(t,e._duration)),0),this._refresh())}running(t){if(!this._running)return!1;const e=this._charts.get(t);return!!(e&&e.running&&e.items.length)}stop(t){const e=this._charts.get(t);if(!e||!e.items.length)return;const i=e.items;let s=i.length-1;for(;s>=0;--s)i[s].cancel();e.items=[],this._notify(t,e,Date.now(),"complete")}remove(t){return this._charts.delete(t)}}; /*! - * @kurkle/color v0.1.9 + * @kurkle/color v0.2.1 * https://github.com/kurkle/color#readme - * (c) 2020 Jukka Kurkela + * (c) 2022 Jukka Kurkela * Released under the MIT License - */const r={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,A:10,B:11,C:12,D:13,E:14,F:15,a:10,b:11,c:12,d:13,e:14,f:15},l="0123456789ABCDEF",h=t=>l[15&t],c=t=>l[(240&t)>>4]+l[15&t],d=t=>(240&t)>>4==(15&t);function u(t){var e=function(t){return d(t.r)&&d(t.g)&&d(t.b)&&d(t.a)}(t)?h:c;return t?"#"+e(t.r)+e(t.g)+e(t.b)+(t.a<255?e(t.a):""):t}function f(t){return t+.5|0}const g=(t,e,i)=>Math.max(Math.min(t,i),e);function p(t){return g(f(2.55*t),0,255)}function m(t){return g(f(255*t),0,255)}function x(t){return g(f(t/2.55)/100,0,1)}function b(t){return g(f(100*t),0,100)}const _=/^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/;const y=/^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/;function v(t,e,i){const s=e*Math.min(i,1-i),n=(e,n=(e+t/30)%12)=>i-s*Math.max(Math.min(n-3,9-n,1),-1);return[n(0),n(8),n(4)]}function w(t,e,i){const s=(s,n=(s+t/60)%6)=>i-i*e*Math.max(Math.min(n,4-n,1),0);return[s(5),s(3),s(1)]}function M(t,e,i){const s=v(t,1,.5);let n;for(e+i>1&&(n=1/(e+i),e*=n,i*=n),n=0;n<3;n++)s[n]*=1-e-i,s[n]+=e;return s}function k(t){const e=t.r/255,i=t.g/255,s=t.b/255,n=Math.max(e,i,s),o=Math.min(e,i,s),a=(n+o)/2;let r,l,h;return n!==o&&(h=n-o,l=a>.5?h/(2-n-o):h/(n+o),r=n===e?(i-s)/h+(i>16&255,o>>8&255,255&o]}return t}(),T.transparent=[0,0,0,0]);const e=T[t.toLowerCase()];return e&&{r:e[0],g:e[1],b:e[2],a:4===e.length?e[3]:255}}function R(t,e,i){if(t){let s=k(t);s[e]=Math.max(0,Math.min(s[e]+s[e]*i,0===e?360:1)),s=P(s),t.r=s[0],t.g=s[1],t.b=s[2]}}function E(t,e){return t?Object.assign(e||{},t):t}function I(t){var e={r:0,g:0,b:0,a:255};return Array.isArray(t)?t.length>=3&&(e={r:t[0],g:t[1],b:t[2],a:255},t.length>3&&(e.a=m(t[3]))):(e=E(t,{r:0,g:0,b:0,a:1})).a=m(e.a),e}function z(t){return"r"===t.charAt(0)?function(t){const e=_.exec(t);let i,s,n,o=255;if(e){if(e[7]!==i){const t=+e[7];o=255&(e[8]?p(t):255*t)}return i=+e[1],s=+e[3],n=+e[5],i=255&(e[2]?p(i):i),s=255&(e[4]?p(s):s),n=255&(e[6]?p(n):n),{r:i,g:s,b:n,a:o}}}(t):C(t)}class F{constructor(t){if(t instanceof F)return t;const e=typeof t;let i;var s,n,o;"object"===e?i=I(t):"string"===e&&(o=(s=t).length,"#"===s[0]&&(4===o||5===o?n={r:255&17*r[s[1]],g:255&17*r[s[2]],b:255&17*r[s[3]],a:5===o?17*r[s[4]]:255}:7!==o&&9!==o||(n={r:r[s[1]]<<4|r[s[2]],g:r[s[3]]<<4|r[s[4]],b:r[s[5]]<<4|r[s[6]],a:9===o?r[s[7]]<<4|r[s[8]]:255})),i=n||L(t)||z(t)),this._rgb=i,this._valid=!!i}get valid(){return this._valid}get rgb(){var t=E(this._rgb);return t&&(t.a=x(t.a)),t}set rgb(t){this._rgb=I(t)}rgbString(){return this._valid?(t=this._rgb)&&(t.a<255?`rgba(${t.r}, ${t.g}, ${t.b}, ${x(t.a)})`:`rgb(${t.r}, ${t.g}, ${t.b})`):this._rgb;var t}hexString(){return this._valid?u(this._rgb):this._rgb}hslString(){return this._valid?function(t){if(!t)return;const e=k(t),i=e[0],s=b(e[1]),n=b(e[2]);return t.a<255?`hsla(${i}, ${s}%, ${n}%, ${x(t.a)})`:`hsl(${i}, ${s}%, ${n}%)`}(this._rgb):this._rgb}mix(t,e){const i=this;if(t){const s=i.rgb,n=t.rgb;let o;const a=e===o?.5:e,r=2*a-1,l=s.a-n.a,h=((r*l==-1?r:(r+l)/(1+r*l))+1)/2;o=1-h,s.r=255&h*s.r+o*n.r+.5,s.g=255&h*s.g+o*n.g+.5,s.b=255&h*s.b+o*n.b+.5,s.a=a*s.a+(1-a)*n.a,i.rgb=s}return i}clone(){return new F(this.rgb)}alpha(t){return this._rgb.a=m(t),this}clearer(t){return this._rgb.a*=1-t,this}greyscale(){const t=this._rgb,e=f(.3*t.r+.59*t.g+.11*t.b);return t.r=t.g=t.b=e,this}opaquer(t){return this._rgb.a*=1+t,this}negate(){const t=this._rgb;return t.r=255-t.r,t.g=255-t.g,t.b=255-t.b,this}lighten(t){return R(this._rgb,2,t),this}darken(t){return R(this._rgb,2,-t),this}saturate(t){return R(this._rgb,1,t),this}desaturate(t){return R(this._rgb,1,-t),this}rotate(t){return function(t,e){var i=k(t);i[0]=D(i[0]+e),i=P(i),t.r=i[0],t.g=i[1],t.b=i[2]}(this._rgb,t),this}}function B(t){return new F(t)}const V=t=>t instanceof CanvasGradient||t instanceof CanvasPattern;function W(t){return V(t)?t:B(t)}function N(t){return V(t)?t:B(t).saturate(.5).darken(.1).hexString()}function H(){}const j=function(){let t=0;return function(){return t++}}();function $(t){return null==t}function Y(t){if(Array.isArray&&Array.isArray(t))return!0;const e=Object.prototype.toString.call(t);return"[object"===e.substr(0,7)&&"Array]"===e.substr(-6)}function U(t){return null!==t&&"[object Object]"===Object.prototype.toString.call(t)}const X=t=>("number"==typeof t||t instanceof Number)&&isFinite(+t);function q(t,e){return X(t)?t:e}function K(t,e){return void 0===t?e:t}const G=(t,e)=>"string"==typeof t&&t.endsWith("%")?parseFloat(t)/100:t/e,Z=(t,e)=>"string"==typeof t&&t.endsWith("%")?parseFloat(t)/100*e:+t;function J(t,e,i){if(t&&"function"==typeof t.call)return t.apply(i,e)}function Q(t,e,i,s){let n,o,a;if(Y(t))if(o=t.length,s)for(n=o-1;n>=0;n--)e.call(i,t[n],n);else for(n=0;ni;)t=t[e.substr(i,s-i)],i=s+1,s=rt(e,i);return t}function ht(t){return t.charAt(0).toUpperCase()+t.slice(1)}const ct=t=>void 0!==t,dt=t=>"function"==typeof t,ut=(t,e)=>{if(t.size!==e.size)return!1;for(const i of t)if(!e.has(i))return!1;return!0};function ft(t){return"mouseup"===t.type||"click"===t.type||"contextmenu"===t.type}const gt=Object.create(null),pt=Object.create(null);function mt(t,e){if(!e)return t;const i=e.split(".");for(let e=0,s=i.length;et.chart.platform.getDevicePixelRatio(),this.elements={},this.events=["mousemove","mouseout","click","touchstart","touchmove"],this.font={family:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",size:12,style:"normal",lineHeight:1.2,weight:null},this.hover={},this.hoverBackgroundColor=(t,e)=>N(e.backgroundColor),this.hoverBorderColor=(t,e)=>N(e.borderColor),this.hoverColor=(t,e)=>N(e.color),this.indexAxis="x",this.interaction={mode:"nearest",intersect:!0},this.maintainAspectRatio=!0,this.onHover=null,this.onClick=null,this.parsing=!0,this.plugins={},this.responsive=!0,this.scale=void 0,this.scales={},this.showLine=!0,this.drawActiveElementsOnTop=!0,this.describe(t)}set(t,e){return xt(this,t,e)}get(t){return mt(this,t)}describe(t,e){return xt(pt,t,e)}override(t,e){return xt(gt,t,e)}route(t,e,i,s){const n=mt(this,t),o=mt(this,i),a="_"+e;Object.defineProperties(n,{[a]:{value:n[e],writable:!0},[e]:{enumerable:!0,get(){const t=this[a],e=o[s];return U(t)?Object.assign({},e,t):K(t,e)},set(t){this[a]=t}}})}}({_scriptable:t=>!t.startsWith("on"),_indexable:t=>"events"!==t,hover:{_fallback:"interaction"},interaction:{_scriptable:!1,_indexable:!1}});const _t=Math.PI,yt=2*_t,vt=yt+_t,wt=Number.POSITIVE_INFINITY,Mt=_t/180,kt=_t/2,St=_t/4,Pt=2*_t/3,Dt=Math.log10,Ct=Math.sign;function Ot(t){const e=Math.round(t);t=Lt(t,e,t/1e3)?e:t;const i=Math.pow(10,Math.floor(Dt(t))),s=t/i;return(s<=1?1:s<=2?2:s<=5?5:10)*i}function At(t){const e=[],i=Math.sqrt(t);let s;for(s=1;st-e)).pop(),e}function Tt(t){return!isNaN(parseFloat(t))&&isFinite(t)}function Lt(t,e,i){return Math.abs(t-e)=t}function Et(t,e,i){let s,n,o;for(s=0,n=t.length;sl&&h=Math.min(e,i)-s&&t<=Math.max(e,i)+s}function Ut(t){return!t||$(t.size)||$(t.family)?null:(t.style?t.style+" ":"")+(t.weight?t.weight+" ":"")+t.size+"px "+t.family}function Xt(t,e,i,s,n){let o=e[n];return o||(o=e[n]=t.measureText(n).width,i.push(n)),o>s&&(s=o),s}function qt(t,e,i,s){let n=(s=s||{}).data=s.data||{},o=s.garbageCollect=s.garbageCollect||[];s.font!==e&&(n=s.data={},o=s.garbageCollect=[],s.font=e),t.save(),t.font=e;let a=0;const r=i.length;let l,h,c,d,u;for(l=0;li.length){for(l=0;l0&&t.stroke()}}function Jt(t,e,i){return i=i||.5,!e||t&&t.x>e.left-i&&t.xe.top-i&&t.y0&&""!==o.strokeColor;let l,h;for(t.save(),t.font=n.string,function(t,e){e.translation&&t.translate(e.translation[0],e.translation[1]);$(e.rotation)||t.rotate(e.rotation);e.color&&(t.fillStyle=e.color);e.textAlign&&(t.textAlign=e.textAlign);e.textBaseline&&(t.textBaseline=e.textBaseline)}(t,o),l=0;lt[i]1;)s=o+n>>1,i(s)?o=s:n=s;return{lo:o,hi:n}}const re=(t,e,i)=>ae(t,i,(s=>t[s][e]ae(t,i,(s=>t[s][e]>=i));function he(t,e,i){let s=0,n=t.length;for(;ss&&t[n-1]>i;)n--;return s>0||n{const i="_onData"+ht(e),s=t[e];Object.defineProperty(t,e,{configurable:!0,enumerable:!1,value(...e){const n=s.apply(this,e);return t._chartjs.listeners.forEach((t=>{"function"==typeof t[i]&&t[i](...e)})),n}})})))}function ue(t,e){const i=t._chartjs;if(!i)return;const s=i.listeners,n=s.indexOf(e);-1!==n&&s.splice(n,1),s.length>0||(ce.forEach((e=>{delete t[e]})),delete t._chartjs)}function fe(t){const e=new Set;let i,s;for(i=0,s=t.length;iwindow.getComputedStyle(t,null);function be(t,e){return xe(t).getPropertyValue(e)}const _e=["top","right","bottom","left"];function ye(t,e,i){const s={};i=i?"-"+i:"";for(let n=0;n<4;n++){const o=_e[n];s[o]=parseFloat(t[e+"-"+o+i])||0}return s.width=s.left+s.right,s.height=s.top+s.bottom,s}function ve(t,e){const{canvas:i,currentDevicePixelRatio:s}=e,n=xe(i),o="border-box"===n.boxSizing,a=ye(n,"padding"),r=ye(n,"border","width"),{x:l,y:h,box:c}=function(t,e){const i=t.native||t,s=i.touches,n=s&&s.length?s[0]:i,{offsetX:o,offsetY:a}=n;let r,l,h=!1;if(((t,e,i)=>(t>0||e>0)&&(!i||!i.shadowRoot))(o,a,i.target))r=o,l=a;else{const t=e.getBoundingClientRect();r=n.clientX-t.left,l=n.clientY-t.top,h=!0}return{x:r,y:l,box:h}}(t,i),d=a.left+(c&&r.left),u=a.top+(c&&r.top);let{width:f,height:g}=e;return o&&(f-=a.width+r.width,g-=a.height+r.height),{x:Math.round((l-d)/f*i.width/s),y:Math.round((h-u)/g*i.height/s)}}const we=t=>Math.round(10*t)/10;function Me(t,e,i,s){const n=xe(t),o=ye(n,"margin"),a=me(n.maxWidth,t,"clientWidth")||wt,r=me(n.maxHeight,t,"clientHeight")||wt,l=function(t,e,i){let s,n;if(void 0===e||void 0===i){const o=pe(t);if(o){const t=o.getBoundingClientRect(),a=xe(o),r=ye(a,"border","width"),l=ye(a,"padding");e=t.width-l.width-r.width,i=t.height-l.height-r.height,s=me(a.maxWidth,o,"clientWidth"),n=me(a.maxHeight,o,"clientHeight")}else e=t.clientWidth,i=t.clientHeight}return{width:e,height:i,maxWidth:s||wt,maxHeight:n||wt}}(t,e,i);let{width:h,height:c}=l;if("content-box"===n.boxSizing){const t=ye(n,"border","width"),e=ye(n,"padding");h-=e.width+t.width,c-=e.height+t.height}return h=Math.max(0,h-o.width),c=Math.max(0,s?Math.floor(h/s):c-o.height),h=we(Math.min(h,a,l.maxWidth)),c=we(Math.min(c,r,l.maxHeight)),h&&!c&&(c=we(h/2)),{width:h,height:c}}function ke(t,e,i){const s=e||1,n=Math.floor(t.height*s),o=Math.floor(t.width*s);t.height=n/s,t.width=o/s;const a=t.canvas;return a.style&&(i||!a.style.height&&!a.style.width)&&(a.style.height=`${t.height}px`,a.style.width=`${t.width}px`),(t.currentDevicePixelRatio!==s||a.height!==n||a.width!==o)&&(t.currentDevicePixelRatio=s,a.height=n,a.width=o,t.ctx.setTransform(s,0,0,s,0,0),!0)}const Se=function(){let t=!1;try{const e={get passive(){return t=!0,!1}};window.addEventListener("test",null,e),window.removeEventListener("test",null,e)}catch(t){}return t}();function Pe(t,e){const i=be(t,e),s=i&&i.match(/^(\d+)(\.\d+)?px$/);return s?+s[1]:void 0}function De(t,e){return"native"in t?{x:t.x,y:t.y}:ve(t,e)}function Ce(t,e,i,s){const{controller:n,data:o,_sorted:a}=t,r=n._cachedMeta.iScale;if(r&&e===r.axis&&"r"!==e&&a&&o.length){const t=r._reversePixels?le:re;if(!s)return t(o,e,i);if(n._sharedOptions){const s=o[0],n="function"==typeof s.getRange&&s.getRange(e);if(n){const s=t(o,e,i-n),a=t(o,e,i+n);return{lo:s.lo,hi:a.hi}}}}return{lo:0,hi:o.length-1}}function Oe(t,e,i,s,n){const o=t.getSortedVisibleDatasetMetas(),a=i[e];for(let t=0,i=o.length;t{t[r](n[a],s)&&o.push({element:t,datasetIndex:e,index:i}),t.inRange(n.x,n.y,s)&&(l=!0)})),i.intersect&&!l?[]:o}var Ee={modes:{index(t,e,i,s){const n=De(e,t),o=i.axis||"x",a=i.intersect?Ae(t,n,o,s):Le(t,n,o,!1,s),r=[];return a.length?(t.getSortedVisibleDatasetMetas().forEach((t=>{const e=a[0].index,i=t.data[e];i&&!i.skip&&r.push({element:i,datasetIndex:t.index,index:e})})),r):[]},dataset(t,e,i,s){const n=De(e,t),o=i.axis||"xy";let a=i.intersect?Ae(t,n,o,s):Le(t,n,o,!1,s);if(a.length>0){const e=a[0].datasetIndex,i=t.getDatasetMeta(e).data;a=[];for(let t=0;tAe(t,De(e,t),i.axis||"xy",s),nearest:(t,e,i,s)=>Le(t,De(e,t),i.axis||"xy",i.intersect,s),x:(t,e,i,s)=>Re(t,e,{axis:"x",intersect:i.intersect},s),y:(t,e,i,s)=>Re(t,e,{axis:"y",intersect:i.intersect},s)}};const Ie=new RegExp(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/),ze=new RegExp(/^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/);function Fe(t,e){const i=(""+t).match(Ie);if(!i||"normal"===i[1])return 1.2*e;switch(t=+i[2],i[3]){case"px":return t;case"%":t/=100}return e*t}function Be(t,e){const i={},s=U(e),n=s?Object.keys(e):e,o=U(t)?s?i=>K(t[i],t[e[i]]):e=>t[e]:()=>t;for(const t of n)i[t]=+o(t)||0;return i}function Ve(t){return Be(t,{top:"y",right:"x",bottom:"y",left:"x"})}function We(t){return Be(t,["topLeft","topRight","bottomLeft","bottomRight"])}function Ne(t){const e=Ve(t);return e.width=e.left+e.right,e.height=e.top+e.bottom,e}function He(t,e){t=t||{},e=e||bt.font;let i=K(t.size,e.size);"string"==typeof i&&(i=parseInt(i,10));let s=K(t.style,e.style);s&&!(""+s).match(ze)&&(console.warn('Invalid font style specified: "'+s+'"'),s="");const n={family:K(t.family,e.family),lineHeight:Fe(K(t.lineHeight,e.lineHeight),i),size:i,style:s,weight:K(t.weight,e.weight),string:""};return n.string=Ut(n),n}function je(t,e,i,s){let n,o,a,r=!0;for(n=0,o=t.length;ni&&0===t?0:t+e;return{min:a(s,-Math.abs(o)),max:a(n,o)}}function Ye(t,e){return Object.assign(Object.create(t),e)}const Ue=["left","top","right","bottom"];function Xe(t,e){return t.filter((t=>t.pos===e))}function qe(t,e){return t.filter((t=>-1===Ue.indexOf(t.pos)&&t.box.axis===e))}function Ke(t,e){return t.sort(((t,i)=>{const s=e?i:t,n=e?t:i;return s.weight===n.weight?s.index-n.index:s.weight-n.weight}))}function Ge(t,e){const i=function(t){const e={};for(const i of t){const{stack:t,pos:s,stackWeight:n}=i;if(!t||!Ue.includes(s))continue;const o=e[t]||(e[t]={count:0,placed:0,weight:0,size:0});o.count++,o.weight+=n}return e}(t),{vBoxMaxWidth:s,hBoxMaxHeight:n}=e;let o,a,r;for(o=0,a=t.length;o{s[t]=Math.max(e[t],i[t])})),s}return s(t?["left","right"]:["top","bottom"])}function ei(t,e,i,s){const n=[];let o,a,r,l,h,c;for(o=0,a=t.length,h=0;ot.box.fullSize)),!0),s=Ke(Xe(e,"left"),!0),n=Ke(Xe(e,"right")),o=Ke(Xe(e,"top"),!0),a=Ke(Xe(e,"bottom")),r=qe(e,"x"),l=qe(e,"y");return{fullSize:i,leftAndTop:s.concat(o),rightAndBottom:n.concat(l).concat(a).concat(r),chartArea:Xe(e,"chartArea"),vertical:s.concat(n).concat(l),horizontal:o.concat(a).concat(r)}}(t.boxes),l=r.vertical,h=r.horizontal;Q(t.boxes,(t=>{"function"==typeof t.beforeLayout&&t.beforeLayout()}));const c=l.reduce(((t,e)=>e.box.options&&!1===e.box.options.display?t:t+1),0)||1,d=Object.freeze({outerWidth:e,outerHeight:i,padding:n,availableWidth:o,availableHeight:a,vBoxMaxWidth:o/2/c,hBoxMaxHeight:a/2}),u=Object.assign({},n);Je(u,Ne(s));const f=Object.assign({maxPadding:u,w:o,h:a,x:n.left,y:n.top},n),g=Ge(l.concat(h),d);ei(r.fullSize,f,d,g),ei(l,f,d,g),ei(h,f,d,g)&&ei(l,f,d,g),function(t){const e=t.maxPadding;function i(i){const s=Math.max(e[i]-t[i],0);return t[i]+=s,s}t.y+=i("top"),t.x+=i("left"),i("right"),i("bottom")}(f),si(r.leftAndTop,f,d,g),f.x+=f.w,f.y+=f.h,si(r.rightAndBottom,f,d,g),t.chartArea={left:f.left,top:f.top,right:f.left+f.w,bottom:f.top+f.h,height:f.h,width:f.w},Q(r.chartArea,(e=>{const i=e.box;Object.assign(i,t.chartArea),i.update(f.w,f.h,{left:0,top:0,right:0,bottom:0})}))}};function oi(t,e=[""],i=t,s,n=(()=>t[0])){ct(s)||(s=mi("_fallback",t));const o={[Symbol.toStringTag]:"Object",_cacheable:!0,_scopes:t,_rootScopes:i,_fallback:s,_getTarget:n,override:n=>oi([n,...t],e,i,s)};return new Proxy(o,{deleteProperty:(e,i)=>(delete e[i],delete e._keys,delete t[0][i],!0),get:(i,s)=>ci(i,s,(()=>function(t,e,i,s){let n;for(const o of e)if(n=mi(li(o,t),i),ct(n))return hi(t,n)?gi(i,s,t,n):n}(s,e,t,i))),getOwnPropertyDescriptor:(t,e)=>Reflect.getOwnPropertyDescriptor(t._scopes[0],e),getPrototypeOf:()=>Reflect.getPrototypeOf(t[0]),has:(t,e)=>xi(t).includes(e),ownKeys:t=>xi(t),set(t,e,i){const s=t._storage||(t._storage=n());return t[e]=s[e]=i,delete t._keys,!0}})}function ai(t,e,i,s){const n={_cacheable:!1,_proxy:t,_context:e,_subProxy:i,_stack:new Set,_descriptors:ri(t,s),setContext:e=>ai(t,e,i,s),override:n=>ai(t.override(n),e,i,s)};return new Proxy(n,{deleteProperty:(e,i)=>(delete e[i],delete t[i],!0),get:(t,e,i)=>ci(t,e,(()=>function(t,e,i){const{_proxy:s,_context:n,_subProxy:o,_descriptors:a}=t;let r=s[e];dt(r)&&a.isScriptable(e)&&(r=function(t,e,i,s){const{_proxy:n,_context:o,_subProxy:a,_stack:r}=i;if(r.has(t))throw new Error("Recursion detected: "+Array.from(r).join("->")+"->"+t);r.add(t),e=e(o,a||s),r.delete(t),hi(t,e)&&(e=gi(n._scopes,n,t,e));return e}(e,r,t,i));Y(r)&&r.length&&(r=function(t,e,i,s){const{_proxy:n,_context:o,_subProxy:a,_descriptors:r}=i;if(ct(o.index)&&s(t))e=e[o.index%e.length];else if(U(e[0])){const i=e,s=n._scopes.filter((t=>t!==i));e=[];for(const l of i){const i=gi(s,n,t,l);e.push(ai(i,o,a&&a[t],r))}}return e}(e,r,t,a.isIndexable));hi(e,r)&&(r=ai(r,n,o&&o[e],a));return r}(t,e,i))),getOwnPropertyDescriptor:(e,i)=>e._descriptors.allKeys?Reflect.has(t,i)?{enumerable:!0,configurable:!0}:void 0:Reflect.getOwnPropertyDescriptor(t,i),getPrototypeOf:()=>Reflect.getPrototypeOf(t),has:(e,i)=>Reflect.has(t,i),ownKeys:()=>Reflect.ownKeys(t),set:(e,i,s)=>(t[i]=s,delete e[i],!0)})}function ri(t,e={scriptable:!0,indexable:!0}){const{_scriptable:i=e.scriptable,_indexable:s=e.indexable,_allKeys:n=e.allKeys}=t;return{allKeys:n,scriptable:i,indexable:s,isScriptable:dt(i)?i:()=>i,isIndexable:dt(s)?s:()=>s}}const li=(t,e)=>t?t+ht(e):e,hi=(t,e)=>U(e)&&"adapters"!==t&&(null===Object.getPrototypeOf(e)||e.constructor===Object);function ci(t,e,i){if(Object.prototype.hasOwnProperty.call(t,e))return t[e];const s=i();return t[e]=s,s}function di(t,e,i){return dt(t)?t(e,i):t}const ui=(t,e)=>!0===t?e:"string"==typeof t?lt(e,t):void 0;function fi(t,e,i,s,n){for(const o of e){const e=ui(i,o);if(e){t.add(e);const o=di(e._fallback,i,n);if(ct(o)&&o!==i&&o!==s)return o}else if(!1===e&&ct(s)&&i!==s)return null}return!1}function gi(t,e,i,s){const n=e._rootScopes,o=di(e._fallback,i,s),a=[...t,...n],r=new Set;r.add(s);let l=pi(r,a,i,o||i,s);return null!==l&&((!ct(o)||o===i||(l=pi(r,a,o,l,s),null!==l))&&oi(Array.from(r),[""],n,o,(()=>function(t,e,i){const s=t._getTarget();e in s||(s[e]={});const n=s[e];if(Y(n)&&U(i))return i;return n}(e,i,s))))}function pi(t,e,i,s,n){for(;i;)i=fi(t,e,i,s,n);return i}function mi(t,e){for(const i of e){if(!i)continue;const e=i[t];if(ct(e))return e}}function xi(t){let e=t._keys;return e||(e=t._keys=function(t){const e=new Set;for(const i of t)for(const t of Object.keys(i).filter((t=>!t.startsWith("_"))))e.add(t);return Array.from(e)}(t._scopes)),e}const bi=Number.EPSILON||1e-14,_i=(t,e)=>e"x"===t?"y":"x";function vi(t,e,i,s){const n=t.skip?e:t,o=e,a=i.skip?e:i,r=Vt(o,n),l=Vt(a,o);let h=r/(r+l),c=l/(r+l);h=isNaN(h)?0:h,c=isNaN(c)?0:c;const d=s*h,u=s*c;return{previous:{x:o.x-d*(a.x-n.x),y:o.y-d*(a.y-n.y)},next:{x:o.x+u*(a.x-n.x),y:o.y+u*(a.y-n.y)}}}function wi(t,e="x"){const i=yi(e),s=t.length,n=Array(s).fill(0),o=Array(s);let a,r,l,h=_i(t,0);for(a=0;a!t.skip))),"monotone"===e.cubicInterpolationMode)wi(t,n);else{let i=s?t[t.length-1]:t[0];for(o=0,a=t.length;o0===t||1===t,Pi=(t,e,i)=>-Math.pow(2,10*(t-=1))*Math.sin((t-e)*yt/i),Di=(t,e,i)=>Math.pow(2,-10*t)*Math.sin((t-e)*yt/i)+1,Ci={linear:t=>t,easeInQuad:t=>t*t,easeOutQuad:t=>-t*(t-2),easeInOutQuad:t=>(t/=.5)<1?.5*t*t:-.5*(--t*(t-2)-1),easeInCubic:t=>t*t*t,easeOutCubic:t=>(t-=1)*t*t+1,easeInOutCubic:t=>(t/=.5)<1?.5*t*t*t:.5*((t-=2)*t*t+2),easeInQuart:t=>t*t*t*t,easeOutQuart:t=>-((t-=1)*t*t*t-1),easeInOutQuart:t=>(t/=.5)<1?.5*t*t*t*t:-.5*((t-=2)*t*t*t-2),easeInQuint:t=>t*t*t*t*t,easeOutQuint:t=>(t-=1)*t*t*t*t+1,easeInOutQuint:t=>(t/=.5)<1?.5*t*t*t*t*t:.5*((t-=2)*t*t*t*t+2),easeInSine:t=>1-Math.cos(t*kt),easeOutSine:t=>Math.sin(t*kt),easeInOutSine:t=>-.5*(Math.cos(_t*t)-1),easeInExpo:t=>0===t?0:Math.pow(2,10*(t-1)),easeOutExpo:t=>1===t?1:1-Math.pow(2,-10*t),easeInOutExpo:t=>Si(t)?t:t<.5?.5*Math.pow(2,10*(2*t-1)):.5*(2-Math.pow(2,-10*(2*t-1))),easeInCirc:t=>t>=1?t:-(Math.sqrt(1-t*t)-1),easeOutCirc:t=>Math.sqrt(1-(t-=1)*t),easeInOutCirc:t=>(t/=.5)<1?-.5*(Math.sqrt(1-t*t)-1):.5*(Math.sqrt(1-(t-=2)*t)+1),easeInElastic:t=>Si(t)?t:Pi(t,.075,.3),easeOutElastic:t=>Si(t)?t:Di(t,.075,.3),easeInOutElastic(t){const e=.1125;return Si(t)?t:t<.5?.5*Pi(2*t,e,.45):.5+.5*Di(2*t-1,e,.45)},easeInBack(t){const e=1.70158;return t*t*((e+1)*t-e)},easeOutBack(t){const e=1.70158;return(t-=1)*t*((e+1)*t+e)+1},easeInOutBack(t){let e=1.70158;return(t/=.5)<1?t*t*((1+(e*=1.525))*t-e)*.5:.5*((t-=2)*t*((1+(e*=1.525))*t+e)+2)},easeInBounce:t=>1-Ci.easeOutBounce(1-t),easeOutBounce(t){const e=7.5625,i=2.75;return t<1/i?e*t*t:t<2/i?e*(t-=1.5/i)*t+.75:t<2.5/i?e*(t-=2.25/i)*t+.9375:e*(t-=2.625/i)*t+.984375},easeInOutBounce:t=>t<.5?.5*Ci.easeInBounce(2*t):.5*Ci.easeOutBounce(2*t-1)+.5};function Oi(t,e,i,s){return{x:t.x+i*(e.x-t.x),y:t.y+i*(e.y-t.y)}}function Ai(t,e,i,s){return{x:t.x+i*(e.x-t.x),y:"middle"===s?i<.5?t.y:e.y:"after"===s?i<1?t.y:e.y:i>0?e.y:t.y}}function Ti(t,e,i,s){const n={x:t.cp2x,y:t.cp2y},o={x:e.cp1x,y:e.cp1y},a=Oi(t,n,i),r=Oi(n,o,i),l=Oi(o,e,i),h=Oi(a,r,i),c=Oi(r,l,i);return Oi(h,c,i)}const Li=new Map;function Ri(t,e,i){return function(t,e){e=e||{};const i=t+JSON.stringify(e);let s=Li.get(i);return s||(s=new Intl.NumberFormat(t,e),Li.set(i,s)),s}(e,i).format(t)}function Ei(t,e,i){return t?function(t,e){return{x:i=>t+t+e-i,setWidth(t){e=t},textAlign:t=>"center"===t?t:"right"===t?"left":"right",xPlus:(t,e)=>t-e,leftForLtr:(t,e)=>t-e}}(e,i):{x:t=>t,setWidth(t){},textAlign:t=>t,xPlus:(t,e)=>t+e,leftForLtr:(t,e)=>t}}function Ii(t,e){let i,s;"ltr"!==e&&"rtl"!==e||(i=t.canvas.style,s=[i.getPropertyValue("direction"),i.getPropertyPriority("direction")],i.setProperty("direction",e,"important"),t.prevTextDirection=s)}function zi(t,e){void 0!==e&&(delete t.prevTextDirection,t.canvas.style.setProperty("direction",e[0],e[1]))}function Fi(t){return"angle"===t?{between:Ht,compare:Wt,normalize:Nt}:{between:Yt,compare:(t,e)=>t-e,normalize:t=>t}}function Bi({start:t,end:e,count:i,loop:s,style:n}){return{start:t%i,end:e%i,loop:s&&(e-t+1)%i==0,style:n}}function Vi(t,e,i){if(!i)return[t];const{property:s,start:n,end:o}=i,a=e.length,{compare:r,between:l,normalize:h}=Fi(s),{start:c,end:d,loop:u,style:f}=function(t,e,i){const{property:s,start:n,end:o}=i,{between:a,normalize:r}=Fi(s),l=e.length;let h,c,{start:d,end:u,loop:f}=t;if(f){for(d+=l,u+=l,h=0,c=l;hb||l(n,x,p)&&0!==r(n,x),v=()=>!b||0===r(o,p)||l(o,x,p);for(let t=c,i=c;t<=d;++t)m=e[t%a],m.skip||(p=h(m[s]),p!==x&&(b=l(p,n,o),null===_&&y()&&(_=0===r(p,n)?t:i),null!==_&&v()&&(g.push(Bi({start:_,end:t,loop:u,count:a,style:f})),_=null),i=t,x=p));return null!==_&&g.push(Bi({start:_,end:d,loop:u,count:a,style:f})),g}function Wi(t,e){const i=[],s=t.segments;for(let n=0;nn&&t[o%e].skip;)o--;return o%=e,{start:n,end:o}}(i,n,o,s);if(!0===s)return Hi(t,[{start:a,end:r,loop:o}],i,e);return Hi(t,function(t,e,i,s){const n=t.length,o=[];let a,r=e,l=t[e];for(a=e+1;a<=i;++a){const i=t[a%n];i.skip||i.stop?l.skip||(s=!1,o.push({start:e%n,end:(a-1)%n,loop:s}),e=r=i.stop?a:null):(r=a,l.skip&&(e=a)),l=i}return null!==r&&o.push({start:e%n,end:r%n,loop:s}),o}(i,a,rnull===t||""===t;const Gi=!!Se&&{passive:!0};function Zi(t,e,i){t.canvas.removeEventListener(e,i,Gi)}function Ji(t,e){for(const i of t)if(i===e||i.contains(e))return!0}function Qi(t,e,i){const s=t.canvas,n=new MutationObserver((t=>{let e=!1;for(const i of t)e=e||Ji(i.addedNodes,s),e=e&&!Ji(i.removedNodes,s);e&&i()}));return n.observe(document,{childList:!0,subtree:!0}),n}function ts(t,e,i){const s=t.canvas,n=new MutationObserver((t=>{let e=!1;for(const i of t)e=e||Ji(i.removedNodes,s),e=e&&!Ji(i.addedNodes,s);e&&i()}));return n.observe(document,{childList:!0,subtree:!0}),n}const es=new Map;let is=0;function ss(){const t=window.devicePixelRatio;t!==is&&(is=t,es.forEach(((e,i)=>{i.currentDevicePixelRatio!==t&&e()})))}function ns(t,i,s){const n=t.canvas,o=n&&pe(n);if(!o)return;const a=e(((t,e)=>{const i=o.clientWidth;s(t,e),i{const e=t[0],i=e.contentRect.width,s=e.contentRect.height;0===i&&0===s||a(i,s)}));return r.observe(o),function(t,e){es.size||window.addEventListener("resize",ss),es.set(t,e)}(t,a),r}function os(t,e,i){i&&i.disconnect(),"resize"===e&&function(t){es.delete(t),es.size||window.removeEventListener("resize",ss)}(t)}function as(t,i,s){const n=t.canvas,o=e((e=>{null!==t.ctx&&s(function(t,e){const i=qi[t.type]||t.type,{x:s,y:n}=ve(t,e);return{type:i,chart:e,native:t,x:void 0!==s?s:null,y:void 0!==n?n:null}}(e,t))}),t,(t=>{const e=t[0];return[e,e.offsetX,e.offsetY]}));return function(t,e,i){t.addEventListener(e,i,Gi)}(n,i,o),o}class rs extends Ui{acquireContext(t,e){const i=t&&t.getContext&&t.getContext("2d");return i&&i.canvas===t?(function(t,e){const i=t.style,s=t.getAttribute("height"),n=t.getAttribute("width");if(t.$chartjs={initial:{height:s,width:n,style:{display:i.display,height:i.height,width:i.width}}},i.display=i.display||"block",i.boxSizing=i.boxSizing||"border-box",Ki(n)){const e=Pe(t,"width");void 0!==e&&(t.width=e)}if(Ki(s))if(""===t.style.height)t.height=t.width/(e||2);else{const e=Pe(t,"height");void 0!==e&&(t.height=e)}}(t,e),i):null}releaseContext(t){const e=t.canvas;if(!e.$chartjs)return!1;const i=e.$chartjs.initial;["height","width"].forEach((t=>{const s=i[t];$(s)?e.removeAttribute(t):e.setAttribute(t,s)}));const s=i.style||{};return Object.keys(s).forEach((t=>{e.style[t]=s[t]})),e.width=e.width,delete e.$chartjs,!0}addEventListener(t,e,i){this.removeEventListener(t,e);const s=t.$proxies||(t.$proxies={}),n={attach:Qi,detach:ts,resize:ns}[e]||as;s[e]=n(t,e,i)}removeEventListener(t,e){const i=t.$proxies||(t.$proxies={}),s=i[e];if(!s)return;({attach:os,detach:os,resize:os}[e]||Zi)(t,e,s),i[e]=void 0}getDevicePixelRatio(){return window.devicePixelRatio}getMaximumSize(t,e,i,s){return Me(t,e,i,s)}isAttached(t){const e=pe(t);return!(!e||!e.isConnected)}}function ls(t){return!ge()||"undefined"!=typeof OffscreenCanvas&&t instanceof OffscreenCanvas?Xi:rs}var hs=Object.freeze({__proto__:null,_detectPlatform:ls,BasePlatform:Ui,BasicPlatform:Xi,DomPlatform:rs});const cs="transparent",ds={boolean:(t,e,i)=>i>.5?e:t,color(t,e,i){const s=W(t||cs),n=s.valid&&W(e||cs);return n&&n.valid?n.mix(s,i).hexString():e},number:(t,e,i)=>t+(e-t)*i};class us{constructor(t,e,i,s){const n=e[i];s=je([t.to,s,n,t.from]);const o=je([t.from,n,s]);this._active=!0,this._fn=t.fn||ds[t.type||typeof o],this._easing=Ci[t.easing]||Ci.linear,this._start=Math.floor(Date.now()+(t.delay||0)),this._duration=this._total=Math.floor(t.duration),this._loop=!!t.loop,this._target=e,this._prop=i,this._from=o,this._to=s,this._promises=void 0}active(){return this._active}update(t,e,i){if(this._active){this._notify(!1);const s=this._target[this._prop],n=i-this._start,o=this._duration-n;this._start=i,this._duration=Math.floor(Math.max(o,t.duration)),this._total+=n,this._loop=!!t.loop,this._to=je([t.to,e,s,t.from]),this._from=je([t.from,s,e])}}cancel(){this._active&&(this.tick(Date.now()),this._active=!1,this._notify(!1))}tick(t){const e=t-this._start,i=this._duration,s=this._prop,n=this._from,o=this._loop,a=this._to;let r;if(this._active=n!==a&&(o||e1?2-r:r,r=this._easing(Math.min(1,Math.max(0,r))),this._target[s]=this._fn(n,a,r))}wait(){const t=this._promises||(this._promises=[]);return new Promise(((e,i)=>{t.push({res:e,rej:i})}))}_notify(t){const e=t?"res":"rej",i=this._promises||[];for(let t=0;t"onProgress"!==t&&"onComplete"!==t&&"fn"!==t}),bt.set("animations",{colors:{type:"color",properties:["color","borderColor","backgroundColor"]},numbers:{type:"number",properties:["x","y","borderWidth","radius","tension"]}}),bt.describe("animations",{_fallback:"animation"}),bt.set("transitions",{active:{animation:{duration:400}},resize:{animation:{duration:0}},show:{animations:{colors:{from:"transparent"},visible:{type:"boolean",duration:0}}},hide:{animations:{colors:{to:"transparent"},visible:{type:"boolean",easing:"linear",fn:t=>0|t}}}});class gs{constructor(t,e){this._chart=t,this._properties=new Map,this.configure(e)}configure(t){if(!U(t))return;const e=this._properties;Object.getOwnPropertyNames(t).forEach((i=>{const s=t[i];if(!U(s))return;const n={};for(const t of fs)n[t]=s[t];(Y(s.properties)&&s.properties||[i]).forEach((t=>{t!==i&&e.has(t)||e.set(t,n)}))}))}_animateOptions(t,e){const i=e.options,s=function(t,e){if(!e)return;let i=t.options;if(!i)return void(t.options=e);i.$shared&&(t.options=i=Object.assign({},i,{$shared:!1,$animations:{}}));return i}(t,i);if(!s)return[];const n=this._createAnimations(s,i);return i.$shared&&function(t,e){const i=[],s=Object.keys(e);for(let e=0;e{t.options=i}),(()=>{})),n}_createAnimations(t,e){const i=this._properties,s=[],n=t.$animations||(t.$animations={}),o=Object.keys(e),a=Date.now();let r;for(r=o.length-1;r>=0;--r){const l=o[r];if("$"===l.charAt(0))continue;if("options"===l){s.push(...this._animateOptions(t,e));continue}const h=e[l];let c=n[l];const d=i.get(l);if(c){if(d&&c.active()){c.update(d,h,a);continue}c.cancel()}d&&d.duration?(n[l]=c=new us(d,t,l,h),s.push(c)):t[l]=h}return s}update(t,e){if(0===this._properties.size)return void Object.assign(t,e);const i=this._createAnimations(t,e);return i.length?(a.add(this._chart,i),!0):void 0}}function ps(t,e){const i=t&&t.options||{},s=i.reverse,n=void 0===i.min?e:0,o=void 0===i.max?e:0;return{start:s?o:n,end:s?n:o}}function ms(t,e){const i=[],s=t._getSortedDatasetMetas(e);let n,o;for(n=0,o=s.length;n0||!i&&e<0)return n.index}return null}function vs(t,e){const{chart:i,_cachedMeta:s}=t,n=i._stacks||(i._stacks={}),{iScale:o,vScale:a,index:r}=s,l=o.axis,h=a.axis,c=function(t,e,i){return`${t.id}.${e.id}.${i.stack||i.type}`}(o,a,s),d=e.length;let u;for(let t=0;ti[t].axis===e)).shift()}function Ms(t,e){const i=t.controller.index,s=t.vScale&&t.vScale.axis;if(s){e=e||t._parsed;for(const t of e){const e=t._stacks;if(!e||void 0===e[s]||void 0===e[s][i])return;delete e[s][i]}}}const ks=t=>"reset"===t||"none"===t,Ss=(t,e)=>e?t:Object.assign({},t);class Ps{constructor(t,e){this.chart=t,this._ctx=t.ctx,this.index=e,this._cachedDataOpts={},this._cachedMeta=this.getMeta(),this._type=this._cachedMeta.type,this.options=void 0,this._parsing=!1,this._data=void 0,this._objectData=void 0,this._sharedOptions=void 0,this._drawStart=void 0,this._drawCount=void 0,this.enableOptionSharing=!1,this.$context=void 0,this._syncList=[],this.initialize()}initialize(){const t=this._cachedMeta;this.configure(),this.linkScales(),t._stacked=bs(t.vScale,t),this.addElements()}updateIndex(t){this.index!==t&&Ms(this._cachedMeta),this.index=t}linkScales(){const t=this.chart,e=this._cachedMeta,i=this.getDataset(),s=(t,e,i,s)=>"x"===t?e:"r"===t?s:i,n=e.xAxisID=K(i.xAxisID,ws(t,"x")),o=e.yAxisID=K(i.yAxisID,ws(t,"y")),a=e.rAxisID=K(i.rAxisID,ws(t,"r")),r=e.indexAxis,l=e.iAxisID=s(r,n,o,a),h=e.vAxisID=s(r,o,n,a);e.xScale=this.getScaleForId(n),e.yScale=this.getScaleForId(o),e.rScale=this.getScaleForId(a),e.iScale=this.getScaleForId(l),e.vScale=this.getScaleForId(h)}getDataset(){return this.chart.data.datasets[this.index]}getMeta(){return this.chart.getDatasetMeta(this.index)}getScaleForId(t){return this.chart.scales[t]}_getOtherScale(t){const e=this._cachedMeta;return t===e.iScale?e.vScale:e.iScale}reset(){this._update("reset")}_destroy(){const t=this._cachedMeta;this._data&&ue(this._data,this),t._stacked&&Ms(t)}_dataCheck(){const t=this.getDataset(),e=t.data||(t.data=[]),i=this._data;if(U(e))this._data=function(t){const e=Object.keys(t),i=new Array(e.length);let s,n,o;for(s=0,n=e.length;s0&&i._parsed[t-1];if(!1===this._parsing)i._parsed=s,i._sorted=!0,h=s;else{h=Y(s[t])?this.parseArrayData(i,s,t,e):U(s[t])?this.parseObjectData(i,s,t,e):this.parsePrimitiveData(i,s,t,e);const n=()=>null===l[a]||d&&l[a]t&&!e.hidden&&e._stacked&&{keys:ms(i,!0),values:null})(e,i,this.chart),l={min:Number.POSITIVE_INFINITY,max:Number.NEGATIVE_INFINITY},{min:h,max:c}=function(t){const{min:e,max:i,minDefined:s,maxDefined:n}=t.getUserBounds();return{min:s?e:Number.NEGATIVE_INFINITY,max:n?i:Number.POSITIVE_INFINITY}}(a);let d,u;function f(){u=s[d];const e=u[a.axis];return!X(u[t.axis])||h>e||c=0;--d)if(!f()){this.updateRangeFromParsed(l,t,u,r);break}return l}getAllParsedValues(t){const e=this._cachedMeta._parsed,i=[];let s,n,o;for(s=0,n=e.length;s=0&&tthis.getContext(i,s)),c);return f.$shared&&(f.$shared=r,n[o]=Object.freeze(Ss(f,r))),f}_resolveAnimations(t,e,i){const s=this.chart,n=this._cachedDataOpts,o=`animation-${e}`,a=n[o];if(a)return a;let r;if(!1!==s.options.animation){const s=this.chart.config,n=s.datasetAnimationScopeKeys(this._type,e),o=s.getOptionScopes(this.getDataset(),n);r=s.createResolver(o,this.getContext(t,i,e))}const l=new gs(s,r&&r.animations);return r&&r._cacheable&&(n[o]=Object.freeze(l)),l}getSharedOptions(t){if(t.$shared)return this._sharedOptions||(this._sharedOptions=Object.assign({},t))}includeOptions(t,e){return!e||ks(t)||this.chart._animationsDisabled}updateElement(t,e,i,s){ks(s)?Object.assign(t,i):this._resolveAnimations(e,s).update(t,i)}updateSharedOptions(t,e,i){t&&!ks(e)&&this._resolveAnimations(void 0,e).update(t,i)}_setStyle(t,e,i,s){t.active=s;const n=this.getStyle(e,s);this._resolveAnimations(e,i,s).update(t,{options:!s&&this.getSharedOptions(n)||n})}removeHoverStyle(t,e,i){this._setStyle(t,i,"active",!1)}setHoverStyle(t,e,i){this._setStyle(t,i,"active",!0)}_removeDatasetHoverStyle(){const t=this._cachedMeta.dataset;t&&this._setStyle(t,void 0,"active",!1)}_setDatasetHoverStyle(){const t=this._cachedMeta.dataset;t&&this._setStyle(t,void 0,"active",!0)}_resyncElements(t){const e=this._data,i=this._cachedMeta.data;for(const[t,e,i]of this._syncList)this[t](e,i);this._syncList=[];const s=i.length,n=e.length,o=Math.min(n,s);o&&this.parse(0,o),n>s?this._insertElements(s,n-s,t):n{for(t.length+=e,a=t.length-1;a>=o;a--)t[a]=t[a-e]};for(r(n),a=t;a{s[t]=i[t]&&i[t].active()?i[t]._to:this[t]})),s}}Ds.defaults={},Ds.defaultRoutes=void 0;const Cs={values:t=>Y(t)?t:""+t,numeric(t,e,i){if(0===t)return"0";const s=this.chart.options.locale;let n,o=t;if(i.length>1){const e=Math.max(Math.abs(i[0].value),Math.abs(i[i.length-1].value));(e<1e-4||e>1e15)&&(n="scientific"),o=function(t,e){let i=e.length>3?e[2].value-e[1].value:e[1].value-e[0].value;Math.abs(i)>=1&&t!==Math.floor(t)&&(i=t-Math.floor(t));return i}(t,i)}const a=Dt(Math.abs(o)),r=Math.max(Math.min(-1*Math.floor(a),20),0),l={notation:n,minimumFractionDigits:r,maximumFractionDigits:r};return Object.assign(l,this.options.ticks.format),Ri(t,s,l)},logarithmic(t,e,i){if(0===t)return"0";const s=t/Math.pow(10,Math.floor(Dt(t)));return 1===s||2===s||5===s?Cs.numeric.call(this,t,e,i):""}};var Os={formatters:Cs};function As(t,e){const i=t.options.ticks,s=i.maxTicksLimit||function(t){const e=t.options.offset,i=t._tickSize(),s=t._length/i+(e?0:1),n=t._maxLength/i;return Math.floor(Math.min(s,n))}(t),n=i.major.enabled?function(t){const e=[];let i,s;for(i=0,s=t.length;is)return function(t,e,i,s){let n,o=0,a=i[0];for(s=Math.ceil(s),n=0;nn)return e}return Math.max(n,1)}(n,e,s);if(o>0){let t,i;const s=o>1?Math.round((r-a)/(o-1)):null;for(Ts(e,l,h,$(s)?0:a-s,a),t=0,i=o-1;te.lineWidth,tickColor:(t,e)=>e.color,offset:!1,borderDash:[],borderDashOffset:0,borderWidth:1},title:{display:!1,text:"",padding:{top:4,bottom:4}},ticks:{minRotation:0,maxRotation:50,mirror:!1,textStrokeWidth:0,textStrokeColor:"",padding:3,display:!0,autoSkip:!0,autoSkipPadding:3,labelOffset:0,callback:Os.formatters.values,minor:{},major:{},align:"center",crossAlign:"near",showLabelBackdrop:!1,backdropColor:"rgba(255, 255, 255, 0.75)",backdropPadding:2}}),bt.route("scale.ticks","color","","color"),bt.route("scale.grid","color","","borderColor"),bt.route("scale.grid","borderColor","","borderColor"),bt.route("scale.title","color","","color"),bt.describe("scale",{_fallback:!1,_scriptable:t=>!t.startsWith("before")&&!t.startsWith("after")&&"callback"!==t&&"parser"!==t,_indexable:t=>"borderDash"!==t&&"tickBorderDash"!==t}),bt.describe("scales",{_fallback:"scale"}),bt.describe("scale.ticks",{_scriptable:t=>"backdropPadding"!==t&&"callback"!==t,_indexable:t=>"backdropPadding"!==t});const Ls=(t,e,i)=>"top"===e||"left"===e?t[e]+i:t[e]-i;function Rs(t,e){const i=[],s=t.length/e,n=t.length;let o=0;for(;oa+r)))return h}function Is(t){return t.drawTicks?t.tickLength:0}function zs(t,e){if(!t.display)return 0;const i=He(t.font,e),s=Ne(t.padding);return(Y(t.text)?t.text.length:1)*i.lineHeight+s.height}function Fs(t,e,i){let n=s(t);return(i&&"right"!==e||!i&&"right"===e)&&(n=(t=>"left"===t?"right":"right"===t?"left":t)(n)),n}class Bs extends Ds{constructor(t){super(),this.id=t.id,this.type=t.type,this.options=void 0,this.ctx=t.ctx,this.chart=t.chart,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.width=void 0,this.height=void 0,this._margins={left:0,right:0,top:0,bottom:0},this.maxWidth=void 0,this.maxHeight=void 0,this.paddingTop=void 0,this.paddingBottom=void 0,this.paddingLeft=void 0,this.paddingRight=void 0,this.axis=void 0,this.labelRotation=void 0,this.min=void 0,this.max=void 0,this._range=void 0,this.ticks=[],this._gridLineItems=null,this._labelItems=null,this._labelSizes=null,this._length=0,this._maxLength=0,this._longestTextCache={},this._startPixel=void 0,this._endPixel=void 0,this._reversePixels=!1,this._userMax=void 0,this._userMin=void 0,this._suggestedMax=void 0,this._suggestedMin=void 0,this._ticksLength=0,this._borderValue=0,this._cache={},this._dataLimitsCached=!1,this.$context=void 0}init(t){this.options=t.setContext(this.getContext()),this.axis=t.axis,this._userMin=this.parse(t.min),this._userMax=this.parse(t.max),this._suggestedMin=this.parse(t.suggestedMin),this._suggestedMax=this.parse(t.suggestedMax)}parse(t,e){return t}getUserBounds(){let{_userMin:t,_userMax:e,_suggestedMin:i,_suggestedMax:s}=this;return t=q(t,Number.POSITIVE_INFINITY),e=q(e,Number.NEGATIVE_INFINITY),i=q(i,Number.POSITIVE_INFINITY),s=q(s,Number.NEGATIVE_INFINITY),{min:q(t,i),max:q(e,s),minDefined:X(t),maxDefined:X(e)}}getMinMax(t){let e,{min:i,max:s,minDefined:n,maxDefined:o}=this.getUserBounds();if(n&&o)return{min:i,max:s};const a=this.getMatchingVisibleMetas();for(let r=0,l=a.length;rs?s:i,s=n&&i>s?i:s,{min:q(i,q(s,i)),max:q(s,q(i,s))}}getPadding(){return{left:this.paddingLeft||0,top:this.paddingTop||0,right:this.paddingRight||0,bottom:this.paddingBottom||0}}getTicks(){return this.ticks}getLabels(){const t=this.chart.data;return this.options.labels||(this.isHorizontal()?t.xLabels:t.yLabels)||t.labels||[]}beforeLayout(){this._cache={},this._dataLimitsCached=!1}beforeUpdate(){J(this.options.beforeUpdate,[this])}update(t,e,i){const{beginAtZero:s,grace:n,ticks:o}=this.options,a=o.sampleSize;this.beforeUpdate(),this.maxWidth=t,this.maxHeight=e,this._margins=i=Object.assign({left:0,right:0,top:0,bottom:0},i),this.ticks=null,this._labelSizes=null,this._gridLineItems=null,this._labelItems=null,this.beforeSetDimensions(),this.setDimensions(),this.afterSetDimensions(),this._maxLength=this.isHorizontal()?this.width+i.left+i.right:this.height+i.top+i.bottom,this._dataLimitsCached||(this.beforeDataLimits(),this.determineDataLimits(),this.afterDataLimits(),this._range=$e(this,n,s),this._dataLimitsCached=!0),this.beforeBuildTicks(),this.ticks=this.buildTicks()||[],this.afterBuildTicks();const r=a=n||i<=1||!this.isHorizontal())return void(this.labelRotation=s);const h=this._getLabelSizes(),c=h.widest.width,d=h.highest.height,u=jt(this.chart.width-c,0,this.maxWidth);o=t.offset?this.maxWidth/i:u/(i-1),c+6>o&&(o=u/(i-(t.offset?.5:1)),a=this.maxHeight-Is(t.grid)-e.padding-zs(t.title,this.chart.options.font),r=Math.sqrt(c*c+d*d),l=zt(Math.min(Math.asin(jt((h.highest.height+6)/o,-1,1)),Math.asin(jt(a/r,-1,1))-Math.asin(jt(d/r,-1,1)))),l=Math.max(s,Math.min(n,l))),this.labelRotation=l}afterCalculateLabelRotation(){J(this.options.afterCalculateLabelRotation,[this])}beforeFit(){J(this.options.beforeFit,[this])}fit(){const t={width:0,height:0},{chart:e,options:{ticks:i,title:s,grid:n}}=this,o=this._isVisible(),a=this.isHorizontal();if(o){const o=zs(s,e.options.font);if(a?(t.width=this.maxWidth,t.height=Is(n)+o):(t.height=this.maxHeight,t.width=Is(n)+o),i.display&&this.ticks.length){const{first:e,last:s,widest:n,highest:o}=this._getLabelSizes(),r=2*i.padding,l=It(this.labelRotation),h=Math.cos(l),c=Math.sin(l);if(a){const e=i.mirror?0:c*n.width+h*o.height;t.height=Math.min(this.maxHeight,t.height+e+r)}else{const e=i.mirror?0:h*n.width+c*o.height;t.width=Math.min(this.maxWidth,t.width+e+r)}this._calculatePadding(e,s,c,h)}}this._handleMargins(),a?(this.width=this._length=e.width-this._margins.left-this._margins.right,this.height=t.height):(this.width=t.width,this.height=this._length=e.height-this._margins.top-this._margins.bottom)}_calculatePadding(t,e,i,s){const{ticks:{align:n,padding:o},position:a}=this.options,r=0!==this.labelRotation,l="top"!==a&&"x"===this.axis;if(this.isHorizontal()){const a=this.getPixelForTick(0)-this.left,h=this.right-this.getPixelForTick(this.ticks.length-1);let c=0,d=0;r?l?(c=s*t.width,d=i*e.height):(c=i*t.height,d=s*e.width):"start"===n?d=e.width:"end"===n?c=t.width:(c=t.width/2,d=e.width/2),this.paddingLeft=Math.max((c-a+o)*this.width/(this.width-a),0),this.paddingRight=Math.max((d-h+o)*this.width/(this.width-h),0)}else{let i=e.height/2,s=t.height/2;"start"===n?(i=0,s=t.height):"end"===n&&(i=e.height,s=0),this.paddingTop=i+o,this.paddingBottom=s+o}}_handleMargins(){this._margins&&(this._margins.left=Math.max(this.paddingLeft,this._margins.left),this._margins.top=Math.max(this.paddingTop,this._margins.top),this._margins.right=Math.max(this.paddingRight,this._margins.right),this._margins.bottom=Math.max(this.paddingBottom,this._margins.bottom))}afterFit(){J(this.options.afterFit,[this])}isHorizontal(){const{axis:t,position:e}=this.options;return"top"===e||"bottom"===e||"x"===t}isFullSize(){return this.options.fullSize}_convertTicksToLabels(t){let e,i;for(this.beforeTickToLabelConversion(),this.generateTickLabels(t),e=0,i=t.length;e{const i=t.gc,s=i.length/2;let n;if(s>e){for(n=0;n({width:n[t]||0,height:o[t]||0});return{first:v(0),last:v(e-1),widest:v(_),highest:v(y),widths:n,heights:o}}getLabelForValue(t){return t}getPixelForValue(t,e){return NaN}getValueForPixel(t){}getPixelForTick(t){const e=this.ticks;return t<0||t>e.length-1?null:this.getPixelForValue(e[t].value)}getPixelForDecimal(t){this._reversePixels&&(t=1-t);const e=this._startPixel+t*this._length;return $t(this._alignToPixels?Kt(this.chart,e,0):e)}getDecimalForPixel(t){const e=(t-this._startPixel)/this._length;return this._reversePixels?1-e:e}getBasePixel(){return this.getPixelForValue(this.getBaseValue())}getBaseValue(){const{min:t,max:e}=this;return t<0&&e<0?e:t>0&&e>0?t:0}getContext(t){const e=this.ticks||[];if(t>=0&&ta*s?a/i:r/s:r*s0}_computeGridLineItems(t){const e=this.axis,i=this.chart,s=this.options,{grid:n,position:o}=s,a=n.offset,r=this.isHorizontal(),l=this.ticks.length+(a?1:0),h=Is(n),c=[],d=n.setContext(this.getContext()),u=d.drawBorder?d.borderWidth:0,f=u/2,g=function(t){return Kt(i,t,u)};let p,m,x,b,_,y,v,w,M,k,S,P;if("top"===o)p=g(this.bottom),y=this.bottom-h,w=p-f,k=g(t.top)+f,P=t.bottom;else if("bottom"===o)p=g(this.top),k=t.top,P=g(t.bottom)-f,y=p+f,w=this.top+h;else if("left"===o)p=g(this.right),_=this.right-h,v=p-f,M=g(t.left)+f,S=t.right;else if("right"===o)p=g(this.left),M=t.left,S=g(t.right)-f,_=p+f,v=this.left+h;else if("x"===e){if("center"===o)p=g((t.top+t.bottom)/2+.5);else if(U(o)){const t=Object.keys(o)[0],e=o[t];p=g(this.chart.scales[t].getPixelForValue(e))}k=t.top,P=t.bottom,y=p+f,w=y+h}else if("y"===e){if("center"===o)p=g((t.left+t.right)/2);else if(U(o)){const t=Object.keys(o)[0],e=o[t];p=g(this.chart.scales[t].getPixelForValue(e))}_=p-f,v=_-h,M=t.left,S=t.right}const D=K(s.ticks.maxTicksLimit,l),C=Math.max(1,Math.ceil(l/D));for(m=0;me.value===t));if(i>=0){return e.setContext(this.getContext(i)).lineWidth}return 0}drawGrid(t){const e=this.options.grid,i=this.ctx,s=this._gridLineItems||(this._gridLineItems=this._computeGridLineItems(t));let n,o;const a=(t,e,s)=>{s.width&&s.color&&(i.save(),i.lineWidth=s.width,i.strokeStyle=s.color,i.setLineDash(s.borderDash||[]),i.lineDashOffset=s.borderDashOffset,i.beginPath(),i.moveTo(t.x,t.y),i.lineTo(e.x,e.y),i.stroke(),i.restore())};if(e.display)for(n=0,o=s.length;n{this.drawBackground(),this.drawGrid(t),this.drawTitle()}},{z:i+1,draw:()=>{this.drawBorder()}},{z:e,draw:t=>{this.drawLabels(t)}}]:[{z:e,draw:t=>{this.draw(t)}}]}getMatchingVisibleMetas(t){const e=this.chart.getSortedVisibleDatasetMetas(),i=this.axis+"AxisID",s=[];let n,o;for(n=0,o=e.length;n{const s=i.split("."),n=s.pop(),o=[t].concat(s).join("."),a=e[i].split("."),r=a.pop(),l=a.join(".");bt.route(o,n,l,r)}))}(e,t.defaultRoutes);t.descriptors&&bt.describe(e,t.descriptors)}(t,o,i),this.override&&bt.override(t.id,t.overrides)),o}get(t){return this.items[t]}unregister(t){const e=this.items,i=t.id,s=this.scope;i in e&&delete e[i],s&&i in bt[s]&&(delete bt[s][i],this.override&&delete gt[i])}}var Ws=new class{constructor(){this.controllers=new Vs(Ps,"datasets",!0),this.elements=new Vs(Ds,"elements"),this.plugins=new Vs(Object,"plugins"),this.scales=new Vs(Bs,"scales"),this._typedRegistries=[this.controllers,this.scales,this.elements]}add(...t){this._each("register",t)}remove(...t){this._each("unregister",t)}addControllers(...t){this._each("register",t,this.controllers)}addElements(...t){this._each("register",t,this.elements)}addPlugins(...t){this._each("register",t,this.plugins)}addScales(...t){this._each("register",t,this.scales)}getController(t){return this._get(t,this.controllers,"controller")}getElement(t){return this._get(t,this.elements,"element")}getPlugin(t){return this._get(t,this.plugins,"plugin")}getScale(t){return this._get(t,this.scales,"scale")}removeControllers(...t){this._each("unregister",t,this.controllers)}removeElements(...t){this._each("unregister",t,this.elements)}removePlugins(...t){this._each("unregister",t,this.plugins)}removeScales(...t){this._each("unregister",t,this.scales)}_each(t,e,i){[...e].forEach((e=>{const s=i||this._getRegistryForType(e);i||s.isForType(e)||s===this.plugins&&e.id?this._exec(t,s,e):Q(e,(e=>{const s=i||this._getRegistryForType(e);this._exec(t,s,e)}))}))}_exec(t,e,i){const s=ht(t);J(i["before"+s],[],i),e[t](i),J(i["after"+s],[],i)}_getRegistryForType(t){for(let e=0;et.filter((t=>!e.some((e=>t.plugin.id===e.plugin.id))));this._notify(s(e,i),t,"stop"),this._notify(s(i,e),t,"start")}}function Hs(t,e){return e||!1!==t?!0===t?{}:t:null}function js(t,e,i,s){const n=t.pluginScopeKeys(e),o=t.getOptionScopes(i,n);return t.createResolver(o,s,[""],{scriptable:!1,indexable:!1,allKeys:!0})}function $s(t,e){const i=bt.datasets[t]||{};return((e.datasets||{})[t]||{}).indexAxis||e.indexAxis||i.indexAxis||"x"}function Ys(t,e){return"x"===t||"y"===t?t:e.axis||("top"===(i=e.position)||"bottom"===i?"x":"left"===i||"right"===i?"y":void 0)||t.charAt(0).toLowerCase();var i}function Us(t){const e=t.options||(t.options={});e.plugins=K(e.plugins,{}),e.scales=function(t,e){const i=gt[t.type]||{scales:{}},s=e.scales||{},n=$s(t.type,e),o=Object.create(null),a=Object.create(null);return Object.keys(s).forEach((t=>{const e=s[t];if(!U(e))return console.error(`Invalid scale configuration for scale: ${t}`);if(e._proxy)return console.warn(`Ignoring resolver passed as options for scale: ${t}`);const r=Ys(t,e),l=function(t,e){return t===e?"_index_":"_value_"}(r,n),h=i.scales||{};o[r]=o[r]||t,a[t]=ot(Object.create(null),[{axis:r},e,h[r],h[l]])})),t.data.datasets.forEach((i=>{const n=i.type||t.type,r=i.indexAxis||$s(n,e),l=(gt[n]||{}).scales||{};Object.keys(l).forEach((t=>{const e=function(t,e){let i=t;return"_index_"===t?i=e:"_value_"===t&&(i="x"===e?"y":"x"),i}(t,r),n=i[e+"AxisID"]||o[e]||e;a[n]=a[n]||Object.create(null),ot(a[n],[{axis:e},s[n],l[t]])}))})),Object.keys(a).forEach((t=>{const e=a[t];ot(e,[bt.scales[e.type],bt.scale])})),a}(t,e)}function Xs(t){return(t=t||{}).datasets=t.datasets||[],t.labels=t.labels||[],t}const qs=new Map,Ks=new Set;function Gs(t,e){let i=qs.get(t);return i||(i=e(),qs.set(t,i),Ks.add(i)),i}const Zs=(t,e,i)=>{const s=lt(e,i);void 0!==s&&t.add(s)};class Js{constructor(t){this._config=function(t){return(t=t||{}).data=Xs(t.data),Us(t),t}(t),this._scopeCache=new Map,this._resolverCache=new Map}get platform(){return this._config.platform}get type(){return this._config.type}set type(t){this._config.type=t}get data(){return this._config.data}set data(t){this._config.data=Xs(t)}get options(){return this._config.options}set options(t){this._config.options=t}get plugins(){return this._config.plugins}update(){const t=this._config;this.clearCache(),Us(t)}clearCache(){this._scopeCache.clear(),this._resolverCache.clear()}datasetScopeKeys(t){return Gs(t,(()=>[[`datasets.${t}`,""]]))}datasetAnimationScopeKeys(t,e){return Gs(`${t}.transition.${e}`,(()=>[[`datasets.${t}.transitions.${e}`,`transitions.${e}`],[`datasets.${t}`,""]]))}datasetElementScopeKeys(t,e){return Gs(`${t}-${e}`,(()=>[[`datasets.${t}.elements.${e}`,`datasets.${t}`,`elements.${e}`,""]]))}pluginScopeKeys(t){const e=t.id;return Gs(`${this.type}-plugin-${e}`,(()=>[[`plugins.${e}`,...t.additionalOptionScopes||[]]]))}_cachedScopes(t,e){const i=this._scopeCache;let s=i.get(t);return s&&!e||(s=new Map,i.set(t,s)),s}getOptionScopes(t,e,i){const{options:s,type:n}=this,o=this._cachedScopes(t,i),a=o.get(e);if(a)return a;const r=new Set;e.forEach((e=>{t&&(r.add(t),e.forEach((e=>Zs(r,t,e)))),e.forEach((t=>Zs(r,s,t))),e.forEach((t=>Zs(r,gt[n]||{},t))),e.forEach((t=>Zs(r,bt,t))),e.forEach((t=>Zs(r,pt,t)))}));const l=Array.from(r);return 0===l.length&&l.push(Object.create(null)),Ks.has(e)&&o.set(e,l),l}chartOptionScopes(){const{options:t,type:e}=this;return[t,gt[e]||{},bt.datasets[e]||{},{type:e},bt,pt]}resolveNamedOptions(t,e,i,s=[""]){const n={$shared:!0},{resolver:o,subPrefixes:a}=Qs(this._resolverCache,t,s);let r=o;if(function(t,e){const{isScriptable:i,isIndexable:s}=ri(t);for(const n of e){const e=i(n),o=s(n),a=(o||e)&&t[n];if(e&&(dt(a)||tn(a))||o&&Y(a))return!0}return!1}(o,e)){n.$shared=!1;r=ai(o,i=dt(i)?i():i,this.createResolver(t,i,a))}for(const t of e)n[t]=r[t];return n}createResolver(t,e,i=[""],s){const{resolver:n}=Qs(this._resolverCache,t,i);return U(e)?ai(n,e,void 0,s):n}}function Qs(t,e,i){let s=t.get(e);s||(s=new Map,t.set(e,s));const n=i.join();let o=s.get(n);if(!o){o={resolver:oi(e,i),subPrefixes:i.filter((t=>!t.toLowerCase().includes("hover")))},s.set(n,o)}return o}const tn=t=>U(t)&&Object.getOwnPropertyNames(t).reduce(((e,i)=>e||dt(t[i])),!1);const en=["top","bottom","left","right","chartArea"];function sn(t,e){return"top"===t||"bottom"===t||-1===en.indexOf(t)&&"x"===e}function nn(t,e){return function(i,s){return i[t]===s[t]?i[e]-s[e]:i[t]-s[t]}}function on(t){const e=t.chart,i=e.options.animation;e.notifyPlugins("afterRender"),J(i&&i.onComplete,[t],e)}function an(t){const e=t.chart,i=e.options.animation;J(i&&i.onProgress,[t],e)}function rn(t){return ge()&&"string"==typeof t?t=document.getElementById(t):t&&t.length&&(t=t[0]),t&&t.canvas&&(t=t.canvas),t}const ln={},hn=t=>{const e=rn(t);return Object.values(ln).filter((t=>t.canvas===e)).pop()};function cn(t,e,i){const s=Object.keys(t);for(const n of s){const s=+n;if(s>=e){const o=t[n];delete t[n],(i>0||s>e)&&(t[s+i]=o)}}}class dn{constructor(t,e){const s=this.config=new Js(e),n=rn(t),o=hn(n);if(o)throw new Error("Canvas is already in use. Chart with ID '"+o.id+"' must be destroyed before the canvas can be reused.");const r=s.createResolver(s.chartOptionScopes(),this.getContext());this.platform=new(s.platform||ls(n)),this.platform.updateConfig(s);const l=this.platform.acquireContext(n,r.aspectRatio),h=l&&l.canvas,c=h&&h.height,d=h&&h.width;this.id=j(),this.ctx=l,this.canvas=h,this.width=d,this.height=c,this._options=r,this._aspectRatio=this.aspectRatio,this._layers=[],this._metasets=[],this._stacks=void 0,this.boxes=[],this.currentDevicePixelRatio=void 0,this.chartArea=void 0,this._active=[],this._lastEvent=void 0,this._listeners={},this._responsiveListeners=void 0,this._sortedMetasets=[],this.scales={},this._plugins=new Ns,this.$proxies={},this._hiddenIndices={},this.attached=!1,this._animationsDisabled=void 0,this.$context=void 0,this._doResize=i((t=>this.update(t)),r.resizeDelay||0),this._dataChanges=[],ln[this.id]=this,l&&h?(a.listen(this,"complete",on),a.listen(this,"progress",an),this._initialize(),this.attached&&this.update()):console.error("Failed to create chart: can't acquire context from the given item")}get aspectRatio(){const{options:{aspectRatio:t,maintainAspectRatio:e},width:i,height:s,_aspectRatio:n}=this;return $(t)?e&&n?n:s?i/s:null:t}get data(){return this.config.data}set data(t){this.config.data=t}get options(){return this._options}set options(t){this.config.options=t}_initialize(){return this.notifyPlugins("beforeInit"),this.options.responsive?this.resize():ke(this,this.options.devicePixelRatio),this.bindEvents(),this.notifyPlugins("afterInit"),this}clear(){return Gt(this.canvas,this.ctx),this}stop(){return a.stop(this),this}resize(t,e){a.running(this)?this._resizeBeforeDraw={width:t,height:e}:this._resize(t,e)}_resize(t,e){const i=this.options,s=this.canvas,n=i.maintainAspectRatio&&this.aspectRatio,o=this.platform.getMaximumSize(s,t,e,n),a=i.devicePixelRatio||this.platform.getDevicePixelRatio(),r=this.width?"resize":"attach";this.width=o.width,this.height=o.height,this._aspectRatio=this.aspectRatio,ke(this,a,!0)&&(this.notifyPlugins("resize",{size:o}),J(i.onResize,[this,o],this),this.attached&&this._doResize(r)&&this.render())}ensureScalesHaveIDs(){Q(this.options.scales||{},((t,e)=>{t.id=e}))}buildOrUpdateScales(){const t=this.options,e=t.scales,i=this.scales,s=Object.keys(i).reduce(((t,e)=>(t[e]=!1,t)),{});let n=[];e&&(n=n.concat(Object.keys(e).map((t=>{const i=e[t],s=Ys(t,i),n="r"===s,o="x"===s;return{options:i,dposition:n?"chartArea":o?"bottom":"left",dtype:n?"radialLinear":o?"category":"linear"}})))),Q(n,(e=>{const n=e.options,o=n.id,a=Ys(o,n),r=K(n.type,e.dtype);void 0!==n.position&&sn(n.position,a)===sn(e.dposition)||(n.position=e.dposition),s[o]=!0;let l=null;if(o in i&&i[o].type===r)l=i[o];else{l=new(Ws.getScale(r))({id:o,type:r,ctx:this.ctx,chart:this}),i[l.id]=l}l.init(n,t)})),Q(s,((t,e)=>{t||delete i[e]})),Q(i,(t=>{ni.configure(this,t,t.options),ni.addBox(this,t)}))}_updateMetasets(){const t=this._metasets,e=this.data.datasets.length,i=t.length;if(t.sort(((t,e)=>t.index-e.index)),i>e){for(let t=e;te.length&&delete this._stacks,t.forEach(((t,i)=>{0===e.filter((e=>e===t._dataset)).length&&this._destroyDatasetMeta(i)}))}buildOrUpdateControllers(){const t=[],e=this.data.datasets;let i,s;for(this._removeUnreferencedMetasets(),i=0,s=e.length;i{this.getDatasetMeta(e).controller.reset()}),this)}reset(){this._resetElements(),this.notifyPlugins("reset")}update(t){const e=this.config;e.update();const i=this._options=e.createResolver(e.chartOptionScopes(),this.getContext()),s=this._animationsDisabled=!i.animation;if(this._updateScales(),this._checkEventBindings(),this._updateHiddenIndices(),this._plugins.invalidate(),!1===this.notifyPlugins("beforeUpdate",{mode:t,cancelable:!0}))return;const n=this.buildOrUpdateControllers();this.notifyPlugins("beforeElementsUpdate");let o=0;for(let t=0,e=this.data.datasets.length;t{t.reset()})),this._updateDatasets(t),this.notifyPlugins("afterUpdate",{mode:t}),this._layers.sort(nn("z","_idx"));const{_active:a,_lastEvent:r}=this;r?this._eventHandler(r,!0):a.length&&this._updateHoverStyles(a,a,!0),this.render()}_updateScales(){Q(this.scales,(t=>{ni.removeBox(this,t)})),this.ensureScalesHaveIDs(),this.buildOrUpdateScales()}_checkEventBindings(){const t=this.options,e=new Set(Object.keys(this._listeners)),i=new Set(t.events);ut(e,i)&&!!this._responsiveListeners===t.responsive||(this.unbindEvents(),this.bindEvents())}_updateHiddenIndices(){const{_hiddenIndices:t}=this,e=this._getUniformDataChanges()||[];for(const{method:i,start:s,count:n}of e){cn(t,s,"_removeElements"===i?-n:n)}}_getUniformDataChanges(){const t=this._dataChanges;if(!t||!t.length)return;this._dataChanges=[];const e=this.data.datasets.length,i=e=>new Set(t.filter((t=>t[0]===e)).map(((t,e)=>e+","+t.splice(1).join(",")))),s=i(0);for(let t=1;tt.split(","))).map((t=>({method:t[1],start:+t[2],count:+t[3]})))}_updateLayout(t){if(!1===this.notifyPlugins("beforeLayout",{cancelable:!0}))return;ni.update(this,this.width,this.height,t);const e=this.chartArea,i=e.width<=0||e.height<=0;this._layers=[],Q(this.boxes,(t=>{i&&"chartArea"===t.position||(t.configure&&t.configure(),this._layers.push(...t._layers()))}),this),this._layers.forEach(((t,e)=>{t._idx=e})),this.notifyPlugins("afterLayout")}_updateDatasets(t){if(!1!==this.notifyPlugins("beforeDatasetsUpdate",{mode:t,cancelable:!0})){for(let t=0,e=this.data.datasets.length;t=0;--e)this._drawDataset(t[e]);this.notifyPlugins("afterDatasetsDraw")}_drawDataset(t){const e=this.ctx,i=t._clip,s=!i.disabled,n=this.chartArea,o={meta:t,index:t.index,cancelable:!0};!1!==this.notifyPlugins("beforeDatasetDraw",o)&&(s&&Qt(e,{left:!1===i.left?0:n.left-i.left,right:!1===i.right?this.width:n.right+i.right,top:!1===i.top?0:n.top-i.top,bottom:!1===i.bottom?this.height:n.bottom+i.bottom}),t.controller.draw(),s&&te(e),o.cancelable=!1,this.notifyPlugins("afterDatasetDraw",o))}getElementsAtEventForMode(t,e,i,s){const n=Ee.modes[e];return"function"==typeof n?n(this,t,i,s):[]}getDatasetMeta(t){const e=this.data.datasets[t],i=this._metasets;let s=i.filter((t=>t&&t._dataset===e)).pop();return s||(s={type:null,data:[],dataset:null,controller:null,hidden:null,xAxisID:null,yAxisID:null,order:e&&e.order||0,index:t,_dataset:e,_parsed:[],_sorted:!1},i.push(s)),s}getContext(){return this.$context||(this.$context=Ye(null,{chart:this,type:"chart"}))}getVisibleDatasetCount(){return this.getSortedVisibleDatasetMetas().length}isDatasetVisible(t){const e=this.data.datasets[t];if(!e)return!1;const i=this.getDatasetMeta(t);return"boolean"==typeof i.hidden?!i.hidden:!e.hidden}setDatasetVisibility(t,e){this.getDatasetMeta(t).hidden=!e}toggleDataVisibility(t){this._hiddenIndices[t]=!this._hiddenIndices[t]}getDataVisibility(t){return!this._hiddenIndices[t]}_updateVisibility(t,e,i){const s=i?"show":"hide",n=this.getDatasetMeta(t),o=n.controller._resolveAnimations(void 0,s);ct(e)?(n.data[e].hidden=!i,this.update()):(this.setDatasetVisibility(t,i),o.update(n,{visible:i}),this.update((e=>e.datasetIndex===t?s:void 0)))}hide(t,e){this._updateVisibility(t,e,!1)}show(t,e){this._updateVisibility(t,e,!0)}_destroyDatasetMeta(t){const e=this._metasets[t];e&&e.controller&&e.controller._destroy(),delete this._metasets[t]}_stop(){let t,e;for(this.stop(),a.remove(this),t=0,e=this.data.datasets.length;t{e.addEventListener(this,i,s),t[i]=s},s=(t,e,i)=>{t.offsetX=e,t.offsetY=i,this._eventHandler(t)};Q(this.options.events,(t=>i(t,s)))}bindResponsiveEvents(){this._responsiveListeners||(this._responsiveListeners={});const t=this._responsiveListeners,e=this.platform,i=(i,s)=>{e.addEventListener(this,i,s),t[i]=s},s=(i,s)=>{t[i]&&(e.removeEventListener(this,i,s),delete t[i])},n=(t,e)=>{this.canvas&&this.resize(t,e)};let o;const a=()=>{s("attach",a),this.attached=!0,this.resize(),i("resize",n),i("detach",o)};o=()=>{this.attached=!1,s("resize",n),this._stop(),this._resize(0,0),i("attach",a)},e.isAttached(this.canvas)?a():o()}unbindEvents(){Q(this._listeners,((t,e)=>{this.platform.removeEventListener(this,e,t)})),this._listeners={},Q(this._responsiveListeners,((t,e)=>{this.platform.removeEventListener(this,e,t)})),this._responsiveListeners=void 0}updateHoverStyle(t,e,i){const s=i?"set":"remove";let n,o,a,r;for("dataset"===e&&(n=this.getDatasetMeta(t[0].datasetIndex),n.controller["_"+s+"DatasetHoverStyle"]()),a=0,r=t.length;a{const i=this.getDatasetMeta(t);if(!i)throw new Error("No dataset found at index "+t);return{datasetIndex:t,element:i.data[e],index:e}}));!tt(i,e)&&(this._active=i,this._lastEvent=null,this._updateHoverStyles(i,e))}notifyPlugins(t,e,i){return this._plugins.notify(this,t,e,i)}_updateHoverStyles(t,e,i){const s=this.options.hover,n=(t,e)=>t.filter((t=>!e.some((e=>t.datasetIndex===e.datasetIndex&&t.index===e.index)))),o=n(e,t),a=i?t:n(t,e);o.length&&this.updateHoverStyle(o,s.mode,!1),a.length&&s.mode&&this.updateHoverStyle(a,s.mode,!0)}_eventHandler(t,e){const i={event:t,replay:e,cancelable:!0,inChartArea:Jt(t,this.chartArea,this._minPadding)},s=e=>(e.options.events||this.options.events).includes(t.native.type);if(!1===this.notifyPlugins("beforeEvent",i,s))return;const n=this._handleEvent(t,e,i.inChartArea);return i.cancelable=!1,this.notifyPlugins("afterEvent",i,s),(n||i.changed)&&this.render(),this}_handleEvent(t,e,i){const{_active:s=[],options:n}=this,o=e,a=this._getActiveElements(t,s,i,o),r=ft(t),l=function(t,e,i,s){return i&&"mouseout"!==t.type?s?e:t:null}(t,this._lastEvent,i,r);i&&(this._lastEvent=null,J(n.onHover,[t,a,this],this),r&&J(n.onClick,[t,a,this],this));const h=!tt(a,s);return(h||e)&&(this._active=a,this._updateHoverStyles(a,s,e)),this._lastEvent=l,h}_getActiveElements(t,e,i,s){if("mouseout"===t.type)return[];if(!i)return e;const n=this.options.hover;return this.getElementsAtEventForMode(t,n.mode,n,s)}}const un=()=>Q(dn.instances,(t=>t._plugins.invalidate())),fn=!0;function gn(){throw new Error("This method is not implemented: Check that a complete date adapter is provided.")}Object.defineProperties(dn,{defaults:{enumerable:fn,value:bt},instances:{enumerable:fn,value:ln},overrides:{enumerable:fn,value:gt},registry:{enumerable:fn,value:Ws},version:{enumerable:fn,value:"3.7.1"},getChart:{enumerable:fn,value:hn},register:{enumerable:fn,value:(...t)=>{Ws.add(...t),un()}},unregister:{enumerable:fn,value:(...t)=>{Ws.remove(...t),un()}}});class pn{constructor(t){this.options=t||{}}formats(){return gn()}parse(t,e){return gn()}format(t,e){return gn()}add(t,e,i){return gn()}diff(t,e,i){return gn()}startOf(t,e,i){return gn()}endOf(t,e){return gn()}}pn.override=function(t){Object.assign(pn.prototype,t)};var mn={_date:pn};function xn(t){const e=t.iScale,i=function(t,e){if(!t._cache.$bar){const i=t.getMatchingVisibleMetas(e);let s=[];for(let e=0,n=i.length;et-e)))}return t._cache.$bar}(e,t.type);let s,n,o,a,r=e._length;const l=()=>{32767!==o&&-32768!==o&&(ct(a)&&(r=Math.min(r,Math.abs(o-a)||r)),a=o)};for(s=0,n=i.length;sMath.abs(r)&&(l=r,h=a),e[i.axis]=h,e._custom={barStart:l,barEnd:h,start:n,end:o,min:a,max:r}}(t,e,i,s):e[i.axis]=i.parse(t,s),e}function _n(t,e,i,s){const n=t.iScale,o=t.vScale,a=n.getLabels(),r=n===o,l=[];let h,c,d,u;for(h=i,c=i+s;ht.x,i="left",s="right"):(e=t.base=i?1:-1)}(c,e,o)*n,d===o&&(p-=c/2),h=p+c),p===e.getPixelForValue(o)){const t=Ct(c)*e.getLineWidthForValue(o)/2;p+=t,c-=t}return{size:c,base:p,head:h,center:h+c/2}}_calculateBarIndexPixels(t,e){const i=e.scale,s=this.options,n=s.skipNull,o=K(s.maxBarThickness,1/0);let a,r;if(e.grouped){const i=n?this._getStackCount(t):e.stackCount,l="flex"===s.barThickness?function(t,e,i,s){const n=e.pixels,o=n[t];let a=t>0?n[t-1]:null,r=t=0;--i)e=Math.max(e,t[i].size(this.resolveDataElementOptions(i))/2);return e>0&&e}getLabelAndValue(t){const e=this._cachedMeta,{xScale:i,yScale:s}=e,n=this.getParsed(t),o=i.getLabelForValue(n.x),a=s.getLabelForValue(n.y),r=n._custom;return{label:e.label,value:"("+o+", "+a+(r?", "+r:"")+")"}}update(t){const e=this._cachedMeta.data;this.updateElements(e,0,e.length,t)}updateElements(t,e,i,s){const n="reset"===s,{iScale:o,vScale:a}=this._cachedMeta,r=this.resolveDataElementOptions(e,s),l=this.getSharedOptions(r),h=this.includeOptions(s,l),c=o.axis,d=a.axis;for(let r=e;r""}}}};class Dn extends Ps{constructor(t,e){super(t,e),this.enableOptionSharing=!0,this.innerRadius=void 0,this.outerRadius=void 0,this.offsetX=void 0,this.offsetY=void 0}linkScales(){}parse(t,e){const i=this.getDataset().data,s=this._cachedMeta;if(!1===this._parsing)s._parsed=i;else{let n,o,a=t=>+i[t];if(U(i[t])){const{key:t="value"}=this._parsing;a=e=>+lt(i[e],t)}for(n=t,o=t+e;nHt(t,r,l,!0)?1:Math.max(e,e*i,s,s*i),g=(t,e,s)=>Ht(t,r,l,!0)?-1:Math.min(e,e*i,s,s*i),p=f(0,h,d),m=f(kt,c,u),x=g(_t,h,d),b=g(_t+kt,c,u);s=(p-x)/2,n=(m-b)/2,o=-(p+x)/2,a=-(m+b)/2}return{ratioX:s,ratioY:n,offsetX:o,offsetY:a}}(c,h,r),p=(i.width-o)/d,m=(i.height-o)/u,x=Math.max(Math.min(p,m)/2,0),b=Z(this.options.radius,x),_=(b-Math.max(b*r,0))/this._getVisibleDatasetWeightTotal();this.offsetX=f*b,this.offsetY=g*b,s.total=this.calculateTotal(),this.outerRadius=b-_*this._getRingWeightOffset(this.index),this.innerRadius=Math.max(this.outerRadius-_*l,0),this.updateElements(n,0,n.length,t)}_circumference(t,e){const i=this.options,s=this._cachedMeta,n=this._getCircumference();return e&&i.animation.animateRotate||!this.chart.getDataVisibility(t)||null===s._parsed[t]||s.data[t].hidden?0:this.calculateCircumference(s._parsed[t]*n/yt)}updateElements(t,e,i,s){const n="reset"===s,o=this.chart,a=o.chartArea,r=o.options.animation,l=(a.left+a.right)/2,h=(a.top+a.bottom)/2,c=n&&r.animateScale,d=c?0:this.innerRadius,u=c?0:this.outerRadius,f=this.resolveDataElementOptions(e,s),g=this.getSharedOptions(f),p=this.includeOptions(s,g);let m,x=this._getRotation();for(m=0;m0&&!isNaN(t)?yt*(Math.abs(t)/e):0}getLabelAndValue(t){const e=this._cachedMeta,i=this.chart,s=i.data.labels||[],n=Ri(e._parsed[t],i.options.locale);return{label:s[t]||"",value:n}}getMaxBorderWidth(t){let e=0;const i=this.chart;let s,n,o,a,r;if(!t)for(s=0,n=i.data.datasets.length;s"spacing"!==t,_indexable:t=>"spacing"!==t},Dn.overrides={aspectRatio:1,plugins:{legend:{labels:{generateLabels(t){const e=t.data;if(e.labels.length&&e.datasets.length){const{labels:{pointStyle:i}}=t.legend.options;return e.labels.map(((e,s)=>{const n=t.getDatasetMeta(0).controller.getStyle(s);return{text:e,fillStyle:n.backgroundColor,strokeStyle:n.borderColor,lineWidth:n.borderWidth,pointStyle:i,hidden:!t.getDataVisibility(s),index:s}}))}return[]}},onClick(t,e,i){i.chart.toggleDataVisibility(e.index),i.chart.update()}},tooltip:{callbacks:{title:()=>"",label(t){let e=t.label;const i=": "+t.formattedValue;return Y(e)?(e=e.slice(),e[0]+=i):e+=i,e}}}}};class Cn extends Ps{initialize(){this.enableOptionSharing=!0,super.initialize()}update(t){const e=this._cachedMeta,{dataset:i,data:s=[],_dataset:n}=e,o=this.chart._animationsDisabled;let{start:a,count:r}=function(t,e,i){const s=e.length;let n=0,o=s;if(t._sorted){const{iScale:a,_parsed:r}=t,l=a.axis,{min:h,max:c,minDefined:d,maxDefined:u}=a.getUserBounds();d&&(n=jt(Math.min(re(r,a.axis,h).lo,i?s:re(e,l,a.getPixelForValue(h)).lo),0,s-1)),o=u?jt(Math.max(re(r,a.axis,c).hi+1,i?0:re(e,l,a.getPixelForValue(c)).hi+1),n,s)-n:s-n}return{start:n,count:o}}(e,s,o);this._drawStart=a,this._drawCount=r,function(t){const{xScale:e,yScale:i,_scaleRanges:s}=t,n={xmin:e.min,xmax:e.max,ymin:i.min,ymax:i.max};if(!s)return t._scaleRanges=n,!0;const o=s.xmin!==e.min||s.xmax!==e.max||s.ymin!==i.min||s.ymax!==i.max;return Object.assign(s,n),o}(e)&&(a=0,r=s.length),i._chart=this.chart,i._datasetIndex=this.index,i._decimated=!!n._decimated,i.points=s;const l=this.resolveDatasetElementOptions(t);this.options.showLine||(l.borderWidth=0),l.segment=this.options.segment,this.updateElement(i,void 0,{animated:!o,options:l},t),this.updateElements(s,a,r,t)}updateElements(t,e,i,s){const n="reset"===s,{iScale:o,vScale:a,_stacked:r,_dataset:l}=this._cachedMeta,h=this.resolveDataElementOptions(e,s),c=this.getSharedOptions(h),d=this.includeOptions(s,c),u=o.axis,f=a.axis,{spanGaps:g,segment:p}=this.options,m=Tt(g)?g:Number.POSITIVE_INFINITY,x=this.chart._animationsDisabled||n||"none"===s;let b=e>0&&this.getParsed(e-1);for(let h=e;h0&&i[u]-b[u]>m,p&&(g.parsed=i,g.raw=l.data[h]),d&&(g.options=c||this.resolveDataElementOptions(h,e.active?"active":s)),x||this.updateElement(e,h,g,s),b=i}this.updateSharedOptions(c,s,h)}getMaxOverflow(){const t=this._cachedMeta,e=t.dataset,i=e.options&&e.options.borderWidth||0,s=t.data||[];if(!s.length)return i;const n=s[0].size(this.resolveDataElementOptions(0)),o=s[s.length-1].size(this.resolveDataElementOptions(s.length-1));return Math.max(i,n,o)/2}draw(){const t=this._cachedMeta;t.dataset.updateControlPoints(this.chart.chartArea,t.iScale.axis),super.draw()}}Cn.id="line",Cn.defaults={datasetElementType:"line",dataElementType:"point",showLine:!0,spanGaps:!1},Cn.overrides={scales:{_index_:{type:"category"},_value_:{type:"linear"}}};class On extends Ps{constructor(t,e){super(t,e),this.innerRadius=void 0,this.outerRadius=void 0}getLabelAndValue(t){const e=this._cachedMeta,i=this.chart,s=i.data.labels||[],n=Ri(e._parsed[t].r,i.options.locale);return{label:s[t]||"",value:n}}update(t){const e=this._cachedMeta.data;this._updateRadius(),this.updateElements(e,0,e.length,t)}_updateRadius(){const t=this.chart,e=t.chartArea,i=t.options,s=Math.min(e.right-e.left,e.bottom-e.top),n=Math.max(s/2,0),o=(n-Math.max(i.cutoutPercentage?n/100*i.cutoutPercentage:1,0))/t.getVisibleDatasetCount();this.outerRadius=n-o*this.index,this.innerRadius=this.outerRadius-o}updateElements(t,e,i,s){const n="reset"===s,o=this.chart,a=this.getDataset(),r=o.options.animation,l=this._cachedMeta.rScale,h=l.xCenter,c=l.yCenter,d=l.getIndexAngle(0)-.5*_t;let u,f=d;const g=360/this.countVisibleElements();for(u=0;u{!isNaN(t.data[s])&&this.chart.getDataVisibility(s)&&i++})),i}_computeAngle(t,e,i){return this.chart.getDataVisibility(t)?It(this.resolveDataElementOptions(t,e).angle||i):0}}On.id="polarArea",On.defaults={dataElementType:"arc",animation:{animateRotate:!0,animateScale:!0},animations:{numbers:{type:"number",properties:["x","y","startAngle","endAngle","innerRadius","outerRadius"]}},indexAxis:"r",startAngle:0},On.overrides={aspectRatio:1,plugins:{legend:{labels:{generateLabels(t){const e=t.data;if(e.labels.length&&e.datasets.length){const{labels:{pointStyle:i}}=t.legend.options;return e.labels.map(((e,s)=>{const n=t.getDatasetMeta(0).controller.getStyle(s);return{text:e,fillStyle:n.backgroundColor,strokeStyle:n.borderColor,lineWidth:n.borderWidth,pointStyle:i,hidden:!t.getDataVisibility(s),index:s}}))}return[]}},onClick(t,e,i){i.chart.toggleDataVisibility(e.index),i.chart.update()}},tooltip:{callbacks:{title:()=>"",label:t=>t.chart.data.labels[t.dataIndex]+": "+t.formattedValue}}},scales:{r:{type:"radialLinear",angleLines:{display:!1},beginAtZero:!0,grid:{circular:!0},pointLabels:{display:!1},startAngle:0}}};class An extends Dn{}An.id="pie",An.defaults={cutout:0,rotation:0,circumference:360,radius:"100%"};class Tn extends Ps{getLabelAndValue(t){const e=this._cachedMeta.vScale,i=this.getParsed(t);return{label:e.getLabels()[t],value:""+e.getLabelForValue(i[e.axis])}}update(t){const e=this._cachedMeta,i=e.dataset,s=e.data||[],n=e.iScale.getLabels();if(i.points=s,"resize"!==t){const e=this.resolveDatasetElementOptions(t);this.options.showLine||(e.borderWidth=0);const o={_loop:!0,_fullLoop:n.length===s.length,options:e};this.updateElement(i,void 0,o,t)}this.updateElements(s,0,s.length,t)}updateElements(t,e,i,s){const n=this.getDataset(),o=this._cachedMeta.rScale,a="reset"===s;for(let r=e;r"",label:t=>"("+t.label+", "+t.formattedValue+")"}}},scales:{x:{type:"linear"},y:{type:"linear"}}};var Rn=Object.freeze({__proto__:null,BarController:Sn,BubbleController:Pn,DoughnutController:Dn,LineController:Cn,PolarAreaController:On,PieController:An,RadarController:Tn,ScatterController:Ln});function En(t,e,i){const{startAngle:s,pixelMargin:n,x:o,y:a,outerRadius:r,innerRadius:l}=e;let h=n/r;t.beginPath(),t.arc(o,a,r,s-h,i+h),l>n?(h=n/l,t.arc(o,a,l,i+h,s-h,!0)):t.arc(o,a,n,i+kt,s-kt),t.closePath(),t.clip()}function In(t,e,i,s){const n=Be(t.options.borderRadius,["outerStart","outerEnd","innerStart","innerEnd"]);const o=(i-e)/2,a=Math.min(o,s*e/2),r=t=>{const e=(i-Math.min(o,t))*s/2;return jt(t,0,Math.min(o,e))};return{outerStart:r(n.outerStart),outerEnd:r(n.outerEnd),innerStart:jt(n.innerStart,0,a),innerEnd:jt(n.innerEnd,0,a)}}function zn(t,e,i,s){return{x:i+t*Math.cos(e),y:s+t*Math.sin(e)}}function Fn(t,e,i,s,n){const{x:o,y:a,startAngle:r,pixelMargin:l,innerRadius:h}=e,c=Math.max(e.outerRadius+s+i-l,0),d=h>0?h+s+i+l:0;let u=0;const f=n-r;if(s){const t=((h>0?h-s:0)+(c>0?c-s:0))/2;u=(f-(0!==t?f*t/(t+s):f))/2}const g=(f-Math.max(.001,f*c-i/_t)/c)/2,p=r+g+u,m=n-g-u,{outerStart:x,outerEnd:b,innerStart:_,innerEnd:y}=In(e,d,c,m-p),v=c-x,w=c-b,M=p+x/v,k=m-b/w,S=d+_,P=d+y,D=p+_/S,C=m-y/P;if(t.beginPath(),t.arc(o,a,c,M,k),b>0){const e=zn(w,k,o,a);t.arc(e.x,e.y,b,k,m+kt)}const O=zn(P,m,o,a);if(t.lineTo(O.x,O.y),y>0){const e=zn(P,C,o,a);t.arc(e.x,e.y,y,m+kt,C+Math.PI)}if(t.arc(o,a,d,m-y/d,p+_/d,!0),_>0){const e=zn(S,D,o,a);t.arc(e.x,e.y,_,D+Math.PI,p-kt)}const A=zn(v,p,o,a);if(t.lineTo(A.x,A.y),x>0){const e=zn(v,M,o,a);t.arc(e.x,e.y,x,p-kt,M)}t.closePath()}function Bn(t,e,i,s,n){const{options:o}=e,{borderWidth:a,borderJoinStyle:r}=o,l="inner"===o.borderAlign;a&&(l?(t.lineWidth=2*a,t.lineJoin=r||"round"):(t.lineWidth=a,t.lineJoin=r||"bevel"),e.fullCircles&&function(t,e,i){const{x:s,y:n,startAngle:o,pixelMargin:a,fullCircles:r}=e,l=Math.max(e.outerRadius-a,0),h=e.innerRadius+a;let c;for(i&&En(t,e,o+yt),t.beginPath(),t.arc(s,n,h,o+yt,o,!0),c=0;c=yt||Ht(n,a,r),f=Yt(o,l+d,h+d);return u&&f}getCenterPoint(t){const{x:e,y:i,startAngle:s,endAngle:n,innerRadius:o,outerRadius:a}=this.getProps(["x","y","startAngle","endAngle","innerRadius","outerRadius","circumference"],t),{offset:r,spacing:l}=this.options,h=(s+n)/2,c=(o+a+l+r)/2;return{x:e+Math.cos(h)*c,y:i+Math.sin(h)*c}}tooltipPosition(t){return this.getCenterPoint(t)}draw(t){const{options:e,circumference:i}=this,s=(e.offset||0)/2,n=(e.spacing||0)/2;if(this.pixelMargin="inner"===e.borderAlign?.33:0,this.fullCircles=i>yt?Math.floor(i/yt):0,0===i||this.innerRadius<0||this.outerRadius<0)return;t.save();let o=0;if(s){o=s/2;const e=(this.startAngle+this.endAngle)/2;t.translate(Math.cos(e)*o,Math.sin(e)*o),this.circumference>=_t&&(o=s)}t.fillStyle=e.backgroundColor,t.strokeStyle=e.borderColor;const a=function(t,e,i,s){const{fullCircles:n,startAngle:o,circumference:a}=e;let r=e.endAngle;if(n){Fn(t,e,i,s,o+yt);for(let e=0;er&&o>r;return{count:s,start:l,loop:e.loop,ilen:h(a+(h?r-t:t))%o,_=()=>{f!==g&&(t.lineTo(m,g),t.lineTo(m,f),t.lineTo(m,p))};for(l&&(d=n[b(0)],t.moveTo(d.x,d.y)),c=0;c<=r;++c){if(d=n[b(c)],d.skip)continue;const e=d.x,i=d.y,s=0|e;s===u?(ig&&(g=i),m=(x*m+e)/++x):(_(),t.lineTo(e,i),u=s,x=0,f=g=i),p=i}_()}function Yn(t){const e=t.options,i=e.borderDash&&e.borderDash.length;return!(t._decimated||t._loop||e.tension||"monotone"===e.cubicInterpolationMode||e.stepped||i)?$n:jn}Vn.id="arc",Vn.defaults={borderAlign:"center",borderColor:"#fff",borderJoinStyle:void 0,borderRadius:0,borderWidth:2,offset:0,spacing:0,angle:void 0},Vn.defaultRoutes={backgroundColor:"backgroundColor"};const Un="function"==typeof Path2D;function Xn(t,e,i,s){Un&&!e.options.segment?function(t,e,i,s){let n=e._path;n||(n=e._path=new Path2D,e.path(n,i,s)&&n.closePath()),Wn(t,e.options),t.stroke(n)}(t,e,i,s):function(t,e,i,s){const{segments:n,options:o}=e,a=Yn(e);for(const r of n)Wn(t,o,r.style),t.beginPath(),a(t,e,r,{start:i,end:i+s-1})&&t.closePath(),t.stroke()}(t,e,i,s)}class qn extends Ds{constructor(t){super(),this.animated=!0,this.options=void 0,this._chart=void 0,this._loop=void 0,this._fullLoop=void 0,this._path=void 0,this._points=void 0,this._segments=void 0,this._decimated=!1,this._pointsUpdated=!1,this._datasetIndex=void 0,t&&Object.assign(this,t)}updateControlPoints(t,e){const i=this.options;if((i.tension||"monotone"===i.cubicInterpolationMode)&&!i.stepped&&!this._pointsUpdated){const s=i.spanGaps?this._loop:this._fullLoop;ki(this._points,i,t,s,e),this._pointsUpdated=!0}}set points(t){this._points=t,delete this._segments,delete this._path,this._pointsUpdated=!1}get points(){return this._points}get segments(){return this._segments||(this._segments=Ni(this,this.options.segment))}first(){const t=this.segments,e=this.points;return t.length&&e[t[0].start]}last(){const t=this.segments,e=this.points,i=t.length;return i&&e[t[i-1].end]}interpolate(t,e){const i=this.options,s=t[e],n=this.points,o=Wi(this,{property:e,start:s,end:s});if(!o.length)return;const a=[],r=function(t){return t.stepped?Ai:t.tension||"monotone"===t.cubicInterpolationMode?Ti:Oi}(i);let l,h;for(l=0,h=o.length;l"borderDash"!==t&&"fill"!==t};class Gn extends Ds{constructor(t){super(),this.options=void 0,this.parsed=void 0,this.skip=void 0,this.stop=void 0,t&&Object.assign(this,t)}inRange(t,e,i){const s=this.options,{x:n,y:o}=this.getProps(["x","y"],i);return Math.pow(t-n,2)+Math.pow(e-o,2){oo(t)}))}var ro={id:"decimation",defaults:{algorithm:"min-max",enabled:!1},beforeElementsUpdate:(t,e,i)=>{if(!i.enabled)return void ao(t);const s=t.width;t.data.datasets.forEach(((e,n)=>{const{_data:o,indexAxis:a}=e,r=t.getDatasetMeta(n),l=o||e.data;if("y"===je([a,t.options.indexAxis]))return;if("line"!==r.type)return;const h=t.scales[r.xAxisID];if("linear"!==h.type&&"time"!==h.type)return;if(t.options.parsing)return;let{start:c,count:d}=function(t,e){const i=e.length;let s,n=0;const{iScale:o}=t,{min:a,max:r,minDefined:l,maxDefined:h}=o.getUserBounds();return l&&(n=jt(re(e,o.axis,a).lo,0,i-1)),s=h?jt(re(e,o.axis,r).hi+1,n,i)-n:i-n,{start:n,count:s}}(r,l);if(d<=(i.threshold||4*s))return void oo(e);let u;switch($(o)&&(e._data=l,delete e.data,Object.defineProperty(e,"data",{configurable:!0,enumerable:!0,get:function(){return this._decimated},set:function(t){this._data=t}})),i.algorithm){case"lttb":u=function(t,e,i,s,n){const o=n.samples||s;if(o>=i)return t.slice(e,e+i);const a=[],r=(i-2)/(o-2);let l=0;const h=e+i-1;let c,d,u,f,g,p=e;for(a[l++]=t[p],c=0;cu&&(u=f,d=t[s],g=s);a[l++]=d,p=g}return a[l++]=t[h],a}(l,c,d,s,i);break;case"min-max":u=function(t,e,i,s){let n,o,a,r,l,h,c,d,u,f,g=0,p=0;const m=[],x=e+i-1,b=t[e].x,_=t[x].x-b;for(n=e;nf&&(f=r,c=n),g=(p*g+o.x)/++p;else{const i=n-1;if(!$(h)&&!$(c)){const e=Math.min(h,c),s=Math.max(h,c);e!==d&&e!==i&&m.push({...t[e],x:g}),s!==d&&s!==i&&m.push({...t[s],x:g})}n>0&&i!==d&&m.push(t[i]),m.push(o),l=e,p=0,u=f=r,h=c=d=n}}return m}(l,c,d,s);break;default:throw new Error(`Unsupported decimation algorithm '${i.algorithm}'`)}e._decimated=u}))},destroy(t){ao(t)}};function lo(t,e,i){const s=function(t){const e=t.options,i=e.fill;let s=K(i&&i.target,i);return void 0===s&&(s=!!e.backgroundColor),!1!==s&&null!==s&&(!0===s?"origin":s)}(t);if(U(s))return!isNaN(s.value)&&s;let n=parseFloat(s);return X(n)&&Math.floor(n)===n?("-"!==s[0]&&"+"!==s[0]||(n=e+n),!(n===e||n<0||n>=i)&&n):["origin","start","end","stack","shape"].indexOf(s)>=0&&s}class ho{constructor(t){this.x=t.x,this.y=t.y,this.radius=t.radius}pathSegment(t,e,i){const{x:s,y:n,radius:o}=this;return e=e||{start:0,end:yt},t.arc(s,n,o,e.end,e.start,!0),!i.bounds}interpolate(t){const{x:e,y:i,radius:s}=this,n=t.angle;return{x:e+Math.cos(n)*s,y:i+Math.sin(n)*s,angle:n}}}function co(t){return(t.scale||{}).getPointPositionForValue?function(t){const{scale:e,fill:i}=t,s=e.options,n=e.getLabels().length,o=[],a=s.reverse?e.max:e.min,r=s.reverse?e.min:e.max;let l,h,c;if(c="start"===i?a:"end"===i?r:U(i)?i.value:e.getBaseValue(),s.grid.circular)return h=e.getPointPositionForValue(0,a),new ho({x:h.x,y:h.y,radius:e.getDistanceFromCenterForValue(c)});for(l=0;lt;e--){const t=i[e];if(!isNaN(t.x)&&!isNaN(t.y))break}return e}function fo(t,e,i){const s=[];for(let n=0;n{e=uo(t,e,n);const a=n[t],r=n[e];null!==s?(o.push({x:a.x,y:s}),o.push({x:r.x,y:s})):null!==i&&(o.push({x:i,y:a.y}),o.push({x:i,y:r.y}))})),o}(t,e),i.length?new qn({points:i,options:{tension:0},_loop:s,_fullLoop:s}):null}function xo(t,e,i){let s=t[e].fill;const n=[e];let o;if(!i)return s;for(;!1!==s&&-1===n.indexOf(s);){if(!X(s))return s;if(o=t[s],!o)return!1;if(o.visible)return s;n.push(s),s=o.fill}return!1}function bo(t,e,i){const{segments:s,points:n}=e;let o=!0,a=!1;t.beginPath();for(const r of s){const{start:s,end:l}=r,h=n[s],c=n[uo(s,l,n)];o?(t.moveTo(h.x,h.y),o=!1):(t.lineTo(h.x,i),t.lineTo(h.x,h.y)),a=!!e.pathSegment(t,r,{move:a}),a?t.closePath():t.lineTo(c.x,i)}t.lineTo(e.first().x,i),t.closePath(),t.clip()}function _o(t,e,i,s){if(s)return;let n=e[t],o=i[t];return"angle"===t&&(n=Nt(n),o=Nt(o)),{property:t,start:n,end:o}}function yo(t,e,i,s){return t&&e?s(t[i],e[i]):t?t[i]:e?e[i]:0}function vo(t,e,i){const{top:s,bottom:n}=e.chart.chartArea,{property:o,start:a,end:r}=i||{};"x"===o&&(t.beginPath(),t.rect(a,s,r-a,n-s),t.clip())}function wo(t,e,i,s){const n=e.interpolate(i,s);n&&t.lineTo(n.x,n.y)}function Mo(t,e){const{line:i,target:s,property:n,color:o,scale:a}=e,r=function(t,e,i){const s=t.segments,n=t.points,o=e.points,a=[];for(const t of s){let{start:s,end:r}=t;r=uo(s,r,n);const l=_o(i,n[s],n[r],t.loop);if(!e.segments){a.push({source:t,target:l,start:n[s],end:n[r]});continue}const h=Wi(e,l);for(const e of h){const s=_o(i,o[e.start],o[e.end],e.loop),r=Vi(t,n,s);for(const t of r)a.push({source:t,target:e,start:{[i]:yo(l,s,"start",Math.max)},end:{[i]:yo(l,s,"end",Math.min)}})}}return a}(i,s,n);for(const{source:e,target:l,start:h,end:c}of r){const{style:{backgroundColor:r=o}={}}=e,d=!0!==s;t.save(),t.fillStyle=r,vo(t,a,d&&_o(n,h,c)),t.beginPath();const u=!!i.pathSegment(t,e);let f;if(d){u?t.closePath():wo(t,s,c,n);const e=!!s.pathSegment(t,l,{move:u,reverse:!0});f=u&&e,f||wo(t,s,h,n)}t.closePath(),t.fill(f?"evenodd":"nonzero"),t.restore()}}function ko(t,e,i){const s=po(e),{line:n,scale:o,axis:a}=e,r=n.options,l=r.fill,h=r.backgroundColor,{above:c=h,below:d=h}=l||{};s&&n.points.length&&(Qt(t,i),function(t,e){const{line:i,target:s,above:n,below:o,area:a,scale:r}=e,l=i._loop?"angle":e.axis;t.save(),"x"===l&&o!==n&&(bo(t,s,a.top),Mo(t,{line:i,target:s,color:n,scale:r,property:l}),t.restore(),t.save(),bo(t,s,a.bottom)),Mo(t,{line:i,target:s,color:o,scale:r,property:l}),t.restore()}(t,{line:n,target:s,above:c,below:d,area:i,scale:o,axis:a}),te(t))}var So={id:"filler",afterDatasetsUpdate(t,e,i){const s=(t.data.datasets||[]).length,n=[];let o,a,r,l;for(a=0;a=0;--e){const i=n[e].$filler;i&&(i.line.updateControlPoints(o,i.axis),s&&ko(t.ctx,i,o))}},beforeDatasetsDraw(t,e,i){if("beforeDatasetsDraw"!==i.drawTime)return;const s=t.getSortedVisibleDatasetMetas();for(let e=s.length-1;e>=0;--e){const i=s[e].$filler;i&&ko(t.ctx,i,t.chartArea)}},beforeDatasetDraw(t,e,i){const s=e.meta.$filler;s&&!1!==s.fill&&"beforeDatasetDraw"===i.drawTime&&ko(t.ctx,s,t.chartArea)},defaults:{propagate:!0,drawTime:"beforeDatasetDraw"}};const Po=(t,e)=>{let{boxHeight:i=e,boxWidth:s=e}=t;return t.usePointStyle&&(i=Math.min(i,e),s=Math.min(s,e)),{boxWidth:s,boxHeight:i,itemHeight:Math.max(e,i)}};class Do extends Ds{constructor(t){super(),this._added=!1,this.legendHitBoxes=[],this._hoveredItem=null,this.doughnutMode=!1,this.chart=t.chart,this.options=t.options,this.ctx=t.ctx,this.legendItems=void 0,this.columnSizes=void 0,this.lineWidths=void 0,this.maxHeight=void 0,this.maxWidth=void 0,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.height=void 0,this.width=void 0,this._margins=void 0,this.position=void 0,this.weight=void 0,this.fullSize=void 0}update(t,e,i){this.maxWidth=t,this.maxHeight=e,this._margins=i,this.setDimensions(),this.buildLabels(),this.fit()}setDimensions(){this.isHorizontal()?(this.width=this.maxWidth,this.left=this._margins.left,this.right=this.width):(this.height=this.maxHeight,this.top=this._margins.top,this.bottom=this.height)}buildLabels(){const t=this.options.labels||{};let e=J(t.generateLabels,[this.chart],this)||[];t.filter&&(e=e.filter((e=>t.filter(e,this.chart.data)))),t.sort&&(e=e.sort(((e,i)=>t.sort(e,i,this.chart.data)))),this.options.reverse&&e.reverse(),this.legendItems=e}fit(){const{options:t,ctx:e}=this;if(!t.display)return void(this.width=this.height=0);const i=t.labels,s=He(i.font),n=s.size,o=this._computeTitleHeight(),{boxWidth:a,itemHeight:r}=Po(i,n);let l,h;e.font=s.string,this.isHorizontal()?(l=this.maxWidth,h=this._fitRows(o,n,a,r)+10):(h=this.maxHeight,l=this._fitCols(o,n,a,r)+10),this.width=Math.min(l,t.maxWidth||this.maxWidth),this.height=Math.min(h,t.maxHeight||this.maxHeight)}_fitRows(t,e,i,s){const{ctx:n,maxWidth:o,options:{labels:{padding:a}}}=this,r=this.legendHitBoxes=[],l=this.lineWidths=[0],h=s+a;let c=t;n.textAlign="left",n.textBaseline="middle";let d=-1,u=-h;return this.legendItems.forEach(((t,f)=>{const g=i+e/2+n.measureText(t.text).width;(0===f||l[l.length-1]+g+2*a>o)&&(c+=h,l[l.length-(f>0?0:1)]=0,u+=h,d++),r[f]={left:0,top:u,row:d,width:g,height:s},l[l.length-1]+=g+a})),c}_fitCols(t,e,i,s){const{ctx:n,maxHeight:o,options:{labels:{padding:a}}}=this,r=this.legendHitBoxes=[],l=this.columnSizes=[],h=o-t;let c=a,d=0,u=0,f=0,g=0;return this.legendItems.forEach(((t,o)=>{const p=i+e/2+n.measureText(t.text).width;o>0&&u+s+2*a>h&&(c+=d+a,l.push({width:d,height:u}),f+=d+a,g++,d=u=0),r[o]={left:f,top:u,col:g,width:p,height:s},d=Math.max(d,p),u+=s+a})),c+=d,l.push({width:d,height:u}),c}adjustHitBoxes(){if(!this.options.display)return;const t=this._computeTitleHeight(),{legendHitBoxes:e,options:{align:i,labels:{padding:s},rtl:o}}=this,a=Ei(o,this.left,this.width);if(this.isHorizontal()){let o=0,r=n(i,this.left+s,this.right-this.lineWidths[o]);for(const l of e)o!==l.row&&(o=l.row,r=n(i,this.left+s,this.right-this.lineWidths[o])),l.top+=this.top+t+s,l.left=a.leftForLtr(a.x(r),l.width),r+=l.width+s}else{let o=0,r=n(i,this.top+t+s,this.bottom-this.columnSizes[o].height);for(const l of e)l.col!==o&&(o=l.col,r=n(i,this.top+t+s,this.bottom-this.columnSizes[o].height)),l.top=r,l.left+=this.left+s,l.left=a.leftForLtr(a.x(l.left),l.width),r+=l.height+s}}isHorizontal(){return"top"===this.options.position||"bottom"===this.options.position}draw(){if(this.options.display){const t=this.ctx;Qt(t,this),this._draw(),te(t)}}_draw(){const{options:t,columnSizes:e,lineWidths:i,ctx:s}=this,{align:a,labels:r}=t,l=bt.color,h=Ei(t.rtl,this.left,this.width),c=He(r.font),{color:d,padding:u}=r,f=c.size,g=f/2;let p;this.drawTitle(),s.textAlign=h.textAlign("left"),s.textBaseline="middle",s.lineWidth=.5,s.font=c.string;const{boxWidth:m,boxHeight:x,itemHeight:b}=Po(r,f),_=this.isHorizontal(),y=this._computeTitleHeight();p=_?{x:n(a,this.left+u,this.right-i[0]),y:this.top+u+y,line:0}:{x:this.left+u,y:n(a,this.top+y+u,this.bottom-e[0].height),line:0},Ii(this.ctx,t.textDirection);const v=b+u;this.legendItems.forEach(((w,M)=>{s.strokeStyle=w.fontColor||d,s.fillStyle=w.fontColor||d;const k=s.measureText(w.text).width,S=h.textAlign(w.textAlign||(w.textAlign=r.textAlign)),P=m+g+k;let D=p.x,C=p.y;h.setWidth(this.width),_?M>0&&D+P+u>this.right&&(C=p.y+=v,p.line++,D=p.x=n(a,this.left+u,this.right-i[p.line])):M>0&&C+v>this.bottom&&(D=p.x=D+e[p.line].width+u,p.line++,C=p.y=n(a,this.top+y+u,this.bottom-e[p.line].height));!function(t,e,i){if(isNaN(m)||m<=0||isNaN(x)||x<0)return;s.save();const n=K(i.lineWidth,1);if(s.fillStyle=K(i.fillStyle,l),s.lineCap=K(i.lineCap,"butt"),s.lineDashOffset=K(i.lineDashOffset,0),s.lineJoin=K(i.lineJoin,"miter"),s.lineWidth=n,s.strokeStyle=K(i.strokeStyle,l),s.setLineDash(K(i.lineDash,[])),r.usePointStyle){const o={radius:m*Math.SQRT2/2,pointStyle:i.pointStyle,rotation:i.rotation,borderWidth:n},a=h.xPlus(t,m/2);Zt(s,o,a,e+g)}else{const o=e+Math.max((f-x)/2,0),a=h.leftForLtr(t,m),r=We(i.borderRadius);s.beginPath(),Object.values(r).some((t=>0!==t))?oe(s,{x:a,y:o,w:m,h:x,radius:r}):s.rect(a,o,m,x),s.fill(),0!==n&&s.stroke()}s.restore()}(h.x(D),C,w),D=o(S,D+m+g,_?D+P:this.right,t.rtl),function(t,e,i){se(s,i.text,t,e+b/2,c,{strikethrough:i.hidden,textAlign:h.textAlign(i.textAlign)})}(h.x(D),C,w),_?p.x+=P+u:p.y+=v})),zi(this.ctx,t.textDirection)}drawTitle(){const t=this.options,e=t.title,i=He(e.font),o=Ne(e.padding);if(!e.display)return;const a=Ei(t.rtl,this.left,this.width),r=this.ctx,l=e.position,h=i.size/2,c=o.top+h;let d,u=this.left,f=this.width;if(this.isHorizontal())f=Math.max(...this.lineWidths),d=this.top+c,u=n(t.align,u,this.right-f);else{const e=this.columnSizes.reduce(((t,e)=>Math.max(t,e.height)),0);d=c+n(t.align,this.top,this.bottom-e-t.labels.padding-this._computeTitleHeight())}const g=n(l,u,u+f);r.textAlign=a.textAlign(s(l)),r.textBaseline="middle",r.strokeStyle=e.color,r.fillStyle=e.color,r.font=i.string,se(r,e.text,g,d,i)}_computeTitleHeight(){const t=this.options.title,e=He(t.font),i=Ne(t.padding);return t.display?e.lineHeight+i.height:0}_getLegendItemAt(t,e){let i,s,n;if(Yt(t,this.left,this.right)&&Yt(e,this.top,this.bottom))for(n=this.legendHitBoxes,i=0;it.chart.options.color,boxWidth:40,padding:10,generateLabels(t){const e=t.data.datasets,{labels:{usePointStyle:i,pointStyle:s,textAlign:n,color:o}}=t.legend.options;return t._getSortedDatasetMetas().map((t=>{const a=t.controller.getStyle(i?0:void 0),r=Ne(a.borderWidth);return{text:e[t.index].label,fillStyle:a.backgroundColor,fontColor:o,hidden:!t.visible,lineCap:a.borderCapStyle,lineDash:a.borderDash,lineDashOffset:a.borderDashOffset,lineJoin:a.borderJoinStyle,lineWidth:(r.width+r.height)/4,strokeStyle:a.borderColor,pointStyle:s||a.pointStyle,rotation:a.rotation,textAlign:n||a.textAlign,borderRadius:0,datasetIndex:t.index}}),this)}},title:{color:t=>t.chart.options.color,display:!1,position:"center",text:""}},descriptors:{_scriptable:t=>!t.startsWith("on"),labels:{_scriptable:t=>!["generateLabels","filter","sort"].includes(t)}}};class Oo extends Ds{constructor(t){super(),this.chart=t.chart,this.options=t.options,this.ctx=t.ctx,this._padding=void 0,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.width=void 0,this.height=void 0,this.position=void 0,this.weight=void 0,this.fullSize=void 0}update(t,e){const i=this.options;if(this.left=0,this.top=0,!i.display)return void(this.width=this.height=this.right=this.bottom=0);this.width=this.right=t,this.height=this.bottom=e;const s=Y(i.text)?i.text.length:1;this._padding=Ne(i.padding);const n=s*He(i.font).lineHeight+this._padding.height;this.isHorizontal()?this.height=n:this.width=n}isHorizontal(){const t=this.options.position;return"top"===t||"bottom"===t}_drawArgs(t){const{top:e,left:i,bottom:s,right:o,options:a}=this,r=a.align;let l,h,c,d=0;return this.isHorizontal()?(h=n(r,i,o),c=e+t,l=o-i):("left"===a.position?(h=i+t,c=n(r,s,e),d=-.5*_t):(h=o-t,c=n(r,e,s),d=.5*_t),l=s-e),{titleX:h,titleY:c,maxWidth:l,rotation:d}}draw(){const t=this.ctx,e=this.options;if(!e.display)return;const i=He(e.font),n=i.lineHeight/2+this._padding.top,{titleX:o,titleY:a,maxWidth:r,rotation:l}=this._drawArgs(n);se(t,e.text,0,0,i,{color:e.color,maxWidth:r,rotation:l,textAlign:s(e.align),textBaseline:"middle",translation:[o,a]})}}var Ao={id:"title",_element:Oo,start(t,e,i){!function(t,e){const i=new Oo({ctx:t.ctx,options:e,chart:t});ni.configure(t,i,e),ni.addBox(t,i),t.titleBlock=i}(t,i)},stop(t){const e=t.titleBlock;ni.removeBox(t,e),delete t.titleBlock},beforeUpdate(t,e,i){const s=t.titleBlock;ni.configure(t,s,i),s.options=i},defaults:{align:"center",display:!1,font:{weight:"bold"},fullSize:!0,padding:10,position:"top",text:"",weight:2e3},defaultRoutes:{color:"color"},descriptors:{_scriptable:!0,_indexable:!1}};const To=new WeakMap;var Lo={id:"subtitle",start(t,e,i){const s=new Oo({ctx:t.ctx,options:i,chart:t});ni.configure(t,s,i),ni.addBox(t,s),To.set(t,s)},stop(t){ni.removeBox(t,To.get(t)),To.delete(t)},beforeUpdate(t,e,i){const s=To.get(t);ni.configure(t,s,i),s.options=i},defaults:{align:"center",display:!1,font:{weight:"normal"},fullSize:!0,padding:0,position:"top",text:"",weight:1500},defaultRoutes:{color:"color"},descriptors:{_scriptable:!0,_indexable:!1}};const Ro={average(t){if(!t.length)return!1;let e,i,s=0,n=0,o=0;for(e=0,i=t.length;e-1?t.split("\n"):t}function zo(t,e){const{element:i,datasetIndex:s,index:n}=e,o=t.getDatasetMeta(s).controller,{label:a,value:r}=o.getLabelAndValue(n);return{chart:t,label:a,parsed:o.getParsed(n),raw:t.data.datasets[s].data[n],formattedValue:r,dataset:o.getDataset(),dataIndex:n,datasetIndex:s,element:i}}function Fo(t,e){const i=t.chart.ctx,{body:s,footer:n,title:o}=t,{boxWidth:a,boxHeight:r}=e,l=He(e.bodyFont),h=He(e.titleFont),c=He(e.footerFont),d=o.length,u=n.length,f=s.length,g=Ne(e.padding);let p=g.height,m=0,x=s.reduce(((t,e)=>t+e.before.length+e.lines.length+e.after.length),0);if(x+=t.beforeBody.length+t.afterBody.length,d&&(p+=d*h.lineHeight+(d-1)*e.titleSpacing+e.titleMarginBottom),x){p+=f*(e.displayColors?Math.max(r,l.lineHeight):l.lineHeight)+(x-f)*l.lineHeight+(x-1)*e.bodySpacing}u&&(p+=e.footerMarginTop+u*c.lineHeight+(u-1)*e.footerSpacing);let b=0;const _=function(t){m=Math.max(m,i.measureText(t).width+b)};return i.save(),i.font=h.string,Q(t.title,_),i.font=l.string,Q(t.beforeBody.concat(t.afterBody),_),b=e.displayColors?a+2+e.boxPadding:0,Q(s,(t=>{Q(t.before,_),Q(t.lines,_),Q(t.after,_)})),b=0,i.font=c.string,Q(t.footer,_),i.restore(),m+=g.width,{width:m,height:p}}function Bo(t,e,i,s){const{x:n,width:o}=i,{width:a,chartArea:{left:r,right:l}}=t;let h="center";return"center"===s?h=n<=(r+l)/2?"left":"right":n<=o/2?h="left":n>=a-o/2&&(h="right"),function(t,e,i,s){const{x:n,width:o}=s,a=i.caretSize+i.caretPadding;return"left"===t&&n+o+a>e.width||"right"===t&&n-o-a<0||void 0}(h,t,e,i)&&(h="center"),h}function Vo(t,e,i){const s=i.yAlign||e.yAlign||function(t,e){const{y:i,height:s}=e;return it.height-s/2?"bottom":"center"}(t,i);return{xAlign:i.xAlign||e.xAlign||Bo(t,e,i,s),yAlign:s}}function Wo(t,e,i,s){const{caretSize:n,caretPadding:o,cornerRadius:a}=t,{xAlign:r,yAlign:l}=i,h=n+o,{topLeft:c,topRight:d,bottomLeft:u,bottomRight:f}=We(a);let g=function(t,e){let{x:i,width:s}=t;return"right"===e?i-=s:"center"===e&&(i-=s/2),i}(e,r);const p=function(t,e,i){let{y:s,height:n}=t;return"top"===e?s+=i:s-="bottom"===e?n+i:n/2,s}(e,l,h);return"center"===l?"left"===r?g+=h:"right"===r&&(g-=h):"left"===r?g-=Math.max(c,u)+n:"right"===r&&(g+=Math.max(d,f)+n),{x:jt(g,0,s.width-e.width),y:jt(p,0,s.height-e.height)}}function No(t,e,i){const s=Ne(i.padding);return"center"===e?t.x+t.width/2:"right"===e?t.x+t.width-s.right:t.x+s.left}function Ho(t){return Eo([],Io(t))}function jo(t,e){const i=e&&e.dataset&&e.dataset.tooltip&&e.dataset.tooltip.callbacks;return i?t.override(i):t}class $o extends Ds{constructor(t){super(),this.opacity=0,this._active=[],this._eventPosition=void 0,this._size=void 0,this._cachedAnimations=void 0,this._tooltipItems=[],this.$animations=void 0,this.$context=void 0,this.chart=t.chart||t._chart,this._chart=this.chart,this.options=t.options,this.dataPoints=void 0,this.title=void 0,this.beforeBody=void 0,this.body=void 0,this.afterBody=void 0,this.footer=void 0,this.xAlign=void 0,this.yAlign=void 0,this.x=void 0,this.y=void 0,this.height=void 0,this.width=void 0,this.caretX=void 0,this.caretY=void 0,this.labelColors=void 0,this.labelPointStyles=void 0,this.labelTextColors=void 0}initialize(t){this.options=t,this._cachedAnimations=void 0,this.$context=void 0}_resolveAnimations(){const t=this._cachedAnimations;if(t)return t;const e=this.chart,i=this.options.setContext(this.getContext()),s=i.enabled&&e.options.animation&&i.animations,n=new gs(this.chart,s);return s._cacheable&&(this._cachedAnimations=Object.freeze(n)),n}getContext(){return this.$context||(this.$context=(t=this.chart.getContext(),e=this,i=this._tooltipItems,Ye(t,{tooltip:e,tooltipItems:i,type:"tooltip"})));var t,e,i}getTitle(t,e){const{callbacks:i}=e,s=i.beforeTitle.apply(this,[t]),n=i.title.apply(this,[t]),o=i.afterTitle.apply(this,[t]);let a=[];return a=Eo(a,Io(s)),a=Eo(a,Io(n)),a=Eo(a,Io(o)),a}getBeforeBody(t,e){return Ho(e.callbacks.beforeBody.apply(this,[t]))}getBody(t,e){const{callbacks:i}=e,s=[];return Q(t,(t=>{const e={before:[],lines:[],after:[]},n=jo(i,t);Eo(e.before,Io(n.beforeLabel.call(this,t))),Eo(e.lines,n.label.call(this,t)),Eo(e.after,Io(n.afterLabel.call(this,t))),s.push(e)})),s}getAfterBody(t,e){return Ho(e.callbacks.afterBody.apply(this,[t]))}getFooter(t,e){const{callbacks:i}=e,s=i.beforeFooter.apply(this,[t]),n=i.footer.apply(this,[t]),o=i.afterFooter.apply(this,[t]);let a=[];return a=Eo(a,Io(s)),a=Eo(a,Io(n)),a=Eo(a,Io(o)),a}_createItems(t){const e=this._active,i=this.chart.data,s=[],n=[],o=[];let a,r,l=[];for(a=0,r=e.length;at.filter(e,s,n,i)))),t.itemSort&&(l=l.sort(((e,s)=>t.itemSort(e,s,i)))),Q(l,(e=>{const i=jo(t.callbacks,e);s.push(i.labelColor.call(this,e)),n.push(i.labelPointStyle.call(this,e)),o.push(i.labelTextColor.call(this,e))})),this.labelColors=s,this.labelPointStyles=n,this.labelTextColors=o,this.dataPoints=l,l}update(t,e){const i=this.options.setContext(this.getContext()),s=this._active;let n,o=[];if(s.length){const t=Ro[i.position].call(this,s,this._eventPosition);o=this._createItems(i),this.title=this.getTitle(o,i),this.beforeBody=this.getBeforeBody(o,i),this.body=this.getBody(o,i),this.afterBody=this.getAfterBody(o,i),this.footer=this.getFooter(o,i);const e=this._size=Fo(this,i),a=Object.assign({},t,e),r=Vo(this.chart,i,a),l=Wo(i,a,r,this.chart);this.xAlign=r.xAlign,this.yAlign=r.yAlign,n={opacity:1,x:l.x,y:l.y,width:e.width,height:e.height,caretX:t.x,caretY:t.y}}else 0!==this.opacity&&(n={opacity:0});this._tooltipItems=o,this.$context=void 0,n&&this._resolveAnimations().update(this,n),t&&i.external&&i.external.call(this,{chart:this.chart,tooltip:this,replay:e})}drawCaret(t,e,i,s){const n=this.getCaretPosition(t,i,s);e.lineTo(n.x1,n.y1),e.lineTo(n.x2,n.y2),e.lineTo(n.x3,n.y3)}getCaretPosition(t,e,i){const{xAlign:s,yAlign:n}=this,{caretSize:o,cornerRadius:a}=i,{topLeft:r,topRight:l,bottomLeft:h,bottomRight:c}=We(a),{x:d,y:u}=t,{width:f,height:g}=e;let p,m,x,b,_,y;return"center"===n?(_=u+g/2,"left"===s?(p=d,m=p-o,b=_+o,y=_-o):(p=d+f,m=p+o,b=_-o,y=_+o),x=p):(m="left"===s?d+Math.max(r,h)+o:"right"===s?d+f-Math.max(l,c)-o:this.caretX,"top"===n?(b=u,_=b-o,p=m-o,x=m+o):(b=u+g,_=b+o,p=m+o,x=m-o),y=b),{x1:p,x2:m,x3:x,y1:b,y2:_,y3:y}}drawTitle(t,e,i){const s=this.title,n=s.length;let o,a,r;if(n){const l=Ei(i.rtl,this.x,this.width);for(t.x=No(this,i.titleAlign,i),e.textAlign=l.textAlign(i.titleAlign),e.textBaseline="middle",o=He(i.titleFont),a=i.titleSpacing,e.fillStyle=i.titleColor,e.font=o.string,r=0;r0!==t))?(t.beginPath(),t.fillStyle=n.multiKeyBackground,oe(t,{x:e,y:g,w:l,h:r,radius:a}),t.fill(),t.stroke(),t.fillStyle=o.backgroundColor,t.beginPath(),oe(t,{x:i,y:g+1,w:l-2,h:r-2,radius:a}),t.fill()):(t.fillStyle=n.multiKeyBackground,t.fillRect(e,g,l,r),t.strokeRect(e,g,l,r),t.fillStyle=o.backgroundColor,t.fillRect(i,g+1,l-2,r-2))}t.fillStyle=this.labelTextColors[i]}drawBody(t,e,i){const{body:s}=this,{bodySpacing:n,bodyAlign:o,displayColors:a,boxHeight:r,boxWidth:l,boxPadding:h}=i,c=He(i.bodyFont);let d=c.lineHeight,u=0;const f=Ei(i.rtl,this.x,this.width),g=function(i){e.fillText(i,f.x(t.x+u),t.y+d/2),t.y+=d+n},p=f.textAlign(o);let m,x,b,_,y,v,w;for(e.textAlign=o,e.textBaseline="middle",e.font=c.string,t.x=No(this,p,i),e.fillStyle=i.bodyColor,Q(this.beforeBody,g),u=a&&"right"!==p?"center"===o?l/2+h:l+2+h:0,_=0,v=s.length;_0&&e.stroke()}_updateAnimationTarget(t){const e=this.chart,i=this.$animations,s=i&&i.x,n=i&&i.y;if(s||n){const i=Ro[t.position].call(this,this._active,this._eventPosition);if(!i)return;const o=this._size=Fo(this,t),a=Object.assign({},i,this._size),r=Vo(e,t,a),l=Wo(t,a,r,e);s._to===l.x&&n._to===l.y||(this.xAlign=r.xAlign,this.yAlign=r.yAlign,this.width=o.width,this.height=o.height,this.caretX=i.x,this.caretY=i.y,this._resolveAnimations().update(this,l))}}draw(t){const e=this.options.setContext(this.getContext());let i=this.opacity;if(!i)return;this._updateAnimationTarget(e);const s={width:this.width,height:this.height},n={x:this.x,y:this.y};i=Math.abs(i)<.001?0:i;const o=Ne(e.padding),a=this.title.length||this.beforeBody.length||this.body.length||this.afterBody.length||this.footer.length;e.enabled&&a&&(t.save(),t.globalAlpha=i,this.drawBackground(n,t,s,e),Ii(t,e.textDirection),n.y+=o.top,this.drawTitle(n,t,e),this.drawBody(n,t,e),this.drawFooter(n,t,e),zi(t,e.textDirection),t.restore())}getActiveElements(){return this._active||[]}setActiveElements(t,e){const i=this._active,s=t.map((({datasetIndex:t,index:e})=>{const i=this.chart.getDatasetMeta(t);if(!i)throw new Error("Cannot find a dataset at index "+t);return{datasetIndex:t,element:i.data[e],index:e}})),n=!tt(i,s),o=this._positionChanged(s,e);(n||o)&&(this._active=s,this._eventPosition=e,this._ignoreReplayEvents=!0,this.update(!0))}handleEvent(t,e,i=!0){if(e&&this._ignoreReplayEvents)return!1;this._ignoreReplayEvents=!1;const s=this.options,n=this._active||[],o=this._getActiveElements(t,n,e,i),a=this._positionChanged(o,t),r=e||!tt(o,n)||a;return r&&(this._active=o,(s.enabled||s.external)&&(this._eventPosition={x:t.x,y:t.y},this.update(!0,e))),r}_getActiveElements(t,e,i,s){const n=this.options;if("mouseout"===t.type)return[];if(!s)return e;const o=this.chart.getElementsAtEventForMode(t,n.mode,n,i);return n.reverse&&o.reverse(),o}_positionChanged(t,e){const{caretX:i,caretY:s,options:n}=this,o=Ro[n.position].call(this,t,e);return!1!==o&&(i!==o.x||s!==o.y)}}$o.positioners=Ro;var Yo={id:"tooltip",_element:$o,positioners:Ro,afterInit(t,e,i){i&&(t.tooltip=new $o({chart:t,options:i}))},beforeUpdate(t,e,i){t.tooltip&&t.tooltip.initialize(i)},reset(t,e,i){t.tooltip&&t.tooltip.initialize(i)},afterDraw(t){const e=t.tooltip,i={tooltip:e};!1!==t.notifyPlugins("beforeTooltipDraw",i)&&(e&&e.draw(t.ctx),t.notifyPlugins("afterTooltipDraw",i))},afterEvent(t,e){if(t.tooltip){const i=e.replay;t.tooltip.handleEvent(e.event,i,e.inChartArea)&&(e.changed=!0)}},defaults:{enabled:!0,external:null,position:"average",backgroundColor:"rgba(0,0,0,0.8)",titleColor:"#fff",titleFont:{weight:"bold"},titleSpacing:2,titleMarginBottom:6,titleAlign:"left",bodyColor:"#fff",bodySpacing:2,bodyFont:{},bodyAlign:"left",footerColor:"#fff",footerSpacing:2,footerMarginTop:6,footerFont:{weight:"bold"},footerAlign:"left",padding:6,caretPadding:2,caretSize:5,cornerRadius:6,boxHeight:(t,e)=>e.bodyFont.size,boxWidth:(t,e)=>e.bodyFont.size,multiKeyBackground:"#fff",displayColors:!0,boxPadding:0,borderColor:"rgba(0,0,0,0)",borderWidth:0,animation:{duration:400,easing:"easeOutQuart"},animations:{numbers:{type:"number",properties:["x","y","width","height","caretX","caretY"]},opacity:{easing:"linear",duration:200}},callbacks:{beforeTitle:H,title(t){if(t.length>0){const e=t[0],i=e.chart.data.labels,s=i?i.length:0;if(this&&this.options&&"dataset"===this.options.mode)return e.dataset.label||"";if(e.label)return e.label;if(s>0&&e.dataIndex"filter"!==t&&"itemSort"!==t&&"external"!==t,_indexable:!1,callbacks:{_scriptable:!1,_indexable:!1},animation:{_fallback:!1},animations:{_fallback:"animation"}},additionalOptionScopes:["interaction"]},Uo=Object.freeze({__proto__:null,Decimation:ro,Filler:So,Legend:Co,SubTitle:Lo,Title:Ao,Tooltip:Yo});function Xo(t,e,i,s){const n=t.indexOf(e);if(-1===n)return((t,e,i,s)=>("string"==typeof e?(i=t.push(e)-1,s.unshift({index:i,label:e})):isNaN(e)&&(i=null),i))(t,e,i,s);return n!==t.lastIndexOf(e)?i:n}class qo extends Bs{constructor(t){super(t),this._startValue=void 0,this._valueRange=0,this._addedLabels=[]}init(t){const e=this._addedLabels;if(e.length){const t=this.getLabels();for(const{index:i,label:s}of e)t[i]===s&&t.splice(i,1);this._addedLabels=[]}super.init(t)}parse(t,e){if($(t))return null;const i=this.getLabels();return((t,e)=>null===t?null:jt(Math.round(t),0,e))(e=isFinite(e)&&i[e]===t?e:Xo(i,t,K(e,t),this._addedLabels),i.length-1)}determineDataLimits(){const{minDefined:t,maxDefined:e}=this.getUserBounds();let{min:i,max:s}=this.getMinMax(!0);"ticks"===this.options.bounds&&(t||(i=0),e||(s=this.getLabels().length-1)),this.min=i,this.max=s}buildTicks(){const t=this.min,e=this.max,i=this.options.offset,s=[];let n=this.getLabels();n=0===t&&e===n.length-1?n:n.slice(t,e+1),this._valueRange=Math.max(n.length-(i?0:1),1),this._startValue=this.min-(i?.5:0);for(let i=t;i<=e;i++)s.push({value:i});return s}getLabelForValue(t){const e=this.getLabels();return t>=0&&te.length-1?null:this.getPixelForValue(e[t].value)}getValueForPixel(t){return Math.round(this._startValue+this.getDecimalForPixel(t)*this._valueRange)}getBasePixel(){return this.bottom}}function Ko(t,e,{horizontal:i,minRotation:s}){const n=It(s),o=(i?Math.sin(n):Math.cos(n))||.001,a=.75*e*(""+t).length;return Math.min(e/o,a)}qo.id="category",qo.defaults={ticks:{callback:qo.prototype.getLabelForValue}};class Go extends Bs{constructor(t){super(t),this.start=void 0,this.end=void 0,this._startValue=void 0,this._endValue=void 0,this._valueRange=0}parse(t,e){return $(t)||("number"==typeof t||t instanceof Number)&&!isFinite(+t)?null:+t}handleTickRangeOptions(){const{beginAtZero:t}=this.options,{minDefined:e,maxDefined:i}=this.getUserBounds();let{min:s,max:n}=this;const o=t=>s=e?s:t,a=t=>n=i?n:t;if(t){const t=Ct(s),e=Ct(n);t<0&&e<0?a(0):t>0&&e>0&&o(0)}if(s===n){let e=1;(n>=Number.MAX_SAFE_INTEGER||s<=Number.MIN_SAFE_INTEGER)&&(e=Math.abs(.05*n)),a(n+e),t||o(s-e)}this.min=s,this.max=n}getTickLimit(){const t=this.options.ticks;let e,{maxTicksLimit:i,stepSize:s}=t;return s?(e=Math.ceil(this.max/s)-Math.floor(this.min/s)+1,e>1e3&&(console.warn(`scales.${this.id}.ticks.stepSize: ${s} would result generating up to ${e} ticks. Limiting to 1000.`),e=1e3)):(e=this.computeTickLimit(),i=i||11),i&&(e=Math.min(i,e)),e}computeTickLimit(){return Number.POSITIVE_INFINITY}buildTicks(){const t=this.options,e=t.ticks;let i=this.getTickLimit();i=Math.max(2,i);const s=function(t,e){const i=[],{bounds:s,step:n,min:o,max:a,precision:r,count:l,maxTicks:h,maxDigits:c,includeBounds:d}=t,u=n||1,f=h-1,{min:g,max:p}=e,m=!$(o),x=!$(a),b=!$(l),_=(p-g)/(c+1);let y,v,w,M,k=Ot((p-g)/f/u)*u;if(k<1e-14&&!m&&!x)return[{value:g},{value:p}];M=Math.ceil(p/k)-Math.floor(g/k),M>f&&(k=Ot(M*k/f/u)*u),$(r)||(y=Math.pow(10,r),k=Math.ceil(k*y)/y),"ticks"===s?(v=Math.floor(g/k)*k,w=Math.ceil(p/k)*k):(v=g,w=p),m&&x&&n&&Rt((a-o)/n,k/1e3)?(M=Math.round(Math.min((a-o)/k,h)),k=(a-o)/M,v=o,w=a):b?(v=m?o:v,w=x?a:w,M=l-1,k=(w-v)/M):(M=(w-v)/k,M=Lt(M,Math.round(M),k/1e3)?Math.round(M):Math.ceil(M));const S=Math.max(Ft(k),Ft(v));y=Math.pow(10,$(r)?S:r),v=Math.round(v*y)/y,w=Math.round(w*y)/y;let P=0;for(m&&(d&&v!==o?(i.push({value:o}),v0?i:null;this._zero=!0}determineDataLimits(){const{min:t,max:e}=this.getMinMax(!0);this.min=X(t)?Math.max(0,t):null,this.max=X(e)?Math.max(0,e):null,this.options.beginAtZero&&(this._zero=!0),this.handleTickRangeOptions()}handleTickRangeOptions(){const{minDefined:t,maxDefined:e}=this.getUserBounds();let i=this.min,s=this.max;const n=e=>i=t?i:e,o=t=>s=e?s:t,a=(t,e)=>Math.pow(10,Math.floor(Dt(t))+e);i===s&&(i<=0?(n(1),o(10)):(n(a(i,-1)),o(a(s,1)))),i<=0&&n(a(s,-1)),s<=0&&o(a(i,1)),this._zero&&this.min!==this._suggestedMin&&i===a(this.min,0)&&n(a(i,-1)),this.min=i,this.max=s}buildTicks(){const t=this.options,e=function(t,e){const i=Math.floor(Dt(e.max)),s=Math.ceil(e.max/Math.pow(10,i)),n=[];let o=q(t.min,Math.pow(10,Math.floor(Dt(e.min)))),a=Math.floor(Dt(o)),r=Math.floor(o/Math.pow(10,a)),l=a<0?Math.pow(10,Math.abs(a)):1;do{n.push({value:o,major:Jo(o)}),++r,10===r&&(r=1,++a,l=a>=0?1:l),o=Math.round(r*Math.pow(10,a)*l)/l}while(an?{start:e-i,end:e}:{start:e,end:e+i}}function ia(t){const e={l:t.left+t._padding.left,r:t.right-t._padding.right,t:t.top+t._padding.top,b:t.bottom-t._padding.bottom},i=Object.assign({},e),s=[],n=[],o=t._pointLabels.length,a=t.options.pointLabels,r=a.centerPointLabels?_t/o:0;for(let d=0;de.r&&(r=(s.end-e.r)/o,t.r=Math.max(t.r,e.r+r)),n.starte.b&&(l=(n.end-e.b)/a,t.b=Math.max(t.b,e.b+l))}function na(t){return 0===t||180===t?"center":t<180?"left":"right"}function oa(t,e,i){return"right"===i?t-=e:"center"===i&&(t-=e/2),t}function aa(t,e,i){return 90===i||270===i?t-=e/2:(i>270||i<90)&&(t-=e),t}function ra(t,e,i,s){const{ctx:n}=t;if(i)n.arc(t.xCenter,t.yCenter,e,0,yt);else{let i=t.getPointPosition(0,e);n.moveTo(i.x,i.y);for(let o=1;o{const i=J(this.options.pointLabels.callback,[t,e],this);return i||0===i?i:""})).filter(((t,e)=>this.chart.getDataVisibility(e)))}fit(){const t=this.options;t.display&&t.pointLabels.display?ia(this):this.setCenterPoint(0,0,0,0)}setCenterPoint(t,e,i,s){this.xCenter+=Math.floor((t-e)/2),this.yCenter+=Math.floor((i-s)/2),this.drawingArea-=Math.min(this.drawingArea/2,Math.max(t,e,i,s))}getIndexAngle(t){return Nt(t*(yt/(this._pointLabels.length||1))+It(this.options.startAngle||0))}getDistanceFromCenterForValue(t){if($(t))return NaN;const e=this.drawingArea/(this.max-this.min);return this.options.reverse?(this.max-t)*e:(t-this.min)*e}getValueForDistanceFromCenter(t){if($(t))return NaN;const e=t/(this.drawingArea/(this.max-this.min));return this.options.reverse?this.max-e:this.min+e}getPointLabelContext(t){const e=this._pointLabels||[];if(t>=0&&t=0;n--){const e=s.setContext(t.getPointLabelContext(n)),o=He(e.font),{x:a,y:r,textAlign:l,left:h,top:c,right:d,bottom:u}=t._pointLabelItems[n],{backdropColor:f}=e;if(!$(f)){const t=Ne(e.backdropPadding);i.fillStyle=f,i.fillRect(h-t.left,c-t.top,d-h+t.width,u-c+t.height)}se(i,t._pointLabels[n],a,r+o.lineHeight/2,o,{color:e.color,textAlign:l,textBaseline:"middle"})}}(this,n),s.display&&this.ticks.forEach(((t,e)=>{if(0!==e){a=this.getDistanceFromCenterForValue(t.value);!function(t,e,i,s){const n=t.ctx,o=e.circular,{color:a,lineWidth:r}=e;!o&&!s||!a||!r||i<0||(n.save(),n.strokeStyle=a,n.lineWidth=r,n.setLineDash(e.borderDash),n.lineDashOffset=e.borderDashOffset,n.beginPath(),ra(t,i,o,s),n.closePath(),n.stroke(),n.restore())}(this,s.setContext(this.getContext(e-1)),a,n)}})),i.display){for(t.save(),o=n-1;o>=0;o--){const s=i.setContext(this.getPointLabelContext(o)),{color:n,lineWidth:l}=s;l&&n&&(t.lineWidth=l,t.strokeStyle=n,t.setLineDash(s.borderDash),t.lineDashOffset=s.borderDashOffset,a=this.getDistanceFromCenterForValue(e.ticks.reverse?this.min:this.max),r=this.getPointPosition(o,a),t.beginPath(),t.moveTo(this.xCenter,this.yCenter),t.lineTo(r.x,r.y),t.stroke())}t.restore()}}drawBorder(){}drawLabels(){const t=this.ctx,e=this.options,i=e.ticks;if(!i.display)return;const s=this.getIndexAngle(0);let n,o;t.save(),t.translate(this.xCenter,this.yCenter),t.rotate(s),t.textAlign="center",t.textBaseline="middle",this.ticks.forEach(((s,a)=>{if(0===a&&!e.reverse)return;const r=i.setContext(this.getContext(a)),l=He(r.font);if(n=this.getDistanceFromCenterForValue(this.ticks[a].value),r.showLabelBackdrop){t.font=l.string,o=t.measureText(s.label).width,t.fillStyle=r.backdropColor;const e=Ne(r.backdropPadding);t.fillRect(-o/2-e.left,-n-l.size/2-e.top,o+e.width,l.size+e.height)}se(t,s.label,0,-n,l,{color:r.color})})),t.restore()}drawTitle(){}}la.id="radialLinear",la.defaults={display:!0,animate:!0,position:"chartArea",angleLines:{display:!0,lineWidth:1,borderDash:[],borderDashOffset:0},grid:{circular:!1},startAngle:0,ticks:{showLabelBackdrop:!0,callback:Os.formatters.numeric},pointLabels:{backdropColor:void 0,backdropPadding:2,display:!0,font:{size:10},callback:t=>t,padding:5,centerPointLabels:!1}},la.defaultRoutes={"angleLines.color":"borderColor","pointLabels.color":"color","ticks.color":"color"},la.descriptors={angleLines:{_fallback:"grid"}};const ha={millisecond:{common:!0,size:1,steps:1e3},second:{common:!0,size:1e3,steps:60},minute:{common:!0,size:6e4,steps:60},hour:{common:!0,size:36e5,steps:24},day:{common:!0,size:864e5,steps:30},week:{common:!1,size:6048e5,steps:4},month:{common:!0,size:2628e6,steps:12},quarter:{common:!1,size:7884e6,steps:4},year:{common:!0,size:3154e7}},ca=Object.keys(ha);function da(t,e){return t-e}function ua(t,e){if($(e))return null;const i=t._adapter,{parser:s,round:n,isoWeekday:o}=t._parseOpts;let a=e;return"function"==typeof s&&(a=s(a)),X(a)||(a="string"==typeof s?i.parse(a,s):i.parse(a)),null===a?null:(n&&(a="week"!==n||!Tt(o)&&!0!==o?i.startOf(a,n):i.startOf(a,"isoWeek",o)),+a)}function fa(t,e,i,s){const n=ca.length;for(let o=ca.indexOf(t);o=e?i[s]:i[n]]=!0}}else t[e]=!0}function pa(t,e,i){const s=[],n={},o=e.length;let a,r;for(a=0;a=0&&(e[l].major=!0);return e}(t,s,n,i):s}class ma extends Bs{constructor(t){super(t),this._cache={data:[],labels:[],all:[]},this._unit="day",this._majorUnit=void 0,this._offsets={},this._normalized=!1,this._parseOpts=void 0}init(t,e){const i=t.time||(t.time={}),s=this._adapter=new mn._date(t.adapters.date);ot(i.displayFormats,s.formats()),this._parseOpts={parser:i.parser,round:i.round,isoWeekday:i.isoWeekday},super.init(t),this._normalized=e.normalized}parse(t,e){return void 0===t?null:ua(this,t)}beforeLayout(){super.beforeLayout(),this._cache={data:[],labels:[],all:[]}}determineDataLimits(){const t=this.options,e=this._adapter,i=t.time.unit||"day";let{min:s,max:n,minDefined:o,maxDefined:a}=this.getUserBounds();function r(t){o||isNaN(t.min)||(s=Math.min(s,t.min)),a||isNaN(t.max)||(n=Math.max(n,t.max))}o&&a||(r(this._getLabelBounds()),"ticks"===t.bounds&&"labels"===t.ticks.source||r(this.getMinMax(!1))),s=X(s)&&!isNaN(s)?s:+e.startOf(Date.now(),i),n=X(n)&&!isNaN(n)?n:+e.endOf(Date.now(),i)+1,this.min=Math.min(s,n-1),this.max=Math.max(s+1,n)}_getLabelBounds(){const t=this.getLabelTimestamps();let e=Number.POSITIVE_INFINITY,i=Number.NEGATIVE_INFINITY;return t.length&&(e=t[0],i=t[t.length-1]),{min:e,max:i}}buildTicks(){const t=this.options,e=t.time,i=t.ticks,s="labels"===i.source?this.getLabelTimestamps():this._generate();"ticks"===t.bounds&&s.length&&(this.min=this._userMin||s[0],this.max=this._userMax||s[s.length-1]);const n=this.min,o=he(s,n,this.max);return this._unit=e.unit||(i.autoSkip?fa(e.minUnit,this.min,this.max,this._getLabelCapacity(n)):function(t,e,i,s,n){for(let o=ca.length-1;o>=ca.indexOf(i);o--){const i=ca[o];if(ha[i].common&&t._adapter.diff(n,s,i)>=e-1)return i}return ca[i?ca.indexOf(i):0]}(this,o.length,e.minUnit,this.min,this.max)),this._majorUnit=i.major.enabled&&"year"!==this._unit?function(t){for(let e=ca.indexOf(t)+1,i=ca.length;e1e5*a)throw new Error(e+" and "+i+" are too far apart with stepSize of "+a+" "+o);const f="data"===s.ticks.source&&this.getDataTimestamps();for(c=u,d=0;ct-e)).map((t=>+t))}getLabelForValue(t){const e=this._adapter,i=this.options.time;return i.tooltipFormat?e.format(t,i.tooltipFormat):e.format(t,i.displayFormats.datetime)}_tickFormatFunction(t,e,i,s){const n=this.options,o=n.time.displayFormats,a=this._unit,r=this._majorUnit,l=a&&o[a],h=r&&o[r],c=i[e],d=r&&h&&c&&c.major,u=this._adapter.format(t,s||(d?h:l)),f=n.ticks.callback;return f?J(f,[u,e,i],this):u}generateTickLabels(t){let e,i,s;for(e=0,i=t.length;e0?a:1}getDataTimestamps(){let t,e,i=this._cache.data||[];if(i.length)return i;const s=this.getMatchingVisibleMetas();if(this._normalized&&s.length)return this._cache.data=s[0].controller.getAllParsedValues(this);for(t=0,e=s.length;t=t[r].pos&&e<=t[l].pos&&({lo:r,hi:l}=re(t,"pos",e)),({pos:s,time:o}=t[r]),({pos:n,time:a}=t[l])):(e>=t[r].time&&e<=t[l].time&&({lo:r,hi:l}=re(t,"time",e)),({time:s,pos:o}=t[r]),({time:n,pos:a}=t[l]));const h=n-s;return h?o+(a-o)*(e-s)/h:o}ma.id="time",ma.defaults={bounds:"data",adapters:{},time:{parser:!1,unit:!1,round:!1,isoWeekday:!1,minUnit:"millisecond",displayFormats:{}},ticks:{source:"auto",major:{enabled:!1}}};class ba extends ma{constructor(t){super(t),this._table=[],this._minPos=void 0,this._tableRange=void 0}initOffsets(){const t=this._getTimestampsForTable(),e=this._table=this.buildLookupTable(t);this._minPos=xa(e,this.min),this._tableRange=xa(e,this.max)-this._minPos,super.initOffsets(t)}buildLookupTable(t){const{min:e,max:i}=this,s=[],n=[];let o,a,r,l,h;for(o=0,a=t.length;o=e&&l<=i&&s.push(l);if(s.length<2)return[{time:e,pos:0},{time:i,pos:1}];for(o=0,a=s.length;oMath.max(Math.min(t,i),e);function _t(t){return xt(bt(2.55*t),0,255)}function yt(t){return xt(bt(255*t),0,255)}function vt(t){return xt(bt(t/2.55)/100,0,1)}function wt(t){return xt(bt(100*t),0,100)}const Mt={0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,A:10,B:11,C:12,D:13,E:14,F:15,a:10,b:11,c:12,d:13,e:14,f:15},kt=[..."0123456789ABCDEF"],St=t=>kt[15&t],Pt=t=>kt[(240&t)>>4]+kt[15&t],Dt=t=>(240&t)>>4==(15&t);function Ot(t){var e=(t=>Dt(t.r)&&Dt(t.g)&&Dt(t.b)&&Dt(t.a))(t)?St:Pt;return t?"#"+e(t.r)+e(t.g)+e(t.b)+((t,e)=>t<255?e(t):"")(t.a,e):void 0}const Ct=/^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/;function At(t,e,i){const s=e*Math.min(i,1-i),n=(e,n=(e+t/30)%12)=>i-s*Math.max(Math.min(n-3,9-n,1),-1);return[n(0),n(8),n(4)]}function Tt(t,e,i){const s=(s,n=(s+t/60)%6)=>i-i*e*Math.max(Math.min(n,4-n,1),0);return[s(5),s(3),s(1)]}function Lt(t,e,i){const s=At(t,1,.5);let n;for(e+i>1&&(n=1/(e+i),e*=n,i*=n),n=0;n<3;n++)s[n]*=1-e-i,s[n]+=e;return s}function Et(t){const e=t.r/255,i=t.g/255,s=t.b/255,n=Math.max(e,i,s),o=Math.min(e,i,s),a=(n+o)/2;let r,l,h;return n!==o&&(h=n-o,l=a>.5?h/(2-n-o):h/(n+o),r=function(t,e,i,s,n){return t===n?(e-i)/s+(e>16&255,o>>8&255,255&o]}return t}(),Nt.transparent=[0,0,0,0]);const e=Nt[t.toLowerCase()];return e&&{r:e[0],g:e[1],b:e[2],a:4===e.length?e[3]:255}}const jt=/^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/;const Ht=t=>t<=.0031308?12.92*t:1.055*Math.pow(t,1/2.4)-.055,$t=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4);function Yt(t,e,i){if(t){let s=Et(t);s[e]=Math.max(0,Math.min(s[e]+s[e]*i,0===e?360:1)),s=It(s),t.r=s[0],t.g=s[1],t.b=s[2]}}function Ut(t,e){return t?Object.assign(e||{},t):t}function Xt(t){var e={r:0,g:0,b:0,a:255};return Array.isArray(t)?t.length>=3&&(e={r:t[0],g:t[1],b:t[2],a:255},t.length>3&&(e.a=yt(t[3]))):(e=Ut(t,{r:0,g:0,b:0,a:1})).a=yt(e.a),e}function qt(t){return"r"===t.charAt(0)?function(t){const e=jt.exec(t);let i,s,n,o=255;if(e){if(e[7]!==i){const t=+e[7];o=e[8]?_t(t):xt(255*t,0,255)}return i=+e[1],s=+e[3],n=+e[5],i=255&(e[2]?_t(i):xt(i,0,255)),s=255&(e[4]?_t(s):xt(s,0,255)),n=255&(e[6]?_t(n):xt(n,0,255)),{r:i,g:s,b:n,a:o}}}(t):Ft(t)}class Kt{constructor(t){if(t instanceof Kt)return t;const e=typeof t;let i;var s,n,o;"object"===e?i=Xt(t):"string"===e&&(o=(s=t).length,"#"===s[0]&&(4===o||5===o?n={r:255&17*Mt[s[1]],g:255&17*Mt[s[2]],b:255&17*Mt[s[3]],a:5===o?17*Mt[s[4]]:255}:7!==o&&9!==o||(n={r:Mt[s[1]]<<4|Mt[s[2]],g:Mt[s[3]]<<4|Mt[s[4]],b:Mt[s[5]]<<4|Mt[s[6]],a:9===o?Mt[s[7]]<<4|Mt[s[8]]:255})),i=n||Wt(t)||qt(t)),this._rgb=i,this._valid=!!i}get valid(){return this._valid}get rgb(){var t=Ut(this._rgb);return t&&(t.a=vt(t.a)),t}set rgb(t){this._rgb=Xt(t)}rgbString(){return this._valid?(t=this._rgb)&&(t.a<255?`rgba(${t.r}, ${t.g}, ${t.b}, ${vt(t.a)})`:`rgb(${t.r}, ${t.g}, ${t.b})`):void 0;var t}hexString(){return this._valid?Ot(this._rgb):void 0}hslString(){return this._valid?function(t){if(!t)return;const e=Et(t),i=e[0],s=wt(e[1]),n=wt(e[2]);return t.a<255?`hsla(${i}, ${s}%, ${n}%, ${vt(t.a)})`:`hsl(${i}, ${s}%, ${n}%)`}(this._rgb):void 0}mix(t,e){if(t){const i=this.rgb,s=t.rgb;let n;const o=e===n?.5:e,a=2*o-1,r=i.a-s.a,l=((a*r==-1?a:(a+r)/(1+a*r))+1)/2;n=1-l,i.r=255&l*i.r+n*s.r+.5,i.g=255&l*i.g+n*s.g+.5,i.b=255&l*i.b+n*s.b+.5,i.a=o*i.a+(1-o)*s.a,this.rgb=i}return this}interpolate(t,e){return t&&(this._rgb=function(t,e,i){const s=$t(vt(t.r)),n=$t(vt(t.g)),o=$t(vt(t.b));return{r:yt(Ht(s+i*($t(vt(e.r))-s))),g:yt(Ht(n+i*($t(vt(e.g))-n))),b:yt(Ht(o+i*($t(vt(e.b))-o))),a:t.a+i*(e.a-t.a)}}(this._rgb,t._rgb,e)),this}clone(){return new Kt(this.rgb)}alpha(t){return this._rgb.a=yt(t),this}clearer(t){return this._rgb.a*=1-t,this}greyscale(){const t=this._rgb,e=bt(.3*t.r+.59*t.g+.11*t.b);return t.r=t.g=t.b=e,this}opaquer(t){return this._rgb.a*=1+t,this}negate(){const t=this._rgb;return t.r=255-t.r,t.g=255-t.g,t.b=255-t.b,this}lighten(t){return Yt(this._rgb,2,t),this}darken(t){return Yt(this._rgb,2,-t),this}saturate(t){return Yt(this._rgb,1,t),this}desaturate(t){return Yt(this._rgb,1,-t),this}rotate(t){return function(t,e){var i=Et(t);i[0]=zt(i[0]+e),i=It(i),t.r=i[0],t.g=i[1],t.b=i[2]}(this._rgb,t),this}}function Gt(t){return new Kt(t)}function Zt(t){if(t&&"object"==typeof t){const e=t.toString();return"[object CanvasPattern]"===e||"[object CanvasGradient]"===e}return!1}function Jt(t){return Zt(t)?t:Gt(t)}function Qt(t){return Zt(t)?t:Gt(t).saturate(.5).darken(.1).hexString()}const te=Object.create(null),ee=Object.create(null);function ie(t,e){if(!e)return t;const i=e.split(".");for(let e=0,s=i.length;et.chart.platform.getDevicePixelRatio(),this.elements={},this.events=["mousemove","mouseout","click","touchstart","touchmove"],this.font={family:"'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",size:12,style:"normal",lineHeight:1.2,weight:null},this.hover={},this.hoverBackgroundColor=(t,e)=>Qt(e.backgroundColor),this.hoverBorderColor=(t,e)=>Qt(e.borderColor),this.hoverColor=(t,e)=>Qt(e.color),this.indexAxis="x",this.interaction={mode:"nearest",intersect:!0,includeInvisible:!1},this.maintainAspectRatio=!0,this.onHover=null,this.onClick=null,this.parsing=!0,this.plugins={},this.responsive=!0,this.scale=void 0,this.scales={},this.showLine=!0,this.drawActiveElementsOnTop=!0,this.describe(t)}set(t,e){return se(this,t,e)}get(t){return ie(this,t)}describe(t,e){return se(ee,t,e)}override(t,e){return se(te,t,e)}route(t,e,i,s){const o=ie(this,t),a=ie(this,i),l="_"+e;Object.defineProperties(o,{[l]:{value:o[e],writable:!0},[e]:{enumerable:!0,get(){const t=this[l],e=a[s];return n(t)?Object.assign({},e,t):r(t,e)},set(t){this[l]=t}}})}}({_scriptable:t=>!t.startsWith("on"),_indexable:t=>"events"!==t,hover:{_fallback:"interaction"},interaction:{_scriptable:!1,_indexable:!1}});function oe(){return"undefined"!=typeof window&&"undefined"!=typeof document}function ae(t){let e=t.parentNode;return e&&"[object ShadowRoot]"===e.toString()&&(e=e.host),e}function re(t,e,i){let s;return"string"==typeof t?(s=parseInt(t,10),-1!==t.indexOf("%")&&(s=s/100*e.parentNode[i])):s=t,s}const le=t=>window.getComputedStyle(t,null);function he(t,e){return le(t).getPropertyValue(e)}const ce=["top","right","bottom","left"];function de(t,e,i){const s={};i=i?"-"+i:"";for(let n=0;n<4;n++){const o=ce[n];s[o]=parseFloat(t[e+"-"+o+i])||0}return s.width=s.left+s.right,s.height=s.top+s.bottom,s}function ue(t,e){if("native"in t)return t;const{canvas:i,currentDevicePixelRatio:s}=e,n=le(i),o="border-box"===n.boxSizing,a=de(n,"padding"),r=de(n,"border","width"),{x:l,y:h,box:c}=function(t,e){const i=t.touches,s=i&&i.length?i[0]:t,{offsetX:n,offsetY:o}=s;let a,r,l=!1;if(((t,e,i)=>(t>0||e>0)&&(!i||!i.shadowRoot))(n,o,t.target))a=n,r=o;else{const t=e.getBoundingClientRect();a=s.clientX-t.left,r=s.clientY-t.top,l=!0}return{x:a,y:r,box:l}}(t,i),d=a.left+(c&&r.left),u=a.top+(c&&r.top);let{width:f,height:g}=e;return o&&(f-=a.width+r.width,g-=a.height+r.height),{x:Math.round((l-d)/f*i.width/s),y:Math.round((h-u)/g*i.height/s)}}const fe=t=>Math.round(10*t)/10;function ge(t,e,i,s){const n=le(t),o=de(n,"margin"),a=re(n.maxWidth,t,"clientWidth")||A,r=re(n.maxHeight,t,"clientHeight")||A,l=function(t,e,i){let s,n;if(void 0===e||void 0===i){const o=ae(t);if(o){const t=o.getBoundingClientRect(),a=le(o),r=de(a,"border","width"),l=de(a,"padding");e=t.width-l.width-r.width,i=t.height-l.height-r.height,s=re(a.maxWidth,o,"clientWidth"),n=re(a.maxHeight,o,"clientHeight")}else e=t.clientWidth,i=t.clientHeight}return{width:e,height:i,maxWidth:s||A,maxHeight:n||A}}(t,e,i);let{width:h,height:c}=l;if("content-box"===n.boxSizing){const t=de(n,"border","width"),e=de(n,"padding");h-=e.width+t.width,c-=e.height+t.height}return h=Math.max(0,h-o.width),c=Math.max(0,s?Math.floor(h/s):c-o.height),h=fe(Math.min(h,a,l.maxWidth)),c=fe(Math.min(c,r,l.maxHeight)),h&&!c&&(c=fe(h/2)),{width:h,height:c}}function pe(t,e,i){const s=e||1,n=Math.floor(t.height*s),o=Math.floor(t.width*s);t.height=n/s,t.width=o/s;const a=t.canvas;return a.style&&(i||!a.style.height&&!a.style.width)&&(a.style.height=`${t.height}px`,a.style.width=`${t.width}px`),(t.currentDevicePixelRatio!==s||a.height!==n||a.width!==o)&&(t.currentDevicePixelRatio=s,a.height=n,a.width=o,t.ctx.setTransform(s,0,0,s,0,0),!0)}const me=function(){let t=!1;try{const e={get passive(){return t=!0,!1}};window.addEventListener("test",null,e),window.removeEventListener("test",null,e)}catch(t){}return t}();function be(t,e){const i=he(t,e),s=i&&i.match(/^(\d+)(\.\d+)?px$/);return s?+s[1]:void 0}function xe(t){return!t||i(t.size)||i(t.family)?null:(t.style?t.style+" ":"")+(t.weight?t.weight+" ":"")+t.size+"px "+t.family}function _e(t,e,i,s,n){let o=e[n];return o||(o=e[n]=t.measureText(n).width,i.push(n)),o>s&&(s=o),s}function ye(t,e,i,n){let o=(n=n||{}).data=n.data||{},a=n.garbageCollect=n.garbageCollect||[];n.font!==e&&(o=n.data={},a=n.garbageCollect=[],n.font=e),t.save(),t.font=e;let r=0;const l=i.length;let h,c,d,u,f;for(h=0;hi.length){for(h=0;h0&&t.stroke()}}function Se(t,e,i){return i=i||.5,!e||t&&t.x>e.left-i&&t.xe.top-i&&t.y0&&""!==r.strokeColor;let c,d;for(t.save(),t.font=a.string,function(t,e){e.translation&&t.translate(e.translation[0],e.translation[1]);i(e.rotation)||t.rotate(e.rotation);e.color&&(t.fillStyle=e.color);e.textAlign&&(t.textAlign=e.textAlign);e.textBaseline&&(t.textBaseline=e.textBaseline)}(t,r),c=0;ct[0])){M(s)||(s=$e("_fallback",t));const o={[Symbol.toStringTag]:"Object",_cacheable:!0,_scopes:t,_rootScopes:i,_fallback:s,_getTarget:n,override:n=>Ee([n,...t],e,i,s)};return new Proxy(o,{deleteProperty:(e,i)=>(delete e[i],delete e._keys,delete t[0][i],!0),get:(i,s)=>Ve(i,s,(()=>function(t,e,i,s){let n;for(const o of e)if(n=$e(ze(o,t),i),M(n))return Fe(t,n)?je(i,s,t,n):n}(s,e,t,i))),getOwnPropertyDescriptor:(t,e)=>Reflect.getOwnPropertyDescriptor(t._scopes[0],e),getPrototypeOf:()=>Reflect.getPrototypeOf(t[0]),has:(t,e)=>Ye(t).includes(e),ownKeys:t=>Ye(t),set(t,e,i){const s=t._storage||(t._storage=n());return t[e]=s[e]=i,delete t._keys,!0}})}function Re(t,e,i,o){const a={_cacheable:!1,_proxy:t,_context:e,_subProxy:i,_stack:new Set,_descriptors:Ie(t,o),setContext:e=>Re(t,e,i,o),override:s=>Re(t.override(s),e,i,o)};return new Proxy(a,{deleteProperty:(e,i)=>(delete e[i],delete t[i],!0),get:(t,e,i)=>Ve(t,e,(()=>function(t,e,i){const{_proxy:o,_context:a,_subProxy:r,_descriptors:l}=t;let h=o[e];k(h)&&l.isScriptable(e)&&(h=function(t,e,i,s){const{_proxy:n,_context:o,_subProxy:a,_stack:r}=i;if(r.has(t))throw new Error("Recursion detected: "+Array.from(r).join("->")+"->"+t);r.add(t),e=e(o,a||s),r.delete(t),Fe(t,e)&&(e=je(n._scopes,n,t,e));return e}(e,h,t,i));s(h)&&h.length&&(h=function(t,e,i,s){const{_proxy:o,_context:a,_subProxy:r,_descriptors:l}=i;if(M(a.index)&&s(t))e=e[a.index%e.length];else if(n(e[0])){const i=e,s=o._scopes.filter((t=>t!==i));e=[];for(const n of i){const i=je(s,o,t,n);e.push(Re(i,a,r&&r[t],l))}}return e}(e,h,t,l.isIndexable));Fe(e,h)&&(h=Re(h,a,r&&r[e],l));return h}(t,e,i))),getOwnPropertyDescriptor:(e,i)=>e._descriptors.allKeys?Reflect.has(t,i)?{enumerable:!0,configurable:!0}:void 0:Reflect.getOwnPropertyDescriptor(t,i),getPrototypeOf:()=>Reflect.getPrototypeOf(t),has:(e,i)=>Reflect.has(t,i),ownKeys:()=>Reflect.ownKeys(t),set:(e,i,s)=>(t[i]=s,delete e[i],!0)})}function Ie(t,e={scriptable:!0,indexable:!0}){const{_scriptable:i=e.scriptable,_indexable:s=e.indexable,_allKeys:n=e.allKeys}=t;return{allKeys:n,scriptable:i,indexable:s,isScriptable:k(i)?i:()=>i,isIndexable:k(s)?s:()=>s}}const ze=(t,e)=>t?t+w(e):e,Fe=(t,e)=>n(e)&&"adapters"!==t&&(null===Object.getPrototypeOf(e)||e.constructor===Object);function Ve(t,e,i){if(Object.prototype.hasOwnProperty.call(t,e))return t[e];const s=i();return t[e]=s,s}function Be(t,e,i){return k(t)?t(e,i):t}const Ne=(t,e)=>!0===t?e:"string"==typeof t?y(e,t):void 0;function We(t,e,i,s,n){for(const o of e){const e=Ne(i,o);if(e){t.add(e);const o=Be(e._fallback,i,n);if(M(o)&&o!==i&&o!==s)return o}else if(!1===e&&M(s)&&i!==s)return null}return!1}function je(t,e,i,o){const a=e._rootScopes,r=Be(e._fallback,i,o),l=[...t,...a],h=new Set;h.add(o);let c=He(h,l,i,r||i,o);return null!==c&&((!M(r)||r===i||(c=He(h,l,r,c,o),null!==c))&&Ee(Array.from(h),[""],a,r,(()=>function(t,e,i){const o=t._getTarget();e in o||(o[e]={});const a=o[e];if(s(a)&&n(i))return i;return a}(e,i,o))))}function He(t,e,i,s,n){for(;i;)i=We(t,e,i,s,n);return i}function $e(t,e){for(const i of e){if(!i)continue;const e=i[t];if(M(e))return e}}function Ye(t){let e=t._keys;return e||(e=t._keys=function(t){const e=new Set;for(const i of t)for(const t of Object.keys(i).filter((t=>!t.startsWith("_"))))e.add(t);return Array.from(e)}(t._scopes)),e}function Ue(t,e,i,s){const{iScale:n}=t,{key:o="r"}=this._parsing,a=new Array(s);let r,l,h,c;for(r=0,l=s;re"x"===t?"y":"x";function Ge(t,e,i,s){const n=t.skip?e:t,o=e,a=i.skip?e:i,r=X(o,n),l=X(a,o);let h=r/(r+l),c=l/(r+l);h=isNaN(h)?0:h,c=isNaN(c)?0:c;const d=s*h,u=s*c;return{previous:{x:o.x-d*(a.x-n.x),y:o.y-d*(a.y-n.y)},next:{x:o.x+u*(a.x-n.x),y:o.y+u*(a.y-n.y)}}}function Ze(t,e="x"){const i=Ke(e),s=t.length,n=Array(s).fill(0),o=Array(s);let a,r,l,h=qe(t,0);for(a=0;a!t.skip))),"monotone"===e.cubicInterpolationMode)Ze(t,n);else{let i=s?t[t.length-1]:t[0];for(o=0,a=t.length;o0===t||1===t,ei=(t,e,i)=>-Math.pow(2,10*(t-=1))*Math.sin((t-e)*O/i),ii=(t,e,i)=>Math.pow(2,-10*t)*Math.sin((t-e)*O/i)+1,si={linear:t=>t,easeInQuad:t=>t*t,easeOutQuad:t=>-t*(t-2),easeInOutQuad:t=>(t/=.5)<1?.5*t*t:-.5*(--t*(t-2)-1),easeInCubic:t=>t*t*t,easeOutCubic:t=>(t-=1)*t*t+1,easeInOutCubic:t=>(t/=.5)<1?.5*t*t*t:.5*((t-=2)*t*t+2),easeInQuart:t=>t*t*t*t,easeOutQuart:t=>-((t-=1)*t*t*t-1),easeInOutQuart:t=>(t/=.5)<1?.5*t*t*t*t:-.5*((t-=2)*t*t*t-2),easeInQuint:t=>t*t*t*t*t,easeOutQuint:t=>(t-=1)*t*t*t*t+1,easeInOutQuint:t=>(t/=.5)<1?.5*t*t*t*t*t:.5*((t-=2)*t*t*t*t+2),easeInSine:t=>1-Math.cos(t*L),easeOutSine:t=>Math.sin(t*L),easeInOutSine:t=>-.5*(Math.cos(D*t)-1),easeInExpo:t=>0===t?0:Math.pow(2,10*(t-1)),easeOutExpo:t=>1===t?1:1-Math.pow(2,-10*t),easeInOutExpo:t=>ti(t)?t:t<.5?.5*Math.pow(2,10*(2*t-1)):.5*(2-Math.pow(2,-10*(2*t-1))),easeInCirc:t=>t>=1?t:-(Math.sqrt(1-t*t)-1),easeOutCirc:t=>Math.sqrt(1-(t-=1)*t),easeInOutCirc:t=>(t/=.5)<1?-.5*(Math.sqrt(1-t*t)-1):.5*(Math.sqrt(1-(t-=2)*t)+1),easeInElastic:t=>ti(t)?t:ei(t,.075,.3),easeOutElastic:t=>ti(t)?t:ii(t,.075,.3),easeInOutElastic(t){const e=.1125;return ti(t)?t:t<.5?.5*ei(2*t,e,.45):.5+.5*ii(2*t-1,e,.45)},easeInBack(t){const e=1.70158;return t*t*((e+1)*t-e)},easeOutBack(t){const e=1.70158;return(t-=1)*t*((e+1)*t+e)+1},easeInOutBack(t){let e=1.70158;return(t/=.5)<1?t*t*((1+(e*=1.525))*t-e)*.5:.5*((t-=2)*t*((1+(e*=1.525))*t+e)+2)},easeInBounce:t=>1-si.easeOutBounce(1-t),easeOutBounce(t){const e=7.5625,i=2.75;return t<1/i?e*t*t:t<2/i?e*(t-=1.5/i)*t+.75:t<2.5/i?e*(t-=2.25/i)*t+.9375:e*(t-=2.625/i)*t+.984375},easeInOutBounce:t=>t<.5?.5*si.easeInBounce(2*t):.5*si.easeOutBounce(2*t-1)+.5};function ni(t,e,i,s){return{x:t.x+i*(e.x-t.x),y:t.y+i*(e.y-t.y)}}function oi(t,e,i,s){return{x:t.x+i*(e.x-t.x),y:"middle"===s?i<.5?t.y:e.y:"after"===s?i<1?t.y:e.y:i>0?e.y:t.y}}function ai(t,e,i,s){const n={x:t.cp2x,y:t.cp2y},o={x:e.cp1x,y:e.cp1y},a=ni(t,n,i),r=ni(n,o,i),l=ni(o,e,i),h=ni(a,r,i),c=ni(r,l,i);return ni(h,c,i)}const ri=new Map;function li(t,e,i){return function(t,e){e=e||{};const i=t+JSON.stringify(e);let s=ri.get(i);return s||(s=new Intl.NumberFormat(t,e),ri.set(i,s)),s}(e,i).format(t)}const hi=new RegExp(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/),ci=new RegExp(/^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/);function di(t,e){const i=(""+t).match(hi);if(!i||"normal"===i[1])return 1.2*e;switch(t=+i[2],i[3]){case"px":return t;case"%":t/=100}return e*t}function ui(t,e){const i={},s=n(e),o=s?Object.keys(e):e,a=n(t)?s?i=>r(t[i],t[e[i]]):e=>t[e]:()=>t;for(const t of o)i[t]=+a(t)||0;return i}function fi(t){return ui(t,{top:"y",right:"x",bottom:"y",left:"x"})}function gi(t){return ui(t,["topLeft","topRight","bottomLeft","bottomRight"])}function pi(t){const e=fi(t);return e.width=e.left+e.right,e.height=e.top+e.bottom,e}function mi(t,e){t=t||{},e=e||ne.font;let i=r(t.size,e.size);"string"==typeof i&&(i=parseInt(i,10));let s=r(t.style,e.style);s&&!(""+s).match(ci)&&(console.warn('Invalid font style specified: "'+s+'"'),s="");const n={family:r(t.family,e.family),lineHeight:di(r(t.lineHeight,e.lineHeight),i),size:i,style:s,weight:r(t.weight,e.weight),string:""};return n.string=xe(n),n}function bi(t,e,i,n){let o,a,r,l=!0;for(o=0,a=t.length;oi&&0===t?0:t+e;return{min:a(s,-Math.abs(o)),max:a(n,o)}}function _i(t,e){return Object.assign(Object.create(t),e)}function yi(t,e,i){return t?function(t,e){return{x:i=>t+t+e-i,setWidth(t){e=t},textAlign:t=>"center"===t?t:"right"===t?"left":"right",xPlus:(t,e)=>t-e,leftForLtr:(t,e)=>t-e}}(e,i):{x:t=>t,setWidth(t){},textAlign:t=>t,xPlus:(t,e)=>t+e,leftForLtr:(t,e)=>t}}function vi(t,e){let i,s;"ltr"!==e&&"rtl"!==e||(i=t.canvas.style,s=[i.getPropertyValue("direction"),i.getPropertyPriority("direction")],i.setProperty("direction",e,"important"),t.prevTextDirection=s)}function wi(t,e){void 0!==e&&(delete t.prevTextDirection,t.canvas.style.setProperty("direction",e[0],e[1]))}function Mi(t){return"angle"===t?{between:G,compare:q,normalize:K}:{between:Q,compare:(t,e)=>t-e,normalize:t=>t}}function ki({start:t,end:e,count:i,loop:s,style:n}){return{start:t%i,end:e%i,loop:s&&(e-t+1)%i==0,style:n}}function Si(t,e,i){if(!i)return[t];const{property:s,start:n,end:o}=i,a=e.length,{compare:r,between:l,normalize:h}=Mi(s),{start:c,end:d,loop:u,style:f}=function(t,e,i){const{property:s,start:n,end:o}=i,{between:a,normalize:r}=Mi(s),l=e.length;let h,c,{start:d,end:u,loop:f}=t;if(f){for(d+=l,u+=l,h=0,c=l;hx||l(n,b,p)&&0!==r(n,b),v=()=>!x||0===r(o,p)||l(o,b,p);for(let t=c,i=c;t<=d;++t)m=e[t%a],m.skip||(p=h(m[s]),p!==b&&(x=l(p,n,o),null===_&&y()&&(_=0===r(p,n)?t:i),null!==_&&v()&&(g.push(ki({start:_,end:t,loop:u,count:a,style:f})),_=null),i=t,b=p));return null!==_&&g.push(ki({start:_,end:d,loop:u,count:a,style:f})),g}function Pi(t,e){const i=[],s=t.segments;for(let n=0;nn&&t[o%e].skip;)o--;return o%=e,{start:n,end:o}}(i,n,o,s);if(!0===s)return Oi(t,[{start:a,end:r,loop:o}],i,e);return Oi(t,function(t,e,i,s){const n=t.length,o=[];let a,r=e,l=t[e];for(a=e+1;a<=i;++a){const i=t[a%n];i.skip||i.stop?l.skip||(s=!1,o.push({start:e%n,end:(a-1)%n,loop:s}),e=r=i.stop?a:null):(r=a,l.skip&&(e=a)),l=i}return null!==r&&o.push({start:e%n,end:r%n,loop:s}),o}(i,a,r{t[a](e[i],n)&&(o.push({element:t,datasetIndex:s,index:l}),r=r||t.inRange(e.x,e.y,n))})),s&&!r?[]:o}var Vi={evaluateInteractionItems:Ei,modes:{index(t,e,i,s){const n=ue(e,t),o=i.axis||"x",a=i.includeInvisible||!1,r=i.intersect?Ri(t,n,o,s,a):zi(t,n,o,!1,s,a),l=[];return r.length?(t.getSortedVisibleDatasetMetas().forEach((t=>{const e=r[0].index,i=t.data[e];i&&!i.skip&&l.push({element:i,datasetIndex:t.index,index:e})})),l):[]},dataset(t,e,i,s){const n=ue(e,t),o=i.axis||"xy",a=i.includeInvisible||!1;let r=i.intersect?Ri(t,n,o,s,a):zi(t,n,o,!1,s,a);if(r.length>0){const e=r[0].datasetIndex,i=t.getDatasetMeta(e).data;r=[];for(let t=0;tRi(t,ue(e,t),i.axis||"xy",s,i.includeInvisible||!1),nearest(t,e,i,s){const n=ue(e,t),o=i.axis||"xy",a=i.includeInvisible||!1;return zi(t,n,o,i.intersect,s,a)},x:(t,e,i,s)=>Fi(t,ue(e,t),"x",i.intersect,s),y:(t,e,i,s)=>Fi(t,ue(e,t),"y",i.intersect,s)}};const Bi=["left","top","right","bottom"];function Ni(t,e){return t.filter((t=>t.pos===e))}function Wi(t,e){return t.filter((t=>-1===Bi.indexOf(t.pos)&&t.box.axis===e))}function ji(t,e){return t.sort(((t,i)=>{const s=e?i:t,n=e?t:i;return s.weight===n.weight?s.index-n.index:s.weight-n.weight}))}function Hi(t,e){const i=function(t){const e={};for(const i of t){const{stack:t,pos:s,stackWeight:n}=i;if(!t||!Bi.includes(s))continue;const o=e[t]||(e[t]={count:0,placed:0,weight:0,size:0});o.count++,o.weight+=n}return e}(t),{vBoxMaxWidth:s,hBoxMaxHeight:n}=e;let o,a,r;for(o=0,a=t.length;o{s[t]=Math.max(e[t],i[t])})),s}return s(t?["left","right"]:["top","bottom"])}function qi(t,e,i,s){const n=[];let o,a,r,l,h,c;for(o=0,a=t.length,h=0;ot.box.fullSize)),!0),s=ji(Ni(e,"left"),!0),n=ji(Ni(e,"right")),o=ji(Ni(e,"top"),!0),a=ji(Ni(e,"bottom")),r=Wi(e,"x"),l=Wi(e,"y");return{fullSize:i,leftAndTop:s.concat(o),rightAndBottom:n.concat(l).concat(a).concat(r),chartArea:Ni(e,"chartArea"),vertical:s.concat(n).concat(l),horizontal:o.concat(a).concat(r)}}(t.boxes),l=r.vertical,h=r.horizontal;d(t.boxes,(t=>{"function"==typeof t.beforeLayout&&t.beforeLayout()}));const c=l.reduce(((t,e)=>e.box.options&&!1===e.box.options.display?t:t+1),0)||1,u=Object.freeze({outerWidth:e,outerHeight:i,padding:n,availableWidth:o,availableHeight:a,vBoxMaxWidth:o/2/c,hBoxMaxHeight:a/2}),f=Object.assign({},n);Yi(f,pi(s));const g=Object.assign({maxPadding:f,w:o,h:a,x:n.left,y:n.top},n),p=Hi(l.concat(h),u);qi(r.fullSize,g,u,p),qi(l,g,u,p),qi(h,g,u,p)&&qi(l,g,u,p),function(t){const e=t.maxPadding;function i(i){const s=Math.max(e[i]-t[i],0);return t[i]+=s,s}t.y+=i("top"),t.x+=i("left"),i("right"),i("bottom")}(g),Gi(r.leftAndTop,g,u,p),g.x+=g.w,g.y+=g.h,Gi(r.rightAndBottom,g,u,p),t.chartArea={left:g.left,top:g.top,right:g.left+g.w,bottom:g.top+g.h,height:g.h,width:g.w},d(r.chartArea,(e=>{const i=e.box;Object.assign(i,t.chartArea),i.update(g.w,g.h,{left:0,top:0,right:0,bottom:0})}))}};class Ji{acquireContext(t,e){}releaseContext(t){return!1}addEventListener(t,e,i){}removeEventListener(t,e,i){}getDevicePixelRatio(){return 1}getMaximumSize(t,e,i,s){return e=Math.max(0,e||t.width),i=i||t.height,{width:e,height:Math.max(0,s?Math.floor(e/s):i)}}isAttached(t){return!0}updateConfig(t){}}class Qi extends Ji{acquireContext(t){return t&&t.getContext&&t.getContext("2d")||null}updateConfig(t){t.options.animation=!1}}const ts={touchstart:"mousedown",touchmove:"mousemove",touchend:"mouseup",pointerenter:"mouseenter",pointerdown:"mousedown",pointermove:"mousemove",pointerup:"mouseup",pointerleave:"mouseout",pointerout:"mouseout"},es=t=>null===t||""===t;const is=!!me&&{passive:!0};function ss(t,e,i){t.canvas.removeEventListener(e,i,is)}function ns(t,e){for(const i of t)if(i===e||i.contains(e))return!0}function os(t,e,i){const s=t.canvas,n=new MutationObserver((t=>{let e=!1;for(const i of t)e=e||ns(i.addedNodes,s),e=e&&!ns(i.removedNodes,s);e&&i()}));return n.observe(document,{childList:!0,subtree:!0}),n}function as(t,e,i){const s=t.canvas,n=new MutationObserver((t=>{let e=!1;for(const i of t)e=e||ns(i.removedNodes,s),e=e&&!ns(i.addedNodes,s);e&&i()}));return n.observe(document,{childList:!0,subtree:!0}),n}const rs=new Map;let ls=0;function hs(){const t=window.devicePixelRatio;t!==ls&&(ls=t,rs.forEach(((e,i)=>{i.currentDevicePixelRatio!==t&&e()})))}function cs(t,e,i){const s=t.canvas,n=s&&ae(s);if(!n)return;const o=ht(((t,e)=>{const s=n.clientWidth;i(t,e),s{const e=t[0],i=e.contentRect.width,s=e.contentRect.height;0===i&&0===s||o(i,s)}));return a.observe(n),function(t,e){rs.size||window.addEventListener("resize",hs),rs.set(t,e)}(t,o),a}function ds(t,e,i){i&&i.disconnect(),"resize"===e&&function(t){rs.delete(t),rs.size||window.removeEventListener("resize",hs)}(t)}function us(t,e,i){const s=t.canvas,n=ht((e=>{null!==t.ctx&&i(function(t,e){const i=ts[t.type]||t.type,{x:s,y:n}=ue(t,e);return{type:i,chart:e,native:t,x:void 0!==s?s:null,y:void 0!==n?n:null}}(e,t))}),t,(t=>{const e=t[0];return[e,e.offsetX,e.offsetY]}));return function(t,e,i){t.addEventListener(e,i,is)}(s,e,n),n}class fs extends Ji{acquireContext(t,e){const i=t&&t.getContext&&t.getContext("2d");return i&&i.canvas===t?(function(t,e){const i=t.style,s=t.getAttribute("height"),n=t.getAttribute("width");if(t.$chartjs={initial:{height:s,width:n,style:{display:i.display,height:i.height,width:i.width}}},i.display=i.display||"block",i.boxSizing=i.boxSizing||"border-box",es(n)){const e=be(t,"width");void 0!==e&&(t.width=e)}if(es(s))if(""===t.style.height)t.height=t.width/(e||2);else{const e=be(t,"height");void 0!==e&&(t.height=e)}}(t,e),i):null}releaseContext(t){const e=t.canvas;if(!e.$chartjs)return!1;const s=e.$chartjs.initial;["height","width"].forEach((t=>{const n=s[t];i(n)?e.removeAttribute(t):e.setAttribute(t,n)}));const n=s.style||{};return Object.keys(n).forEach((t=>{e.style[t]=n[t]})),e.width=e.width,delete e.$chartjs,!0}addEventListener(t,e,i){this.removeEventListener(t,e);const s=t.$proxies||(t.$proxies={}),n={attach:os,detach:as,resize:cs}[e]||us;s[e]=n(t,e,i)}removeEventListener(t,e){const i=t.$proxies||(t.$proxies={}),s=i[e];if(!s)return;({attach:ds,detach:ds,resize:ds}[e]||ss)(t,e,s),i[e]=void 0}getDevicePixelRatio(){return window.devicePixelRatio}getMaximumSize(t,e,i,s){return ge(t,e,i,s)}isAttached(t){const e=ae(t);return!(!e||!e.isConnected)}}function gs(t){return!oe()||"undefined"!=typeof OffscreenCanvas&&t instanceof OffscreenCanvas?Qi:fs}var ps=Object.freeze({__proto__:null,_detectPlatform:gs,BasePlatform:Ji,BasicPlatform:Qi,DomPlatform:fs});const ms="transparent",bs={boolean:(t,e,i)=>i>.5?e:t,color(t,e,i){const s=Jt(t||ms),n=s.valid&&Jt(e||ms);return n&&n.valid?n.mix(s,i).hexString():e},number:(t,e,i)=>t+(e-t)*i};class xs{constructor(t,e,i,s){const n=e[i];s=bi([t.to,s,n,t.from]);const o=bi([t.from,n,s]);this._active=!0,this._fn=t.fn||bs[t.type||typeof o],this._easing=si[t.easing]||si.linear,this._start=Math.floor(Date.now()+(t.delay||0)),this._duration=this._total=Math.floor(t.duration),this._loop=!!t.loop,this._target=e,this._prop=i,this._from=o,this._to=s,this._promises=void 0}active(){return this._active}update(t,e,i){if(this._active){this._notify(!1);const s=this._target[this._prop],n=i-this._start,o=this._duration-n;this._start=i,this._duration=Math.floor(Math.max(o,t.duration)),this._total+=n,this._loop=!!t.loop,this._to=bi([t.to,e,s,t.from]),this._from=bi([t.from,s,e])}}cancel(){this._active&&(this.tick(Date.now()),this._active=!1,this._notify(!1))}tick(t){const e=t-this._start,i=this._duration,s=this._prop,n=this._from,o=this._loop,a=this._to;let r;if(this._active=n!==a&&(o||e1?2-r:r,r=this._easing(Math.min(1,Math.max(0,r))),this._target[s]=this._fn(n,a,r))}wait(){const t=this._promises||(this._promises=[]);return new Promise(((e,i)=>{t.push({res:e,rej:i})}))}_notify(t){const e=t?"res":"rej",i=this._promises||[];for(let t=0;t"onProgress"!==t&&"onComplete"!==t&&"fn"!==t}),ne.set("animations",{colors:{type:"color",properties:["color","borderColor","backgroundColor"]},numbers:{type:"number",properties:["x","y","borderWidth","radius","tension"]}}),ne.describe("animations",{_fallback:"animation"}),ne.set("transitions",{active:{animation:{duration:400}},resize:{animation:{duration:0}},show:{animations:{colors:{from:"transparent"},visible:{type:"boolean",duration:0}}},hide:{animations:{colors:{to:"transparent"},visible:{type:"boolean",easing:"linear",fn:t=>0|t}}}});class ys{constructor(t,e){this._chart=t,this._properties=new Map,this.configure(e)}configure(t){if(!n(t))return;const e=this._properties;Object.getOwnPropertyNames(t).forEach((i=>{const o=t[i];if(!n(o))return;const a={};for(const t of _s)a[t]=o[t];(s(o.properties)&&o.properties||[i]).forEach((t=>{t!==i&&e.has(t)||e.set(t,a)}))}))}_animateOptions(t,e){const i=e.options,s=function(t,e){if(!e)return;let i=t.options;if(!i)return void(t.options=e);i.$shared&&(t.options=i=Object.assign({},i,{$shared:!1,$animations:{}}));return i}(t,i);if(!s)return[];const n=this._createAnimations(s,i);return i.$shared&&function(t,e){const i=[],s=Object.keys(e);for(let e=0;e{t.options=i}),(()=>{})),n}_createAnimations(t,e){const i=this._properties,s=[],n=t.$animations||(t.$animations={}),o=Object.keys(e),a=Date.now();let r;for(r=o.length-1;r>=0;--r){const l=o[r];if("$"===l.charAt(0))continue;if("options"===l){s.push(...this._animateOptions(t,e));continue}const h=e[l];let c=n[l];const d=i.get(l);if(c){if(d&&c.active()){c.update(d,h,a);continue}c.cancel()}d&&d.duration?(n[l]=c=new xs(d,t,l,h),s.push(c)):t[l]=h}return s}update(t,e){if(0===this._properties.size)return void Object.assign(t,e);const i=this._createAnimations(t,e);return i.length?(mt.add(this._chart,i),!0):void 0}}function vs(t,e){const i=t&&t.options||{},s=i.reverse,n=void 0===i.min?e:0,o=void 0===i.max?e:0;return{start:s?o:n,end:s?n:o}}function ws(t,e){const i=[],s=t._getSortedDatasetMetas(e);let n,o;for(n=0,o=s.length;n0||!i&&e<0)return n.index}return null}function Ds(t,e){const{chart:i,_cachedMeta:s}=t,n=i._stacks||(i._stacks={}),{iScale:o,vScale:a,index:r}=s,l=o.axis,h=a.axis,c=function(t,e,i){return`${t.id}.${e.id}.${i.stack||i.type}`}(o,a,s),d=e.length;let u;for(let t=0;ti[t].axis===e)).shift()}function Cs(t,e){const i=t.controller.index,s=t.vScale&&t.vScale.axis;if(s){e=e||t._parsed;for(const t of e){const e=t._stacks;if(!e||void 0===e[s]||void 0===e[s][i])return;delete e[s][i]}}}const As=t=>"reset"===t||"none"===t,Ts=(t,e)=>e?t:Object.assign({},t);class Ls{constructor(t,e){this.chart=t,this._ctx=t.ctx,this.index=e,this._cachedDataOpts={},this._cachedMeta=this.getMeta(),this._type=this._cachedMeta.type,this.options=void 0,this._parsing=!1,this._data=void 0,this._objectData=void 0,this._sharedOptions=void 0,this._drawStart=void 0,this._drawCount=void 0,this.enableOptionSharing=!1,this.supportsDecimation=!1,this.$context=void 0,this._syncList=[],this.initialize()}initialize(){const t=this._cachedMeta;this.configure(),this.linkScales(),t._stacked=ks(t.vScale,t),this.addElements()}updateIndex(t){this.index!==t&&Cs(this._cachedMeta),this.index=t}linkScales(){const t=this.chart,e=this._cachedMeta,i=this.getDataset(),s=(t,e,i,s)=>"x"===t?e:"r"===t?s:i,n=e.xAxisID=r(i.xAxisID,Os(t,"x")),o=e.yAxisID=r(i.yAxisID,Os(t,"y")),a=e.rAxisID=r(i.rAxisID,Os(t,"r")),l=e.indexAxis,h=e.iAxisID=s(l,n,o,a),c=e.vAxisID=s(l,o,n,a);e.xScale=this.getScaleForId(n),e.yScale=this.getScaleForId(o),e.rScale=this.getScaleForId(a),e.iScale=this.getScaleForId(h),e.vScale=this.getScaleForId(c)}getDataset(){return this.chart.data.datasets[this.index]}getMeta(){return this.chart.getDatasetMeta(this.index)}getScaleForId(t){return this.chart.scales[t]}_getOtherScale(t){const e=this._cachedMeta;return t===e.iScale?e.vScale:e.iScale}reset(){this._update("reset")}_destroy(){const t=this._cachedMeta;this._data&&at(this._data,this),t._stacked&&Cs(t)}_dataCheck(){const t=this.getDataset(),e=t.data||(t.data=[]),i=this._data;if(n(e))this._data=function(t){const e=Object.keys(t),i=new Array(e.length);let s,n,o;for(s=0,n=e.length;s0&&i._parsed[t-1];if(!1===this._parsing)i._parsed=o,i._sorted=!0,d=o;else{d=s(o[t])?this.parseArrayData(i,o,t,e):n(o[t])?this.parseObjectData(i,o,t,e):this.parsePrimitiveData(i,o,t,e);const a=()=>null===c[l]||f&&c[l]t&&!e.hidden&&e._stacked&&{keys:ws(i,!0),values:null})(e,i,this.chart),h={min:Number.POSITIVE_INFINITY,max:Number.NEGATIVE_INFINITY},{min:c,max:d}=function(t){const{min:e,max:i,minDefined:s,maxDefined:n}=t.getUserBounds();return{min:s?e:Number.NEGATIVE_INFINITY,max:n?i:Number.POSITIVE_INFINITY}}(r);let u,f;function g(){f=s[u];const e=f[r.axis];return!o(f[t.axis])||c>e||d=0;--u)if(!g()){this.updateRangeFromParsed(h,t,f,l);break}return h}getAllParsedValues(t){const e=this._cachedMeta._parsed,i=[];let s,n,a;for(s=0,n=e.length;s=0&&tthis.getContext(i,s)),c);return f.$shared&&(f.$shared=r,n[o]=Object.freeze(Ts(f,r))),f}_resolveAnimations(t,e,i){const s=this.chart,n=this._cachedDataOpts,o=`animation-${e}`,a=n[o];if(a)return a;let r;if(!1!==s.options.animation){const s=this.chart.config,n=s.datasetAnimationScopeKeys(this._type,e),o=s.getOptionScopes(this.getDataset(),n);r=s.createResolver(o,this.getContext(t,i,e))}const l=new ys(s,r&&r.animations);return r&&r._cacheable&&(n[o]=Object.freeze(l)),l}getSharedOptions(t){if(t.$shared)return this._sharedOptions||(this._sharedOptions=Object.assign({},t))}includeOptions(t,e){return!e||As(t)||this.chart._animationsDisabled}_getSharedOptions(t,e){const i=this.resolveDataElementOptions(t,e),s=this._sharedOptions,n=this.getSharedOptions(i),o=this.includeOptions(e,n)||n!==s;return this.updateSharedOptions(n,e,i),{sharedOptions:n,includeOptions:o}}updateElement(t,e,i,s){As(s)?Object.assign(t,i):this._resolveAnimations(e,s).update(t,i)}updateSharedOptions(t,e,i){t&&!As(e)&&this._resolveAnimations(void 0,e).update(t,i)}_setStyle(t,e,i,s){t.active=s;const n=this.getStyle(e,s);this._resolveAnimations(e,i,s).update(t,{options:!s&&this.getSharedOptions(n)||n})}removeHoverStyle(t,e,i){this._setStyle(t,i,"active",!1)}setHoverStyle(t,e,i){this._setStyle(t,i,"active",!0)}_removeDatasetHoverStyle(){const t=this._cachedMeta.dataset;t&&this._setStyle(t,void 0,"active",!1)}_setDatasetHoverStyle(){const t=this._cachedMeta.dataset;t&&this._setStyle(t,void 0,"active",!0)}_resyncElements(t){const e=this._data,i=this._cachedMeta.data;for(const[t,e,i]of this._syncList)this[t](e,i);this._syncList=[];const s=i.length,n=e.length,o=Math.min(n,s);o&&this.parse(0,o),n>s?this._insertElements(s,n-s,t):n{for(t.length+=e,a=t.length-1;a>=o;a--)t[a]=t[a-e]};for(r(n),a=t;a{s[t]=i[t]&&i[t].active()?i[t]._to:this[t]})),s}}Es.defaults={},Es.defaultRoutes=void 0;const Rs={values:t=>s(t)?t:""+t,numeric(t,e,i){if(0===t)return"0";const s=this.chart.options.locale;let n,o=t;if(i.length>1){const e=Math.max(Math.abs(i[0].value),Math.abs(i[i.length-1].value));(e<1e-4||e>1e15)&&(n="scientific"),o=function(t,e){let i=e.length>3?e[2].value-e[1].value:e[1].value-e[0].value;Math.abs(i)>=1&&t!==Math.floor(t)&&(i=t-Math.floor(t));return i}(t,i)}const a=I(Math.abs(o)),r=Math.max(Math.min(-1*Math.floor(a),20),0),l={notation:n,minimumFractionDigits:r,maximumFractionDigits:r};return Object.assign(l,this.options.ticks.format),li(t,s,l)},logarithmic(t,e,i){if(0===t)return"0";const s=t/Math.pow(10,Math.floor(I(t)));return 1===s||2===s||5===s?Rs.numeric.call(this,t,e,i):""}};var Is={formatters:Rs};function zs(t,e){const s=t.options.ticks,n=s.maxTicksLimit||function(t){const e=t.options.offset,i=t._tickSize(),s=t._length/i+(e?0:1),n=t._maxLength/i;return Math.floor(Math.min(s,n))}(t),o=s.major.enabled?function(t){const e=[];let i,s;for(i=0,s=t.length;in)return function(t,e,i,s){let n,o=0,a=i[0];for(s=Math.ceil(s),n=0;nn)return e}return Math.max(n,1)}(o,e,n);if(a>0){let t,s;const n=a>1?Math.round((l-r)/(a-1)):null;for(Fs(e,h,c,i(n)?0:r-n,r),t=0,s=a-1;te.lineWidth,tickColor:(t,e)=>e.color,offset:!1,borderDash:[],borderDashOffset:0,borderWidth:1},title:{display:!1,text:"",padding:{top:4,bottom:4}},ticks:{minRotation:0,maxRotation:50,mirror:!1,textStrokeWidth:0,textStrokeColor:"",padding:3,display:!0,autoSkip:!0,autoSkipPadding:3,labelOffset:0,callback:Is.formatters.values,minor:{},major:{},align:"center",crossAlign:"near",showLabelBackdrop:!1,backdropColor:"rgba(255, 255, 255, 0.75)",backdropPadding:2}}),ne.route("scale.ticks","color","","color"),ne.route("scale.grid","color","","borderColor"),ne.route("scale.grid","borderColor","","borderColor"),ne.route("scale.title","color","","color"),ne.describe("scale",{_fallback:!1,_scriptable:t=>!t.startsWith("before")&&!t.startsWith("after")&&"callback"!==t&&"parser"!==t,_indexable:t=>"borderDash"!==t&&"tickBorderDash"!==t}),ne.describe("scales",{_fallback:"scale"}),ne.describe("scale.ticks",{_scriptable:t=>"backdropPadding"!==t&&"callback"!==t,_indexable:t=>"backdropPadding"!==t});const Vs=(t,e,i)=>"top"===e||"left"===e?t[e]+i:t[e]-i;function Bs(t,e){const i=[],s=t.length/e,n=t.length;let o=0;for(;oa+r)))return h}function Ws(t){return t.drawTicks?t.tickLength:0}function js(t,e){if(!t.display)return 0;const i=mi(t.font,e),n=pi(t.padding);return(s(t.text)?t.text.length:1)*i.lineHeight+n.height}function Hs(t,e,i){let s=dt(t);return(i&&"right"!==e||!i&&"right"===e)&&(s=(t=>"left"===t?"right":"right"===t?"left":t)(s)),s}class $s extends Es{constructor(t){super(),this.id=t.id,this.type=t.type,this.options=void 0,this.ctx=t.ctx,this.chart=t.chart,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.width=void 0,this.height=void 0,this._margins={left:0,right:0,top:0,bottom:0},this.maxWidth=void 0,this.maxHeight=void 0,this.paddingTop=void 0,this.paddingBottom=void 0,this.paddingLeft=void 0,this.paddingRight=void 0,this.axis=void 0,this.labelRotation=void 0,this.min=void 0,this.max=void 0,this._range=void 0,this.ticks=[],this._gridLineItems=null,this._labelItems=null,this._labelSizes=null,this._length=0,this._maxLength=0,this._longestTextCache={},this._startPixel=void 0,this._endPixel=void 0,this._reversePixels=!1,this._userMax=void 0,this._userMin=void 0,this._suggestedMax=void 0,this._suggestedMin=void 0,this._ticksLength=0,this._borderValue=0,this._cache={},this._dataLimitsCached=!1,this.$context=void 0}init(t){this.options=t.setContext(this.getContext()),this.axis=t.axis,this._userMin=this.parse(t.min),this._userMax=this.parse(t.max),this._suggestedMin=this.parse(t.suggestedMin),this._suggestedMax=this.parse(t.suggestedMax)}parse(t,e){return t}getUserBounds(){let{_userMin:t,_userMax:e,_suggestedMin:i,_suggestedMax:s}=this;return t=a(t,Number.POSITIVE_INFINITY),e=a(e,Number.NEGATIVE_INFINITY),i=a(i,Number.POSITIVE_INFINITY),s=a(s,Number.NEGATIVE_INFINITY),{min:a(t,i),max:a(e,s),minDefined:o(t),maxDefined:o(e)}}getMinMax(t){let e,{min:i,max:s,minDefined:n,maxDefined:o}=this.getUserBounds();if(n&&o)return{min:i,max:s};const r=this.getMatchingVisibleMetas();for(let a=0,l=r.length;as?s:i,s=n&&i>s?i:s,{min:a(i,a(s,i)),max:a(s,a(i,s))}}getPadding(){return{left:this.paddingLeft||0,top:this.paddingTop||0,right:this.paddingRight||0,bottom:this.paddingBottom||0}}getTicks(){return this.ticks}getLabels(){const t=this.chart.data;return this.options.labels||(this.isHorizontal()?t.xLabels:t.yLabels)||t.labels||[]}beforeLayout(){this._cache={},this._dataLimitsCached=!1}beforeUpdate(){c(this.options.beforeUpdate,[this])}update(t,e,i){const{beginAtZero:s,grace:n,ticks:o}=this.options,a=o.sampleSize;this.beforeUpdate(),this.maxWidth=t,this.maxHeight=e,this._margins=i=Object.assign({left:0,right:0,top:0,bottom:0},i),this.ticks=null,this._labelSizes=null,this._gridLineItems=null,this._labelItems=null,this.beforeSetDimensions(),this.setDimensions(),this.afterSetDimensions(),this._maxLength=this.isHorizontal()?this.width+i.left+i.right:this.height+i.top+i.bottom,this._dataLimitsCached||(this.beforeDataLimits(),this.determineDataLimits(),this.afterDataLimits(),this._range=xi(this,n,s),this._dataLimitsCached=!0),this.beforeBuildTicks(),this.ticks=this.buildTicks()||[],this.afterBuildTicks();const r=a=n||i<=1||!this.isHorizontal())return void(this.labelRotation=s);const h=this._getLabelSizes(),c=h.widest.width,d=h.highest.height,u=Z(this.chart.width-c,0,this.maxWidth);o=t.offset?this.maxWidth/i:u/(i-1),c+6>o&&(o=u/(i-(t.offset?.5:1)),a=this.maxHeight-Ws(t.grid)-e.padding-js(t.title,this.chart.options.font),r=Math.sqrt(c*c+d*d),l=$(Math.min(Math.asin(Z((h.highest.height+6)/o,-1,1)),Math.asin(Z(a/r,-1,1))-Math.asin(Z(d/r,-1,1)))),l=Math.max(s,Math.min(n,l))),this.labelRotation=l}afterCalculateLabelRotation(){c(this.options.afterCalculateLabelRotation,[this])}afterAutoSkip(){}beforeFit(){c(this.options.beforeFit,[this])}fit(){const t={width:0,height:0},{chart:e,options:{ticks:i,title:s,grid:n}}=this,o=this._isVisible(),a=this.isHorizontal();if(o){const o=js(s,e.options.font);if(a?(t.width=this.maxWidth,t.height=Ws(n)+o):(t.height=this.maxHeight,t.width=Ws(n)+o),i.display&&this.ticks.length){const{first:e,last:s,widest:n,highest:o}=this._getLabelSizes(),r=2*i.padding,l=H(this.labelRotation),h=Math.cos(l),c=Math.sin(l);if(a){const e=i.mirror?0:c*n.width+h*o.height;t.height=Math.min(this.maxHeight,t.height+e+r)}else{const e=i.mirror?0:h*n.width+c*o.height;t.width=Math.min(this.maxWidth,t.width+e+r)}this._calculatePadding(e,s,c,h)}}this._handleMargins(),a?(this.width=this._length=e.width-this._margins.left-this._margins.right,this.height=t.height):(this.width=t.width,this.height=this._length=e.height-this._margins.top-this._margins.bottom)}_calculatePadding(t,e,i,s){const{ticks:{align:n,padding:o},position:a}=this.options,r=0!==this.labelRotation,l="top"!==a&&"x"===this.axis;if(this.isHorizontal()){const a=this.getPixelForTick(0)-this.left,h=this.right-this.getPixelForTick(this.ticks.length-1);let c=0,d=0;r?l?(c=s*t.width,d=i*e.height):(c=i*t.height,d=s*e.width):"start"===n?d=e.width:"end"===n?c=t.width:"inner"!==n&&(c=t.width/2,d=e.width/2),this.paddingLeft=Math.max((c-a+o)*this.width/(this.width-a),0),this.paddingRight=Math.max((d-h+o)*this.width/(this.width-h),0)}else{let i=e.height/2,s=t.height/2;"start"===n?(i=0,s=t.height):"end"===n&&(i=e.height,s=0),this.paddingTop=i+o,this.paddingBottom=s+o}}_handleMargins(){this._margins&&(this._margins.left=Math.max(this.paddingLeft,this._margins.left),this._margins.top=Math.max(this.paddingTop,this._margins.top),this._margins.right=Math.max(this.paddingRight,this._margins.right),this._margins.bottom=Math.max(this.paddingBottom,this._margins.bottom))}afterFit(){c(this.options.afterFit,[this])}isHorizontal(){const{axis:t,position:e}=this.options;return"top"===e||"bottom"===e||"x"===t}isFullSize(){return this.options.fullSize}_convertTicksToLabels(t){let e,s;for(this.beforeTickToLabelConversion(),this.generateTickLabels(t),e=0,s=t.length;e{const i=t.gc,s=i.length/2;let n;if(s>e){for(n=0;n({width:a[t]||0,height:r[t]||0});return{first:k(0),last:k(e-1),widest:k(w),highest:k(M),widths:a,heights:r}}getLabelForValue(t){return t}getPixelForValue(t,e){return NaN}getValueForPixel(t){}getPixelForTick(t){const e=this.ticks;return t<0||t>e.length-1?null:this.getPixelForValue(e[t].value)}getPixelForDecimal(t){this._reversePixels&&(t=1-t);const e=this._startPixel+t*this._length;return J(this._alignToPixels?ve(this.chart,e,0):e)}getDecimalForPixel(t){const e=(t-this._startPixel)/this._length;return this._reversePixels?1-e:e}getBasePixel(){return this.getPixelForValue(this.getBaseValue())}getBaseValue(){const{min:t,max:e}=this;return t<0&&e<0?e:t>0&&e>0?t:0}getContext(t){const e=this.ticks||[];if(t>=0&&ta*s?a/i:r/s:r*s0}_computeGridLineItems(t){const e=this.axis,i=this.chart,s=this.options,{grid:o,position:a}=s,l=o.offset,h=this.isHorizontal(),c=this.ticks.length+(l?1:0),d=Ws(o),u=[],f=o.setContext(this.getContext()),g=f.drawBorder?f.borderWidth:0,p=g/2,m=function(t){return ve(i,t,g)};let b,x,_,y,v,w,M,k,S,P,D,O;if("top"===a)b=m(this.bottom),w=this.bottom-d,k=b-p,P=m(t.top)+p,O=t.bottom;else if("bottom"===a)b=m(this.top),P=t.top,O=m(t.bottom)-p,w=b+p,k=this.top+d;else if("left"===a)b=m(this.right),v=this.right-d,M=b-p,S=m(t.left)+p,D=t.right;else if("right"===a)b=m(this.left),S=t.left,D=m(t.right)-p,v=b+p,M=this.left+d;else if("x"===e){if("center"===a)b=m((t.top+t.bottom)/2+.5);else if(n(a)){const t=Object.keys(a)[0],e=a[t];b=m(this.chart.scales[t].getPixelForValue(e))}P=t.top,O=t.bottom,w=b+p,k=w+d}else if("y"===e){if("center"===a)b=m((t.left+t.right)/2);else if(n(a)){const t=Object.keys(a)[0],e=a[t];b=m(this.chart.scales[t].getPixelForValue(e))}v=b-p,M=v-d,S=t.left,D=t.right}const C=r(s.ticks.maxTicksLimit,c),A=Math.max(1,Math.ceil(c/C));for(x=0;xe.value===t));if(i>=0){return e.setContext(this.getContext(i)).lineWidth}return 0}drawGrid(t){const e=this.options.grid,i=this.ctx,s=this._gridLineItems||(this._gridLineItems=this._computeGridLineItems(t));let n,o;const a=(t,e,s)=>{s.width&&s.color&&(i.save(),i.lineWidth=s.width,i.strokeStyle=s.color,i.setLineDash(s.borderDash||[]),i.lineDashOffset=s.borderDashOffset,i.beginPath(),i.moveTo(t.x,t.y),i.lineTo(e.x,e.y),i.stroke(),i.restore())};if(e.display)for(n=0,o=s.length;n{this.drawBackground(),this.drawGrid(t),this.drawTitle()}},{z:i+1,draw:()=>{this.drawBorder()}},{z:e,draw:t=>{this.drawLabels(t)}}]:[{z:e,draw:t=>{this.draw(t)}}]}getMatchingVisibleMetas(t){const e=this.chart.getSortedVisibleDatasetMetas(),i=this.axis+"AxisID",s=[];let n,o;for(n=0,o=e.length;n{const s=i.split("."),n=s.pop(),o=[t].concat(s).join("."),a=e[i].split("."),r=a.pop(),l=a.join(".");ne.route(o,n,l,r)}))}(e,t.defaultRoutes);t.descriptors&&ne.describe(e,t.descriptors)}(t,o,i),this.override&&ne.override(t.id,t.overrides)),o}get(t){return this.items[t]}unregister(t){const e=this.items,i=t.id,s=this.scope;i in e&&delete e[i],s&&i in ne[s]&&(delete ne[s][i],this.override&&delete te[i])}}var Us=new class{constructor(){this.controllers=new Ys(Ls,"datasets",!0),this.elements=new Ys(Es,"elements"),this.plugins=new Ys(Object,"plugins"),this.scales=new Ys($s,"scales"),this._typedRegistries=[this.controllers,this.scales,this.elements]}add(...t){this._each("register",t)}remove(...t){this._each("unregister",t)}addControllers(...t){this._each("register",t,this.controllers)}addElements(...t){this._each("register",t,this.elements)}addPlugins(...t){this._each("register",t,this.plugins)}addScales(...t){this._each("register",t,this.scales)}getController(t){return this._get(t,this.controllers,"controller")}getElement(t){return this._get(t,this.elements,"element")}getPlugin(t){return this._get(t,this.plugins,"plugin")}getScale(t){return this._get(t,this.scales,"scale")}removeControllers(...t){this._each("unregister",t,this.controllers)}removeElements(...t){this._each("unregister",t,this.elements)}removePlugins(...t){this._each("unregister",t,this.plugins)}removeScales(...t){this._each("unregister",t,this.scales)}_each(t,e,i){[...e].forEach((e=>{const s=i||this._getRegistryForType(e);i||s.isForType(e)||s===this.plugins&&e.id?this._exec(t,s,e):d(e,(e=>{const s=i||this._getRegistryForType(e);this._exec(t,s,e)}))}))}_exec(t,e,i){const s=w(t);c(i["before"+s],[],i),e[t](i),c(i["after"+s],[],i)}_getRegistryForType(t){for(let e=0;et.filter((t=>!e.some((e=>t.plugin.id===e.plugin.id))));this._notify(s(e,i),t,"stop"),this._notify(s(i,e),t,"start")}}function qs(t,e){return e||!1!==t?!0===t?{}:t:null}function Ks(t,{plugin:e,local:i},s,n){const o=t.pluginScopeKeys(e),a=t.getOptionScopes(s,o);return i&&e.defaults&&a.push(e.defaults),t.createResolver(a,n,[""],{scriptable:!1,indexable:!1,allKeys:!0})}function Gs(t,e){const i=ne.datasets[t]||{};return((e.datasets||{})[t]||{}).indexAxis||e.indexAxis||i.indexAxis||"x"}function Zs(t,e){return"x"===t||"y"===t?t:e.axis||("top"===(i=e.position)||"bottom"===i?"x":"left"===i||"right"===i?"y":void 0)||t.charAt(0).toLowerCase();var i}function Js(t){const e=t.options||(t.options={});e.plugins=r(e.plugins,{}),e.scales=function(t,e){const i=te[t.type]||{scales:{}},s=e.scales||{},o=Gs(t.type,e),a=Object.create(null),r=Object.create(null);return Object.keys(s).forEach((t=>{const e=s[t];if(!n(e))return console.error(`Invalid scale configuration for scale: ${t}`);if(e._proxy)return console.warn(`Ignoring resolver passed as options for scale: ${t}`);const l=Zs(t,e),h=function(t,e){return t===e?"_index_":"_value_"}(l,o),c=i.scales||{};a[l]=a[l]||t,r[t]=b(Object.create(null),[{axis:l},e,c[l],c[h]])})),t.data.datasets.forEach((i=>{const n=i.type||t.type,o=i.indexAxis||Gs(n,e),l=(te[n]||{}).scales||{};Object.keys(l).forEach((t=>{const e=function(t,e){let i=t;return"_index_"===t?i=e:"_value_"===t&&(i="x"===e?"y":"x"),i}(t,o),n=i[e+"AxisID"]||a[e]||e;r[n]=r[n]||Object.create(null),b(r[n],[{axis:e},s[n],l[t]])}))})),Object.keys(r).forEach((t=>{const e=r[t];b(e,[ne.scales[e.type],ne.scale])})),r}(t,e)}function Qs(t){return(t=t||{}).datasets=t.datasets||[],t.labels=t.labels||[],t}const tn=new Map,en=new Set;function sn(t,e){let i=tn.get(t);return i||(i=e(),tn.set(t,i),en.add(i)),i}const nn=(t,e,i)=>{const s=y(e,i);void 0!==s&&t.add(s)};class on{constructor(t){this._config=function(t){return(t=t||{}).data=Qs(t.data),Js(t),t}(t),this._scopeCache=new Map,this._resolverCache=new Map}get platform(){return this._config.platform}get type(){return this._config.type}set type(t){this._config.type=t}get data(){return this._config.data}set data(t){this._config.data=Qs(t)}get options(){return this._config.options}set options(t){this._config.options=t}get plugins(){return this._config.plugins}update(){const t=this._config;this.clearCache(),Js(t)}clearCache(){this._scopeCache.clear(),this._resolverCache.clear()}datasetScopeKeys(t){return sn(t,(()=>[[`datasets.${t}`,""]]))}datasetAnimationScopeKeys(t,e){return sn(`${t}.transition.${e}`,(()=>[[`datasets.${t}.transitions.${e}`,`transitions.${e}`],[`datasets.${t}`,""]]))}datasetElementScopeKeys(t,e){return sn(`${t}-${e}`,(()=>[[`datasets.${t}.elements.${e}`,`datasets.${t}`,`elements.${e}`,""]]))}pluginScopeKeys(t){const e=t.id;return sn(`${this.type}-plugin-${e}`,(()=>[[`plugins.${e}`,...t.additionalOptionScopes||[]]]))}_cachedScopes(t,e){const i=this._scopeCache;let s=i.get(t);return s&&!e||(s=new Map,i.set(t,s)),s}getOptionScopes(t,e,i){const{options:s,type:n}=this,o=this._cachedScopes(t,i),a=o.get(e);if(a)return a;const r=new Set;e.forEach((e=>{t&&(r.add(t),e.forEach((e=>nn(r,t,e)))),e.forEach((t=>nn(r,s,t))),e.forEach((t=>nn(r,te[n]||{},t))),e.forEach((t=>nn(r,ne,t))),e.forEach((t=>nn(r,ee,t)))}));const l=Array.from(r);return 0===l.length&&l.push(Object.create(null)),en.has(e)&&o.set(e,l),l}chartOptionScopes(){const{options:t,type:e}=this;return[t,te[e]||{},ne.datasets[e]||{},{type:e},ne,ee]}resolveNamedOptions(t,e,i,n=[""]){const o={$shared:!0},{resolver:a,subPrefixes:r}=an(this._resolverCache,t,n);let l=a;if(function(t,e){const{isScriptable:i,isIndexable:n}=Ie(t);for(const o of e){const e=i(o),a=n(o),r=(a||e)&&t[o];if(e&&(k(r)||rn(r))||a&&s(r))return!0}return!1}(a,e)){o.$shared=!1;l=Re(a,i=k(i)?i():i,this.createResolver(t,i,r))}for(const t of e)o[t]=l[t];return o}createResolver(t,e,i=[""],s){const{resolver:o}=an(this._resolverCache,t,i);return n(e)?Re(o,e,void 0,s):o}}function an(t,e,i){let s=t.get(e);s||(s=new Map,t.set(e,s));const n=i.join();let o=s.get(n);if(!o){o={resolver:Ee(e,i),subPrefixes:i.filter((t=>!t.toLowerCase().includes("hover")))},s.set(n,o)}return o}const rn=t=>n(t)&&Object.getOwnPropertyNames(t).reduce(((e,i)=>e||k(t[i])),!1);const ln=["top","bottom","left","right","chartArea"];function hn(t,e){return"top"===t||"bottom"===t||-1===ln.indexOf(t)&&"x"===e}function cn(t,e){return function(i,s){return i[t]===s[t]?i[e]-s[e]:i[t]-s[t]}}function dn(t){const e=t.chart,i=e.options.animation;e.notifyPlugins("afterRender"),c(i&&i.onComplete,[t],e)}function un(t){const e=t.chart,i=e.options.animation;c(i&&i.onProgress,[t],e)}function fn(t){return oe()&&"string"==typeof t?t=document.getElementById(t):t&&t.length&&(t=t[0]),t&&t.canvas&&(t=t.canvas),t}const gn={},pn=t=>{const e=fn(t);return Object.values(gn).filter((t=>t.canvas===e)).pop()};function mn(t,e,i){const s=Object.keys(t);for(const n of s){const s=+n;if(s>=e){const o=t[n];delete t[n],(i>0||s>e)&&(t[s+i]=o)}}}class bn{constructor(t,i){const s=this.config=new on(i),n=fn(t),o=pn(n);if(o)throw new Error("Canvas is already in use. Chart with ID '"+o.id+"' must be destroyed before the canvas with ID '"+o.canvas.id+"' can be reused.");const a=s.createResolver(s.chartOptionScopes(),this.getContext());this.platform=new(s.platform||gs(n)),this.platform.updateConfig(s);const r=this.platform.acquireContext(n,a.aspectRatio),l=r&&r.canvas,h=l&&l.height,c=l&&l.width;this.id=e(),this.ctx=r,this.canvas=l,this.width=c,this.height=h,this._options=a,this._aspectRatio=this.aspectRatio,this._layers=[],this._metasets=[],this._stacks=void 0,this.boxes=[],this.currentDevicePixelRatio=void 0,this.chartArea=void 0,this._active=[],this._lastEvent=void 0,this._listeners={},this._responsiveListeners=void 0,this._sortedMetasets=[],this.scales={},this._plugins=new Xs,this.$proxies={},this._hiddenIndices={},this.attached=!1,this._animationsDisabled=void 0,this.$context=void 0,this._doResize=ct((t=>this.update(t)),a.resizeDelay||0),this._dataChanges=[],gn[this.id]=this,r&&l?(mt.listen(this,"complete",dn),mt.listen(this,"progress",un),this._initialize(),this.attached&&this.update()):console.error("Failed to create chart: can't acquire context from the given item")}get aspectRatio(){const{options:{aspectRatio:t,maintainAspectRatio:e},width:s,height:n,_aspectRatio:o}=this;return i(t)?e&&o?o:n?s/n:null:t}get data(){return this.config.data}set data(t){this.config.data=t}get options(){return this._options}set options(t){this.config.options=t}_initialize(){return this.notifyPlugins("beforeInit"),this.options.responsive?this.resize():pe(this,this.options.devicePixelRatio),this.bindEvents(),this.notifyPlugins("afterInit"),this}clear(){return we(this.canvas,this.ctx),this}stop(){return mt.stop(this),this}resize(t,e){mt.running(this)?this._resizeBeforeDraw={width:t,height:e}:this._resize(t,e)}_resize(t,e){const i=this.options,s=this.canvas,n=i.maintainAspectRatio&&this.aspectRatio,o=this.platform.getMaximumSize(s,t,e,n),a=i.devicePixelRatio||this.platform.getDevicePixelRatio(),r=this.width?"resize":"attach";this.width=o.width,this.height=o.height,this._aspectRatio=this.aspectRatio,pe(this,a,!0)&&(this.notifyPlugins("resize",{size:o}),c(i.onResize,[this,o],this),this.attached&&this._doResize(r)&&this.render())}ensureScalesHaveIDs(){d(this.options.scales||{},((t,e)=>{t.id=e}))}buildOrUpdateScales(){const t=this.options,e=t.scales,i=this.scales,s=Object.keys(i).reduce(((t,e)=>(t[e]=!1,t)),{});let n=[];e&&(n=n.concat(Object.keys(e).map((t=>{const i=e[t],s=Zs(t,i),n="r"===s,o="x"===s;return{options:i,dposition:n?"chartArea":o?"bottom":"left",dtype:n?"radialLinear":o?"category":"linear"}})))),d(n,(e=>{const n=e.options,o=n.id,a=Zs(o,n),l=r(n.type,e.dtype);void 0!==n.position&&hn(n.position,a)===hn(e.dposition)||(n.position=e.dposition),s[o]=!0;let h=null;if(o in i&&i[o].type===l)h=i[o];else{h=new(Us.getScale(l))({id:o,type:l,ctx:this.ctx,chart:this}),i[h.id]=h}h.init(n,t)})),d(s,((t,e)=>{t||delete i[e]})),d(i,(t=>{Zi.configure(this,t,t.options),Zi.addBox(this,t)}))}_updateMetasets(){const t=this._metasets,e=this.data.datasets.length,i=t.length;if(t.sort(((t,e)=>t.index-e.index)),i>e){for(let t=e;te.length&&delete this._stacks,t.forEach(((t,i)=>{0===e.filter((e=>e===t._dataset)).length&&this._destroyDatasetMeta(i)}))}buildOrUpdateControllers(){const t=[],e=this.data.datasets;let i,s;for(this._removeUnreferencedMetasets(),i=0,s=e.length;i{this.getDatasetMeta(e).controller.reset()}),this)}reset(){this._resetElements(),this.notifyPlugins("reset")}update(t){const e=this.config;e.update();const i=this._options=e.createResolver(e.chartOptionScopes(),this.getContext()),s=this._animationsDisabled=!i.animation;if(this._updateScales(),this._checkEventBindings(),this._updateHiddenIndices(),this._plugins.invalidate(),!1===this.notifyPlugins("beforeUpdate",{mode:t,cancelable:!0}))return;const n=this.buildOrUpdateControllers();this.notifyPlugins("beforeElementsUpdate");let o=0;for(let t=0,e=this.data.datasets.length;t{t.reset()})),this._updateDatasets(t),this.notifyPlugins("afterUpdate",{mode:t}),this._layers.sort(cn("z","_idx"));const{_active:a,_lastEvent:r}=this;r?this._eventHandler(r,!0):a.length&&this._updateHoverStyles(a,a,!0),this.render()}_updateScales(){d(this.scales,(t=>{Zi.removeBox(this,t)})),this.ensureScalesHaveIDs(),this.buildOrUpdateScales()}_checkEventBindings(){const t=this.options,e=new Set(Object.keys(this._listeners)),i=new Set(t.events);S(e,i)&&!!this._responsiveListeners===t.responsive||(this.unbindEvents(),this.bindEvents())}_updateHiddenIndices(){const{_hiddenIndices:t}=this,e=this._getUniformDataChanges()||[];for(const{method:i,start:s,count:n}of e){mn(t,s,"_removeElements"===i?-n:n)}}_getUniformDataChanges(){const t=this._dataChanges;if(!t||!t.length)return;this._dataChanges=[];const e=this.data.datasets.length,i=e=>new Set(t.filter((t=>t[0]===e)).map(((t,e)=>e+","+t.splice(1).join(",")))),s=i(0);for(let t=1;tt.split(","))).map((t=>({method:t[1],start:+t[2],count:+t[3]})))}_updateLayout(t){if(!1===this.notifyPlugins("beforeLayout",{cancelable:!0}))return;Zi.update(this,this.width,this.height,t);const e=this.chartArea,i=e.width<=0||e.height<=0;this._layers=[],d(this.boxes,(t=>{i&&"chartArea"===t.position||(t.configure&&t.configure(),this._layers.push(...t._layers()))}),this),this._layers.forEach(((t,e)=>{t._idx=e})),this.notifyPlugins("afterLayout")}_updateDatasets(t){if(!1!==this.notifyPlugins("beforeDatasetsUpdate",{mode:t,cancelable:!0})){for(let t=0,e=this.data.datasets.length;t=0;--e)this._drawDataset(t[e]);this.notifyPlugins("afterDatasetsDraw")}_drawDataset(t){const e=this.ctx,i=t._clip,s=!i.disabled,n=this.chartArea,o={meta:t,index:t.index,cancelable:!0};!1!==this.notifyPlugins("beforeDatasetDraw",o)&&(s&&Pe(e,{left:!1===i.left?0:n.left-i.left,right:!1===i.right?this.width:n.right+i.right,top:!1===i.top?0:n.top-i.top,bottom:!1===i.bottom?this.height:n.bottom+i.bottom}),t.controller.draw(),s&&De(e),o.cancelable=!1,this.notifyPlugins("afterDatasetDraw",o))}isPointInArea(t){return Se(t,this.chartArea,this._minPadding)}getElementsAtEventForMode(t,e,i,s){const n=Vi.modes[e];return"function"==typeof n?n(this,t,i,s):[]}getDatasetMeta(t){const e=this.data.datasets[t],i=this._metasets;let s=i.filter((t=>t&&t._dataset===e)).pop();return s||(s={type:null,data:[],dataset:null,controller:null,hidden:null,xAxisID:null,yAxisID:null,order:e&&e.order||0,index:t,_dataset:e,_parsed:[],_sorted:!1},i.push(s)),s}getContext(){return this.$context||(this.$context=_i(null,{chart:this,type:"chart"}))}getVisibleDatasetCount(){return this.getSortedVisibleDatasetMetas().length}isDatasetVisible(t){const e=this.data.datasets[t];if(!e)return!1;const i=this.getDatasetMeta(t);return"boolean"==typeof i.hidden?!i.hidden:!e.hidden}setDatasetVisibility(t,e){this.getDatasetMeta(t).hidden=!e}toggleDataVisibility(t){this._hiddenIndices[t]=!this._hiddenIndices[t]}getDataVisibility(t){return!this._hiddenIndices[t]}_updateVisibility(t,e,i){const s=i?"show":"hide",n=this.getDatasetMeta(t),o=n.controller._resolveAnimations(void 0,s);M(e)?(n.data[e].hidden=!i,this.update()):(this.setDatasetVisibility(t,i),o.update(n,{visible:i}),this.update((e=>e.datasetIndex===t?s:void 0)))}hide(t,e){this._updateVisibility(t,e,!1)}show(t,e){this._updateVisibility(t,e,!0)}_destroyDatasetMeta(t){const e=this._metasets[t];e&&e.controller&&e.controller._destroy(),delete this._metasets[t]}_stop(){let t,e;for(this.stop(),mt.remove(this),t=0,e=this.data.datasets.length;t{e.addEventListener(this,i,s),t[i]=s},s=(t,e,i)=>{t.offsetX=e,t.offsetY=i,this._eventHandler(t)};d(this.options.events,(t=>i(t,s)))}bindResponsiveEvents(){this._responsiveListeners||(this._responsiveListeners={});const t=this._responsiveListeners,e=this.platform,i=(i,s)=>{e.addEventListener(this,i,s),t[i]=s},s=(i,s)=>{t[i]&&(e.removeEventListener(this,i,s),delete t[i])},n=(t,e)=>{this.canvas&&this.resize(t,e)};let o;const a=()=>{s("attach",a),this.attached=!0,this.resize(),i("resize",n),i("detach",o)};o=()=>{this.attached=!1,s("resize",n),this._stop(),this._resize(0,0),i("attach",a)},e.isAttached(this.canvas)?a():o()}unbindEvents(){d(this._listeners,((t,e)=>{this.platform.removeEventListener(this,e,t)})),this._listeners={},d(this._responsiveListeners,((t,e)=>{this.platform.removeEventListener(this,e,t)})),this._responsiveListeners=void 0}updateHoverStyle(t,e,i){const s=i?"set":"remove";let n,o,a,r;for("dataset"===e&&(n=this.getDatasetMeta(t[0].datasetIndex),n.controller["_"+s+"DatasetHoverStyle"]()),a=0,r=t.length;a{const i=this.getDatasetMeta(t);if(!i)throw new Error("No dataset found at index "+t);return{datasetIndex:t,element:i.data[e],index:e}}));!u(i,e)&&(this._active=i,this._lastEvent=null,this._updateHoverStyles(i,e))}notifyPlugins(t,e,i){return this._plugins.notify(this,t,e,i)}_updateHoverStyles(t,e,i){const s=this.options.hover,n=(t,e)=>t.filter((t=>!e.some((e=>t.datasetIndex===e.datasetIndex&&t.index===e.index)))),o=n(e,t),a=i?t:n(t,e);o.length&&this.updateHoverStyle(o,s.mode,!1),a.length&&s.mode&&this.updateHoverStyle(a,s.mode,!0)}_eventHandler(t,e){const i={event:t,replay:e,cancelable:!0,inChartArea:this.isPointInArea(t)},s=e=>(e.options.events||this.options.events).includes(t.native.type);if(!1===this.notifyPlugins("beforeEvent",i,s))return;const n=this._handleEvent(t,e,i.inChartArea);return i.cancelable=!1,this.notifyPlugins("afterEvent",i,s),(n||i.changed)&&this.render(),this}_handleEvent(t,e,i){const{_active:s=[],options:n}=this,o=e,a=this._getActiveElements(t,s,i,o),r=P(t),l=function(t,e,i,s){return i&&"mouseout"!==t.type?s?e:t:null}(t,this._lastEvent,i,r);i&&(this._lastEvent=null,c(n.onHover,[t,a,this],this),r&&c(n.onClick,[t,a,this],this));const h=!u(a,s);return(h||e)&&(this._active=a,this._updateHoverStyles(a,s,e)),this._lastEvent=l,h}_getActiveElements(t,e,i,s){if("mouseout"===t.type)return[];if(!i)return e;const n=this.options.hover;return this.getElementsAtEventForMode(t,n.mode,n,s)}}const xn=()=>d(bn.instances,(t=>t._plugins.invalidate())),_n=!0;function yn(){throw new Error("This method is not implemented: Check that a complete date adapter is provided.")}Object.defineProperties(bn,{defaults:{enumerable:_n,value:ne},instances:{enumerable:_n,value:gn},overrides:{enumerable:_n,value:te},registry:{enumerable:_n,value:Us},version:{enumerable:_n,value:"3.9.1"},getChart:{enumerable:_n,value:pn},register:{enumerable:_n,value:(...t)=>{Us.add(...t),xn()}},unregister:{enumerable:_n,value:(...t)=>{Us.remove(...t),xn()}}});class vn{constructor(t){this.options=t||{}}init(t){}formats(){return yn()}parse(t,e){return yn()}format(t,e){return yn()}add(t,e,i){return yn()}diff(t,e,i){return yn()}startOf(t,e,i){return yn()}endOf(t,e){return yn()}}vn.override=function(t){Object.assign(vn.prototype,t)};var wn={_date:vn};function Mn(t){const e=t.iScale,i=function(t,e){if(!t._cache.$bar){const i=t.getMatchingVisibleMetas(e);let s=[];for(let e=0,n=i.length;et-e)))}return t._cache.$bar}(e,t.type);let s,n,o,a,r=e._length;const l=()=>{32767!==o&&-32768!==o&&(M(a)&&(r=Math.min(r,Math.abs(o-a)||r)),a=o)};for(s=0,n=i.length;sMath.abs(r)&&(l=r,h=a),e[i.axis]=h,e._custom={barStart:l,barEnd:h,start:n,end:o,min:a,max:r}}(t,e,i,n):e[i.axis]=i.parse(t,n),e}function Sn(t,e,i,s){const n=t.iScale,o=t.vScale,a=n.getLabels(),r=n===o,l=[];let h,c,d,u;for(h=i,c=i+s;ht.x,i="left",s="right"):(e=t.baset.controller.options.grouped)),o=s.options.stacked,a=[],r=t=>{const s=t.controller.getParsed(e),n=s&&s[t.vScale.axis];if(i(n)||isNaN(n))return!0};for(const i of n)if((void 0===e||!r(i))&&((!1===o||-1===a.indexOf(i.stack)||void 0===o&&void 0===i.stack)&&a.push(i.stack),i.index===t))break;return a.length||a.push(void 0),a}_getStackCount(t){return this._getStacks(void 0,t).length}_getStackIndex(t,e,i){const s=this._getStacks(t,i),n=void 0!==e?s.indexOf(e):-1;return-1===n?s.length-1:n}_getRuler(){const t=this.options,e=this._cachedMeta,i=e.iScale,s=[];let n,o;for(n=0,o=e.data.length;n=i?1:-1)}(d,e,a)*o,u===a&&(m-=d/2);const t=e.getPixelForDecimal(0),i=e.getPixelForDecimal(1),s=Math.min(t,i),n=Math.max(t,i);m=Math.max(Math.min(m,n),s),c=m+d}if(m===e.getPixelForValue(a)){const t=z(d)*e.getLineWidthForValue(a)/2;m+=t,d-=t}return{size:d,base:m,head:c,center:c+d/2}}_calculateBarIndexPixels(t,e){const s=e.scale,n=this.options,o=n.skipNull,a=r(n.maxBarThickness,1/0);let l,h;if(e.grouped){const s=o?this._getStackCount(t):e.stackCount,r="flex"===n.barThickness?function(t,e,i,s){const n=e.pixels,o=n[t];let a=t>0?n[t-1]:null,r=t=0;--i)e=Math.max(e,t[i].size(this.resolveDataElementOptions(i))/2);return e>0&&e}getLabelAndValue(t){const e=this._cachedMeta,{xScale:i,yScale:s}=e,n=this.getParsed(t),o=i.getLabelForValue(n.x),a=s.getLabelForValue(n.y),r=n._custom;return{label:e.label,value:"("+o+", "+a+(r?", "+r:"")+")"}}update(t){const e=this._cachedMeta.data;this.updateElements(e,0,e.length,t)}updateElements(t,e,i,s){const n="reset"===s,{iScale:o,vScale:a}=this._cachedMeta,{sharedOptions:r,includeOptions:l}=this._getSharedOptions(e,s),h=o.axis,c=a.axis;for(let d=e;d""}}}};class En extends Ls{constructor(t,e){super(t,e),this.enableOptionSharing=!0,this.innerRadius=void 0,this.outerRadius=void 0,this.offsetX=void 0,this.offsetY=void 0}linkScales(){}parse(t,e){const i=this.getDataset().data,s=this._cachedMeta;if(!1===this._parsing)s._parsed=i;else{let o,a,r=t=>+i[t];if(n(i[t])){const{key:t="value"}=this._parsing;r=e=>+y(i[e],t)}for(o=t,a=t+e;oG(t,r,l,!0)?1:Math.max(e,e*i,s,s*i),g=(t,e,s)=>G(t,r,l,!0)?-1:Math.min(e,e*i,s,s*i),p=f(0,h,d),m=f(L,c,u),b=g(D,h,d),x=g(D+L,c,u);s=(p-b)/2,n=(m-x)/2,o=-(p+b)/2,a=-(m+x)/2}return{ratioX:s,ratioY:n,offsetX:o,offsetY:a}}(u,d,r),b=(i.width-o)/f,x=(i.height-o)/g,_=Math.max(Math.min(b,x)/2,0),y=h(this.options.radius,_),v=(y-Math.max(y*r,0))/this._getVisibleDatasetWeightTotal();this.offsetX=p*y,this.offsetY=m*y,s.total=this.calculateTotal(),this.outerRadius=y-v*this._getRingWeightOffset(this.index),this.innerRadius=Math.max(this.outerRadius-v*c,0),this.updateElements(n,0,n.length,t)}_circumference(t,e){const i=this.options,s=this._cachedMeta,n=this._getCircumference();return e&&i.animation.animateRotate||!this.chart.getDataVisibility(t)||null===s._parsed[t]||s.data[t].hidden?0:this.calculateCircumference(s._parsed[t]*n/O)}updateElements(t,e,i,s){const n="reset"===s,o=this.chart,a=o.chartArea,r=o.options.animation,l=(a.left+a.right)/2,h=(a.top+a.bottom)/2,c=n&&r.animateScale,d=c?0:this.innerRadius,u=c?0:this.outerRadius,{sharedOptions:f,includeOptions:g}=this._getSharedOptions(e,s);let p,m=this._getRotation();for(p=0;p0&&!isNaN(t)?O*(Math.abs(t)/e):0}getLabelAndValue(t){const e=this._cachedMeta,i=this.chart,s=i.data.labels||[],n=li(e._parsed[t],i.options.locale);return{label:s[t]||"",value:n}}getMaxBorderWidth(t){let e=0;const i=this.chart;let s,n,o,a,r;if(!t)for(s=0,n=i.data.datasets.length;s"spacing"!==t,_indexable:t=>"spacing"!==t},En.overrides={aspectRatio:1,plugins:{legend:{labels:{generateLabels(t){const e=t.data;if(e.labels.length&&e.datasets.length){const{labels:{pointStyle:i}}=t.legend.options;return e.labels.map(((e,s)=>{const n=t.getDatasetMeta(0).controller.getStyle(s);return{text:e,fillStyle:n.backgroundColor,strokeStyle:n.borderColor,lineWidth:n.borderWidth,pointStyle:i,hidden:!t.getDataVisibility(s),index:s}}))}return[]}},onClick(t,e,i){i.chart.toggleDataVisibility(e.index),i.chart.update()}},tooltip:{callbacks:{title:()=>"",label(t){let e=t.label;const i=": "+t.formattedValue;return s(e)?(e=e.slice(),e[0]+=i):e+=i,e}}}}};class Rn extends Ls{initialize(){this.enableOptionSharing=!0,this.supportsDecimation=!0,super.initialize()}update(t){const e=this._cachedMeta,{dataset:i,data:s=[],_dataset:n}=e,o=this.chart._animationsDisabled;let{start:a,count:r}=gt(e,s,o);this._drawStart=a,this._drawCount=r,pt(e)&&(a=0,r=s.length),i._chart=this.chart,i._datasetIndex=this.index,i._decimated=!!n._decimated,i.points=s;const l=this.resolveDatasetElementOptions(t);this.options.showLine||(l.borderWidth=0),l.segment=this.options.segment,this.updateElement(i,void 0,{animated:!o,options:l},t),this.updateElements(s,a,r,t)}updateElements(t,e,s,n){const o="reset"===n,{iScale:a,vScale:r,_stacked:l,_dataset:h}=this._cachedMeta,{sharedOptions:c,includeOptions:d}=this._getSharedOptions(e,n),u=a.axis,f=r.axis,{spanGaps:g,segment:p}=this.options,m=B(g)?g:Number.POSITIVE_INFINITY,b=this.chart._animationsDisabled||o||"none"===n;let x=e>0&&this.getParsed(e-1);for(let g=e;g0&&Math.abs(s[u]-x[u])>m,p&&(_.parsed=s,_.raw=h.data[g]),d&&(_.options=c||this.resolveDataElementOptions(g,e.active?"active":n)),b||this.updateElement(e,g,_,n),x=s}}getMaxOverflow(){const t=this._cachedMeta,e=t.dataset,i=e.options&&e.options.borderWidth||0,s=t.data||[];if(!s.length)return i;const n=s[0].size(this.resolveDataElementOptions(0)),o=s[s.length-1].size(this.resolveDataElementOptions(s.length-1));return Math.max(i,n,o)/2}draw(){const t=this._cachedMeta;t.dataset.updateControlPoints(this.chart.chartArea,t.iScale.axis),super.draw()}}Rn.id="line",Rn.defaults={datasetElementType:"line",dataElementType:"point",showLine:!0,spanGaps:!1},Rn.overrides={scales:{_index_:{type:"category"},_value_:{type:"linear"}}};class In extends Ls{constructor(t,e){super(t,e),this.innerRadius=void 0,this.outerRadius=void 0}getLabelAndValue(t){const e=this._cachedMeta,i=this.chart,s=i.data.labels||[],n=li(e._parsed[t].r,i.options.locale);return{label:s[t]||"",value:n}}parseObjectData(t,e,i,s){return Ue.bind(this)(t,e,i,s)}update(t){const e=this._cachedMeta.data;this._updateRadius(),this.updateElements(e,0,e.length,t)}getMinMax(){const t=this._cachedMeta,e={min:Number.POSITIVE_INFINITY,max:Number.NEGATIVE_INFINITY};return t.data.forEach(((t,i)=>{const s=this.getParsed(i).r;!isNaN(s)&&this.chart.getDataVisibility(i)&&(se.max&&(e.max=s))})),e}_updateRadius(){const t=this.chart,e=t.chartArea,i=t.options,s=Math.min(e.right-e.left,e.bottom-e.top),n=Math.max(s/2,0),o=(n-Math.max(i.cutoutPercentage?n/100*i.cutoutPercentage:1,0))/t.getVisibleDatasetCount();this.outerRadius=n-o*this.index,this.innerRadius=this.outerRadius-o}updateElements(t,e,i,s){const n="reset"===s,o=this.chart,a=o.options.animation,r=this._cachedMeta.rScale,l=r.xCenter,h=r.yCenter,c=r.getIndexAngle(0)-.5*D;let d,u=c;const f=360/this.countVisibleElements();for(d=0;d{!isNaN(this.getParsed(i).r)&&this.chart.getDataVisibility(i)&&e++})),e}_computeAngle(t,e,i){return this.chart.getDataVisibility(t)?H(this.resolveDataElementOptions(t,e).angle||i):0}}In.id="polarArea",In.defaults={dataElementType:"arc",animation:{animateRotate:!0,animateScale:!0},animations:{numbers:{type:"number",properties:["x","y","startAngle","endAngle","innerRadius","outerRadius"]}},indexAxis:"r",startAngle:0},In.overrides={aspectRatio:1,plugins:{legend:{labels:{generateLabels(t){const e=t.data;if(e.labels.length&&e.datasets.length){const{labels:{pointStyle:i}}=t.legend.options;return e.labels.map(((e,s)=>{const n=t.getDatasetMeta(0).controller.getStyle(s);return{text:e,fillStyle:n.backgroundColor,strokeStyle:n.borderColor,lineWidth:n.borderWidth,pointStyle:i,hidden:!t.getDataVisibility(s),index:s}}))}return[]}},onClick(t,e,i){i.chart.toggleDataVisibility(e.index),i.chart.update()}},tooltip:{callbacks:{title:()=>"",label:t=>t.chart.data.labels[t.dataIndex]+": "+t.formattedValue}}},scales:{r:{type:"radialLinear",angleLines:{display:!1},beginAtZero:!0,grid:{circular:!0},pointLabels:{display:!1},startAngle:0}}};class zn extends En{}zn.id="pie",zn.defaults={cutout:0,rotation:0,circumference:360,radius:"100%"};class Fn extends Ls{getLabelAndValue(t){const e=this._cachedMeta.vScale,i=this.getParsed(t);return{label:e.getLabels()[t],value:""+e.getLabelForValue(i[e.axis])}}parseObjectData(t,e,i,s){return Ue.bind(this)(t,e,i,s)}update(t){const e=this._cachedMeta,i=e.dataset,s=e.data||[],n=e.iScale.getLabels();if(i.points=s,"resize"!==t){const e=this.resolveDatasetElementOptions(t);this.options.showLine||(e.borderWidth=0);const o={_loop:!0,_fullLoop:n.length===s.length,options:e};this.updateElement(i,void 0,o,t)}this.updateElements(s,0,s.length,t)}updateElements(t,e,i,s){const n=this._cachedMeta.rScale,o="reset"===s;for(let a=e;a0&&this.getParsed(e-1);for(let c=e;c0&&Math.abs(s[f]-_[f])>b,m&&(p.parsed=s,p.raw=h.data[c]),u&&(p.options=d||this.resolveDataElementOptions(c,e.active?"active":n)),x||this.updateElement(e,c,p,n),_=s}this.updateSharedOptions(d,n,c)}getMaxOverflow(){const t=this._cachedMeta,e=t.data||[];if(!this.options.showLine){let t=0;for(let i=e.length-1;i>=0;--i)t=Math.max(t,e[i].size(this.resolveDataElementOptions(i))/2);return t>0&&t}const i=t.dataset,s=i.options&&i.options.borderWidth||0;if(!e.length)return s;const n=e[0].size(this.resolveDataElementOptions(0)),o=e[e.length-1].size(this.resolveDataElementOptions(e.length-1));return Math.max(s,n,o)/2}}Vn.id="scatter",Vn.defaults={datasetElementType:!1,dataElementType:"point",showLine:!1,fill:!1},Vn.overrides={interaction:{mode:"point"},plugins:{tooltip:{callbacks:{title:()=>"",label:t=>"("+t.label+", "+t.formattedValue+")"}}},scales:{x:{type:"linear"},y:{type:"linear"}}};var Bn=Object.freeze({__proto__:null,BarController:Tn,BubbleController:Ln,DoughnutController:En,LineController:Rn,PolarAreaController:In,PieController:zn,RadarController:Fn,ScatterController:Vn});function Nn(t,e,i){const{startAngle:s,pixelMargin:n,x:o,y:a,outerRadius:r,innerRadius:l}=e;let h=n/r;t.beginPath(),t.arc(o,a,r,s-h,i+h),l>n?(h=n/l,t.arc(o,a,l,i+h,s-h,!0)):t.arc(o,a,n,i+L,s-L),t.closePath(),t.clip()}function Wn(t,e,i,s){const n=ui(t.options.borderRadius,["outerStart","outerEnd","innerStart","innerEnd"]);const o=(i-e)/2,a=Math.min(o,s*e/2),r=t=>{const e=(i-Math.min(o,t))*s/2;return Z(t,0,Math.min(o,e))};return{outerStart:r(n.outerStart),outerEnd:r(n.outerEnd),innerStart:Z(n.innerStart,0,a),innerEnd:Z(n.innerEnd,0,a)}}function jn(t,e,i,s){return{x:i+t*Math.cos(e),y:s+t*Math.sin(e)}}function Hn(t,e,i,s,n,o){const{x:a,y:r,startAngle:l,pixelMargin:h,innerRadius:c}=e,d=Math.max(e.outerRadius+s+i-h,0),u=c>0?c+s+i+h:0;let f=0;const g=n-l;if(s){const t=((c>0?c-s:0)+(d>0?d-s:0))/2;f=(g-(0!==t?g*t/(t+s):g))/2}const p=(g-Math.max(.001,g*d-i/D)/d)/2,m=l+p+f,b=n-p-f,{outerStart:x,outerEnd:_,innerStart:y,innerEnd:v}=Wn(e,u,d,b-m),w=d-x,M=d-_,k=m+x/w,S=b-_/M,P=u+y,O=u+v,C=m+y/P,A=b-v/O;if(t.beginPath(),o){if(t.arc(a,r,d,k,S),_>0){const e=jn(M,S,a,r);t.arc(e.x,e.y,_,S,b+L)}const e=jn(O,b,a,r);if(t.lineTo(e.x,e.y),v>0){const e=jn(O,A,a,r);t.arc(e.x,e.y,v,b+L,A+Math.PI)}if(t.arc(a,r,u,b-v/u,m+y/u,!0),y>0){const e=jn(P,C,a,r);t.arc(e.x,e.y,y,C+Math.PI,m-L)}const i=jn(w,m,a,r);if(t.lineTo(i.x,i.y),x>0){const e=jn(w,k,a,r);t.arc(e.x,e.y,x,m-L,k)}}else{t.moveTo(a,r);const e=Math.cos(k)*d+a,i=Math.sin(k)*d+r;t.lineTo(e,i);const s=Math.cos(S)*d+a,n=Math.sin(S)*d+r;t.lineTo(s,n)}t.closePath()}function $n(t,e,i,s,n,o){const{options:a}=e,{borderWidth:r,borderJoinStyle:l}=a,h="inner"===a.borderAlign;r&&(h?(t.lineWidth=2*r,t.lineJoin=l||"round"):(t.lineWidth=r,t.lineJoin=l||"bevel"),e.fullCircles&&function(t,e,i){const{x:s,y:n,startAngle:o,pixelMargin:a,fullCircles:r}=e,l=Math.max(e.outerRadius-a,0),h=e.innerRadius+a;let c;for(i&&Nn(t,e,o+O),t.beginPath(),t.arc(s,n,h,o+O,o,!0),c=0;c=O||G(n,a,l),g=Q(o,h+u,c+u);return f&&g}getCenterPoint(t){const{x:e,y:i,startAngle:s,endAngle:n,innerRadius:o,outerRadius:a}=this.getProps(["x","y","startAngle","endAngle","innerRadius","outerRadius","circumference"],t),{offset:r,spacing:l}=this.options,h=(s+n)/2,c=(o+a+l+r)/2;return{x:e+Math.cos(h)*c,y:i+Math.sin(h)*c}}tooltipPosition(t){return this.getCenterPoint(t)}draw(t){const{options:e,circumference:i}=this,s=(e.offset||0)/2,n=(e.spacing||0)/2,o=e.circular;if(this.pixelMargin="inner"===e.borderAlign?.33:0,this.fullCircles=i>O?Math.floor(i/O):0,0===i||this.innerRadius<0||this.outerRadius<0)return;t.save();let a=0;if(s){a=s/2;const e=(this.startAngle+this.endAngle)/2;t.translate(Math.cos(e)*a,Math.sin(e)*a),this.circumference>=D&&(a=s)}t.fillStyle=e.backgroundColor,t.strokeStyle=e.borderColor;const r=function(t,e,i,s,n){const{fullCircles:o,startAngle:a,circumference:r}=e;let l=e.endAngle;if(o){Hn(t,e,i,s,a+O,n);for(let e=0;er&&o>r;return{count:s,start:l,loop:e.loop,ilen:h(a+(h?r-t:t))%o,_=()=>{f!==g&&(t.lineTo(m,g),t.lineTo(m,f),t.lineTo(m,p))};for(l&&(d=n[x(0)],t.moveTo(d.x,d.y)),c=0;c<=r;++c){if(d=n[x(c)],d.skip)continue;const e=d.x,i=d.y,s=0|e;s===u?(ig&&(g=i),m=(b*m+e)/++b):(_(),t.lineTo(e,i),u=s,b=0,f=g=i),p=i}_()}function Zn(t){const e=t.options,i=e.borderDash&&e.borderDash.length;return!(t._decimated||t._loop||e.tension||"monotone"===e.cubicInterpolationMode||e.stepped||i)?Gn:Kn}Yn.id="arc",Yn.defaults={borderAlign:"center",borderColor:"#fff",borderJoinStyle:void 0,borderRadius:0,borderWidth:2,offset:0,spacing:0,angle:void 0,circular:!0},Yn.defaultRoutes={backgroundColor:"backgroundColor"};const Jn="function"==typeof Path2D;function Qn(t,e,i,s){Jn&&!e.options.segment?function(t,e,i,s){let n=e._path;n||(n=e._path=new Path2D,e.path(n,i,s)&&n.closePath()),Un(t,e.options),t.stroke(n)}(t,e,i,s):function(t,e,i,s){const{segments:n,options:o}=e,a=Zn(e);for(const r of n)Un(t,o,r.style),t.beginPath(),a(t,e,r,{start:i,end:i+s-1})&&t.closePath(),t.stroke()}(t,e,i,s)}class to extends Es{constructor(t){super(),this.animated=!0,this.options=void 0,this._chart=void 0,this._loop=void 0,this._fullLoop=void 0,this._path=void 0,this._points=void 0,this._segments=void 0,this._decimated=!1,this._pointsUpdated=!1,this._datasetIndex=void 0,t&&Object.assign(this,t)}updateControlPoints(t,e){const i=this.options;if((i.tension||"monotone"===i.cubicInterpolationMode)&&!i.stepped&&!this._pointsUpdated){const s=i.spanGaps?this._loop:this._fullLoop;Qe(this._points,i,t,s,e),this._pointsUpdated=!0}}set points(t){this._points=t,delete this._segments,delete this._path,this._pointsUpdated=!1}get points(){return this._points}get segments(){return this._segments||(this._segments=Di(this,this.options.segment))}first(){const t=this.segments,e=this.points;return t.length&&e[t[0].start]}last(){const t=this.segments,e=this.points,i=t.length;return i&&e[t[i-1].end]}interpolate(t,e){const i=this.options,s=t[e],n=this.points,o=Pi(this,{property:e,start:s,end:s});if(!o.length)return;const a=[],r=function(t){return t.stepped?oi:t.tension||"monotone"===t.cubicInterpolationMode?ai:ni}(i);let l,h;for(l=0,h=o.length;l"borderDash"!==t&&"fill"!==t};class io extends Es{constructor(t){super(),this.options=void 0,this.parsed=void 0,this.skip=void 0,this.stop=void 0,t&&Object.assign(this,t)}inRange(t,e,i){const s=this.options,{x:n,y:o}=this.getProps(["x","y"],i);return Math.pow(t-n,2)+Math.pow(e-o,2){uo(t)}))}var go={id:"decimation",defaults:{algorithm:"min-max",enabled:!1},beforeElementsUpdate:(t,e,s)=>{if(!s.enabled)return void fo(t);const n=t.width;t.data.datasets.forEach(((e,o)=>{const{_data:a,indexAxis:r}=e,l=t.getDatasetMeta(o),h=a||e.data;if("y"===bi([r,t.options.indexAxis]))return;if(!l.controller.supportsDecimation)return;const c=t.scales[l.xAxisID];if("linear"!==c.type&&"time"!==c.type)return;if(t.options.parsing)return;let{start:d,count:u}=function(t,e){const i=e.length;let s,n=0;const{iScale:o}=t,{min:a,max:r,minDefined:l,maxDefined:h}=o.getUserBounds();return l&&(n=Z(et(e,o.axis,a).lo,0,i-1)),s=h?Z(et(e,o.axis,r).hi+1,n,i)-n:i-n,{start:n,count:s}}(l,h);if(u<=(s.threshold||4*n))return void uo(e);let f;switch(i(a)&&(e._data=h,delete e.data,Object.defineProperty(e,"data",{configurable:!0,enumerable:!0,get:function(){return this._decimated},set:function(t){this._data=t}})),s.algorithm){case"lttb":f=function(t,e,i,s,n){const o=n.samples||s;if(o>=i)return t.slice(e,e+i);const a=[],r=(i-2)/(o-2);let l=0;const h=e+i-1;let c,d,u,f,g,p=e;for(a[l++]=t[p],c=0;cu&&(u=f,d=t[s],g=s);a[l++]=d,p=g}return a[l++]=t[h],a}(h,d,u,n,s);break;case"min-max":f=function(t,e,s,n){let o,a,r,l,h,c,d,u,f,g,p=0,m=0;const b=[],x=e+s-1,_=t[e].x,y=t[x].x-_;for(o=e;og&&(g=l,d=o),p=(m*p+a.x)/++m;else{const s=o-1;if(!i(c)&&!i(d)){const e=Math.min(c,d),i=Math.max(c,d);e!==u&&e!==s&&b.push({...t[e],x:p}),i!==u&&i!==s&&b.push({...t[i],x:p})}o>0&&s!==u&&b.push(t[s]),b.push(a),h=e,m=0,f=g=l,c=d=u=o}}return b}(h,d,u,n);break;default:throw new Error(`Unsupported decimation algorithm '${s.algorithm}'`)}e._decimated=f}))},destroy(t){fo(t)}};function po(t,e,i,s){if(s)return;let n=e[t],o=i[t];return"angle"===t&&(n=K(n),o=K(o)),{property:t,start:n,end:o}}function mo(t,e,i){for(;e>t;e--){const t=i[e];if(!isNaN(t.x)&&!isNaN(t.y))break}return e}function bo(t,e,i,s){return t&&e?s(t[i],e[i]):t?t[i]:e?e[i]:0}function xo(t,e){let i=[],n=!1;return s(t)?(n=!0,i=t):i=function(t,e){const{x:i=null,y:s=null}=t||{},n=e.points,o=[];return e.segments.forEach((({start:t,end:e})=>{e=mo(t,e,n);const a=n[t],r=n[e];null!==s?(o.push({x:a.x,y:s}),o.push({x:r.x,y:s})):null!==i&&(o.push({x:i,y:a.y}),o.push({x:i,y:r.y}))})),o}(t,e),i.length?new to({points:i,options:{tension:0},_loop:n,_fullLoop:n}):null}function _o(t){return t&&!1!==t.fill}function yo(t,e,i){let s=t[e].fill;const n=[e];let a;if(!i)return s;for(;!1!==s&&-1===n.indexOf(s);){if(!o(s))return s;if(a=t[s],!a)return!1;if(a.visible)return s;n.push(s),s=a.fill}return!1}function vo(t,e,i){const s=function(t){const e=t.options,i=e.fill;let s=r(i&&i.target,i);void 0===s&&(s=!!e.backgroundColor);if(!1===s||null===s)return!1;if(!0===s)return"origin";return s}(t);if(n(s))return!isNaN(s.value)&&s;let a=parseFloat(s);return o(a)&&Math.floor(a)===a?function(t,e,i,s){"-"!==t&&"+"!==t||(i=e+i);if(i===e||i<0||i>=s)return!1;return i}(s[0],e,a,i):["origin","start","end","stack","shape"].indexOf(s)>=0&&s}function wo(t,e,i){const s=[];for(let n=0;n=0;--e){const i=n[e].$filler;i&&(i.line.updateControlPoints(o,i.axis),s&&i.fill&&Po(t.ctx,i,o))}},beforeDatasetsDraw(t,e,i){if("beforeDatasetsDraw"!==i.drawTime)return;const s=t.getSortedVisibleDatasetMetas();for(let e=s.length-1;e>=0;--e){const i=s[e].$filler;_o(i)&&Po(t.ctx,i,t.chartArea)}},beforeDatasetDraw(t,e,i){const s=e.meta.$filler;_o(s)&&"beforeDatasetDraw"===i.drawTime&&Po(t.ctx,s,t.chartArea)},defaults:{propagate:!0,drawTime:"beforeDatasetDraw"}};const Lo=(t,e)=>{let{boxHeight:i=e,boxWidth:s=e}=t;return t.usePointStyle&&(i=Math.min(i,e),s=t.pointStyleWidth||Math.min(s,e)),{boxWidth:s,boxHeight:i,itemHeight:Math.max(e,i)}};class Eo extends Es{constructor(t){super(),this._added=!1,this.legendHitBoxes=[],this._hoveredItem=null,this.doughnutMode=!1,this.chart=t.chart,this.options=t.options,this.ctx=t.ctx,this.legendItems=void 0,this.columnSizes=void 0,this.lineWidths=void 0,this.maxHeight=void 0,this.maxWidth=void 0,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.height=void 0,this.width=void 0,this._margins=void 0,this.position=void 0,this.weight=void 0,this.fullSize=void 0}update(t,e,i){this.maxWidth=t,this.maxHeight=e,this._margins=i,this.setDimensions(),this.buildLabels(),this.fit()}setDimensions(){this.isHorizontal()?(this.width=this.maxWidth,this.left=this._margins.left,this.right=this.width):(this.height=this.maxHeight,this.top=this._margins.top,this.bottom=this.height)}buildLabels(){const t=this.options.labels||{};let e=c(t.generateLabels,[this.chart],this)||[];t.filter&&(e=e.filter((e=>t.filter(e,this.chart.data)))),t.sort&&(e=e.sort(((e,i)=>t.sort(e,i,this.chart.data)))),this.options.reverse&&e.reverse(),this.legendItems=e}fit(){const{options:t,ctx:e}=this;if(!t.display)return void(this.width=this.height=0);const i=t.labels,s=mi(i.font),n=s.size,o=this._computeTitleHeight(),{boxWidth:a,itemHeight:r}=Lo(i,n);let l,h;e.font=s.string,this.isHorizontal()?(l=this.maxWidth,h=this._fitRows(o,n,a,r)+10):(h=this.maxHeight,l=this._fitCols(o,n,a,r)+10),this.width=Math.min(l,t.maxWidth||this.maxWidth),this.height=Math.min(h,t.maxHeight||this.maxHeight)}_fitRows(t,e,i,s){const{ctx:n,maxWidth:o,options:{labels:{padding:a}}}=this,r=this.legendHitBoxes=[],l=this.lineWidths=[0],h=s+a;let c=t;n.textAlign="left",n.textBaseline="middle";let d=-1,u=-h;return this.legendItems.forEach(((t,f)=>{const g=i+e/2+n.measureText(t.text).width;(0===f||l[l.length-1]+g+2*a>o)&&(c+=h,l[l.length-(f>0?0:1)]=0,u+=h,d++),r[f]={left:0,top:u,row:d,width:g,height:s},l[l.length-1]+=g+a})),c}_fitCols(t,e,i,s){const{ctx:n,maxHeight:o,options:{labels:{padding:a}}}=this,r=this.legendHitBoxes=[],l=this.columnSizes=[],h=o-t;let c=a,d=0,u=0,f=0,g=0;return this.legendItems.forEach(((t,o)=>{const p=i+e/2+n.measureText(t.text).width;o>0&&u+s+2*a>h&&(c+=d+a,l.push({width:d,height:u}),f+=d+a,g++,d=u=0),r[o]={left:f,top:u,col:g,width:p,height:s},d=Math.max(d,p),u+=s+a})),c+=d,l.push({width:d,height:u}),c}adjustHitBoxes(){if(!this.options.display)return;const t=this._computeTitleHeight(),{legendHitBoxes:e,options:{align:i,labels:{padding:s},rtl:n}}=this,o=yi(n,this.left,this.width);if(this.isHorizontal()){let n=0,a=ut(i,this.left+s,this.right-this.lineWidths[n]);for(const r of e)n!==r.row&&(n=r.row,a=ut(i,this.left+s,this.right-this.lineWidths[n])),r.top+=this.top+t+s,r.left=o.leftForLtr(o.x(a),r.width),a+=r.width+s}else{let n=0,a=ut(i,this.top+t+s,this.bottom-this.columnSizes[n].height);for(const r of e)r.col!==n&&(n=r.col,a=ut(i,this.top+t+s,this.bottom-this.columnSizes[n].height)),r.top=a,r.left+=this.left+s,r.left=o.leftForLtr(o.x(r.left),r.width),a+=r.height+s}}isHorizontal(){return"top"===this.options.position||"bottom"===this.options.position}draw(){if(this.options.display){const t=this.ctx;Pe(t,this),this._draw(),De(t)}}_draw(){const{options:t,columnSizes:e,lineWidths:i,ctx:s}=this,{align:n,labels:o}=t,a=ne.color,l=yi(t.rtl,this.left,this.width),h=mi(o.font),{color:c,padding:d}=o,u=h.size,f=u/2;let g;this.drawTitle(),s.textAlign=l.textAlign("left"),s.textBaseline="middle",s.lineWidth=.5,s.font=h.string;const{boxWidth:p,boxHeight:m,itemHeight:b}=Lo(o,u),x=this.isHorizontal(),_=this._computeTitleHeight();g=x?{x:ut(n,this.left+d,this.right-i[0]),y:this.top+d+_,line:0}:{x:this.left+d,y:ut(n,this.top+_+d,this.bottom-e[0].height),line:0},vi(this.ctx,t.textDirection);const y=b+d;this.legendItems.forEach(((v,w)=>{s.strokeStyle=v.fontColor||c,s.fillStyle=v.fontColor||c;const M=s.measureText(v.text).width,k=l.textAlign(v.textAlign||(v.textAlign=o.textAlign)),S=p+f+M;let P=g.x,D=g.y;l.setWidth(this.width),x?w>0&&P+S+d>this.right&&(D=g.y+=y,g.line++,P=g.x=ut(n,this.left+d,this.right-i[g.line])):w>0&&D+y>this.bottom&&(P=g.x=P+e[g.line].width+d,g.line++,D=g.y=ut(n,this.top+_+d,this.bottom-e[g.line].height));!function(t,e,i){if(isNaN(p)||p<=0||isNaN(m)||m<0)return;s.save();const n=r(i.lineWidth,1);if(s.fillStyle=r(i.fillStyle,a),s.lineCap=r(i.lineCap,"butt"),s.lineDashOffset=r(i.lineDashOffset,0),s.lineJoin=r(i.lineJoin,"miter"),s.lineWidth=n,s.strokeStyle=r(i.strokeStyle,a),s.setLineDash(r(i.lineDash,[])),o.usePointStyle){const a={radius:m*Math.SQRT2/2,pointStyle:i.pointStyle,rotation:i.rotation,borderWidth:n},r=l.xPlus(t,p/2);ke(s,a,r,e+f,o.pointStyleWidth&&p)}else{const o=e+Math.max((u-m)/2,0),a=l.leftForLtr(t,p),r=gi(i.borderRadius);s.beginPath(),Object.values(r).some((t=>0!==t))?Le(s,{x:a,y:o,w:p,h:m,radius:r}):s.rect(a,o,p,m),s.fill(),0!==n&&s.stroke()}s.restore()}(l.x(P),D,v),P=ft(k,P+p+f,x?P+S:this.right,t.rtl),function(t,e,i){Ae(s,i.text,t,e+b/2,h,{strikethrough:i.hidden,textAlign:l.textAlign(i.textAlign)})}(l.x(P),D,v),x?g.x+=S+d:g.y+=y})),wi(this.ctx,t.textDirection)}drawTitle(){const t=this.options,e=t.title,i=mi(e.font),s=pi(e.padding);if(!e.display)return;const n=yi(t.rtl,this.left,this.width),o=this.ctx,a=e.position,r=i.size/2,l=s.top+r;let h,c=this.left,d=this.width;if(this.isHorizontal())d=Math.max(...this.lineWidths),h=this.top+l,c=ut(t.align,c,this.right-d);else{const e=this.columnSizes.reduce(((t,e)=>Math.max(t,e.height)),0);h=l+ut(t.align,this.top,this.bottom-e-t.labels.padding-this._computeTitleHeight())}const u=ut(a,c,c+d);o.textAlign=n.textAlign(dt(a)),o.textBaseline="middle",o.strokeStyle=e.color,o.fillStyle=e.color,o.font=i.string,Ae(o,e.text,u,h,i)}_computeTitleHeight(){const t=this.options.title,e=mi(t.font),i=pi(t.padding);return t.display?e.lineHeight+i.height:0}_getLegendItemAt(t,e){let i,s,n;if(Q(t,this.left,this.right)&&Q(e,this.top,this.bottom))for(n=this.legendHitBoxes,i=0;it.chart.options.color,boxWidth:40,padding:10,generateLabels(t){const e=t.data.datasets,{labels:{usePointStyle:i,pointStyle:s,textAlign:n,color:o}}=t.legend.options;return t._getSortedDatasetMetas().map((t=>{const a=t.controller.getStyle(i?0:void 0),r=pi(a.borderWidth);return{text:e[t.index].label,fillStyle:a.backgroundColor,fontColor:o,hidden:!t.visible,lineCap:a.borderCapStyle,lineDash:a.borderDash,lineDashOffset:a.borderDashOffset,lineJoin:a.borderJoinStyle,lineWidth:(r.width+r.height)/4,strokeStyle:a.borderColor,pointStyle:s||a.pointStyle,rotation:a.rotation,textAlign:n||a.textAlign,borderRadius:0,datasetIndex:t.index}}),this)}},title:{color:t=>t.chart.options.color,display:!1,position:"center",text:""}},descriptors:{_scriptable:t=>!t.startsWith("on"),labels:{_scriptable:t=>!["generateLabels","filter","sort"].includes(t)}}};class Io extends Es{constructor(t){super(),this.chart=t.chart,this.options=t.options,this.ctx=t.ctx,this._padding=void 0,this.top=void 0,this.bottom=void 0,this.left=void 0,this.right=void 0,this.width=void 0,this.height=void 0,this.position=void 0,this.weight=void 0,this.fullSize=void 0}update(t,e){const i=this.options;if(this.left=0,this.top=0,!i.display)return void(this.width=this.height=this.right=this.bottom=0);this.width=this.right=t,this.height=this.bottom=e;const n=s(i.text)?i.text.length:1;this._padding=pi(i.padding);const o=n*mi(i.font).lineHeight+this._padding.height;this.isHorizontal()?this.height=o:this.width=o}isHorizontal(){const t=this.options.position;return"top"===t||"bottom"===t}_drawArgs(t){const{top:e,left:i,bottom:s,right:n,options:o}=this,a=o.align;let r,l,h,c=0;return this.isHorizontal()?(l=ut(a,i,n),h=e+t,r=n-i):("left"===o.position?(l=i+t,h=ut(a,s,e),c=-.5*D):(l=n-t,h=ut(a,e,s),c=.5*D),r=s-e),{titleX:l,titleY:h,maxWidth:r,rotation:c}}draw(){const t=this.ctx,e=this.options;if(!e.display)return;const i=mi(e.font),s=i.lineHeight/2+this._padding.top,{titleX:n,titleY:o,maxWidth:a,rotation:r}=this._drawArgs(s);Ae(t,e.text,0,0,i,{color:e.color,maxWidth:a,rotation:r,textAlign:dt(e.align),textBaseline:"middle",translation:[n,o]})}}var zo={id:"title",_element:Io,start(t,e,i){!function(t,e){const i=new Io({ctx:t.ctx,options:e,chart:t});Zi.configure(t,i,e),Zi.addBox(t,i),t.titleBlock=i}(t,i)},stop(t){const e=t.titleBlock;Zi.removeBox(t,e),delete t.titleBlock},beforeUpdate(t,e,i){const s=t.titleBlock;Zi.configure(t,s,i),s.options=i},defaults:{align:"center",display:!1,font:{weight:"bold"},fullSize:!0,padding:10,position:"top",text:"",weight:2e3},defaultRoutes:{color:"color"},descriptors:{_scriptable:!0,_indexable:!1}};const Fo=new WeakMap;var Vo={id:"subtitle",start(t,e,i){const s=new Io({ctx:t.ctx,options:i,chart:t});Zi.configure(t,s,i),Zi.addBox(t,s),Fo.set(t,s)},stop(t){Zi.removeBox(t,Fo.get(t)),Fo.delete(t)},beforeUpdate(t,e,i){const s=Fo.get(t);Zi.configure(t,s,i),s.options=i},defaults:{align:"center",display:!1,font:{weight:"normal"},fullSize:!0,padding:0,position:"top",text:"",weight:1500},defaultRoutes:{color:"color"},descriptors:{_scriptable:!0,_indexable:!1}};const Bo={average(t){if(!t.length)return!1;let e,i,s=0,n=0,o=0;for(e=0,i=t.length;e-1?t.split("\n"):t}function jo(t,e){const{element:i,datasetIndex:s,index:n}=e,o=t.getDatasetMeta(s).controller,{label:a,value:r}=o.getLabelAndValue(n);return{chart:t,label:a,parsed:o.getParsed(n),raw:t.data.datasets[s].data[n],formattedValue:r,dataset:o.getDataset(),dataIndex:n,datasetIndex:s,element:i}}function Ho(t,e){const i=t.chart.ctx,{body:s,footer:n,title:o}=t,{boxWidth:a,boxHeight:r}=e,l=mi(e.bodyFont),h=mi(e.titleFont),c=mi(e.footerFont),u=o.length,f=n.length,g=s.length,p=pi(e.padding);let m=p.height,b=0,x=s.reduce(((t,e)=>t+e.before.length+e.lines.length+e.after.length),0);if(x+=t.beforeBody.length+t.afterBody.length,u&&(m+=u*h.lineHeight+(u-1)*e.titleSpacing+e.titleMarginBottom),x){m+=g*(e.displayColors?Math.max(r,l.lineHeight):l.lineHeight)+(x-g)*l.lineHeight+(x-1)*e.bodySpacing}f&&(m+=e.footerMarginTop+f*c.lineHeight+(f-1)*e.footerSpacing);let _=0;const y=function(t){b=Math.max(b,i.measureText(t).width+_)};return i.save(),i.font=h.string,d(t.title,y),i.font=l.string,d(t.beforeBody.concat(t.afterBody),y),_=e.displayColors?a+2+e.boxPadding:0,d(s,(t=>{d(t.before,y),d(t.lines,y),d(t.after,y)})),_=0,i.font=c.string,d(t.footer,y),i.restore(),b+=p.width,{width:b,height:m}}function $o(t,e,i,s){const{x:n,width:o}=i,{width:a,chartArea:{left:r,right:l}}=t;let h="center";return"center"===s?h=n<=(r+l)/2?"left":"right":n<=o/2?h="left":n>=a-o/2&&(h="right"),function(t,e,i,s){const{x:n,width:o}=s,a=i.caretSize+i.caretPadding;return"left"===t&&n+o+a>e.width||"right"===t&&n-o-a<0||void 0}(h,t,e,i)&&(h="center"),h}function Yo(t,e,i){const s=i.yAlign||e.yAlign||function(t,e){const{y:i,height:s}=e;return it.height-s/2?"bottom":"center"}(t,i);return{xAlign:i.xAlign||e.xAlign||$o(t,e,i,s),yAlign:s}}function Uo(t,e,i,s){const{caretSize:n,caretPadding:o,cornerRadius:a}=t,{xAlign:r,yAlign:l}=i,h=n+o,{topLeft:c,topRight:d,bottomLeft:u,bottomRight:f}=gi(a);let g=function(t,e){let{x:i,width:s}=t;return"right"===e?i-=s:"center"===e&&(i-=s/2),i}(e,r);const p=function(t,e,i){let{y:s,height:n}=t;return"top"===e?s+=i:s-="bottom"===e?n+i:n/2,s}(e,l,h);return"center"===l?"left"===r?g+=h:"right"===r&&(g-=h):"left"===r?g-=Math.max(c,u)+n:"right"===r&&(g+=Math.max(d,f)+n),{x:Z(g,0,s.width-e.width),y:Z(p,0,s.height-e.height)}}function Xo(t,e,i){const s=pi(i.padding);return"center"===e?t.x+t.width/2:"right"===e?t.x+t.width-s.right:t.x+s.left}function qo(t){return No([],Wo(t))}function Ko(t,e){const i=e&&e.dataset&&e.dataset.tooltip&&e.dataset.tooltip.callbacks;return i?t.override(i):t}class Go extends Es{constructor(t){super(),this.opacity=0,this._active=[],this._eventPosition=void 0,this._size=void 0,this._cachedAnimations=void 0,this._tooltipItems=[],this.$animations=void 0,this.$context=void 0,this.chart=t.chart||t._chart,this._chart=this.chart,this.options=t.options,this.dataPoints=void 0,this.title=void 0,this.beforeBody=void 0,this.body=void 0,this.afterBody=void 0,this.footer=void 0,this.xAlign=void 0,this.yAlign=void 0,this.x=void 0,this.y=void 0,this.height=void 0,this.width=void 0,this.caretX=void 0,this.caretY=void 0,this.labelColors=void 0,this.labelPointStyles=void 0,this.labelTextColors=void 0}initialize(t){this.options=t,this._cachedAnimations=void 0,this.$context=void 0}_resolveAnimations(){const t=this._cachedAnimations;if(t)return t;const e=this.chart,i=this.options.setContext(this.getContext()),s=i.enabled&&e.options.animation&&i.animations,n=new ys(this.chart,s);return s._cacheable&&(this._cachedAnimations=Object.freeze(n)),n}getContext(){return this.$context||(this.$context=(t=this.chart.getContext(),e=this,i=this._tooltipItems,_i(t,{tooltip:e,tooltipItems:i,type:"tooltip"})));var t,e,i}getTitle(t,e){const{callbacks:i}=e,s=i.beforeTitle.apply(this,[t]),n=i.title.apply(this,[t]),o=i.afterTitle.apply(this,[t]);let a=[];return a=No(a,Wo(s)),a=No(a,Wo(n)),a=No(a,Wo(o)),a}getBeforeBody(t,e){return qo(e.callbacks.beforeBody.apply(this,[t]))}getBody(t,e){const{callbacks:i}=e,s=[];return d(t,(t=>{const e={before:[],lines:[],after:[]},n=Ko(i,t);No(e.before,Wo(n.beforeLabel.call(this,t))),No(e.lines,n.label.call(this,t)),No(e.after,Wo(n.afterLabel.call(this,t))),s.push(e)})),s}getAfterBody(t,e){return qo(e.callbacks.afterBody.apply(this,[t]))}getFooter(t,e){const{callbacks:i}=e,s=i.beforeFooter.apply(this,[t]),n=i.footer.apply(this,[t]),o=i.afterFooter.apply(this,[t]);let a=[];return a=No(a,Wo(s)),a=No(a,Wo(n)),a=No(a,Wo(o)),a}_createItems(t){const e=this._active,i=this.chart.data,s=[],n=[],o=[];let a,r,l=[];for(a=0,r=e.length;at.filter(e,s,n,i)))),t.itemSort&&(l=l.sort(((e,s)=>t.itemSort(e,s,i)))),d(l,(e=>{const i=Ko(t.callbacks,e);s.push(i.labelColor.call(this,e)),n.push(i.labelPointStyle.call(this,e)),o.push(i.labelTextColor.call(this,e))})),this.labelColors=s,this.labelPointStyles=n,this.labelTextColors=o,this.dataPoints=l,l}update(t,e){const i=this.options.setContext(this.getContext()),s=this._active;let n,o=[];if(s.length){const t=Bo[i.position].call(this,s,this._eventPosition);o=this._createItems(i),this.title=this.getTitle(o,i),this.beforeBody=this.getBeforeBody(o,i),this.body=this.getBody(o,i),this.afterBody=this.getAfterBody(o,i),this.footer=this.getFooter(o,i);const e=this._size=Ho(this,i),a=Object.assign({},t,e),r=Yo(this.chart,i,a),l=Uo(i,a,r,this.chart);this.xAlign=r.xAlign,this.yAlign=r.yAlign,n={opacity:1,x:l.x,y:l.y,width:e.width,height:e.height,caretX:t.x,caretY:t.y}}else 0!==this.opacity&&(n={opacity:0});this._tooltipItems=o,this.$context=void 0,n&&this._resolveAnimations().update(this,n),t&&i.external&&i.external.call(this,{chart:this.chart,tooltip:this,replay:e})}drawCaret(t,e,i,s){const n=this.getCaretPosition(t,i,s);e.lineTo(n.x1,n.y1),e.lineTo(n.x2,n.y2),e.lineTo(n.x3,n.y3)}getCaretPosition(t,e,i){const{xAlign:s,yAlign:n}=this,{caretSize:o,cornerRadius:a}=i,{topLeft:r,topRight:l,bottomLeft:h,bottomRight:c}=gi(a),{x:d,y:u}=t,{width:f,height:g}=e;let p,m,b,x,_,y;return"center"===n?(_=u+g/2,"left"===s?(p=d,m=p-o,x=_+o,y=_-o):(p=d+f,m=p+o,x=_-o,y=_+o),b=p):(m="left"===s?d+Math.max(r,h)+o:"right"===s?d+f-Math.max(l,c)-o:this.caretX,"top"===n?(x=u,_=x-o,p=m-o,b=m+o):(x=u+g,_=x+o,p=m+o,b=m-o),y=x),{x1:p,x2:m,x3:b,y1:x,y2:_,y3:y}}drawTitle(t,e,i){const s=this.title,n=s.length;let o,a,r;if(n){const l=yi(i.rtl,this.x,this.width);for(t.x=Xo(this,i.titleAlign,i),e.textAlign=l.textAlign(i.titleAlign),e.textBaseline="middle",o=mi(i.titleFont),a=i.titleSpacing,e.fillStyle=i.titleColor,e.font=o.string,r=0;r0!==t))?(t.beginPath(),t.fillStyle=o.multiKeyBackground,Le(t,{x:e,y:p,w:h,h:l,radius:r}),t.fill(),t.stroke(),t.fillStyle=a.backgroundColor,t.beginPath(),Le(t,{x:i,y:p+1,w:h-2,h:l-2,radius:r}),t.fill()):(t.fillStyle=o.multiKeyBackground,t.fillRect(e,p,h,l),t.strokeRect(e,p,h,l),t.fillStyle=a.backgroundColor,t.fillRect(i,p+1,h-2,l-2))}t.fillStyle=this.labelTextColors[i]}drawBody(t,e,i){const{body:s}=this,{bodySpacing:n,bodyAlign:o,displayColors:a,boxHeight:r,boxWidth:l,boxPadding:h}=i,c=mi(i.bodyFont);let u=c.lineHeight,f=0;const g=yi(i.rtl,this.x,this.width),p=function(i){e.fillText(i,g.x(t.x+f),t.y+u/2),t.y+=u+n},m=g.textAlign(o);let b,x,_,y,v,w,M;for(e.textAlign=o,e.textBaseline="middle",e.font=c.string,t.x=Xo(this,m,i),e.fillStyle=i.bodyColor,d(this.beforeBody,p),f=a&&"right"!==m?"center"===o?l/2+h:l+2+h:0,y=0,w=s.length;y0&&e.stroke()}_updateAnimationTarget(t){const e=this.chart,i=this.$animations,s=i&&i.x,n=i&&i.y;if(s||n){const i=Bo[t.position].call(this,this._active,this._eventPosition);if(!i)return;const o=this._size=Ho(this,t),a=Object.assign({},i,this._size),r=Yo(e,t,a),l=Uo(t,a,r,e);s._to===l.x&&n._to===l.y||(this.xAlign=r.xAlign,this.yAlign=r.yAlign,this.width=o.width,this.height=o.height,this.caretX=i.x,this.caretY=i.y,this._resolveAnimations().update(this,l))}}_willRender(){return!!this.opacity}draw(t){const e=this.options.setContext(this.getContext());let i=this.opacity;if(!i)return;this._updateAnimationTarget(e);const s={width:this.width,height:this.height},n={x:this.x,y:this.y};i=Math.abs(i)<.001?0:i;const o=pi(e.padding),a=this.title.length||this.beforeBody.length||this.body.length||this.afterBody.length||this.footer.length;e.enabled&&a&&(t.save(),t.globalAlpha=i,this.drawBackground(n,t,s,e),vi(t,e.textDirection),n.y+=o.top,this.drawTitle(n,t,e),this.drawBody(n,t,e),this.drawFooter(n,t,e),wi(t,e.textDirection),t.restore())}getActiveElements(){return this._active||[]}setActiveElements(t,e){const i=this._active,s=t.map((({datasetIndex:t,index:e})=>{const i=this.chart.getDatasetMeta(t);if(!i)throw new Error("Cannot find a dataset at index "+t);return{datasetIndex:t,element:i.data[e],index:e}})),n=!u(i,s),o=this._positionChanged(s,e);(n||o)&&(this._active=s,this._eventPosition=e,this._ignoreReplayEvents=!0,this.update(!0))}handleEvent(t,e,i=!0){if(e&&this._ignoreReplayEvents)return!1;this._ignoreReplayEvents=!1;const s=this.options,n=this._active||[],o=this._getActiveElements(t,n,e,i),a=this._positionChanged(o,t),r=e||!u(o,n)||a;return r&&(this._active=o,(s.enabled||s.external)&&(this._eventPosition={x:t.x,y:t.y},this.update(!0,e))),r}_getActiveElements(t,e,i,s){const n=this.options;if("mouseout"===t.type)return[];if(!s)return e;const o=this.chart.getElementsAtEventForMode(t,n.mode,n,i);return n.reverse&&o.reverse(),o}_positionChanged(t,e){const{caretX:i,caretY:s,options:n}=this,o=Bo[n.position].call(this,t,e);return!1!==o&&(i!==o.x||s!==o.y)}}Go.positioners=Bo;var Zo={id:"tooltip",_element:Go,positioners:Bo,afterInit(t,e,i){i&&(t.tooltip=new Go({chart:t,options:i}))},beforeUpdate(t,e,i){t.tooltip&&t.tooltip.initialize(i)},reset(t,e,i){t.tooltip&&t.tooltip.initialize(i)},afterDraw(t){const e=t.tooltip;if(e&&e._willRender()){const i={tooltip:e};if(!1===t.notifyPlugins("beforeTooltipDraw",i))return;e.draw(t.ctx),t.notifyPlugins("afterTooltipDraw",i)}},afterEvent(t,e){if(t.tooltip){const i=e.replay;t.tooltip.handleEvent(e.event,i,e.inChartArea)&&(e.changed=!0)}},defaults:{enabled:!0,external:null,position:"average",backgroundColor:"rgba(0,0,0,0.8)",titleColor:"#fff",titleFont:{weight:"bold"},titleSpacing:2,titleMarginBottom:6,titleAlign:"left",bodyColor:"#fff",bodySpacing:2,bodyFont:{},bodyAlign:"left",footerColor:"#fff",footerSpacing:2,footerMarginTop:6,footerFont:{weight:"bold"},footerAlign:"left",padding:6,caretPadding:2,caretSize:5,cornerRadius:6,boxHeight:(t,e)=>e.bodyFont.size,boxWidth:(t,e)=>e.bodyFont.size,multiKeyBackground:"#fff",displayColors:!0,boxPadding:0,borderColor:"rgba(0,0,0,0)",borderWidth:0,animation:{duration:400,easing:"easeOutQuart"},animations:{numbers:{type:"number",properties:["x","y","width","height","caretX","caretY"]},opacity:{easing:"linear",duration:200}},callbacks:{beforeTitle:t,title(t){if(t.length>0){const e=t[0],i=e.chart.data.labels,s=i?i.length:0;if(this&&this.options&&"dataset"===this.options.mode)return e.dataset.label||"";if(e.label)return e.label;if(s>0&&e.dataIndex"filter"!==t&&"itemSort"!==t&&"external"!==t,_indexable:!1,callbacks:{_scriptable:!1,_indexable:!1},animation:{_fallback:!1},animations:{_fallback:"animation"}},additionalOptionScopes:["interaction"]},Jo=Object.freeze({__proto__:null,Decimation:go,Filler:To,Legend:Ro,SubTitle:Vo,Title:zo,Tooltip:Zo});function Qo(t,e,i,s){const n=t.indexOf(e);if(-1===n)return((t,e,i,s)=>("string"==typeof e?(i=t.push(e)-1,s.unshift({index:i,label:e})):isNaN(e)&&(i=null),i))(t,e,i,s);return n!==t.lastIndexOf(e)?i:n}class ta extends $s{constructor(t){super(t),this._startValue=void 0,this._valueRange=0,this._addedLabels=[]}init(t){const e=this._addedLabels;if(e.length){const t=this.getLabels();for(const{index:i,label:s}of e)t[i]===s&&t.splice(i,1);this._addedLabels=[]}super.init(t)}parse(t,e){if(i(t))return null;const s=this.getLabels();return((t,e)=>null===t?null:Z(Math.round(t),0,e))(e=isFinite(e)&&s[e]===t?e:Qo(s,t,r(e,t),this._addedLabels),s.length-1)}determineDataLimits(){const{minDefined:t,maxDefined:e}=this.getUserBounds();let{min:i,max:s}=this.getMinMax(!0);"ticks"===this.options.bounds&&(t||(i=0),e||(s=this.getLabels().length-1)),this.min=i,this.max=s}buildTicks(){const t=this.min,e=this.max,i=this.options.offset,s=[];let n=this.getLabels();n=0===t&&e===n.length-1?n:n.slice(t,e+1),this._valueRange=Math.max(n.length-(i?0:1),1),this._startValue=this.min-(i?.5:0);for(let i=t;i<=e;i++)s.push({value:i});return s}getLabelForValue(t){const e=this.getLabels();return t>=0&&te.length-1?null:this.getPixelForValue(e[t].value)}getValueForPixel(t){return Math.round(this._startValue+this.getDecimalForPixel(t)*this._valueRange)}getBasePixel(){return this.bottom}}function ea(t,e,{horizontal:i,minRotation:s}){const n=H(s),o=(i?Math.sin(n):Math.cos(n))||.001,a=.75*e*(""+t).length;return Math.min(e/o,a)}ta.id="category",ta.defaults={ticks:{callback:ta.prototype.getLabelForValue}};class ia extends $s{constructor(t){super(t),this.start=void 0,this.end=void 0,this._startValue=void 0,this._endValue=void 0,this._valueRange=0}parse(t,e){return i(t)||("number"==typeof t||t instanceof Number)&&!isFinite(+t)?null:+t}handleTickRangeOptions(){const{beginAtZero:t}=this.options,{minDefined:e,maxDefined:i}=this.getUserBounds();let{min:s,max:n}=this;const o=t=>s=e?s:t,a=t=>n=i?n:t;if(t){const t=z(s),e=z(n);t<0&&e<0?a(0):t>0&&e>0&&o(0)}if(s===n){let e=1;(n>=Number.MAX_SAFE_INTEGER||s<=Number.MIN_SAFE_INTEGER)&&(e=Math.abs(.05*n)),a(n+e),t||o(s-e)}this.min=s,this.max=n}getTickLimit(){const t=this.options.ticks;let e,{maxTicksLimit:i,stepSize:s}=t;return s?(e=Math.ceil(this.max/s)-Math.floor(this.min/s)+1,e>1e3&&(console.warn(`scales.${this.id}.ticks.stepSize: ${s} would result generating up to ${e} ticks. Limiting to 1000.`),e=1e3)):(e=this.computeTickLimit(),i=i||11),i&&(e=Math.min(i,e)),e}computeTickLimit(){return Number.POSITIVE_INFINITY}buildTicks(){const t=this.options,e=t.ticks;let s=this.getTickLimit();s=Math.max(2,s);const n=function(t,e){const s=[],{bounds:n,step:o,min:a,max:r,precision:l,count:h,maxTicks:c,maxDigits:d,includeBounds:u}=t,f=o||1,g=c-1,{min:p,max:m}=e,b=!i(a),x=!i(r),_=!i(h),y=(m-p)/(d+1);let v,w,M,k,S=F((m-p)/g/f)*f;if(S<1e-14&&!b&&!x)return[{value:p},{value:m}];k=Math.ceil(m/S)-Math.floor(p/S),k>g&&(S=F(k*S/g/f)*f),i(l)||(v=Math.pow(10,l),S=Math.ceil(S*v)/v),"ticks"===n?(w=Math.floor(p/S)*S,M=Math.ceil(m/S)*S):(w=p,M=m),b&&x&&o&&W((r-a)/o,S/1e3)?(k=Math.round(Math.min((r-a)/S,c)),S=(r-a)/k,w=a,M=r):_?(w=b?a:w,M=x?r:M,k=h-1,S=(M-w)/k):(k=(M-w)/S,k=N(k,Math.round(k),S/1e3)?Math.round(k):Math.ceil(k));const P=Math.max(Y(S),Y(w));v=Math.pow(10,i(l)?P:l),w=Math.round(w*v)/v,M=Math.round(M*v)/v;let D=0;for(b&&(u&&w!==a?(s.push({value:a}),w0?i:null;this._zero=!0}determineDataLimits(){const{min:t,max:e}=this.getMinMax(!0);this.min=o(t)?Math.max(0,t):null,this.max=o(e)?Math.max(0,e):null,this.options.beginAtZero&&(this._zero=!0),this.handleTickRangeOptions()}handleTickRangeOptions(){const{minDefined:t,maxDefined:e}=this.getUserBounds();let i=this.min,s=this.max;const n=e=>i=t?i:e,o=t=>s=e?s:t,a=(t,e)=>Math.pow(10,Math.floor(I(t))+e);i===s&&(i<=0?(n(1),o(10)):(n(a(i,-1)),o(a(s,1)))),i<=0&&n(a(s,-1)),s<=0&&o(a(i,1)),this._zero&&this.min!==this._suggestedMin&&i===a(this.min,0)&&n(a(i,-1)),this.min=i,this.max=s}buildTicks(){const t=this.options,e=function(t,e){const i=Math.floor(I(e.max)),s=Math.ceil(e.max/Math.pow(10,i)),n=[];let o=a(t.min,Math.pow(10,Math.floor(I(e.min)))),r=Math.floor(I(o)),l=Math.floor(o/Math.pow(10,r)),h=r<0?Math.pow(10,Math.abs(r)):1;do{n.push({value:o,major:na(o)}),++l,10===l&&(l=1,++r,h=r>=0?1:h),o=Math.round(l*Math.pow(10,r)*h)/h}while(rn?{start:e-i,end:e}:{start:e,end:e+i}}function la(t){const e={l:t.left+t._padding.left,r:t.right-t._padding.right,t:t.top+t._padding.top,b:t.bottom-t._padding.bottom},i=Object.assign({},e),n=[],o=[],a=t._pointLabels.length,r=t.options.pointLabels,l=r.centerPointLabels?D/a:0;for(let u=0;ue.r&&(r=(s.end-e.r)/o,t.r=Math.max(t.r,e.r+r)),n.starte.b&&(l=(n.end-e.b)/a,t.b=Math.max(t.b,e.b+l))}function ca(t){return 0===t||180===t?"center":t<180?"left":"right"}function da(t,e,i){return"right"===i?t-=e:"center"===i&&(t-=e/2),t}function ua(t,e,i){return 90===i||270===i?t-=e/2:(i>270||i<90)&&(t-=e),t}function fa(t,e,i,s){const{ctx:n}=t;if(i)n.arc(t.xCenter,t.yCenter,e,0,O);else{let i=t.getPointPosition(0,e);n.moveTo(i.x,i.y);for(let o=1;o{const i=c(this.options.pointLabels.callback,[t,e],this);return i||0===i?i:""})).filter(((t,e)=>this.chart.getDataVisibility(e)))}fit(){const t=this.options;t.display&&t.pointLabels.display?la(this):this.setCenterPoint(0,0,0,0)}setCenterPoint(t,e,i,s){this.xCenter+=Math.floor((t-e)/2),this.yCenter+=Math.floor((i-s)/2),this.drawingArea-=Math.min(this.drawingArea/2,Math.max(t,e,i,s))}getIndexAngle(t){return K(t*(O/(this._pointLabels.length||1))+H(this.options.startAngle||0))}getDistanceFromCenterForValue(t){if(i(t))return NaN;const e=this.drawingArea/(this.max-this.min);return this.options.reverse?(this.max-t)*e:(t-this.min)*e}getValueForDistanceFromCenter(t){if(i(t))return NaN;const e=t/(this.drawingArea/(this.max-this.min));return this.options.reverse?this.max-e:this.min+e}getPointLabelContext(t){const e=this._pointLabels||[];if(t>=0&&t=0;o--){const e=n.setContext(t.getPointLabelContext(o)),a=mi(e.font),{x:r,y:l,textAlign:h,left:c,top:d,right:u,bottom:f}=t._pointLabelItems[o],{backdropColor:g}=e;if(!i(g)){const t=gi(e.borderRadius),i=pi(e.backdropPadding);s.fillStyle=g;const n=c-i.left,o=d-i.top,a=u-c+i.width,r=f-d+i.height;Object.values(t).some((t=>0!==t))?(s.beginPath(),Le(s,{x:n,y:o,w:a,h:r,radius:t}),s.fill()):s.fillRect(n,o,a,r)}Ae(s,t._pointLabels[o],r,l+a.lineHeight/2,a,{color:e.color,textAlign:h,textBaseline:"middle"})}}(this,o),n.display&&this.ticks.forEach(((t,e)=>{if(0!==e){r=this.getDistanceFromCenterForValue(t.value);!function(t,e,i,s){const n=t.ctx,o=e.circular,{color:a,lineWidth:r}=e;!o&&!s||!a||!r||i<0||(n.save(),n.strokeStyle=a,n.lineWidth=r,n.setLineDash(e.borderDash),n.lineDashOffset=e.borderDashOffset,n.beginPath(),fa(t,i,o,s),n.closePath(),n.stroke(),n.restore())}(this,n.setContext(this.getContext(e-1)),r,o)}})),s.display){for(t.save(),a=o-1;a>=0;a--){const i=s.setContext(this.getPointLabelContext(a)),{color:n,lineWidth:o}=i;o&&n&&(t.lineWidth=o,t.strokeStyle=n,t.setLineDash(i.borderDash),t.lineDashOffset=i.borderDashOffset,r=this.getDistanceFromCenterForValue(e.ticks.reverse?this.min:this.max),l=this.getPointPosition(a,r),t.beginPath(),t.moveTo(this.xCenter,this.yCenter),t.lineTo(l.x,l.y),t.stroke())}t.restore()}}drawBorder(){}drawLabels(){const t=this.ctx,e=this.options,i=e.ticks;if(!i.display)return;const s=this.getIndexAngle(0);let n,o;t.save(),t.translate(this.xCenter,this.yCenter),t.rotate(s),t.textAlign="center",t.textBaseline="middle",this.ticks.forEach(((s,a)=>{if(0===a&&!e.reverse)return;const r=i.setContext(this.getContext(a)),l=mi(r.font);if(n=this.getDistanceFromCenterForValue(this.ticks[a].value),r.showLabelBackdrop){t.font=l.string,o=t.measureText(s.label).width,t.fillStyle=r.backdropColor;const e=pi(r.backdropPadding);t.fillRect(-o/2-e.left,-n-l.size/2-e.top,o+e.width,l.size+e.height)}Ae(t,s.label,0,-n,l,{color:r.color})})),t.restore()}drawTitle(){}}ga.id="radialLinear",ga.defaults={display:!0,animate:!0,position:"chartArea",angleLines:{display:!0,lineWidth:1,borderDash:[],borderDashOffset:0},grid:{circular:!1},startAngle:0,ticks:{showLabelBackdrop:!0,callback:Is.formatters.numeric},pointLabels:{backdropColor:void 0,backdropPadding:2,display:!0,font:{size:10},callback:t=>t,padding:5,centerPointLabels:!1}},ga.defaultRoutes={"angleLines.color":"borderColor","pointLabels.color":"color","ticks.color":"color"},ga.descriptors={angleLines:{_fallback:"grid"}};const pa={millisecond:{common:!0,size:1,steps:1e3},second:{common:!0,size:1e3,steps:60},minute:{common:!0,size:6e4,steps:60},hour:{common:!0,size:36e5,steps:24},day:{common:!0,size:864e5,steps:30},week:{common:!1,size:6048e5,steps:4},month:{common:!0,size:2628e6,steps:12},quarter:{common:!1,size:7884e6,steps:4},year:{common:!0,size:3154e7}},ma=Object.keys(pa);function ba(t,e){return t-e}function xa(t,e){if(i(e))return null;const s=t._adapter,{parser:n,round:a,isoWeekday:r}=t._parseOpts;let l=e;return"function"==typeof n&&(l=n(l)),o(l)||(l="string"==typeof n?s.parse(l,n):s.parse(l)),null===l?null:(a&&(l="week"!==a||!B(r)&&!0!==r?s.startOf(l,a):s.startOf(l,"isoWeek",r)),+l)}function _a(t,e,i,s){const n=ma.length;for(let o=ma.indexOf(t);o=e?i[s]:i[n]]=!0}}else t[e]=!0}function va(t,e,i){const s=[],n={},o=e.length;let a,r;for(a=0;a=0&&(e[l].major=!0);return e}(t,s,n,i):s}class wa extends $s{constructor(t){super(t),this._cache={data:[],labels:[],all:[]},this._unit="day",this._majorUnit=void 0,this._offsets={},this._normalized=!1,this._parseOpts=void 0}init(t,e){const i=t.time||(t.time={}),s=this._adapter=new wn._date(t.adapters.date);s.init(e),b(i.displayFormats,s.formats()),this._parseOpts={parser:i.parser,round:i.round,isoWeekday:i.isoWeekday},super.init(t),this._normalized=e.normalized}parse(t,e){return void 0===t?null:xa(this,t)}beforeLayout(){super.beforeLayout(),this._cache={data:[],labels:[],all:[]}}determineDataLimits(){const t=this.options,e=this._adapter,i=t.time.unit||"day";let{min:s,max:n,minDefined:a,maxDefined:r}=this.getUserBounds();function l(t){a||isNaN(t.min)||(s=Math.min(s,t.min)),r||isNaN(t.max)||(n=Math.max(n,t.max))}a&&r||(l(this._getLabelBounds()),"ticks"===t.bounds&&"labels"===t.ticks.source||l(this.getMinMax(!1))),s=o(s)&&!isNaN(s)?s:+e.startOf(Date.now(),i),n=o(n)&&!isNaN(n)?n:+e.endOf(Date.now(),i)+1,this.min=Math.min(s,n-1),this.max=Math.max(s+1,n)}_getLabelBounds(){const t=this.getLabelTimestamps();let e=Number.POSITIVE_INFINITY,i=Number.NEGATIVE_INFINITY;return t.length&&(e=t[0],i=t[t.length-1]),{min:e,max:i}}buildTicks(){const t=this.options,e=t.time,i=t.ticks,s="labels"===i.source?this.getLabelTimestamps():this._generate();"ticks"===t.bounds&&s.length&&(this.min=this._userMin||s[0],this.max=this._userMax||s[s.length-1]);const n=this.min,o=st(s,n,this.max);return this._unit=e.unit||(i.autoSkip?_a(e.minUnit,this.min,this.max,this._getLabelCapacity(n)):function(t,e,i,s,n){for(let o=ma.length-1;o>=ma.indexOf(i);o--){const i=ma[o];if(pa[i].common&&t._adapter.diff(n,s,i)>=e-1)return i}return ma[i?ma.indexOf(i):0]}(this,o.length,e.minUnit,this.min,this.max)),this._majorUnit=i.major.enabled&&"year"!==this._unit?function(t){for(let e=ma.indexOf(t)+1,i=ma.length;e+t.value)))}initOffsets(t){let e,i,s=0,n=0;this.options.offset&&t.length&&(e=this.getDecimalForValue(t[0]),s=1===t.length?1-e:(this.getDecimalForValue(t[1])-e)/2,i=this.getDecimalForValue(t[t.length-1]),n=1===t.length?i:(i-this.getDecimalForValue(t[t.length-2]))/2);const o=t.length<3?.5:.25;s=Z(s,0,o),n=Z(n,0,o),this._offsets={start:s,end:n,factor:1/(s+1+n)}}_generate(){const t=this._adapter,e=this.min,i=this.max,s=this.options,n=s.time,o=n.unit||_a(n.minUnit,e,i,this._getLabelCapacity(e)),a=r(n.stepSize,1),l="week"===o&&n.isoWeekday,h=B(l)||!0===l,c={};let d,u,f=e;if(h&&(f=+t.startOf(f,"isoWeek",l)),f=+t.startOf(f,h?"day":o),t.diff(i,e,o)>1e5*a)throw new Error(e+" and "+i+" are too far apart with stepSize of "+a+" "+o);const g="data"===s.ticks.source&&this.getDataTimestamps();for(d=f,u=0;dt-e)).map((t=>+t))}getLabelForValue(t){const e=this._adapter,i=this.options.time;return i.tooltipFormat?e.format(t,i.tooltipFormat):e.format(t,i.displayFormats.datetime)}_tickFormatFunction(t,e,i,s){const n=this.options,o=n.time.displayFormats,a=this._unit,r=this._majorUnit,l=a&&o[a],h=r&&o[r],d=i[e],u=r&&h&&d&&d.major,f=this._adapter.format(t,s||(u?h:l)),g=n.ticks.callback;return g?c(g,[f,e,i],this):f}generateTickLabels(t){let e,i,s;for(e=0,i=t.length;e0?a:1}getDataTimestamps(){let t,e,i=this._cache.data||[];if(i.length)return i;const s=this.getMatchingVisibleMetas();if(this._normalized&&s.length)return this._cache.data=s[0].controller.getAllParsedValues(this);for(t=0,e=s.length;t=t[r].pos&&e<=t[l].pos&&({lo:r,hi:l}=et(t,"pos",e)),({pos:s,time:o}=t[r]),({pos:n,time:a}=t[l])):(e>=t[r].time&&e<=t[l].time&&({lo:r,hi:l}=et(t,"time",e)),({time:s,pos:o}=t[r]),({time:n,pos:a}=t[l]));const h=n-s;return h?o+(a-o)*(e-s)/h:o}wa.id="time",wa.defaults={bounds:"data",adapters:{},time:{parser:!1,unit:!1,round:!1,isoWeekday:!1,minUnit:"millisecond",displayFormats:{}},ticks:{source:"auto",major:{enabled:!1}}};class ka extends wa{constructor(t){super(t),this._table=[],this._minPos=void 0,this._tableRange=void 0}initOffsets(){const t=this._getTimestampsForTable(),e=this._table=this.buildLookupTable(t);this._minPos=Ma(e,this.min),this._tableRange=Ma(e,this.max)-this._minPos,super.initOffsets(t)}buildLookupTable(t){const{min:e,max:i}=this,s=[],n=[];let o,a,r,l,h;for(o=0,a=t.length;o=e&&l<=i&&s.push(l);if(s.length<2)return[{time:e,pos:0},{time:i,pos:1}];for(o=0,a=s.length;o fn({ + chart, + initial: anims.initial, + numSteps, + currentStep: Math.min(date - anims.start, numSteps) + })); + } + _refresh() { + if (this._request) { + return; + } + this._running = true; + this._request = requestAnimFrame.call(window, () => { + this._update(); + this._request = null; + if (this._running) { + this._refresh(); + } + }); + } + _update(date = Date.now()) { + let remaining = 0; + this._charts.forEach((anims, chart) => { + if (!anims.running || !anims.items.length) { + return; + } + const items = anims.items; + let i = items.length - 1; + let draw = false; + let item; + for (; i >= 0; --i) { + item = items[i]; + if (item._active) { + if (item._total > anims.duration) { + anims.duration = item._total; + } + item.tick(date); + draw = true; + } else { + items[i] = items[items.length - 1]; + items.pop(); + } + } + if (draw) { + chart.draw(); + this._notify(chart, anims, date, 'progress'); + } + if (!items.length) { + anims.running = false; + this._notify(chart, anims, date, 'complete'); + anims.initial = false; + } + remaining += items.length; + }); + this._lastDate = date; + if (remaining === 0) { + this._running = false; + } + } + _getAnims(chart) { + const charts = this._charts; + let anims = charts.get(chart); + if (!anims) { + anims = { + running: false, + initial: true, + items: [], + listeners: { + complete: [], + progress: [] + } + }; + charts.set(chart, anims); + } + return anims; + } + listen(chart, event, cb) { + this._getAnims(chart).listeners[event].push(cb); + } + add(chart, items) { + if (!items || !items.length) { + return; + } + this._getAnims(chart).items.push(...items); + } + has(chart) { + return this._getAnims(chart).items.length > 0; + } + start(chart) { + const anims = this._charts.get(chart); + if (!anims) { + return; + } + anims.running = true; + anims.start = Date.now(); + anims.duration = anims.items.reduce((acc, cur) => Math.max(acc, cur._duration), 0); + this._refresh(); + } + running(chart) { + if (!this._running) { + return false; + } + const anims = this._charts.get(chart); + if (!anims || !anims.running || !anims.items.length) { + return false; + } + return true; + } + stop(chart) { + const anims = this._charts.get(chart); + if (!anims || !anims.items.length) { + return; + } + const items = anims.items; + let i = items.length - 1; + for (; i >= 0; --i) { + items[i].cancel(); + } + anims.items = []; + this._notify(chart, anims, Date.now(), 'complete'); + } + remove(chart) { + return this._charts.delete(chart); + } +} +var animator = new Animator(); + +const transparent = 'transparent'; +const interpolators = { + boolean(from, to, factor) { + return factor > 0.5 ? to : from; + }, + color(from, to, factor) { + const c0 = color(from || transparent); + const c1 = c0.valid && color(to || transparent); + return c1 && c1.valid + ? c1.mix(c0, factor).hexString() + : to; + }, + number(from, to, factor) { + return from + (to - from) * factor; + } +}; +class Animation { + constructor(cfg, target, prop, to) { + const currentValue = target[prop]; + to = resolve([cfg.to, to, currentValue, cfg.from]); + const from = resolve([cfg.from, currentValue, to]); + this._active = true; + this._fn = cfg.fn || interpolators[cfg.type || typeof from]; + this._easing = effects[cfg.easing] || effects.linear; + this._start = Math.floor(Date.now() + (cfg.delay || 0)); + this._duration = this._total = Math.floor(cfg.duration); + this._loop = !!cfg.loop; + this._target = target; + this._prop = prop; + this._from = from; + this._to = to; + this._promises = undefined; + } + active() { + return this._active; + } + update(cfg, to, date) { + if (this._active) { + this._notify(false); + const currentValue = this._target[this._prop]; + const elapsed = date - this._start; + const remain = this._duration - elapsed; + this._start = date; + this._duration = Math.floor(Math.max(remain, cfg.duration)); + this._total += elapsed; + this._loop = !!cfg.loop; + this._to = resolve([cfg.to, to, currentValue, cfg.from]); + this._from = resolve([cfg.from, currentValue, to]); + } + } + cancel() { + if (this._active) { + this.tick(Date.now()); + this._active = false; + this._notify(false); + } + } + tick(date) { + const elapsed = date - this._start; + const duration = this._duration; + const prop = this._prop; + const from = this._from; + const loop = this._loop; + const to = this._to; + let factor; + this._active = from !== to && (loop || (elapsed < duration)); + if (!this._active) { + this._target[prop] = to; + this._notify(true); + return; + } + if (elapsed < 0) { + this._target[prop] = from; + return; + } + factor = (elapsed / duration) % 2; + factor = loop && factor > 1 ? 2 - factor : factor; + factor = this._easing(Math.min(1, Math.max(0, factor))); + this._target[prop] = this._fn(from, to, factor); + } + wait() { + const promises = this._promises || (this._promises = []); + return new Promise((res, rej) => { + promises.push({res, rej}); + }); + } + _notify(resolved) { + const method = resolved ? 'res' : 'rej'; + const promises = this._promises || []; + for (let i = 0; i < promises.length; i++) { + promises[i][method](); + } + } +} + +const numbers = ['x', 'y', 'borderWidth', 'radius', 'tension']; +const colors = ['color', 'borderColor', 'backgroundColor']; +defaults.set('animation', { + delay: undefined, + duration: 1000, + easing: 'easeOutQuart', + fn: undefined, + from: undefined, + loop: undefined, + to: undefined, + type: undefined, +}); +const animationOptions = Object.keys(defaults.animation); +defaults.describe('animation', { + _fallback: false, + _indexable: false, + _scriptable: (name) => name !== 'onProgress' && name !== 'onComplete' && name !== 'fn', +}); +defaults.set('animations', { + colors: { + type: 'color', + properties: colors + }, + numbers: { + type: 'number', + properties: numbers + }, +}); +defaults.describe('animations', { + _fallback: 'animation', +}); +defaults.set('transitions', { + active: { + animation: { + duration: 400 + } + }, + resize: { + animation: { + duration: 0 + } + }, + show: { + animations: { + colors: { + from: 'transparent' + }, + visible: { + type: 'boolean', + duration: 0 + }, + } + }, + hide: { + animations: { + colors: { + to: 'transparent' + }, + visible: { + type: 'boolean', + easing: 'linear', + fn: v => v | 0 + }, + } + } +}); +class Animations { + constructor(chart, config) { + this._chart = chart; + this._properties = new Map(); + this.configure(config); + } + configure(config) { + if (!isObject(config)) { + return; + } + const animatedProps = this._properties; + Object.getOwnPropertyNames(config).forEach(key => { + const cfg = config[key]; + if (!isObject(cfg)) { + return; + } + const resolved = {}; + for (const option of animationOptions) { + resolved[option] = cfg[option]; + } + (isArray(cfg.properties) && cfg.properties || [key]).forEach((prop) => { + if (prop === key || !animatedProps.has(prop)) { + animatedProps.set(prop, resolved); + } + }); + }); + } + _animateOptions(target, values) { + const newOptions = values.options; + const options = resolveTargetOptions(target, newOptions); + if (!options) { + return []; + } + const animations = this._createAnimations(options, newOptions); + if (newOptions.$shared) { + awaitAll(target.options.$animations, newOptions).then(() => { + target.options = newOptions; + }, () => { + }); + } + return animations; + } + _createAnimations(target, values) { + const animatedProps = this._properties; + const animations = []; + const running = target.$animations || (target.$animations = {}); + const props = Object.keys(values); + const date = Date.now(); + let i; + for (i = props.length - 1; i >= 0; --i) { + const prop = props[i]; + if (prop.charAt(0) === '$') { + continue; + } + if (prop === 'options') { + animations.push(...this._animateOptions(target, values)); + continue; + } + const value = values[prop]; + let animation = running[prop]; + const cfg = animatedProps.get(prop); + if (animation) { + if (cfg && animation.active()) { + animation.update(cfg, value, date); + continue; + } else { + animation.cancel(); + } + } + if (!cfg || !cfg.duration) { + target[prop] = value; + continue; + } + running[prop] = animation = new Animation(cfg, target, prop, value); + animations.push(animation); + } + return animations; + } + update(target, values) { + if (this._properties.size === 0) { + Object.assign(target, values); + return; + } + const animations = this._createAnimations(target, values); + if (animations.length) { + animator.add(this._chart, animations); + return true; + } + } +} +function awaitAll(animations, properties) { + const running = []; + const keys = Object.keys(properties); + for (let i = 0; i < keys.length; i++) { + const anim = animations[keys[i]]; + if (anim && anim.active()) { + running.push(anim.wait()); + } + } + return Promise.all(running); +} +function resolveTargetOptions(target, newOptions) { + if (!newOptions) { + return; + } + let options = target.options; + if (!options) { + target.options = newOptions; + return; + } + if (options.$shared) { + target.options = options = Object.assign({}, options, {$shared: false, $animations: {}}); + } + return options; +} + +function scaleClip(scale, allowedOverflow) { + const opts = scale && scale.options || {}; + const reverse = opts.reverse; + const min = opts.min === undefined ? allowedOverflow : 0; + const max = opts.max === undefined ? allowedOverflow : 0; + return { + start: reverse ? max : min, + end: reverse ? min : max + }; +} +function defaultClip(xScale, yScale, allowedOverflow) { + if (allowedOverflow === false) { + return false; + } + const x = scaleClip(xScale, allowedOverflow); + const y = scaleClip(yScale, allowedOverflow); + return { + top: y.end, + right: x.end, + bottom: y.start, + left: x.start + }; +} +function toClip(value) { + let t, r, b, l; + if (isObject(value)) { + t = value.top; + r = value.right; + b = value.bottom; + l = value.left; + } else { + t = r = b = l = value; + } + return { + top: t, + right: r, + bottom: b, + left: l, + disabled: value === false + }; +} +function getSortedDatasetIndices(chart, filterVisible) { + const keys = []; + const metasets = chart._getSortedDatasetMetas(filterVisible); + let i, ilen; + for (i = 0, ilen = metasets.length; i < ilen; ++i) { + keys.push(metasets[i].index); + } + return keys; +} +function applyStack(stack, value, dsIndex, options = {}) { + const keys = stack.keys; + const singleMode = options.mode === 'single'; + let i, ilen, datasetIndex, otherValue; + if (value === null) { + return; + } + for (i = 0, ilen = keys.length; i < ilen; ++i) { + datasetIndex = +keys[i]; + if (datasetIndex === dsIndex) { + if (options.all) { + continue; + } + break; + } + otherValue = stack.values[datasetIndex]; + if (isNumberFinite(otherValue) && (singleMode || (value === 0 || sign(value) === sign(otherValue)))) { + value += otherValue; + } + } + return value; +} +function convertObjectDataToArray(data) { + const keys = Object.keys(data); + const adata = new Array(keys.length); + let i, ilen, key; + for (i = 0, ilen = keys.length; i < ilen; ++i) { + key = keys[i]; + adata[i] = { + x: key, + y: data[key] + }; + } + return adata; +} +function isStacked(scale, meta) { + const stacked = scale && scale.options.stacked; + return stacked || (stacked === undefined && meta.stack !== undefined); +} +function getStackKey(indexScale, valueScale, meta) { + return `${indexScale.id}.${valueScale.id}.${meta.stack || meta.type}`; +} +function getUserBounds(scale) { + const {min, max, minDefined, maxDefined} = scale.getUserBounds(); + return { + min: minDefined ? min : Number.NEGATIVE_INFINITY, + max: maxDefined ? max : Number.POSITIVE_INFINITY + }; +} +function getOrCreateStack(stacks, stackKey, indexValue) { + const subStack = stacks[stackKey] || (stacks[stackKey] = {}); + return subStack[indexValue] || (subStack[indexValue] = {}); +} +function getLastIndexInStack(stack, vScale, positive, type) { + for (const meta of vScale.getMatchingVisibleMetas(type).reverse()) { + const value = stack[meta.index]; + if ((positive && value > 0) || (!positive && value < 0)) { + return meta.index; + } + } + return null; +} +function updateStacks(controller, parsed) { + const {chart, _cachedMeta: meta} = controller; + const stacks = chart._stacks || (chart._stacks = {}); + const {iScale, vScale, index: datasetIndex} = meta; + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const key = getStackKey(iScale, vScale, meta); + const ilen = parsed.length; + let stack; + for (let i = 0; i < ilen; ++i) { + const item = parsed[i]; + const {[iAxis]: index, [vAxis]: value} = item; + const itemStacks = item._stacks || (item._stacks = {}); + stack = itemStacks[vAxis] = getOrCreateStack(stacks, key, index); + stack[datasetIndex] = value; + stack._top = getLastIndexInStack(stack, vScale, true, meta.type); + stack._bottom = getLastIndexInStack(stack, vScale, false, meta.type); + } +} +function getFirstScaleId(chart, axis) { + const scales = chart.scales; + return Object.keys(scales).filter(key => scales[key].axis === axis).shift(); +} +function createDatasetContext(parent, index) { + return createContext(parent, + { + active: false, + dataset: undefined, + datasetIndex: index, + index, + mode: 'default', + type: 'dataset' + } + ); +} +function createDataContext(parent, index, element) { + return createContext(parent, { + active: false, + dataIndex: index, + parsed: undefined, + raw: undefined, + element, + index, + mode: 'default', + type: 'data' + }); +} +function clearStacks(meta, items) { + const datasetIndex = meta.controller.index; + const axis = meta.vScale && meta.vScale.axis; + if (!axis) { + return; + } + items = items || meta._parsed; + for (const parsed of items) { + const stacks = parsed._stacks; + if (!stacks || stacks[axis] === undefined || stacks[axis][datasetIndex] === undefined) { + return; + } + delete stacks[axis][datasetIndex]; + } +} +const isDirectUpdateMode = (mode) => mode === 'reset' || mode === 'none'; +const cloneIfNotShared = (cached, shared) => shared ? cached : Object.assign({}, cached); +const createStack = (canStack, meta, chart) => canStack && !meta.hidden && meta._stacked + && {keys: getSortedDatasetIndices(chart, true), values: null}; +class DatasetController { + constructor(chart, datasetIndex) { + this.chart = chart; + this._ctx = chart.ctx; + this.index = datasetIndex; + this._cachedDataOpts = {}; + this._cachedMeta = this.getMeta(); + this._type = this._cachedMeta.type; + this.options = undefined; + this._parsing = false; + this._data = undefined; + this._objectData = undefined; + this._sharedOptions = undefined; + this._drawStart = undefined; + this._drawCount = undefined; + this.enableOptionSharing = false; + this.supportsDecimation = false; + this.$context = undefined; + this._syncList = []; + this.initialize(); + } + initialize() { + const meta = this._cachedMeta; + this.configure(); + this.linkScales(); + meta._stacked = isStacked(meta.vScale, meta); + this.addElements(); + } + updateIndex(datasetIndex) { + if (this.index !== datasetIndex) { + clearStacks(this._cachedMeta); + } + this.index = datasetIndex; + } + linkScales() { + const chart = this.chart; + const meta = this._cachedMeta; + const dataset = this.getDataset(); + const chooseId = (axis, x, y, r) => axis === 'x' ? x : axis === 'r' ? r : y; + const xid = meta.xAxisID = valueOrDefault(dataset.xAxisID, getFirstScaleId(chart, 'x')); + const yid = meta.yAxisID = valueOrDefault(dataset.yAxisID, getFirstScaleId(chart, 'y')); + const rid = meta.rAxisID = valueOrDefault(dataset.rAxisID, getFirstScaleId(chart, 'r')); + const indexAxis = meta.indexAxis; + const iid = meta.iAxisID = chooseId(indexAxis, xid, yid, rid); + const vid = meta.vAxisID = chooseId(indexAxis, yid, xid, rid); + meta.xScale = this.getScaleForId(xid); + meta.yScale = this.getScaleForId(yid); + meta.rScale = this.getScaleForId(rid); + meta.iScale = this.getScaleForId(iid); + meta.vScale = this.getScaleForId(vid); + } + getDataset() { + return this.chart.data.datasets[this.index]; + } + getMeta() { + return this.chart.getDatasetMeta(this.index); + } + getScaleForId(scaleID) { + return this.chart.scales[scaleID]; + } + _getOtherScale(scale) { + const meta = this._cachedMeta; + return scale === meta.iScale + ? meta.vScale + : meta.iScale; + } + reset() { + this._update('reset'); + } + _destroy() { + const meta = this._cachedMeta; + if (this._data) { + unlistenArrayEvents(this._data, this); + } + if (meta._stacked) { + clearStacks(meta); + } + } + _dataCheck() { + const dataset = this.getDataset(); + const data = dataset.data || (dataset.data = []); + const _data = this._data; + if (isObject(data)) { + this._data = convertObjectDataToArray(data); + } else if (_data !== data) { + if (_data) { + unlistenArrayEvents(_data, this); + const meta = this._cachedMeta; + clearStacks(meta); + meta._parsed = []; + } + if (data && Object.isExtensible(data)) { + listenArrayEvents(data, this); + } + this._syncList = []; + this._data = data; + } + } + addElements() { + const meta = this._cachedMeta; + this._dataCheck(); + if (this.datasetElementType) { + meta.dataset = new this.datasetElementType(); + } + } + buildOrUpdateElements(resetNewElements) { + const meta = this._cachedMeta; + const dataset = this.getDataset(); + let stackChanged = false; + this._dataCheck(); + const oldStacked = meta._stacked; + meta._stacked = isStacked(meta.vScale, meta); + if (meta.stack !== dataset.stack) { + stackChanged = true; + clearStacks(meta); + meta.stack = dataset.stack; + } + this._resyncElements(resetNewElements); + if (stackChanged || oldStacked !== meta._stacked) { + updateStacks(this, meta._parsed); + } + } + configure() { + const config = this.chart.config; + const scopeKeys = config.datasetScopeKeys(this._type); + const scopes = config.getOptionScopes(this.getDataset(), scopeKeys, true); + this.options = config.createResolver(scopes, this.getContext()); + this._parsing = this.options.parsing; + this._cachedDataOpts = {}; + } + parse(start, count) { + const {_cachedMeta: meta, _data: data} = this; + const {iScale, _stacked} = meta; + const iAxis = iScale.axis; + let sorted = start === 0 && count === data.length ? true : meta._sorted; + let prev = start > 0 && meta._parsed[start - 1]; + let i, cur, parsed; + if (this._parsing === false) { + meta._parsed = data; + meta._sorted = true; + parsed = data; + } else { + if (isArray(data[start])) { + parsed = this.parseArrayData(meta, data, start, count); + } else if (isObject(data[start])) { + parsed = this.parseObjectData(meta, data, start, count); + } else { + parsed = this.parsePrimitiveData(meta, data, start, count); + } + const isNotInOrderComparedToPrev = () => cur[iAxis] === null || (prev && cur[iAxis] < prev[iAxis]); + for (i = 0; i < count; ++i) { + meta._parsed[i + start] = cur = parsed[i]; + if (sorted) { + if (isNotInOrderComparedToPrev()) { + sorted = false; + } + prev = cur; + } + } + meta._sorted = sorted; + } + if (_stacked) { + updateStacks(this, parsed); + } + } + parsePrimitiveData(meta, data, start, count) { + const {iScale, vScale} = meta; + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const labels = iScale.getLabels(); + const singleScale = iScale === vScale; + const parsed = new Array(count); + let i, ilen, index; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + parsed[i] = { + [iAxis]: singleScale || iScale.parse(labels[index], index), + [vAxis]: vScale.parse(data[index], index) + }; + } + return parsed; + } + parseArrayData(meta, data, start, count) { + const {xScale, yScale} = meta; + const parsed = new Array(count); + let i, ilen, index, item; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + item = data[index]; + parsed[i] = { + x: xScale.parse(item[0], index), + y: yScale.parse(item[1], index) + }; + } + return parsed; + } + parseObjectData(meta, data, start, count) { + const {xScale, yScale} = meta; + const {xAxisKey = 'x', yAxisKey = 'y'} = this._parsing; + const parsed = new Array(count); + let i, ilen, index, item; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + item = data[index]; + parsed[i] = { + x: xScale.parse(resolveObjectKey(item, xAxisKey), index), + y: yScale.parse(resolveObjectKey(item, yAxisKey), index) + }; + } + return parsed; + } + getParsed(index) { + return this._cachedMeta._parsed[index]; + } + getDataElement(index) { + return this._cachedMeta.data[index]; + } + applyStack(scale, parsed, mode) { + const chart = this.chart; + const meta = this._cachedMeta; + const value = parsed[scale.axis]; + const stack = { + keys: getSortedDatasetIndices(chart, true), + values: parsed._stacks[scale.axis] + }; + return applyStack(stack, value, meta.index, {mode}); + } + updateRangeFromParsed(range, scale, parsed, stack) { + const parsedValue = parsed[scale.axis]; + let value = parsedValue === null ? NaN : parsedValue; + const values = stack && parsed._stacks[scale.axis]; + if (stack && values) { + stack.values = values; + value = applyStack(stack, parsedValue, this._cachedMeta.index); + } + range.min = Math.min(range.min, value); + range.max = Math.max(range.max, value); + } + getMinMax(scale, canStack) { + const meta = this._cachedMeta; + const _parsed = meta._parsed; + const sorted = meta._sorted && scale === meta.iScale; + const ilen = _parsed.length; + const otherScale = this._getOtherScale(scale); + const stack = createStack(canStack, meta, this.chart); + const range = {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY}; + const {min: otherMin, max: otherMax} = getUserBounds(otherScale); + let i, parsed; + function _skip() { + parsed = _parsed[i]; + const otherValue = parsed[otherScale.axis]; + return !isNumberFinite(parsed[scale.axis]) || otherMin > otherValue || otherMax < otherValue; + } + for (i = 0; i < ilen; ++i) { + if (_skip()) { + continue; + } + this.updateRangeFromParsed(range, scale, parsed, stack); + if (sorted) { + break; + } + } + if (sorted) { + for (i = ilen - 1; i >= 0; --i) { + if (_skip()) { + continue; + } + this.updateRangeFromParsed(range, scale, parsed, stack); + break; + } + } + return range; + } + getAllParsedValues(scale) { + const parsed = this._cachedMeta._parsed; + const values = []; + let i, ilen, value; + for (i = 0, ilen = parsed.length; i < ilen; ++i) { + value = parsed[i][scale.axis]; + if (isNumberFinite(value)) { + values.push(value); + } + } + return values; + } + getMaxOverflow() { + return false; + } + getLabelAndValue(index) { + const meta = this._cachedMeta; + const iScale = meta.iScale; + const vScale = meta.vScale; + const parsed = this.getParsed(index); + return { + label: iScale ? '' + iScale.getLabelForValue(parsed[iScale.axis]) : '', + value: vScale ? '' + vScale.getLabelForValue(parsed[vScale.axis]) : '' + }; + } + _update(mode) { + const meta = this._cachedMeta; + this.update(mode || 'default'); + meta._clip = toClip(valueOrDefault(this.options.clip, defaultClip(meta.xScale, meta.yScale, this.getMaxOverflow()))); + } + update(mode) {} + draw() { + const ctx = this._ctx; + const chart = this.chart; + const meta = this._cachedMeta; + const elements = meta.data || []; + const area = chart.chartArea; + const active = []; + const start = this._drawStart || 0; + const count = this._drawCount || (elements.length - start); + const drawActiveElementsOnTop = this.options.drawActiveElementsOnTop; + let i; + if (meta.dataset) { + meta.dataset.draw(ctx, area, start, count); + } + for (i = start; i < start + count; ++i) { + const element = elements[i]; + if (element.hidden) { + continue; + } + if (element.active && drawActiveElementsOnTop) { + active.push(element); + } else { + element.draw(ctx, area); + } + } + for (i = 0; i < active.length; ++i) { + active[i].draw(ctx, area); + } + } + getStyle(index, active) { + const mode = active ? 'active' : 'default'; + return index === undefined && this._cachedMeta.dataset + ? this.resolveDatasetElementOptions(mode) + : this.resolveDataElementOptions(index || 0, mode); + } + getContext(index, active, mode) { + const dataset = this.getDataset(); + let context; + if (index >= 0 && index < this._cachedMeta.data.length) { + const element = this._cachedMeta.data[index]; + context = element.$context || + (element.$context = createDataContext(this.getContext(), index, element)); + context.parsed = this.getParsed(index); + context.raw = dataset.data[index]; + context.index = context.dataIndex = index; + } else { + context = this.$context || + (this.$context = createDatasetContext(this.chart.getContext(), this.index)); + context.dataset = dataset; + context.index = context.datasetIndex = this.index; + } + context.active = !!active; + context.mode = mode; + return context; + } + resolveDatasetElementOptions(mode) { + return this._resolveElementOptions(this.datasetElementType.id, mode); + } + resolveDataElementOptions(index, mode) { + return this._resolveElementOptions(this.dataElementType.id, mode, index); + } + _resolveElementOptions(elementType, mode = 'default', index) { + const active = mode === 'active'; + const cache = this._cachedDataOpts; + const cacheKey = elementType + '-' + mode; + const cached = cache[cacheKey]; + const sharing = this.enableOptionSharing && defined(index); + if (cached) { + return cloneIfNotShared(cached, sharing); + } + const config = this.chart.config; + const scopeKeys = config.datasetElementScopeKeys(this._type, elementType); + const prefixes = active ? [`${elementType}Hover`, 'hover', elementType, ''] : [elementType, '']; + const scopes = config.getOptionScopes(this.getDataset(), scopeKeys); + const names = Object.keys(defaults.elements[elementType]); + const context = () => this.getContext(index, active); + const values = config.resolveNamedOptions(scopes, names, context, prefixes); + if (values.$shared) { + values.$shared = sharing; + cache[cacheKey] = Object.freeze(cloneIfNotShared(values, sharing)); + } + return values; + } + _resolveAnimations(index, transition, active) { + const chart = this.chart; + const cache = this._cachedDataOpts; + const cacheKey = `animation-${transition}`; + const cached = cache[cacheKey]; + if (cached) { + return cached; + } + let options; + if (chart.options.animation !== false) { + const config = this.chart.config; + const scopeKeys = config.datasetAnimationScopeKeys(this._type, transition); + const scopes = config.getOptionScopes(this.getDataset(), scopeKeys); + options = config.createResolver(scopes, this.getContext(index, active, transition)); + } + const animations = new Animations(chart, options && options.animations); + if (options && options._cacheable) { + cache[cacheKey] = Object.freeze(animations); + } + return animations; + } + getSharedOptions(options) { + if (!options.$shared) { + return; + } + return this._sharedOptions || (this._sharedOptions = Object.assign({}, options)); + } + includeOptions(mode, sharedOptions) { + return !sharedOptions || isDirectUpdateMode(mode) || this.chart._animationsDisabled; + } + _getSharedOptions(start, mode) { + const firstOpts = this.resolveDataElementOptions(start, mode); + const previouslySharedOptions = this._sharedOptions; + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions) || (sharedOptions !== previouslySharedOptions); + this.updateSharedOptions(sharedOptions, mode, firstOpts); + return {sharedOptions, includeOptions}; + } + updateElement(element, index, properties, mode) { + if (isDirectUpdateMode(mode)) { + Object.assign(element, properties); + } else { + this._resolveAnimations(index, mode).update(element, properties); + } + } + updateSharedOptions(sharedOptions, mode, newOptions) { + if (sharedOptions && !isDirectUpdateMode(mode)) { + this._resolveAnimations(undefined, mode).update(sharedOptions, newOptions); + } + } + _setStyle(element, index, mode, active) { + element.active = active; + const options = this.getStyle(index, active); + this._resolveAnimations(index, mode, active).update(element, { + options: (!active && this.getSharedOptions(options)) || options + }); + } + removeHoverStyle(element, datasetIndex, index) { + this._setStyle(element, index, 'active', false); + } + setHoverStyle(element, datasetIndex, index) { + this._setStyle(element, index, 'active', true); + } + _removeDatasetHoverStyle() { + const element = this._cachedMeta.dataset; + if (element) { + this._setStyle(element, undefined, 'active', false); + } + } + _setDatasetHoverStyle() { + const element = this._cachedMeta.dataset; + if (element) { + this._setStyle(element, undefined, 'active', true); + } + } + _resyncElements(resetNewElements) { + const data = this._data; + const elements = this._cachedMeta.data; + for (const [method, arg1, arg2] of this._syncList) { + this[method](arg1, arg2); + } + this._syncList = []; + const numMeta = elements.length; + const numData = data.length; + const count = Math.min(numData, numMeta); + if (count) { + this.parse(0, count); + } + if (numData > numMeta) { + this._insertElements(numMeta, numData - numMeta, resetNewElements); + } else if (numData < numMeta) { + this._removeElements(numData, numMeta - numData); + } + } + _insertElements(start, count, resetNewElements = true) { + const meta = this._cachedMeta; + const data = meta.data; + const end = start + count; + let i; + const move = (arr) => { + arr.length += count; + for (i = arr.length - 1; i >= end; i--) { + arr[i] = arr[i - count]; + } + }; + move(data); + for (i = start; i < end; ++i) { + data[i] = new this.dataElementType(); + } + if (this._parsing) { + move(meta._parsed); + } + this.parse(start, count); + if (resetNewElements) { + this.updateElements(data, start, count, 'reset'); + } + } + updateElements(element, start, count, mode) {} + _removeElements(start, count) { + const meta = this._cachedMeta; + if (this._parsing) { + const removed = meta._parsed.splice(start, count); + if (meta._stacked) { + clearStacks(meta, removed); + } + } + meta.data.splice(start, count); + } + _sync(args) { + if (this._parsing) { + this._syncList.push(args); + } else { + const [method, arg1, arg2] = args; + this[method](arg1, arg2); + } + this.chart._dataChanges.push([this.index, ...args]); + } + _onDataPush() { + const count = arguments.length; + this._sync(['_insertElements', this.getDataset().data.length - count, count]); + } + _onDataPop() { + this._sync(['_removeElements', this._cachedMeta.data.length - 1, 1]); + } + _onDataShift() { + this._sync(['_removeElements', 0, 1]); + } + _onDataSplice(start, count) { + if (count) { + this._sync(['_removeElements', start, count]); + } + const newCount = arguments.length - 2; + if (newCount) { + this._sync(['_insertElements', start, newCount]); + } + } + _onDataUnshift() { + this._sync(['_insertElements', 0, arguments.length]); + } +} +DatasetController.defaults = {}; +DatasetController.prototype.datasetElementType = null; +DatasetController.prototype.dataElementType = null; + +function getAllScaleValues(scale, type) { + if (!scale._cache.$bar) { + const visibleMetas = scale.getMatchingVisibleMetas(type); + let values = []; + for (let i = 0, ilen = visibleMetas.length; i < ilen; i++) { + values = values.concat(visibleMetas[i].controller.getAllParsedValues(scale)); + } + scale._cache.$bar = _arrayUnique(values.sort((a, b) => a - b)); + } + return scale._cache.$bar; +} +function computeMinSampleSize(meta) { + const scale = meta.iScale; + const values = getAllScaleValues(scale, meta.type); + let min = scale._length; + let i, ilen, curr, prev; + const updateMinAndPrev = () => { + if (curr === 32767 || curr === -32768) { + return; + } + if (defined(prev)) { + min = Math.min(min, Math.abs(curr - prev) || min); + } + prev = curr; + }; + for (i = 0, ilen = values.length; i < ilen; ++i) { + curr = scale.getPixelForValue(values[i]); + updateMinAndPrev(); + } + prev = undefined; + for (i = 0, ilen = scale.ticks.length; i < ilen; ++i) { + curr = scale.getPixelForTick(i); + updateMinAndPrev(); + } + return min; +} +function computeFitCategoryTraits(index, ruler, options, stackCount) { + const thickness = options.barThickness; + let size, ratio; + if (isNullOrUndef(thickness)) { + size = ruler.min * options.categoryPercentage; + ratio = options.barPercentage; + } else { + size = thickness * stackCount; + ratio = 1; + } + return { + chunk: size / stackCount, + ratio, + start: ruler.pixels[index] - (size / 2) + }; +} +function computeFlexCategoryTraits(index, ruler, options, stackCount) { + const pixels = ruler.pixels; + const curr = pixels[index]; + let prev = index > 0 ? pixels[index - 1] : null; + let next = index < pixels.length - 1 ? pixels[index + 1] : null; + const percent = options.categoryPercentage; + if (prev === null) { + prev = curr - (next === null ? ruler.end - ruler.start : next - curr); + } + if (next === null) { + next = curr + curr - prev; + } + const start = curr - (curr - Math.min(prev, next)) / 2 * percent; + const size = Math.abs(next - prev) / 2 * percent; + return { + chunk: size / stackCount, + ratio: options.barPercentage, + start + }; +} +function parseFloatBar(entry, item, vScale, i) { + const startValue = vScale.parse(entry[0], i); + const endValue = vScale.parse(entry[1], i); + const min = Math.min(startValue, endValue); + const max = Math.max(startValue, endValue); + let barStart = min; + let barEnd = max; + if (Math.abs(min) > Math.abs(max)) { + barStart = max; + barEnd = min; + } + item[vScale.axis] = barEnd; + item._custom = { + barStart, + barEnd, + start: startValue, + end: endValue, + min, + max + }; +} +function parseValue(entry, item, vScale, i) { + if (isArray(entry)) { + parseFloatBar(entry, item, vScale, i); + } else { + item[vScale.axis] = vScale.parse(entry, i); + } + return item; +} +function parseArrayOrPrimitive(meta, data, start, count) { + const iScale = meta.iScale; + const vScale = meta.vScale; + const labels = iScale.getLabels(); + const singleScale = iScale === vScale; + const parsed = []; + let i, ilen, item, entry; + for (i = start, ilen = start + count; i < ilen; ++i) { + entry = data[i]; + item = {}; + item[iScale.axis] = singleScale || iScale.parse(labels[i], i); + parsed.push(parseValue(entry, item, vScale, i)); + } + return parsed; +} +function isFloatBar(custom) { + return custom && custom.barStart !== undefined && custom.barEnd !== undefined; +} +function barSign(size, vScale, actualBase) { + if (size !== 0) { + return sign(size); + } + return (vScale.isHorizontal() ? 1 : -1) * (vScale.min >= actualBase ? 1 : -1); +} +function borderProps(properties) { + let reverse, start, end, top, bottom; + if (properties.horizontal) { + reverse = properties.base > properties.x; + start = 'left'; + end = 'right'; + } else { + reverse = properties.base < properties.y; + start = 'bottom'; + end = 'top'; + } + if (reverse) { + top = 'end'; + bottom = 'start'; + } else { + top = 'start'; + bottom = 'end'; + } + return {start, end, reverse, top, bottom}; +} +function setBorderSkipped(properties, options, stack, index) { + let edge = options.borderSkipped; + const res = {}; + if (!edge) { + properties.borderSkipped = res; + return; + } + if (edge === true) { + properties.borderSkipped = {top: true, right: true, bottom: true, left: true}; + return; + } + const {start, end, reverse, top, bottom} = borderProps(properties); + if (edge === 'middle' && stack) { + properties.enableBorderRadius = true; + if ((stack._top || 0) === index) { + edge = top; + } else if ((stack._bottom || 0) === index) { + edge = bottom; + } else { + res[parseEdge(bottom, start, end, reverse)] = true; + edge = top; + } + } + res[parseEdge(edge, start, end, reverse)] = true; + properties.borderSkipped = res; +} +function parseEdge(edge, a, b, reverse) { + if (reverse) { + edge = swap(edge, a, b); + edge = startEnd(edge, b, a); + } else { + edge = startEnd(edge, a, b); + } + return edge; +} +function swap(orig, v1, v2) { + return orig === v1 ? v2 : orig === v2 ? v1 : orig; +} +function startEnd(v, start, end) { + return v === 'start' ? start : v === 'end' ? end : v; +} +function setInflateAmount(properties, {inflateAmount}, ratio) { + properties.inflateAmount = inflateAmount === 'auto' + ? ratio === 1 ? 0.33 : 0 + : inflateAmount; +} +class BarController extends DatasetController { + parsePrimitiveData(meta, data, start, count) { + return parseArrayOrPrimitive(meta, data, start, count); + } + parseArrayData(meta, data, start, count) { + return parseArrayOrPrimitive(meta, data, start, count); + } + parseObjectData(meta, data, start, count) { + const {iScale, vScale} = meta; + const {xAxisKey = 'x', yAxisKey = 'y'} = this._parsing; + const iAxisKey = iScale.axis === 'x' ? xAxisKey : yAxisKey; + const vAxisKey = vScale.axis === 'x' ? xAxisKey : yAxisKey; + const parsed = []; + let i, ilen, item, obj; + for (i = start, ilen = start + count; i < ilen; ++i) { + obj = data[i]; + item = {}; + item[iScale.axis] = iScale.parse(resolveObjectKey(obj, iAxisKey), i); + parsed.push(parseValue(resolveObjectKey(obj, vAxisKey), item, vScale, i)); + } + return parsed; + } + updateRangeFromParsed(range, scale, parsed, stack) { + super.updateRangeFromParsed(range, scale, parsed, stack); + const custom = parsed._custom; + if (custom && scale === this._cachedMeta.vScale) { + range.min = Math.min(range.min, custom.min); + range.max = Math.max(range.max, custom.max); + } + } + getMaxOverflow() { + return 0; + } + getLabelAndValue(index) { + const meta = this._cachedMeta; + const {iScale, vScale} = meta; + const parsed = this.getParsed(index); + const custom = parsed._custom; + const value = isFloatBar(custom) + ? '[' + custom.start + ', ' + custom.end + ']' + : '' + vScale.getLabelForValue(parsed[vScale.axis]); + return { + label: '' + iScale.getLabelForValue(parsed[iScale.axis]), + value + }; + } + initialize() { + this.enableOptionSharing = true; + super.initialize(); + const meta = this._cachedMeta; + meta.stack = this.getDataset().stack; + } + update(mode) { + const meta = this._cachedMeta; + this.updateElements(meta.data, 0, meta.data.length, mode); + } + updateElements(bars, start, count, mode) { + const reset = mode === 'reset'; + const {index, _cachedMeta: {vScale}} = this; + const base = vScale.getBasePixel(); + const horizontal = vScale.isHorizontal(); + const ruler = this._getRuler(); + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); + for (let i = start; i < start + count; i++) { + const parsed = this.getParsed(i); + const vpixels = reset || isNullOrUndef(parsed[vScale.axis]) ? {base, head: base} : this._calculateBarValuePixels(i); + const ipixels = this._calculateBarIndexPixels(i, ruler); + const stack = (parsed._stacks || {})[vScale.axis]; + const properties = { + horizontal, + base: vpixels.base, + enableBorderRadius: !stack || isFloatBar(parsed._custom) || (index === stack._top || index === stack._bottom), + x: horizontal ? vpixels.head : ipixels.center, + y: horizontal ? ipixels.center : vpixels.head, + height: horizontal ? ipixels.size : Math.abs(vpixels.size), + width: horizontal ? Math.abs(vpixels.size) : ipixels.size + }; + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, bars[i].active ? 'active' : mode); + } + const options = properties.options || bars[i].options; + setBorderSkipped(properties, options, stack, index); + setInflateAmount(properties, options, ruler.ratio); + this.updateElement(bars[i], i, properties, mode); + } + } + _getStacks(last, dataIndex) { + const {iScale} = this._cachedMeta; + const metasets = iScale.getMatchingVisibleMetas(this._type) + .filter(meta => meta.controller.options.grouped); + const stacked = iScale.options.stacked; + const stacks = []; + const skipNull = (meta) => { + const parsed = meta.controller.getParsed(dataIndex); + const val = parsed && parsed[meta.vScale.axis]; + if (isNullOrUndef(val) || isNaN(val)) { + return true; + } + }; + for (const meta of metasets) { + if (dataIndex !== undefined && skipNull(meta)) { + continue; + } + if (stacked === false || stacks.indexOf(meta.stack) === -1 || + (stacked === undefined && meta.stack === undefined)) { + stacks.push(meta.stack); + } + if (meta.index === last) { + break; + } + } + if (!stacks.length) { + stacks.push(undefined); + } + return stacks; + } + _getStackCount(index) { + return this._getStacks(undefined, index).length; + } + _getStackIndex(datasetIndex, name, dataIndex) { + const stacks = this._getStacks(datasetIndex, dataIndex); + const index = (name !== undefined) + ? stacks.indexOf(name) + : -1; + return (index === -1) + ? stacks.length - 1 + : index; + } + _getRuler() { + const opts = this.options; + const meta = this._cachedMeta; + const iScale = meta.iScale; + const pixels = []; + let i, ilen; + for (i = 0, ilen = meta.data.length; i < ilen; ++i) { + pixels.push(iScale.getPixelForValue(this.getParsed(i)[iScale.axis], i)); + } + const barThickness = opts.barThickness; + const min = barThickness || computeMinSampleSize(meta); + return { + min, + pixels, + start: iScale._startPixel, + end: iScale._endPixel, + stackCount: this._getStackCount(), + scale: iScale, + grouped: opts.grouped, + ratio: barThickness ? 1 : opts.categoryPercentage * opts.barPercentage + }; + } + _calculateBarValuePixels(index) { + const {_cachedMeta: {vScale, _stacked}, options: {base: baseValue, minBarLength}} = this; + const actualBase = baseValue || 0; + const parsed = this.getParsed(index); + const custom = parsed._custom; + const floating = isFloatBar(custom); + let value = parsed[vScale.axis]; + let start = 0; + let length = _stacked ? this.applyStack(vScale, parsed, _stacked) : value; + let head, size; + if (length !== value) { + start = length - value; + length = value; + } + if (floating) { + value = custom.barStart; + length = custom.barEnd - custom.barStart; + if (value !== 0 && sign(value) !== sign(custom.barEnd)) { + start = 0; + } + start += value; + } + const startValue = !isNullOrUndef(baseValue) && !floating ? baseValue : start; + let base = vScale.getPixelForValue(startValue); + if (this.chart.getDataVisibility(index)) { + head = vScale.getPixelForValue(start + length); + } else { + head = base; + } + size = head - base; + if (Math.abs(size) < minBarLength) { + size = barSign(size, vScale, actualBase) * minBarLength; + if (value === actualBase) { + base -= size / 2; + } + const startPixel = vScale.getPixelForDecimal(0); + const endPixel = vScale.getPixelForDecimal(1); + const min = Math.min(startPixel, endPixel); + const max = Math.max(startPixel, endPixel); + base = Math.max(Math.min(base, max), min); + head = base + size; + } + if (base === vScale.getPixelForValue(actualBase)) { + const halfGrid = sign(size) * vScale.getLineWidthForValue(actualBase) / 2; + base += halfGrid; + size -= halfGrid; + } + return { + size, + base, + head, + center: head + size / 2 + }; + } + _calculateBarIndexPixels(index, ruler) { + const scale = ruler.scale; + const options = this.options; + const skipNull = options.skipNull; + const maxBarThickness = valueOrDefault(options.maxBarThickness, Infinity); + let center, size; + if (ruler.grouped) { + const stackCount = skipNull ? this._getStackCount(index) : ruler.stackCount; + const range = options.barThickness === 'flex' + ? computeFlexCategoryTraits(index, ruler, options, stackCount) + : computeFitCategoryTraits(index, ruler, options, stackCount); + const stackIndex = this._getStackIndex(this.index, this._cachedMeta.stack, skipNull ? index : undefined); + center = range.start + (range.chunk * stackIndex) + (range.chunk / 2); + size = Math.min(maxBarThickness, range.chunk * range.ratio); + } else { + center = scale.getPixelForValue(this.getParsed(index)[scale.axis], index); + size = Math.min(maxBarThickness, ruler.min * ruler.ratio); + } + return { + base: center - size / 2, + head: center + size / 2, + center, + size + }; + } + draw() { + const meta = this._cachedMeta; + const vScale = meta.vScale; + const rects = meta.data; + const ilen = rects.length; + let i = 0; + for (; i < ilen; ++i) { + if (this.getParsed(i)[vScale.axis] !== null) { + rects[i].draw(this._ctx); + } + } + } +} +BarController.id = 'bar'; +BarController.defaults = { + datasetElementType: false, + dataElementType: 'bar', + categoryPercentage: 0.8, + barPercentage: 0.9, + grouped: true, + animations: { + numbers: { + type: 'number', + properties: ['x', 'y', 'base', 'width', 'height'] + } + } +}; +BarController.overrides = { + scales: { + _index_: { + type: 'category', + offset: true, + grid: { + offset: true + } + }, + _value_: { + type: 'linear', + beginAtZero: true, + } + } +}; + +class BubbleController extends DatasetController { + initialize() { + this.enableOptionSharing = true; + super.initialize(); + } + parsePrimitiveData(meta, data, start, count) { + const parsed = super.parsePrimitiveData(meta, data, start, count); + for (let i = 0; i < parsed.length; i++) { + parsed[i]._custom = this.resolveDataElementOptions(i + start).radius; + } + return parsed; + } + parseArrayData(meta, data, start, count) { + const parsed = super.parseArrayData(meta, data, start, count); + for (let i = 0; i < parsed.length; i++) { + const item = data[start + i]; + parsed[i]._custom = valueOrDefault(item[2], this.resolveDataElementOptions(i + start).radius); + } + return parsed; + } + parseObjectData(meta, data, start, count) { + const parsed = super.parseObjectData(meta, data, start, count); + for (let i = 0; i < parsed.length; i++) { + const item = data[start + i]; + parsed[i]._custom = valueOrDefault(item && item.r && +item.r, this.resolveDataElementOptions(i + start).radius); + } + return parsed; + } + getMaxOverflow() { + const data = this._cachedMeta.data; + let max = 0; + for (let i = data.length - 1; i >= 0; --i) { + max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2); + } + return max > 0 && max; + } + getLabelAndValue(index) { + const meta = this._cachedMeta; + const {xScale, yScale} = meta; + const parsed = this.getParsed(index); + const x = xScale.getLabelForValue(parsed.x); + const y = yScale.getLabelForValue(parsed.y); + const r = parsed._custom; + return { + label: meta.label, + value: '(' + x + ', ' + y + (r ? ', ' + r : '') + ')' + }; + } + update(mode) { + const points = this._cachedMeta.data; + this.updateElements(points, 0, points.length, mode); + } + updateElements(points, start, count, mode) { + const reset = mode === 'reset'; + const {iScale, vScale} = this._cachedMeta; + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); + const iAxis = iScale.axis; + const vAxis = vScale.axis; + for (let i = start; i < start + count; i++) { + const point = points[i]; + const parsed = !reset && this.getParsed(i); + const properties = {}; + const iPixel = properties[iAxis] = reset ? iScale.getPixelForDecimal(0.5) : iScale.getPixelForValue(parsed[iAxis]); + const vPixel = properties[vAxis] = reset ? vScale.getBasePixel() : vScale.getPixelForValue(parsed[vAxis]); + properties.skip = isNaN(iPixel) || isNaN(vPixel); + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); + if (reset) { + properties.options.radius = 0; + } + } + this.updateElement(point, i, properties, mode); + } + } + resolveDataElementOptions(index, mode) { + const parsed = this.getParsed(index); + let values = super.resolveDataElementOptions(index, mode); + if (values.$shared) { + values = Object.assign({}, values, {$shared: false}); + } + const radius = values.radius; + if (mode !== 'active') { + values.radius = 0; + } + values.radius += valueOrDefault(parsed && parsed._custom, radius); + return values; + } +} +BubbleController.id = 'bubble'; +BubbleController.defaults = { + datasetElementType: false, + dataElementType: 'point', + animations: { + numbers: { + type: 'number', + properties: ['x', 'y', 'borderWidth', 'radius'] + } + } +}; +BubbleController.overrides = { + scales: { + x: { + type: 'linear' + }, + y: { + type: 'linear' + } + }, + plugins: { + tooltip: { + callbacks: { + title() { + return ''; + } + } + } + } +}; + +function getRatioAndOffset(rotation, circumference, cutout) { + let ratioX = 1; + let ratioY = 1; + let offsetX = 0; + let offsetY = 0; + if (circumference < TAU) { + const startAngle = rotation; + const endAngle = startAngle + circumference; + const startX = Math.cos(startAngle); + const startY = Math.sin(startAngle); + const endX = Math.cos(endAngle); + const endY = Math.sin(endAngle); + const calcMax = (angle, a, b) => _angleBetween(angle, startAngle, endAngle, true) ? 1 : Math.max(a, a * cutout, b, b * cutout); + const calcMin = (angle, a, b) => _angleBetween(angle, startAngle, endAngle, true) ? -1 : Math.min(a, a * cutout, b, b * cutout); + const maxX = calcMax(0, startX, endX); + const maxY = calcMax(HALF_PI, startY, endY); + const minX = calcMin(PI, startX, endX); + const minY = calcMin(PI + HALF_PI, startY, endY); + ratioX = (maxX - minX) / 2; + ratioY = (maxY - minY) / 2; + offsetX = -(maxX + minX) / 2; + offsetY = -(maxY + minY) / 2; + } + return {ratioX, ratioY, offsetX, offsetY}; +} +class DoughnutController extends DatasetController { + constructor(chart, datasetIndex) { + super(chart, datasetIndex); + this.enableOptionSharing = true; + this.innerRadius = undefined; + this.outerRadius = undefined; + this.offsetX = undefined; + this.offsetY = undefined; + } + linkScales() {} + parse(start, count) { + const data = this.getDataset().data; + const meta = this._cachedMeta; + if (this._parsing === false) { + meta._parsed = data; + } else { + let getter = (i) => +data[i]; + if (isObject(data[start])) { + const {key = 'value'} = this._parsing; + getter = (i) => +resolveObjectKey(data[i], key); + } + let i, ilen; + for (i = start, ilen = start + count; i < ilen; ++i) { + meta._parsed[i] = getter(i); + } + } + } + _getRotation() { + return toRadians(this.options.rotation - 90); + } + _getCircumference() { + return toRadians(this.options.circumference); + } + _getRotationExtents() { + let min = TAU; + let max = -TAU; + for (let i = 0; i < this.chart.data.datasets.length; ++i) { + if (this.chart.isDatasetVisible(i)) { + const controller = this.chart.getDatasetMeta(i).controller; + const rotation = controller._getRotation(); + const circumference = controller._getCircumference(); + min = Math.min(min, rotation); + max = Math.max(max, rotation + circumference); + } + } + return { + rotation: min, + circumference: max - min, + }; + } + update(mode) { + const chart = this.chart; + const {chartArea} = chart; + const meta = this._cachedMeta; + const arcs = meta.data; + const spacing = this.getMaxBorderWidth() + this.getMaxOffset(arcs) + this.options.spacing; + const maxSize = Math.max((Math.min(chartArea.width, chartArea.height) - spacing) / 2, 0); + const cutout = Math.min(toPercentage(this.options.cutout, maxSize), 1); + const chartWeight = this._getRingWeight(this.index); + const {circumference, rotation} = this._getRotationExtents(); + const {ratioX, ratioY, offsetX, offsetY} = getRatioAndOffset(rotation, circumference, cutout); + const maxWidth = (chartArea.width - spacing) / ratioX; + const maxHeight = (chartArea.height - spacing) / ratioY; + const maxRadius = Math.max(Math.min(maxWidth, maxHeight) / 2, 0); + const outerRadius = toDimension(this.options.radius, maxRadius); + const innerRadius = Math.max(outerRadius * cutout, 0); + const radiusLength = (outerRadius - innerRadius) / this._getVisibleDatasetWeightTotal(); + this.offsetX = offsetX * outerRadius; + this.offsetY = offsetY * outerRadius; + meta.total = this.calculateTotal(); + this.outerRadius = outerRadius - radiusLength * this._getRingWeightOffset(this.index); + this.innerRadius = Math.max(this.outerRadius - radiusLength * chartWeight, 0); + this.updateElements(arcs, 0, arcs.length, mode); + } + _circumference(i, reset) { + const opts = this.options; + const meta = this._cachedMeta; + const circumference = this._getCircumference(); + if ((reset && opts.animation.animateRotate) || !this.chart.getDataVisibility(i) || meta._parsed[i] === null || meta.data[i].hidden) { + return 0; + } + return this.calculateCircumference(meta._parsed[i] * circumference / TAU); + } + updateElements(arcs, start, count, mode) { + const reset = mode === 'reset'; + const chart = this.chart; + const chartArea = chart.chartArea; + const opts = chart.options; + const animationOpts = opts.animation; + const centerX = (chartArea.left + chartArea.right) / 2; + const centerY = (chartArea.top + chartArea.bottom) / 2; + const animateScale = reset && animationOpts.animateScale; + const innerRadius = animateScale ? 0 : this.innerRadius; + const outerRadius = animateScale ? 0 : this.outerRadius; + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); + let startAngle = this._getRotation(); + let i; + for (i = 0; i < start; ++i) { + startAngle += this._circumference(i, reset); + } + for (i = start; i < start + count; ++i) { + const circumference = this._circumference(i, reset); + const arc = arcs[i]; + const properties = { + x: centerX + this.offsetX, + y: centerY + this.offsetY, + startAngle, + endAngle: startAngle + circumference, + circumference, + outerRadius, + innerRadius + }; + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, arc.active ? 'active' : mode); + } + startAngle += circumference; + this.updateElement(arc, i, properties, mode); + } + } + calculateTotal() { + const meta = this._cachedMeta; + const metaData = meta.data; + let total = 0; + let i; + for (i = 0; i < metaData.length; i++) { + const value = meta._parsed[i]; + if (value !== null && !isNaN(value) && this.chart.getDataVisibility(i) && !metaData[i].hidden) { + total += Math.abs(value); + } + } + return total; + } + calculateCircumference(value) { + const total = this._cachedMeta.total; + if (total > 0 && !isNaN(value)) { + return TAU * (Math.abs(value) / total); + } + return 0; + } + getLabelAndValue(index) { + const meta = this._cachedMeta; + const chart = this.chart; + const labels = chart.data.labels || []; + const value = formatNumber(meta._parsed[index], chart.options.locale); + return { + label: labels[index] || '', + value, + }; + } + getMaxBorderWidth(arcs) { + let max = 0; + const chart = this.chart; + let i, ilen, meta, controller, options; + if (!arcs) { + for (i = 0, ilen = chart.data.datasets.length; i < ilen; ++i) { + if (chart.isDatasetVisible(i)) { + meta = chart.getDatasetMeta(i); + arcs = meta.data; + controller = meta.controller; + break; + } + } + } + if (!arcs) { + return 0; + } + for (i = 0, ilen = arcs.length; i < ilen; ++i) { + options = controller.resolveDataElementOptions(i); + if (options.borderAlign !== 'inner') { + max = Math.max(max, options.borderWidth || 0, options.hoverBorderWidth || 0); + } + } + return max; + } + getMaxOffset(arcs) { + let max = 0; + for (let i = 0, ilen = arcs.length; i < ilen; ++i) { + const options = this.resolveDataElementOptions(i); + max = Math.max(max, options.offset || 0, options.hoverOffset || 0); + } + return max; + } + _getRingWeightOffset(datasetIndex) { + let ringWeightOffset = 0; + for (let i = 0; i < datasetIndex; ++i) { + if (this.chart.isDatasetVisible(i)) { + ringWeightOffset += this._getRingWeight(i); + } + } + return ringWeightOffset; + } + _getRingWeight(datasetIndex) { + return Math.max(valueOrDefault(this.chart.data.datasets[datasetIndex].weight, 1), 0); + } + _getVisibleDatasetWeightTotal() { + return this._getRingWeightOffset(this.chart.data.datasets.length) || 1; + } +} +DoughnutController.id = 'doughnut'; +DoughnutController.defaults = { + datasetElementType: false, + dataElementType: 'arc', + animation: { + animateRotate: true, + animateScale: false + }, + animations: { + numbers: { + type: 'number', + properties: ['circumference', 'endAngle', 'innerRadius', 'outerRadius', 'startAngle', 'x', 'y', 'offset', 'borderWidth', 'spacing'] + }, + }, + cutout: '50%', + rotation: 0, + circumference: 360, + radius: '100%', + spacing: 0, + indexAxis: 'r', +}; +DoughnutController.descriptors = { + _scriptable: (name) => name !== 'spacing', + _indexable: (name) => name !== 'spacing', +}; +DoughnutController.overrides = { + aspectRatio: 1, + plugins: { + legend: { + labels: { + generateLabels(chart) { + const data = chart.data; + if (data.labels.length && data.datasets.length) { + const {labels: {pointStyle}} = chart.legend.options; + return data.labels.map((label, i) => { + const meta = chart.getDatasetMeta(0); + const style = meta.controller.getStyle(i); + return { + text: label, + fillStyle: style.backgroundColor, + strokeStyle: style.borderColor, + lineWidth: style.borderWidth, + pointStyle: pointStyle, + hidden: !chart.getDataVisibility(i), + index: i + }; + }); + } + return []; + } + }, + onClick(e, legendItem, legend) { + legend.chart.toggleDataVisibility(legendItem.index); + legend.chart.update(); + } + }, + tooltip: { + callbacks: { + title() { + return ''; + }, + label(tooltipItem) { + let dataLabel = tooltipItem.label; + const value = ': ' + tooltipItem.formattedValue; + if (isArray(dataLabel)) { + dataLabel = dataLabel.slice(); + dataLabel[0] += value; + } else { + dataLabel += value; + } + return dataLabel; + } + } + } + } +}; + +class LineController extends DatasetController { + initialize() { + this.enableOptionSharing = true; + this.supportsDecimation = true; + super.initialize(); + } + update(mode) { + const meta = this._cachedMeta; + const {dataset: line, data: points = [], _dataset} = meta; + const animationsDisabled = this.chart._animationsDisabled; + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + this._drawStart = start; + this._drawCount = count; + if (_scaleRangesChanged(meta)) { + start = 0; + count = points.length; + } + line._chart = this.chart; + line._datasetIndex = this.index; + line._decimated = !!_dataset._decimated; + line.points = points; + const options = this.resolveDatasetElementOptions(mode); + if (!this.options.showLine) { + options.borderWidth = 0; + } + options.segment = this.options.segment; + this.updateElement(line, undefined, { + animated: !animationsDisabled, + options + }, mode); + this.updateElements(points, start, count, mode); + } + updateElements(points, start, count, mode) { + const reset = mode === 'reset'; + const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; + const {sharedOptions, includeOptions} = this._getSharedOptions(start, mode); + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const {spanGaps, segment} = this.options; + const maxGapLength = isNumber(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY; + const directUpdate = this.chart._animationsDisabled || reset || mode === 'none'; + let prevParsed = start > 0 && this.getParsed(start - 1); + for (let i = start; i < start + count; ++i) { + const point = points[i]; + const parsed = this.getParsed(i); + const properties = directUpdate ? point : {}; + const nullData = isNullOrUndef(parsed[vAxis]); + const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); + const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); + properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; + if (segment) { + properties.parsed = parsed; + properties.raw = _dataset.data[i]; + } + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); + } + if (!directUpdate) { + this.updateElement(point, i, properties, mode); + } + prevParsed = parsed; + } + } + getMaxOverflow() { + const meta = this._cachedMeta; + const dataset = meta.dataset; + const border = dataset.options && dataset.options.borderWidth || 0; + const data = meta.data || []; + if (!data.length) { + return border; + } + const firstPoint = data[0].size(this.resolveDataElementOptions(0)); + const lastPoint = data[data.length - 1].size(this.resolveDataElementOptions(data.length - 1)); + return Math.max(border, firstPoint, lastPoint) / 2; + } + draw() { + const meta = this._cachedMeta; + meta.dataset.updateControlPoints(this.chart.chartArea, meta.iScale.axis); + super.draw(); + } +} +LineController.id = 'line'; +LineController.defaults = { + datasetElementType: 'line', + dataElementType: 'point', + showLine: true, + spanGaps: false, +}; +LineController.overrides = { + scales: { + _index_: { + type: 'category', + }, + _value_: { + type: 'linear', + }, + } +}; + +class PolarAreaController extends DatasetController { + constructor(chart, datasetIndex) { + super(chart, datasetIndex); + this.innerRadius = undefined; + this.outerRadius = undefined; + } + getLabelAndValue(index) { + const meta = this._cachedMeta; + const chart = this.chart; + const labels = chart.data.labels || []; + const value = formatNumber(meta._parsed[index].r, chart.options.locale); + return { + label: labels[index] || '', + value, + }; + } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } + update(mode) { + const arcs = this._cachedMeta.data; + this._updateRadius(); + this.updateElements(arcs, 0, arcs.length, mode); + } + getMinMax() { + const meta = this._cachedMeta; + const range = {min: Number.POSITIVE_INFINITY, max: Number.NEGATIVE_INFINITY}; + meta.data.forEach((element, index) => { + const parsed = this.getParsed(index).r; + if (!isNaN(parsed) && this.chart.getDataVisibility(index)) { + if (parsed < range.min) { + range.min = parsed; + } + if (parsed > range.max) { + range.max = parsed; + } + } + }); + return range; + } + _updateRadius() { + const chart = this.chart; + const chartArea = chart.chartArea; + const opts = chart.options; + const minSize = Math.min(chartArea.right - chartArea.left, chartArea.bottom - chartArea.top); + const outerRadius = Math.max(minSize / 2, 0); + const innerRadius = Math.max(opts.cutoutPercentage ? (outerRadius / 100) * (opts.cutoutPercentage) : 1, 0); + const radiusLength = (outerRadius - innerRadius) / chart.getVisibleDatasetCount(); + this.outerRadius = outerRadius - (radiusLength * this.index); + this.innerRadius = this.outerRadius - radiusLength; + } + updateElements(arcs, start, count, mode) { + const reset = mode === 'reset'; + const chart = this.chart; + const opts = chart.options; + const animationOpts = opts.animation; + const scale = this._cachedMeta.rScale; + const centerX = scale.xCenter; + const centerY = scale.yCenter; + const datasetStartAngle = scale.getIndexAngle(0) - 0.5 * PI; + let angle = datasetStartAngle; + let i; + const defaultAngle = 360 / this.countVisibleElements(); + for (i = 0; i < start; ++i) { + angle += this._computeAngle(i, mode, defaultAngle); + } + for (i = start; i < start + count; i++) { + const arc = arcs[i]; + let startAngle = angle; + let endAngle = angle + this._computeAngle(i, mode, defaultAngle); + let outerRadius = chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(this.getParsed(i).r) : 0; + angle = endAngle; + if (reset) { + if (animationOpts.animateScale) { + outerRadius = 0; + } + if (animationOpts.animateRotate) { + startAngle = endAngle = datasetStartAngle; + } + } + const properties = { + x: centerX, + y: centerY, + innerRadius: 0, + outerRadius, + startAngle, + endAngle, + options: this.resolveDataElementOptions(i, arc.active ? 'active' : mode) + }; + this.updateElement(arc, i, properties, mode); + } + } + countVisibleElements() { + const meta = this._cachedMeta; + let count = 0; + meta.data.forEach((element, index) => { + if (!isNaN(this.getParsed(index).r) && this.chart.getDataVisibility(index)) { + count++; + } + }); + return count; + } + _computeAngle(index, mode, defaultAngle) { + return this.chart.getDataVisibility(index) + ? toRadians(this.resolveDataElementOptions(index, mode).angle || defaultAngle) + : 0; + } +} +PolarAreaController.id = 'polarArea'; +PolarAreaController.defaults = { + dataElementType: 'arc', + animation: { + animateRotate: true, + animateScale: true + }, + animations: { + numbers: { + type: 'number', + properties: ['x', 'y', 'startAngle', 'endAngle', 'innerRadius', 'outerRadius'] + }, + }, + indexAxis: 'r', + startAngle: 0, +}; +PolarAreaController.overrides = { + aspectRatio: 1, + plugins: { + legend: { + labels: { + generateLabels(chart) { + const data = chart.data; + if (data.labels.length && data.datasets.length) { + const {labels: {pointStyle}} = chart.legend.options; + return data.labels.map((label, i) => { + const meta = chart.getDatasetMeta(0); + const style = meta.controller.getStyle(i); + return { + text: label, + fillStyle: style.backgroundColor, + strokeStyle: style.borderColor, + lineWidth: style.borderWidth, + pointStyle: pointStyle, + hidden: !chart.getDataVisibility(i), + index: i + }; + }); + } + return []; + } + }, + onClick(e, legendItem, legend) { + legend.chart.toggleDataVisibility(legendItem.index); + legend.chart.update(); + } + }, + tooltip: { + callbacks: { + title() { + return ''; + }, + label(context) { + return context.chart.data.labels[context.dataIndex] + ': ' + context.formattedValue; + } + } + } + }, + scales: { + r: { + type: 'radialLinear', + angleLines: { + display: false + }, + beginAtZero: true, + grid: { + circular: true + }, + pointLabels: { + display: false + }, + startAngle: 0 + } + } +}; + +class PieController extends DoughnutController { +} +PieController.id = 'pie'; +PieController.defaults = { + cutout: 0, + rotation: 0, + circumference: 360, + radius: '100%' +}; + +class RadarController extends DatasetController { + getLabelAndValue(index) { + const vScale = this._cachedMeta.vScale; + const parsed = this.getParsed(index); + return { + label: vScale.getLabels()[index], + value: '' + vScale.getLabelForValue(parsed[vScale.axis]) + }; + } + parseObjectData(meta, data, start, count) { + return _parseObjectDataRadialScale.bind(this)(meta, data, start, count); + } + update(mode) { + const meta = this._cachedMeta; + const line = meta.dataset; + const points = meta.data || []; + const labels = meta.iScale.getLabels(); + line.points = points; + if (mode !== 'resize') { + const options = this.resolveDatasetElementOptions(mode); + if (!this.options.showLine) { + options.borderWidth = 0; + } + const properties = { + _loop: true, + _fullLoop: labels.length === points.length, + options + }; + this.updateElement(line, undefined, properties, mode); + } + this.updateElements(points, 0, points.length, mode); + } + updateElements(points, start, count, mode) { + const scale = this._cachedMeta.rScale; + const reset = mode === 'reset'; + for (let i = start; i < start + count; i++) { + const point = points[i]; + const options = this.resolveDataElementOptions(i, point.active ? 'active' : mode); + const pointPosition = scale.getPointPositionForValue(i, this.getParsed(i).r); + const x = reset ? scale.xCenter : pointPosition.x; + const y = reset ? scale.yCenter : pointPosition.y; + const properties = { + x, + y, + angle: pointPosition.angle, + skip: isNaN(x) || isNaN(y), + options + }; + this.updateElement(point, i, properties, mode); + } + } +} +RadarController.id = 'radar'; +RadarController.defaults = { + datasetElementType: 'line', + dataElementType: 'point', + indexAxis: 'r', + showLine: true, + elements: { + line: { + fill: 'start' + } + }, +}; +RadarController.overrides = { + aspectRatio: 1, + scales: { + r: { + type: 'radialLinear', + } + } +}; + +class Element { + constructor() { + this.x = undefined; + this.y = undefined; + this.active = false; + this.options = undefined; + this.$animations = undefined; + } + tooltipPosition(useFinalPosition) { + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + return {x, y}; + } + hasValue() { + return isNumber(this.x) && isNumber(this.y); + } + getProps(props, final) { + const anims = this.$animations; + if (!final || !anims) { + return this; + } + const ret = {}; + props.forEach(prop => { + ret[prop] = anims[prop] && anims[prop].active() ? anims[prop]._to : this[prop]; + }); + return ret; + } +} +Element.defaults = {}; +Element.defaultRoutes = undefined; + +const formatters = { + values(value) { + return isArray(value) ? value : '' + value; + }, + numeric(tickValue, index, ticks) { + if (tickValue === 0) { + return '0'; + } + const locale = this.chart.options.locale; + let notation; + let delta = tickValue; + if (ticks.length > 1) { + const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); + if (maxTick < 1e-4 || maxTick > 1e+15) { + notation = 'scientific'; + } + delta = calculateDelta(tickValue, ticks); + } + const logDelta = log10(Math.abs(delta)); + const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); + const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; + Object.assign(options, this.options.ticks.format); + return formatNumber(tickValue, locale, options); + }, + logarithmic(tickValue, index, ticks) { + if (tickValue === 0) { + return '0'; + } + const remain = tickValue / (Math.pow(10, Math.floor(log10(tickValue)))); + if (remain === 1 || remain === 2 || remain === 5) { + return formatters.numeric.call(this, tickValue, index, ticks); + } + return ''; + } +}; +function calculateDelta(tickValue, ticks) { + let delta = ticks.length > 3 ? ticks[2].value - ticks[1].value : ticks[1].value - ticks[0].value; + if (Math.abs(delta) >= 1 && tickValue !== Math.floor(tickValue)) { + delta = tickValue - Math.floor(tickValue); + } + return delta; +} +var Ticks = {formatters}; + +defaults.set('scale', { + display: true, + offset: false, + reverse: false, + beginAtZero: false, + bounds: 'ticks', + grace: 0, + grid: { + display: true, + lineWidth: 1, + drawBorder: true, + drawOnChartArea: true, + drawTicks: true, + tickLength: 8, + tickWidth: (_ctx, options) => options.lineWidth, + tickColor: (_ctx, options) => options.color, + offset: false, + borderDash: [], + borderDashOffset: 0.0, + borderWidth: 1 + }, + title: { + display: false, + text: '', + padding: { + top: 4, + bottom: 4 + } + }, + ticks: { + minRotation: 0, + maxRotation: 50, + mirror: false, + textStrokeWidth: 0, + textStrokeColor: '', + padding: 3, + display: true, + autoSkip: true, + autoSkipPadding: 3, + labelOffset: 0, + callback: Ticks.formatters.values, + minor: {}, + major: {}, + align: 'center', + crossAlign: 'near', + showLabelBackdrop: false, + backdropColor: 'rgba(255, 255, 255, 0.75)', + backdropPadding: 2, + } +}); +defaults.route('scale.ticks', 'color', '', 'color'); +defaults.route('scale.grid', 'color', '', 'borderColor'); +defaults.route('scale.grid', 'borderColor', '', 'borderColor'); +defaults.route('scale.title', 'color', '', 'color'); +defaults.describe('scale', { + _fallback: false, + _scriptable: (name) => !name.startsWith('before') && !name.startsWith('after') && name !== 'callback' && name !== 'parser', + _indexable: (name) => name !== 'borderDash' && name !== 'tickBorderDash', +}); +defaults.describe('scales', { + _fallback: 'scale', +}); +defaults.describe('scale.ticks', { + _scriptable: (name) => name !== 'backdropPadding' && name !== 'callback', + _indexable: (name) => name !== 'backdropPadding', +}); + +function autoSkip(scale, ticks) { + const tickOpts = scale.options.ticks; + const ticksLimit = tickOpts.maxTicksLimit || determineMaxTicks(scale); + const majorIndices = tickOpts.major.enabled ? getMajorIndices(ticks) : []; + const numMajorIndices = majorIndices.length; + const first = majorIndices[0]; + const last = majorIndices[numMajorIndices - 1]; + const newTicks = []; + if (numMajorIndices > ticksLimit) { + skipMajors(ticks, newTicks, majorIndices, numMajorIndices / ticksLimit); + return newTicks; + } + const spacing = calculateSpacing(majorIndices, ticks, ticksLimit); + if (numMajorIndices > 0) { + let i, ilen; + const avgMajorSpacing = numMajorIndices > 1 ? Math.round((last - first) / (numMajorIndices - 1)) : null; + skip(ticks, newTicks, spacing, isNullOrUndef(avgMajorSpacing) ? 0 : first - avgMajorSpacing, first); + for (i = 0, ilen = numMajorIndices - 1; i < ilen; i++) { + skip(ticks, newTicks, spacing, majorIndices[i], majorIndices[i + 1]); + } + skip(ticks, newTicks, spacing, last, isNullOrUndef(avgMajorSpacing) ? ticks.length : last + avgMajorSpacing); + return newTicks; + } + skip(ticks, newTicks, spacing); + return newTicks; +} +function determineMaxTicks(scale) { + const offset = scale.options.offset; + const tickLength = scale._tickSize(); + const maxScale = scale._length / tickLength + (offset ? 0 : 1); + const maxChart = scale._maxLength / tickLength; + return Math.floor(Math.min(maxScale, maxChart)); +} +function calculateSpacing(majorIndices, ticks, ticksLimit) { + const evenMajorSpacing = getEvenSpacing(majorIndices); + const spacing = ticks.length / ticksLimit; + if (!evenMajorSpacing) { + return Math.max(spacing, 1); + } + const factors = _factorize(evenMajorSpacing); + for (let i = 0, ilen = factors.length - 1; i < ilen; i++) { + const factor = factors[i]; + if (factor > spacing) { + return factor; + } + } + return Math.max(spacing, 1); +} +function getMajorIndices(ticks) { + const result = []; + let i, ilen; + for (i = 0, ilen = ticks.length; i < ilen; i++) { + if (ticks[i].major) { + result.push(i); + } + } + return result; +} +function skipMajors(ticks, newTicks, majorIndices, spacing) { + let count = 0; + let next = majorIndices[0]; + let i; + spacing = Math.ceil(spacing); + for (i = 0; i < ticks.length; i++) { + if (i === next) { + newTicks.push(ticks[i]); + count++; + next = majorIndices[count * spacing]; + } + } +} +function skip(ticks, newTicks, spacing, majorStart, majorEnd) { + const start = valueOrDefault(majorStart, 0); + const end = Math.min(valueOrDefault(majorEnd, ticks.length), ticks.length); + let count = 0; + let length, i, next; + spacing = Math.ceil(spacing); + if (majorEnd) { + length = majorEnd - majorStart; + spacing = length / Math.floor(length / spacing); + } + next = start; + while (next < 0) { + count++; + next = Math.round(start + count * spacing); + } + for (i = Math.max(start, 0); i < end; i++) { + if (i === next) { + newTicks.push(ticks[i]); + count++; + next = Math.round(start + count * spacing); + } + } +} +function getEvenSpacing(arr) { + const len = arr.length; + let i, diff; + if (len < 2) { + return false; + } + for (diff = arr[0], i = 1; i < len; ++i) { + if (arr[i] - arr[i - 1] !== diff) { + return false; + } + } + return diff; +} + +const reverseAlign = (align) => align === 'left' ? 'right' : align === 'right' ? 'left' : align; +const offsetFromEdge = (scale, edge, offset) => edge === 'top' || edge === 'left' ? scale[edge] + offset : scale[edge] - offset; +function sample(arr, numItems) { + const result = []; + const increment = arr.length / numItems; + const len = arr.length; + let i = 0; + for (; i < len; i += increment) { + result.push(arr[Math.floor(i)]); + } + return result; +} +function getPixelForGridLine(scale, index, offsetGridLines) { + const length = scale.ticks.length; + const validIndex = Math.min(index, length - 1); + const start = scale._startPixel; + const end = scale._endPixel; + const epsilon = 1e-6; + let lineValue = scale.getPixelForTick(validIndex); + let offset; + if (offsetGridLines) { + if (length === 1) { + offset = Math.max(lineValue - start, end - lineValue); + } else if (index === 0) { + offset = (scale.getPixelForTick(1) - lineValue) / 2; + } else { + offset = (lineValue - scale.getPixelForTick(validIndex - 1)) / 2; + } + lineValue += validIndex < index ? offset : -offset; + if (lineValue < start - epsilon || lineValue > end + epsilon) { + return; + } + } + return lineValue; +} +function garbageCollect(caches, length) { + each(caches, (cache) => { + const gc = cache.gc; + const gcLen = gc.length / 2; + let i; + if (gcLen > length) { + for (i = 0; i < gcLen; ++i) { + delete cache.data[gc[i]]; + } + gc.splice(0, gcLen); + } + }); +} +function getTickMarkLength(options) { + return options.drawTicks ? options.tickLength : 0; +} +function getTitleHeight(options, fallback) { + if (!options.display) { + return 0; + } + const font = toFont(options.font, fallback); + const padding = toPadding(options.padding); + const lines = isArray(options.text) ? options.text.length : 1; + return (lines * font.lineHeight) + padding.height; +} +function createScaleContext(parent, scale) { + return createContext(parent, { + scale, + type: 'scale' + }); +} +function createTickContext(parent, index, tick) { + return createContext(parent, { + tick, + index, + type: 'tick' + }); +} +function titleAlign(align, position, reverse) { + let ret = _toLeftRightCenter(align); + if ((reverse && position !== 'right') || (!reverse && position === 'right')) { + ret = reverseAlign(ret); + } + return ret; +} +function titleArgs(scale, offset, position, align) { + const {top, left, bottom, right, chart} = scale; + const {chartArea, scales} = chart; + let rotation = 0; + let maxWidth, titleX, titleY; + const height = bottom - top; + const width = right - left; + if (scale.isHorizontal()) { + titleX = _alignStartEnd(align, left, right); + if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + titleY = scales[positionAxisID].getPixelForValue(value) + height - offset; + } else if (position === 'center') { + titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset; + } else { + titleY = offsetFromEdge(scale, position, offset); + } + maxWidth = right - left; + } else { + if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + titleX = scales[positionAxisID].getPixelForValue(value) - width + offset; + } else if (position === 'center') { + titleX = (chartArea.left + chartArea.right) / 2 - width + offset; + } else { + titleX = offsetFromEdge(scale, position, offset); + } + titleY = _alignStartEnd(align, bottom, top); + rotation = position === 'left' ? -HALF_PI : HALF_PI; + } + return {titleX, titleY, maxWidth, rotation}; +} +class Scale extends Element { + constructor(cfg) { + super(); + this.id = cfg.id; + this.type = cfg.type; + this.options = undefined; + this.ctx = cfg.ctx; + this.chart = cfg.chart; + this.top = undefined; + this.bottom = undefined; + this.left = undefined; + this.right = undefined; + this.width = undefined; + this.height = undefined; + this._margins = { + left: 0, + right: 0, + top: 0, + bottom: 0 + }; + this.maxWidth = undefined; + this.maxHeight = undefined; + this.paddingTop = undefined; + this.paddingBottom = undefined; + this.paddingLeft = undefined; + this.paddingRight = undefined; + this.axis = undefined; + this.labelRotation = undefined; + this.min = undefined; + this.max = undefined; + this._range = undefined; + this.ticks = []; + this._gridLineItems = null; + this._labelItems = null; + this._labelSizes = null; + this._length = 0; + this._maxLength = 0; + this._longestTextCache = {}; + this._startPixel = undefined; + this._endPixel = undefined; + this._reversePixels = false; + this._userMax = undefined; + this._userMin = undefined; + this._suggestedMax = undefined; + this._suggestedMin = undefined; + this._ticksLength = 0; + this._borderValue = 0; + this._cache = {}; + this._dataLimitsCached = false; + this.$context = undefined; + } + init(options) { + this.options = options.setContext(this.getContext()); + this.axis = options.axis; + this._userMin = this.parse(options.min); + this._userMax = this.parse(options.max); + this._suggestedMin = this.parse(options.suggestedMin); + this._suggestedMax = this.parse(options.suggestedMax); + } + parse(raw, index) { + return raw; + } + getUserBounds() { + let {_userMin, _userMax, _suggestedMin, _suggestedMax} = this; + _userMin = finiteOrDefault(_userMin, Number.POSITIVE_INFINITY); + _userMax = finiteOrDefault(_userMax, Number.NEGATIVE_INFINITY); + _suggestedMin = finiteOrDefault(_suggestedMin, Number.POSITIVE_INFINITY); + _suggestedMax = finiteOrDefault(_suggestedMax, Number.NEGATIVE_INFINITY); + return { + min: finiteOrDefault(_userMin, _suggestedMin), + max: finiteOrDefault(_userMax, _suggestedMax), + minDefined: isNumberFinite(_userMin), + maxDefined: isNumberFinite(_userMax) + }; + } + getMinMax(canStack) { + let {min, max, minDefined, maxDefined} = this.getUserBounds(); + let range; + if (minDefined && maxDefined) { + return {min, max}; + } + const metas = this.getMatchingVisibleMetas(); + for (let i = 0, ilen = metas.length; i < ilen; ++i) { + range = metas[i].controller.getMinMax(this, canStack); + if (!minDefined) { + min = Math.min(min, range.min); + } + if (!maxDefined) { + max = Math.max(max, range.max); + } + } + min = maxDefined && min > max ? max : min; + max = minDefined && min > max ? min : max; + return { + min: finiteOrDefault(min, finiteOrDefault(max, min)), + max: finiteOrDefault(max, finiteOrDefault(min, max)) + }; + } + getPadding() { + return { + left: this.paddingLeft || 0, + top: this.paddingTop || 0, + right: this.paddingRight || 0, + bottom: this.paddingBottom || 0 + }; + } + getTicks() { + return this.ticks; + } + getLabels() { + const data = this.chart.data; + return this.options.labels || (this.isHorizontal() ? data.xLabels : data.yLabels) || data.labels || []; + } + beforeLayout() { + this._cache = {}; + this._dataLimitsCached = false; + } + beforeUpdate() { + callback(this.options.beforeUpdate, [this]); + } + update(maxWidth, maxHeight, margins) { + const {beginAtZero, grace, ticks: tickOpts} = this.options; + const sampleSize = tickOpts.sampleSize; + this.beforeUpdate(); + this.maxWidth = maxWidth; + this.maxHeight = maxHeight; + this._margins = margins = Object.assign({ + left: 0, + right: 0, + top: 0, + bottom: 0 + }, margins); + this.ticks = null; + this._labelSizes = null; + this._gridLineItems = null; + this._labelItems = null; + this.beforeSetDimensions(); + this.setDimensions(); + this.afterSetDimensions(); + this._maxLength = this.isHorizontal() + ? this.width + margins.left + margins.right + : this.height + margins.top + margins.bottom; + if (!this._dataLimitsCached) { + this.beforeDataLimits(); + this.determineDataLimits(); + this.afterDataLimits(); + this._range = _addGrace(this, grace, beginAtZero); + this._dataLimitsCached = true; + } + this.beforeBuildTicks(); + this.ticks = this.buildTicks() || []; + this.afterBuildTicks(); + const samplingEnabled = sampleSize < this.ticks.length; + this._convertTicksToLabels(samplingEnabled ? sample(this.ticks, sampleSize) : this.ticks); + this.configure(); + this.beforeCalculateLabelRotation(); + this.calculateLabelRotation(); + this.afterCalculateLabelRotation(); + if (tickOpts.display && (tickOpts.autoSkip || tickOpts.source === 'auto')) { + this.ticks = autoSkip(this, this.ticks); + this._labelSizes = null; + this.afterAutoSkip(); + } + if (samplingEnabled) { + this._convertTicksToLabels(this.ticks); + } + this.beforeFit(); + this.fit(); + this.afterFit(); + this.afterUpdate(); + } + configure() { + let reversePixels = this.options.reverse; + let startPixel, endPixel; + if (this.isHorizontal()) { + startPixel = this.left; + endPixel = this.right; + } else { + startPixel = this.top; + endPixel = this.bottom; + reversePixels = !reversePixels; + } + this._startPixel = startPixel; + this._endPixel = endPixel; + this._reversePixels = reversePixels; + this._length = endPixel - startPixel; + this._alignToPixels = this.options.alignToPixels; + } + afterUpdate() { + callback(this.options.afterUpdate, [this]); + } + beforeSetDimensions() { + callback(this.options.beforeSetDimensions, [this]); + } + setDimensions() { + if (this.isHorizontal()) { + this.width = this.maxWidth; + this.left = 0; + this.right = this.width; + } else { + this.height = this.maxHeight; + this.top = 0; + this.bottom = this.height; + } + this.paddingLeft = 0; + this.paddingTop = 0; + this.paddingRight = 0; + this.paddingBottom = 0; + } + afterSetDimensions() { + callback(this.options.afterSetDimensions, [this]); + } + _callHooks(name) { + this.chart.notifyPlugins(name, this.getContext()); + callback(this.options[name], [this]); + } + beforeDataLimits() { + this._callHooks('beforeDataLimits'); + } + determineDataLimits() {} + afterDataLimits() { + this._callHooks('afterDataLimits'); + } + beforeBuildTicks() { + this._callHooks('beforeBuildTicks'); + } + buildTicks() { + return []; + } + afterBuildTicks() { + this._callHooks('afterBuildTicks'); + } + beforeTickToLabelConversion() { + callback(this.options.beforeTickToLabelConversion, [this]); + } + generateTickLabels(ticks) { + const tickOpts = this.options.ticks; + let i, ilen, tick; + for (i = 0, ilen = ticks.length; i < ilen; i++) { + tick = ticks[i]; + tick.label = callback(tickOpts.callback, [tick.value, i, ticks], this); + } + } + afterTickToLabelConversion() { + callback(this.options.afterTickToLabelConversion, [this]); + } + beforeCalculateLabelRotation() { + callback(this.options.beforeCalculateLabelRotation, [this]); + } + calculateLabelRotation() { + const options = this.options; + const tickOpts = options.ticks; + const numTicks = this.ticks.length; + const minRotation = tickOpts.minRotation || 0; + const maxRotation = tickOpts.maxRotation; + let labelRotation = minRotation; + let tickWidth, maxHeight, maxLabelDiagonal; + if (!this._isVisible() || !tickOpts.display || minRotation >= maxRotation || numTicks <= 1 || !this.isHorizontal()) { + this.labelRotation = minRotation; + return; + } + const labelSizes = this._getLabelSizes(); + const maxLabelWidth = labelSizes.widest.width; + const maxLabelHeight = labelSizes.highest.height; + const maxWidth = _limitValue(this.chart.width - maxLabelWidth, 0, this.maxWidth); + tickWidth = options.offset ? this.maxWidth / numTicks : maxWidth / (numTicks - 1); + if (maxLabelWidth + 6 > tickWidth) { + tickWidth = maxWidth / (numTicks - (options.offset ? 0.5 : 1)); + maxHeight = this.maxHeight - getTickMarkLength(options.grid) + - tickOpts.padding - getTitleHeight(options.title, this.chart.options.font); + maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight); + labelRotation = toDegrees(Math.min( + Math.asin(_limitValue((labelSizes.highest.height + 6) / tickWidth, -1, 1)), + Math.asin(_limitValue(maxHeight / maxLabelDiagonal, -1, 1)) - Math.asin(_limitValue(maxLabelHeight / maxLabelDiagonal, -1, 1)) + )); + labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation)); + } + this.labelRotation = labelRotation; + } + afterCalculateLabelRotation() { + callback(this.options.afterCalculateLabelRotation, [this]); + } + afterAutoSkip() {} + beforeFit() { + callback(this.options.beforeFit, [this]); + } + fit() { + const minSize = { + width: 0, + height: 0 + }; + const {chart, options: {ticks: tickOpts, title: titleOpts, grid: gridOpts}} = this; + const display = this._isVisible(); + const isHorizontal = this.isHorizontal(); + if (display) { + const titleHeight = getTitleHeight(titleOpts, chart.options.font); + if (isHorizontal) { + minSize.width = this.maxWidth; + minSize.height = getTickMarkLength(gridOpts) + titleHeight; + } else { + minSize.height = this.maxHeight; + minSize.width = getTickMarkLength(gridOpts) + titleHeight; + } + if (tickOpts.display && this.ticks.length) { + const {first, last, widest, highest} = this._getLabelSizes(); + const tickPadding = tickOpts.padding * 2; + const angleRadians = toRadians(this.labelRotation); + const cos = Math.cos(angleRadians); + const sin = Math.sin(angleRadians); + if (isHorizontal) { + const labelHeight = tickOpts.mirror ? 0 : sin * widest.width + cos * highest.height; + minSize.height = Math.min(this.maxHeight, minSize.height + labelHeight + tickPadding); + } else { + const labelWidth = tickOpts.mirror ? 0 : cos * widest.width + sin * highest.height; + minSize.width = Math.min(this.maxWidth, minSize.width + labelWidth + tickPadding); + } + this._calculatePadding(first, last, sin, cos); + } + } + this._handleMargins(); + if (isHorizontal) { + this.width = this._length = chart.width - this._margins.left - this._margins.right; + this.height = minSize.height; + } else { + this.width = minSize.width; + this.height = this._length = chart.height - this._margins.top - this._margins.bottom; + } + } + _calculatePadding(first, last, sin, cos) { + const {ticks: {align, padding}, position} = this.options; + const isRotated = this.labelRotation !== 0; + const labelsBelowTicks = position !== 'top' && this.axis === 'x'; + if (this.isHorizontal()) { + const offsetLeft = this.getPixelForTick(0) - this.left; + const offsetRight = this.right - this.getPixelForTick(this.ticks.length - 1); + let paddingLeft = 0; + let paddingRight = 0; + if (isRotated) { + if (labelsBelowTicks) { + paddingLeft = cos * first.width; + paddingRight = sin * last.height; + } else { + paddingLeft = sin * first.height; + paddingRight = cos * last.width; + } + } else if (align === 'start') { + paddingRight = last.width; + } else if (align === 'end') { + paddingLeft = first.width; + } else if (align !== 'inner') { + paddingLeft = first.width / 2; + paddingRight = last.width / 2; + } + this.paddingLeft = Math.max((paddingLeft - offsetLeft + padding) * this.width / (this.width - offsetLeft), 0); + this.paddingRight = Math.max((paddingRight - offsetRight + padding) * this.width / (this.width - offsetRight), 0); + } else { + let paddingTop = last.height / 2; + let paddingBottom = first.height / 2; + if (align === 'start') { + paddingTop = 0; + paddingBottom = first.height; + } else if (align === 'end') { + paddingTop = last.height; + paddingBottom = 0; + } + this.paddingTop = paddingTop + padding; + this.paddingBottom = paddingBottom + padding; + } + } + _handleMargins() { + if (this._margins) { + this._margins.left = Math.max(this.paddingLeft, this._margins.left); + this._margins.top = Math.max(this.paddingTop, this._margins.top); + this._margins.right = Math.max(this.paddingRight, this._margins.right); + this._margins.bottom = Math.max(this.paddingBottom, this._margins.bottom); + } + } + afterFit() { + callback(this.options.afterFit, [this]); + } + isHorizontal() { + const {axis, position} = this.options; + return position === 'top' || position === 'bottom' || axis === 'x'; + } + isFullSize() { + return this.options.fullSize; + } + _convertTicksToLabels(ticks) { + this.beforeTickToLabelConversion(); + this.generateTickLabels(ticks); + let i, ilen; + for (i = 0, ilen = ticks.length; i < ilen; i++) { + if (isNullOrUndef(ticks[i].label)) { + ticks.splice(i, 1); + ilen--; + i--; + } + } + this.afterTickToLabelConversion(); + } + _getLabelSizes() { + let labelSizes = this._labelSizes; + if (!labelSizes) { + const sampleSize = this.options.ticks.sampleSize; + let ticks = this.ticks; + if (sampleSize < ticks.length) { + ticks = sample(ticks, sampleSize); + } + this._labelSizes = labelSizes = this._computeLabelSizes(ticks, ticks.length); + } + return labelSizes; + } + _computeLabelSizes(ticks, length) { + const {ctx, _longestTextCache: caches} = this; + const widths = []; + const heights = []; + let widestLabelSize = 0; + let highestLabelSize = 0; + let i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel; + for (i = 0; i < length; ++i) { + label = ticks[i].label; + tickFont = this._resolveTickFontOptions(i); + ctx.font = fontString = tickFont.string; + cache = caches[fontString] = caches[fontString] || {data: {}, gc: []}; + lineHeight = tickFont.lineHeight; + width = height = 0; + if (!isNullOrUndef(label) && !isArray(label)) { + width = _measureText(ctx, cache.data, cache.gc, width, label); + height = lineHeight; + } else if (isArray(label)) { + for (j = 0, jlen = label.length; j < jlen; ++j) { + nestedLabel = label[j]; + if (!isNullOrUndef(nestedLabel) && !isArray(nestedLabel)) { + width = _measureText(ctx, cache.data, cache.gc, width, nestedLabel); + height += lineHeight; + } + } + } + widths.push(width); + heights.push(height); + widestLabelSize = Math.max(width, widestLabelSize); + highestLabelSize = Math.max(height, highestLabelSize); + } + garbageCollect(caches, length); + const widest = widths.indexOf(widestLabelSize); + const highest = heights.indexOf(highestLabelSize); + const valueAt = (idx) => ({width: widths[idx] || 0, height: heights[idx] || 0}); + return { + first: valueAt(0), + last: valueAt(length - 1), + widest: valueAt(widest), + highest: valueAt(highest), + widths, + heights, + }; + } + getLabelForValue(value) { + return value; + } + getPixelForValue(value, index) { + return NaN; + } + getValueForPixel(pixel) {} + getPixelForTick(index) { + const ticks = this.ticks; + if (index < 0 || index > ticks.length - 1) { + return null; + } + return this.getPixelForValue(ticks[index].value); + } + getPixelForDecimal(decimal) { + if (this._reversePixels) { + decimal = 1 - decimal; + } + const pixel = this._startPixel + decimal * this._length; + return _int16Range(this._alignToPixels ? _alignPixel(this.chart, pixel, 0) : pixel); + } + getDecimalForPixel(pixel) { + const decimal = (pixel - this._startPixel) / this._length; + return this._reversePixels ? 1 - decimal : decimal; + } + getBasePixel() { + return this.getPixelForValue(this.getBaseValue()); + } + getBaseValue() { + const {min, max} = this; + return min < 0 && max < 0 ? max : + min > 0 && max > 0 ? min : + 0; + } + getContext(index) { + const ticks = this.ticks || []; + if (index >= 0 && index < ticks.length) { + const tick = ticks[index]; + return tick.$context || + (tick.$context = createTickContext(this.getContext(), index, tick)); + } + return this.$context || + (this.$context = createScaleContext(this.chart.getContext(), this)); + } + _tickSize() { + const optionTicks = this.options.ticks; + const rot = toRadians(this.labelRotation); + const cos = Math.abs(Math.cos(rot)); + const sin = Math.abs(Math.sin(rot)); + const labelSizes = this._getLabelSizes(); + const padding = optionTicks.autoSkipPadding || 0; + const w = labelSizes ? labelSizes.widest.width + padding : 0; + const h = labelSizes ? labelSizes.highest.height + padding : 0; + return this.isHorizontal() + ? h * cos > w * sin ? w / cos : h / sin + : h * sin < w * cos ? h / cos : w / sin; + } + _isVisible() { + const display = this.options.display; + if (display !== 'auto') { + return !!display; + } + return this.getMatchingVisibleMetas().length > 0; + } + _computeGridLineItems(chartArea) { + const axis = this.axis; + const chart = this.chart; + const options = this.options; + const {grid, position} = options; + const offset = grid.offset; + const isHorizontal = this.isHorizontal(); + const ticks = this.ticks; + const ticksLength = ticks.length + (offset ? 1 : 0); + const tl = getTickMarkLength(grid); + const items = []; + const borderOpts = grid.setContext(this.getContext()); + const axisWidth = borderOpts.drawBorder ? borderOpts.borderWidth : 0; + const axisHalfWidth = axisWidth / 2; + const alignBorderValue = function(pixel) { + return _alignPixel(chart, pixel, axisWidth); + }; + let borderValue, i, lineValue, alignedLineValue; + let tx1, ty1, tx2, ty2, x1, y1, x2, y2; + if (position === 'top') { + borderValue = alignBorderValue(this.bottom); + ty1 = this.bottom - tl; + ty2 = borderValue - axisHalfWidth; + y1 = alignBorderValue(chartArea.top) + axisHalfWidth; + y2 = chartArea.bottom; + } else if (position === 'bottom') { + borderValue = alignBorderValue(this.top); + y1 = chartArea.top; + y2 = alignBorderValue(chartArea.bottom) - axisHalfWidth; + ty1 = borderValue + axisHalfWidth; + ty2 = this.top + tl; + } else if (position === 'left') { + borderValue = alignBorderValue(this.right); + tx1 = this.right - tl; + tx2 = borderValue - axisHalfWidth; + x1 = alignBorderValue(chartArea.left) + axisHalfWidth; + x2 = chartArea.right; + } else if (position === 'right') { + borderValue = alignBorderValue(this.left); + x1 = chartArea.left; + x2 = alignBorderValue(chartArea.right) - axisHalfWidth; + tx1 = borderValue + axisHalfWidth; + tx2 = this.left + tl; + } else if (axis === 'x') { + if (position === 'center') { + borderValue = alignBorderValue((chartArea.top + chartArea.bottom) / 2 + 0.5); + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); + } + y1 = chartArea.top; + y2 = chartArea.bottom; + ty1 = borderValue + axisHalfWidth; + ty2 = ty1 + tl; + } else if (axis === 'y') { + if (position === 'center') { + borderValue = alignBorderValue((chartArea.left + chartArea.right) / 2); + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + borderValue = alignBorderValue(this.chart.scales[positionAxisID].getPixelForValue(value)); + } + tx1 = borderValue - axisHalfWidth; + tx2 = tx1 - tl; + x1 = chartArea.left; + x2 = chartArea.right; + } + const limit = valueOrDefault(options.ticks.maxTicksLimit, ticksLength); + const step = Math.max(1, Math.ceil(ticksLength / limit)); + for (i = 0; i < ticksLength; i += step) { + const optsAtIndex = grid.setContext(this.getContext(i)); + const lineWidth = optsAtIndex.lineWidth; + const lineColor = optsAtIndex.color; + const borderDash = optsAtIndex.borderDash || []; + const borderDashOffset = optsAtIndex.borderDashOffset; + const tickWidth = optsAtIndex.tickWidth; + const tickColor = optsAtIndex.tickColor; + const tickBorderDash = optsAtIndex.tickBorderDash || []; + const tickBorderDashOffset = optsAtIndex.tickBorderDashOffset; + lineValue = getPixelForGridLine(this, i, offset); + if (lineValue === undefined) { + continue; + } + alignedLineValue = _alignPixel(chart, lineValue, lineWidth); + if (isHorizontal) { + tx1 = tx2 = x1 = x2 = alignedLineValue; + } else { + ty1 = ty2 = y1 = y2 = alignedLineValue; + } + items.push({ + tx1, + ty1, + tx2, + ty2, + x1, + y1, + x2, + y2, + width: lineWidth, + color: lineColor, + borderDash, + borderDashOffset, + tickWidth, + tickColor, + tickBorderDash, + tickBorderDashOffset, + }); + } + this._ticksLength = ticksLength; + this._borderValue = borderValue; + return items; + } + _computeLabelItems(chartArea) { + const axis = this.axis; + const options = this.options; + const {position, ticks: optionTicks} = options; + const isHorizontal = this.isHorizontal(); + const ticks = this.ticks; + const {align, crossAlign, padding, mirror} = optionTicks; + const tl = getTickMarkLength(options.grid); + const tickAndPadding = tl + padding; + const hTickAndPadding = mirror ? -padding : tickAndPadding; + const rotation = -toRadians(this.labelRotation); + const items = []; + let i, ilen, tick, label, x, y, textAlign, pixel, font, lineHeight, lineCount, textOffset; + let textBaseline = 'middle'; + if (position === 'top') { + y = this.bottom - hTickAndPadding; + textAlign = this._getXAxisLabelAlignment(); + } else if (position === 'bottom') { + y = this.top + hTickAndPadding; + textAlign = this._getXAxisLabelAlignment(); + } else if (position === 'left') { + const ret = this._getYAxisLabelAlignment(tl); + textAlign = ret.textAlign; + x = ret.x; + } else if (position === 'right') { + const ret = this._getYAxisLabelAlignment(tl); + textAlign = ret.textAlign; + x = ret.x; + } else if (axis === 'x') { + if (position === 'center') { + y = ((chartArea.top + chartArea.bottom) / 2) + tickAndPadding; + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + y = this.chart.scales[positionAxisID].getPixelForValue(value) + tickAndPadding; + } + textAlign = this._getXAxisLabelAlignment(); + } else if (axis === 'y') { + if (position === 'center') { + x = ((chartArea.left + chartArea.right) / 2) - tickAndPadding; + } else if (isObject(position)) { + const positionAxisID = Object.keys(position)[0]; + const value = position[positionAxisID]; + x = this.chart.scales[positionAxisID].getPixelForValue(value); + } + textAlign = this._getYAxisLabelAlignment(tl).textAlign; + } + if (axis === 'y') { + if (align === 'start') { + textBaseline = 'top'; + } else if (align === 'end') { + textBaseline = 'bottom'; + } + } + const labelSizes = this._getLabelSizes(); + for (i = 0, ilen = ticks.length; i < ilen; ++i) { + tick = ticks[i]; + label = tick.label; + const optsAtIndex = optionTicks.setContext(this.getContext(i)); + pixel = this.getPixelForTick(i) + optionTicks.labelOffset; + font = this._resolveTickFontOptions(i); + lineHeight = font.lineHeight; + lineCount = isArray(label) ? label.length : 1; + const halfCount = lineCount / 2; + const color = optsAtIndex.color; + const strokeColor = optsAtIndex.textStrokeColor; + const strokeWidth = optsAtIndex.textStrokeWidth; + let tickTextAlign = textAlign; + if (isHorizontal) { + x = pixel; + if (textAlign === 'inner') { + if (i === ilen - 1) { + tickTextAlign = !this.options.reverse ? 'right' : 'left'; + } else if (i === 0) { + tickTextAlign = !this.options.reverse ? 'left' : 'right'; + } else { + tickTextAlign = 'center'; + } + } + if (position === 'top') { + if (crossAlign === 'near' || rotation !== 0) { + textOffset = -lineCount * lineHeight + lineHeight / 2; + } else if (crossAlign === 'center') { + textOffset = -labelSizes.highest.height / 2 - halfCount * lineHeight + lineHeight; + } else { + textOffset = -labelSizes.highest.height + lineHeight / 2; + } + } else { + if (crossAlign === 'near' || rotation !== 0) { + textOffset = lineHeight / 2; + } else if (crossAlign === 'center') { + textOffset = labelSizes.highest.height / 2 - halfCount * lineHeight; + } else { + textOffset = labelSizes.highest.height - lineCount * lineHeight; + } + } + if (mirror) { + textOffset *= -1; + } + } else { + y = pixel; + textOffset = (1 - lineCount) * lineHeight / 2; + } + let backdrop; + if (optsAtIndex.showLabelBackdrop) { + const labelPadding = toPadding(optsAtIndex.backdropPadding); + const height = labelSizes.heights[i]; + const width = labelSizes.widths[i]; + let top = y + textOffset - labelPadding.top; + let left = x - labelPadding.left; + switch (textBaseline) { + case 'middle': + top -= height / 2; + break; + case 'bottom': + top -= height; + break; + } + switch (textAlign) { + case 'center': + left -= width / 2; + break; + case 'right': + left -= width; + break; + } + backdrop = { + left, + top, + width: width + labelPadding.width, + height: height + labelPadding.height, + color: optsAtIndex.backdropColor, + }; + } + items.push({ + rotation, + label, + font, + color, + strokeColor, + strokeWidth, + textOffset, + textAlign: tickTextAlign, + textBaseline, + translation: [x, y], + backdrop, + }); + } + return items; + } + _getXAxisLabelAlignment() { + const {position, ticks} = this.options; + const rotation = -toRadians(this.labelRotation); + if (rotation) { + return position === 'top' ? 'left' : 'right'; + } + let align = 'center'; + if (ticks.align === 'start') { + align = 'left'; + } else if (ticks.align === 'end') { + align = 'right'; + } else if (ticks.align === 'inner') { + align = 'inner'; + } + return align; + } + _getYAxisLabelAlignment(tl) { + const {position, ticks: {crossAlign, mirror, padding}} = this.options; + const labelSizes = this._getLabelSizes(); + const tickAndPadding = tl + padding; + const widest = labelSizes.widest.width; + let textAlign; + let x; + if (position === 'left') { + if (mirror) { + x = this.right + padding; + if (crossAlign === 'near') { + textAlign = 'left'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x += (widest / 2); + } else { + textAlign = 'right'; + x += widest; + } + } else { + x = this.right - tickAndPadding; + if (crossAlign === 'near') { + textAlign = 'right'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x -= (widest / 2); + } else { + textAlign = 'left'; + x = this.left; + } + } + } else if (position === 'right') { + if (mirror) { + x = this.left + padding; + if (crossAlign === 'near') { + textAlign = 'right'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x -= (widest / 2); + } else { + textAlign = 'left'; + x -= widest; + } + } else { + x = this.left + tickAndPadding; + if (crossAlign === 'near') { + textAlign = 'left'; + } else if (crossAlign === 'center') { + textAlign = 'center'; + x += widest / 2; + } else { + textAlign = 'right'; + x = this.right; + } + } + } else { + textAlign = 'right'; + } + return {textAlign, x}; + } + _computeLabelArea() { + if (this.options.ticks.mirror) { + return; + } + const chart = this.chart; + const position = this.options.position; + if (position === 'left' || position === 'right') { + return {top: 0, left: this.left, bottom: chart.height, right: this.right}; + } if (position === 'top' || position === 'bottom') { + return {top: this.top, left: 0, bottom: this.bottom, right: chart.width}; + } + } + drawBackground() { + const {ctx, options: {backgroundColor}, left, top, width, height} = this; + if (backgroundColor) { + ctx.save(); + ctx.fillStyle = backgroundColor; + ctx.fillRect(left, top, width, height); + ctx.restore(); + } + } + getLineWidthForValue(value) { + const grid = this.options.grid; + if (!this._isVisible() || !grid.display) { + return 0; + } + const ticks = this.ticks; + const index = ticks.findIndex(t => t.value === value); + if (index >= 0) { + const opts = grid.setContext(this.getContext(index)); + return opts.lineWidth; + } + return 0; + } + drawGrid(chartArea) { + const grid = this.options.grid; + const ctx = this.ctx; + const items = this._gridLineItems || (this._gridLineItems = this._computeGridLineItems(chartArea)); + let i, ilen; + const drawLine = (p1, p2, style) => { + if (!style.width || !style.color) { + return; + } + ctx.save(); + ctx.lineWidth = style.width; + ctx.strokeStyle = style.color; + ctx.setLineDash(style.borderDash || []); + ctx.lineDashOffset = style.borderDashOffset; + ctx.beginPath(); + ctx.moveTo(p1.x, p1.y); + ctx.lineTo(p2.x, p2.y); + ctx.stroke(); + ctx.restore(); + }; + if (grid.display) { + for (i = 0, ilen = items.length; i < ilen; ++i) { + const item = items[i]; + if (grid.drawOnChartArea) { + drawLine( + {x: item.x1, y: item.y1}, + {x: item.x2, y: item.y2}, + item + ); + } + if (grid.drawTicks) { + drawLine( + {x: item.tx1, y: item.ty1}, + {x: item.tx2, y: item.ty2}, + { + color: item.tickColor, + width: item.tickWidth, + borderDash: item.tickBorderDash, + borderDashOffset: item.tickBorderDashOffset + } + ); + } + } + } + } + drawBorder() { + const {chart, ctx, options: {grid}} = this; + const borderOpts = grid.setContext(this.getContext()); + const axisWidth = grid.drawBorder ? borderOpts.borderWidth : 0; + if (!axisWidth) { + return; + } + const lastLineWidth = grid.setContext(this.getContext(0)).lineWidth; + const borderValue = this._borderValue; + let x1, x2, y1, y2; + if (this.isHorizontal()) { + x1 = _alignPixel(chart, this.left, axisWidth) - axisWidth / 2; + x2 = _alignPixel(chart, this.right, lastLineWidth) + lastLineWidth / 2; + y1 = y2 = borderValue; + } else { + y1 = _alignPixel(chart, this.top, axisWidth) - axisWidth / 2; + y2 = _alignPixel(chart, this.bottom, lastLineWidth) + lastLineWidth / 2; + x1 = x2 = borderValue; + } + ctx.save(); + ctx.lineWidth = borderOpts.borderWidth; + ctx.strokeStyle = borderOpts.borderColor; + ctx.beginPath(); + ctx.moveTo(x1, y1); + ctx.lineTo(x2, y2); + ctx.stroke(); + ctx.restore(); + } + drawLabels(chartArea) { + const optionTicks = this.options.ticks; + if (!optionTicks.display) { + return; + } + const ctx = this.ctx; + const area = this._computeLabelArea(); + if (area) { + clipArea(ctx, area); + } + const items = this._labelItems || (this._labelItems = this._computeLabelItems(chartArea)); + let i, ilen; + for (i = 0, ilen = items.length; i < ilen; ++i) { + const item = items[i]; + const tickFont = item.font; + const label = item.label; + if (item.backdrop) { + ctx.fillStyle = item.backdrop.color; + ctx.fillRect(item.backdrop.left, item.backdrop.top, item.backdrop.width, item.backdrop.height); + } + let y = item.textOffset; + renderText(ctx, label, 0, y, tickFont, item); + } + if (area) { + unclipArea(ctx); + } + } + drawTitle() { + const {ctx, options: {position, title, reverse}} = this; + if (!title.display) { + return; + } + const font = toFont(title.font); + const padding = toPadding(title.padding); + const align = title.align; + let offset = font.lineHeight / 2; + if (position === 'bottom' || position === 'center' || isObject(position)) { + offset += padding.bottom; + if (isArray(title.text)) { + offset += font.lineHeight * (title.text.length - 1); + } + } else { + offset += padding.top; + } + const {titleX, titleY, maxWidth, rotation} = titleArgs(this, offset, position, align); + renderText(ctx, title.text, 0, 0, font, { + color: title.color, + maxWidth, + rotation, + textAlign: titleAlign(align, position, reverse), + textBaseline: 'middle', + translation: [titleX, titleY], + }); + } + draw(chartArea) { + if (!this._isVisible()) { + return; + } + this.drawBackground(); + this.drawGrid(chartArea); + this.drawBorder(); + this.drawTitle(); + this.drawLabels(chartArea); + } + _layers() { + const opts = this.options; + const tz = opts.ticks && opts.ticks.z || 0; + const gz = valueOrDefault(opts.grid && opts.grid.z, -1); + if (!this._isVisible() || this.draw !== Scale.prototype.draw) { + return [{ + z: tz, + draw: (chartArea) => { + this.draw(chartArea); + } + }]; + } + return [{ + z: gz, + draw: (chartArea) => { + this.drawBackground(); + this.drawGrid(chartArea); + this.drawTitle(); + } + }, { + z: gz + 1, + draw: () => { + this.drawBorder(); + } + }, { + z: tz, + draw: (chartArea) => { + this.drawLabels(chartArea); + } + }]; + } + getMatchingVisibleMetas(type) { + const metas = this.chart.getSortedVisibleDatasetMetas(); + const axisID = this.axis + 'AxisID'; + const result = []; + let i, ilen; + for (i = 0, ilen = metas.length; i < ilen; ++i) { + const meta = metas[i]; + if (meta[axisID] === this.id && (!type || meta.type === type)) { + result.push(meta); + } + } + return result; + } + _resolveTickFontOptions(index) { + const opts = this.options.ticks.setContext(this.getContext(index)); + return toFont(opts.font); + } + _maxDigits() { + const fontSize = this._resolveTickFontOptions(0).lineHeight; + return (this.isHorizontal() ? this.width : this.height) / fontSize; + } +} + +class TypedRegistry { + constructor(type, scope, override) { + this.type = type; + this.scope = scope; + this.override = override; + this.items = Object.create(null); + } + isForType(type) { + return Object.prototype.isPrototypeOf.call(this.type.prototype, type.prototype); + } + register(item) { + const proto = Object.getPrototypeOf(item); + let parentScope; + if (isIChartComponent(proto)) { + parentScope = this.register(proto); + } + const items = this.items; + const id = item.id; + const scope = this.scope + '.' + id; + if (!id) { + throw new Error('class does not have id: ' + item); + } + if (id in items) { + return scope; + } + items[id] = item; + registerDefaults(item, scope, parentScope); + if (this.override) { + defaults.override(item.id, item.overrides); + } + return scope; + } + get(id) { + return this.items[id]; + } + unregister(item) { + const items = this.items; + const id = item.id; + const scope = this.scope; + if (id in items) { + delete items[id]; + } + if (scope && id in defaults[scope]) { + delete defaults[scope][id]; + if (this.override) { + delete overrides[id]; + } + } + } +} +function registerDefaults(item, scope, parentScope) { + const itemDefaults = merge(Object.create(null), [ + parentScope ? defaults.get(parentScope) : {}, + defaults.get(scope), + item.defaults + ]); + defaults.set(scope, itemDefaults); + if (item.defaultRoutes) { + routeDefaults(scope, item.defaultRoutes); + } + if (item.descriptors) { + defaults.describe(scope, item.descriptors); + } +} +function routeDefaults(scope, routes) { + Object.keys(routes).forEach(property => { + const propertyParts = property.split('.'); + const sourceName = propertyParts.pop(); + const sourceScope = [scope].concat(propertyParts).join('.'); + const parts = routes[property].split('.'); + const targetName = parts.pop(); + const targetScope = parts.join('.'); + defaults.route(sourceScope, sourceName, targetScope, targetName); + }); +} +function isIChartComponent(proto) { + return 'id' in proto && 'defaults' in proto; +} + +class Registry { + constructor() { + this.controllers = new TypedRegistry(DatasetController, 'datasets', true); + this.elements = new TypedRegistry(Element, 'elements'); + this.plugins = new TypedRegistry(Object, 'plugins'); + this.scales = new TypedRegistry(Scale, 'scales'); + this._typedRegistries = [this.controllers, this.scales, this.elements]; + } + add(...args) { + this._each('register', args); + } + remove(...args) { + this._each('unregister', args); + } + addControllers(...args) { + this._each('register', args, this.controllers); + } + addElements(...args) { + this._each('register', args, this.elements); + } + addPlugins(...args) { + this._each('register', args, this.plugins); + } + addScales(...args) { + this._each('register', args, this.scales); + } + getController(id) { + return this._get(id, this.controllers, 'controller'); + } + getElement(id) { + return this._get(id, this.elements, 'element'); + } + getPlugin(id) { + return this._get(id, this.plugins, 'plugin'); + } + getScale(id) { + return this._get(id, this.scales, 'scale'); + } + removeControllers(...args) { + this._each('unregister', args, this.controllers); + } + removeElements(...args) { + this._each('unregister', args, this.elements); + } + removePlugins(...args) { + this._each('unregister', args, this.plugins); + } + removeScales(...args) { + this._each('unregister', args, this.scales); + } + _each(method, args, typedRegistry) { + [...args].forEach(arg => { + const reg = typedRegistry || this._getRegistryForType(arg); + if (typedRegistry || reg.isForType(arg) || (reg === this.plugins && arg.id)) { + this._exec(method, reg, arg); + } else { + each(arg, item => { + const itemReg = typedRegistry || this._getRegistryForType(item); + this._exec(method, itemReg, item); + }); + } + }); + } + _exec(method, registry, component) { + const camelMethod = _capitalize(method); + callback(component['before' + camelMethod], [], component); + registry[method](component); + callback(component['after' + camelMethod], [], component); + } + _getRegistryForType(type) { + for (let i = 0; i < this._typedRegistries.length; i++) { + const reg = this._typedRegistries[i]; + if (reg.isForType(type)) { + return reg; + } + } + return this.plugins; + } + _get(id, typedRegistry, type) { + const item = typedRegistry.get(id); + if (item === undefined) { + throw new Error('"' + id + '" is not a registered ' + type + '.'); + } + return item; + } +} +var registry = new Registry(); + +class ScatterController extends DatasetController { + update(mode) { + const meta = this._cachedMeta; + const {data: points = []} = meta; + const animationsDisabled = this.chart._animationsDisabled; + let {start, count} = _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled); + this._drawStart = start; + this._drawCount = count; + if (_scaleRangesChanged(meta)) { + start = 0; + count = points.length; + } + if (this.options.showLine) { + const {dataset: line, _dataset} = meta; + line._chart = this.chart; + line._datasetIndex = this.index; + line._decimated = !!_dataset._decimated; + line.points = points; + const options = this.resolveDatasetElementOptions(mode); + options.segment = this.options.segment; + this.updateElement(line, undefined, { + animated: !animationsDisabled, + options + }, mode); + } + this.updateElements(points, start, count, mode); + } + addElements() { + const {showLine} = this.options; + if (!this.datasetElementType && showLine) { + this.datasetElementType = registry.getElement('line'); + } + super.addElements(); + } + updateElements(points, start, count, mode) { + const reset = mode === 'reset'; + const {iScale, vScale, _stacked, _dataset} = this._cachedMeta; + const firstOpts = this.resolveDataElementOptions(start, mode); + const sharedOptions = this.getSharedOptions(firstOpts); + const includeOptions = this.includeOptions(mode, sharedOptions); + const iAxis = iScale.axis; + const vAxis = vScale.axis; + const {spanGaps, segment} = this.options; + const maxGapLength = isNumber(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY; + const directUpdate = this.chart._animationsDisabled || reset || mode === 'none'; + let prevParsed = start > 0 && this.getParsed(start - 1); + for (let i = start; i < start + count; ++i) { + const point = points[i]; + const parsed = this.getParsed(i); + const properties = directUpdate ? point : {}; + const nullData = isNullOrUndef(parsed[vAxis]); + const iPixel = properties[iAxis] = iScale.getPixelForValue(parsed[iAxis], i); + const vPixel = properties[vAxis] = reset || nullData ? vScale.getBasePixel() : vScale.getPixelForValue(_stacked ? this.applyStack(vScale, parsed, _stacked) : parsed[vAxis], i); + properties.skip = isNaN(iPixel) || isNaN(vPixel) || nullData; + properties.stop = i > 0 && (Math.abs(parsed[iAxis] - prevParsed[iAxis])) > maxGapLength; + if (segment) { + properties.parsed = parsed; + properties.raw = _dataset.data[i]; + } + if (includeOptions) { + properties.options = sharedOptions || this.resolveDataElementOptions(i, point.active ? 'active' : mode); + } + if (!directUpdate) { + this.updateElement(point, i, properties, mode); + } + prevParsed = parsed; + } + this.updateSharedOptions(sharedOptions, mode, firstOpts); + } + getMaxOverflow() { + const meta = this._cachedMeta; + const data = meta.data || []; + if (!this.options.showLine) { + let max = 0; + for (let i = data.length - 1; i >= 0; --i) { + max = Math.max(max, data[i].size(this.resolveDataElementOptions(i)) / 2); + } + return max > 0 && max; + } + const dataset = meta.dataset; + const border = dataset.options && dataset.options.borderWidth || 0; + if (!data.length) { + return border; + } + const firstPoint = data[0].size(this.resolveDataElementOptions(0)); + const lastPoint = data[data.length - 1].size(this.resolveDataElementOptions(data.length - 1)); + return Math.max(border, firstPoint, lastPoint) / 2; + } +} +ScatterController.id = 'scatter'; +ScatterController.defaults = { + datasetElementType: false, + dataElementType: 'point', + showLine: false, + fill: false +}; +ScatterController.overrides = { + interaction: { + mode: 'point' + }, + plugins: { + tooltip: { + callbacks: { + title() { + return ''; + }, + label(item) { + return '(' + item.label + ', ' + item.formattedValue + ')'; + } + } + } + }, + scales: { + x: { + type: 'linear' + }, + y: { + type: 'linear' + } + } +}; + +var controllers = /*#__PURE__*/Object.freeze({ +__proto__: null, +BarController: BarController, +BubbleController: BubbleController, +DoughnutController: DoughnutController, +LineController: LineController, +PolarAreaController: PolarAreaController, +PieController: PieController, +RadarController: RadarController, +ScatterController: ScatterController +}); + +function abstract() { + throw new Error('This method is not implemented: Check that a complete date adapter is provided.'); +} +class DateAdapter { + constructor(options) { + this.options = options || {}; + } + init(chartOptions) {} + formats() { + return abstract(); + } + parse(value, format) { + return abstract(); + } + format(timestamp, format) { + return abstract(); + } + add(timestamp, amount, unit) { + return abstract(); + } + diff(a, b, unit) { + return abstract(); + } + startOf(timestamp, unit, weekday) { + return abstract(); + } + endOf(timestamp, unit) { + return abstract(); + } +} +DateAdapter.override = function(members) { + Object.assign(DateAdapter.prototype, members); +}; +var adapters = { + _date: DateAdapter +}; + +function binarySearch(metaset, axis, value, intersect) { + const {controller, data, _sorted} = metaset; + const iScale = controller._cachedMeta.iScale; + if (iScale && axis === iScale.axis && axis !== 'r' && _sorted && data.length) { + const lookupMethod = iScale._reversePixels ? _rlookupByKey : _lookupByKey; + if (!intersect) { + return lookupMethod(data, axis, value); + } else if (controller._sharedOptions) { + const el = data[0]; + const range = typeof el.getRange === 'function' && el.getRange(axis); + if (range) { + const start = lookupMethod(data, axis, value - range); + const end = lookupMethod(data, axis, value + range); + return {lo: start.lo, hi: end.hi}; + } + } + } + return {lo: 0, hi: data.length - 1}; +} +function evaluateInteractionItems(chart, axis, position, handler, intersect) { + const metasets = chart.getSortedVisibleDatasetMetas(); + const value = position[axis]; + for (let i = 0, ilen = metasets.length; i < ilen; ++i) { + const {index, data} = metasets[i]; + const {lo, hi} = binarySearch(metasets[i], axis, value, intersect); + for (let j = lo; j <= hi; ++j) { + const element = data[j]; + if (!element.skip) { + handler(element, index, j); + } + } + } +} +function getDistanceMetricForAxis(axis) { + const useX = axis.indexOf('x') !== -1; + const useY = axis.indexOf('y') !== -1; + return function(pt1, pt2) { + const deltaX = useX ? Math.abs(pt1.x - pt2.x) : 0; + const deltaY = useY ? Math.abs(pt1.y - pt2.y) : 0; + return Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)); + }; +} +function getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) { + const items = []; + if (!includeInvisible && !chart.isPointInArea(position)) { + return items; + } + const evaluationFunc = function(element, datasetIndex, index) { + if (!includeInvisible && !_isPointInArea(element, chart.chartArea, 0)) { + return; + } + if (element.inRange(position.x, position.y, useFinalPosition)) { + items.push({element, datasetIndex, index}); + } + }; + evaluateInteractionItems(chart, axis, position, evaluationFunc, true); + return items; +} +function getNearestRadialItems(chart, position, axis, useFinalPosition) { + let items = []; + function evaluationFunc(element, datasetIndex, index) { + const {startAngle, endAngle} = element.getProps(['startAngle', 'endAngle'], useFinalPosition); + const {angle} = getAngleFromPoint(element, {x: position.x, y: position.y}); + if (_angleBetween(angle, startAngle, endAngle)) { + items.push({element, datasetIndex, index}); + } + } + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + let items = []; + const distanceMetric = getDistanceMetricForAxis(axis); + let minDistance = Number.POSITIVE_INFINITY; + function evaluationFunc(element, datasetIndex, index) { + const inRange = element.inRange(position.x, position.y, useFinalPosition); + if (intersect && !inRange) { + return; + } + const center = element.getCenterPoint(useFinalPosition); + const pointInArea = !!includeInvisible || chart.isPointInArea(center); + if (!pointInArea && !inRange) { + return; + } + const distance = distanceMetric(position, center); + if (distance < minDistance) { + items = [{element, datasetIndex, index}]; + minDistance = distance; + } else if (distance === minDistance) { + items.push({element, datasetIndex, index}); + } + } + evaluateInteractionItems(chart, axis, position, evaluationFunc); + return items; +} +function getNearestItems(chart, position, axis, intersect, useFinalPosition, includeInvisible) { + if (!includeInvisible && !chart.isPointInArea(position)) { + return []; + } + return axis === 'r' && !intersect + ? getNearestRadialItems(chart, position, axis, useFinalPosition) + : getNearestCartesianItems(chart, position, axis, intersect, useFinalPosition, includeInvisible); +} +function getAxisItems(chart, position, axis, intersect, useFinalPosition) { + const items = []; + const rangeMethod = axis === 'x' ? 'inXRange' : 'inYRange'; + let intersectsItem = false; + evaluateInteractionItems(chart, axis, position, (element, datasetIndex, index) => { + if (element[rangeMethod](position[axis], useFinalPosition)) { + items.push({element, datasetIndex, index}); + intersectsItem = intersectsItem || element.inRange(position.x, position.y, useFinalPosition); + } + }); + if (intersect && !intersectsItem) { + return []; + } + return items; +} +var Interaction = { + evaluateInteractionItems, + modes: { + index(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'x'; + const includeInvisible = options.includeInvisible || false; + const items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) + : getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + const elements = []; + if (!items.length) { + return []; + } + chart.getSortedVisibleDatasetMetas().forEach((meta) => { + const index = items[0].index; + const element = meta.data[index]; + if (element && !element.skip) { + elements.push({element, datasetIndex: meta.index, index}); + } + }); + return elements; + }, + dataset(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + let items = options.intersect + ? getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible) : + getNearestItems(chart, position, axis, false, useFinalPosition, includeInvisible); + if (items.length > 0) { + const datasetIndex = items[0].datasetIndex; + const data = chart.getDatasetMeta(datasetIndex).data; + items = []; + for (let i = 0; i < data.length; ++i) { + items.push({element: data[i], datasetIndex, index: i}); + } + } + return items; + }, + point(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getIntersectItems(chart, position, axis, useFinalPosition, includeInvisible); + }, + nearest(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + const axis = options.axis || 'xy'; + const includeInvisible = options.includeInvisible || false; + return getNearestItems(chart, position, axis, options.intersect, useFinalPosition, includeInvisible); + }, + x(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'x', options.intersect, useFinalPosition); + }, + y(chart, e, options, useFinalPosition) { + const position = getRelativePosition(e, chart); + return getAxisItems(chart, position, 'y', options.intersect, useFinalPosition); + } + } +}; + +const STATIC_POSITIONS = ['left', 'top', 'right', 'bottom']; +function filterByPosition(array, position) { + return array.filter(v => v.pos === position); +} +function filterDynamicPositionByAxis(array, axis) { + return array.filter(v => STATIC_POSITIONS.indexOf(v.pos) === -1 && v.box.axis === axis); +} +function sortByWeight(array, reverse) { + return array.sort((a, b) => { + const v0 = reverse ? b : a; + const v1 = reverse ? a : b; + return v0.weight === v1.weight ? + v0.index - v1.index : + v0.weight - v1.weight; + }); +} +function wrapBoxes(boxes) { + const layoutBoxes = []; + let i, ilen, box, pos, stack, stackWeight; + for (i = 0, ilen = (boxes || []).length; i < ilen; ++i) { + box = boxes[i]; + ({position: pos, options: {stack, stackWeight = 1}} = box); + layoutBoxes.push({ + index: i, + box, + pos, + horizontal: box.isHorizontal(), + weight: box.weight, + stack: stack && (pos + stack), + stackWeight + }); + } + return layoutBoxes; +} +function buildStacks(layouts) { + const stacks = {}; + for (const wrap of layouts) { + const {stack, pos, stackWeight} = wrap; + if (!stack || !STATIC_POSITIONS.includes(pos)) { + continue; + } + const _stack = stacks[stack] || (stacks[stack] = {count: 0, placed: 0, weight: 0, size: 0}); + _stack.count++; + _stack.weight += stackWeight; + } + return stacks; +} +function setLayoutDims(layouts, params) { + const stacks = buildStacks(layouts); + const {vBoxMaxWidth, hBoxMaxHeight} = params; + let i, ilen, layout; + for (i = 0, ilen = layouts.length; i < ilen; ++i) { + layout = layouts[i]; + const {fullSize} = layout.box; + const stack = stacks[layout.stack]; + const factor = stack && layout.stackWeight / stack.weight; + if (layout.horizontal) { + layout.width = factor ? factor * vBoxMaxWidth : fullSize && params.availableWidth; + layout.height = hBoxMaxHeight; + } else { + layout.width = vBoxMaxWidth; + layout.height = factor ? factor * hBoxMaxHeight : fullSize && params.availableHeight; + } + } + return stacks; +} +function buildLayoutBoxes(boxes) { + const layoutBoxes = wrapBoxes(boxes); + const fullSize = sortByWeight(layoutBoxes.filter(wrap => wrap.box.fullSize), true); + const left = sortByWeight(filterByPosition(layoutBoxes, 'left'), true); + const right = sortByWeight(filterByPosition(layoutBoxes, 'right')); + const top = sortByWeight(filterByPosition(layoutBoxes, 'top'), true); + const bottom = sortByWeight(filterByPosition(layoutBoxes, 'bottom')); + const centerHorizontal = filterDynamicPositionByAxis(layoutBoxes, 'x'); + const centerVertical = filterDynamicPositionByAxis(layoutBoxes, 'y'); + return { + fullSize, + leftAndTop: left.concat(top), + rightAndBottom: right.concat(centerVertical).concat(bottom).concat(centerHorizontal), + chartArea: filterByPosition(layoutBoxes, 'chartArea'), + vertical: left.concat(right).concat(centerVertical), + horizontal: top.concat(bottom).concat(centerHorizontal) + }; +} +function getCombinedMax(maxPadding, chartArea, a, b) { + return Math.max(maxPadding[a], chartArea[a]) + Math.max(maxPadding[b], chartArea[b]); +} +function updateMaxPadding(maxPadding, boxPadding) { + maxPadding.top = Math.max(maxPadding.top, boxPadding.top); + maxPadding.left = Math.max(maxPadding.left, boxPadding.left); + maxPadding.bottom = Math.max(maxPadding.bottom, boxPadding.bottom); + maxPadding.right = Math.max(maxPadding.right, boxPadding.right); +} +function updateDims(chartArea, params, layout, stacks) { + const {pos, box} = layout; + const maxPadding = chartArea.maxPadding; + if (!isObject(pos)) { + if (layout.size) { + chartArea[pos] -= layout.size; + } + const stack = stacks[layout.stack] || {size: 0, count: 1}; + stack.size = Math.max(stack.size, layout.horizontal ? box.height : box.width); + layout.size = stack.size / stack.count; + chartArea[pos] += layout.size; + } + if (box.getPadding) { + updateMaxPadding(maxPadding, box.getPadding()); + } + const newWidth = Math.max(0, params.outerWidth - getCombinedMax(maxPadding, chartArea, 'left', 'right')); + const newHeight = Math.max(0, params.outerHeight - getCombinedMax(maxPadding, chartArea, 'top', 'bottom')); + const widthChanged = newWidth !== chartArea.w; + const heightChanged = newHeight !== chartArea.h; + chartArea.w = newWidth; + chartArea.h = newHeight; + return layout.horizontal + ? {same: widthChanged, other: heightChanged} + : {same: heightChanged, other: widthChanged}; +} +function handleMaxPadding(chartArea) { + const maxPadding = chartArea.maxPadding; + function updatePos(pos) { + const change = Math.max(maxPadding[pos] - chartArea[pos], 0); + chartArea[pos] += change; + return change; + } + chartArea.y += updatePos('top'); + chartArea.x += updatePos('left'); + updatePos('right'); + updatePos('bottom'); +} +function getMargins(horizontal, chartArea) { + const maxPadding = chartArea.maxPadding; + function marginForPositions(positions) { + const margin = {left: 0, top: 0, right: 0, bottom: 0}; + positions.forEach((pos) => { + margin[pos] = Math.max(chartArea[pos], maxPadding[pos]); + }); + return margin; + } + return horizontal + ? marginForPositions(['left', 'right']) + : marginForPositions(['top', 'bottom']); +} +function fitBoxes(boxes, chartArea, params, stacks) { + const refitBoxes = []; + let i, ilen, layout, box, refit, changed; + for (i = 0, ilen = boxes.length, refit = 0; i < ilen; ++i) { + layout = boxes[i]; + box = layout.box; + box.update( + layout.width || chartArea.w, + layout.height || chartArea.h, + getMargins(layout.horizontal, chartArea) + ); + const {same, other} = updateDims(chartArea, params, layout, stacks); + refit |= same && refitBoxes.length; + changed = changed || other; + if (!box.fullSize) { + refitBoxes.push(layout); + } + } + return refit && fitBoxes(refitBoxes, chartArea, params, stacks) || changed; +} +function setBoxDims(box, left, top, width, height) { + box.top = top; + box.left = left; + box.right = left + width; + box.bottom = top + height; + box.width = width; + box.height = height; +} +function placeBoxes(boxes, chartArea, params, stacks) { + const userPadding = params.padding; + let {x, y} = chartArea; + for (const layout of boxes) { + const box = layout.box; + const stack = stacks[layout.stack] || {count: 1, placed: 0, weight: 1}; + const weight = (layout.stackWeight / stack.weight) || 1; + if (layout.horizontal) { + const width = chartArea.w * weight; + const height = stack.size || box.height; + if (defined(stack.start)) { + y = stack.start; + } + if (box.fullSize) { + setBoxDims(box, userPadding.left, y, params.outerWidth - userPadding.right - userPadding.left, height); + } else { + setBoxDims(box, chartArea.left + stack.placed, y, width, height); + } + stack.start = y; + stack.placed += width; + y = box.bottom; + } else { + const height = chartArea.h * weight; + const width = stack.size || box.width; + if (defined(stack.start)) { + x = stack.start; + } + if (box.fullSize) { + setBoxDims(box, x, userPadding.top, width, params.outerHeight - userPadding.bottom - userPadding.top); + } else { + setBoxDims(box, x, chartArea.top + stack.placed, width, height); + } + stack.start = x; + stack.placed += height; + x = box.right; + } + } + chartArea.x = x; + chartArea.y = y; +} +defaults.set('layout', { + autoPadding: true, + padding: { + top: 0, + right: 0, + bottom: 0, + left: 0 + } +}); +var layouts = { + addBox(chart, item) { + if (!chart.boxes) { + chart.boxes = []; + } + item.fullSize = item.fullSize || false; + item.position = item.position || 'top'; + item.weight = item.weight || 0; + item._layers = item._layers || function() { + return [{ + z: 0, + draw(chartArea) { + item.draw(chartArea); + } + }]; + }; + chart.boxes.push(item); + }, + removeBox(chart, layoutItem) { + const index = chart.boxes ? chart.boxes.indexOf(layoutItem) : -1; + if (index !== -1) { + chart.boxes.splice(index, 1); + } + }, + configure(chart, item, options) { + item.fullSize = options.fullSize; + item.position = options.position; + item.weight = options.weight; + }, + update(chart, width, height, minPadding) { + if (!chart) { + return; + } + const padding = toPadding(chart.options.layout.padding); + const availableWidth = Math.max(width - padding.width, 0); + const availableHeight = Math.max(height - padding.height, 0); + const boxes = buildLayoutBoxes(chart.boxes); + const verticalBoxes = boxes.vertical; + const horizontalBoxes = boxes.horizontal; + each(chart.boxes, box => { + if (typeof box.beforeLayout === 'function') { + box.beforeLayout(); + } + }); + const visibleVerticalBoxCount = verticalBoxes.reduce((total, wrap) => + wrap.box.options && wrap.box.options.display === false ? total : total + 1, 0) || 1; + const params = Object.freeze({ + outerWidth: width, + outerHeight: height, + padding, + availableWidth, + availableHeight, + vBoxMaxWidth: availableWidth / 2 / visibleVerticalBoxCount, + hBoxMaxHeight: availableHeight / 2 + }); + const maxPadding = Object.assign({}, padding); + updateMaxPadding(maxPadding, toPadding(minPadding)); + const chartArea = Object.assign({ + maxPadding, + w: availableWidth, + h: availableHeight, + x: padding.left, + y: padding.top + }, padding); + const stacks = setLayoutDims(verticalBoxes.concat(horizontalBoxes), params); + fitBoxes(boxes.fullSize, chartArea, params, stacks); + fitBoxes(verticalBoxes, chartArea, params, stacks); + if (fitBoxes(horizontalBoxes, chartArea, params, stacks)) { + fitBoxes(verticalBoxes, chartArea, params, stacks); + } + handleMaxPadding(chartArea); + placeBoxes(boxes.leftAndTop, chartArea, params, stacks); + chartArea.x += chartArea.w; + chartArea.y += chartArea.h; + placeBoxes(boxes.rightAndBottom, chartArea, params, stacks); + chart.chartArea = { + left: chartArea.left, + top: chartArea.top, + right: chartArea.left + chartArea.w, + bottom: chartArea.top + chartArea.h, + height: chartArea.h, + width: chartArea.w, + }; + each(boxes.chartArea, (layout) => { + const box = layout.box; + Object.assign(box, chart.chartArea); + box.update(chartArea.w, chartArea.h, {left: 0, top: 0, right: 0, bottom: 0}); + }); + } +}; + +class BasePlatform { + acquireContext(canvas, aspectRatio) {} + releaseContext(context) { + return false; + } + addEventListener(chart, type, listener) {} + removeEventListener(chart, type, listener) {} + getDevicePixelRatio() { + return 1; + } + getMaximumSize(element, width, height, aspectRatio) { + width = Math.max(0, width || element.width); + height = height || element.height; + return { + width, + height: Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height) + }; + } + isAttached(canvas) { + return true; + } + updateConfig(config) { + } +} + +class BasicPlatform extends BasePlatform { + acquireContext(item) { + return item && item.getContext && item.getContext('2d') || null; + } + updateConfig(config) { + config.options.animation = false; + } +} + +const EXPANDO_KEY = '$chartjs'; +const EVENT_TYPES = { + touchstart: 'mousedown', + touchmove: 'mousemove', + touchend: 'mouseup', + pointerenter: 'mouseenter', + pointerdown: 'mousedown', + pointermove: 'mousemove', + pointerup: 'mouseup', + pointerleave: 'mouseout', + pointerout: 'mouseout' +}; +const isNullOrEmpty = value => value === null || value === ''; +function initCanvas(canvas, aspectRatio) { + const style = canvas.style; + const renderHeight = canvas.getAttribute('height'); + const renderWidth = canvas.getAttribute('width'); + canvas[EXPANDO_KEY] = { + initial: { + height: renderHeight, + width: renderWidth, + style: { + display: style.display, + height: style.height, + width: style.width + } + } + }; + style.display = style.display || 'block'; + style.boxSizing = style.boxSizing || 'border-box'; + if (isNullOrEmpty(renderWidth)) { + const displayWidth = readUsedSize(canvas, 'width'); + if (displayWidth !== undefined) { + canvas.width = displayWidth; + } + } + if (isNullOrEmpty(renderHeight)) { + if (canvas.style.height === '') { + canvas.height = canvas.width / (aspectRatio || 2); + } else { + const displayHeight = readUsedSize(canvas, 'height'); + if (displayHeight !== undefined) { + canvas.height = displayHeight; + } + } + } + return canvas; +} +const eventListenerOptions = supportsEventListenerOptions ? {passive: true} : false; +function addListener(node, type, listener) { + node.addEventListener(type, listener, eventListenerOptions); +} +function removeListener(chart, type, listener) { + chart.canvas.removeEventListener(type, listener, eventListenerOptions); +} +function fromNativeEvent(event, chart) { + const type = EVENT_TYPES[event.type] || event.type; + const {x, y} = getRelativePosition(event, chart); + return { + type, + chart, + native: event, + x: x !== undefined ? x : null, + y: y !== undefined ? y : null, + }; +} +function nodeListContains(nodeList, canvas) { + for (const node of nodeList) { + if (node === canvas || node.contains(canvas)) { + return true; + } + } +} +function createAttachObserver(chart, type, listener) { + const canvas = chart.canvas; + const observer = new MutationObserver(entries => { + let trigger = false; + for (const entry of entries) { + trigger = trigger || nodeListContains(entry.addedNodes, canvas); + trigger = trigger && !nodeListContains(entry.removedNodes, canvas); + } + if (trigger) { + listener(); + } + }); + observer.observe(document, {childList: true, subtree: true}); + return observer; +} +function createDetachObserver(chart, type, listener) { + const canvas = chart.canvas; + const observer = new MutationObserver(entries => { + let trigger = false; + for (const entry of entries) { + trigger = trigger || nodeListContains(entry.removedNodes, canvas); + trigger = trigger && !nodeListContains(entry.addedNodes, canvas); + } + if (trigger) { + listener(); + } + }); + observer.observe(document, {childList: true, subtree: true}); + return observer; +} +const drpListeningCharts = new Map(); +let oldDevicePixelRatio = 0; +function onWindowResize() { + const dpr = window.devicePixelRatio; + if (dpr === oldDevicePixelRatio) { + return; + } + oldDevicePixelRatio = dpr; + drpListeningCharts.forEach((resize, chart) => { + if (chart.currentDevicePixelRatio !== dpr) { + resize(); + } + }); +} +function listenDevicePixelRatioChanges(chart, resize) { + if (!drpListeningCharts.size) { + window.addEventListener('resize', onWindowResize); + } + drpListeningCharts.set(chart, resize); +} +function unlistenDevicePixelRatioChanges(chart) { + drpListeningCharts.delete(chart); + if (!drpListeningCharts.size) { + window.removeEventListener('resize', onWindowResize); + } +} +function createResizeObserver(chart, type, listener) { + const canvas = chart.canvas; + const container = canvas && _getParentNode(canvas); + if (!container) { + return; + } + const resize = throttled((width, height) => { + const w = container.clientWidth; + listener(width, height); + if (w < container.clientWidth) { + listener(); + } + }, window); + const observer = new ResizeObserver(entries => { + const entry = entries[0]; + const width = entry.contentRect.width; + const height = entry.contentRect.height; + if (width === 0 && height === 0) { + return; + } + resize(width, height); + }); + observer.observe(container); + listenDevicePixelRatioChanges(chart, resize); + return observer; +} +function releaseObserver(chart, type, observer) { + if (observer) { + observer.disconnect(); + } + if (type === 'resize') { + unlistenDevicePixelRatioChanges(chart); + } +} +function createProxyAndListen(chart, type, listener) { + const canvas = chart.canvas; + const proxy = throttled((event) => { + if (chart.ctx !== null) { + listener(fromNativeEvent(event, chart)); + } + }, chart, (args) => { + const event = args[0]; + return [event, event.offsetX, event.offsetY]; + }); + addListener(canvas, type, proxy); + return proxy; +} +class DomPlatform extends BasePlatform { + acquireContext(canvas, aspectRatio) { + const context = canvas && canvas.getContext && canvas.getContext('2d'); + if (context && context.canvas === canvas) { + initCanvas(canvas, aspectRatio); + return context; + } + return null; + } + releaseContext(context) { + const canvas = context.canvas; + if (!canvas[EXPANDO_KEY]) { + return false; + } + const initial = canvas[EXPANDO_KEY].initial; + ['height', 'width'].forEach((prop) => { + const value = initial[prop]; + if (isNullOrUndef(value)) { + canvas.removeAttribute(prop); + } else { + canvas.setAttribute(prop, value); + } + }); + const style = initial.style || {}; + Object.keys(style).forEach((key) => { + canvas.style[key] = style[key]; + }); + canvas.width = canvas.width; + delete canvas[EXPANDO_KEY]; + return true; + } + addEventListener(chart, type, listener) { + this.removeEventListener(chart, type); + const proxies = chart.$proxies || (chart.$proxies = {}); + const handlers = { + attach: createAttachObserver, + detach: createDetachObserver, + resize: createResizeObserver + }; + const handler = handlers[type] || createProxyAndListen; + proxies[type] = handler(chart, type, listener); + } + removeEventListener(chart, type) { + const proxies = chart.$proxies || (chart.$proxies = {}); + const proxy = proxies[type]; + if (!proxy) { + return; + } + const handlers = { + attach: releaseObserver, + detach: releaseObserver, + resize: releaseObserver + }; + const handler = handlers[type] || removeListener; + handler(chart, type, proxy); + proxies[type] = undefined; + } + getDevicePixelRatio() { + return window.devicePixelRatio; + } + getMaximumSize(canvas, width, height, aspectRatio) { + return getMaximumSize(canvas, width, height, aspectRatio); + } + isAttached(canvas) { + const container = _getParentNode(canvas); + return !!(container && container.isConnected); + } +} + +function _detectPlatform(canvas) { + if (!_isDomSupported() || (typeof OffscreenCanvas !== 'undefined' && canvas instanceof OffscreenCanvas)) { + return BasicPlatform; + } + return DomPlatform; +} + +class PluginService { + constructor() { + this._init = []; + } + notify(chart, hook, args, filter) { + if (hook === 'beforeInit') { + this._init = this._createDescriptors(chart, true); + this._notify(this._init, chart, 'install'); + } + const descriptors = filter ? this._descriptors(chart).filter(filter) : this._descriptors(chart); + const result = this._notify(descriptors, chart, hook, args); + if (hook === 'afterDestroy') { + this._notify(descriptors, chart, 'stop'); + this._notify(this._init, chart, 'uninstall'); + } + return result; + } + _notify(descriptors, chart, hook, args) { + args = args || {}; + for (const descriptor of descriptors) { + const plugin = descriptor.plugin; + const method = plugin[hook]; + const params = [chart, args, descriptor.options]; + if (callback(method, params, plugin) === false && args.cancelable) { + return false; + } + } + return true; + } + invalidate() { + if (!isNullOrUndef(this._cache)) { + this._oldCache = this._cache; + this._cache = undefined; + } + } + _descriptors(chart) { + if (this._cache) { + return this._cache; + } + const descriptors = this._cache = this._createDescriptors(chart); + this._notifyStateChanges(chart); + return descriptors; + } + _createDescriptors(chart, all) { + const config = chart && chart.config; + const options = valueOrDefault(config.options && config.options.plugins, {}); + const plugins = allPlugins(config); + return options === false && !all ? [] : createDescriptors(chart, plugins, options, all); + } + _notifyStateChanges(chart) { + const previousDescriptors = this._oldCache || []; + const descriptors = this._cache; + const diff = (a, b) => a.filter(x => !b.some(y => x.plugin.id === y.plugin.id)); + this._notify(diff(previousDescriptors, descriptors), chart, 'stop'); + this._notify(diff(descriptors, previousDescriptors), chart, 'start'); + } +} +function allPlugins(config) { + const localIds = {}; + const plugins = []; + const keys = Object.keys(registry.plugins.items); + for (let i = 0; i < keys.length; i++) { + plugins.push(registry.getPlugin(keys[i])); + } + const local = config.plugins || []; + for (let i = 0; i < local.length; i++) { + const plugin = local[i]; + if (plugins.indexOf(plugin) === -1) { + plugins.push(plugin); + localIds[plugin.id] = true; + } + } + return {plugins, localIds}; +} +function getOpts(options, all) { + if (!all && options === false) { + return null; + } + if (options === true) { + return {}; + } + return options; +} +function createDescriptors(chart, {plugins, localIds}, options, all) { + const result = []; + const context = chart.getContext(); + for (const plugin of plugins) { + const id = plugin.id; + const opts = getOpts(options[id], all); + if (opts === null) { + continue; + } + result.push({ + plugin, + options: pluginOpts(chart.config, {plugin, local: localIds[id]}, opts, context) + }); + } + return result; +} +function pluginOpts(config, {plugin, local}, opts, context) { + const keys = config.pluginScopeKeys(plugin); + const scopes = config.getOptionScopes(opts, keys); + if (local && plugin.defaults) { + scopes.push(plugin.defaults); + } + return config.createResolver(scopes, context, [''], { + scriptable: false, + indexable: false, + allKeys: true + }); +} + +function getIndexAxis(type, options) { + const datasetDefaults = defaults.datasets[type] || {}; + const datasetOptions = (options.datasets || {})[type] || {}; + return datasetOptions.indexAxis || options.indexAxis || datasetDefaults.indexAxis || 'x'; +} +function getAxisFromDefaultScaleID(id, indexAxis) { + let axis = id; + if (id === '_index_') { + axis = indexAxis; + } else if (id === '_value_') { + axis = indexAxis === 'x' ? 'y' : 'x'; + } + return axis; +} +function getDefaultScaleIDFromAxis(axis, indexAxis) { + return axis === indexAxis ? '_index_' : '_value_'; +} +function axisFromPosition(position) { + if (position === 'top' || position === 'bottom') { + return 'x'; + } + if (position === 'left' || position === 'right') { + return 'y'; + } +} +function determineAxis(id, scaleOptions) { + if (id === 'x' || id === 'y') { + return id; + } + return scaleOptions.axis || axisFromPosition(scaleOptions.position) || id.charAt(0).toLowerCase(); +} +function mergeScaleConfig(config, options) { + const chartDefaults = overrides[config.type] || {scales: {}}; + const configScales = options.scales || {}; + const chartIndexAxis = getIndexAxis(config.type, options); + const firstIDs = Object.create(null); + const scales = Object.create(null); + Object.keys(configScales).forEach(id => { + const scaleConf = configScales[id]; + if (!isObject(scaleConf)) { + return console.error(`Invalid scale configuration for scale: ${id}`); + } + if (scaleConf._proxy) { + return console.warn(`Ignoring resolver passed as options for scale: ${id}`); + } + const axis = determineAxis(id, scaleConf); + const defaultId = getDefaultScaleIDFromAxis(axis, chartIndexAxis); + const defaultScaleOptions = chartDefaults.scales || {}; + firstIDs[axis] = firstIDs[axis] || id; + scales[id] = mergeIf(Object.create(null), [{axis}, scaleConf, defaultScaleOptions[axis], defaultScaleOptions[defaultId]]); + }); + config.data.datasets.forEach(dataset => { + const type = dataset.type || config.type; + const indexAxis = dataset.indexAxis || getIndexAxis(type, options); + const datasetDefaults = overrides[type] || {}; + const defaultScaleOptions = datasetDefaults.scales || {}; + Object.keys(defaultScaleOptions).forEach(defaultID => { + const axis = getAxisFromDefaultScaleID(defaultID, indexAxis); + const id = dataset[axis + 'AxisID'] || firstIDs[axis] || axis; + scales[id] = scales[id] || Object.create(null); + mergeIf(scales[id], [{axis}, configScales[id], defaultScaleOptions[defaultID]]); + }); + }); + Object.keys(scales).forEach(key => { + const scale = scales[key]; + mergeIf(scale, [defaults.scales[scale.type], defaults.scale]); + }); + return scales; +} +function initOptions(config) { + const options = config.options || (config.options = {}); + options.plugins = valueOrDefault(options.plugins, {}); + options.scales = mergeScaleConfig(config, options); +} +function initData(data) { + data = data || {}; + data.datasets = data.datasets || []; + data.labels = data.labels || []; + return data; +} +function initConfig(config) { + config = config || {}; + config.data = initData(config.data); + initOptions(config); + return config; +} +const keyCache = new Map(); +const keysCached = new Set(); +function cachedKeys(cacheKey, generate) { + let keys = keyCache.get(cacheKey); + if (!keys) { + keys = generate(); + keyCache.set(cacheKey, keys); + keysCached.add(keys); + } + return keys; +} +const addIfFound = (set, obj, key) => { + const opts = resolveObjectKey(obj, key); + if (opts !== undefined) { + set.add(opts); + } +}; +class Config { + constructor(config) { + this._config = initConfig(config); + this._scopeCache = new Map(); + this._resolverCache = new Map(); + } + get platform() { + return this._config.platform; + } + get type() { + return this._config.type; + } + set type(type) { + this._config.type = type; + } + get data() { + return this._config.data; + } + set data(data) { + this._config.data = initData(data); + } + get options() { + return this._config.options; + } + set options(options) { + this._config.options = options; + } + get plugins() { + return this._config.plugins; + } + update() { + const config = this._config; + this.clearCache(); + initOptions(config); + } + clearCache() { + this._scopeCache.clear(); + this._resolverCache.clear(); + } + datasetScopeKeys(datasetType) { + return cachedKeys(datasetType, + () => [[ + `datasets.${datasetType}`, + '' + ]]); + } + datasetAnimationScopeKeys(datasetType, transition) { + return cachedKeys(`${datasetType}.transition.${transition}`, + () => [ + [ + `datasets.${datasetType}.transitions.${transition}`, + `transitions.${transition}`, + ], + [ + `datasets.${datasetType}`, + '' + ] + ]); + } + datasetElementScopeKeys(datasetType, elementType) { + return cachedKeys(`${datasetType}-${elementType}`, + () => [[ + `datasets.${datasetType}.elements.${elementType}`, + `datasets.${datasetType}`, + `elements.${elementType}`, + '' + ]]); + } + pluginScopeKeys(plugin) { + const id = plugin.id; + const type = this.type; + return cachedKeys(`${type}-plugin-${id}`, + () => [[ + `plugins.${id}`, + ...plugin.additionalOptionScopes || [], + ]]); + } + _cachedScopes(mainScope, resetCache) { + const _scopeCache = this._scopeCache; + let cache = _scopeCache.get(mainScope); + if (!cache || resetCache) { + cache = new Map(); + _scopeCache.set(mainScope, cache); + } + return cache; + } + getOptionScopes(mainScope, keyLists, resetCache) { + const {options, type} = this; + const cache = this._cachedScopes(mainScope, resetCache); + const cached = cache.get(keyLists); + if (cached) { + return cached; + } + const scopes = new Set(); + keyLists.forEach(keys => { + if (mainScope) { + scopes.add(mainScope); + keys.forEach(key => addIfFound(scopes, mainScope, key)); + } + keys.forEach(key => addIfFound(scopes, options, key)); + keys.forEach(key => addIfFound(scopes, overrides[type] || {}, key)); + keys.forEach(key => addIfFound(scopes, defaults, key)); + keys.forEach(key => addIfFound(scopes, descriptors, key)); + }); + const array = Array.from(scopes); + if (array.length === 0) { + array.push(Object.create(null)); + } + if (keysCached.has(keyLists)) { + cache.set(keyLists, array); + } + return array; + } + chartOptionScopes() { + const {options, type} = this; + return [ + options, + overrides[type] || {}, + defaults.datasets[type] || {}, + {type}, + defaults, + descriptors + ]; + } + resolveNamedOptions(scopes, names, context, prefixes = ['']) { + const result = {$shared: true}; + const {resolver, subPrefixes} = getResolver(this._resolverCache, scopes, prefixes); + let options = resolver; + if (needContext(resolver, names)) { + result.$shared = false; + context = isFunction(context) ? context() : context; + const subResolver = this.createResolver(scopes, context, subPrefixes); + options = _attachContext(resolver, context, subResolver); + } + for (const prop of names) { + result[prop] = options[prop]; + } + return result; + } + createResolver(scopes, context, prefixes = [''], descriptorDefaults) { + const {resolver} = getResolver(this._resolverCache, scopes, prefixes); + return isObject(context) + ? _attachContext(resolver, context, undefined, descriptorDefaults) + : resolver; + } +} +function getResolver(resolverCache, scopes, prefixes) { + let cache = resolverCache.get(scopes); + if (!cache) { + cache = new Map(); + resolverCache.set(scopes, cache); + } + const cacheKey = prefixes.join(); + let cached = cache.get(cacheKey); + if (!cached) { + const resolver = _createResolver(scopes, prefixes); + cached = { + resolver, + subPrefixes: prefixes.filter(p => !p.toLowerCase().includes('hover')) + }; + cache.set(cacheKey, cached); + } + return cached; +} +const hasFunction = value => isObject(value) + && Object.getOwnPropertyNames(value).reduce((acc, key) => acc || isFunction(value[key]), false); +function needContext(proxy, names) { + const {isScriptable, isIndexable} = _descriptors(proxy); + for (const prop of names) { + const scriptable = isScriptable(prop); + const indexable = isIndexable(prop); + const value = (indexable || scriptable) && proxy[prop]; + if ((scriptable && (isFunction(value) || hasFunction(value))) + || (indexable && isArray(value))) { + return true; + } + } + return false; +} + +var version = "3.9.1"; + +const KNOWN_POSITIONS = ['top', 'bottom', 'left', 'right', 'chartArea']; +function positionIsHorizontal(position, axis) { + return position === 'top' || position === 'bottom' || (KNOWN_POSITIONS.indexOf(position) === -1 && axis === 'x'); +} +function compare2Level(l1, l2) { + return function(a, b) { + return a[l1] === b[l1] + ? a[l2] - b[l2] + : a[l1] - b[l1]; + }; +} +function onAnimationsComplete(context) { + const chart = context.chart; + const animationOptions = chart.options.animation; + chart.notifyPlugins('afterRender'); + callback(animationOptions && animationOptions.onComplete, [context], chart); +} +function onAnimationProgress(context) { + const chart = context.chart; + const animationOptions = chart.options.animation; + callback(animationOptions && animationOptions.onProgress, [context], chart); +} +function getCanvas(item) { + if (_isDomSupported() && typeof item === 'string') { + item = document.getElementById(item); + } else if (item && item.length) { + item = item[0]; + } + if (item && item.canvas) { + item = item.canvas; + } + return item; +} +const instances = {}; +const getChart = (key) => { + const canvas = getCanvas(key); + return Object.values(instances).filter((c) => c.canvas === canvas).pop(); +}; +function moveNumericKeys(obj, start, move) { + const keys = Object.keys(obj); + for (const key of keys) { + const intKey = +key; + if (intKey >= start) { + const value = obj[key]; + delete obj[key]; + if (move > 0 || intKey > start) { + obj[intKey + move] = value; + } + } + } +} +function determineLastEvent(e, lastEvent, inChartArea, isClick) { + if (!inChartArea || e.type === 'mouseout') { + return null; + } + if (isClick) { + return lastEvent; + } + return e; +} +class Chart { + constructor(item, userConfig) { + const config = this.config = new Config(userConfig); + const initialCanvas = getCanvas(item); + const existingChart = getChart(initialCanvas); + if (existingChart) { + throw new Error( + 'Canvas is already in use. Chart with ID \'' + existingChart.id + '\'' + + ' must be destroyed before the canvas with ID \'' + existingChart.canvas.id + '\' can be reused.' + ); + } + const options = config.createResolver(config.chartOptionScopes(), this.getContext()); + this.platform = new (config.platform || _detectPlatform(initialCanvas))(); + this.platform.updateConfig(config); + const context = this.platform.acquireContext(initialCanvas, options.aspectRatio); + const canvas = context && context.canvas; + const height = canvas && canvas.height; + const width = canvas && canvas.width; + this.id = uid(); + this.ctx = context; + this.canvas = canvas; + this.width = width; + this.height = height; + this._options = options; + this._aspectRatio = this.aspectRatio; + this._layers = []; + this._metasets = []; + this._stacks = undefined; + this.boxes = []; + this.currentDevicePixelRatio = undefined; + this.chartArea = undefined; + this._active = []; + this._lastEvent = undefined; + this._listeners = {}; + this._responsiveListeners = undefined; + this._sortedMetasets = []; + this.scales = {}; + this._plugins = new PluginService(); + this.$proxies = {}; + this._hiddenIndices = {}; + this.attached = false; + this._animationsDisabled = undefined; + this.$context = undefined; + this._doResize = debounce(mode => this.update(mode), options.resizeDelay || 0); + this._dataChanges = []; + instances[this.id] = this; + if (!context || !canvas) { + console.error("Failed to create chart: can't acquire context from the given item"); + return; + } + animator.listen(this, 'complete', onAnimationsComplete); + animator.listen(this, 'progress', onAnimationProgress); + this._initialize(); + if (this.attached) { + this.update(); + } + } + get aspectRatio() { + const {options: {aspectRatio, maintainAspectRatio}, width, height, _aspectRatio} = this; + if (!isNullOrUndef(aspectRatio)) { + return aspectRatio; + } + if (maintainAspectRatio && _aspectRatio) { + return _aspectRatio; + } + return height ? width / height : null; + } + get data() { + return this.config.data; + } + set data(data) { + this.config.data = data; + } + get options() { + return this._options; + } + set options(options) { + this.config.options = options; + } + _initialize() { + this.notifyPlugins('beforeInit'); + if (this.options.responsive) { + this.resize(); + } else { + retinaScale(this, this.options.devicePixelRatio); + } + this.bindEvents(); + this.notifyPlugins('afterInit'); + return this; + } + clear() { + clearCanvas(this.canvas, this.ctx); + return this; + } + stop() { + animator.stop(this); + return this; + } + resize(width, height) { + if (!animator.running(this)) { + this._resize(width, height); + } else { + this._resizeBeforeDraw = {width, height}; + } + } + _resize(width, height) { + const options = this.options; + const canvas = this.canvas; + const aspectRatio = options.maintainAspectRatio && this.aspectRatio; + const newSize = this.platform.getMaximumSize(canvas, width, height, aspectRatio); + const newRatio = options.devicePixelRatio || this.platform.getDevicePixelRatio(); + const mode = this.width ? 'resize' : 'attach'; + this.width = newSize.width; + this.height = newSize.height; + this._aspectRatio = this.aspectRatio; + if (!retinaScale(this, newRatio, true)) { + return; + } + this.notifyPlugins('resize', {size: newSize}); + callback(options.onResize, [this, newSize], this); + if (this.attached) { + if (this._doResize(mode)) { + this.render(); + } + } + } + ensureScalesHaveIDs() { + const options = this.options; + const scalesOptions = options.scales || {}; + each(scalesOptions, (axisOptions, axisID) => { + axisOptions.id = axisID; + }); + } + buildOrUpdateScales() { + const options = this.options; + const scaleOpts = options.scales; + const scales = this.scales; + const updated = Object.keys(scales).reduce((obj, id) => { + obj[id] = false; + return obj; + }, {}); + let items = []; + if (scaleOpts) { + items = items.concat( + Object.keys(scaleOpts).map((id) => { + const scaleOptions = scaleOpts[id]; + const axis = determineAxis(id, scaleOptions); + const isRadial = axis === 'r'; + const isHorizontal = axis === 'x'; + return { + options: scaleOptions, + dposition: isRadial ? 'chartArea' : isHorizontal ? 'bottom' : 'left', + dtype: isRadial ? 'radialLinear' : isHorizontal ? 'category' : 'linear' + }; + }) + ); + } + each(items, (item) => { + const scaleOptions = item.options; + const id = scaleOptions.id; + const axis = determineAxis(id, scaleOptions); + const scaleType = valueOrDefault(scaleOptions.type, item.dtype); + if (scaleOptions.position === undefined || positionIsHorizontal(scaleOptions.position, axis) !== positionIsHorizontal(item.dposition)) { + scaleOptions.position = item.dposition; + } + updated[id] = true; + let scale = null; + if (id in scales && scales[id].type === scaleType) { + scale = scales[id]; + } else { + const scaleClass = registry.getScale(scaleType); + scale = new scaleClass({ + id, + type: scaleType, + ctx: this.ctx, + chart: this + }); + scales[scale.id] = scale; + } + scale.init(scaleOptions, options); + }); + each(updated, (hasUpdated, id) => { + if (!hasUpdated) { + delete scales[id]; + } + }); + each(scales, (scale) => { + layouts.configure(this, scale, scale.options); + layouts.addBox(this, scale); + }); + } + _updateMetasets() { + const metasets = this._metasets; + const numData = this.data.datasets.length; + const numMeta = metasets.length; + metasets.sort((a, b) => a.index - b.index); + if (numMeta > numData) { + for (let i = numData; i < numMeta; ++i) { + this._destroyDatasetMeta(i); + } + metasets.splice(numData, numMeta - numData); + } + this._sortedMetasets = metasets.slice(0).sort(compare2Level('order', 'index')); + } + _removeUnreferencedMetasets() { + const {_metasets: metasets, data: {datasets}} = this; + if (metasets.length > datasets.length) { + delete this._stacks; + } + metasets.forEach((meta, index) => { + if (datasets.filter(x => x === meta._dataset).length === 0) { + this._destroyDatasetMeta(index); + } + }); + } + buildOrUpdateControllers() { + const newControllers = []; + const datasets = this.data.datasets; + let i, ilen; + this._removeUnreferencedMetasets(); + for (i = 0, ilen = datasets.length; i < ilen; i++) { + const dataset = datasets[i]; + let meta = this.getDatasetMeta(i); + const type = dataset.type || this.config.type; + if (meta.type && meta.type !== type) { + this._destroyDatasetMeta(i); + meta = this.getDatasetMeta(i); + } + meta.type = type; + meta.indexAxis = dataset.indexAxis || getIndexAxis(type, this.options); + meta.order = dataset.order || 0; + meta.index = i; + meta.label = '' + dataset.label; + meta.visible = this.isDatasetVisible(i); + if (meta.controller) { + meta.controller.updateIndex(i); + meta.controller.linkScales(); + } else { + const ControllerClass = registry.getController(type); + const {datasetElementType, dataElementType} = defaults.datasets[type]; + Object.assign(ControllerClass.prototype, { + dataElementType: registry.getElement(dataElementType), + datasetElementType: datasetElementType && registry.getElement(datasetElementType) + }); + meta.controller = new ControllerClass(this, i); + newControllers.push(meta.controller); + } + } + this._updateMetasets(); + return newControllers; + } + _resetElements() { + each(this.data.datasets, (dataset, datasetIndex) => { + this.getDatasetMeta(datasetIndex).controller.reset(); + }, this); + } + reset() { + this._resetElements(); + this.notifyPlugins('reset'); + } + update(mode) { + const config = this.config; + config.update(); + const options = this._options = config.createResolver(config.chartOptionScopes(), this.getContext()); + const animsDisabled = this._animationsDisabled = !options.animation; + this._updateScales(); + this._checkEventBindings(); + this._updateHiddenIndices(); + this._plugins.invalidate(); + if (this.notifyPlugins('beforeUpdate', {mode, cancelable: true}) === false) { + return; + } + const newControllers = this.buildOrUpdateControllers(); + this.notifyPlugins('beforeElementsUpdate'); + let minPadding = 0; + for (let i = 0, ilen = this.data.datasets.length; i < ilen; i++) { + const {controller} = this.getDatasetMeta(i); + const reset = !animsDisabled && newControllers.indexOf(controller) === -1; + controller.buildOrUpdateElements(reset); + minPadding = Math.max(+controller.getMaxOverflow(), minPadding); + } + minPadding = this._minPadding = options.layout.autoPadding ? minPadding : 0; + this._updateLayout(minPadding); + if (!animsDisabled) { + each(newControllers, (controller) => { + controller.reset(); + }); + } + this._updateDatasets(mode); + this.notifyPlugins('afterUpdate', {mode}); + this._layers.sort(compare2Level('z', '_idx')); + const {_active, _lastEvent} = this; + if (_lastEvent) { + this._eventHandler(_lastEvent, true); + } else if (_active.length) { + this._updateHoverStyles(_active, _active, true); + } + this.render(); + } + _updateScales() { + each(this.scales, (scale) => { + layouts.removeBox(this, scale); + }); + this.ensureScalesHaveIDs(); + this.buildOrUpdateScales(); + } + _checkEventBindings() { + const options = this.options; + const existingEvents = new Set(Object.keys(this._listeners)); + const newEvents = new Set(options.events); + if (!setsEqual(existingEvents, newEvents) || !!this._responsiveListeners !== options.responsive) { + this.unbindEvents(); + this.bindEvents(); + } + } + _updateHiddenIndices() { + const {_hiddenIndices} = this; + const changes = this._getUniformDataChanges() || []; + for (const {method, start, count} of changes) { + const move = method === '_removeElements' ? -count : count; + moveNumericKeys(_hiddenIndices, start, move); + } + } + _getUniformDataChanges() { + const _dataChanges = this._dataChanges; + if (!_dataChanges || !_dataChanges.length) { + return; + } + this._dataChanges = []; + const datasetCount = this.data.datasets.length; + const makeSet = (idx) => new Set( + _dataChanges + .filter(c => c[0] === idx) + .map((c, i) => i + ',' + c.splice(1).join(',')) + ); + const changeSet = makeSet(0); + for (let i = 1; i < datasetCount; i++) { + if (!setsEqual(changeSet, makeSet(i))) { + return; + } + } + return Array.from(changeSet) + .map(c => c.split(',')) + .map(a => ({method: a[1], start: +a[2], count: +a[3]})); + } + _updateLayout(minPadding) { + if (this.notifyPlugins('beforeLayout', {cancelable: true}) === false) { + return; + } + layouts.update(this, this.width, this.height, minPadding); + const area = this.chartArea; + const noArea = area.width <= 0 || area.height <= 0; + this._layers = []; + each(this.boxes, (box) => { + if (noArea && box.position === 'chartArea') { + return; + } + if (box.configure) { + box.configure(); + } + this._layers.push(...box._layers()); + }, this); + this._layers.forEach((item, index) => { + item._idx = index; + }); + this.notifyPlugins('afterLayout'); + } + _updateDatasets(mode) { + if (this.notifyPlugins('beforeDatasetsUpdate', {mode, cancelable: true}) === false) { + return; + } + for (let i = 0, ilen = this.data.datasets.length; i < ilen; ++i) { + this.getDatasetMeta(i).controller.configure(); + } + for (let i = 0, ilen = this.data.datasets.length; i < ilen; ++i) { + this._updateDataset(i, isFunction(mode) ? mode({datasetIndex: i}) : mode); + } + this.notifyPlugins('afterDatasetsUpdate', {mode}); + } + _updateDataset(index, mode) { + const meta = this.getDatasetMeta(index); + const args = {meta, index, mode, cancelable: true}; + if (this.notifyPlugins('beforeDatasetUpdate', args) === false) { + return; + } + meta.controller._update(mode); + args.cancelable = false; + this.notifyPlugins('afterDatasetUpdate', args); + } + render() { + if (this.notifyPlugins('beforeRender', {cancelable: true}) === false) { + return; + } + if (animator.has(this)) { + if (this.attached && !animator.running(this)) { + animator.start(this); + } + } else { + this.draw(); + onAnimationsComplete({chart: this}); + } + } + draw() { + let i; + if (this._resizeBeforeDraw) { + const {width, height} = this._resizeBeforeDraw; + this._resize(width, height); + this._resizeBeforeDraw = null; + } + this.clear(); + if (this.width <= 0 || this.height <= 0) { + return; + } + if (this.notifyPlugins('beforeDraw', {cancelable: true}) === false) { + return; + } + const layers = this._layers; + for (i = 0; i < layers.length && layers[i].z <= 0; ++i) { + layers[i].draw(this.chartArea); + } + this._drawDatasets(); + for (; i < layers.length; ++i) { + layers[i].draw(this.chartArea); + } + this.notifyPlugins('afterDraw'); + } + _getSortedDatasetMetas(filterVisible) { + const metasets = this._sortedMetasets; + const result = []; + let i, ilen; + for (i = 0, ilen = metasets.length; i < ilen; ++i) { + const meta = metasets[i]; + if (!filterVisible || meta.visible) { + result.push(meta); + } + } + return result; + } + getSortedVisibleDatasetMetas() { + return this._getSortedDatasetMetas(true); + } + _drawDatasets() { + if (this.notifyPlugins('beforeDatasetsDraw', {cancelable: true}) === false) { + return; + } + const metasets = this.getSortedVisibleDatasetMetas(); + for (let i = metasets.length - 1; i >= 0; --i) { + this._drawDataset(metasets[i]); + } + this.notifyPlugins('afterDatasetsDraw'); + } + _drawDataset(meta) { + const ctx = this.ctx; + const clip = meta._clip; + const useClip = !clip.disabled; + const area = this.chartArea; + const args = { + meta, + index: meta.index, + cancelable: true + }; + if (this.notifyPlugins('beforeDatasetDraw', args) === false) { + return; + } + if (useClip) { + clipArea(ctx, { + left: clip.left === false ? 0 : area.left - clip.left, + right: clip.right === false ? this.width : area.right + clip.right, + top: clip.top === false ? 0 : area.top - clip.top, + bottom: clip.bottom === false ? this.height : area.bottom + clip.bottom + }); + } + meta.controller.draw(); + if (useClip) { + unclipArea(ctx); + } + args.cancelable = false; + this.notifyPlugins('afterDatasetDraw', args); + } + isPointInArea(point) { + return _isPointInArea(point, this.chartArea, this._minPadding); + } + getElementsAtEventForMode(e, mode, options, useFinalPosition) { + const method = Interaction.modes[mode]; + if (typeof method === 'function') { + return method(this, e, options, useFinalPosition); + } + return []; + } + getDatasetMeta(datasetIndex) { + const dataset = this.data.datasets[datasetIndex]; + const metasets = this._metasets; + let meta = metasets.filter(x => x && x._dataset === dataset).pop(); + if (!meta) { + meta = { + type: null, + data: [], + dataset: null, + controller: null, + hidden: null, + xAxisID: null, + yAxisID: null, + order: dataset && dataset.order || 0, + index: datasetIndex, + _dataset: dataset, + _parsed: [], + _sorted: false + }; + metasets.push(meta); + } + return meta; + } + getContext() { + return this.$context || (this.$context = createContext(null, {chart: this, type: 'chart'})); + } + getVisibleDatasetCount() { + return this.getSortedVisibleDatasetMetas().length; + } + isDatasetVisible(datasetIndex) { + const dataset = this.data.datasets[datasetIndex]; + if (!dataset) { + return false; + } + const meta = this.getDatasetMeta(datasetIndex); + return typeof meta.hidden === 'boolean' ? !meta.hidden : !dataset.hidden; + } + setDatasetVisibility(datasetIndex, visible) { + const meta = this.getDatasetMeta(datasetIndex); + meta.hidden = !visible; + } + toggleDataVisibility(index) { + this._hiddenIndices[index] = !this._hiddenIndices[index]; + } + getDataVisibility(index) { + return !this._hiddenIndices[index]; + } + _updateVisibility(datasetIndex, dataIndex, visible) { + const mode = visible ? 'show' : 'hide'; + const meta = this.getDatasetMeta(datasetIndex); + const anims = meta.controller._resolveAnimations(undefined, mode); + if (defined(dataIndex)) { + meta.data[dataIndex].hidden = !visible; + this.update(); + } else { + this.setDatasetVisibility(datasetIndex, visible); + anims.update(meta, {visible}); + this.update((ctx) => ctx.datasetIndex === datasetIndex ? mode : undefined); + } + } + hide(datasetIndex, dataIndex) { + this._updateVisibility(datasetIndex, dataIndex, false); + } + show(datasetIndex, dataIndex) { + this._updateVisibility(datasetIndex, dataIndex, true); + } + _destroyDatasetMeta(datasetIndex) { + const meta = this._metasets[datasetIndex]; + if (meta && meta.controller) { + meta.controller._destroy(); + } + delete this._metasets[datasetIndex]; + } + _stop() { + let i, ilen; + this.stop(); + animator.remove(this); + for (i = 0, ilen = this.data.datasets.length; i < ilen; ++i) { + this._destroyDatasetMeta(i); + } + } + destroy() { + this.notifyPlugins('beforeDestroy'); + const {canvas, ctx} = this; + this._stop(); + this.config.clearCache(); + if (canvas) { + this.unbindEvents(); + clearCanvas(canvas, ctx); + this.platform.releaseContext(ctx); + this.canvas = null; + this.ctx = null; + } + this.notifyPlugins('destroy'); + delete instances[this.id]; + this.notifyPlugins('afterDestroy'); + } + toBase64Image(...args) { + return this.canvas.toDataURL(...args); + } + bindEvents() { + this.bindUserEvents(); + if (this.options.responsive) { + this.bindResponsiveEvents(); + } else { + this.attached = true; + } + } + bindUserEvents() { + const listeners = this._listeners; + const platform = this.platform; + const _add = (type, listener) => { + platform.addEventListener(this, type, listener); + listeners[type] = listener; + }; + const listener = (e, x, y) => { + e.offsetX = x; + e.offsetY = y; + this._eventHandler(e); + }; + each(this.options.events, (type) => _add(type, listener)); + } + bindResponsiveEvents() { + if (!this._responsiveListeners) { + this._responsiveListeners = {}; + } + const listeners = this._responsiveListeners; + const platform = this.platform; + const _add = (type, listener) => { + platform.addEventListener(this, type, listener); + listeners[type] = listener; + }; + const _remove = (type, listener) => { + if (listeners[type]) { + platform.removeEventListener(this, type, listener); + delete listeners[type]; + } + }; + const listener = (width, height) => { + if (this.canvas) { + this.resize(width, height); + } + }; + let detached; + const attached = () => { + _remove('attach', attached); + this.attached = true; + this.resize(); + _add('resize', listener); + _add('detach', detached); + }; + detached = () => { + this.attached = false; + _remove('resize', listener); + this._stop(); + this._resize(0, 0); + _add('attach', attached); + }; + if (platform.isAttached(this.canvas)) { + attached(); + } else { + detached(); + } + } + unbindEvents() { + each(this._listeners, (listener, type) => { + this.platform.removeEventListener(this, type, listener); + }); + this._listeners = {}; + each(this._responsiveListeners, (listener, type) => { + this.platform.removeEventListener(this, type, listener); + }); + this._responsiveListeners = undefined; + } + updateHoverStyle(items, mode, enabled) { + const prefix = enabled ? 'set' : 'remove'; + let meta, item, i, ilen; + if (mode === 'dataset') { + meta = this.getDatasetMeta(items[0].datasetIndex); + meta.controller['_' + prefix + 'DatasetHoverStyle'](); + } + for (i = 0, ilen = items.length; i < ilen; ++i) { + item = items[i]; + const controller = item && this.getDatasetMeta(item.datasetIndex).controller; + if (controller) { + controller[prefix + 'HoverStyle'](item.element, item.datasetIndex, item.index); + } + } + } + getActiveElements() { + return this._active || []; + } + setActiveElements(activeElements) { + const lastActive = this._active || []; + const active = activeElements.map(({datasetIndex, index}) => { + const meta = this.getDatasetMeta(datasetIndex); + if (!meta) { + throw new Error('No dataset found at index ' + datasetIndex); + } + return { + datasetIndex, + element: meta.data[index], + index, + }; + }); + const changed = !_elementsEqual(active, lastActive); + if (changed) { + this._active = active; + this._lastEvent = null; + this._updateHoverStyles(active, lastActive); + } + } + notifyPlugins(hook, args, filter) { + return this._plugins.notify(this, hook, args, filter); + } + _updateHoverStyles(active, lastActive, replay) { + const hoverOptions = this.options.hover; + const diff = (a, b) => a.filter(x => !b.some(y => x.datasetIndex === y.datasetIndex && x.index === y.index)); + const deactivated = diff(lastActive, active); + const activated = replay ? active : diff(active, lastActive); + if (deactivated.length) { + this.updateHoverStyle(deactivated, hoverOptions.mode, false); + } + if (activated.length && hoverOptions.mode) { + this.updateHoverStyle(activated, hoverOptions.mode, true); + } + } + _eventHandler(e, replay) { + const args = { + event: e, + replay, + cancelable: true, + inChartArea: this.isPointInArea(e) + }; + const eventFilter = (plugin) => (plugin.options.events || this.options.events).includes(e.native.type); + if (this.notifyPlugins('beforeEvent', args, eventFilter) === false) { + return; + } + const changed = this._handleEvent(e, replay, args.inChartArea); + args.cancelable = false; + this.notifyPlugins('afterEvent', args, eventFilter); + if (changed || args.changed) { + this.render(); + } + return this; + } + _handleEvent(e, replay, inChartArea) { + const {_active: lastActive = [], options} = this; + const useFinalPosition = replay; + const active = this._getActiveElements(e, lastActive, inChartArea, useFinalPosition); + const isClick = _isClickEvent(e); + const lastEvent = determineLastEvent(e, this._lastEvent, inChartArea, isClick); + if (inChartArea) { + this._lastEvent = null; + callback(options.onHover, [e, active, this], this); + if (isClick) { + callback(options.onClick, [e, active, this], this); + } + } + const changed = !_elementsEqual(active, lastActive); + if (changed || replay) { + this._active = active; + this._updateHoverStyles(active, lastActive, replay); + } + this._lastEvent = lastEvent; + return changed; + } + _getActiveElements(e, lastActive, inChartArea, useFinalPosition) { + if (e.type === 'mouseout') { + return []; + } + if (!inChartArea) { + return lastActive; + } + const hoverOptions = this.options.hover; + return this.getElementsAtEventForMode(e, hoverOptions.mode, hoverOptions, useFinalPosition); + } +} +const invalidatePlugins = () => each(Chart.instances, (chart) => chart._plugins.invalidate()); +const enumerable = true; +Object.defineProperties(Chart, { + defaults: { + enumerable, + value: defaults + }, + instances: { + enumerable, + value: instances + }, + overrides: { + enumerable, + value: overrides + }, + registry: { + enumerable, + value: registry + }, + version: { + enumerable, + value: version + }, + getChart: { + enumerable, + value: getChart + }, + register: { + enumerable, + value: (...items) => { + registry.add(...items); + invalidatePlugins(); + } + }, + unregister: { + enumerable, + value: (...items) => { + registry.remove(...items); + invalidatePlugins(); + } + } +}); + +function clipArc(ctx, element, endAngle) { + const {startAngle, pixelMargin, x, y, outerRadius, innerRadius} = element; + let angleMargin = pixelMargin / outerRadius; + ctx.beginPath(); + ctx.arc(x, y, outerRadius, startAngle - angleMargin, endAngle + angleMargin); + if (innerRadius > pixelMargin) { + angleMargin = pixelMargin / innerRadius; + ctx.arc(x, y, innerRadius, endAngle + angleMargin, startAngle - angleMargin, true); + } else { + ctx.arc(x, y, pixelMargin, endAngle + HALF_PI, startAngle - HALF_PI); + } + ctx.closePath(); + ctx.clip(); +} +function toRadiusCorners(value) { + return _readValueToProps(value, ['outerStart', 'outerEnd', 'innerStart', 'innerEnd']); +} +function parseBorderRadius$1(arc, innerRadius, outerRadius, angleDelta) { + const o = toRadiusCorners(arc.options.borderRadius); + const halfThickness = (outerRadius - innerRadius) / 2; + const innerLimit = Math.min(halfThickness, angleDelta * innerRadius / 2); + const computeOuterLimit = (val) => { + const outerArcLimit = (outerRadius - Math.min(halfThickness, val)) * angleDelta / 2; + return _limitValue(val, 0, Math.min(halfThickness, outerArcLimit)); + }; + return { + outerStart: computeOuterLimit(o.outerStart), + outerEnd: computeOuterLimit(o.outerEnd), + innerStart: _limitValue(o.innerStart, 0, innerLimit), + innerEnd: _limitValue(o.innerEnd, 0, innerLimit), + }; +} +function rThetaToXY(r, theta, x, y) { + return { + x: x + r * Math.cos(theta), + y: y + r * Math.sin(theta), + }; +} +function pathArc(ctx, element, offset, spacing, end, circular) { + const {x, y, startAngle: start, pixelMargin, innerRadius: innerR} = element; + const outerRadius = Math.max(element.outerRadius + spacing + offset - pixelMargin, 0); + const innerRadius = innerR > 0 ? innerR + spacing + offset + pixelMargin : 0; + let spacingOffset = 0; + const alpha = end - start; + if (spacing) { + const noSpacingInnerRadius = innerR > 0 ? innerR - spacing : 0; + const noSpacingOuterRadius = outerRadius > 0 ? outerRadius - spacing : 0; + const avNogSpacingRadius = (noSpacingInnerRadius + noSpacingOuterRadius) / 2; + const adjustedAngle = avNogSpacingRadius !== 0 ? (alpha * avNogSpacingRadius) / (avNogSpacingRadius + spacing) : alpha; + spacingOffset = (alpha - adjustedAngle) / 2; + } + const beta = Math.max(0.001, alpha * outerRadius - offset / PI) / outerRadius; + const angleOffset = (alpha - beta) / 2; + const startAngle = start + angleOffset + spacingOffset; + const endAngle = end - angleOffset - spacingOffset; + const {outerStart, outerEnd, innerStart, innerEnd} = parseBorderRadius$1(element, innerRadius, outerRadius, endAngle - startAngle); + const outerStartAdjustedRadius = outerRadius - outerStart; + const outerEndAdjustedRadius = outerRadius - outerEnd; + const outerStartAdjustedAngle = startAngle + outerStart / outerStartAdjustedRadius; + const outerEndAdjustedAngle = endAngle - outerEnd / outerEndAdjustedRadius; + const innerStartAdjustedRadius = innerRadius + innerStart; + const innerEndAdjustedRadius = innerRadius + innerEnd; + const innerStartAdjustedAngle = startAngle + innerStart / innerStartAdjustedRadius; + const innerEndAdjustedAngle = endAngle - innerEnd / innerEndAdjustedRadius; + ctx.beginPath(); + if (circular) { + ctx.arc(x, y, outerRadius, outerStartAdjustedAngle, outerEndAdjustedAngle); + if (outerEnd > 0) { + const pCenter = rThetaToXY(outerEndAdjustedRadius, outerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerEnd, outerEndAdjustedAngle, endAngle + HALF_PI); + } + const p4 = rThetaToXY(innerEndAdjustedRadius, endAngle, x, y); + ctx.lineTo(p4.x, p4.y); + if (innerEnd > 0) { + const pCenter = rThetaToXY(innerEndAdjustedRadius, innerEndAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerEnd, endAngle + HALF_PI, innerEndAdjustedAngle + Math.PI); + } + ctx.arc(x, y, innerRadius, endAngle - (innerEnd / innerRadius), startAngle + (innerStart / innerRadius), true); + if (innerStart > 0) { + const pCenter = rThetaToXY(innerStartAdjustedRadius, innerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, innerStart, innerStartAdjustedAngle + Math.PI, startAngle - HALF_PI); + } + const p8 = rThetaToXY(outerStartAdjustedRadius, startAngle, x, y); + ctx.lineTo(p8.x, p8.y); + if (outerStart > 0) { + const pCenter = rThetaToXY(outerStartAdjustedRadius, outerStartAdjustedAngle, x, y); + ctx.arc(pCenter.x, pCenter.y, outerStart, startAngle - HALF_PI, outerStartAdjustedAngle); + } + } else { + ctx.moveTo(x, y); + const outerStartX = Math.cos(outerStartAdjustedAngle) * outerRadius + x; + const outerStartY = Math.sin(outerStartAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerStartX, outerStartY); + const outerEndX = Math.cos(outerEndAdjustedAngle) * outerRadius + x; + const outerEndY = Math.sin(outerEndAdjustedAngle) * outerRadius + y; + ctx.lineTo(outerEndX, outerEndY); + } + ctx.closePath(); +} +function drawArc(ctx, element, offset, spacing, circular) { + const {fullCircles, startAngle, circumference} = element; + let endAngle = element.endAngle; + if (fullCircles) { + pathArc(ctx, element, offset, spacing, startAngle + TAU, circular); + for (let i = 0; i < fullCircles; ++i) { + ctx.fill(); + } + if (!isNaN(circumference)) { + endAngle = startAngle + circumference % TAU; + if (circumference % TAU === 0) { + endAngle += TAU; + } + } + } + pathArc(ctx, element, offset, spacing, endAngle, circular); + ctx.fill(); + return endAngle; +} +function drawFullCircleBorders(ctx, element, inner) { + const {x, y, startAngle, pixelMargin, fullCircles} = element; + const outerRadius = Math.max(element.outerRadius - pixelMargin, 0); + const innerRadius = element.innerRadius + pixelMargin; + let i; + if (inner) { + clipArc(ctx, element, startAngle + TAU); + } + ctx.beginPath(); + ctx.arc(x, y, innerRadius, startAngle + TAU, startAngle, true); + for (i = 0; i < fullCircles; ++i) { + ctx.stroke(); + } + ctx.beginPath(); + ctx.arc(x, y, outerRadius, startAngle, startAngle + TAU); + for (i = 0; i < fullCircles; ++i) { + ctx.stroke(); + } +} +function drawBorder(ctx, element, offset, spacing, endAngle, circular) { + const {options} = element; + const {borderWidth, borderJoinStyle} = options; + const inner = options.borderAlign === 'inner'; + if (!borderWidth) { + return; + } + if (inner) { + ctx.lineWidth = borderWidth * 2; + ctx.lineJoin = borderJoinStyle || 'round'; + } else { + ctx.lineWidth = borderWidth; + ctx.lineJoin = borderJoinStyle || 'bevel'; + } + if (element.fullCircles) { + drawFullCircleBorders(ctx, element, inner); + } + if (inner) { + clipArc(ctx, element, endAngle); + } + pathArc(ctx, element, offset, spacing, endAngle, circular); + ctx.stroke(); +} +class ArcElement extends Element { + constructor(cfg) { + super(); + this.options = undefined; + this.circumference = undefined; + this.startAngle = undefined; + this.endAngle = undefined; + this.innerRadius = undefined; + this.outerRadius = undefined; + this.pixelMargin = 0; + this.fullCircles = 0; + if (cfg) { + Object.assign(this, cfg); + } + } + inRange(chartX, chartY, useFinalPosition) { + const point = this.getProps(['x', 'y'], useFinalPosition); + const {angle, distance} = getAngleFromPoint(point, {x: chartX, y: chartY}); + const {startAngle, endAngle, innerRadius, outerRadius, circumference} = this.getProps([ + 'startAngle', + 'endAngle', + 'innerRadius', + 'outerRadius', + 'circumference' + ], useFinalPosition); + const rAdjust = this.options.spacing / 2; + const _circumference = valueOrDefault(circumference, endAngle - startAngle); + const betweenAngles = _circumference >= TAU || _angleBetween(angle, startAngle, endAngle); + const withinRadius = _isBetween(distance, innerRadius + rAdjust, outerRadius + rAdjust); + return (betweenAngles && withinRadius); + } + getCenterPoint(useFinalPosition) { + const {x, y, startAngle, endAngle, innerRadius, outerRadius} = this.getProps([ + 'x', + 'y', + 'startAngle', + 'endAngle', + 'innerRadius', + 'outerRadius', + 'circumference', + ], useFinalPosition); + const {offset, spacing} = this.options; + const halfAngle = (startAngle + endAngle) / 2; + const halfRadius = (innerRadius + outerRadius + spacing + offset) / 2; + return { + x: x + Math.cos(halfAngle) * halfRadius, + y: y + Math.sin(halfAngle) * halfRadius + }; + } + tooltipPosition(useFinalPosition) { + return this.getCenterPoint(useFinalPosition); + } + draw(ctx) { + const {options, circumference} = this; + const offset = (options.offset || 0) / 2; + const spacing = (options.spacing || 0) / 2; + const circular = options.circular; + this.pixelMargin = (options.borderAlign === 'inner') ? 0.33 : 0; + this.fullCircles = circumference > TAU ? Math.floor(circumference / TAU) : 0; + if (circumference === 0 || this.innerRadius < 0 || this.outerRadius < 0) { + return; + } + ctx.save(); + let radiusOffset = 0; + if (offset) { + radiusOffset = offset / 2; + const halfAngle = (this.startAngle + this.endAngle) / 2; + ctx.translate(Math.cos(halfAngle) * radiusOffset, Math.sin(halfAngle) * radiusOffset); + if (this.circumference >= PI) { + radiusOffset = offset; + } + } + ctx.fillStyle = options.backgroundColor; + ctx.strokeStyle = options.borderColor; + const endAngle = drawArc(ctx, this, radiusOffset, spacing, circular); + drawBorder(ctx, this, radiusOffset, spacing, endAngle, circular); + ctx.restore(); + } +} +ArcElement.id = 'arc'; +ArcElement.defaults = { + borderAlign: 'center', + borderColor: '#fff', + borderJoinStyle: undefined, + borderRadius: 0, + borderWidth: 2, + offset: 0, + spacing: 0, + angle: undefined, + circular: true, +}; +ArcElement.defaultRoutes = { + backgroundColor: 'backgroundColor' +}; + +function setStyle(ctx, options, style = options) { + ctx.lineCap = valueOrDefault(style.borderCapStyle, options.borderCapStyle); + ctx.setLineDash(valueOrDefault(style.borderDash, options.borderDash)); + ctx.lineDashOffset = valueOrDefault(style.borderDashOffset, options.borderDashOffset); + ctx.lineJoin = valueOrDefault(style.borderJoinStyle, options.borderJoinStyle); + ctx.lineWidth = valueOrDefault(style.borderWidth, options.borderWidth); + ctx.strokeStyle = valueOrDefault(style.borderColor, options.borderColor); +} +function lineTo(ctx, previous, target) { + ctx.lineTo(target.x, target.y); +} +function getLineMethod(options) { + if (options.stepped) { + return _steppedLineTo; + } + if (options.tension || options.cubicInterpolationMode === 'monotone') { + return _bezierCurveTo; + } + return lineTo; +} +function pathVars(points, segment, params = {}) { + const count = points.length; + const {start: paramsStart = 0, end: paramsEnd = count - 1} = params; + const {start: segmentStart, end: segmentEnd} = segment; + const start = Math.max(paramsStart, segmentStart); + const end = Math.min(paramsEnd, segmentEnd); + const outside = paramsStart < segmentStart && paramsEnd < segmentStart || paramsStart > segmentEnd && paramsEnd > segmentEnd; + return { + count, + start, + loop: segment.loop, + ilen: end < start && !outside ? count + end - start : end - start + }; +} +function pathSegment(ctx, line, segment, params) { + const {points, options} = line; + const {count, start, loop, ilen} = pathVars(points, segment, params); + const lineMethod = getLineMethod(options); + let {move = true, reverse} = params || {}; + let i, point, prev; + for (i = 0; i <= ilen; ++i) { + point = points[(start + (reverse ? ilen - i : i)) % count]; + if (point.skip) { + continue; + } else if (move) { + ctx.moveTo(point.x, point.y); + move = false; + } else { + lineMethod(ctx, prev, point, reverse, options.stepped); + } + prev = point; + } + if (loop) { + point = points[(start + (reverse ? ilen : 0)) % count]; + lineMethod(ctx, prev, point, reverse, options.stepped); + } + return !!loop; +} +function fastPathSegment(ctx, line, segment, params) { + const points = line.points; + const {count, start, ilen} = pathVars(points, segment, params); + const {move = true, reverse} = params || {}; + let avgX = 0; + let countX = 0; + let i, point, prevX, minY, maxY, lastY; + const pointIndex = (index) => (start + (reverse ? ilen - index : index)) % count; + const drawX = () => { + if (minY !== maxY) { + ctx.lineTo(avgX, maxY); + ctx.lineTo(avgX, minY); + ctx.lineTo(avgX, lastY); + } + }; + if (move) { + point = points[pointIndex(0)]; + ctx.moveTo(point.x, point.y); + } + for (i = 0; i <= ilen; ++i) { + point = points[pointIndex(i)]; + if (point.skip) { + continue; + } + const x = point.x; + const y = point.y; + const truncX = x | 0; + if (truncX === prevX) { + if (y < minY) { + minY = y; + } else if (y > maxY) { + maxY = y; + } + avgX = (countX * avgX + x) / ++countX; + } else { + drawX(); + ctx.lineTo(x, y); + prevX = truncX; + countX = 0; + minY = maxY = y; + } + lastY = y; + } + drawX(); +} +function _getSegmentMethod(line) { + const opts = line.options; + const borderDash = opts.borderDash && opts.borderDash.length; + const useFastPath = !line._decimated && !line._loop && !opts.tension && opts.cubicInterpolationMode !== 'monotone' && !opts.stepped && !borderDash; + return useFastPath ? fastPathSegment : pathSegment; +} +function _getInterpolationMethod(options) { + if (options.stepped) { + return _steppedInterpolation; + } + if (options.tension || options.cubicInterpolationMode === 'monotone') { + return _bezierInterpolation; + } + return _pointInLine; +} +function strokePathWithCache(ctx, line, start, count) { + let path = line._path; + if (!path) { + path = line._path = new Path2D(); + if (line.path(path, start, count)) { + path.closePath(); + } + } + setStyle(ctx, line.options); + ctx.stroke(path); +} +function strokePathDirect(ctx, line, start, count) { + const {segments, options} = line; + const segmentMethod = _getSegmentMethod(line); + for (const segment of segments) { + setStyle(ctx, options, segment.style); + ctx.beginPath(); + if (segmentMethod(ctx, line, segment, {start, end: start + count - 1})) { + ctx.closePath(); + } + ctx.stroke(); + } +} +const usePath2D = typeof Path2D === 'function'; +function draw(ctx, line, start, count) { + if (usePath2D && !line.options.segment) { + strokePathWithCache(ctx, line, start, count); + } else { + strokePathDirect(ctx, line, start, count); + } +} +class LineElement extends Element { + constructor(cfg) { + super(); + this.animated = true; + this.options = undefined; + this._chart = undefined; + this._loop = undefined; + this._fullLoop = undefined; + this._path = undefined; + this._points = undefined; + this._segments = undefined; + this._decimated = false; + this._pointsUpdated = false; + this._datasetIndex = undefined; + if (cfg) { + Object.assign(this, cfg); + } + } + updateControlPoints(chartArea, indexAxis) { + const options = this.options; + if ((options.tension || options.cubicInterpolationMode === 'monotone') && !options.stepped && !this._pointsUpdated) { + const loop = options.spanGaps ? this._loop : this._fullLoop; + _updateBezierControlPoints(this._points, options, chartArea, loop, indexAxis); + this._pointsUpdated = true; + } + } + set points(points) { + this._points = points; + delete this._segments; + delete this._path; + this._pointsUpdated = false; + } + get points() { + return this._points; + } + get segments() { + return this._segments || (this._segments = _computeSegments(this, this.options.segment)); + } + first() { + const segments = this.segments; + const points = this.points; + return segments.length && points[segments[0].start]; + } + last() { + const segments = this.segments; + const points = this.points; + const count = segments.length; + return count && points[segments[count - 1].end]; + } + interpolate(point, property) { + const options = this.options; + const value = point[property]; + const points = this.points; + const segments = _boundSegments(this, {property, start: value, end: value}); + if (!segments.length) { + return; + } + const result = []; + const _interpolate = _getInterpolationMethod(options); + let i, ilen; + for (i = 0, ilen = segments.length; i < ilen; ++i) { + const {start, end} = segments[i]; + const p1 = points[start]; + const p2 = points[end]; + if (p1 === p2) { + result.push(p1); + continue; + } + const t = Math.abs((value - p1[property]) / (p2[property] - p1[property])); + const interpolated = _interpolate(p1, p2, t, options.stepped); + interpolated[property] = point[property]; + result.push(interpolated); + } + return result.length === 1 ? result[0] : result; + } + pathSegment(ctx, segment, params) { + const segmentMethod = _getSegmentMethod(this); + return segmentMethod(ctx, this, segment, params); + } + path(ctx, start, count) { + const segments = this.segments; + const segmentMethod = _getSegmentMethod(this); + let loop = this._loop; + start = start || 0; + count = count || (this.points.length - start); + for (const segment of segments) { + loop &= segmentMethod(ctx, this, segment, {start, end: start + count - 1}); + } + return !!loop; + } + draw(ctx, chartArea, start, count) { + const options = this.options || {}; + const points = this.points || []; + if (points.length && options.borderWidth) { + ctx.save(); + draw(ctx, this, start, count); + ctx.restore(); + } + if (this.animated) { + this._pointsUpdated = false; + this._path = undefined; + } + } +} +LineElement.id = 'line'; +LineElement.defaults = { + borderCapStyle: 'butt', + borderDash: [], + borderDashOffset: 0, + borderJoinStyle: 'miter', + borderWidth: 3, + capBezierPoints: true, + cubicInterpolationMode: 'default', + fill: false, + spanGaps: false, + stepped: false, + tension: 0, +}; +LineElement.defaultRoutes = { + backgroundColor: 'backgroundColor', + borderColor: 'borderColor' +}; +LineElement.descriptors = { + _scriptable: true, + _indexable: (name) => name !== 'borderDash' && name !== 'fill', +}; + +function inRange$1(el, pos, axis, useFinalPosition) { + const options = el.options; + const {[axis]: value} = el.getProps([axis], useFinalPosition); + return (Math.abs(pos - value) < options.radius + options.hitRadius); +} +class PointElement extends Element { + constructor(cfg) { + super(); + this.options = undefined; + this.parsed = undefined; + this.skip = undefined; + this.stop = undefined; + if (cfg) { + Object.assign(this, cfg); + } + } + inRange(mouseX, mouseY, useFinalPosition) { + const options = this.options; + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + return ((Math.pow(mouseX - x, 2) + Math.pow(mouseY - y, 2)) < Math.pow(options.hitRadius + options.radius, 2)); + } + inXRange(mouseX, useFinalPosition) { + return inRange$1(this, mouseX, 'x', useFinalPosition); + } + inYRange(mouseY, useFinalPosition) { + return inRange$1(this, mouseY, 'y', useFinalPosition); + } + getCenterPoint(useFinalPosition) { + const {x, y} = this.getProps(['x', 'y'], useFinalPosition); + return {x, y}; + } + size(options) { + options = options || this.options || {}; + let radius = options.radius || 0; + radius = Math.max(radius, radius && options.hoverRadius || 0); + const borderWidth = radius && options.borderWidth || 0; + return (radius + borderWidth) * 2; + } + draw(ctx, area) { + const options = this.options; + if (this.skip || options.radius < 0.1 || !_isPointInArea(this, area, this.size(options) / 2)) { + return; + } + ctx.strokeStyle = options.borderColor; + ctx.lineWidth = options.borderWidth; + ctx.fillStyle = options.backgroundColor; + drawPoint(ctx, options, this.x, this.y); + } + getRange() { + const options = this.options || {}; + return options.radius + options.hitRadius; + } +} +PointElement.id = 'point'; +PointElement.defaults = { + borderWidth: 1, + hitRadius: 1, + hoverBorderWidth: 1, + hoverRadius: 4, + pointStyle: 'circle', + radius: 3, + rotation: 0 +}; +PointElement.defaultRoutes = { + backgroundColor: 'backgroundColor', + borderColor: 'borderColor' +}; + +function getBarBounds(bar, useFinalPosition) { + const {x, y, base, width, height} = bar.getProps(['x', 'y', 'base', 'width', 'height'], useFinalPosition); + let left, right, top, bottom, half; + if (bar.horizontal) { + half = height / 2; + left = Math.min(x, base); + right = Math.max(x, base); + top = y - half; + bottom = y + half; + } else { + half = width / 2; + left = x - half; + right = x + half; + top = Math.min(y, base); + bottom = Math.max(y, base); + } + return {left, top, right, bottom}; +} +function skipOrLimit(skip, value, min, max) { + return skip ? 0 : _limitValue(value, min, max); +} +function parseBorderWidth(bar, maxW, maxH) { + const value = bar.options.borderWidth; + const skip = bar.borderSkipped; + const o = toTRBL(value); + return { + t: skipOrLimit(skip.top, o.top, 0, maxH), + r: skipOrLimit(skip.right, o.right, 0, maxW), + b: skipOrLimit(skip.bottom, o.bottom, 0, maxH), + l: skipOrLimit(skip.left, o.left, 0, maxW) + }; +} +function parseBorderRadius(bar, maxW, maxH) { + const {enableBorderRadius} = bar.getProps(['enableBorderRadius']); + const value = bar.options.borderRadius; + const o = toTRBLCorners(value); + const maxR = Math.min(maxW, maxH); + const skip = bar.borderSkipped; + const enableBorder = enableBorderRadius || isObject(value); + return { + topLeft: skipOrLimit(!enableBorder || skip.top || skip.left, o.topLeft, 0, maxR), + topRight: skipOrLimit(!enableBorder || skip.top || skip.right, o.topRight, 0, maxR), + bottomLeft: skipOrLimit(!enableBorder || skip.bottom || skip.left, o.bottomLeft, 0, maxR), + bottomRight: skipOrLimit(!enableBorder || skip.bottom || skip.right, o.bottomRight, 0, maxR) + }; +} +function boundingRects(bar) { + const bounds = getBarBounds(bar); + const width = bounds.right - bounds.left; + const height = bounds.bottom - bounds.top; + const border = parseBorderWidth(bar, width / 2, height / 2); + const radius = parseBorderRadius(bar, width / 2, height / 2); + return { + outer: { + x: bounds.left, + y: bounds.top, + w: width, + h: height, + radius + }, + inner: { + x: bounds.left + border.l, + y: bounds.top + border.t, + w: width - border.l - border.r, + h: height - border.t - border.b, + radius: { + topLeft: Math.max(0, radius.topLeft - Math.max(border.t, border.l)), + topRight: Math.max(0, radius.topRight - Math.max(border.t, border.r)), + bottomLeft: Math.max(0, radius.bottomLeft - Math.max(border.b, border.l)), + bottomRight: Math.max(0, radius.bottomRight - Math.max(border.b, border.r)), + } + } + }; +} +function inRange(bar, x, y, useFinalPosition) { + const skipX = x === null; + const skipY = y === null; + const skipBoth = skipX && skipY; + const bounds = bar && !skipBoth && getBarBounds(bar, useFinalPosition); + return bounds + && (skipX || _isBetween(x, bounds.left, bounds.right)) + && (skipY || _isBetween(y, bounds.top, bounds.bottom)); +} +function hasRadius(radius) { + return radius.topLeft || radius.topRight || radius.bottomLeft || radius.bottomRight; +} +function addNormalRectPath(ctx, rect) { + ctx.rect(rect.x, rect.y, rect.w, rect.h); +} +function inflateRect(rect, amount, refRect = {}) { + const x = rect.x !== refRect.x ? -amount : 0; + const y = rect.y !== refRect.y ? -amount : 0; + const w = (rect.x + rect.w !== refRect.x + refRect.w ? amount : 0) - x; + const h = (rect.y + rect.h !== refRect.y + refRect.h ? amount : 0) - y; + return { + x: rect.x + x, + y: rect.y + y, + w: rect.w + w, + h: rect.h + h, + radius: rect.radius + }; +} +class BarElement extends Element { + constructor(cfg) { + super(); + this.options = undefined; + this.horizontal = undefined; + this.base = undefined; + this.width = undefined; + this.height = undefined; + this.inflateAmount = undefined; + if (cfg) { + Object.assign(this, cfg); + } + } + draw(ctx) { + const {inflateAmount, options: {borderColor, backgroundColor}} = this; + const {inner, outer} = boundingRects(this); + const addRectPath = hasRadius(outer.radius) ? addRoundedRectPath : addNormalRectPath; + ctx.save(); + if (outer.w !== inner.w || outer.h !== inner.h) { + ctx.beginPath(); + addRectPath(ctx, inflateRect(outer, inflateAmount, inner)); + ctx.clip(); + addRectPath(ctx, inflateRect(inner, -inflateAmount, outer)); + ctx.fillStyle = borderColor; + ctx.fill('evenodd'); + } + ctx.beginPath(); + addRectPath(ctx, inflateRect(inner, inflateAmount)); + ctx.fillStyle = backgroundColor; + ctx.fill(); + ctx.restore(); + } + inRange(mouseX, mouseY, useFinalPosition) { + return inRange(this, mouseX, mouseY, useFinalPosition); + } + inXRange(mouseX, useFinalPosition) { + return inRange(this, mouseX, null, useFinalPosition); + } + inYRange(mouseY, useFinalPosition) { + return inRange(this, null, mouseY, useFinalPosition); + } + getCenterPoint(useFinalPosition) { + const {x, y, base, horizontal} = this.getProps(['x', 'y', 'base', 'horizontal'], useFinalPosition); + return { + x: horizontal ? (x + base) / 2 : x, + y: horizontal ? y : (y + base) / 2 + }; + } + getRange(axis) { + return axis === 'x' ? this.width / 2 : this.height / 2; + } +} +BarElement.id = 'bar'; +BarElement.defaults = { + borderSkipped: 'start', + borderWidth: 0, + borderRadius: 0, + inflateAmount: 'auto', + pointStyle: undefined +}; +BarElement.defaultRoutes = { + backgroundColor: 'backgroundColor', + borderColor: 'borderColor' +}; + +var elements = /*#__PURE__*/Object.freeze({ +__proto__: null, +ArcElement: ArcElement, +LineElement: LineElement, +PointElement: PointElement, +BarElement: BarElement +}); + +function lttbDecimation(data, start, count, availableWidth, options) { + const samples = options.samples || availableWidth; + if (samples >= count) { + return data.slice(start, start + count); + } + const decimated = []; + const bucketWidth = (count - 2) / (samples - 2); + let sampledIndex = 0; + const endIndex = start + count - 1; + let a = start; + let i, maxAreaPoint, maxArea, area, nextA; + decimated[sampledIndex++] = data[a]; + for (i = 0; i < samples - 2; i++) { + let avgX = 0; + let avgY = 0; + let j; + const avgRangeStart = Math.floor((i + 1) * bucketWidth) + 1 + start; + const avgRangeEnd = Math.min(Math.floor((i + 2) * bucketWidth) + 1, count) + start; + const avgRangeLength = avgRangeEnd - avgRangeStart; + for (j = avgRangeStart; j < avgRangeEnd; j++) { + avgX += data[j].x; + avgY += data[j].y; + } + avgX /= avgRangeLength; + avgY /= avgRangeLength; + const rangeOffs = Math.floor(i * bucketWidth) + 1 + start; + const rangeTo = Math.min(Math.floor((i + 1) * bucketWidth) + 1, count) + start; + const {x: pointAx, y: pointAy} = data[a]; + maxArea = area = -1; + for (j = rangeOffs; j < rangeTo; j++) { + area = 0.5 * Math.abs( + (pointAx - avgX) * (data[j].y - pointAy) - + (pointAx - data[j].x) * (avgY - pointAy) + ); + if (area > maxArea) { + maxArea = area; + maxAreaPoint = data[j]; + nextA = j; + } + } + decimated[sampledIndex++] = maxAreaPoint; + a = nextA; + } + decimated[sampledIndex++] = data[endIndex]; + return decimated; +} +function minMaxDecimation(data, start, count, availableWidth) { + let avgX = 0; + let countX = 0; + let i, point, x, y, prevX, minIndex, maxIndex, startIndex, minY, maxY; + const decimated = []; + const endIndex = start + count - 1; + const xMin = data[start].x; + const xMax = data[endIndex].x; + const dx = xMax - xMin; + for (i = start; i < start + count; ++i) { + point = data[i]; + x = (point.x - xMin) / dx * availableWidth; + y = point.y; + const truncX = x | 0; + if (truncX === prevX) { + if (y < minY) { + minY = y; + minIndex = i; + } else if (y > maxY) { + maxY = y; + maxIndex = i; + } + avgX = (countX * avgX + point.x) / ++countX; + } else { + const lastIndex = i - 1; + if (!isNullOrUndef(minIndex) && !isNullOrUndef(maxIndex)) { + const intermediateIndex1 = Math.min(minIndex, maxIndex); + const intermediateIndex2 = Math.max(minIndex, maxIndex); + if (intermediateIndex1 !== startIndex && intermediateIndex1 !== lastIndex) { + decimated.push({ + ...data[intermediateIndex1], + x: avgX, + }); + } + if (intermediateIndex2 !== startIndex && intermediateIndex2 !== lastIndex) { + decimated.push({ + ...data[intermediateIndex2], + x: avgX + }); + } + } + if (i > 0 && lastIndex !== startIndex) { + decimated.push(data[lastIndex]); + } + decimated.push(point); + prevX = truncX; + countX = 0; + minY = maxY = y; + minIndex = maxIndex = startIndex = i; + } + } + return decimated; +} +function cleanDecimatedDataset(dataset) { + if (dataset._decimated) { + const data = dataset._data; + delete dataset._decimated; + delete dataset._data; + Object.defineProperty(dataset, 'data', {value: data}); + } +} +function cleanDecimatedData(chart) { + chart.data.datasets.forEach((dataset) => { + cleanDecimatedDataset(dataset); + }); +} +function getStartAndCountOfVisiblePointsSimplified(meta, points) { + const pointCount = points.length; + let start = 0; + let count; + const {iScale} = meta; + const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); + if (minDefined) { + start = _limitValue(_lookupByKey(points, iScale.axis, min).lo, 0, pointCount - 1); + } + if (maxDefined) { + count = _limitValue(_lookupByKey(points, iScale.axis, max).hi + 1, start, pointCount) - start; + } else { + count = pointCount - start; + } + return {start, count}; +} +var plugin_decimation = { + id: 'decimation', + defaults: { + algorithm: 'min-max', + enabled: false, + }, + beforeElementsUpdate: (chart, args, options) => { + if (!options.enabled) { + cleanDecimatedData(chart); + return; + } + const availableWidth = chart.width; + chart.data.datasets.forEach((dataset, datasetIndex) => { + const {_data, indexAxis} = dataset; + const meta = chart.getDatasetMeta(datasetIndex); + const data = _data || dataset.data; + if (resolve([indexAxis, chart.options.indexAxis]) === 'y') { + return; + } + if (!meta.controller.supportsDecimation) { + return; + } + const xAxis = chart.scales[meta.xAxisID]; + if (xAxis.type !== 'linear' && xAxis.type !== 'time') { + return; + } + if (chart.options.parsing) { + return; + } + let {start, count} = getStartAndCountOfVisiblePointsSimplified(meta, data); + const threshold = options.threshold || 4 * availableWidth; + if (count <= threshold) { + cleanDecimatedDataset(dataset); + return; + } + if (isNullOrUndef(_data)) { + dataset._data = data; + delete dataset.data; + Object.defineProperty(dataset, 'data', { + configurable: true, + enumerable: true, + get: function() { + return this._decimated; + }, + set: function(d) { + this._data = d; + } + }); + } + let decimated; + switch (options.algorithm) { + case 'lttb': + decimated = lttbDecimation(data, start, count, availableWidth, options); + break; + case 'min-max': + decimated = minMaxDecimation(data, start, count, availableWidth); + break; + default: + throw new Error(`Unsupported decimation algorithm '${options.algorithm}'`); + } + dataset._decimated = decimated; + }); + }, + destroy(chart) { + cleanDecimatedData(chart); + } +}; + +function _segments(line, target, property) { + const segments = line.segments; + const points = line.points; + const tpoints = target.points; + const parts = []; + for (const segment of segments) { + let {start, end} = segment; + end = _findSegmentEnd(start, end, points); + const bounds = _getBounds(property, points[start], points[end], segment.loop); + if (!target.segments) { + parts.push({ + source: segment, + target: bounds, + start: points[start], + end: points[end] + }); + continue; + } + const targetSegments = _boundSegments(target, bounds); + for (const tgt of targetSegments) { + const subBounds = _getBounds(property, tpoints[tgt.start], tpoints[tgt.end], tgt.loop); + const fillSources = _boundSegment(segment, points, subBounds); + for (const fillSource of fillSources) { + parts.push({ + source: fillSource, + target: tgt, + start: { + [property]: _getEdge(bounds, subBounds, 'start', Math.max) + }, + end: { + [property]: _getEdge(bounds, subBounds, 'end', Math.min) + } + }); + } + } + } + return parts; +} +function _getBounds(property, first, last, loop) { + if (loop) { + return; + } + let start = first[property]; + let end = last[property]; + if (property === 'angle') { + start = _normalizeAngle(start); + end = _normalizeAngle(end); + } + return {property, start, end}; +} +function _pointsFromSegments(boundary, line) { + const {x = null, y = null} = boundary || {}; + const linePoints = line.points; + const points = []; + line.segments.forEach(({start, end}) => { + end = _findSegmentEnd(start, end, linePoints); + const first = linePoints[start]; + const last = linePoints[end]; + if (y !== null) { + points.push({x: first.x, y}); + points.push({x: last.x, y}); + } else if (x !== null) { + points.push({x, y: first.y}); + points.push({x, y: last.y}); + } + }); + return points; +} +function _findSegmentEnd(start, end, points) { + for (;end > start; end--) { + const point = points[end]; + if (!isNaN(point.x) && !isNaN(point.y)) { + break; + } + } + return end; +} +function _getEdge(a, b, prop, fn) { + if (a && b) { + return fn(a[prop], b[prop]); + } + return a ? a[prop] : b ? b[prop] : 0; +} + +function _createBoundaryLine(boundary, line) { + let points = []; + let _loop = false; + if (isArray(boundary)) { + _loop = true; + points = boundary; + } else { + points = _pointsFromSegments(boundary, line); + } + return points.length ? new LineElement({ + points, + options: {tension: 0}, + _loop, + _fullLoop: _loop + }) : null; +} +function _shouldApplyFill(source) { + return source && source.fill !== false; +} + +function _resolveTarget(sources, index, propagate) { + const source = sources[index]; + let fill = source.fill; + const visited = [index]; + let target; + if (!propagate) { + return fill; + } + while (fill !== false && visited.indexOf(fill) === -1) { + if (!isNumberFinite(fill)) { + return fill; + } + target = sources[fill]; + if (!target) { + return false; + } + if (target.visible) { + return fill; + } + visited.push(fill); + fill = target.fill; + } + return false; +} +function _decodeFill(line, index, count) { + const fill = parseFillOption(line); + if (isObject(fill)) { + return isNaN(fill.value) ? false : fill; + } + let target = parseFloat(fill); + if (isNumberFinite(target) && Math.floor(target) === target) { + return decodeTargetIndex(fill[0], index, target, count); + } + return ['origin', 'start', 'end', 'stack', 'shape'].indexOf(fill) >= 0 && fill; +} +function decodeTargetIndex(firstCh, index, target, count) { + if (firstCh === '-' || firstCh === '+') { + target = index + target; + } + if (target === index || target < 0 || target >= count) { + return false; + } + return target; +} +function _getTargetPixel(fill, scale) { + let pixel = null; + if (fill === 'start') { + pixel = scale.bottom; + } else if (fill === 'end') { + pixel = scale.top; + } else if (isObject(fill)) { + pixel = scale.getPixelForValue(fill.value); + } else if (scale.getBasePixel) { + pixel = scale.getBasePixel(); + } + return pixel; +} +function _getTargetValue(fill, scale, startValue) { + let value; + if (fill === 'start') { + value = startValue; + } else if (fill === 'end') { + value = scale.options.reverse ? scale.min : scale.max; + } else if (isObject(fill)) { + value = fill.value; + } else { + value = scale.getBaseValue(); + } + return value; +} +function parseFillOption(line) { + const options = line.options; + const fillOption = options.fill; + let fill = valueOrDefault(fillOption && fillOption.target, fillOption); + if (fill === undefined) { + fill = !!options.backgroundColor; + } + if (fill === false || fill === null) { + return false; + } + if (fill === true) { + return 'origin'; + } + return fill; +} + +function _buildStackLine(source) { + const {scale, index, line} = source; + const points = []; + const segments = line.segments; + const sourcePoints = line.points; + const linesBelow = getLinesBelow(scale, index); + linesBelow.push(_createBoundaryLine({x: null, y: scale.bottom}, line)); + for (let i = 0; i < segments.length; i++) { + const segment = segments[i]; + for (let j = segment.start; j <= segment.end; j++) { + addPointsBelow(points, sourcePoints[j], linesBelow); + } + } + return new LineElement({points, options: {}}); +} +function getLinesBelow(scale, index) { + const below = []; + const metas = scale.getMatchingVisibleMetas('line'); + for (let i = 0; i < metas.length; i++) { + const meta = metas[i]; + if (meta.index === index) { + break; + } + if (!meta.hidden) { + below.unshift(meta.dataset); + } + } + return below; +} +function addPointsBelow(points, sourcePoint, linesBelow) { + const postponed = []; + for (let j = 0; j < linesBelow.length; j++) { + const line = linesBelow[j]; + const {first, last, point} = findPoint(line, sourcePoint, 'x'); + if (!point || (first && last)) { + continue; + } + if (first) { + postponed.unshift(point); + } else { + points.push(point); + if (!last) { + break; + } + } + } + points.push(...postponed); +} +function findPoint(line, sourcePoint, property) { + const point = line.interpolate(sourcePoint, property); + if (!point) { + return {}; + } + const pointValue = point[property]; + const segments = line.segments; + const linePoints = line.points; + let first = false; + let last = false; + for (let i = 0; i < segments.length; i++) { + const segment = segments[i]; + const firstValue = linePoints[segment.start][property]; + const lastValue = linePoints[segment.end][property]; + if (_isBetween(pointValue, firstValue, lastValue)) { + first = pointValue === firstValue; + last = pointValue === lastValue; + break; + } + } + return {first, last, point}; +} + +class simpleArc { + constructor(opts) { + this.x = opts.x; + this.y = opts.y; + this.radius = opts.radius; + } + pathSegment(ctx, bounds, opts) { + const {x, y, radius} = this; + bounds = bounds || {start: 0, end: TAU}; + ctx.arc(x, y, radius, bounds.end, bounds.start, true); + return !opts.bounds; + } + interpolate(point) { + const {x, y, radius} = this; + const angle = point.angle; + return { + x: x + Math.cos(angle) * radius, + y: y + Math.sin(angle) * radius, + angle + }; + } +} + +function _getTarget(source) { + const {chart, fill, line} = source; + if (isNumberFinite(fill)) { + return getLineByIndex(chart, fill); + } + if (fill === 'stack') { + return _buildStackLine(source); + } + if (fill === 'shape') { + return true; + } + const boundary = computeBoundary(source); + if (boundary instanceof simpleArc) { + return boundary; + } + return _createBoundaryLine(boundary, line); +} +function getLineByIndex(chart, index) { + const meta = chart.getDatasetMeta(index); + const visible = meta && chart.isDatasetVisible(index); + return visible ? meta.dataset : null; +} +function computeBoundary(source) { + const scale = source.scale || {}; + if (scale.getPointPositionForValue) { + return computeCircularBoundary(source); + } + return computeLinearBoundary(source); +} +function computeLinearBoundary(source) { + const {scale = {}, fill} = source; + const pixel = _getTargetPixel(fill, scale); + if (isNumberFinite(pixel)) { + const horizontal = scale.isHorizontal(); + return { + x: horizontal ? pixel : null, + y: horizontal ? null : pixel + }; + } + return null; +} +function computeCircularBoundary(source) { + const {scale, fill} = source; + const options = scale.options; + const length = scale.getLabels().length; + const start = options.reverse ? scale.max : scale.min; + const value = _getTargetValue(fill, scale, start); + const target = []; + if (options.grid.circular) { + const center = scale.getPointPositionForValue(0, start); + return new simpleArc({ + x: center.x, + y: center.y, + radius: scale.getDistanceFromCenterForValue(value) + }); + } + for (let i = 0; i < length; ++i) { + target.push(scale.getPointPositionForValue(i, value)); + } + return target; +} + +function _drawfill(ctx, source, area) { + const target = _getTarget(source); + const {line, scale, axis} = source; + const lineOpts = line.options; + const fillOption = lineOpts.fill; + const color = lineOpts.backgroundColor; + const {above = color, below = color} = fillOption || {}; + if (target && line.points.length) { + clipArea(ctx, area); + doFill(ctx, {line, target, above, below, area, scale, axis}); + unclipArea(ctx); + } +} +function doFill(ctx, cfg) { + const {line, target, above, below, area, scale} = cfg; + const property = line._loop ? 'angle' : cfg.axis; + ctx.save(); + if (property === 'x' && below !== above) { + clipVertical(ctx, target, area.top); + fill(ctx, {line, target, color: above, scale, property}); + ctx.restore(); + ctx.save(); + clipVertical(ctx, target, area.bottom); + } + fill(ctx, {line, target, color: below, scale, property}); + ctx.restore(); +} +function clipVertical(ctx, target, clipY) { + const {segments, points} = target; + let first = true; + let lineLoop = false; + ctx.beginPath(); + for (const segment of segments) { + const {start, end} = segment; + const firstPoint = points[start]; + const lastPoint = points[_findSegmentEnd(start, end, points)]; + if (first) { + ctx.moveTo(firstPoint.x, firstPoint.y); + first = false; + } else { + ctx.lineTo(firstPoint.x, clipY); + ctx.lineTo(firstPoint.x, firstPoint.y); + } + lineLoop = !!target.pathSegment(ctx, segment, {move: lineLoop}); + if (lineLoop) { + ctx.closePath(); + } else { + ctx.lineTo(lastPoint.x, clipY); + } + } + ctx.lineTo(target.first().x, clipY); + ctx.closePath(); + ctx.clip(); +} +function fill(ctx, cfg) { + const {line, target, property, color, scale} = cfg; + const segments = _segments(line, target, property); + for (const {source: src, target: tgt, start, end} of segments) { + const {style: {backgroundColor = color} = {}} = src; + const notShape = target !== true; + ctx.save(); + ctx.fillStyle = backgroundColor; + clipBounds(ctx, scale, notShape && _getBounds(property, start, end)); + ctx.beginPath(); + const lineLoop = !!line.pathSegment(ctx, src); + let loop; + if (notShape) { + if (lineLoop) { + ctx.closePath(); + } else { + interpolatedLineTo(ctx, target, end, property); + } + const targetLoop = !!target.pathSegment(ctx, tgt, {move: lineLoop, reverse: true}); + loop = lineLoop && targetLoop; + if (!loop) { + interpolatedLineTo(ctx, target, start, property); + } + } + ctx.closePath(); + ctx.fill(loop ? 'evenodd' : 'nonzero'); + ctx.restore(); + } +} +function clipBounds(ctx, scale, bounds) { + const {top, bottom} = scale.chart.chartArea; + const {property, start, end} = bounds || {}; + if (property === 'x') { + ctx.beginPath(); + ctx.rect(start, top, end - start, bottom - top); + ctx.clip(); + } +} +function interpolatedLineTo(ctx, target, point, property) { + const interpolatedPoint = target.interpolate(point, property); + if (interpolatedPoint) { + ctx.lineTo(interpolatedPoint.x, interpolatedPoint.y); + } +} + +var index = { + id: 'filler', + afterDatasetsUpdate(chart, _args, options) { + const count = (chart.data.datasets || []).length; + const sources = []; + let meta, i, line, source; + for (i = 0; i < count; ++i) { + meta = chart.getDatasetMeta(i); + line = meta.dataset; + source = null; + if (line && line.options && line instanceof LineElement) { + source = { + visible: chart.isDatasetVisible(i), + index: i, + fill: _decodeFill(line, i, count), + chart, + axis: meta.controller.options.indexAxis, + scale: meta.vScale, + line, + }; + } + meta.$filler = source; + sources.push(source); + } + for (i = 0; i < count; ++i) { + source = sources[i]; + if (!source || source.fill === false) { + continue; + } + source.fill = _resolveTarget(sources, i, options.propagate); + } + }, + beforeDraw(chart, _args, options) { + const draw = options.drawTime === 'beforeDraw'; + const metasets = chart.getSortedVisibleDatasetMetas(); + const area = chart.chartArea; + for (let i = metasets.length - 1; i >= 0; --i) { + const source = metasets[i].$filler; + if (!source) { + continue; + } + source.line.updateControlPoints(area, source.axis); + if (draw && source.fill) { + _drawfill(chart.ctx, source, area); + } + } + }, + beforeDatasetsDraw(chart, _args, options) { + if (options.drawTime !== 'beforeDatasetsDraw') { + return; + } + const metasets = chart.getSortedVisibleDatasetMetas(); + for (let i = metasets.length - 1; i >= 0; --i) { + const source = metasets[i].$filler; + if (_shouldApplyFill(source)) { + _drawfill(chart.ctx, source, chart.chartArea); + } + } + }, + beforeDatasetDraw(chart, args, options) { + const source = args.meta.$filler; + if (!_shouldApplyFill(source) || options.drawTime !== 'beforeDatasetDraw') { + return; + } + _drawfill(chart.ctx, source, chart.chartArea); + }, + defaults: { + propagate: true, + drawTime: 'beforeDatasetDraw' + } +}; + +const getBoxSize = (labelOpts, fontSize) => { + let {boxHeight = fontSize, boxWidth = fontSize} = labelOpts; + if (labelOpts.usePointStyle) { + boxHeight = Math.min(boxHeight, fontSize); + boxWidth = labelOpts.pointStyleWidth || Math.min(boxWidth, fontSize); + } + return { + boxWidth, + boxHeight, + itemHeight: Math.max(fontSize, boxHeight) + }; +}; +const itemsEqual = (a, b) => a !== null && b !== null && a.datasetIndex === b.datasetIndex && a.index === b.index; +class Legend extends Element { + constructor(config) { + super(); + this._added = false; + this.legendHitBoxes = []; + this._hoveredItem = null; + this.doughnutMode = false; + this.chart = config.chart; + this.options = config.options; + this.ctx = config.ctx; + this.legendItems = undefined; + this.columnSizes = undefined; + this.lineWidths = undefined; + this.maxHeight = undefined; + this.maxWidth = undefined; + this.top = undefined; + this.bottom = undefined; + this.left = undefined; + this.right = undefined; + this.height = undefined; + this.width = undefined; + this._margins = undefined; + this.position = undefined; + this.weight = undefined; + this.fullSize = undefined; + } + update(maxWidth, maxHeight, margins) { + this.maxWidth = maxWidth; + this.maxHeight = maxHeight; + this._margins = margins; + this.setDimensions(); + this.buildLabels(); + this.fit(); + } + setDimensions() { + if (this.isHorizontal()) { + this.width = this.maxWidth; + this.left = this._margins.left; + this.right = this.width; + } else { + this.height = this.maxHeight; + this.top = this._margins.top; + this.bottom = this.height; + } + } + buildLabels() { + const labelOpts = this.options.labels || {}; + let legendItems = callback(labelOpts.generateLabels, [this.chart], this) || []; + if (labelOpts.filter) { + legendItems = legendItems.filter((item) => labelOpts.filter(item, this.chart.data)); + } + if (labelOpts.sort) { + legendItems = legendItems.sort((a, b) => labelOpts.sort(a, b, this.chart.data)); + } + if (this.options.reverse) { + legendItems.reverse(); + } + this.legendItems = legendItems; + } + fit() { + const {options, ctx} = this; + if (!options.display) { + this.width = this.height = 0; + return; + } + const labelOpts = options.labels; + const labelFont = toFont(labelOpts.font); + const fontSize = labelFont.size; + const titleHeight = this._computeTitleHeight(); + const {boxWidth, itemHeight} = getBoxSize(labelOpts, fontSize); + let width, height; + ctx.font = labelFont.string; + if (this.isHorizontal()) { + width = this.maxWidth; + height = this._fitRows(titleHeight, fontSize, boxWidth, itemHeight) + 10; + } else { + height = this.maxHeight; + width = this._fitCols(titleHeight, fontSize, boxWidth, itemHeight) + 10; + } + this.width = Math.min(width, options.maxWidth || this.maxWidth); + this.height = Math.min(height, options.maxHeight || this.maxHeight); + } + _fitRows(titleHeight, fontSize, boxWidth, itemHeight) { + const {ctx, maxWidth, options: {labels: {padding}}} = this; + const hitboxes = this.legendHitBoxes = []; + const lineWidths = this.lineWidths = [0]; + const lineHeight = itemHeight + padding; + let totalHeight = titleHeight; + ctx.textAlign = 'left'; + ctx.textBaseline = 'middle'; + let row = -1; + let top = -lineHeight; + this.legendItems.forEach((legendItem, i) => { + const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width; + if (i === 0 || lineWidths[lineWidths.length - 1] + itemWidth + 2 * padding > maxWidth) { + totalHeight += lineHeight; + lineWidths[lineWidths.length - (i > 0 ? 0 : 1)] = 0; + top += lineHeight; + row++; + } + hitboxes[i] = {left: 0, top, row, width: itemWidth, height: itemHeight}; + lineWidths[lineWidths.length - 1] += itemWidth + padding; + }); + return totalHeight; + } + _fitCols(titleHeight, fontSize, boxWidth, itemHeight) { + const {ctx, maxHeight, options: {labels: {padding}}} = this; + const hitboxes = this.legendHitBoxes = []; + const columnSizes = this.columnSizes = []; + const heightLimit = maxHeight - titleHeight; + let totalWidth = padding; + let currentColWidth = 0; + let currentColHeight = 0; + let left = 0; + let col = 0; + this.legendItems.forEach((legendItem, i) => { + const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width; + if (i > 0 && currentColHeight + itemHeight + 2 * padding > heightLimit) { + totalWidth += currentColWidth + padding; + columnSizes.push({width: currentColWidth, height: currentColHeight}); + left += currentColWidth + padding; + col++; + currentColWidth = currentColHeight = 0; + } + hitboxes[i] = {left, top: currentColHeight, col, width: itemWidth, height: itemHeight}; + currentColWidth = Math.max(currentColWidth, itemWidth); + currentColHeight += itemHeight + padding; + }); + totalWidth += currentColWidth; + columnSizes.push({width: currentColWidth, height: currentColHeight}); + return totalWidth; + } + adjustHitBoxes() { + if (!this.options.display) { + return; + } + const titleHeight = this._computeTitleHeight(); + const {legendHitBoxes: hitboxes, options: {align, labels: {padding}, rtl}} = this; + const rtlHelper = getRtlAdapter(rtl, this.left, this.width); + if (this.isHorizontal()) { + let row = 0; + let left = _alignStartEnd(align, this.left + padding, this.right - this.lineWidths[row]); + for (const hitbox of hitboxes) { + if (row !== hitbox.row) { + row = hitbox.row; + left = _alignStartEnd(align, this.left + padding, this.right - this.lineWidths[row]); + } + hitbox.top += this.top + titleHeight + padding; + hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(left), hitbox.width); + left += hitbox.width + padding; + } + } else { + let col = 0; + let top = _alignStartEnd(align, this.top + titleHeight + padding, this.bottom - this.columnSizes[col].height); + for (const hitbox of hitboxes) { + if (hitbox.col !== col) { + col = hitbox.col; + top = _alignStartEnd(align, this.top + titleHeight + padding, this.bottom - this.columnSizes[col].height); + } + hitbox.top = top; + hitbox.left += this.left + padding; + hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(hitbox.left), hitbox.width); + top += hitbox.height + padding; + } + } + } + isHorizontal() { + return this.options.position === 'top' || this.options.position === 'bottom'; + } + draw() { + if (this.options.display) { + const ctx = this.ctx; + clipArea(ctx, this); + this._draw(); + unclipArea(ctx); + } + } + _draw() { + const {options: opts, columnSizes, lineWidths, ctx} = this; + const {align, labels: labelOpts} = opts; + const defaultColor = defaults.color; + const rtlHelper = getRtlAdapter(opts.rtl, this.left, this.width); + const labelFont = toFont(labelOpts.font); + const {color: fontColor, padding} = labelOpts; + const fontSize = labelFont.size; + const halfFontSize = fontSize / 2; + let cursor; + this.drawTitle(); + ctx.textAlign = rtlHelper.textAlign('left'); + ctx.textBaseline = 'middle'; + ctx.lineWidth = 0.5; + ctx.font = labelFont.string; + const {boxWidth, boxHeight, itemHeight} = getBoxSize(labelOpts, fontSize); + const drawLegendBox = function(x, y, legendItem) { + if (isNaN(boxWidth) || boxWidth <= 0 || isNaN(boxHeight) || boxHeight < 0) { + return; + } + ctx.save(); + const lineWidth = valueOrDefault(legendItem.lineWidth, 1); + ctx.fillStyle = valueOrDefault(legendItem.fillStyle, defaultColor); + ctx.lineCap = valueOrDefault(legendItem.lineCap, 'butt'); + ctx.lineDashOffset = valueOrDefault(legendItem.lineDashOffset, 0); + ctx.lineJoin = valueOrDefault(legendItem.lineJoin, 'miter'); + ctx.lineWidth = lineWidth; + ctx.strokeStyle = valueOrDefault(legendItem.strokeStyle, defaultColor); + ctx.setLineDash(valueOrDefault(legendItem.lineDash, [])); + if (labelOpts.usePointStyle) { + const drawOptions = { + radius: boxHeight * Math.SQRT2 / 2, + pointStyle: legendItem.pointStyle, + rotation: legendItem.rotation, + borderWidth: lineWidth + }; + const centerX = rtlHelper.xPlus(x, boxWidth / 2); + const centerY = y + halfFontSize; + drawPointLegend(ctx, drawOptions, centerX, centerY, labelOpts.pointStyleWidth && boxWidth); + } else { + const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0); + const xBoxLeft = rtlHelper.leftForLtr(x, boxWidth); + const borderRadius = toTRBLCorners(legendItem.borderRadius); + ctx.beginPath(); + if (Object.values(borderRadius).some(v => v !== 0)) { + addRoundedRectPath(ctx, { + x: xBoxLeft, + y: yBoxTop, + w: boxWidth, + h: boxHeight, + radius: borderRadius, + }); + } else { + ctx.rect(xBoxLeft, yBoxTop, boxWidth, boxHeight); + } + ctx.fill(); + if (lineWidth !== 0) { + ctx.stroke(); + } + } + ctx.restore(); + }; + const fillText = function(x, y, legendItem) { + renderText(ctx, legendItem.text, x, y + (itemHeight / 2), labelFont, { + strikethrough: legendItem.hidden, + textAlign: rtlHelper.textAlign(legendItem.textAlign) + }); + }; + const isHorizontal = this.isHorizontal(); + const titleHeight = this._computeTitleHeight(); + if (isHorizontal) { + cursor = { + x: _alignStartEnd(align, this.left + padding, this.right - lineWidths[0]), + y: this.top + padding + titleHeight, + line: 0 + }; + } else { + cursor = { + x: this.left + padding, + y: _alignStartEnd(align, this.top + titleHeight + padding, this.bottom - columnSizes[0].height), + line: 0 + }; + } + overrideTextDirection(this.ctx, opts.textDirection); + const lineHeight = itemHeight + padding; + this.legendItems.forEach((legendItem, i) => { + ctx.strokeStyle = legendItem.fontColor || fontColor; + ctx.fillStyle = legendItem.fontColor || fontColor; + const textWidth = ctx.measureText(legendItem.text).width; + const textAlign = rtlHelper.textAlign(legendItem.textAlign || (legendItem.textAlign = labelOpts.textAlign)); + const width = boxWidth + halfFontSize + textWidth; + let x = cursor.x; + let y = cursor.y; + rtlHelper.setWidth(this.width); + if (isHorizontal) { + if (i > 0 && x + width + padding > this.right) { + y = cursor.y += lineHeight; + cursor.line++; + x = cursor.x = _alignStartEnd(align, this.left + padding, this.right - lineWidths[cursor.line]); + } + } else if (i > 0 && y + lineHeight > this.bottom) { + x = cursor.x = x + columnSizes[cursor.line].width + padding; + cursor.line++; + y = cursor.y = _alignStartEnd(align, this.top + titleHeight + padding, this.bottom - columnSizes[cursor.line].height); + } + const realX = rtlHelper.x(x); + drawLegendBox(realX, y, legendItem); + x = _textX(textAlign, x + boxWidth + halfFontSize, isHorizontal ? x + width : this.right, opts.rtl); + fillText(rtlHelper.x(x), y, legendItem); + if (isHorizontal) { + cursor.x += width + padding; + } else { + cursor.y += lineHeight; + } + }); + restoreTextDirection(this.ctx, opts.textDirection); + } + drawTitle() { + const opts = this.options; + const titleOpts = opts.title; + const titleFont = toFont(titleOpts.font); + const titlePadding = toPadding(titleOpts.padding); + if (!titleOpts.display) { + return; + } + const rtlHelper = getRtlAdapter(opts.rtl, this.left, this.width); + const ctx = this.ctx; + const position = titleOpts.position; + const halfFontSize = titleFont.size / 2; + const topPaddingPlusHalfFontSize = titlePadding.top + halfFontSize; + let y; + let left = this.left; + let maxWidth = this.width; + if (this.isHorizontal()) { + maxWidth = Math.max(...this.lineWidths); + y = this.top + topPaddingPlusHalfFontSize; + left = _alignStartEnd(opts.align, left, this.right - maxWidth); + } else { + const maxHeight = this.columnSizes.reduce((acc, size) => Math.max(acc, size.height), 0); + y = topPaddingPlusHalfFontSize + _alignStartEnd(opts.align, this.top, this.bottom - maxHeight - opts.labels.padding - this._computeTitleHeight()); + } + const x = _alignStartEnd(position, left, left + maxWidth); + ctx.textAlign = rtlHelper.textAlign(_toLeftRightCenter(position)); + ctx.textBaseline = 'middle'; + ctx.strokeStyle = titleOpts.color; + ctx.fillStyle = titleOpts.color; + ctx.font = titleFont.string; + renderText(ctx, titleOpts.text, x, y, titleFont); + } + _computeTitleHeight() { + const titleOpts = this.options.title; + const titleFont = toFont(titleOpts.font); + const titlePadding = toPadding(titleOpts.padding); + return titleOpts.display ? titleFont.lineHeight + titlePadding.height : 0; + } + _getLegendItemAt(x, y) { + let i, hitBox, lh; + if (_isBetween(x, this.left, this.right) + && _isBetween(y, this.top, this.bottom)) { + lh = this.legendHitBoxes; + for (i = 0; i < lh.length; ++i) { + hitBox = lh[i]; + if (_isBetween(x, hitBox.left, hitBox.left + hitBox.width) + && _isBetween(y, hitBox.top, hitBox.top + hitBox.height)) { + return this.legendItems[i]; + } + } + } + return null; + } + handleEvent(e) { + const opts = this.options; + if (!isListened(e.type, opts)) { + return; + } + const hoveredItem = this._getLegendItemAt(e.x, e.y); + if (e.type === 'mousemove' || e.type === 'mouseout') { + const previous = this._hoveredItem; + const sameItem = itemsEqual(previous, hoveredItem); + if (previous && !sameItem) { + callback(opts.onLeave, [e, previous, this], this); + } + this._hoveredItem = hoveredItem; + if (hoveredItem && !sameItem) { + callback(opts.onHover, [e, hoveredItem, this], this); + } + } else if (hoveredItem) { + callback(opts.onClick, [e, hoveredItem, this], this); + } + } +} +function isListened(type, opts) { + if ((type === 'mousemove' || type === 'mouseout') && (opts.onHover || opts.onLeave)) { + return true; + } + if (opts.onClick && (type === 'click' || type === 'mouseup')) { + return true; + } + return false; +} +var plugin_legend = { + id: 'legend', + _element: Legend, + start(chart, _args, options) { + const legend = chart.legend = new Legend({ctx: chart.ctx, options, chart}); + layouts.configure(chart, legend, options); + layouts.addBox(chart, legend); + }, + stop(chart) { + layouts.removeBox(chart, chart.legend); + delete chart.legend; + }, + beforeUpdate(chart, _args, options) { + const legend = chart.legend; + layouts.configure(chart, legend, options); + legend.options = options; + }, + afterUpdate(chart) { + const legend = chart.legend; + legend.buildLabels(); + legend.adjustHitBoxes(); + }, + afterEvent(chart, args) { + if (!args.replay) { + chart.legend.handleEvent(args.event); + } + }, + defaults: { + display: true, + position: 'top', + align: 'center', + fullSize: true, + reverse: false, + weight: 1000, + onClick(e, legendItem, legend) { + const index = legendItem.datasetIndex; + const ci = legend.chart; + if (ci.isDatasetVisible(index)) { + ci.hide(index); + legendItem.hidden = true; + } else { + ci.show(index); + legendItem.hidden = false; + } + }, + onHover: null, + onLeave: null, + labels: { + color: (ctx) => ctx.chart.options.color, + boxWidth: 40, + padding: 10, + generateLabels(chart) { + const datasets = chart.data.datasets; + const {labels: {usePointStyle, pointStyle, textAlign, color}} = chart.legend.options; + return chart._getSortedDatasetMetas().map((meta) => { + const style = meta.controller.getStyle(usePointStyle ? 0 : undefined); + const borderWidth = toPadding(style.borderWidth); + return { + text: datasets[meta.index].label, + fillStyle: style.backgroundColor, + fontColor: color, + hidden: !meta.visible, + lineCap: style.borderCapStyle, + lineDash: style.borderDash, + lineDashOffset: style.borderDashOffset, + lineJoin: style.borderJoinStyle, + lineWidth: (borderWidth.width + borderWidth.height) / 4, + strokeStyle: style.borderColor, + pointStyle: pointStyle || style.pointStyle, + rotation: style.rotation, + textAlign: textAlign || style.textAlign, + borderRadius: 0, + datasetIndex: meta.index + }; + }, this); + } + }, + title: { + color: (ctx) => ctx.chart.options.color, + display: false, + position: 'center', + text: '', + } + }, + descriptors: { + _scriptable: (name) => !name.startsWith('on'), + labels: { + _scriptable: (name) => !['generateLabels', 'filter', 'sort'].includes(name), + } + }, +}; + +class Title extends Element { + constructor(config) { + super(); + this.chart = config.chart; + this.options = config.options; + this.ctx = config.ctx; + this._padding = undefined; + this.top = undefined; + this.bottom = undefined; + this.left = undefined; + this.right = undefined; + this.width = undefined; + this.height = undefined; + this.position = undefined; + this.weight = undefined; + this.fullSize = undefined; + } + update(maxWidth, maxHeight) { + const opts = this.options; + this.left = 0; + this.top = 0; + if (!opts.display) { + this.width = this.height = this.right = this.bottom = 0; + return; + } + this.width = this.right = maxWidth; + this.height = this.bottom = maxHeight; + const lineCount = isArray(opts.text) ? opts.text.length : 1; + this._padding = toPadding(opts.padding); + const textSize = lineCount * toFont(opts.font).lineHeight + this._padding.height; + if (this.isHorizontal()) { + this.height = textSize; + } else { + this.width = textSize; + } + } + isHorizontal() { + const pos = this.options.position; + return pos === 'top' || pos === 'bottom'; + } + _drawArgs(offset) { + const {top, left, bottom, right, options} = this; + const align = options.align; + let rotation = 0; + let maxWidth, titleX, titleY; + if (this.isHorizontal()) { + titleX = _alignStartEnd(align, left, right); + titleY = top + offset; + maxWidth = right - left; + } else { + if (options.position === 'left') { + titleX = left + offset; + titleY = _alignStartEnd(align, bottom, top); + rotation = PI * -0.5; + } else { + titleX = right - offset; + titleY = _alignStartEnd(align, top, bottom); + rotation = PI * 0.5; + } + maxWidth = bottom - top; + } + return {titleX, titleY, maxWidth, rotation}; + } + draw() { + const ctx = this.ctx; + const opts = this.options; + if (!opts.display) { + return; + } + const fontOpts = toFont(opts.font); + const lineHeight = fontOpts.lineHeight; + const offset = lineHeight / 2 + this._padding.top; + const {titleX, titleY, maxWidth, rotation} = this._drawArgs(offset); + renderText(ctx, opts.text, 0, 0, fontOpts, { + color: opts.color, + maxWidth, + rotation, + textAlign: _toLeftRightCenter(opts.align), + textBaseline: 'middle', + translation: [titleX, titleY], + }); + } +} +function createTitle(chart, titleOpts) { + const title = new Title({ + ctx: chart.ctx, + options: titleOpts, + chart + }); + layouts.configure(chart, title, titleOpts); + layouts.addBox(chart, title); + chart.titleBlock = title; +} +var plugin_title = { + id: 'title', + _element: Title, + start(chart, _args, options) { + createTitle(chart, options); + }, + stop(chart) { + const titleBlock = chart.titleBlock; + layouts.removeBox(chart, titleBlock); + delete chart.titleBlock; + }, + beforeUpdate(chart, _args, options) { + const title = chart.titleBlock; + layouts.configure(chart, title, options); + title.options = options; + }, + defaults: { + align: 'center', + display: false, + font: { + weight: 'bold', + }, + fullSize: true, + padding: 10, + position: 'top', + text: '', + weight: 2000 + }, + defaultRoutes: { + color: 'color' + }, + descriptors: { + _scriptable: true, + _indexable: false, + }, +}; + +const map = new WeakMap(); +var plugin_subtitle = { + id: 'subtitle', + start(chart, _args, options) { + const title = new Title({ + ctx: chart.ctx, + options, + chart + }); + layouts.configure(chart, title, options); + layouts.addBox(chart, title); + map.set(chart, title); + }, + stop(chart) { + layouts.removeBox(chart, map.get(chart)); + map.delete(chart); + }, + beforeUpdate(chart, _args, options) { + const title = map.get(chart); + layouts.configure(chart, title, options); + title.options = options; + }, + defaults: { + align: 'center', + display: false, + font: { + weight: 'normal', + }, + fullSize: true, + padding: 0, + position: 'top', + text: '', + weight: 1500 + }, + defaultRoutes: { + color: 'color' + }, + descriptors: { + _scriptable: true, + _indexable: false, + }, +}; + +const positioners = { + average(items) { + if (!items.length) { + return false; + } + let i, len; + let x = 0; + let y = 0; + let count = 0; + for (i = 0, len = items.length; i < len; ++i) { + const el = items[i].element; + if (el && el.hasValue()) { + const pos = el.tooltipPosition(); + x += pos.x; + y += pos.y; + ++count; + } + } + return { + x: x / count, + y: y / count + }; + }, + nearest(items, eventPosition) { + if (!items.length) { + return false; + } + let x = eventPosition.x; + let y = eventPosition.y; + let minDistance = Number.POSITIVE_INFINITY; + let i, len, nearestElement; + for (i = 0, len = items.length; i < len; ++i) { + const el = items[i].element; + if (el && el.hasValue()) { + const center = el.getCenterPoint(); + const d = distanceBetweenPoints(eventPosition, center); + if (d < minDistance) { + minDistance = d; + nearestElement = el; + } + } + } + if (nearestElement) { + const tp = nearestElement.tooltipPosition(); + x = tp.x; + y = tp.y; + } + return { + x, + y + }; + } +}; +function pushOrConcat(base, toPush) { + if (toPush) { + if (isArray(toPush)) { + Array.prototype.push.apply(base, toPush); + } else { + base.push(toPush); + } + } + return base; +} +function splitNewlines(str) { + if ((typeof str === 'string' || str instanceof String) && str.indexOf('\n') > -1) { + return str.split('\n'); + } + return str; +} +function createTooltipItem(chart, item) { + const {element, datasetIndex, index} = item; + const controller = chart.getDatasetMeta(datasetIndex).controller; + const {label, value} = controller.getLabelAndValue(index); + return { + chart, + label, + parsed: controller.getParsed(index), + raw: chart.data.datasets[datasetIndex].data[index], + formattedValue: value, + dataset: controller.getDataset(), + dataIndex: index, + datasetIndex, + element + }; +} +function getTooltipSize(tooltip, options) { + const ctx = tooltip.chart.ctx; + const {body, footer, title} = tooltip; + const {boxWidth, boxHeight} = options; + const bodyFont = toFont(options.bodyFont); + const titleFont = toFont(options.titleFont); + const footerFont = toFont(options.footerFont); + const titleLineCount = title.length; + const footerLineCount = footer.length; + const bodyLineItemCount = body.length; + const padding = toPadding(options.padding); + let height = padding.height; + let width = 0; + let combinedBodyLength = body.reduce((count, bodyItem) => count + bodyItem.before.length + bodyItem.lines.length + bodyItem.after.length, 0); + combinedBodyLength += tooltip.beforeBody.length + tooltip.afterBody.length; + if (titleLineCount) { + height += titleLineCount * titleFont.lineHeight + + (titleLineCount - 1) * options.titleSpacing + + options.titleMarginBottom; + } + if (combinedBodyLength) { + const bodyLineHeight = options.displayColors ? Math.max(boxHeight, bodyFont.lineHeight) : bodyFont.lineHeight; + height += bodyLineItemCount * bodyLineHeight + + (combinedBodyLength - bodyLineItemCount) * bodyFont.lineHeight + + (combinedBodyLength - 1) * options.bodySpacing; + } + if (footerLineCount) { + height += options.footerMarginTop + + footerLineCount * footerFont.lineHeight + + (footerLineCount - 1) * options.footerSpacing; + } + let widthPadding = 0; + const maxLineWidth = function(line) { + width = Math.max(width, ctx.measureText(line).width + widthPadding); + }; + ctx.save(); + ctx.font = titleFont.string; + each(tooltip.title, maxLineWidth); + ctx.font = bodyFont.string; + each(tooltip.beforeBody.concat(tooltip.afterBody), maxLineWidth); + widthPadding = options.displayColors ? (boxWidth + 2 + options.boxPadding) : 0; + each(body, (bodyItem) => { + each(bodyItem.before, maxLineWidth); + each(bodyItem.lines, maxLineWidth); + each(bodyItem.after, maxLineWidth); + }); + widthPadding = 0; + ctx.font = footerFont.string; + each(tooltip.footer, maxLineWidth); + ctx.restore(); + width += padding.width; + return {width, height}; +} +function determineYAlign(chart, size) { + const {y, height} = size; + if (y < height / 2) { + return 'top'; + } else if (y > (chart.height - height / 2)) { + return 'bottom'; + } + return 'center'; +} +function doesNotFitWithAlign(xAlign, chart, options, size) { + const {x, width} = size; + const caret = options.caretSize + options.caretPadding; + if (xAlign === 'left' && x + width + caret > chart.width) { + return true; + } + if (xAlign === 'right' && x - width - caret < 0) { + return true; + } +} +function determineXAlign(chart, options, size, yAlign) { + const {x, width} = size; + const {width: chartWidth, chartArea: {left, right}} = chart; + let xAlign = 'center'; + if (yAlign === 'center') { + xAlign = x <= (left + right) / 2 ? 'left' : 'right'; + } else if (x <= width / 2) { + xAlign = 'left'; + } else if (x >= chartWidth - width / 2) { + xAlign = 'right'; + } + if (doesNotFitWithAlign(xAlign, chart, options, size)) { + xAlign = 'center'; + } + return xAlign; +} +function determineAlignment(chart, options, size) { + const yAlign = size.yAlign || options.yAlign || determineYAlign(chart, size); + return { + xAlign: size.xAlign || options.xAlign || determineXAlign(chart, options, size, yAlign), + yAlign + }; +} +function alignX(size, xAlign) { + let {x, width} = size; + if (xAlign === 'right') { + x -= width; + } else if (xAlign === 'center') { + x -= (width / 2); + } + return x; +} +function alignY(size, yAlign, paddingAndSize) { + let {y, height} = size; + if (yAlign === 'top') { + y += paddingAndSize; + } else if (yAlign === 'bottom') { + y -= height + paddingAndSize; + } else { + y -= (height / 2); + } + return y; +} +function getBackgroundPoint(options, size, alignment, chart) { + const {caretSize, caretPadding, cornerRadius} = options; + const {xAlign, yAlign} = alignment; + const paddingAndSize = caretSize + caretPadding; + const {topLeft, topRight, bottomLeft, bottomRight} = toTRBLCorners(cornerRadius); + let x = alignX(size, xAlign); + const y = alignY(size, yAlign, paddingAndSize); + if (yAlign === 'center') { + if (xAlign === 'left') { + x += paddingAndSize; + } else if (xAlign === 'right') { + x -= paddingAndSize; + } + } else if (xAlign === 'left') { + x -= Math.max(topLeft, bottomLeft) + caretSize; + } else if (xAlign === 'right') { + x += Math.max(topRight, bottomRight) + caretSize; + } + return { + x: _limitValue(x, 0, chart.width - size.width), + y: _limitValue(y, 0, chart.height - size.height) + }; +} +function getAlignedX(tooltip, align, options) { + const padding = toPadding(options.padding); + return align === 'center' + ? tooltip.x + tooltip.width / 2 + : align === 'right' + ? tooltip.x + tooltip.width - padding.right + : tooltip.x + padding.left; +} +function getBeforeAfterBodyLines(callback) { + return pushOrConcat([], splitNewlines(callback)); +} +function createTooltipContext(parent, tooltip, tooltipItems) { + return createContext(parent, { + tooltip, + tooltipItems, + type: 'tooltip' + }); +} +function overrideCallbacks(callbacks, context) { + const override = context && context.dataset && context.dataset.tooltip && context.dataset.tooltip.callbacks; + return override ? callbacks.override(override) : callbacks; +} +class Tooltip extends Element { + constructor(config) { + super(); + this.opacity = 0; + this._active = []; + this._eventPosition = undefined; + this._size = undefined; + this._cachedAnimations = undefined; + this._tooltipItems = []; + this.$animations = undefined; + this.$context = undefined; + this.chart = config.chart || config._chart; + this._chart = this.chart; + this.options = config.options; + this.dataPoints = undefined; + this.title = undefined; + this.beforeBody = undefined; + this.body = undefined; + this.afterBody = undefined; + this.footer = undefined; + this.xAlign = undefined; + this.yAlign = undefined; + this.x = undefined; + this.y = undefined; + this.height = undefined; + this.width = undefined; + this.caretX = undefined; + this.caretY = undefined; + this.labelColors = undefined; + this.labelPointStyles = undefined; + this.labelTextColors = undefined; + } + initialize(options) { + this.options = options; + this._cachedAnimations = undefined; + this.$context = undefined; + } + _resolveAnimations() { + const cached = this._cachedAnimations; + if (cached) { + return cached; + } + const chart = this.chart; + const options = this.options.setContext(this.getContext()); + const opts = options.enabled && chart.options.animation && options.animations; + const animations = new Animations(this.chart, opts); + if (opts._cacheable) { + this._cachedAnimations = Object.freeze(animations); + } + return animations; + } + getContext() { + return this.$context || + (this.$context = createTooltipContext(this.chart.getContext(), this, this._tooltipItems)); + } + getTitle(context, options) { + const {callbacks} = options; + const beforeTitle = callbacks.beforeTitle.apply(this, [context]); + const title = callbacks.title.apply(this, [context]); + const afterTitle = callbacks.afterTitle.apply(this, [context]); + let lines = []; + lines = pushOrConcat(lines, splitNewlines(beforeTitle)); + lines = pushOrConcat(lines, splitNewlines(title)); + lines = pushOrConcat(lines, splitNewlines(afterTitle)); + return lines; + } + getBeforeBody(tooltipItems, options) { + return getBeforeAfterBodyLines(options.callbacks.beforeBody.apply(this, [tooltipItems])); + } + getBody(tooltipItems, options) { + const {callbacks} = options; + const bodyItems = []; + each(tooltipItems, (context) => { + const bodyItem = { + before: [], + lines: [], + after: [] + }; + const scoped = overrideCallbacks(callbacks, context); + pushOrConcat(bodyItem.before, splitNewlines(scoped.beforeLabel.call(this, context))); + pushOrConcat(bodyItem.lines, scoped.label.call(this, context)); + pushOrConcat(bodyItem.after, splitNewlines(scoped.afterLabel.call(this, context))); + bodyItems.push(bodyItem); + }); + return bodyItems; + } + getAfterBody(tooltipItems, options) { + return getBeforeAfterBodyLines(options.callbacks.afterBody.apply(this, [tooltipItems])); + } + getFooter(tooltipItems, options) { + const {callbacks} = options; + const beforeFooter = callbacks.beforeFooter.apply(this, [tooltipItems]); + const footer = callbacks.footer.apply(this, [tooltipItems]); + const afterFooter = callbacks.afterFooter.apply(this, [tooltipItems]); + let lines = []; + lines = pushOrConcat(lines, splitNewlines(beforeFooter)); + lines = pushOrConcat(lines, splitNewlines(footer)); + lines = pushOrConcat(lines, splitNewlines(afterFooter)); + return lines; + } + _createItems(options) { + const active = this._active; + const data = this.chart.data; + const labelColors = []; + const labelPointStyles = []; + const labelTextColors = []; + let tooltipItems = []; + let i, len; + for (i = 0, len = active.length; i < len; ++i) { + tooltipItems.push(createTooltipItem(this.chart, active[i])); + } + if (options.filter) { + tooltipItems = tooltipItems.filter((element, index, array) => options.filter(element, index, array, data)); + } + if (options.itemSort) { + tooltipItems = tooltipItems.sort((a, b) => options.itemSort(a, b, data)); + } + each(tooltipItems, (context) => { + const scoped = overrideCallbacks(options.callbacks, context); + labelColors.push(scoped.labelColor.call(this, context)); + labelPointStyles.push(scoped.labelPointStyle.call(this, context)); + labelTextColors.push(scoped.labelTextColor.call(this, context)); + }); + this.labelColors = labelColors; + this.labelPointStyles = labelPointStyles; + this.labelTextColors = labelTextColors; + this.dataPoints = tooltipItems; + return tooltipItems; + } + update(changed, replay) { + const options = this.options.setContext(this.getContext()); + const active = this._active; + let properties; + let tooltipItems = []; + if (!active.length) { + if (this.opacity !== 0) { + properties = { + opacity: 0 + }; + } + } else { + const position = positioners[options.position].call(this, active, this._eventPosition); + tooltipItems = this._createItems(options); + this.title = this.getTitle(tooltipItems, options); + this.beforeBody = this.getBeforeBody(tooltipItems, options); + this.body = this.getBody(tooltipItems, options); + this.afterBody = this.getAfterBody(tooltipItems, options); + this.footer = this.getFooter(tooltipItems, options); + const size = this._size = getTooltipSize(this, options); + const positionAndSize = Object.assign({}, position, size); + const alignment = determineAlignment(this.chart, options, positionAndSize); + const backgroundPoint = getBackgroundPoint(options, positionAndSize, alignment, this.chart); + this.xAlign = alignment.xAlign; + this.yAlign = alignment.yAlign; + properties = { + opacity: 1, + x: backgroundPoint.x, + y: backgroundPoint.y, + width: size.width, + height: size.height, + caretX: position.x, + caretY: position.y + }; + } + this._tooltipItems = tooltipItems; + this.$context = undefined; + if (properties) { + this._resolveAnimations().update(this, properties); + } + if (changed && options.external) { + options.external.call(this, {chart: this.chart, tooltip: this, replay}); + } + } + drawCaret(tooltipPoint, ctx, size, options) { + const caretPosition = this.getCaretPosition(tooltipPoint, size, options); + ctx.lineTo(caretPosition.x1, caretPosition.y1); + ctx.lineTo(caretPosition.x2, caretPosition.y2); + ctx.lineTo(caretPosition.x3, caretPosition.y3); + } + getCaretPosition(tooltipPoint, size, options) { + const {xAlign, yAlign} = this; + const {caretSize, cornerRadius} = options; + const {topLeft, topRight, bottomLeft, bottomRight} = toTRBLCorners(cornerRadius); + const {x: ptX, y: ptY} = tooltipPoint; + const {width, height} = size; + let x1, x2, x3, y1, y2, y3; + if (yAlign === 'center') { + y2 = ptY + (height / 2); + if (xAlign === 'left') { + x1 = ptX; + x2 = x1 - caretSize; + y1 = y2 + caretSize; + y3 = y2 - caretSize; + } else { + x1 = ptX + width; + x2 = x1 + caretSize; + y1 = y2 - caretSize; + y3 = y2 + caretSize; + } + x3 = x1; + } else { + if (xAlign === 'left') { + x2 = ptX + Math.max(topLeft, bottomLeft) + (caretSize); + } else if (xAlign === 'right') { + x2 = ptX + width - Math.max(topRight, bottomRight) - caretSize; + } else { + x2 = this.caretX; + } + if (yAlign === 'top') { + y1 = ptY; + y2 = y1 - caretSize; + x1 = x2 - caretSize; + x3 = x2 + caretSize; + } else { + y1 = ptY + height; + y2 = y1 + caretSize; + x1 = x2 + caretSize; + x3 = x2 - caretSize; + } + y3 = y1; + } + return {x1, x2, x3, y1, y2, y3}; + } + drawTitle(pt, ctx, options) { + const title = this.title; + const length = title.length; + let titleFont, titleSpacing, i; + if (length) { + const rtlHelper = getRtlAdapter(options.rtl, this.x, this.width); + pt.x = getAlignedX(this, options.titleAlign, options); + ctx.textAlign = rtlHelper.textAlign(options.titleAlign); + ctx.textBaseline = 'middle'; + titleFont = toFont(options.titleFont); + titleSpacing = options.titleSpacing; + ctx.fillStyle = options.titleColor; + ctx.font = titleFont.string; + for (i = 0; i < length; ++i) { + ctx.fillText(title[i], rtlHelper.x(pt.x), pt.y + titleFont.lineHeight / 2); + pt.y += titleFont.lineHeight + titleSpacing; + if (i + 1 === length) { + pt.y += options.titleMarginBottom - titleSpacing; + } + } + } + } + _drawColorBox(ctx, pt, i, rtlHelper, options) { + const labelColors = this.labelColors[i]; + const labelPointStyle = this.labelPointStyles[i]; + const {boxHeight, boxWidth, boxPadding} = options; + const bodyFont = toFont(options.bodyFont); + const colorX = getAlignedX(this, 'left', options); + const rtlColorX = rtlHelper.x(colorX); + const yOffSet = boxHeight < bodyFont.lineHeight ? (bodyFont.lineHeight - boxHeight) / 2 : 0; + const colorY = pt.y + yOffSet; + if (options.usePointStyle) { + const drawOptions = { + radius: Math.min(boxWidth, boxHeight) / 2, + pointStyle: labelPointStyle.pointStyle, + rotation: labelPointStyle.rotation, + borderWidth: 1 + }; + const centerX = rtlHelper.leftForLtr(rtlColorX, boxWidth) + boxWidth / 2; + const centerY = colorY + boxHeight / 2; + ctx.strokeStyle = options.multiKeyBackground; + ctx.fillStyle = options.multiKeyBackground; + drawPoint(ctx, drawOptions, centerX, centerY); + ctx.strokeStyle = labelColors.borderColor; + ctx.fillStyle = labelColors.backgroundColor; + drawPoint(ctx, drawOptions, centerX, centerY); + } else { + ctx.lineWidth = isObject(labelColors.borderWidth) ? Math.max(...Object.values(labelColors.borderWidth)) : (labelColors.borderWidth || 1); + ctx.strokeStyle = labelColors.borderColor; + ctx.setLineDash(labelColors.borderDash || []); + ctx.lineDashOffset = labelColors.borderDashOffset || 0; + const outerX = rtlHelper.leftForLtr(rtlColorX, boxWidth - boxPadding); + const innerX = rtlHelper.leftForLtr(rtlHelper.xPlus(rtlColorX, 1), boxWidth - boxPadding - 2); + const borderRadius = toTRBLCorners(labelColors.borderRadius); + if (Object.values(borderRadius).some(v => v !== 0)) { + ctx.beginPath(); + ctx.fillStyle = options.multiKeyBackground; + addRoundedRectPath(ctx, { + x: outerX, + y: colorY, + w: boxWidth, + h: boxHeight, + radius: borderRadius, + }); + ctx.fill(); + ctx.stroke(); + ctx.fillStyle = labelColors.backgroundColor; + ctx.beginPath(); + addRoundedRectPath(ctx, { + x: innerX, + y: colorY + 1, + w: boxWidth - 2, + h: boxHeight - 2, + radius: borderRadius, + }); + ctx.fill(); + } else { + ctx.fillStyle = options.multiKeyBackground; + ctx.fillRect(outerX, colorY, boxWidth, boxHeight); + ctx.strokeRect(outerX, colorY, boxWidth, boxHeight); + ctx.fillStyle = labelColors.backgroundColor; + ctx.fillRect(innerX, colorY + 1, boxWidth - 2, boxHeight - 2); + } + } + ctx.fillStyle = this.labelTextColors[i]; + } + drawBody(pt, ctx, options) { + const {body} = this; + const {bodySpacing, bodyAlign, displayColors, boxHeight, boxWidth, boxPadding} = options; + const bodyFont = toFont(options.bodyFont); + let bodyLineHeight = bodyFont.lineHeight; + let xLinePadding = 0; + const rtlHelper = getRtlAdapter(options.rtl, this.x, this.width); + const fillLineOfText = function(line) { + ctx.fillText(line, rtlHelper.x(pt.x + xLinePadding), pt.y + bodyLineHeight / 2); + pt.y += bodyLineHeight + bodySpacing; + }; + const bodyAlignForCalculation = rtlHelper.textAlign(bodyAlign); + let bodyItem, textColor, lines, i, j, ilen, jlen; + ctx.textAlign = bodyAlign; + ctx.textBaseline = 'middle'; + ctx.font = bodyFont.string; + pt.x = getAlignedX(this, bodyAlignForCalculation, options); + ctx.fillStyle = options.bodyColor; + each(this.beforeBody, fillLineOfText); + xLinePadding = displayColors && bodyAlignForCalculation !== 'right' + ? bodyAlign === 'center' ? (boxWidth / 2 + boxPadding) : (boxWidth + 2 + boxPadding) + : 0; + for (i = 0, ilen = body.length; i < ilen; ++i) { + bodyItem = body[i]; + textColor = this.labelTextColors[i]; + ctx.fillStyle = textColor; + each(bodyItem.before, fillLineOfText); + lines = bodyItem.lines; + if (displayColors && lines.length) { + this._drawColorBox(ctx, pt, i, rtlHelper, options); + bodyLineHeight = Math.max(bodyFont.lineHeight, boxHeight); + } + for (j = 0, jlen = lines.length; j < jlen; ++j) { + fillLineOfText(lines[j]); + bodyLineHeight = bodyFont.lineHeight; + } + each(bodyItem.after, fillLineOfText); + } + xLinePadding = 0; + bodyLineHeight = bodyFont.lineHeight; + each(this.afterBody, fillLineOfText); + pt.y -= bodySpacing; + } + drawFooter(pt, ctx, options) { + const footer = this.footer; + const length = footer.length; + let footerFont, i; + if (length) { + const rtlHelper = getRtlAdapter(options.rtl, this.x, this.width); + pt.x = getAlignedX(this, options.footerAlign, options); + pt.y += options.footerMarginTop; + ctx.textAlign = rtlHelper.textAlign(options.footerAlign); + ctx.textBaseline = 'middle'; + footerFont = toFont(options.footerFont); + ctx.fillStyle = options.footerColor; + ctx.font = footerFont.string; + for (i = 0; i < length; ++i) { + ctx.fillText(footer[i], rtlHelper.x(pt.x), pt.y + footerFont.lineHeight / 2); + pt.y += footerFont.lineHeight + options.footerSpacing; + } + } + } + drawBackground(pt, ctx, tooltipSize, options) { + const {xAlign, yAlign} = this; + const {x, y} = pt; + const {width, height} = tooltipSize; + const {topLeft, topRight, bottomLeft, bottomRight} = toTRBLCorners(options.cornerRadius); + ctx.fillStyle = options.backgroundColor; + ctx.strokeStyle = options.borderColor; + ctx.lineWidth = options.borderWidth; + ctx.beginPath(); + ctx.moveTo(x + topLeft, y); + if (yAlign === 'top') { + this.drawCaret(pt, ctx, tooltipSize, options); + } + ctx.lineTo(x + width - topRight, y); + ctx.quadraticCurveTo(x + width, y, x + width, y + topRight); + if (yAlign === 'center' && xAlign === 'right') { + this.drawCaret(pt, ctx, tooltipSize, options); + } + ctx.lineTo(x + width, y + height - bottomRight); + ctx.quadraticCurveTo(x + width, y + height, x + width - bottomRight, y + height); + if (yAlign === 'bottom') { + this.drawCaret(pt, ctx, tooltipSize, options); + } + ctx.lineTo(x + bottomLeft, y + height); + ctx.quadraticCurveTo(x, y + height, x, y + height - bottomLeft); + if (yAlign === 'center' && xAlign === 'left') { + this.drawCaret(pt, ctx, tooltipSize, options); + } + ctx.lineTo(x, y + topLeft); + ctx.quadraticCurveTo(x, y, x + topLeft, y); + ctx.closePath(); + ctx.fill(); + if (options.borderWidth > 0) { + ctx.stroke(); + } + } + _updateAnimationTarget(options) { + const chart = this.chart; + const anims = this.$animations; + const animX = anims && anims.x; + const animY = anims && anims.y; + if (animX || animY) { + const position = positioners[options.position].call(this, this._active, this._eventPosition); + if (!position) { + return; + } + const size = this._size = getTooltipSize(this, options); + const positionAndSize = Object.assign({}, position, this._size); + const alignment = determineAlignment(chart, options, positionAndSize); + const point = getBackgroundPoint(options, positionAndSize, alignment, chart); + if (animX._to !== point.x || animY._to !== point.y) { + this.xAlign = alignment.xAlign; + this.yAlign = alignment.yAlign; + this.width = size.width; + this.height = size.height; + this.caretX = position.x; + this.caretY = position.y; + this._resolveAnimations().update(this, point); + } + } + } + _willRender() { + return !!this.opacity; + } + draw(ctx) { + const options = this.options.setContext(this.getContext()); + let opacity = this.opacity; + if (!opacity) { + return; + } + this._updateAnimationTarget(options); + const tooltipSize = { + width: this.width, + height: this.height + }; + const pt = { + x: this.x, + y: this.y + }; + opacity = Math.abs(opacity) < 1e-3 ? 0 : opacity; + const padding = toPadding(options.padding); + const hasTooltipContent = this.title.length || this.beforeBody.length || this.body.length || this.afterBody.length || this.footer.length; + if (options.enabled && hasTooltipContent) { + ctx.save(); + ctx.globalAlpha = opacity; + this.drawBackground(pt, ctx, tooltipSize, options); + overrideTextDirection(ctx, options.textDirection); + pt.y += padding.top; + this.drawTitle(pt, ctx, options); + this.drawBody(pt, ctx, options); + this.drawFooter(pt, ctx, options); + restoreTextDirection(ctx, options.textDirection); + ctx.restore(); + } + } + getActiveElements() { + return this._active || []; + } + setActiveElements(activeElements, eventPosition) { + const lastActive = this._active; + const active = activeElements.map(({datasetIndex, index}) => { + const meta = this.chart.getDatasetMeta(datasetIndex); + if (!meta) { + throw new Error('Cannot find a dataset at index ' + datasetIndex); + } + return { + datasetIndex, + element: meta.data[index], + index, + }; + }); + const changed = !_elementsEqual(lastActive, active); + const positionChanged = this._positionChanged(active, eventPosition); + if (changed || positionChanged) { + this._active = active; + this._eventPosition = eventPosition; + this._ignoreReplayEvents = true; + this.update(true); + } + } + handleEvent(e, replay, inChartArea = true) { + if (replay && this._ignoreReplayEvents) { + return false; + } + this._ignoreReplayEvents = false; + const options = this.options; + const lastActive = this._active || []; + const active = this._getActiveElements(e, lastActive, replay, inChartArea); + const positionChanged = this._positionChanged(active, e); + const changed = replay || !_elementsEqual(active, lastActive) || positionChanged; + if (changed) { + this._active = active; + if (options.enabled || options.external) { + this._eventPosition = { + x: e.x, + y: e.y + }; + this.update(true, replay); + } + } + return changed; + } + _getActiveElements(e, lastActive, replay, inChartArea) { + const options = this.options; + if (e.type === 'mouseout') { + return []; + } + if (!inChartArea) { + return lastActive; + } + const active = this.chart.getElementsAtEventForMode(e, options.mode, options, replay); + if (options.reverse) { + active.reverse(); + } + return active; + } + _positionChanged(active, e) { + const {caretX, caretY, options} = this; + const position = positioners[options.position].call(this, active, e); + return position !== false && (caretX !== position.x || caretY !== position.y); + } +} +Tooltip.positioners = positioners; +var plugin_tooltip = { + id: 'tooltip', + _element: Tooltip, + positioners, + afterInit(chart, _args, options) { + if (options) { + chart.tooltip = new Tooltip({chart, options}); + } + }, + beforeUpdate(chart, _args, options) { + if (chart.tooltip) { + chart.tooltip.initialize(options); + } + }, + reset(chart, _args, options) { + if (chart.tooltip) { + chart.tooltip.initialize(options); + } + }, + afterDraw(chart) { + const tooltip = chart.tooltip; + if (tooltip && tooltip._willRender()) { + const args = { + tooltip + }; + if (chart.notifyPlugins('beforeTooltipDraw', args) === false) { + return; + } + tooltip.draw(chart.ctx); + chart.notifyPlugins('afterTooltipDraw', args); + } + }, + afterEvent(chart, args) { + if (chart.tooltip) { + const useFinalPosition = args.replay; + if (chart.tooltip.handleEvent(args.event, useFinalPosition, args.inChartArea)) { + args.changed = true; + } + } + }, + defaults: { + enabled: true, + external: null, + position: 'average', + backgroundColor: 'rgba(0,0,0,0.8)', + titleColor: '#fff', + titleFont: { + weight: 'bold', + }, + titleSpacing: 2, + titleMarginBottom: 6, + titleAlign: 'left', + bodyColor: '#fff', + bodySpacing: 2, + bodyFont: { + }, + bodyAlign: 'left', + footerColor: '#fff', + footerSpacing: 2, + footerMarginTop: 6, + footerFont: { + weight: 'bold', + }, + footerAlign: 'left', + padding: 6, + caretPadding: 2, + caretSize: 5, + cornerRadius: 6, + boxHeight: (ctx, opts) => opts.bodyFont.size, + boxWidth: (ctx, opts) => opts.bodyFont.size, + multiKeyBackground: '#fff', + displayColors: true, + boxPadding: 0, + borderColor: 'rgba(0,0,0,0)', + borderWidth: 0, + animation: { + duration: 400, + easing: 'easeOutQuart', + }, + animations: { + numbers: { + type: 'number', + properties: ['x', 'y', 'width', 'height', 'caretX', 'caretY'], + }, + opacity: { + easing: 'linear', + duration: 200 + } + }, + callbacks: { + beforeTitle: noop, + title(tooltipItems) { + if (tooltipItems.length > 0) { + const item = tooltipItems[0]; + const labels = item.chart.data.labels; + const labelCount = labels ? labels.length : 0; + if (this && this.options && this.options.mode === 'dataset') { + return item.dataset.label || ''; + } else if (item.label) { + return item.label; + } else if (labelCount > 0 && item.dataIndex < labelCount) { + return labels[item.dataIndex]; + } + } + return ''; + }, + afterTitle: noop, + beforeBody: noop, + beforeLabel: noop, + label(tooltipItem) { + if (this && this.options && this.options.mode === 'dataset') { + return tooltipItem.label + ': ' + tooltipItem.formattedValue || tooltipItem.formattedValue; + } + let label = tooltipItem.dataset.label || ''; + if (label) { + label += ': '; + } + const value = tooltipItem.formattedValue; + if (!isNullOrUndef(value)) { + label += value; + } + return label; + }, + labelColor(tooltipItem) { + const meta = tooltipItem.chart.getDatasetMeta(tooltipItem.datasetIndex); + const options = meta.controller.getStyle(tooltipItem.dataIndex); + return { + borderColor: options.borderColor, + backgroundColor: options.backgroundColor, + borderWidth: options.borderWidth, + borderDash: options.borderDash, + borderDashOffset: options.borderDashOffset, + borderRadius: 0, + }; + }, + labelTextColor() { + return this.options.bodyColor; + }, + labelPointStyle(tooltipItem) { + const meta = tooltipItem.chart.getDatasetMeta(tooltipItem.datasetIndex); + const options = meta.controller.getStyle(tooltipItem.dataIndex); + return { + pointStyle: options.pointStyle, + rotation: options.rotation, + }; + }, + afterLabel: noop, + afterBody: noop, + beforeFooter: noop, + footer: noop, + afterFooter: noop + } + }, + defaultRoutes: { + bodyFont: 'font', + footerFont: 'font', + titleFont: 'font' + }, + descriptors: { + _scriptable: (name) => name !== 'filter' && name !== 'itemSort' && name !== 'external', + _indexable: false, + callbacks: { + _scriptable: false, + _indexable: false, + }, + animation: { + _fallback: false + }, + animations: { + _fallback: 'animation' + } + }, + additionalOptionScopes: ['interaction'] +}; + +var plugins = /*#__PURE__*/Object.freeze({ +__proto__: null, +Decimation: plugin_decimation, +Filler: index, +Legend: plugin_legend, +SubTitle: plugin_subtitle, +Title: plugin_title, +Tooltip: plugin_tooltip +}); + +const addIfString = (labels, raw, index, addedLabels) => { + if (typeof raw === 'string') { + index = labels.push(raw) - 1; + addedLabels.unshift({index, label: raw}); + } else if (isNaN(raw)) { + index = null; + } + return index; +}; +function findOrAddLabel(labels, raw, index, addedLabels) { + const first = labels.indexOf(raw); + if (first === -1) { + return addIfString(labels, raw, index, addedLabels); + } + const last = labels.lastIndexOf(raw); + return first !== last ? index : first; +} +const validIndex = (index, max) => index === null ? null : _limitValue(Math.round(index), 0, max); +class CategoryScale extends Scale { + constructor(cfg) { + super(cfg); + this._startValue = undefined; + this._valueRange = 0; + this._addedLabels = []; + } + init(scaleOptions) { + const added = this._addedLabels; + if (added.length) { + const labels = this.getLabels(); + for (const {index, label} of added) { + if (labels[index] === label) { + labels.splice(index, 1); + } + } + this._addedLabels = []; + } + super.init(scaleOptions); + } + parse(raw, index) { + if (isNullOrUndef(raw)) { + return null; + } + const labels = this.getLabels(); + index = isFinite(index) && labels[index] === raw ? index + : findOrAddLabel(labels, raw, valueOrDefault(index, raw), this._addedLabels); + return validIndex(index, labels.length - 1); + } + determineDataLimits() { + const {minDefined, maxDefined} = this.getUserBounds(); + let {min, max} = this.getMinMax(true); + if (this.options.bounds === 'ticks') { + if (!minDefined) { + min = 0; + } + if (!maxDefined) { + max = this.getLabels().length - 1; + } + } + this.min = min; + this.max = max; + } + buildTicks() { + const min = this.min; + const max = this.max; + const offset = this.options.offset; + const ticks = []; + let labels = this.getLabels(); + labels = (min === 0 && max === labels.length - 1) ? labels : labels.slice(min, max + 1); + this._valueRange = Math.max(labels.length - (offset ? 0 : 1), 1); + this._startValue = this.min - (offset ? 0.5 : 0); + for (let value = min; value <= max; value++) { + ticks.push({value}); + } + return ticks; + } + getLabelForValue(value) { + const labels = this.getLabels(); + if (value >= 0 && value < labels.length) { + return labels[value]; + } + return value; + } + configure() { + super.configure(); + if (!this.isHorizontal()) { + this._reversePixels = !this._reversePixels; + } + } + getPixelForValue(value) { + if (typeof value !== 'number') { + value = this.parse(value); + } + return value === null ? NaN : this.getPixelForDecimal((value - this._startValue) / this._valueRange); + } + getPixelForTick(index) { + const ticks = this.ticks; + if (index < 0 || index > ticks.length - 1) { + return null; + } + return this.getPixelForValue(ticks[index].value); + } + getValueForPixel(pixel) { + return Math.round(this._startValue + this.getDecimalForPixel(pixel) * this._valueRange); + } + getBasePixel() { + return this.bottom; + } +} +CategoryScale.id = 'category'; +CategoryScale.defaults = { + ticks: { + callback: CategoryScale.prototype.getLabelForValue + } +}; + +function generateTicks$1(generationOptions, dataRange) { + const ticks = []; + const MIN_SPACING = 1e-14; + const {bounds, step, min, max, precision, count, maxTicks, maxDigits, includeBounds} = generationOptions; + const unit = step || 1; + const maxSpaces = maxTicks - 1; + const {min: rmin, max: rmax} = dataRange; + const minDefined = !isNullOrUndef(min); + const maxDefined = !isNullOrUndef(max); + const countDefined = !isNullOrUndef(count); + const minSpacing = (rmax - rmin) / (maxDigits + 1); + let spacing = niceNum((rmax - rmin) / maxSpaces / unit) * unit; + let factor, niceMin, niceMax, numSpaces; + if (spacing < MIN_SPACING && !minDefined && !maxDefined) { + return [{value: rmin}, {value: rmax}]; + } + numSpaces = Math.ceil(rmax / spacing) - Math.floor(rmin / spacing); + if (numSpaces > maxSpaces) { + spacing = niceNum(numSpaces * spacing / maxSpaces / unit) * unit; + } + if (!isNullOrUndef(precision)) { + factor = Math.pow(10, precision); + spacing = Math.ceil(spacing * factor) / factor; + } + if (bounds === 'ticks') { + niceMin = Math.floor(rmin / spacing) * spacing; + niceMax = Math.ceil(rmax / spacing) * spacing; + } else { + niceMin = rmin; + niceMax = rmax; + } + if (minDefined && maxDefined && step && almostWhole((max - min) / step, spacing / 1000)) { + numSpaces = Math.round(Math.min((max - min) / spacing, maxTicks)); + spacing = (max - min) / numSpaces; + niceMin = min; + niceMax = max; + } else if (countDefined) { + niceMin = minDefined ? min : niceMin; + niceMax = maxDefined ? max : niceMax; + numSpaces = count - 1; + spacing = (niceMax - niceMin) / numSpaces; + } else { + numSpaces = (niceMax - niceMin) / spacing; + if (almostEquals(numSpaces, Math.round(numSpaces), spacing / 1000)) { + numSpaces = Math.round(numSpaces); + } else { + numSpaces = Math.ceil(numSpaces); + } + } + const decimalPlaces = Math.max( + _decimalPlaces(spacing), + _decimalPlaces(niceMin) + ); + factor = Math.pow(10, isNullOrUndef(precision) ? decimalPlaces : precision); + niceMin = Math.round(niceMin * factor) / factor; + niceMax = Math.round(niceMax * factor) / factor; + let j = 0; + if (minDefined) { + if (includeBounds && niceMin !== min) { + ticks.push({value: min}); + if (niceMin < min) { + j++; + } + if (almostEquals(Math.round((niceMin + j * spacing) * factor) / factor, min, relativeLabelSize(min, minSpacing, generationOptions))) { + j++; + } + } else if (niceMin < min) { + j++; + } + } + for (; j < numSpaces; ++j) { + ticks.push({value: Math.round((niceMin + j * spacing) * factor) / factor}); + } + if (maxDefined && includeBounds && niceMax !== max) { + if (ticks.length && almostEquals(ticks[ticks.length - 1].value, max, relativeLabelSize(max, minSpacing, generationOptions))) { + ticks[ticks.length - 1].value = max; + } else { + ticks.push({value: max}); + } + } else if (!maxDefined || niceMax === max) { + ticks.push({value: niceMax}); + } + return ticks; +} +function relativeLabelSize(value, minSpacing, {horizontal, minRotation}) { + const rad = toRadians(minRotation); + const ratio = (horizontal ? Math.sin(rad) : Math.cos(rad)) || 0.001; + const length = 0.75 * minSpacing * ('' + value).length; + return Math.min(minSpacing / ratio, length); +} +class LinearScaleBase extends Scale { + constructor(cfg) { + super(cfg); + this.start = undefined; + this.end = undefined; + this._startValue = undefined; + this._endValue = undefined; + this._valueRange = 0; + } + parse(raw, index) { + if (isNullOrUndef(raw)) { + return null; + } + if ((typeof raw === 'number' || raw instanceof Number) && !isFinite(+raw)) { + return null; + } + return +raw; + } + handleTickRangeOptions() { + const {beginAtZero} = this.options; + const {minDefined, maxDefined} = this.getUserBounds(); + let {min, max} = this; + const setMin = v => (min = minDefined ? min : v); + const setMax = v => (max = maxDefined ? max : v); + if (beginAtZero) { + const minSign = sign(min); + const maxSign = sign(max); + if (minSign < 0 && maxSign < 0) { + setMax(0); + } else if (minSign > 0 && maxSign > 0) { + setMin(0); + } + } + if (min === max) { + let offset = 1; + if (max >= Number.MAX_SAFE_INTEGER || min <= Number.MIN_SAFE_INTEGER) { + offset = Math.abs(max * 0.05); + } + setMax(max + offset); + if (!beginAtZero) { + setMin(min - offset); + } + } + this.min = min; + this.max = max; + } + getTickLimit() { + const tickOpts = this.options.ticks; + let {maxTicksLimit, stepSize} = tickOpts; + let maxTicks; + if (stepSize) { + maxTicks = Math.ceil(this.max / stepSize) - Math.floor(this.min / stepSize) + 1; + if (maxTicks > 1000) { + console.warn(`scales.${this.id}.ticks.stepSize: ${stepSize} would result generating up to ${maxTicks} ticks. Limiting to 1000.`); + maxTicks = 1000; + } + } else { + maxTicks = this.computeTickLimit(); + maxTicksLimit = maxTicksLimit || 11; + } + if (maxTicksLimit) { + maxTicks = Math.min(maxTicksLimit, maxTicks); + } + return maxTicks; + } + computeTickLimit() { + return Number.POSITIVE_INFINITY; + } + buildTicks() { + const opts = this.options; + const tickOpts = opts.ticks; + let maxTicks = this.getTickLimit(); + maxTicks = Math.max(2, maxTicks); + const numericGeneratorOptions = { + maxTicks, + bounds: opts.bounds, + min: opts.min, + max: opts.max, + precision: tickOpts.precision, + step: tickOpts.stepSize, + count: tickOpts.count, + maxDigits: this._maxDigits(), + horizontal: this.isHorizontal(), + minRotation: tickOpts.minRotation || 0, + includeBounds: tickOpts.includeBounds !== false + }; + const dataRange = this._range || this; + const ticks = generateTicks$1(numericGeneratorOptions, dataRange); + if (opts.bounds === 'ticks') { + _setMinAndMaxByKey(ticks, this, 'value'); + } + if (opts.reverse) { + ticks.reverse(); + this.start = this.max; + this.end = this.min; + } else { + this.start = this.min; + this.end = this.max; + } + return ticks; + } + configure() { + const ticks = this.ticks; + let start = this.min; + let end = this.max; + super.configure(); + if (this.options.offset && ticks.length) { + const offset = (end - start) / Math.max(ticks.length - 1, 1) / 2; + start -= offset; + end += offset; + } + this._startValue = start; + this._endValue = end; + this._valueRange = end - start; + } + getLabelForValue(value) { + return formatNumber(value, this.chart.options.locale, this.options.ticks.format); + } +} + +class LinearScale extends LinearScaleBase { + determineDataLimits() { + const {min, max} = this.getMinMax(true); + this.min = isNumberFinite(min) ? min : 0; + this.max = isNumberFinite(max) ? max : 1; + this.handleTickRangeOptions(); + } + computeTickLimit() { + const horizontal = this.isHorizontal(); + const length = horizontal ? this.width : this.height; + const minRotation = toRadians(this.options.ticks.minRotation); + const ratio = (horizontal ? Math.sin(minRotation) : Math.cos(minRotation)) || 0.001; + const tickFont = this._resolveTickFontOptions(0); + return Math.ceil(length / Math.min(40, tickFont.lineHeight / ratio)); + } + getPixelForValue(value) { + return value === null ? NaN : this.getPixelForDecimal((value - this._startValue) / this._valueRange); + } + getValueForPixel(pixel) { + return this._startValue + this.getDecimalForPixel(pixel) * this._valueRange; + } +} +LinearScale.id = 'linear'; +LinearScale.defaults = { + ticks: { + callback: Ticks.formatters.numeric + } +}; + +function isMajor(tickVal) { + const remain = tickVal / (Math.pow(10, Math.floor(log10(tickVal)))); + return remain === 1; +} +function generateTicks(generationOptions, dataRange) { + const endExp = Math.floor(log10(dataRange.max)); + const endSignificand = Math.ceil(dataRange.max / Math.pow(10, endExp)); + const ticks = []; + let tickVal = finiteOrDefault(generationOptions.min, Math.pow(10, Math.floor(log10(dataRange.min)))); + let exp = Math.floor(log10(tickVal)); + let significand = Math.floor(tickVal / Math.pow(10, exp)); + let precision = exp < 0 ? Math.pow(10, Math.abs(exp)) : 1; + do { + ticks.push({value: tickVal, major: isMajor(tickVal)}); + ++significand; + if (significand === 10) { + significand = 1; + ++exp; + precision = exp >= 0 ? 1 : precision; + } + tickVal = Math.round(significand * Math.pow(10, exp) * precision) / precision; + } while (exp < endExp || (exp === endExp && significand < endSignificand)); + const lastTick = finiteOrDefault(generationOptions.max, tickVal); + ticks.push({value: lastTick, major: isMajor(tickVal)}); + return ticks; +} +class LogarithmicScale extends Scale { + constructor(cfg) { + super(cfg); + this.start = undefined; + this.end = undefined; + this._startValue = undefined; + this._valueRange = 0; + } + parse(raw, index) { + const value = LinearScaleBase.prototype.parse.apply(this, [raw, index]); + if (value === 0) { + this._zero = true; + return undefined; + } + return isNumberFinite(value) && value > 0 ? value : null; + } + determineDataLimits() { + const {min, max} = this.getMinMax(true); + this.min = isNumberFinite(min) ? Math.max(0, min) : null; + this.max = isNumberFinite(max) ? Math.max(0, max) : null; + if (this.options.beginAtZero) { + this._zero = true; + } + this.handleTickRangeOptions(); + } + handleTickRangeOptions() { + const {minDefined, maxDefined} = this.getUserBounds(); + let min = this.min; + let max = this.max; + const setMin = v => (min = minDefined ? min : v); + const setMax = v => (max = maxDefined ? max : v); + const exp = (v, m) => Math.pow(10, Math.floor(log10(v)) + m); + if (min === max) { + if (min <= 0) { + setMin(1); + setMax(10); + } else { + setMin(exp(min, -1)); + setMax(exp(max, +1)); + } + } + if (min <= 0) { + setMin(exp(max, -1)); + } + if (max <= 0) { + setMax(exp(min, +1)); + } + if (this._zero && this.min !== this._suggestedMin && min === exp(this.min, 0)) { + setMin(exp(min, -1)); + } + this.min = min; + this.max = max; + } + buildTicks() { + const opts = this.options; + const generationOptions = { + min: this._userMin, + max: this._userMax + }; + const ticks = generateTicks(generationOptions, this); + if (opts.bounds === 'ticks') { + _setMinAndMaxByKey(ticks, this, 'value'); + } + if (opts.reverse) { + ticks.reverse(); + this.start = this.max; + this.end = this.min; + } else { + this.start = this.min; + this.end = this.max; + } + return ticks; + } + getLabelForValue(value) { + return value === undefined + ? '0' + : formatNumber(value, this.chart.options.locale, this.options.ticks.format); + } + configure() { + const start = this.min; + super.configure(); + this._startValue = log10(start); + this._valueRange = log10(this.max) - log10(start); + } + getPixelForValue(value) { + if (value === undefined || value === 0) { + value = this.min; + } + if (value === null || isNaN(value)) { + return NaN; + } + return this.getPixelForDecimal(value === this.min + ? 0 + : (log10(value) - this._startValue) / this._valueRange); + } + getValueForPixel(pixel) { + const decimal = this.getDecimalForPixel(pixel); + return Math.pow(10, this._startValue + decimal * this._valueRange); + } +} +LogarithmicScale.id = 'logarithmic'; +LogarithmicScale.defaults = { + ticks: { + callback: Ticks.formatters.logarithmic, + major: { + enabled: true + } + } +}; + +function getTickBackdropHeight(opts) { + const tickOpts = opts.ticks; + if (tickOpts.display && opts.display) { + const padding = toPadding(tickOpts.backdropPadding); + return valueOrDefault(tickOpts.font && tickOpts.font.size, defaults.font.size) + padding.height; + } + return 0; +} +function measureLabelSize(ctx, font, label) { + label = isArray(label) ? label : [label]; + return { + w: _longestText(ctx, font.string, label), + h: label.length * font.lineHeight + }; +} +function determineLimits(angle, pos, size, min, max) { + if (angle === min || angle === max) { + return { + start: pos - (size / 2), + end: pos + (size / 2) + }; + } else if (angle < min || angle > max) { + return { + start: pos - size, + end: pos + }; + } + return { + start: pos, + end: pos + size + }; +} +function fitWithPointLabels(scale) { + const orig = { + l: scale.left + scale._padding.left, + r: scale.right - scale._padding.right, + t: scale.top + scale._padding.top, + b: scale.bottom - scale._padding.bottom + }; + const limits = Object.assign({}, orig); + const labelSizes = []; + const padding = []; + const valueCount = scale._pointLabels.length; + const pointLabelOpts = scale.options.pointLabels; + const additionalAngle = pointLabelOpts.centerPointLabels ? PI / valueCount : 0; + for (let i = 0; i < valueCount; i++) { + const opts = pointLabelOpts.setContext(scale.getPointLabelContext(i)); + padding[i] = opts.padding; + const pointPosition = scale.getPointPosition(i, scale.drawingArea + padding[i], additionalAngle); + const plFont = toFont(opts.font); + const textSize = measureLabelSize(scale.ctx, plFont, scale._pointLabels[i]); + labelSizes[i] = textSize; + const angleRadians = _normalizeAngle(scale.getIndexAngle(i) + additionalAngle); + const angle = Math.round(toDegrees(angleRadians)); + const hLimits = determineLimits(angle, pointPosition.x, textSize.w, 0, 180); + const vLimits = determineLimits(angle, pointPosition.y, textSize.h, 90, 270); + updateLimits(limits, orig, angleRadians, hLimits, vLimits); + } + scale.setCenterPoint( + orig.l - limits.l, + limits.r - orig.r, + orig.t - limits.t, + limits.b - orig.b + ); + scale._pointLabelItems = buildPointLabelItems(scale, labelSizes, padding); +} +function updateLimits(limits, orig, angle, hLimits, vLimits) { + const sin = Math.abs(Math.sin(angle)); + const cos = Math.abs(Math.cos(angle)); + let x = 0; + let y = 0; + if (hLimits.start < orig.l) { + x = (orig.l - hLimits.start) / sin; + limits.l = Math.min(limits.l, orig.l - x); + } else if (hLimits.end > orig.r) { + x = (hLimits.end - orig.r) / sin; + limits.r = Math.max(limits.r, orig.r + x); + } + if (vLimits.start < orig.t) { + y = (orig.t - vLimits.start) / cos; + limits.t = Math.min(limits.t, orig.t - y); + } else if (vLimits.end > orig.b) { + y = (vLimits.end - orig.b) / cos; + limits.b = Math.max(limits.b, orig.b + y); + } +} +function buildPointLabelItems(scale, labelSizes, padding) { + const items = []; + const valueCount = scale._pointLabels.length; + const opts = scale.options; + const extra = getTickBackdropHeight(opts) / 2; + const outerDistance = scale.drawingArea; + const additionalAngle = opts.pointLabels.centerPointLabels ? PI / valueCount : 0; + for (let i = 0; i < valueCount; i++) { + const pointLabelPosition = scale.getPointPosition(i, outerDistance + extra + padding[i], additionalAngle); + const angle = Math.round(toDegrees(_normalizeAngle(pointLabelPosition.angle + HALF_PI))); + const size = labelSizes[i]; + const y = yForAngle(pointLabelPosition.y, size.h, angle); + const textAlign = getTextAlignForAngle(angle); + const left = leftForTextAlign(pointLabelPosition.x, size.w, textAlign); + items.push({ + x: pointLabelPosition.x, + y, + textAlign, + left, + top: y, + right: left + size.w, + bottom: y + size.h + }); + } + return items; +} +function getTextAlignForAngle(angle) { + if (angle === 0 || angle === 180) { + return 'center'; + } else if (angle < 180) { + return 'left'; + } + return 'right'; +} +function leftForTextAlign(x, w, align) { + if (align === 'right') { + x -= w; + } else if (align === 'center') { + x -= (w / 2); + } + return x; +} +function yForAngle(y, h, angle) { + if (angle === 90 || angle === 270) { + y -= (h / 2); + } else if (angle > 270 || angle < 90) { + y -= h; + } + return y; +} +function drawPointLabels(scale, labelCount) { + const {ctx, options: {pointLabels}} = scale; + for (let i = labelCount - 1; i >= 0; i--) { + const optsAtIndex = pointLabels.setContext(scale.getPointLabelContext(i)); + const plFont = toFont(optsAtIndex.font); + const {x, y, textAlign, left, top, right, bottom} = scale._pointLabelItems[i]; + const {backdropColor} = optsAtIndex; + if (!isNullOrUndef(backdropColor)) { + const borderRadius = toTRBLCorners(optsAtIndex.borderRadius); + const padding = toPadding(optsAtIndex.backdropPadding); + ctx.fillStyle = backdropColor; + const backdropLeft = left - padding.left; + const backdropTop = top - padding.top; + const backdropWidth = right - left + padding.width; + const backdropHeight = bottom - top + padding.height; + if (Object.values(borderRadius).some(v => v !== 0)) { + ctx.beginPath(); + addRoundedRectPath(ctx, { + x: backdropLeft, + y: backdropTop, + w: backdropWidth, + h: backdropHeight, + radius: borderRadius, + }); + ctx.fill(); + } else { + ctx.fillRect(backdropLeft, backdropTop, backdropWidth, backdropHeight); + } + } + renderText( + ctx, + scale._pointLabels[i], + x, + y + (plFont.lineHeight / 2), + plFont, + { + color: optsAtIndex.color, + textAlign: textAlign, + textBaseline: 'middle' + } + ); + } +} +function pathRadiusLine(scale, radius, circular, labelCount) { + const {ctx} = scale; + if (circular) { + ctx.arc(scale.xCenter, scale.yCenter, radius, 0, TAU); + } else { + let pointPosition = scale.getPointPosition(0, radius); + ctx.moveTo(pointPosition.x, pointPosition.y); + for (let i = 1; i < labelCount; i++) { + pointPosition = scale.getPointPosition(i, radius); + ctx.lineTo(pointPosition.x, pointPosition.y); + } + } +} +function drawRadiusLine(scale, gridLineOpts, radius, labelCount) { + const ctx = scale.ctx; + const circular = gridLineOpts.circular; + const {color, lineWidth} = gridLineOpts; + if ((!circular && !labelCount) || !color || !lineWidth || radius < 0) { + return; + } + ctx.save(); + ctx.strokeStyle = color; + ctx.lineWidth = lineWidth; + ctx.setLineDash(gridLineOpts.borderDash); + ctx.lineDashOffset = gridLineOpts.borderDashOffset; + ctx.beginPath(); + pathRadiusLine(scale, radius, circular, labelCount); + ctx.closePath(); + ctx.stroke(); + ctx.restore(); +} +function createPointLabelContext(parent, index, label) { + return createContext(parent, { + label, + index, + type: 'pointLabel' + }); +} +class RadialLinearScale extends LinearScaleBase { + constructor(cfg) { + super(cfg); + this.xCenter = undefined; + this.yCenter = undefined; + this.drawingArea = undefined; + this._pointLabels = []; + this._pointLabelItems = []; + } + setDimensions() { + const padding = this._padding = toPadding(getTickBackdropHeight(this.options) / 2); + const w = this.width = this.maxWidth - padding.width; + const h = this.height = this.maxHeight - padding.height; + this.xCenter = Math.floor(this.left + w / 2 + padding.left); + this.yCenter = Math.floor(this.top + h / 2 + padding.top); + this.drawingArea = Math.floor(Math.min(w, h) / 2); + } + determineDataLimits() { + const {min, max} = this.getMinMax(false); + this.min = isNumberFinite(min) && !isNaN(min) ? min : 0; + this.max = isNumberFinite(max) && !isNaN(max) ? max : 0; + this.handleTickRangeOptions(); + } + computeTickLimit() { + return Math.ceil(this.drawingArea / getTickBackdropHeight(this.options)); + } + generateTickLabels(ticks) { + LinearScaleBase.prototype.generateTickLabels.call(this, ticks); + this._pointLabels = this.getLabels() + .map((value, index) => { + const label = callback(this.options.pointLabels.callback, [value, index], this); + return label || label === 0 ? label : ''; + }) + .filter((v, i) => this.chart.getDataVisibility(i)); + } + fit() { + const opts = this.options; + if (opts.display && opts.pointLabels.display) { + fitWithPointLabels(this); + } else { + this.setCenterPoint(0, 0, 0, 0); + } + } + setCenterPoint(leftMovement, rightMovement, topMovement, bottomMovement) { + this.xCenter += Math.floor((leftMovement - rightMovement) / 2); + this.yCenter += Math.floor((topMovement - bottomMovement) / 2); + this.drawingArea -= Math.min(this.drawingArea / 2, Math.max(leftMovement, rightMovement, topMovement, bottomMovement)); + } + getIndexAngle(index) { + const angleMultiplier = TAU / (this._pointLabels.length || 1); + const startAngle = this.options.startAngle || 0; + return _normalizeAngle(index * angleMultiplier + toRadians(startAngle)); + } + getDistanceFromCenterForValue(value) { + if (isNullOrUndef(value)) { + return NaN; + } + const scalingFactor = this.drawingArea / (this.max - this.min); + if (this.options.reverse) { + return (this.max - value) * scalingFactor; + } + return (value - this.min) * scalingFactor; + } + getValueForDistanceFromCenter(distance) { + if (isNullOrUndef(distance)) { + return NaN; + } + const scaledDistance = distance / (this.drawingArea / (this.max - this.min)); + return this.options.reverse ? this.max - scaledDistance : this.min + scaledDistance; + } + getPointLabelContext(index) { + const pointLabels = this._pointLabels || []; + if (index >= 0 && index < pointLabels.length) { + const pointLabel = pointLabels[index]; + return createPointLabelContext(this.getContext(), index, pointLabel); + } + } + getPointPosition(index, distanceFromCenter, additionalAngle = 0) { + const angle = this.getIndexAngle(index) - HALF_PI + additionalAngle; + return { + x: Math.cos(angle) * distanceFromCenter + this.xCenter, + y: Math.sin(angle) * distanceFromCenter + this.yCenter, + angle + }; + } + getPointPositionForValue(index, value) { + return this.getPointPosition(index, this.getDistanceFromCenterForValue(value)); + } + getBasePosition(index) { + return this.getPointPositionForValue(index || 0, this.getBaseValue()); + } + getPointLabelPosition(index) { + const {left, top, right, bottom} = this._pointLabelItems[index]; + return { + left, + top, + right, + bottom, + }; + } + drawBackground() { + const {backgroundColor, grid: {circular}} = this.options; + if (backgroundColor) { + const ctx = this.ctx; + ctx.save(); + ctx.beginPath(); + pathRadiusLine(this, this.getDistanceFromCenterForValue(this._endValue), circular, this._pointLabels.length); + ctx.closePath(); + ctx.fillStyle = backgroundColor; + ctx.fill(); + ctx.restore(); + } + } + drawGrid() { + const ctx = this.ctx; + const opts = this.options; + const {angleLines, grid} = opts; + const labelCount = this._pointLabels.length; + let i, offset, position; + if (opts.pointLabels.display) { + drawPointLabels(this, labelCount); + } + if (grid.display) { + this.ticks.forEach((tick, index) => { + if (index !== 0) { + offset = this.getDistanceFromCenterForValue(tick.value); + const optsAtIndex = grid.setContext(this.getContext(index - 1)); + drawRadiusLine(this, optsAtIndex, offset, labelCount); + } + }); + } + if (angleLines.display) { + ctx.save(); + for (i = labelCount - 1; i >= 0; i--) { + const optsAtIndex = angleLines.setContext(this.getPointLabelContext(i)); + const {color, lineWidth} = optsAtIndex; + if (!lineWidth || !color) { + continue; + } + ctx.lineWidth = lineWidth; + ctx.strokeStyle = color; + ctx.setLineDash(optsAtIndex.borderDash); + ctx.lineDashOffset = optsAtIndex.borderDashOffset; + offset = this.getDistanceFromCenterForValue(opts.ticks.reverse ? this.min : this.max); + position = this.getPointPosition(i, offset); + ctx.beginPath(); + ctx.moveTo(this.xCenter, this.yCenter); + ctx.lineTo(position.x, position.y); + ctx.stroke(); + } + ctx.restore(); + } + } + drawBorder() {} + drawLabels() { + const ctx = this.ctx; + const opts = this.options; + const tickOpts = opts.ticks; + if (!tickOpts.display) { + return; + } + const startAngle = this.getIndexAngle(0); + let offset, width; + ctx.save(); + ctx.translate(this.xCenter, this.yCenter); + ctx.rotate(startAngle); + ctx.textAlign = 'center'; + ctx.textBaseline = 'middle'; + this.ticks.forEach((tick, index) => { + if (index === 0 && !opts.reverse) { + return; + } + const optsAtIndex = tickOpts.setContext(this.getContext(index)); + const tickFont = toFont(optsAtIndex.font); + offset = this.getDistanceFromCenterForValue(this.ticks[index].value); + if (optsAtIndex.showLabelBackdrop) { + ctx.font = tickFont.string; + width = ctx.measureText(tick.label).width; + ctx.fillStyle = optsAtIndex.backdropColor; + const padding = toPadding(optsAtIndex.backdropPadding); + ctx.fillRect( + -width / 2 - padding.left, + -offset - tickFont.size / 2 - padding.top, + width + padding.width, + tickFont.size + padding.height + ); + } + renderText(ctx, tick.label, 0, -offset, tickFont, { + color: optsAtIndex.color, + }); + }); + ctx.restore(); + } + drawTitle() {} +} +RadialLinearScale.id = 'radialLinear'; +RadialLinearScale.defaults = { + display: true, + animate: true, + position: 'chartArea', + angleLines: { + display: true, + lineWidth: 1, + borderDash: [], + borderDashOffset: 0.0 + }, + grid: { + circular: false + }, + startAngle: 0, + ticks: { + showLabelBackdrop: true, + callback: Ticks.formatters.numeric + }, + pointLabels: { + backdropColor: undefined, + backdropPadding: 2, + display: true, + font: { + size: 10 + }, + callback(label) { + return label; + }, + padding: 5, + centerPointLabels: false + } +}; +RadialLinearScale.defaultRoutes = { + 'angleLines.color': 'borderColor', + 'pointLabels.color': 'color', + 'ticks.color': 'color' +}; +RadialLinearScale.descriptors = { + angleLines: { + _fallback: 'grid' + } +}; + +const INTERVALS = { + millisecond: {common: true, size: 1, steps: 1000}, + second: {common: true, size: 1000, steps: 60}, + minute: {common: true, size: 60000, steps: 60}, + hour: {common: true, size: 3600000, steps: 24}, + day: {common: true, size: 86400000, steps: 30}, + week: {common: false, size: 604800000, steps: 4}, + month: {common: true, size: 2.628e9, steps: 12}, + quarter: {common: false, size: 7.884e9, steps: 4}, + year: {common: true, size: 3.154e10} +}; +const UNITS = (Object.keys(INTERVALS)); +function sorter(a, b) { + return a - b; +} +function parse(scale, input) { + if (isNullOrUndef(input)) { + return null; + } + const adapter = scale._adapter; + const {parser, round, isoWeekday} = scale._parseOpts; + let value = input; + if (typeof parser === 'function') { + value = parser(value); + } + if (!isNumberFinite(value)) { + value = typeof parser === 'string' + ? adapter.parse(value, parser) + : adapter.parse(value); + } + if (value === null) { + return null; + } + if (round) { + value = round === 'week' && (isNumber(isoWeekday) || isoWeekday === true) + ? adapter.startOf(value, 'isoWeek', isoWeekday) + : adapter.startOf(value, round); + } + return +value; +} +function determineUnitForAutoTicks(minUnit, min, max, capacity) { + const ilen = UNITS.length; + for (let i = UNITS.indexOf(minUnit); i < ilen - 1; ++i) { + const interval = INTERVALS[UNITS[i]]; + const factor = interval.steps ? interval.steps : Number.MAX_SAFE_INTEGER; + if (interval.common && Math.ceil((max - min) / (factor * interval.size)) <= capacity) { + return UNITS[i]; + } + } + return UNITS[ilen - 1]; +} +function determineUnitForFormatting(scale, numTicks, minUnit, min, max) { + for (let i = UNITS.length - 1; i >= UNITS.indexOf(minUnit); i--) { + const unit = UNITS[i]; + if (INTERVALS[unit].common && scale._adapter.diff(max, min, unit) >= numTicks - 1) { + return unit; + } + } + return UNITS[minUnit ? UNITS.indexOf(minUnit) : 0]; +} +function determineMajorUnit(unit) { + for (let i = UNITS.indexOf(unit) + 1, ilen = UNITS.length; i < ilen; ++i) { + if (INTERVALS[UNITS[i]].common) { + return UNITS[i]; + } + } +} +function addTick(ticks, time, timestamps) { + if (!timestamps) { + ticks[time] = true; + } else if (timestamps.length) { + const {lo, hi} = _lookup(timestamps, time); + const timestamp = timestamps[lo] >= time ? timestamps[lo] : timestamps[hi]; + ticks[timestamp] = true; + } +} +function setMajorTicks(scale, ticks, map, majorUnit) { + const adapter = scale._adapter; + const first = +adapter.startOf(ticks[0].value, majorUnit); + const last = ticks[ticks.length - 1].value; + let major, index; + for (major = first; major <= last; major = +adapter.add(major, 1, majorUnit)) { + index = map[major]; + if (index >= 0) { + ticks[index].major = true; + } + } + return ticks; +} +function ticksFromTimestamps(scale, values, majorUnit) { + const ticks = []; + const map = {}; + const ilen = values.length; + let i, value; + for (i = 0; i < ilen; ++i) { + value = values[i]; + map[value] = i; + ticks.push({ + value, + major: false + }); + } + return (ilen === 0 || !majorUnit) ? ticks : setMajorTicks(scale, ticks, map, majorUnit); +} +class TimeScale extends Scale { + constructor(props) { + super(props); + this._cache = { + data: [], + labels: [], + all: [] + }; + this._unit = 'day'; + this._majorUnit = undefined; + this._offsets = {}; + this._normalized = false; + this._parseOpts = undefined; + } + init(scaleOpts, opts) { + const time = scaleOpts.time || (scaleOpts.time = {}); + const adapter = this._adapter = new adapters._date(scaleOpts.adapters.date); + adapter.init(opts); + mergeIf(time.displayFormats, adapter.formats()); + this._parseOpts = { + parser: time.parser, + round: time.round, + isoWeekday: time.isoWeekday + }; + super.init(scaleOpts); + this._normalized = opts.normalized; + } + parse(raw, index) { + if (raw === undefined) { + return null; + } + return parse(this, raw); + } + beforeLayout() { + super.beforeLayout(); + this._cache = { + data: [], + labels: [], + all: [] + }; + } + determineDataLimits() { + const options = this.options; + const adapter = this._adapter; + const unit = options.time.unit || 'day'; + let {min, max, minDefined, maxDefined} = this.getUserBounds(); + function _applyBounds(bounds) { + if (!minDefined && !isNaN(bounds.min)) { + min = Math.min(min, bounds.min); + } + if (!maxDefined && !isNaN(bounds.max)) { + max = Math.max(max, bounds.max); + } + } + if (!minDefined || !maxDefined) { + _applyBounds(this._getLabelBounds()); + if (options.bounds !== 'ticks' || options.ticks.source !== 'labels') { + _applyBounds(this.getMinMax(false)); + } + } + min = isNumberFinite(min) && !isNaN(min) ? min : +adapter.startOf(Date.now(), unit); + max = isNumberFinite(max) && !isNaN(max) ? max : +adapter.endOf(Date.now(), unit) + 1; + this.min = Math.min(min, max - 1); + this.max = Math.max(min + 1, max); + } + _getLabelBounds() { + const arr = this.getLabelTimestamps(); + let min = Number.POSITIVE_INFINITY; + let max = Number.NEGATIVE_INFINITY; + if (arr.length) { + min = arr[0]; + max = arr[arr.length - 1]; + } + return {min, max}; + } + buildTicks() { + const options = this.options; + const timeOpts = options.time; + const tickOpts = options.ticks; + const timestamps = tickOpts.source === 'labels' ? this.getLabelTimestamps() : this._generate(); + if (options.bounds === 'ticks' && timestamps.length) { + this.min = this._userMin || timestamps[0]; + this.max = this._userMax || timestamps[timestamps.length - 1]; + } + const min = this.min; + const max = this.max; + const ticks = _filterBetween(timestamps, min, max); + this._unit = timeOpts.unit || (tickOpts.autoSkip + ? determineUnitForAutoTicks(timeOpts.minUnit, this.min, this.max, this._getLabelCapacity(min)) + : determineUnitForFormatting(this, ticks.length, timeOpts.minUnit, this.min, this.max)); + this._majorUnit = !tickOpts.major.enabled || this._unit === 'year' ? undefined + : determineMajorUnit(this._unit); + this.initOffsets(timestamps); + if (options.reverse) { + ticks.reverse(); + } + return ticksFromTimestamps(this, ticks, this._majorUnit); + } + afterAutoSkip() { + if (this.options.offsetAfterAutoskip) { + this.initOffsets(this.ticks.map(tick => +tick.value)); + } + } + initOffsets(timestamps) { + let start = 0; + let end = 0; + let first, last; + if (this.options.offset && timestamps.length) { + first = this.getDecimalForValue(timestamps[0]); + if (timestamps.length === 1) { + start = 1 - first; + } else { + start = (this.getDecimalForValue(timestamps[1]) - first) / 2; + } + last = this.getDecimalForValue(timestamps[timestamps.length - 1]); + if (timestamps.length === 1) { + end = last; + } else { + end = (last - this.getDecimalForValue(timestamps[timestamps.length - 2])) / 2; + } + } + const limit = timestamps.length < 3 ? 0.5 : 0.25; + start = _limitValue(start, 0, limit); + end = _limitValue(end, 0, limit); + this._offsets = {start, end, factor: 1 / (start + 1 + end)}; + } + _generate() { + const adapter = this._adapter; + const min = this.min; + const max = this.max; + const options = this.options; + const timeOpts = options.time; + const minor = timeOpts.unit || determineUnitForAutoTicks(timeOpts.minUnit, min, max, this._getLabelCapacity(min)); + const stepSize = valueOrDefault(timeOpts.stepSize, 1); + const weekday = minor === 'week' ? timeOpts.isoWeekday : false; + const hasWeekday = isNumber(weekday) || weekday === true; + const ticks = {}; + let first = min; + let time, count; + if (hasWeekday) { + first = +adapter.startOf(first, 'isoWeek', weekday); + } + first = +adapter.startOf(first, hasWeekday ? 'day' : minor); + if (adapter.diff(max, min, minor) > 100000 * stepSize) { + throw new Error(min + ' and ' + max + ' are too far apart with stepSize of ' + stepSize + ' ' + minor); + } + const timestamps = options.ticks.source === 'data' && this.getDataTimestamps(); + for (time = first, count = 0; time < max; time = +adapter.add(time, stepSize, minor), count++) { + addTick(ticks, time, timestamps); + } + if (time === max || options.bounds === 'ticks' || count === 1) { + addTick(ticks, time, timestamps); + } + return Object.keys(ticks).sort((a, b) => a - b).map(x => +x); + } + getLabelForValue(value) { + const adapter = this._adapter; + const timeOpts = this.options.time; + if (timeOpts.tooltipFormat) { + return adapter.format(value, timeOpts.tooltipFormat); + } + return adapter.format(value, timeOpts.displayFormats.datetime); + } + _tickFormatFunction(time, index, ticks, format) { + const options = this.options; + const formats = options.time.displayFormats; + const unit = this._unit; + const majorUnit = this._majorUnit; + const minorFormat = unit && formats[unit]; + const majorFormat = majorUnit && formats[majorUnit]; + const tick = ticks[index]; + const major = majorUnit && majorFormat && tick && tick.major; + const label = this._adapter.format(time, format || (major ? majorFormat : minorFormat)); + const formatter = options.ticks.callback; + return formatter ? callback(formatter, [label, index, ticks], this) : label; + } + generateTickLabels(ticks) { + let i, ilen, tick; + for (i = 0, ilen = ticks.length; i < ilen; ++i) { + tick = ticks[i]; + tick.label = this._tickFormatFunction(tick.value, i, ticks); + } + } + getDecimalForValue(value) { + return value === null ? NaN : (value - this.min) / (this.max - this.min); + } + getPixelForValue(value) { + const offsets = this._offsets; + const pos = this.getDecimalForValue(value); + return this.getPixelForDecimal((offsets.start + pos) * offsets.factor); + } + getValueForPixel(pixel) { + const offsets = this._offsets; + const pos = this.getDecimalForPixel(pixel) / offsets.factor - offsets.end; + return this.min + pos * (this.max - this.min); + } + _getLabelSize(label) { + const ticksOpts = this.options.ticks; + const tickLabelWidth = this.ctx.measureText(label).width; + const angle = toRadians(this.isHorizontal() ? ticksOpts.maxRotation : ticksOpts.minRotation); + const cosRotation = Math.cos(angle); + const sinRotation = Math.sin(angle); + const tickFontSize = this._resolveTickFontOptions(0).size; + return { + w: (tickLabelWidth * cosRotation) + (tickFontSize * sinRotation), + h: (tickLabelWidth * sinRotation) + (tickFontSize * cosRotation) + }; + } + _getLabelCapacity(exampleTime) { + const timeOpts = this.options.time; + const displayFormats = timeOpts.displayFormats; + const format = displayFormats[timeOpts.unit] || displayFormats.millisecond; + const exampleLabel = this._tickFormatFunction(exampleTime, 0, ticksFromTimestamps(this, [exampleTime], this._majorUnit), format); + const size = this._getLabelSize(exampleLabel); + const capacity = Math.floor(this.isHorizontal() ? this.width / size.w : this.height / size.h) - 1; + return capacity > 0 ? capacity : 1; + } + getDataTimestamps() { + let timestamps = this._cache.data || []; + let i, ilen; + if (timestamps.length) { + return timestamps; + } + const metas = this.getMatchingVisibleMetas(); + if (this._normalized && metas.length) { + return (this._cache.data = metas[0].controller.getAllParsedValues(this)); + } + for (i = 0, ilen = metas.length; i < ilen; ++i) { + timestamps = timestamps.concat(metas[i].controller.getAllParsedValues(this)); + } + return (this._cache.data = this.normalize(timestamps)); + } + getLabelTimestamps() { + const timestamps = this._cache.labels || []; + let i, ilen; + if (timestamps.length) { + return timestamps; + } + const labels = this.getLabels(); + for (i = 0, ilen = labels.length; i < ilen; ++i) { + timestamps.push(parse(this, labels[i])); + } + return (this._cache.labels = this._normalized ? timestamps : this.normalize(timestamps)); + } + normalize(values) { + return _arrayUnique(values.sort(sorter)); + } +} +TimeScale.id = 'time'; +TimeScale.defaults = { + bounds: 'data', + adapters: {}, + time: { + parser: false, + unit: false, + round: false, + isoWeekday: false, + minUnit: 'millisecond', + displayFormats: {} + }, + ticks: { + source: 'auto', + major: { + enabled: false + } + } +}; + +function interpolate(table, val, reverse) { + let lo = 0; + let hi = table.length - 1; + let prevSource, nextSource, prevTarget, nextTarget; + if (reverse) { + if (val >= table[lo].pos && val <= table[hi].pos) { + ({lo, hi} = _lookupByKey(table, 'pos', val)); + } + ({pos: prevSource, time: prevTarget} = table[lo]); + ({pos: nextSource, time: nextTarget} = table[hi]); + } else { + if (val >= table[lo].time && val <= table[hi].time) { + ({lo, hi} = _lookupByKey(table, 'time', val)); + } + ({time: prevSource, pos: prevTarget} = table[lo]); + ({time: nextSource, pos: nextTarget} = table[hi]); + } + const span = nextSource - prevSource; + return span ? prevTarget + (nextTarget - prevTarget) * (val - prevSource) / span : prevTarget; +} +class TimeSeriesScale extends TimeScale { + constructor(props) { + super(props); + this._table = []; + this._minPos = undefined; + this._tableRange = undefined; + } + initOffsets() { + const timestamps = this._getTimestampsForTable(); + const table = this._table = this.buildLookupTable(timestamps); + this._minPos = interpolate(table, this.min); + this._tableRange = interpolate(table, this.max) - this._minPos; + super.initOffsets(timestamps); + } + buildLookupTable(timestamps) { + const {min, max} = this; + const items = []; + const table = []; + let i, ilen, prev, curr, next; + for (i = 0, ilen = timestamps.length; i < ilen; ++i) { + curr = timestamps[i]; + if (curr >= min && curr <= max) { + items.push(curr); + } + } + if (items.length < 2) { + return [ + {time: min, pos: 0}, + {time: max, pos: 1} + ]; + } + for (i = 0, ilen = items.length; i < ilen; ++i) { + next = items[i + 1]; + prev = items[i - 1]; + curr = items[i]; + if (Math.round((next + prev) / 2) !== curr) { + table.push({time: curr, pos: i / (ilen - 1)}); + } + } + return table; + } + _getTimestampsForTable() { + let timestamps = this._cache.all || []; + if (timestamps.length) { + return timestamps; + } + const data = this.getDataTimestamps(); + const label = this.getLabelTimestamps(); + if (data.length && label.length) { + timestamps = this.normalize(data.concat(label)); + } else { + timestamps = data.length ? data : label; + } + timestamps = this._cache.all = timestamps; + return timestamps; + } + getDecimalForValue(value) { + return (interpolate(this._table, value) - this._minPos) / this._tableRange; + } + getValueForPixel(pixel) { + const offsets = this._offsets; + const decimal = this.getDecimalForPixel(pixel) / offsets.factor - offsets.end; + return interpolate(this._table, decimal * this._tableRange + this._minPos, true); + } +} +TimeSeriesScale.id = 'timeseries'; +TimeSeriesScale.defaults = TimeScale.defaults; + +var scales = /*#__PURE__*/Object.freeze({ +__proto__: null, +CategoryScale: CategoryScale, +LinearScale: LinearScale, +LogarithmicScale: LogarithmicScale, +RadialLinearScale: RadialLinearScale, +TimeScale: TimeScale, +TimeSeriesScale: TimeSeriesScale +}); + +const registerables = [ + controllers, + elements, + plugins, + scales, +]; + +export { Animation, Animations, ArcElement, BarController, BarElement, BasePlatform, BasicPlatform, BubbleController, CategoryScale, Chart, DatasetController, plugin_decimation as Decimation, DomPlatform, DoughnutController, Element, index as Filler, Interaction, plugin_legend as Legend, LineController, LineElement, LinearScale, LogarithmicScale, PieController, PointElement, PolarAreaController, RadarController, RadialLinearScale, Scale, ScatterController, plugin_subtitle as SubTitle, Ticks, TimeScale, TimeSeriesScale, plugin_title as Title, plugin_tooltip as Tooltip, adapters as _adapters, _detectPlatform, animator, controllers, elements, layouts, plugins, registerables, registry, scales }; diff --git a/static/js/chartjs-adapter-luxon.js b/static/js/chartjs-adapter-luxon.js index b105f830..d212862a 100644 --- a/static/js/chartjs-adapter-luxon.js +++ b/static/js/chartjs-adapter-luxon.js @@ -1,7 +1,7 @@ /*! - * chartjs-adapter-luxon v1.1.0 + * chartjs-adapter-luxon v1.3.1 * https://www.chartjs.org - * (c) 2021 chartjs-adapter-luxon Contributors + * (c) 2023 chartjs-adapter-luxon Contributors * Released under the MIT license */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("chart.js"),require("luxon")):"function"==typeof define&&define.amd?define(["chart.js","luxon"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Chart,e.luxon)}(this,(function(e,t){"use strict";const n={datetime:t.DateTime.DATETIME_MED_WITH_SECONDS,millisecond:"h:mm:ss.SSS a",second:t.DateTime.TIME_WITH_SECONDS,minute:t.DateTime.TIME_SIMPLE,hour:{hour:"numeric"},day:{day:"numeric",month:"short"},week:"DD",month:{month:"short",year:"numeric"},quarter:"'Q'q - yyyy",year:{year:"numeric"}};e._adapters._date.override({_id:"luxon",_create:function(e){return t.DateTime.fromMillis(e,this.options)},formats:function(){return n},parse:function(e,n){const r=this.options;if(null==e)return null;const i=typeof e;return"number"===i?e=this._create(e):"string"===i?e="string"==typeof n?t.DateTime.fromFormat(e,n,r):t.DateTime.fromISO(e,r):e instanceof Date?e=t.DateTime.fromJSDate(e,r):"object"!==i||e instanceof t.DateTime||(e=t.DateTime.fromObject(e)),e.isValid?e.valueOf():null},format:function(e,t){const n=this._create(e);return"string"==typeof t?n.toFormat(t,this.options):n.toLocaleString(t)},add:function(e,t,n){const r={};return r[n]=t,this._create(e).plus(r).valueOf()},diff:function(e,t,n){return this._create(e).diff(this._create(t)).as(n).valueOf()},startOf:function(e,t,n){if("isoWeek"===t){n=Math.trunc(Math.min(Math.max(0,n),6));const t=this._create(e);return t.minus({days:(t.weekday-n+7)%7}).startOf("day").valueOf()}return t?this._create(e).startOf(t).valueOf():e},endOf:function(e,t){return this._create(e).endOf(t).valueOf()}})})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("chart.js"),require("luxon")):"function"==typeof define&&define.amd?define(["chart.js","luxon"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Chart,e.luxon)}(this,(function(e,t){"use strict";const n={datetime:t.DateTime.DATETIME_MED_WITH_SECONDS,millisecond:"h:mm:ss.SSS a",second:t.DateTime.TIME_WITH_SECONDS,minute:t.DateTime.TIME_SIMPLE,hour:{hour:"numeric"},day:{day:"numeric",month:"short"},week:"DD",month:{month:"short",year:"numeric"},quarter:"'Q'q - yyyy",year:{year:"numeric"}};e._adapters._date.override({_id:"luxon",_create:function(e){return t.DateTime.fromMillis(e,this.options)},init(e){this.options.locale||(this.options.locale=e.locale)},formats:function(){return n},parse:function(e,n){const i=this.options,r=typeof e;return null===e||"undefined"===r?null:("number"===r?e=this._create(e):"string"===r?e="string"==typeof n?t.DateTime.fromFormat(e,n,i):t.DateTime.fromISO(e,i):e instanceof Date?e=t.DateTime.fromJSDate(e,i):"object"!==r||e instanceof t.DateTime||(e=t.DateTime.fromObject(e,i)),e.isValid?e.valueOf():null)},format:function(e,t){const n=this._create(e);return"string"==typeof t?n.toFormat(t):n.toLocaleString(t)},add:function(e,t,n){const i={};return i[n]=t,this._create(e).plus(i).valueOf()},diff:function(e,t,n){return this._create(e).diff(this._create(t)).as(n).valueOf()},startOf:function(e,t,n){if("isoWeek"===t){n=Math.trunc(Math.min(Math.max(0,n),6));const t=this._create(e);return t.minus({days:(t.weekday-n+7)%7}).startOf("day").valueOf()}return t?this._create(e).startOf(t).valueOf():e},endOf:function(e,t){return this._create(e).endOf(t).valueOf()}})})); diff --git a/static/js/chartjs-plugin-zoom.min.js b/static/js/chartjs-plugin-zoom.min.js index eaa343ab..a6c55771 100644 --- a/static/js/chartjs-plugin-zoom.min.js +++ b/static/js/chartjs-plugin-zoom.min.js @@ -1,7 +1,7 @@ /*! -* chartjs-plugin-zoom v1.2.1 +* chartjs-plugin-zoom v2.0.1 * undefined - * (c) 2016-2022 chartjs-plugin-zoom Contributors + * (c) 2016-2023 chartjs-plugin-zoom Contributors * Released under the MIT License */ -!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n(require("chart.js"),require("hammerjs"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["chart.js","hammerjs","chart.js/helpers"],n):(e="undefined"!=typeof globalThis?globalThis:e||self).ChartZoom=n(e.Chart,e.Hammer,e.Chart.helpers)}(this,(function(e,n,t){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(n);const i=e=>e&&e.enabled&&e.modifierKey,c=(e,n)=>e&&n[e+"Key"],r=(e,n)=>e&&!n[e+"Key"];function l(e,n,t){return void 0===e||("string"==typeof e?-1!==e.indexOf(n):"function"==typeof e&&-1!==e({chart:t}).indexOf(n))}function s(e,n,o){const a=function({x:e,y:n},t){const o=t.scales,a=Object.keys(o);for(let t=0;t=i.top&&n<=i.bottom&&e>=i.left&&e<=i.right)return i}return null}(n,o);if(a&&l(e,a.axis,o))return[a];const i=[];return t.each(o.scales,(function(n){l(e,n.axis,o)||i.push(n)})),i}const m=new WeakMap;function u(e){let n=m.get(e);return n||(n={originalScaleLimits:{},updatedScaleLimits:{},handlers:{},panDelta:{}},m.set(e,n)),n}function d(e,n,t){const o=e.max-e.min,a=o*(n-1),i=e.isHorizontal()?t.x:t.y,c=Math.max(0,Math.min(1,(e.getValueForPixel(i)-e.min)/o||0));return{min:a*c,max:a*(1-c)}}function f(e,n,o,a,i){let c=o[a];if("original"===c){const o=e.originalScaleLimits[n.id][a];c=t.valueOrDefault(o.options,o.scale)}return t.valueOrDefault(c,i)}function h(e,{min:n,max:t},o,a=!1){const i=u(e.chart),{id:c,axis:r,options:l}=e,s=o&&(o[c]||o[r])||{},{minRange:m=0}=s,d=f(i,e,s,"min",-1/0),h=f(i,e,s,"max",1/0),p=Math.max(n,d),x=Math.min(t,h),g=a?Math.max(x-p,m):e.max-e.min;if(x-p!==g)if(d>x-g)n=p,t=p+g;else if(h0===e||isNaN(e)?0:e<0?Math.min(Math.round(e),-1):Math.max(Math.round(e),1);const x={second:500,minute:3e4,hour:18e5,day:432e5,week:3024e5,month:1296e6,quarter:5184e6,year:157248e5};function g(e,n,t,o=!1){const{min:a,max:i,options:c}=e,r=c.time&&c.time.round,l=x[r]||0,s=e.getValueForPixel(e.getPixelForValue(a+l)-n),m=e.getValueForPixel(e.getPixelForValue(i+l)-n),{min:u=-1/0,max:d=1/0}=o&&t&&t[e.axis]||{};return!!(isNaN(s)||isNaN(m)||sd)||h(e,{min:s,max:m},t,o)}function b(e,n,t){return g(e,n,t,!0)}const y={category:function(e,n,t,o){const a=d(e,n,t);return e.min===e.max&&n<1&&function(e){const n=e.getLabels().length-1;e.min>0&&(e.min-=1),e.maxr&&(a=Math.max(0,a-l),i=1===c?a:a+c,s=0===a),h(e,{min:a,max:i},t)||s},default:g,logarithmic:b,timeseries:b};function z(e,n){t.each(e,((t,o)=>{n[o]||delete e[o]}))}function M(e,n){const{scales:o}=e,{originalScaleLimits:a,updatedScaleLimits:i}=n;return t.each(o,(function(e){(function(e,n,t){const{id:o,options:{min:a,max:i}}=e;if(!n[o]||!t[o])return!0;const c=t[o];return c.min!==a||c.max!==i})(e,a,i)&&(a[e.id]={min:{scale:e.min,options:e.options.min},max:{scale:e.max,options:e.options.max}})})),z(a,o),z(i,o),a}function k(e,n,o,a){const i=y[e.type]||y.default;t.callback(i,[e,n,o,a])}function w(e){const n=e.chartArea;return{x:(n.left+n.right)/2,y:(n.top+n.bottom)/2}}function S(e,n,o="none"){const{x:a=1,y:i=1,focalPoint:c=w(e)}="number"==typeof n?{x:n,y:n}:n,r=u(e),{options:{limits:m,zoom:d}}=r,{mode:f="xy",overScaleMode:h}=d||{};M(e,r);const p=1!==a&&l(f,"x",e),x=1!==i&&l(f,"y",e),g=h&&s(h,c,e);t.each(g||e.scales,(function(e){e.isHorizontal()&&p?k(e,a,c,m):!e.isHorizontal()&&x&&k(e,i,c,m)})),e.update(o),t.callback(d.onZoom,[{chart:e}])}function P(e,n,t){const o=e.getValueForPixel(n),a=e.getValueForPixel(t);return{min:Math.min(o,a),max:Math.max(o,a)}}function C(e){const n=u(e);let o=1,a=1;return t.each(e.scales,(function(e){const i=function(e,n){const o=e.originalScaleLimits[n];if(!o)return;const{min:a,max:i}=o;return t.valueOrDefault(i.options,i.scale)-t.valueOrDefault(a.options,a.scale)}(n,e.id);if(i){const n=Math.round(i/(e.max-e.min)*100)/100;o=Math.min(o,n),a=Math.max(a,n)}})),o<1?o:a}function j(e,n,o,a){const{panDelta:i}=a,c=i[e.id]||0;t.sign(c)===t.sign(n)&&(n+=c);const r=v[e.type]||v.default;t.callback(r,[e,n,o])?i[e.id]=0:i[e.id]=n}function Z(e,n,o,a="none"){const{x:i=0,y:c=0}="number"==typeof n?{x:n,y:n}:n,r=u(e),{options:{pan:s,limits:m}}=r,{mode:d="xy",onPan:f}=s||{};M(e,r);const h=0!==i&&l(d,"x",e),p=0!==c&&l(d,"y",e);t.each(o||e.scales,(function(e){e.isHorizontal()&&h?j(e,i,m,r):!e.isHorizontal()&&p&&j(e,c,m,r)})),e.update(a),t.callback(f,[{chart:e}])}function L(e){const n=u(e),t={};for(const o of Object.keys(e.scales)){const{min:e,max:a}=n.originalScaleLimits[o]||{min:{},max:{}};t[o]={min:e.scale,max:a.scale}}return t}function R(e,n){const{handlers:t}=u(e),o=t[n];o&&o.target&&(o.target.removeEventListener(n,o),delete t[n])}function Y(e,n,t,o){const{handlers:a,options:i}=u(e),c=a[t];c&&c.target===n||(R(e,t),a[t]=n=>o(e,n,i),a[t].target=n,n.addEventListener(t,a[t]))}function O(e,n){const t=u(e);t.dragStart&&(t.dragging=!0,t.dragEnd=n,e.update("none"))}function T(e,n,o){const{onZoomStart:a,onZoomRejected:i}=o;if(a){const{left:o,top:c}=n.target.getBoundingClientRect(),r={x:n.clientX-o,y:n.clientY-c};if(!1===t.callback(a,[{chart:e,event:n,point:r}]))return t.callback(i,[{chart:e,event:n}]),!1}}function X(e,n){const o=u(e),{pan:a,zoom:l={}}=o.options;if(c(i(a),n)||r(i(l.drag),n))return t.callback(l.onZoomRejected,[{chart:e,event:n}]);!1!==T(e,n,l)&&(o.dragStart=n,Y(e,e.canvas,"mousemove",O))}function D(e,n,t,o){const{left:a,top:i}=t.target.getBoundingClientRect(),c=l(n,"x",e),r=l(n,"y",e);let{top:s,left:m,right:u,bottom:d,width:f,height:h}=e.chartArea;c&&(m=Math.min(t.clientX,o.clientX)-a,u=Math.max(t.clientX,o.clientX)-a),r&&(s=Math.min(t.clientY,o.clientY)-i,d=Math.max(t.clientY,o.clientY)-i);const p=u-m,x=d-s;return{left:m,top:s,right:u,bottom:d,width:p,height:x,zoomX:c&&p?1+(f-p)/f:1,zoomY:r&&x?1+(h-x)/h:1}}function E(e,n){const o=u(e);if(!o.dragStart)return;R(e,"mousemove");const{mode:a,onZoomComplete:i,drag:{threshold:c=0}}=o.options.zoom,r=D(e,a,o.dragStart,n),s=l(a,"x",e)?r.width:0,m=l(a,"y",e)?r.height:0,d=Math.sqrt(s*s+m*m);if(o.dragStart=o.dragEnd=null,d<=c)return o.dragging=!1,void e.update("none");!function(e,n,o,a="none"){const i=u(e),{options:{limits:c,zoom:r}}=i,{mode:s="xy"}=r;M(e,i);const m=l(s,"x",e),d=l(s,"y",e);t.each(e.scales,(function(e){e.isHorizontal()&&m?h(e,P(e,n.x,o.x),c,!0):!e.isHorizontal()&&d&&h(e,P(e,n.y,o.y),c,!0)})),e.update(a),t.callback(r.onZoom,[{chart:e}])}(e,{x:r.left,y:r.top},{x:r.right,y:r.bottom},"zoom"),setTimeout((()=>o.dragging=!1),500),t.callback(i,[{chart:e}])}function F(e,n){const{handlers:{onZoomComplete:o},options:{zoom:a}}=u(e);if(!function(e,n,o){if(r(i(o.wheel),n))t.callback(o.onZoomRejected,[{chart:e,event:n}]);else if(!1!==T(e,n,o)&&(n.cancelable&&n.preventDefault(),void 0!==n.deltaY))return!0}(e,n,a))return;const c=n.target.getBoundingClientRect(),l=1+(n.deltaY>=0?-a.wheel.speed:a.wheel.speed);S(e,{x:l,y:l,focalPoint:{x:n.clientX-c.left,y:n.clientY-c.top}}),o&&o()}function H(e,n,o,a){o&&(u(e).handlers[n]=function(e,n){let t;return function(){return clearTimeout(t),t=setTimeout(e,n),n}}((()=>t.callback(o,[{chart:e}])),a))}function V(e,n){return function(o,a){const{pan:l,zoom:s={}}=n.options;if(!l||!l.enabled)return!1;const m=a&&a.srcEvent;return!m||(!(!n.panning&&"mouse"===a.pointerType&&(r(i(l),m)||c(i(s.drag),m)))||(t.callback(l.onPanRejected,[{chart:e,event:a}]),!1))}}function B(e,n,t){if(n.scale){const{center:o,pointers:a}=t,i=1/n.scale*t.scale,c=t.target.getBoundingClientRect(),r=function(e,n){const t=Math.abs(e.clientX-n.clientX),o=Math.abs(e.clientY-n.clientY),a=t/o;let i,c;return a>.3&&a<1.7?i=c=!0:t>o?i=!0:c=!0,{x:i,y:c}}(a[0],a[1]),s=n.options.zoom.mode;S(e,{x:r.x&&l(s,"x",e)?i:1,y:r.y&&l(s,"y",e)?i:1,focalPoint:{x:o.x-c.left,y:o.y-c.top}}),n.scale=t.scale}}function K(e,n,t){const o=n.delta;o&&(n.panning=!0,Z(e,{x:t.deltaX-o.x,y:t.deltaY-o.y},n.panScales),n.delta={x:t.deltaX,y:t.deltaY})}const N=new WeakMap;function q(e,n){const o=u(e),i=e.canvas,{pan:c,zoom:r}=n,l=new a.default.Manager(i);r&&r.pinch.enabled&&(l.add(new a.default.Pinch),l.on("pinchstart",(()=>function(e,n){n.options.zoom.pinch.enabled&&(n.scale=1)}(0,o))),l.on("pinch",(n=>B(e,o,n))),l.on("pinchend",(n=>function(e,n,o){n.scale&&(B(e,n,o),n.scale=null,t.callback(n.options.zoom.onZoomComplete,[{chart:e}]))}(e,o,n)))),c&&c.enabled&&(l.add(new a.default.Pan({threshold:c.threshold,enable:V(e,o)})),l.on("panstart",(n=>function(e,n,o){const{enabled:a,overScaleMode:i,onPanStart:c,onPanRejected:r}=n.options.pan;if(!a)return;const l=o.target.getBoundingClientRect(),m={x:o.center.x-l.left,y:o.center.y-l.top};if(!1===t.callback(c,[{chart:e,event:o,point:m}]))return t.callback(r,[{chart:e,event:o}]);n.panScales=i&&s(i,m,e),n.delta={x:0,y:0},clearTimeout(n.panEndTimeout),K(e,n,o)}(e,o,n))),l.on("panmove",(n=>K(e,o,n))),l.on("panend",(()=>function(e,n){n.delta=null,n.panning&&(n.panEndTimeout=setTimeout((()=>n.panning=!1),500),t.callback(n.options.pan.onPanComplete,[{chart:e}]))}(e,o)))),N.set(e,l)}var W={id:"zoom",version:"1.2.1",defaults:{pan:{enabled:!1,mode:"xy",threshold:10,modifierKey:null},zoom:{wheel:{enabled:!1,speed:.1,modifierKey:null},drag:{enabled:!1,modifierKey:null},pinch:{enabled:!1},mode:"xy"}},start:function(e,n,o){u(e).options=o,Object.prototype.hasOwnProperty.call(o.zoom,"enabled")&&console.warn("The option `zoom.enabled` is no longer supported. Please use `zoom.wheel.enabled`, `zoom.drag.enabled`, or `zoom.pinch.enabled`."),a.default&&q(e,o),e.pan=(n,t,o)=>Z(e,n,t,o),e.zoom=(n,t)=>S(e,n,t),e.zoomScale=(n,t,o)=>function(e,n,t,o="none"){M(e,u(e)),h(e.scales[n],t,void 0,!0),e.update(o)}(e,n,t,o),e.resetZoom=n=>function(e,n="default"){const o=u(e),a=M(e,o);t.each(e.scales,(function(e){const n=e.options;a[e.id]?(n.min=a[e.id].min.options,n.max=a[e.id].max.options):(delete n.min,delete n.max)})),e.update(n),t.callback(o.options.zoom.onZoomComplete,[{chart:e}])}(e,n),e.getZoomLevel=()=>C(e),e.getInitialScaleBounds=()=>L(e),e.isZoomedOrPanned=()=>function(e){const n=L(e);for(const t of Object.keys(e.scales)){const{min:o,max:a}=n[t];if(void 0!==o&&e.scales[t].min!==o)return!0;if(void 0!==a&&e.scales[t].max!==a)return!0}return!1}(e)},beforeEvent(e){const n=u(e);if(n.panning||n.dragging)return!1},beforeUpdate:function(e,n,t){u(e).options=t,function(e,n){const t=e.canvas,{wheel:o,drag:a,onZoomComplete:i}=n.zoom;o.enabled?(Y(e,t,"wheel",F),H(e,"onZoomComplete",i,250)):R(e,"wheel"),a.enabled?(Y(e,t,"mousedown",X),Y(e,t.ownerDocument,"mouseup",E)):(R(e,"mousedown"),R(e,"mousemove"),R(e,"mouseup"))}(e,t)},beforeDatasetsDraw:function(e,n,t){const{dragStart:o,dragEnd:a}=u(e);if(a){const{left:n,top:i,width:c,height:r}=D(e,t.zoom.mode,o,a),l=t.zoom.drag,s=e.ctx;s.save(),s.beginPath(),s.fillStyle=l.backgroundColor||"rgba(225,225,225,0.3)",s.fillRect(n,i,c,r),l.borderWidth>0&&(s.lineWidth=l.borderWidth,s.strokeStyle=l.borderColor||"rgba(225,225,225)",s.strokeRect(n,i,c,r)),s.restore()}},stop:function(e){!function(e){R(e,"mousedown"),R(e,"mousemove"),R(e,"mouseup"),R(e,"wheel"),R(e,"click")}(e),a.default&&function(e){const n=N.get(e);n&&(n.remove("pinchstart"),n.remove("pinch"),n.remove("pinchend"),n.remove("panstart"),n.remove("pan"),n.remove("panend"),n.destroy(),N.delete(e))}(e),function(e){m.delete(e)}(e)},panFunctions:v,zoomFunctions:y};return e.Chart.register(W),W})); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("chart.js"),require("hammerjs"),require("chart.js/helpers")):"function"==typeof define&&define.amd?define(["chart.js","hammerjs","chart.js/helpers"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).ChartZoom=t(e.Chart,e.Hammer,e.Chart.helpers)}(this,(function(e,t,n){"use strict";function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(t);const i=e=>e&&e.enabled&&e.modifierKey,c=(e,t)=>e&&t[e+"Key"],r=(e,t)=>e&&!t[e+"Key"];function s(e,t,n){return void 0===e||("string"==typeof e?-1!==e.indexOf(t):"function"==typeof e&&-1!==e({chart:n}).indexOf(t))}function l(e,t){return"function"==typeof e&&(e=e({chart:t})),"string"==typeof e?{x:-1!==e.indexOf("x"),y:-1!==e.indexOf("y")}:{x:!1,y:!1}}function u(e,t,o){const{mode:a="xy",scaleMode:i,overScaleMode:c}=e||{},r=function({x:e,y:t},n){const o=n.scales,a=Object.keys(o);for(let n=0;n=i.top&&t<=i.bottom&&e>=i.left&&e<=i.right)return i}return null}(t,o),s=l(a,o),u=l(i,o);if(c){const e=l(c,o);for(const t of["x","y"])e[t]&&(u[t]=s[t],s[t]=!1)}if(r&&u[r.axis])return[r];const m=[];return n.each(o.scales,(function(e){s[e.axis]&&m.push(e)})),m}const m=new WeakMap;function d(e){let t=m.get(e);return t||(t={originalScaleLimits:{},updatedScaleLimits:{},handlers:{},panDelta:{}},m.set(e,t)),t}function f(e,t,n){const o=e.max-e.min,a=o*(t-1),i=e.isHorizontal()?n.x:n.y,c=Math.max(0,Math.min(1,(e.getValueForPixel(i)-e.min)/o||0));return{min:a*c,max:a*(1-c)}}function p(e,t,o,a,i){let c=o[a];if("original"===c){const o=e.originalScaleLimits[t.id][a];c=n.valueOrDefault(o.options,o.scale)}return n.valueOrDefault(c,i)}function h(e,{min:t,max:n},o,a=!1){const i=d(e.chart),{id:c,axis:r,options:s}=e,l=o&&(o[c]||o[r])||{},{minRange:u=0}=l,m=p(i,e,l,"min",-1/0),f=p(i,e,l,"max",1/0),h=a?Math.max(n-t,u):e.max-e.min,x=(h-n+t)/2;return n+=x,(t-=x)f&&(n=f,t=Math.max(f-h,m)),s.min=t,s.max=n,i.updatedScaleLimits[e.id]={min:t,max:n},e.parse(t)!==e.min||e.parse(n)!==e.max}const x=e=>0===e||isNaN(e)?0:e<0?Math.min(Math.round(e),-1):Math.max(Math.round(e),1);const g={second:500,minute:3e4,hour:18e5,day:432e5,week:3024e5,month:1296e6,quarter:5184e6,year:157248e5};function y(e,t,n,o=!1){const{min:a,max:i,options:c}=e,r=c.time&&c.time.round,s=g[r]||0,l=e.getValueForPixel(e.getPixelForValue(a+s)-t),u=e.getValueForPixel(e.getPixelForValue(i+s)-t),{min:m=-1/0,max:d=1/0}=o&&n&&n[e.axis]||{};return!!(isNaN(l)||isNaN(u)||ld)||h(e,{min:l,max:u},n,o)}function b(e,t,n){return y(e,t,n,!0)}const v={category:function(e,t,n,o){const a=f(e,t,n);return e.min===e.max&&t<1&&function(e){const t=e.getLabels().length-1;e.min>0&&(e.min-=1),e.maxr&&(a=Math.max(0,a-s),i=1===c?a:a+c,l=0===a),h(e,{min:a,max:i},n)||l},default:y,logarithmic:b,timeseries:b};function M(e,t){n.each(e,((n,o)=>{t[o]||delete e[o]}))}function k(e,t){const{scales:o}=e,{originalScaleLimits:a,updatedScaleLimits:i}=t;return n.each(o,(function(e){(function(e,t,n){const{id:o,options:{min:a,max:i}}=e;if(!t[o]||!n[o])return!0;const c=n[o];return c.min!==a||c.max!==i})(e,a,i)&&(a[e.id]={min:{scale:e.min,options:e.options.min},max:{scale:e.max,options:e.options.max}})})),M(a,o),M(i,o),a}function S(e,t,o,a){const i=v[e.type]||v.default;n.callback(i,[e,t,o,a])}function P(e,t,o,a,i){const c=w[e.type]||w.default;n.callback(c,[e,t,o,a,i])}function D(e){const t=e.chartArea;return{x:(t.left+t.right)/2,y:(t.top+t.bottom)/2}}function j(e,t,o="none"){const{x:a=1,y:i=1,focalPoint:c=D(e)}="number"==typeof t?{x:t,y:t}:t,r=d(e),{options:{limits:s,zoom:l}}=r;k(e,r);const m=1!==a,f=1!==i,p=u(l,c,e);n.each(p||e.scales,(function(e){e.isHorizontal()&&m?S(e,a,c,s):!e.isHorizontal()&&f&&S(e,i,c,s)})),e.update(o),n.callback(l.onZoom,[{chart:e}])}function O(e,t,o,a="none"){const i=d(e),{options:{limits:c,zoom:r}}=i,{mode:l="xy"}=r;k(e,i);const u=s(l,"x",e),m=s(l,"y",e);n.each(e.scales,(function(e){e.isHorizontal()&&u?P(e,t.x,o.x,c):!e.isHorizontal()&&m&&P(e,t.y,o.y,c)})),e.update(a),n.callback(r.onZoom,[{chart:e}])}function C(e){const t=d(e);let o=1,a=1;return n.each(e.scales,(function(e){const i=function(e,t){const o=e.originalScaleLimits[t];if(!o)return;const{min:a,max:i}=o;return n.valueOrDefault(i.options,i.scale)-n.valueOrDefault(a.options,a.scale)}(t,e.id);if(i){const t=Math.round(i/(e.max-e.min)*100)/100;o=Math.min(o,t),a=Math.max(a,t)}})),o<1?o:a}function R(e,t,o,a){const{panDelta:i}=a,c=i[e.id]||0;n.sign(c)===n.sign(t)&&(t+=c);const r=z[e.type]||z.default;n.callback(r,[e,t,o])?i[e.id]=0:i[e.id]=t}function Z(e,t,o,a="none"){const{x:i=0,y:c=0}="number"==typeof t?{x:t,y:t}:t,r=d(e),{options:{pan:s,limits:l}}=r,{onPan:u}=s||{};k(e,r);const m=0!==i,f=0!==c;n.each(o||e.scales,(function(e){e.isHorizontal()&&m?R(e,i,l,r):!e.isHorizontal()&&f&&R(e,c,l,r)})),e.update(a),n.callback(u,[{chart:e}])}function T(e){const t=d(e);k(e,t);const n={};for(const o of Object.keys(e.scales)){const{min:e,max:a}=t.originalScaleLimits[o]||{min:{},max:{}};n[o]={min:e.scale,max:a.scale}}return n}function L(e,t){const{handlers:n}=d(e),o=n[t];o&&o.target&&(o.target.removeEventListener(t,o),delete n[t])}function E(e,t,n,o){const{handlers:a,options:i}=d(e),c=a[n];c&&c.target===t||(L(e,n),a[n]=t=>o(e,t,i),a[n].target=t,t.addEventListener(n,a[n]))}function F(e,t){const n=d(e);n.dragStart&&(n.dragging=!0,n.dragEnd=t,e.update("none"))}function H(e,t){const n=d(e);n.dragStart&&"Escape"===t.key&&(L(e,"keydown"),n.dragging=!1,n.dragStart=n.dragEnd=null,e.update("none"))}function Y(e,t,o){const{onZoomStart:a,onZoomRejected:i}=o;if(a){const o=n.getRelativePosition(t,e);if(!1===n.callback(a,[{chart:e,event:t,point:o}]))return n.callback(i,[{chart:e,event:t}]),!1}}function V(e,t){const o=d(e),{pan:a,zoom:s={}}=o.options;if(0!==t.button||c(i(a),t)||r(i(s.drag),t))return n.callback(s.onZoomRejected,[{chart:e,event:t}]);!1!==Y(e,t,s)&&(o.dragStart=t,E(e,e.canvas,"mousemove",F),E(e,window.document,"keydown",H))}function K(e,t,o,a){const i=s(t,"x",e),c=s(t,"y",e);let{top:r,left:l,right:u,bottom:m,width:d,height:f}=e.chartArea;const p=n.getRelativePosition(o,e),h=n.getRelativePosition(a,e);i&&(l=Math.min(p.x,h.x),u=Math.max(p.x,h.x)),c&&(r=Math.min(p.y,h.y),m=Math.max(p.y,h.y));const x=u-l,g=m-r;return{left:l,top:r,right:u,bottom:m,width:x,height:g,zoomX:i&&x?1+(d-x)/d:1,zoomY:c&&g?1+(f-g)/f:1}}function N(e,t){const o=d(e);if(!o.dragStart)return;L(e,"mousemove");const{mode:a,onZoomComplete:i,drag:{threshold:c=0}}=o.options.zoom,r=K(e,a,o.dragStart,t),l=s(a,"x",e)?r.width:0,u=s(a,"y",e)?r.height:0,m=Math.sqrt(l*l+u*u);if(o.dragStart=o.dragEnd=null,m<=c)return o.dragging=!1,void e.update("none");O(e,{x:r.left,y:r.top},{x:r.right,y:r.bottom},"zoom"),setTimeout((()=>o.dragging=!1),500),n.callback(i,[{chart:e}])}function X(e,t){const{handlers:{onZoomComplete:o},options:{zoom:a}}=d(e);if(!function(e,t,o){if(r(i(o.wheel),t))n.callback(o.onZoomRejected,[{chart:e,event:t}]);else if(!1!==Y(e,t,o)&&(t.cancelable&&t.preventDefault(),void 0!==t.deltaY))return!0}(e,t,a))return;const c=t.target.getBoundingClientRect(),s=1+(t.deltaY>=0?-a.wheel.speed:a.wheel.speed);j(e,{x:s,y:s,focalPoint:{x:t.clientX-c.left,y:t.clientY-c.top}}),o&&o()}function q(e,t,o,a){o&&(d(e).handlers[t]=function(e,t){let n;return function(){return clearTimeout(n),n=setTimeout(e,t),t}}((()=>n.callback(o,[{chart:e}])),a))}function W(e,t){return function(o,a){const{pan:s,zoom:l={}}=t.options;if(!s||!s.enabled)return!1;const u=a&&a.srcEvent;return!u||(!(!t.panning&&"mouse"===a.pointerType&&(r(i(s),u)||c(i(l.drag),u)))||(n.callback(s.onPanRejected,[{chart:e,event:a}]),!1))}}function B(e,t,n){if(t.scale){const{center:o,pointers:a}=n,i=1/t.scale*n.scale,c=n.target.getBoundingClientRect(),r=function(e,t){const n=Math.abs(e.clientX-t.clientX),o=Math.abs(e.clientY-t.clientY),a=n/o;let i,c;return a>.3&&a<1.7?i=c=!0:n>o?i=!0:c=!0,{x:i,y:c}}(a[0],a[1]),l=t.options.zoom.mode;j(e,{x:r.x&&s(l,"x",e)?i:1,y:r.y&&s(l,"y",e)?i:1,focalPoint:{x:o.x-c.left,y:o.y-c.top}}),t.scale=n.scale}}function A(e,t,n){const o=t.delta;o&&(t.panning=!0,Z(e,{x:n.deltaX-o.x,y:n.deltaY-o.y},t.panScales),t.delta={x:n.deltaX,y:n.deltaY})}const I=new WeakMap;function U(e,t){const o=d(e),i=e.canvas,{pan:c,zoom:r}=t,s=new a.default.Manager(i);r&&r.pinch.enabled&&(s.add(new a.default.Pinch),s.on("pinchstart",(()=>function(e,t){t.options.zoom.pinch.enabled&&(t.scale=1)}(0,o))),s.on("pinch",(t=>B(e,o,t))),s.on("pinchend",(t=>function(e,t,o){t.scale&&(B(e,t,o),t.scale=null,n.callback(t.options.zoom.onZoomComplete,[{chart:e}]))}(e,o,t)))),c&&c.enabled&&(s.add(new a.default.Pan({threshold:c.threshold,enable:W(e,o)})),s.on("panstart",(t=>function(e,t,o){const{enabled:a,onPanStart:i,onPanRejected:c}=t.options.pan;if(!a)return;const r=o.target.getBoundingClientRect(),s={x:o.center.x-r.left,y:o.center.y-r.top};if(!1===n.callback(i,[{chart:e,event:o,point:s}]))return n.callback(c,[{chart:e,event:o}]);t.panScales=u(t.options.pan,s,e),t.delta={x:0,y:0},clearTimeout(t.panEndTimeout),A(e,t,o)}(e,o,t))),s.on("panmove",(t=>A(e,o,t))),s.on("panend",(()=>function(e,t){t.delta=null,t.panning&&(t.panEndTimeout=setTimeout((()=>t.panning=!1),500),n.callback(t.options.pan.onPanComplete,[{chart:e}]))}(e,o)))),I.set(e,s)}function G(e,t,n){const o=n.zoom.drag,{dragStart:a,dragEnd:i}=d(e);if(o.drawTime!==t||!i)return;const{left:c,top:r,width:s,height:l}=K(e,n.zoom.mode,a,i),u=e.ctx;u.save(),u.beginPath(),u.fillStyle=o.backgroundColor||"rgba(225,225,225,0.3)",u.fillRect(c,r,s,l),o.borderWidth>0&&(u.lineWidth=o.borderWidth,u.strokeStyle=o.borderColor||"rgba(225,225,225)",u.strokeRect(c,r,s,l)),u.restore()}var J={id:"zoom",version:"2.0.1",defaults:{pan:{enabled:!1,mode:"xy",threshold:10,modifierKey:null},zoom:{wheel:{enabled:!1,speed:.1,modifierKey:null},drag:{enabled:!1,drawTime:"beforeDatasetsDraw",modifierKey:null},pinch:{enabled:!1},mode:"xy"}},start:function(e,t,o){d(e).options=o,Object.prototype.hasOwnProperty.call(o.zoom,"enabled")&&console.warn("The option `zoom.enabled` is no longer supported. Please use `zoom.wheel.enabled`, `zoom.drag.enabled`, or `zoom.pinch.enabled`."),(Object.prototype.hasOwnProperty.call(o.zoom,"overScaleMode")||Object.prototype.hasOwnProperty.call(o.pan,"overScaleMode"))&&console.warn("The option `overScaleMode` is deprecated. Please use `scaleMode` instead (and update `mode` as desired)."),a.default&&U(e,o),e.pan=(t,n,o)=>Z(e,t,n,o),e.zoom=(t,n)=>j(e,t,n),e.zoomRect=(t,n,o)=>O(e,t,n,o),e.zoomScale=(t,n,o)=>function(e,t,n,o="none"){k(e,d(e)),h(e.scales[t],n,void 0,!0),e.update(o)}(e,t,n,o),e.resetZoom=t=>function(e,t="default"){const o=d(e),a=k(e,o);n.each(e.scales,(function(e){const t=e.options;a[e.id]?(t.min=a[e.id].min.options,t.max=a[e.id].max.options):(delete t.min,delete t.max)})),e.update(t),n.callback(o.options.zoom.onZoomComplete,[{chart:e}])}(e,t),e.getZoomLevel=()=>C(e),e.getInitialScaleBounds=()=>T(e),e.isZoomedOrPanned=()=>function(e){const t=T(e);for(const n of Object.keys(e.scales)){const{min:o,max:a}=t[n];if(void 0!==o&&e.scales[n].min!==o)return!0;if(void 0!==a&&e.scales[n].max!==a)return!0}return!1}(e)},beforeEvent(e){const t=d(e);if(t.panning||t.dragging)return!1},beforeUpdate:function(e,t,n){d(e).options=n,function(e,t){const n=e.canvas,{wheel:o,drag:a,onZoomComplete:i}=t.zoom;o.enabled?(E(e,n,"wheel",X),q(e,"onZoomComplete",i,250)):L(e,"wheel"),a.enabled?(E(e,n,"mousedown",V),E(e,n.ownerDocument,"mouseup",N)):(L(e,"mousedown"),L(e,"mousemove"),L(e,"mouseup"),L(e,"keydown"))}(e,n)},beforeDatasetsDraw(e,t,n){G(e,"beforeDatasetsDraw",n)},afterDatasetsDraw(e,t,n){G(e,"afterDatasetsDraw",n)},beforeDraw(e,t,n){G(e,"beforeDraw",n)},afterDraw(e,t,n){G(e,"afterDraw",n)},stop:function(e){!function(e){L(e,"mousedown"),L(e,"mousemove"),L(e,"mouseup"),L(e,"wheel"),L(e,"click"),L(e,"keydown")}(e),a.default&&function(e){const t=I.get(e);t&&(t.remove("pinchstart"),t.remove("pinch"),t.remove("pinchend"),t.remove("panstart"),t.remove("pan"),t.remove("panend"),t.destroy(),I.delete(e))}(e),function(e){m.delete(e)}(e)},panFunctions:z,zoomFunctions:v,zoomRectFunctions:w};return e.Chart.register(J),J})); diff --git a/static/js/chunks/helpers.segment.js b/static/js/chunks/helpers.segment.js index bc4fc994..5cffd79e 100644 --- a/static/js/chunks/helpers.segment.js +++ b/static/js/chunks/helpers.segment.js @@ -1,54 +1,9 @@ /*! - * Chart.js v3.7.1 + * Chart.js v3.9.1 * https://www.chartjs.org * (c) 2022 Chart.js Contributors * Released under the MIT License */ -function fontString(pixelSize, fontStyle, fontFamily) { - return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; -} -const requestAnimFrame = (function() { - if (typeof window === 'undefined') { - return function(callback) { - return callback(); - }; - } - return window.requestAnimationFrame; -}()); -function throttled(fn, thisArg, updateFn) { - const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args)); - let ticking = false; - let args = []; - return function(...rest) { - args = updateArgs(rest); - if (!ticking) { - ticking = true; - requestAnimFrame.call(window, () => { - ticking = false; - fn.apply(thisArg, args); - }); - } - }; -} -function debounce(fn, delay) { - let timeout; - return function(...args) { - if (delay) { - clearTimeout(timeout); - timeout = setTimeout(fn, delay, args); - } else { - fn.apply(this, args); - } - return delay; - }; -} -const _toLeftRightCenter = (align) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; -const _alignStartEnd = (align, start, end) => align === 'start' ? start : align === 'end' ? end : (start + end) / 2; -const _textX = (align, left, right, rtl) => { - const check = rtl ? 'left' : 'right'; - return align === check ? right : align === 'center' ? (left + right) / 2 : left; -}; - function noop() {} const uid = (function() { let id = 0; @@ -64,7 +19,7 @@ function isArray(value) { return true; } const type = Object.prototype.toString.call(value); - if (type.substr(0, 7) === '[object' && type.substr(-6) === 'Array]') { + if (type.slice(0, 7) === '[object' && type.slice(-6) === 'Array]') { return true; } return false; @@ -199,24 +154,41 @@ function _deprecated(scope, value, previous, current) { '" is deprecated. Please use "' + current + '" instead'); } } -const emptyString = ''; -const dot = '.'; -function indexOfDotOrLength(key, start) { - const idx = key.indexOf(dot, start); - return idx === -1 ? key.length : idx; -} +const keyResolvers = { + '': v => v, + x: o => o.x, + y: o => o.y +}; function resolveObjectKey(obj, key) { - if (key === emptyString) { + const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); + return resolver(obj); +} +function _getKeyResolver(key) { + const keys = _splitKey(key); + return obj => { + for (const k of keys) { + if (k === '') { + break; + } + obj = obj && obj[k]; + } return obj; + }; +} +function _splitKey(key) { + const parts = key.split('.'); + const keys = []; + let tmp = ''; + for (const part of parts) { + tmp += part; + if (tmp.endsWith('\\')) { + tmp = tmp.slice(0, -1) + '.'; + } else { + keys.push(tmp); + tmp = ''; + } } - let pos = 0; - let idx = indexOfDotOrLength(key, pos); - while (obj && idx > pos) { - obj = obj[key.substr(pos, idx - pos)]; - pos = idx + 1; - idx = indexOfDotOrLength(key, pos); - } - return obj; + return keys; } function _capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); @@ -353,6 +325,190 @@ function _isBetween(value, start, end, epsilon = 1e-6) { return value >= Math.min(start, end) - epsilon && value <= Math.max(start, end) + epsilon; } +function _lookup(table, value, cmp) { + cmp = cmp || ((index) => table[index] < value); + let hi = table.length - 1; + let lo = 0; + let mid; + while (hi - lo > 1) { + mid = (lo + hi) >> 1; + if (cmp(mid)) { + lo = mid; + } else { + hi = mid; + } + } + return {lo, hi}; +} +const _lookupByKey = (table, key, value, last) => + _lookup(table, value, last + ? index => table[index][key] <= value + : index => table[index][key] < value); +const _rlookupByKey = (table, key, value) => + _lookup(table, value, index => table[index][key] >= value); +function _filterBetween(values, min, max) { + let start = 0; + let end = values.length; + while (start < end && values[start] < min) { + start++; + } + while (end > start && values[end - 1] > max) { + end--; + } + return start > 0 || end < values.length + ? values.slice(start, end) + : values; +} +const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift']; +function listenArrayEvents(array, listener) { + if (array._chartjs) { + array._chartjs.listeners.push(listener); + return; + } + Object.defineProperty(array, '_chartjs', { + configurable: true, + enumerable: false, + value: { + listeners: [listener] + } + }); + arrayEvents.forEach((key) => { + const method = '_onData' + _capitalize(key); + const base = array[key]; + Object.defineProperty(array, key, { + configurable: true, + enumerable: false, + value(...args) { + const res = base.apply(this, args); + array._chartjs.listeners.forEach((object) => { + if (typeof object[method] === 'function') { + object[method](...args); + } + }); + return res; + } + }); + }); +} +function unlistenArrayEvents(array, listener) { + const stub = array._chartjs; + if (!stub) { + return; + } + const listeners = stub.listeners; + const index = listeners.indexOf(listener); + if (index !== -1) { + listeners.splice(index, 1); + } + if (listeners.length > 0) { + return; + } + arrayEvents.forEach((key) => { + delete array[key]; + }); + delete array._chartjs; +} +function _arrayUnique(items) { + const set = new Set(); + let i, ilen; + for (i = 0, ilen = items.length; i < ilen; ++i) { + set.add(items[i]); + } + if (set.size === ilen) { + return items; + } + return Array.from(set); +} + +function fontString(pixelSize, fontStyle, fontFamily) { + return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; +} +const requestAnimFrame = (function() { + if (typeof window === 'undefined') { + return function(callback) { + return callback(); + }; + } + return window.requestAnimationFrame; +}()); +function throttled(fn, thisArg, updateFn) { + const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args)); + let ticking = false; + let args = []; + return function(...rest) { + args = updateArgs(rest); + if (!ticking) { + ticking = true; + requestAnimFrame.call(window, () => { + ticking = false; + fn.apply(thisArg, args); + }); + } + }; +} +function debounce(fn, delay) { + let timeout; + return function(...args) { + if (delay) { + clearTimeout(timeout); + timeout = setTimeout(fn, delay, args); + } else { + fn.apply(this, args); + } + return delay; + }; +} +const _toLeftRightCenter = (align) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; +const _alignStartEnd = (align, start, end) => align === 'start' ? start : align === 'end' ? end : (start + end) / 2; +const _textX = (align, left, right, rtl) => { + const check = rtl ? 'left' : 'right'; + return align === check ? right : align === 'center' ? (left + right) / 2 : left; +}; +function _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { + const pointCount = points.length; + let start = 0; + let count = pointCount; + if (meta._sorted) { + const {iScale, _parsed} = meta; + const axis = iScale.axis; + const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); + if (minDefined) { + start = _limitValue(Math.min( + _lookupByKey(_parsed, iScale.axis, min).lo, + animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo), + 0, pointCount - 1); + } + if (maxDefined) { + count = _limitValue(Math.max( + _lookupByKey(_parsed, iScale.axis, max, true).hi + 1, + animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max), true).hi + 1), + start, pointCount) - start; + } else { + count = pointCount - start; + } + } + return {start, count}; +} +function _scaleRangesChanged(meta) { + const {xScale, yScale, _scaleRanges} = meta; + const newRanges = { + xmin: xScale.min, + xmax: xScale.max, + ymin: yScale.min, + ymax: yScale.max + }; + if (!_scaleRanges) { + meta._scaleRanges = newRanges; + return true; + } + const changed = _scaleRanges.xmin !== xScale.min + || _scaleRanges.xmax !== xScale.max + || _scaleRanges.ymin !== yScale.min + || _scaleRanges.ymax !== yScale.max; + Object.assign(_scaleRanges, newRanges); + return changed; +} + const atEdge = (t) => t === 0 || t === 1; const elasticIn = (t, s, p) => -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p)); const elasticOut = (t, s, p) => Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1; @@ -437,561 +593,589 @@ const effects = { }; /*! - * @kurkle/color v0.1.9 + * @kurkle/color v0.2.1 * https://github.com/kurkle/color#readme - * (c) 2020 Jukka Kurkela + * (c) 2022 Jukka Kurkela * Released under the MIT License */ -const map = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15}; -const hex = '0123456789ABCDEF'; -const h1 = (b) => hex[b & 0xF]; -const h2 = (b) => hex[(b & 0xF0) >> 4] + hex[b & 0xF]; -const eq = (b) => (((b & 0xF0) >> 4) === (b & 0xF)); -function isShort(v) { - return eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a); -} -function hexParse(str) { - var len = str.length; - var ret; - if (str[0] === '#') { - if (len === 4 || len === 5) { - ret = { - r: 255 & map[str[1]] * 17, - g: 255 & map[str[2]] * 17, - b: 255 & map[str[3]] * 17, - a: len === 5 ? map[str[4]] * 17 : 255 - }; - } else if (len === 7 || len === 9) { - ret = { - r: map[str[1]] << 4 | map[str[2]], - g: map[str[3]] << 4 | map[str[4]], - b: map[str[5]] << 4 | map[str[6]], - a: len === 9 ? (map[str[7]] << 4 | map[str[8]]) : 255 - }; - } - } - return ret; -} -function hexString(v) { - var f = isShort(v) ? h1 : h2; - return v - ? '#' + f(v.r) + f(v.g) + f(v.b) + (v.a < 255 ? f(v.a) : '') - : v; -} function round(v) { - return v + 0.5 | 0; + return v + 0.5 | 0; } const lim = (v, l, h) => Math.max(Math.min(v, h), l); function p2b(v) { - return lim(round(v * 2.55), 0, 255); + return lim(round(v * 2.55), 0, 255); } function n2b(v) { - return lim(round(v * 255), 0, 255); + return lim(round(v * 255), 0, 255); } function b2n(v) { - return lim(round(v / 2.55) / 100, 0, 1); + return lim(round(v / 2.55) / 100, 0, 1); } function n2p(v) { - return lim(round(v * 100), 0, 100); -} -const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/; -function rgbParse(str) { - const m = RGB_RE.exec(str); - let a = 255; - let r, g, b; - if (!m) { - return; - } - if (m[7] !== r) { - const v = +m[7]; - a = 255 & (m[8] ? p2b(v) : v * 255); - } - r = +m[1]; - g = +m[3]; - b = +m[5]; - r = 255 & (m[2] ? p2b(r) : r); - g = 255 & (m[4] ? p2b(g) : g); - b = 255 & (m[6] ? p2b(b) : b); - return { - r: r, - g: g, - b: b, - a: a - }; + return lim(round(v * 100), 0, 100); +} +const map$1 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15}; +const hex = [...'0123456789ABCDEF']; +const h1 = b => hex[b & 0xF]; +const h2 = b => hex[(b & 0xF0) >> 4] + hex[b & 0xF]; +const eq = b => ((b & 0xF0) >> 4) === (b & 0xF); +const isShort = v => eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a); +function hexParse(str) { + var len = str.length; + var ret; + if (str[0] === '#') { + if (len === 4 || len === 5) { + ret = { + r: 255 & map$1[str[1]] * 17, + g: 255 & map$1[str[2]] * 17, + b: 255 & map$1[str[3]] * 17, + a: len === 5 ? map$1[str[4]] * 17 : 255 + }; + } else if (len === 7 || len === 9) { + ret = { + r: map$1[str[1]] << 4 | map$1[str[2]], + g: map$1[str[3]] << 4 | map$1[str[4]], + b: map$1[str[5]] << 4 | map$1[str[6]], + a: len === 9 ? (map$1[str[7]] << 4 | map$1[str[8]]) : 255 + }; + } + } + return ret; } -function rgbString(v) { - return v && ( - v.a < 255 - ? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})` - : `rgb(${v.r}, ${v.g}, ${v.b})` - ); +const alpha = (a, f) => a < 255 ? f(a) : ''; +function hexString(v) { + var f = isShort(v) ? h1 : h2; + return v + ? '#' + f(v.r) + f(v.g) + f(v.b) + alpha(v.a, f) + : undefined; } const HUE_RE = /^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/; function hsl2rgbn(h, s, l) { - const a = s * Math.min(l, 1 - l); - const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); - return [f(0), f(8), f(4)]; + const a = s * Math.min(l, 1 - l); + const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return [f(0), f(8), f(4)]; } function hsv2rgbn(h, s, v) { - const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0); - return [f(5), f(3), f(1)]; + const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0); + return [f(5), f(3), f(1)]; } function hwb2rgbn(h, w, b) { - const rgb = hsl2rgbn(h, 1, 0.5); - let i; - if (w + b > 1) { - i = 1 / (w + b); - w *= i; - b *= i; - } - for (i = 0; i < 3; i++) { - rgb[i] *= 1 - w - b; - rgb[i] += w; - } - return rgb; + const rgb = hsl2rgbn(h, 1, 0.5); + let i; + if (w + b > 1) { + i = 1 / (w + b); + w *= i; + b *= i; + } + for (i = 0; i < 3; i++) { + rgb[i] *= 1 - w - b; + rgb[i] += w; + } + return rgb; +} +function hueValue(r, g, b, d, max) { + if (r === max) { + return ((g - b) / d) + (g < b ? 6 : 0); + } + if (g === max) { + return (b - r) / d + 2; + } + return (r - g) / d + 4; } function rgb2hsl(v) { - const range = 255; - const r = v.r / range; - const g = v.g / range; - const b = v.b / range; - const max = Math.max(r, g, b); - const min = Math.min(r, g, b); - const l = (max + min) / 2; - let h, s, d; - if (max !== min) { - d = max - min; - s = l > 0.5 ? d / (2 - max - min) : d / (max + min); - h = max === r - ? ((g - b) / d) + (g < b ? 6 : 0) - : max === g - ? (b - r) / d + 2 - : (r - g) / d + 4; - h = h * 60 + 0.5; - } - return [h | 0, s || 0, l]; + const range = 255; + const r = v.r / range; + const g = v.g / range; + const b = v.b / range; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const l = (max + min) / 2; + let h, s, d; + if (max !== min) { + d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + h = hueValue(r, g, b, d, max); + h = h * 60 + 0.5; + } + return [h | 0, s || 0, l]; } function calln(f, a, b, c) { - return ( - Array.isArray(a) - ? f(a[0], a[1], a[2]) - : f(a, b, c) - ).map(n2b); + return ( + Array.isArray(a) + ? f(a[0], a[1], a[2]) + : f(a, b, c) + ).map(n2b); } function hsl2rgb(h, s, l) { - return calln(hsl2rgbn, h, s, l); + return calln(hsl2rgbn, h, s, l); } function hwb2rgb(h, w, b) { - return calln(hwb2rgbn, h, w, b); + return calln(hwb2rgbn, h, w, b); } function hsv2rgb(h, s, v) { - return calln(hsv2rgbn, h, s, v); + return calln(hsv2rgbn, h, s, v); } function hue(h) { - return (h % 360 + 360) % 360; + return (h % 360 + 360) % 360; } function hueParse(str) { - const m = HUE_RE.exec(str); - let a = 255; - let v; - if (!m) { - return; - } - if (m[5] !== v) { - a = m[6] ? p2b(+m[5]) : n2b(+m[5]); - } - const h = hue(+m[2]); - const p1 = +m[3] / 100; - const p2 = +m[4] / 100; - if (m[1] === 'hwb') { - v = hwb2rgb(h, p1, p2); - } else if (m[1] === 'hsv') { - v = hsv2rgb(h, p1, p2); - } else { - v = hsl2rgb(h, p1, p2); - } - return { - r: v[0], - g: v[1], - b: v[2], - a: a - }; + const m = HUE_RE.exec(str); + let a = 255; + let v; + if (!m) { + return; + } + if (m[5] !== v) { + a = m[6] ? p2b(+m[5]) : n2b(+m[5]); + } + const h = hue(+m[2]); + const p1 = +m[3] / 100; + const p2 = +m[4] / 100; + if (m[1] === 'hwb') { + v = hwb2rgb(h, p1, p2); + } else if (m[1] === 'hsv') { + v = hsv2rgb(h, p1, p2); + } else { + v = hsl2rgb(h, p1, p2); + } + return { + r: v[0], + g: v[1], + b: v[2], + a: a + }; } function rotate(v, deg) { - var h = rgb2hsl(v); - h[0] = hue(h[0] + deg); - h = hsl2rgb(h); - v.r = h[0]; - v.g = h[1]; - v.b = h[2]; + var h = rgb2hsl(v); + h[0] = hue(h[0] + deg); + h = hsl2rgb(h); + v.r = h[0]; + v.g = h[1]; + v.b = h[2]; } function hslString(v) { - if (!v) { - return; - } - const a = rgb2hsl(v); - const h = a[0]; - const s = n2p(a[1]); - const l = n2p(a[2]); - return v.a < 255 - ? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})` - : `hsl(${h}, ${s}%, ${l}%)`; -} -const map$1 = { - x: 'dark', - Z: 'light', - Y: 're', - X: 'blu', - W: 'gr', - V: 'medium', - U: 'slate', - A: 'ee', - T: 'ol', - S: 'or', - B: 'ra', - C: 'lateg', - D: 'ights', - R: 'in', - Q: 'turquois', - E: 'hi', - P: 'ro', - O: 'al', - N: 'le', - M: 'de', - L: 'yello', - F: 'en', - K: 'ch', - G: 'arks', - H: 'ea', - I: 'ightg', - J: 'wh' + if (!v) { + return; + } + const a = rgb2hsl(v); + const h = a[0]; + const s = n2p(a[1]); + const l = n2p(a[2]); + return v.a < 255 + ? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})` + : `hsl(${h}, ${s}%, ${l}%)`; +} +const map = { + x: 'dark', + Z: 'light', + Y: 're', + X: 'blu', + W: 'gr', + V: 'medium', + U: 'slate', + A: 'ee', + T: 'ol', + S: 'or', + B: 'ra', + C: 'lateg', + D: 'ights', + R: 'in', + Q: 'turquois', + E: 'hi', + P: 'ro', + O: 'al', + N: 'le', + M: 'de', + L: 'yello', + F: 'en', + K: 'ch', + G: 'arks', + H: 'ea', + I: 'ightg', + J: 'wh' }; -const names = { - OiceXe: 'f0f8ff', - antiquewEte: 'faebd7', - aqua: 'ffff', - aquamarRe: '7fffd4', - azuY: 'f0ffff', - beige: 'f5f5dc', - bisque: 'ffe4c4', - black: '0', - blanKedOmond: 'ffebcd', - Xe: 'ff', - XeviTet: '8a2be2', - bPwn: 'a52a2a', - burlywood: 'deb887', - caMtXe: '5f9ea0', - KartYuse: '7fff00', - KocTate: 'd2691e', - cSO: 'ff7f50', - cSnflowerXe: '6495ed', - cSnsilk: 'fff8dc', - crimson: 'dc143c', - cyan: 'ffff', - xXe: '8b', - xcyan: '8b8b', - xgTMnPd: 'b8860b', - xWay: 'a9a9a9', - xgYF: '6400', - xgYy: 'a9a9a9', - xkhaki: 'bdb76b', - xmagFta: '8b008b', - xTivegYF: '556b2f', - xSange: 'ff8c00', - xScEd: '9932cc', - xYd: '8b0000', - xsOmon: 'e9967a', - xsHgYF: '8fbc8f', - xUXe: '483d8b', - xUWay: '2f4f4f', - xUgYy: '2f4f4f', - xQe: 'ced1', - xviTet: '9400d3', - dAppRk: 'ff1493', - dApskyXe: 'bfff', - dimWay: '696969', - dimgYy: '696969', - dodgerXe: '1e90ff', - fiYbrick: 'b22222', - flSOwEte: 'fffaf0', - foYstWAn: '228b22', - fuKsia: 'ff00ff', - gaRsbSo: 'dcdcdc', - ghostwEte: 'f8f8ff', - gTd: 'ffd700', - gTMnPd: 'daa520', - Way: '808080', - gYF: '8000', - gYFLw: 'adff2f', - gYy: '808080', - honeyMw: 'f0fff0', - hotpRk: 'ff69b4', - RdianYd: 'cd5c5c', - Rdigo: '4b0082', - ivSy: 'fffff0', - khaki: 'f0e68c', - lavFMr: 'e6e6fa', - lavFMrXsh: 'fff0f5', - lawngYF: '7cfc00', - NmoncEffon: 'fffacd', - ZXe: 'add8e6', - ZcSO: 'f08080', - Zcyan: 'e0ffff', - ZgTMnPdLw: 'fafad2', - ZWay: 'd3d3d3', - ZgYF: '90ee90', - ZgYy: 'd3d3d3', - ZpRk: 'ffb6c1', - ZsOmon: 'ffa07a', - ZsHgYF: '20b2aa', - ZskyXe: '87cefa', - ZUWay: '778899', - ZUgYy: '778899', - ZstAlXe: 'b0c4de', - ZLw: 'ffffe0', - lime: 'ff00', - limegYF: '32cd32', - lRF: 'faf0e6', - magFta: 'ff00ff', - maPon: '800000', - VaquamarRe: '66cdaa', - VXe: 'cd', - VScEd: 'ba55d3', - VpurpN: '9370db', - VsHgYF: '3cb371', - VUXe: '7b68ee', - VsprRggYF: 'fa9a', - VQe: '48d1cc', - VviTetYd: 'c71585', - midnightXe: '191970', - mRtcYam: 'f5fffa', - mistyPse: 'ffe4e1', - moccasR: 'ffe4b5', - navajowEte: 'ffdead', - navy: '80', - Tdlace: 'fdf5e6', - Tive: '808000', - TivedBb: '6b8e23', - Sange: 'ffa500', - SangeYd: 'ff4500', - ScEd: 'da70d6', - pOegTMnPd: 'eee8aa', - pOegYF: '98fb98', - pOeQe: 'afeeee', - pOeviTetYd: 'db7093', - papayawEp: 'ffefd5', - pHKpuff: 'ffdab9', - peru: 'cd853f', - pRk: 'ffc0cb', - plum: 'dda0dd', - powMrXe: 'b0e0e6', - purpN: '800080', - YbeccapurpN: '663399', - Yd: 'ff0000', - Psybrown: 'bc8f8f', - PyOXe: '4169e1', - saddNbPwn: '8b4513', - sOmon: 'fa8072', - sandybPwn: 'f4a460', - sHgYF: '2e8b57', - sHshell: 'fff5ee', - siFna: 'a0522d', - silver: 'c0c0c0', - skyXe: '87ceeb', - UXe: '6a5acd', - UWay: '708090', - UgYy: '708090', - snow: 'fffafa', - sprRggYF: 'ff7f', - stAlXe: '4682b4', - tan: 'd2b48c', - teO: '8080', - tEstN: 'd8bfd8', - tomato: 'ff6347', - Qe: '40e0d0', - viTet: 'ee82ee', - JHt: 'f5deb3', - wEte: 'ffffff', - wEtesmoke: 'f5f5f5', - Lw: 'ffff00', - LwgYF: '9acd32' +const names$1 = { + OiceXe: 'f0f8ff', + antiquewEte: 'faebd7', + aqua: 'ffff', + aquamarRe: '7fffd4', + azuY: 'f0ffff', + beige: 'f5f5dc', + bisque: 'ffe4c4', + black: '0', + blanKedOmond: 'ffebcd', + Xe: 'ff', + XeviTet: '8a2be2', + bPwn: 'a52a2a', + burlywood: 'deb887', + caMtXe: '5f9ea0', + KartYuse: '7fff00', + KocTate: 'd2691e', + cSO: 'ff7f50', + cSnflowerXe: '6495ed', + cSnsilk: 'fff8dc', + crimson: 'dc143c', + cyan: 'ffff', + xXe: '8b', + xcyan: '8b8b', + xgTMnPd: 'b8860b', + xWay: 'a9a9a9', + xgYF: '6400', + xgYy: 'a9a9a9', + xkhaki: 'bdb76b', + xmagFta: '8b008b', + xTivegYF: '556b2f', + xSange: 'ff8c00', + xScEd: '9932cc', + xYd: '8b0000', + xsOmon: 'e9967a', + xsHgYF: '8fbc8f', + xUXe: '483d8b', + xUWay: '2f4f4f', + xUgYy: '2f4f4f', + xQe: 'ced1', + xviTet: '9400d3', + dAppRk: 'ff1493', + dApskyXe: 'bfff', + dimWay: '696969', + dimgYy: '696969', + dodgerXe: '1e90ff', + fiYbrick: 'b22222', + flSOwEte: 'fffaf0', + foYstWAn: '228b22', + fuKsia: 'ff00ff', + gaRsbSo: 'dcdcdc', + ghostwEte: 'f8f8ff', + gTd: 'ffd700', + gTMnPd: 'daa520', + Way: '808080', + gYF: '8000', + gYFLw: 'adff2f', + gYy: '808080', + honeyMw: 'f0fff0', + hotpRk: 'ff69b4', + RdianYd: 'cd5c5c', + Rdigo: '4b0082', + ivSy: 'fffff0', + khaki: 'f0e68c', + lavFMr: 'e6e6fa', + lavFMrXsh: 'fff0f5', + lawngYF: '7cfc00', + NmoncEffon: 'fffacd', + ZXe: 'add8e6', + ZcSO: 'f08080', + Zcyan: 'e0ffff', + ZgTMnPdLw: 'fafad2', + ZWay: 'd3d3d3', + ZgYF: '90ee90', + ZgYy: 'd3d3d3', + ZpRk: 'ffb6c1', + ZsOmon: 'ffa07a', + ZsHgYF: '20b2aa', + ZskyXe: '87cefa', + ZUWay: '778899', + ZUgYy: '778899', + ZstAlXe: 'b0c4de', + ZLw: 'ffffe0', + lime: 'ff00', + limegYF: '32cd32', + lRF: 'faf0e6', + magFta: 'ff00ff', + maPon: '800000', + VaquamarRe: '66cdaa', + VXe: 'cd', + VScEd: 'ba55d3', + VpurpN: '9370db', + VsHgYF: '3cb371', + VUXe: '7b68ee', + VsprRggYF: 'fa9a', + VQe: '48d1cc', + VviTetYd: 'c71585', + midnightXe: '191970', + mRtcYam: 'f5fffa', + mistyPse: 'ffe4e1', + moccasR: 'ffe4b5', + navajowEte: 'ffdead', + navy: '80', + Tdlace: 'fdf5e6', + Tive: '808000', + TivedBb: '6b8e23', + Sange: 'ffa500', + SangeYd: 'ff4500', + ScEd: 'da70d6', + pOegTMnPd: 'eee8aa', + pOegYF: '98fb98', + pOeQe: 'afeeee', + pOeviTetYd: 'db7093', + papayawEp: 'ffefd5', + pHKpuff: 'ffdab9', + peru: 'cd853f', + pRk: 'ffc0cb', + plum: 'dda0dd', + powMrXe: 'b0e0e6', + purpN: '800080', + YbeccapurpN: '663399', + Yd: 'ff0000', + Psybrown: 'bc8f8f', + PyOXe: '4169e1', + saddNbPwn: '8b4513', + sOmon: 'fa8072', + sandybPwn: 'f4a460', + sHgYF: '2e8b57', + sHshell: 'fff5ee', + siFna: 'a0522d', + silver: 'c0c0c0', + skyXe: '87ceeb', + UXe: '6a5acd', + UWay: '708090', + UgYy: '708090', + snow: 'fffafa', + sprRggYF: 'ff7f', + stAlXe: '4682b4', + tan: 'd2b48c', + teO: '8080', + tEstN: 'd8bfd8', + tomato: 'ff6347', + Qe: '40e0d0', + viTet: 'ee82ee', + JHt: 'f5deb3', + wEte: 'ffffff', + wEtesmoke: 'f5f5f5', + Lw: 'ffff00', + LwgYF: '9acd32' }; function unpack() { - const unpacked = {}; - const keys = Object.keys(names); - const tkeys = Object.keys(map$1); - let i, j, k, ok, nk; - for (i = 0; i < keys.length; i++) { - ok = nk = keys[i]; - for (j = 0; j < tkeys.length; j++) { - k = tkeys[j]; - nk = nk.replace(k, map$1[k]); - } - k = parseInt(names[ok], 16); - unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF]; - } - return unpacked; -} -let names$1; + const unpacked = {}; + const keys = Object.keys(names$1); + const tkeys = Object.keys(map); + let i, j, k, ok, nk; + for (i = 0; i < keys.length; i++) { + ok = nk = keys[i]; + for (j = 0; j < tkeys.length; j++) { + k = tkeys[j]; + nk = nk.replace(k, map[k]); + } + k = parseInt(names$1[ok], 16); + unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF]; + } + return unpacked; +} +let names; function nameParse(str) { - if (!names$1) { - names$1 = unpack(); - names$1.transparent = [0, 0, 0, 0]; - } - const a = names$1[str.toLowerCase()]; - return a && { - r: a[0], - g: a[1], - b: a[2], - a: a.length === 4 ? a[3] : 255 - }; + if (!names) { + names = unpack(); + names.transparent = [0, 0, 0, 0]; + } + const a = names[str.toLowerCase()]; + return a && { + r: a[0], + g: a[1], + b: a[2], + a: a.length === 4 ? a[3] : 255 + }; +} +const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/; +function rgbParse(str) { + const m = RGB_RE.exec(str); + let a = 255; + let r, g, b; + if (!m) { + return; + } + if (m[7] !== r) { + const v = +m[7]; + a = m[8] ? p2b(v) : lim(v * 255, 0, 255); + } + r = +m[1]; + g = +m[3]; + b = +m[5]; + r = 255 & (m[2] ? p2b(r) : lim(r, 0, 255)); + g = 255 & (m[4] ? p2b(g) : lim(g, 0, 255)); + b = 255 & (m[6] ? p2b(b) : lim(b, 0, 255)); + return { + r: r, + g: g, + b: b, + a: a + }; +} +function rgbString(v) { + return v && ( + v.a < 255 + ? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})` + : `rgb(${v.r}, ${v.g}, ${v.b})` + ); +} +const to = v => v <= 0.0031308 ? v * 12.92 : Math.pow(v, 1.0 / 2.4) * 1.055 - 0.055; +const from = v => v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); +function interpolate(rgb1, rgb2, t) { + const r = from(b2n(rgb1.r)); + const g = from(b2n(rgb1.g)); + const b = from(b2n(rgb1.b)); + return { + r: n2b(to(r + t * (from(b2n(rgb2.r)) - r))), + g: n2b(to(g + t * (from(b2n(rgb2.g)) - g))), + b: n2b(to(b + t * (from(b2n(rgb2.b)) - b))), + a: rgb1.a + t * (rgb2.a - rgb1.a) + }; } function modHSL(v, i, ratio) { - if (v) { - let tmp = rgb2hsl(v); - tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1)); - tmp = hsl2rgb(tmp); - v.r = tmp[0]; - v.g = tmp[1]; - v.b = tmp[2]; - } + if (v) { + let tmp = rgb2hsl(v); + tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1)); + tmp = hsl2rgb(tmp); + v.r = tmp[0]; + v.g = tmp[1]; + v.b = tmp[2]; + } } function clone(v, proto) { - return v ? Object.assign(proto || {}, v) : v; + return v ? Object.assign(proto || {}, v) : v; } function fromObject(input) { - var v = {r: 0, g: 0, b: 0, a: 255}; - if (Array.isArray(input)) { - if (input.length >= 3) { - v = {r: input[0], g: input[1], b: input[2], a: 255}; - if (input.length > 3) { - v.a = n2b(input[3]); - } - } - } else { - v = clone(input, {r: 0, g: 0, b: 0, a: 1}); - v.a = n2b(v.a); - } - return v; + var v = {r: 0, g: 0, b: 0, a: 255}; + if (Array.isArray(input)) { + if (input.length >= 3) { + v = {r: input[0], g: input[1], b: input[2], a: 255}; + if (input.length > 3) { + v.a = n2b(input[3]); + } + } + } else { + v = clone(input, {r: 0, g: 0, b: 0, a: 1}); + v.a = n2b(v.a); + } + return v; } function functionParse(str) { - if (str.charAt(0) === 'r') { - return rgbParse(str); - } - return hueParse(str); + if (str.charAt(0) === 'r') { + return rgbParse(str); + } + return hueParse(str); } class Color { - constructor(input) { - if (input instanceof Color) { - return input; - } - const type = typeof input; - let v; - if (type === 'object') { - v = fromObject(input); - } else if (type === 'string') { - v = hexParse(input) || nameParse(input) || functionParse(input); - } - this._rgb = v; - this._valid = !!v; - } - get valid() { - return this._valid; - } - get rgb() { - var v = clone(this._rgb); - if (v) { - v.a = b2n(v.a); - } - return v; - } - set rgb(obj) { - this._rgb = fromObject(obj); - } - rgbString() { - return this._valid ? rgbString(this._rgb) : this._rgb; - } - hexString() { - return this._valid ? hexString(this._rgb) : this._rgb; - } - hslString() { - return this._valid ? hslString(this._rgb) : this._rgb; - } - mix(color, weight) { - const me = this; - if (color) { - const c1 = me.rgb; - const c2 = color.rgb; - let w2; - const p = weight === w2 ? 0.5 : weight; - const w = 2 * p - 1; - const a = c1.a - c2.a; - const w1 = ((w * a === -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0; - w2 = 1 - w1; - c1.r = 0xFF & w1 * c1.r + w2 * c2.r + 0.5; - c1.g = 0xFF & w1 * c1.g + w2 * c2.g + 0.5; - c1.b = 0xFF & w1 * c1.b + w2 * c2.b + 0.5; - c1.a = p * c1.a + (1 - p) * c2.a; - me.rgb = c1; - } - return me; - } - clone() { - return new Color(this.rgb); - } - alpha(a) { - this._rgb.a = n2b(a); - return this; - } - clearer(ratio) { - const rgb = this._rgb; - rgb.a *= 1 - ratio; - return this; - } - greyscale() { - const rgb = this._rgb; - const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11); - rgb.r = rgb.g = rgb.b = val; - return this; - } - opaquer(ratio) { - const rgb = this._rgb; - rgb.a *= 1 + ratio; - return this; - } - negate() { - const v = this._rgb; - v.r = 255 - v.r; - v.g = 255 - v.g; - v.b = 255 - v.b; - return this; - } - lighten(ratio) { - modHSL(this._rgb, 2, ratio); - return this; - } - darken(ratio) { - modHSL(this._rgb, 2, -ratio); - return this; - } - saturate(ratio) { - modHSL(this._rgb, 1, ratio); - return this; - } - desaturate(ratio) { - modHSL(this._rgb, 1, -ratio); - return this; - } - rotate(deg) { - rotate(this._rgb, deg); - return this; - } + constructor(input) { + if (input instanceof Color) { + return input; + } + const type = typeof input; + let v; + if (type === 'object') { + v = fromObject(input); + } else if (type === 'string') { + v = hexParse(input) || nameParse(input) || functionParse(input); + } + this._rgb = v; + this._valid = !!v; + } + get valid() { + return this._valid; + } + get rgb() { + var v = clone(this._rgb); + if (v) { + v.a = b2n(v.a); + } + return v; + } + set rgb(obj) { + this._rgb = fromObject(obj); + } + rgbString() { + return this._valid ? rgbString(this._rgb) : undefined; + } + hexString() { + return this._valid ? hexString(this._rgb) : undefined; + } + hslString() { + return this._valid ? hslString(this._rgb) : undefined; + } + mix(color, weight) { + if (color) { + const c1 = this.rgb; + const c2 = color.rgb; + let w2; + const p = weight === w2 ? 0.5 : weight; + const w = 2 * p - 1; + const a = c1.a - c2.a; + const w1 = ((w * a === -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0; + w2 = 1 - w1; + c1.r = 0xFF & w1 * c1.r + w2 * c2.r + 0.5; + c1.g = 0xFF & w1 * c1.g + w2 * c2.g + 0.5; + c1.b = 0xFF & w1 * c1.b + w2 * c2.b + 0.5; + c1.a = p * c1.a + (1 - p) * c2.a; + this.rgb = c1; + } + return this; + } + interpolate(color, t) { + if (color) { + this._rgb = interpolate(this._rgb, color._rgb, t); + } + return this; + } + clone() { + return new Color(this.rgb); + } + alpha(a) { + this._rgb.a = n2b(a); + return this; + } + clearer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 - ratio; + return this; + } + greyscale() { + const rgb = this._rgb; + const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11); + rgb.r = rgb.g = rgb.b = val; + return this; + } + opaquer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 + ratio; + return this; + } + negate() { + const v = this._rgb; + v.r = 255 - v.r; + v.g = 255 - v.g; + v.b = 255 - v.b; + return this; + } + lighten(ratio) { + modHSL(this._rgb, 2, ratio); + return this; + } + darken(ratio) { + modHSL(this._rgb, 2, -ratio); + return this; + } + saturate(ratio) { + modHSL(this._rgb, 1, ratio); + return this; + } + desaturate(ratio) { + modHSL(this._rgb, 1, -ratio); + return this; + } + rotate(deg) { + rotate(this._rgb, deg); + return this; + } } function index_esm(input) { - return new Color(input); + return new Color(input); } -const isPatternOrGradient = (value) => value instanceof CanvasGradient || value instanceof CanvasPattern; +function isPatternOrGradient(value) { + if (value && typeof value === 'object') { + const type = value.toString(); + return type === '[object CanvasPattern]' || type === '[object CanvasGradient]'; + } + return false; +} function color(value) { return isPatternOrGradient(value) ? value : index_esm(value); } @@ -1050,7 +1234,8 @@ class Defaults { this.indexAxis = 'x'; this.interaction = { mode: 'nearest', - intersect: true + intersect: true, + includeInvisible: false }; this.maintainAspectRatio = true; this.onHover = null; @@ -1184,7 +1369,10 @@ function clearCanvas(canvas, ctx) { ctx.restore(); } function drawPoint(ctx, options, x, y) { - let type, xOffset, yOffset, size, cornerRadius; + drawPointLegend(ctx, options, x, y, null); +} +function drawPointLegend(ctx, options, x, y, w) { + let type, xOffset, yOffset, size, cornerRadius, width; const style = options.pointStyle; const rotation = options.rotation; const radius = options.radius; @@ -1206,7 +1394,11 @@ function drawPoint(ctx, options, x, y) { ctx.beginPath(); switch (style) { default: - ctx.arc(x, y, radius, 0, TAU); + if (w) { + ctx.ellipse(x, y, w / 2, radius, 0, 0, TAU); + } else { + ctx.arc(x, y, radius, 0, TAU); + } ctx.closePath(); break; case 'triangle': @@ -1231,7 +1423,8 @@ function drawPoint(ctx, options, x, y) { case 'rect': if (!rotation) { size = Math.SQRT1_2 * radius; - ctx.rect(x - size, y - size, 2 * size, 2 * size); + width = w ? w / 2 : size; + ctx.rect(x - width, y - size, 2 * width, 2 * size); break; } rad += QUARTER_PI; @@ -1270,7 +1463,7 @@ function drawPoint(ctx, options, x, y) { ctx.lineTo(x - yOffset, y + xOffset); break; case 'line': - xOffset = Math.cos(rad) * radius; + xOffset = w ? w / 2 : Math.cos(rad) * radius; yOffset = Math.sin(rad) * radius; ctx.moveTo(x - xOffset, y - yOffset); ctx.lineTo(x + xOffset, y + yOffset); @@ -1499,99 +1692,6 @@ function createContext(parentContext, context) { return Object.assign(Object.create(parentContext), context); } -function _lookup(table, value, cmp) { - cmp = cmp || ((index) => table[index] < value); - let hi = table.length - 1; - let lo = 0; - let mid; - while (hi - lo > 1) { - mid = (lo + hi) >> 1; - if (cmp(mid)) { - lo = mid; - } else { - hi = mid; - } - } - return {lo, hi}; -} -const _lookupByKey = (table, key, value) => - _lookup(table, value, index => table[index][key] < value); -const _rlookupByKey = (table, key, value) => - _lookup(table, value, index => table[index][key] >= value); -function _filterBetween(values, min, max) { - let start = 0; - let end = values.length; - while (start < end && values[start] < min) { - start++; - } - while (end > start && values[end - 1] > max) { - end--; - } - return start > 0 || end < values.length - ? values.slice(start, end) - : values; -} -const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift']; -function listenArrayEvents(array, listener) { - if (array._chartjs) { - array._chartjs.listeners.push(listener); - return; - } - Object.defineProperty(array, '_chartjs', { - configurable: true, - enumerable: false, - value: { - listeners: [listener] - } - }); - arrayEvents.forEach((key) => { - const method = '_onData' + _capitalize(key); - const base = array[key]; - Object.defineProperty(array, key, { - configurable: true, - enumerable: false, - value(...args) { - const res = base.apply(this, args); - array._chartjs.listeners.forEach((object) => { - if (typeof object[method] === 'function') { - object[method](...args); - } - }); - return res; - } - }); - }); -} -function unlistenArrayEvents(array, listener) { - const stub = array._chartjs; - if (!stub) { - return; - } - const listeners = stub.listeners; - const index = listeners.indexOf(listener); - if (index !== -1) { - listeners.splice(index, 1); - } - if (listeners.length > 0) { - return; - } - arrayEvents.forEach((key) => { - delete array[key]; - }); - delete array._chartjs; -} -function _arrayUnique(items) { - const set = new Set(); - let i, ilen; - for (i = 0, ilen = items.length; i < ilen; ++i) { - set.add(items[i]); - } - if (set.size === ilen) { - return items; - } - return Array.from(set); -} - function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fallback, getTarget = () => scopes[0]) { if (!defined(fallback)) { fallback = _resolve('_fallback', scopes); @@ -1835,6 +1935,20 @@ function resolveKeysFromAllScopes(scopes) { } return Array.from(set); } +function _parseObjectDataRadialScale(meta, data, start, count) { + const {iScale} = meta; + const {key = 'r'} = this._parsing; + const parsed = new Array(count); + let i, ilen, index, item; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + item = data[index]; + parsed[i] = { + r: iScale.parse(resolveObjectKey(item, key), index) + }; + } + return parsed; +} const EPSILON = Number.EPSILON || 1e-14; const getPoint = (points, i) => i < points.length && !points[i].skip && points[i]; @@ -2031,8 +2145,7 @@ function getPositionedStyle(styles, style, suffix) { return result; } const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot); -function getCanvasPosition(evt, canvas) { - const e = evt.native || evt; +function getCanvasPosition(e, canvas) { const touches = e.touches; const source = touches && touches.length ? touches[0] : e; const {offsetX, offsetY} = source; @@ -2050,6 +2163,9 @@ function getCanvasPosition(evt, canvas) { return {x, y, box}; } function getRelativePosition(evt, chart) { + if ('native' in evt) { + return evt; + } const {canvas, currentDevicePixelRatio} = chart; const style = getComputedStyle(canvas); const borderBox = style.boxSizing === 'border-box'; @@ -2500,4 +2616,4 @@ function styleChanged(style, prevStyle) { return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle); } -export { _toLeftRightCenter as $, _rlookupByKey as A, getAngleFromPoint as B, toPadding as C, each as D, getMaximumSize as E, _getParentNode as F, readUsedSize as G, HALF_PI as H, throttled as I, supportsEventListenerOptions as J, _isDomSupported as K, log10 as L, _factorize as M, finiteOrDefault as N, callback as O, PI as P, _addGrace as Q, toDegrees as R, _measureText as S, TAU as T, _int16Range as U, _alignPixel as V, clipArea as W, renderText as X, unclipArea as Y, toFont as Z, _arrayUnique as _, resolve as a, _angleDiff as a$, _alignStartEnd as a0, overrides as a1, merge as a2, _capitalize as a3, descriptors as a4, isFunction as a5, _attachContext as a6, _createResolver as a7, _descriptors as a8, mergeIf as a9, restoreTextDirection as aA, noop as aB, distanceBetweenPoints as aC, _setMinAndMaxByKey as aD, niceNum as aE, almostWhole as aF, almostEquals as aG, _decimalPlaces as aH, _longestText as aI, _filterBetween as aJ, _lookup as aK, getHoverColor as aL, clone$1 as aM, _merger as aN, _mergerIf as aO, _deprecated as aP, toFontString as aQ, splineCurve as aR, splineCurveMonotone as aS, getStyle as aT, fontString as aU, toLineHeight as aV, PITAU as aW, INFINITY as aX, RAD_PER_DEG as aY, QUARTER_PI as aZ, TWO_THIRDS_PI as a_, uid as aa, debounce as ab, retinaScale as ac, clearCanvas as ad, setsEqual as ae, _elementsEqual as af, _isClickEvent as ag, _isBetween as ah, _readValueToProps as ai, _updateBezierControlPoints as aj, _computeSegments as ak, _boundSegments as al, _steppedInterpolation as am, _bezierInterpolation as an, _pointInLine as ao, _steppedLineTo as ap, _bezierCurveTo as aq, drawPoint as ar, addRoundedRectPath as as, toTRBL as at, toTRBLCorners as au, _boundSegment as av, _normalizeAngle as aw, getRtlAdapter as ax, overrideTextDirection as ay, _textX as az, isArray as b, color as c, defaults as d, effects as e, resolveObjectKey as f, isNumberFinite as g, createContext as h, isObject as i, defined as j, isNullOrUndef as k, listenArrayEvents as l, toPercentage as m, toDimension as n, formatNumber as o, _angleBetween as p, isNumber as q, requestAnimFrame as r, sign as s, toRadians as t, unlistenArrayEvents as u, valueOrDefault as v, _limitValue as w, _lookupByKey as x, getRelativePosition as y, _isPointInArea as z }; +export { _isPointInArea as $, _factorize as A, finiteOrDefault as B, callback as C, _addGrace as D, _limitValue as E, toDegrees as F, _measureText as G, HALF_PI as H, _int16Range as I, _alignPixel as J, toPadding as K, clipArea as L, renderText as M, unclipArea as N, toFont as O, PI as P, each as Q, _toLeftRightCenter as R, _alignStartEnd as S, TAU as T, overrides as U, merge as V, _capitalize as W, getRelativePosition as X, _rlookupByKey as Y, _lookupByKey as Z, _arrayUnique as _, resolve as a, toLineHeight as a$, getAngleFromPoint as a0, getMaximumSize as a1, _getParentNode as a2, readUsedSize as a3, throttled as a4, supportsEventListenerOptions as a5, _isDomSupported as a6, descriptors as a7, isFunction as a8, _attachContext as a9, getRtlAdapter as aA, overrideTextDirection as aB, _textX as aC, restoreTextDirection as aD, drawPointLegend as aE, noop as aF, distanceBetweenPoints as aG, _setMinAndMaxByKey as aH, niceNum as aI, almostWhole as aJ, almostEquals as aK, _decimalPlaces as aL, _longestText as aM, _filterBetween as aN, _lookup as aO, isPatternOrGradient as aP, getHoverColor as aQ, clone$1 as aR, _merger as aS, _mergerIf as aT, _deprecated as aU, _splitKey as aV, toFontString as aW, splineCurve as aX, splineCurveMonotone as aY, getStyle as aZ, fontString as a_, _createResolver as aa, _descriptors as ab, mergeIf as ac, uid as ad, debounce as ae, retinaScale as af, clearCanvas as ag, setsEqual as ah, _elementsEqual as ai, _isClickEvent as aj, _isBetween as ak, _readValueToProps as al, _updateBezierControlPoints as am, _computeSegments as an, _boundSegments as ao, _steppedInterpolation as ap, _bezierInterpolation as aq, _pointInLine as ar, _steppedLineTo as as, _bezierCurveTo as at, drawPoint as au, addRoundedRectPath as av, toTRBL as aw, toTRBLCorners as ax, _boundSegment as ay, _normalizeAngle as az, isArray as b, PITAU as b0, INFINITY as b1, RAD_PER_DEG as b2, QUARTER_PI as b3, TWO_THIRDS_PI as b4, _angleDiff as b5, color as c, defaults as d, effects as e, resolveObjectKey as f, isNumberFinite as g, createContext as h, isObject as i, defined as j, isNullOrUndef as k, listenArrayEvents as l, toPercentage as m, toDimension as n, formatNumber as o, _angleBetween as p, _getStartAndCountOfVisiblePoints as q, requestAnimFrame as r, sign as s, toRadians as t, unlistenArrayEvents as u, valueOrDefault as v, _scaleRangesChanged as w, isNumber as x, _parseObjectDataRadialScale as y, log10 as z }; diff --git a/static/js/chunks/helpers.segment.mjs b/static/js/chunks/helpers.segment.mjs new file mode 100644 index 00000000..5cffd79e --- /dev/null +++ b/static/js/chunks/helpers.segment.mjs @@ -0,0 +1,2619 @@ +/*! + * Chart.js v3.9.1 + * https://www.chartjs.org + * (c) 2022 Chart.js Contributors + * Released under the MIT License + */ +function noop() {} +const uid = (function() { + let id = 0; + return function() { + return id++; + }; +}()); +function isNullOrUndef(value) { + return value === null || typeof value === 'undefined'; +} +function isArray(value) { + if (Array.isArray && Array.isArray(value)) { + return true; + } + const type = Object.prototype.toString.call(value); + if (type.slice(0, 7) === '[object' && type.slice(-6) === 'Array]') { + return true; + } + return false; +} +function isObject(value) { + return value !== null && Object.prototype.toString.call(value) === '[object Object]'; +} +const isNumberFinite = (value) => (typeof value === 'number' || value instanceof Number) && isFinite(+value); +function finiteOrDefault(value, defaultValue) { + return isNumberFinite(value) ? value : defaultValue; +} +function valueOrDefault(value, defaultValue) { + return typeof value === 'undefined' ? defaultValue : value; +} +const toPercentage = (value, dimension) => + typeof value === 'string' && value.endsWith('%') ? + parseFloat(value) / 100 + : value / dimension; +const toDimension = (value, dimension) => + typeof value === 'string' && value.endsWith('%') ? + parseFloat(value) / 100 * dimension + : +value; +function callback(fn, args, thisArg) { + if (fn && typeof fn.call === 'function') { + return fn.apply(thisArg, args); + } +} +function each(loopable, fn, thisArg, reverse) { + let i, len, keys; + if (isArray(loopable)) { + len = loopable.length; + if (reverse) { + for (i = len - 1; i >= 0; i--) { + fn.call(thisArg, loopable[i], i); + } + } else { + for (i = 0; i < len; i++) { + fn.call(thisArg, loopable[i], i); + } + } + } else if (isObject(loopable)) { + keys = Object.keys(loopable); + len = keys.length; + for (i = 0; i < len; i++) { + fn.call(thisArg, loopable[keys[i]], keys[i]); + } + } +} +function _elementsEqual(a0, a1) { + let i, ilen, v0, v1; + if (!a0 || !a1 || a0.length !== a1.length) { + return false; + } + for (i = 0, ilen = a0.length; i < ilen; ++i) { + v0 = a0[i]; + v1 = a1[i]; + if (v0.datasetIndex !== v1.datasetIndex || v0.index !== v1.index) { + return false; + } + } + return true; +} +function clone$1(source) { + if (isArray(source)) { + return source.map(clone$1); + } + if (isObject(source)) { + const target = Object.create(null); + const keys = Object.keys(source); + const klen = keys.length; + let k = 0; + for (; k < klen; ++k) { + target[keys[k]] = clone$1(source[keys[k]]); + } + return target; + } + return source; +} +function isValidKey(key) { + return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1; +} +function _merger(key, target, source, options) { + if (!isValidKey(key)) { + return; + } + const tval = target[key]; + const sval = source[key]; + if (isObject(tval) && isObject(sval)) { + merge(tval, sval, options); + } else { + target[key] = clone$1(sval); + } +} +function merge(target, source, options) { + const sources = isArray(source) ? source : [source]; + const ilen = sources.length; + if (!isObject(target)) { + return target; + } + options = options || {}; + const merger = options.merger || _merger; + for (let i = 0; i < ilen; ++i) { + source = sources[i]; + if (!isObject(source)) { + continue; + } + const keys = Object.keys(source); + for (let k = 0, klen = keys.length; k < klen; ++k) { + merger(keys[k], target, source, options); + } + } + return target; +} +function mergeIf(target, source) { + return merge(target, source, {merger: _mergerIf}); +} +function _mergerIf(key, target, source) { + if (!isValidKey(key)) { + return; + } + const tval = target[key]; + const sval = source[key]; + if (isObject(tval) && isObject(sval)) { + mergeIf(tval, sval); + } else if (!Object.prototype.hasOwnProperty.call(target, key)) { + target[key] = clone$1(sval); + } +} +function _deprecated(scope, value, previous, current) { + if (value !== undefined) { + console.warn(scope + ': "' + previous + + '" is deprecated. Please use "' + current + '" instead'); + } +} +const keyResolvers = { + '': v => v, + x: o => o.x, + y: o => o.y +}; +function resolveObjectKey(obj, key) { + const resolver = keyResolvers[key] || (keyResolvers[key] = _getKeyResolver(key)); + return resolver(obj); +} +function _getKeyResolver(key) { + const keys = _splitKey(key); + return obj => { + for (const k of keys) { + if (k === '') { + break; + } + obj = obj && obj[k]; + } + return obj; + }; +} +function _splitKey(key) { + const parts = key.split('.'); + const keys = []; + let tmp = ''; + for (const part of parts) { + tmp += part; + if (tmp.endsWith('\\')) { + tmp = tmp.slice(0, -1) + '.'; + } else { + keys.push(tmp); + tmp = ''; + } + } + return keys; +} +function _capitalize(str) { + return str.charAt(0).toUpperCase() + str.slice(1); +} +const defined = (value) => typeof value !== 'undefined'; +const isFunction = (value) => typeof value === 'function'; +const setsEqual = (a, b) => { + if (a.size !== b.size) { + return false; + } + for (const item of a) { + if (!b.has(item)) { + return false; + } + } + return true; +}; +function _isClickEvent(e) { + return e.type === 'mouseup' || e.type === 'click' || e.type === 'contextmenu'; +} + +const PI = Math.PI; +const TAU = 2 * PI; +const PITAU = TAU + PI; +const INFINITY = Number.POSITIVE_INFINITY; +const RAD_PER_DEG = PI / 180; +const HALF_PI = PI / 2; +const QUARTER_PI = PI / 4; +const TWO_THIRDS_PI = PI * 2 / 3; +const log10 = Math.log10; +const sign = Math.sign; +function niceNum(range) { + const roundedRange = Math.round(range); + range = almostEquals(range, roundedRange, range / 1000) ? roundedRange : range; + const niceRange = Math.pow(10, Math.floor(log10(range))); + const fraction = range / niceRange; + const niceFraction = fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 5 ? 5 : 10; + return niceFraction * niceRange; +} +function _factorize(value) { + const result = []; + const sqrt = Math.sqrt(value); + let i; + for (i = 1; i < sqrt; i++) { + if (value % i === 0) { + result.push(i); + result.push(value / i); + } + } + if (sqrt === (sqrt | 0)) { + result.push(sqrt); + } + result.sort((a, b) => a - b).pop(); + return result; +} +function isNumber(n) { + return !isNaN(parseFloat(n)) && isFinite(n); +} +function almostEquals(x, y, epsilon) { + return Math.abs(x - y) < epsilon; +} +function almostWhole(x, epsilon) { + const rounded = Math.round(x); + return ((rounded - epsilon) <= x) && ((rounded + epsilon) >= x); +} +function _setMinAndMaxByKey(array, target, property) { + let i, ilen, value; + for (i = 0, ilen = array.length; i < ilen; i++) { + value = array[i][property]; + if (!isNaN(value)) { + target.min = Math.min(target.min, value); + target.max = Math.max(target.max, value); + } + } +} +function toRadians(degrees) { + return degrees * (PI / 180); +} +function toDegrees(radians) { + return radians * (180 / PI); +} +function _decimalPlaces(x) { + if (!isNumberFinite(x)) { + return; + } + let e = 1; + let p = 0; + while (Math.round(x * e) / e !== x) { + e *= 10; + p++; + } + return p; +} +function getAngleFromPoint(centrePoint, anglePoint) { + const distanceFromXCenter = anglePoint.x - centrePoint.x; + const distanceFromYCenter = anglePoint.y - centrePoint.y; + const radialDistanceFromCenter = Math.sqrt(distanceFromXCenter * distanceFromXCenter + distanceFromYCenter * distanceFromYCenter); + let angle = Math.atan2(distanceFromYCenter, distanceFromXCenter); + if (angle < (-0.5 * PI)) { + angle += TAU; + } + return { + angle, + distance: radialDistanceFromCenter + }; +} +function distanceBetweenPoints(pt1, pt2) { + return Math.sqrt(Math.pow(pt2.x - pt1.x, 2) + Math.pow(pt2.y - pt1.y, 2)); +} +function _angleDiff(a, b) { + return (a - b + PITAU) % TAU - PI; +} +function _normalizeAngle(a) { + return (a % TAU + TAU) % TAU; +} +function _angleBetween(angle, start, end, sameAngleIsFullCircle) { + const a = _normalizeAngle(angle); + const s = _normalizeAngle(start); + const e = _normalizeAngle(end); + const angleToStart = _normalizeAngle(s - a); + const angleToEnd = _normalizeAngle(e - a); + const startToAngle = _normalizeAngle(a - s); + const endToAngle = _normalizeAngle(a - e); + return a === s || a === e || (sameAngleIsFullCircle && s === e) + || (angleToStart > angleToEnd && startToAngle < endToAngle); +} +function _limitValue(value, min, max) { + return Math.max(min, Math.min(max, value)); +} +function _int16Range(value) { + return _limitValue(value, -32768, 32767); +} +function _isBetween(value, start, end, epsilon = 1e-6) { + return value >= Math.min(start, end) - epsilon && value <= Math.max(start, end) + epsilon; +} + +function _lookup(table, value, cmp) { + cmp = cmp || ((index) => table[index] < value); + let hi = table.length - 1; + let lo = 0; + let mid; + while (hi - lo > 1) { + mid = (lo + hi) >> 1; + if (cmp(mid)) { + lo = mid; + } else { + hi = mid; + } + } + return {lo, hi}; +} +const _lookupByKey = (table, key, value, last) => + _lookup(table, value, last + ? index => table[index][key] <= value + : index => table[index][key] < value); +const _rlookupByKey = (table, key, value) => + _lookup(table, value, index => table[index][key] >= value); +function _filterBetween(values, min, max) { + let start = 0; + let end = values.length; + while (start < end && values[start] < min) { + start++; + } + while (end > start && values[end - 1] > max) { + end--; + } + return start > 0 || end < values.length + ? values.slice(start, end) + : values; +} +const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift']; +function listenArrayEvents(array, listener) { + if (array._chartjs) { + array._chartjs.listeners.push(listener); + return; + } + Object.defineProperty(array, '_chartjs', { + configurable: true, + enumerable: false, + value: { + listeners: [listener] + } + }); + arrayEvents.forEach((key) => { + const method = '_onData' + _capitalize(key); + const base = array[key]; + Object.defineProperty(array, key, { + configurable: true, + enumerable: false, + value(...args) { + const res = base.apply(this, args); + array._chartjs.listeners.forEach((object) => { + if (typeof object[method] === 'function') { + object[method](...args); + } + }); + return res; + } + }); + }); +} +function unlistenArrayEvents(array, listener) { + const stub = array._chartjs; + if (!stub) { + return; + } + const listeners = stub.listeners; + const index = listeners.indexOf(listener); + if (index !== -1) { + listeners.splice(index, 1); + } + if (listeners.length > 0) { + return; + } + arrayEvents.forEach((key) => { + delete array[key]; + }); + delete array._chartjs; +} +function _arrayUnique(items) { + const set = new Set(); + let i, ilen; + for (i = 0, ilen = items.length; i < ilen; ++i) { + set.add(items[i]); + } + if (set.size === ilen) { + return items; + } + return Array.from(set); +} + +function fontString(pixelSize, fontStyle, fontFamily) { + return fontStyle + ' ' + pixelSize + 'px ' + fontFamily; +} +const requestAnimFrame = (function() { + if (typeof window === 'undefined') { + return function(callback) { + return callback(); + }; + } + return window.requestAnimationFrame; +}()); +function throttled(fn, thisArg, updateFn) { + const updateArgs = updateFn || ((args) => Array.prototype.slice.call(args)); + let ticking = false; + let args = []; + return function(...rest) { + args = updateArgs(rest); + if (!ticking) { + ticking = true; + requestAnimFrame.call(window, () => { + ticking = false; + fn.apply(thisArg, args); + }); + } + }; +} +function debounce(fn, delay) { + let timeout; + return function(...args) { + if (delay) { + clearTimeout(timeout); + timeout = setTimeout(fn, delay, args); + } else { + fn.apply(this, args); + } + return delay; + }; +} +const _toLeftRightCenter = (align) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center'; +const _alignStartEnd = (align, start, end) => align === 'start' ? start : align === 'end' ? end : (start + end) / 2; +const _textX = (align, left, right, rtl) => { + const check = rtl ? 'left' : 'right'; + return align === check ? right : align === 'center' ? (left + right) / 2 : left; +}; +function _getStartAndCountOfVisiblePoints(meta, points, animationsDisabled) { + const pointCount = points.length; + let start = 0; + let count = pointCount; + if (meta._sorted) { + const {iScale, _parsed} = meta; + const axis = iScale.axis; + const {min, max, minDefined, maxDefined} = iScale.getUserBounds(); + if (minDefined) { + start = _limitValue(Math.min( + _lookupByKey(_parsed, iScale.axis, min).lo, + animationsDisabled ? pointCount : _lookupByKey(points, axis, iScale.getPixelForValue(min)).lo), + 0, pointCount - 1); + } + if (maxDefined) { + count = _limitValue(Math.max( + _lookupByKey(_parsed, iScale.axis, max, true).hi + 1, + animationsDisabled ? 0 : _lookupByKey(points, axis, iScale.getPixelForValue(max), true).hi + 1), + start, pointCount) - start; + } else { + count = pointCount - start; + } + } + return {start, count}; +} +function _scaleRangesChanged(meta) { + const {xScale, yScale, _scaleRanges} = meta; + const newRanges = { + xmin: xScale.min, + xmax: xScale.max, + ymin: yScale.min, + ymax: yScale.max + }; + if (!_scaleRanges) { + meta._scaleRanges = newRanges; + return true; + } + const changed = _scaleRanges.xmin !== xScale.min + || _scaleRanges.xmax !== xScale.max + || _scaleRanges.ymin !== yScale.min + || _scaleRanges.ymax !== yScale.max; + Object.assign(_scaleRanges, newRanges); + return changed; +} + +const atEdge = (t) => t === 0 || t === 1; +const elasticIn = (t, s, p) => -(Math.pow(2, 10 * (t -= 1)) * Math.sin((t - s) * TAU / p)); +const elasticOut = (t, s, p) => Math.pow(2, -10 * t) * Math.sin((t - s) * TAU / p) + 1; +const effects = { + linear: t => t, + easeInQuad: t => t * t, + easeOutQuad: t => -t * (t - 2), + easeInOutQuad: t => ((t /= 0.5) < 1) + ? 0.5 * t * t + : -0.5 * ((--t) * (t - 2) - 1), + easeInCubic: t => t * t * t, + easeOutCubic: t => (t -= 1) * t * t + 1, + easeInOutCubic: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t + : 0.5 * ((t -= 2) * t * t + 2), + easeInQuart: t => t * t * t * t, + easeOutQuart: t => -((t -= 1) * t * t * t - 1), + easeInOutQuart: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t * t + : -0.5 * ((t -= 2) * t * t * t - 2), + easeInQuint: t => t * t * t * t * t, + easeOutQuint: t => (t -= 1) * t * t * t * t + 1, + easeInOutQuint: t => ((t /= 0.5) < 1) + ? 0.5 * t * t * t * t * t + : 0.5 * ((t -= 2) * t * t * t * t + 2), + easeInSine: t => -Math.cos(t * HALF_PI) + 1, + easeOutSine: t => Math.sin(t * HALF_PI), + easeInOutSine: t => -0.5 * (Math.cos(PI * t) - 1), + easeInExpo: t => (t === 0) ? 0 : Math.pow(2, 10 * (t - 1)), + easeOutExpo: t => (t === 1) ? 1 : -Math.pow(2, -10 * t) + 1, + easeInOutExpo: t => atEdge(t) ? t : t < 0.5 + ? 0.5 * Math.pow(2, 10 * (t * 2 - 1)) + : 0.5 * (-Math.pow(2, -10 * (t * 2 - 1)) + 2), + easeInCirc: t => (t >= 1) ? t : -(Math.sqrt(1 - t * t) - 1), + easeOutCirc: t => Math.sqrt(1 - (t -= 1) * t), + easeInOutCirc: t => ((t /= 0.5) < 1) + ? -0.5 * (Math.sqrt(1 - t * t) - 1) + : 0.5 * (Math.sqrt(1 - (t -= 2) * t) + 1), + easeInElastic: t => atEdge(t) ? t : elasticIn(t, 0.075, 0.3), + easeOutElastic: t => atEdge(t) ? t : elasticOut(t, 0.075, 0.3), + easeInOutElastic(t) { + const s = 0.1125; + const p = 0.45; + return atEdge(t) ? t : + t < 0.5 + ? 0.5 * elasticIn(t * 2, s, p) + : 0.5 + 0.5 * elasticOut(t * 2 - 1, s, p); + }, + easeInBack(t) { + const s = 1.70158; + return t * t * ((s + 1) * t - s); + }, + easeOutBack(t) { + const s = 1.70158; + return (t -= 1) * t * ((s + 1) * t + s) + 1; + }, + easeInOutBack(t) { + let s = 1.70158; + if ((t /= 0.5) < 1) { + return 0.5 * (t * t * (((s *= (1.525)) + 1) * t - s)); + } + return 0.5 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2); + }, + easeInBounce: t => 1 - effects.easeOutBounce(1 - t), + easeOutBounce(t) { + const m = 7.5625; + const d = 2.75; + if (t < (1 / d)) { + return m * t * t; + } + if (t < (2 / d)) { + return m * (t -= (1.5 / d)) * t + 0.75; + } + if (t < (2.5 / d)) { + return m * (t -= (2.25 / d)) * t + 0.9375; + } + return m * (t -= (2.625 / d)) * t + 0.984375; + }, + easeInOutBounce: t => (t < 0.5) + ? effects.easeInBounce(t * 2) * 0.5 + : effects.easeOutBounce(t * 2 - 1) * 0.5 + 0.5, +}; + +/*! + * @kurkle/color v0.2.1 + * https://github.com/kurkle/color#readme + * (c) 2022 Jukka Kurkela + * Released under the MIT License + */ +function round(v) { + return v + 0.5 | 0; +} +const lim = (v, l, h) => Math.max(Math.min(v, h), l); +function p2b(v) { + return lim(round(v * 2.55), 0, 255); +} +function n2b(v) { + return lim(round(v * 255), 0, 255); +} +function b2n(v) { + return lim(round(v / 2.55) / 100, 0, 1); +} +function n2p(v) { + return lim(round(v * 100), 0, 100); +} +const map$1 = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, A: 10, B: 11, C: 12, D: 13, E: 14, F: 15, a: 10, b: 11, c: 12, d: 13, e: 14, f: 15}; +const hex = [...'0123456789ABCDEF']; +const h1 = b => hex[b & 0xF]; +const h2 = b => hex[(b & 0xF0) >> 4] + hex[b & 0xF]; +const eq = b => ((b & 0xF0) >> 4) === (b & 0xF); +const isShort = v => eq(v.r) && eq(v.g) && eq(v.b) && eq(v.a); +function hexParse(str) { + var len = str.length; + var ret; + if (str[0] === '#') { + if (len === 4 || len === 5) { + ret = { + r: 255 & map$1[str[1]] * 17, + g: 255 & map$1[str[2]] * 17, + b: 255 & map$1[str[3]] * 17, + a: len === 5 ? map$1[str[4]] * 17 : 255 + }; + } else if (len === 7 || len === 9) { + ret = { + r: map$1[str[1]] << 4 | map$1[str[2]], + g: map$1[str[3]] << 4 | map$1[str[4]], + b: map$1[str[5]] << 4 | map$1[str[6]], + a: len === 9 ? (map$1[str[7]] << 4 | map$1[str[8]]) : 255 + }; + } + } + return ret; +} +const alpha = (a, f) => a < 255 ? f(a) : ''; +function hexString(v) { + var f = isShort(v) ? h1 : h2; + return v + ? '#' + f(v.r) + f(v.g) + f(v.b) + alpha(v.a, f) + : undefined; +} +const HUE_RE = /^(hsla?|hwb|hsv)\(\s*([-+.e\d]+)(?:deg)?[\s,]+([-+.e\d]+)%[\s,]+([-+.e\d]+)%(?:[\s,]+([-+.e\d]+)(%)?)?\s*\)$/; +function hsl2rgbn(h, s, l) { + const a = s * Math.min(l, 1 - l); + const f = (n, k = (n + h / 30) % 12) => l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); + return [f(0), f(8), f(4)]; +} +function hsv2rgbn(h, s, v) { + const f = (n, k = (n + h / 60) % 6) => v - v * s * Math.max(Math.min(k, 4 - k, 1), 0); + return [f(5), f(3), f(1)]; +} +function hwb2rgbn(h, w, b) { + const rgb = hsl2rgbn(h, 1, 0.5); + let i; + if (w + b > 1) { + i = 1 / (w + b); + w *= i; + b *= i; + } + for (i = 0; i < 3; i++) { + rgb[i] *= 1 - w - b; + rgb[i] += w; + } + return rgb; +} +function hueValue(r, g, b, d, max) { + if (r === max) { + return ((g - b) / d) + (g < b ? 6 : 0); + } + if (g === max) { + return (b - r) / d + 2; + } + return (r - g) / d + 4; +} +function rgb2hsl(v) { + const range = 255; + const r = v.r / range; + const g = v.g / range; + const b = v.b / range; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + const l = (max + min) / 2; + let h, s, d; + if (max !== min) { + d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + h = hueValue(r, g, b, d, max); + h = h * 60 + 0.5; + } + return [h | 0, s || 0, l]; +} +function calln(f, a, b, c) { + return ( + Array.isArray(a) + ? f(a[0], a[1], a[2]) + : f(a, b, c) + ).map(n2b); +} +function hsl2rgb(h, s, l) { + return calln(hsl2rgbn, h, s, l); +} +function hwb2rgb(h, w, b) { + return calln(hwb2rgbn, h, w, b); +} +function hsv2rgb(h, s, v) { + return calln(hsv2rgbn, h, s, v); +} +function hue(h) { + return (h % 360 + 360) % 360; +} +function hueParse(str) { + const m = HUE_RE.exec(str); + let a = 255; + let v; + if (!m) { + return; + } + if (m[5] !== v) { + a = m[6] ? p2b(+m[5]) : n2b(+m[5]); + } + const h = hue(+m[2]); + const p1 = +m[3] / 100; + const p2 = +m[4] / 100; + if (m[1] === 'hwb') { + v = hwb2rgb(h, p1, p2); + } else if (m[1] === 'hsv') { + v = hsv2rgb(h, p1, p2); + } else { + v = hsl2rgb(h, p1, p2); + } + return { + r: v[0], + g: v[1], + b: v[2], + a: a + }; +} +function rotate(v, deg) { + var h = rgb2hsl(v); + h[0] = hue(h[0] + deg); + h = hsl2rgb(h); + v.r = h[0]; + v.g = h[1]; + v.b = h[2]; +} +function hslString(v) { + if (!v) { + return; + } + const a = rgb2hsl(v); + const h = a[0]; + const s = n2p(a[1]); + const l = n2p(a[2]); + return v.a < 255 + ? `hsla(${h}, ${s}%, ${l}%, ${b2n(v.a)})` + : `hsl(${h}, ${s}%, ${l}%)`; +} +const map = { + x: 'dark', + Z: 'light', + Y: 're', + X: 'blu', + W: 'gr', + V: 'medium', + U: 'slate', + A: 'ee', + T: 'ol', + S: 'or', + B: 'ra', + C: 'lateg', + D: 'ights', + R: 'in', + Q: 'turquois', + E: 'hi', + P: 'ro', + O: 'al', + N: 'le', + M: 'de', + L: 'yello', + F: 'en', + K: 'ch', + G: 'arks', + H: 'ea', + I: 'ightg', + J: 'wh' +}; +const names$1 = { + OiceXe: 'f0f8ff', + antiquewEte: 'faebd7', + aqua: 'ffff', + aquamarRe: '7fffd4', + azuY: 'f0ffff', + beige: 'f5f5dc', + bisque: 'ffe4c4', + black: '0', + blanKedOmond: 'ffebcd', + Xe: 'ff', + XeviTet: '8a2be2', + bPwn: 'a52a2a', + burlywood: 'deb887', + caMtXe: '5f9ea0', + KartYuse: '7fff00', + KocTate: 'd2691e', + cSO: 'ff7f50', + cSnflowerXe: '6495ed', + cSnsilk: 'fff8dc', + crimson: 'dc143c', + cyan: 'ffff', + xXe: '8b', + xcyan: '8b8b', + xgTMnPd: 'b8860b', + xWay: 'a9a9a9', + xgYF: '6400', + xgYy: 'a9a9a9', + xkhaki: 'bdb76b', + xmagFta: '8b008b', + xTivegYF: '556b2f', + xSange: 'ff8c00', + xScEd: '9932cc', + xYd: '8b0000', + xsOmon: 'e9967a', + xsHgYF: '8fbc8f', + xUXe: '483d8b', + xUWay: '2f4f4f', + xUgYy: '2f4f4f', + xQe: 'ced1', + xviTet: '9400d3', + dAppRk: 'ff1493', + dApskyXe: 'bfff', + dimWay: '696969', + dimgYy: '696969', + dodgerXe: '1e90ff', + fiYbrick: 'b22222', + flSOwEte: 'fffaf0', + foYstWAn: '228b22', + fuKsia: 'ff00ff', + gaRsbSo: 'dcdcdc', + ghostwEte: 'f8f8ff', + gTd: 'ffd700', + gTMnPd: 'daa520', + Way: '808080', + gYF: '8000', + gYFLw: 'adff2f', + gYy: '808080', + honeyMw: 'f0fff0', + hotpRk: 'ff69b4', + RdianYd: 'cd5c5c', + Rdigo: '4b0082', + ivSy: 'fffff0', + khaki: 'f0e68c', + lavFMr: 'e6e6fa', + lavFMrXsh: 'fff0f5', + lawngYF: '7cfc00', + NmoncEffon: 'fffacd', + ZXe: 'add8e6', + ZcSO: 'f08080', + Zcyan: 'e0ffff', + ZgTMnPdLw: 'fafad2', + ZWay: 'd3d3d3', + ZgYF: '90ee90', + ZgYy: 'd3d3d3', + ZpRk: 'ffb6c1', + ZsOmon: 'ffa07a', + ZsHgYF: '20b2aa', + ZskyXe: '87cefa', + ZUWay: '778899', + ZUgYy: '778899', + ZstAlXe: 'b0c4de', + ZLw: 'ffffe0', + lime: 'ff00', + limegYF: '32cd32', + lRF: 'faf0e6', + magFta: 'ff00ff', + maPon: '800000', + VaquamarRe: '66cdaa', + VXe: 'cd', + VScEd: 'ba55d3', + VpurpN: '9370db', + VsHgYF: '3cb371', + VUXe: '7b68ee', + VsprRggYF: 'fa9a', + VQe: '48d1cc', + VviTetYd: 'c71585', + midnightXe: '191970', + mRtcYam: 'f5fffa', + mistyPse: 'ffe4e1', + moccasR: 'ffe4b5', + navajowEte: 'ffdead', + navy: '80', + Tdlace: 'fdf5e6', + Tive: '808000', + TivedBb: '6b8e23', + Sange: 'ffa500', + SangeYd: 'ff4500', + ScEd: 'da70d6', + pOegTMnPd: 'eee8aa', + pOegYF: '98fb98', + pOeQe: 'afeeee', + pOeviTetYd: 'db7093', + papayawEp: 'ffefd5', + pHKpuff: 'ffdab9', + peru: 'cd853f', + pRk: 'ffc0cb', + plum: 'dda0dd', + powMrXe: 'b0e0e6', + purpN: '800080', + YbeccapurpN: '663399', + Yd: 'ff0000', + Psybrown: 'bc8f8f', + PyOXe: '4169e1', + saddNbPwn: '8b4513', + sOmon: 'fa8072', + sandybPwn: 'f4a460', + sHgYF: '2e8b57', + sHshell: 'fff5ee', + siFna: 'a0522d', + silver: 'c0c0c0', + skyXe: '87ceeb', + UXe: '6a5acd', + UWay: '708090', + UgYy: '708090', + snow: 'fffafa', + sprRggYF: 'ff7f', + stAlXe: '4682b4', + tan: 'd2b48c', + teO: '8080', + tEstN: 'd8bfd8', + tomato: 'ff6347', + Qe: '40e0d0', + viTet: 'ee82ee', + JHt: 'f5deb3', + wEte: 'ffffff', + wEtesmoke: 'f5f5f5', + Lw: 'ffff00', + LwgYF: '9acd32' +}; +function unpack() { + const unpacked = {}; + const keys = Object.keys(names$1); + const tkeys = Object.keys(map); + let i, j, k, ok, nk; + for (i = 0; i < keys.length; i++) { + ok = nk = keys[i]; + for (j = 0; j < tkeys.length; j++) { + k = tkeys[j]; + nk = nk.replace(k, map[k]); + } + k = parseInt(names$1[ok], 16); + unpacked[nk] = [k >> 16 & 0xFF, k >> 8 & 0xFF, k & 0xFF]; + } + return unpacked; +} +let names; +function nameParse(str) { + if (!names) { + names = unpack(); + names.transparent = [0, 0, 0, 0]; + } + const a = names[str.toLowerCase()]; + return a && { + r: a[0], + g: a[1], + b: a[2], + a: a.length === 4 ? a[3] : 255 + }; +} +const RGB_RE = /^rgba?\(\s*([-+.\d]+)(%)?[\s,]+([-+.e\d]+)(%)?[\s,]+([-+.e\d]+)(%)?(?:[\s,/]+([-+.e\d]+)(%)?)?\s*\)$/; +function rgbParse(str) { + const m = RGB_RE.exec(str); + let a = 255; + let r, g, b; + if (!m) { + return; + } + if (m[7] !== r) { + const v = +m[7]; + a = m[8] ? p2b(v) : lim(v * 255, 0, 255); + } + r = +m[1]; + g = +m[3]; + b = +m[5]; + r = 255 & (m[2] ? p2b(r) : lim(r, 0, 255)); + g = 255 & (m[4] ? p2b(g) : lim(g, 0, 255)); + b = 255 & (m[6] ? p2b(b) : lim(b, 0, 255)); + return { + r: r, + g: g, + b: b, + a: a + }; +} +function rgbString(v) { + return v && ( + v.a < 255 + ? `rgba(${v.r}, ${v.g}, ${v.b}, ${b2n(v.a)})` + : `rgb(${v.r}, ${v.g}, ${v.b})` + ); +} +const to = v => v <= 0.0031308 ? v * 12.92 : Math.pow(v, 1.0 / 2.4) * 1.055 - 0.055; +const from = v => v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); +function interpolate(rgb1, rgb2, t) { + const r = from(b2n(rgb1.r)); + const g = from(b2n(rgb1.g)); + const b = from(b2n(rgb1.b)); + return { + r: n2b(to(r + t * (from(b2n(rgb2.r)) - r))), + g: n2b(to(g + t * (from(b2n(rgb2.g)) - g))), + b: n2b(to(b + t * (from(b2n(rgb2.b)) - b))), + a: rgb1.a + t * (rgb2.a - rgb1.a) + }; +} +function modHSL(v, i, ratio) { + if (v) { + let tmp = rgb2hsl(v); + tmp[i] = Math.max(0, Math.min(tmp[i] + tmp[i] * ratio, i === 0 ? 360 : 1)); + tmp = hsl2rgb(tmp); + v.r = tmp[0]; + v.g = tmp[1]; + v.b = tmp[2]; + } +} +function clone(v, proto) { + return v ? Object.assign(proto || {}, v) : v; +} +function fromObject(input) { + var v = {r: 0, g: 0, b: 0, a: 255}; + if (Array.isArray(input)) { + if (input.length >= 3) { + v = {r: input[0], g: input[1], b: input[2], a: 255}; + if (input.length > 3) { + v.a = n2b(input[3]); + } + } + } else { + v = clone(input, {r: 0, g: 0, b: 0, a: 1}); + v.a = n2b(v.a); + } + return v; +} +function functionParse(str) { + if (str.charAt(0) === 'r') { + return rgbParse(str); + } + return hueParse(str); +} +class Color { + constructor(input) { + if (input instanceof Color) { + return input; + } + const type = typeof input; + let v; + if (type === 'object') { + v = fromObject(input); + } else if (type === 'string') { + v = hexParse(input) || nameParse(input) || functionParse(input); + } + this._rgb = v; + this._valid = !!v; + } + get valid() { + return this._valid; + } + get rgb() { + var v = clone(this._rgb); + if (v) { + v.a = b2n(v.a); + } + return v; + } + set rgb(obj) { + this._rgb = fromObject(obj); + } + rgbString() { + return this._valid ? rgbString(this._rgb) : undefined; + } + hexString() { + return this._valid ? hexString(this._rgb) : undefined; + } + hslString() { + return this._valid ? hslString(this._rgb) : undefined; + } + mix(color, weight) { + if (color) { + const c1 = this.rgb; + const c2 = color.rgb; + let w2; + const p = weight === w2 ? 0.5 : weight; + const w = 2 * p - 1; + const a = c1.a - c2.a; + const w1 = ((w * a === -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0; + w2 = 1 - w1; + c1.r = 0xFF & w1 * c1.r + w2 * c2.r + 0.5; + c1.g = 0xFF & w1 * c1.g + w2 * c2.g + 0.5; + c1.b = 0xFF & w1 * c1.b + w2 * c2.b + 0.5; + c1.a = p * c1.a + (1 - p) * c2.a; + this.rgb = c1; + } + return this; + } + interpolate(color, t) { + if (color) { + this._rgb = interpolate(this._rgb, color._rgb, t); + } + return this; + } + clone() { + return new Color(this.rgb); + } + alpha(a) { + this._rgb.a = n2b(a); + return this; + } + clearer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 - ratio; + return this; + } + greyscale() { + const rgb = this._rgb; + const val = round(rgb.r * 0.3 + rgb.g * 0.59 + rgb.b * 0.11); + rgb.r = rgb.g = rgb.b = val; + return this; + } + opaquer(ratio) { + const rgb = this._rgb; + rgb.a *= 1 + ratio; + return this; + } + negate() { + const v = this._rgb; + v.r = 255 - v.r; + v.g = 255 - v.g; + v.b = 255 - v.b; + return this; + } + lighten(ratio) { + modHSL(this._rgb, 2, ratio); + return this; + } + darken(ratio) { + modHSL(this._rgb, 2, -ratio); + return this; + } + saturate(ratio) { + modHSL(this._rgb, 1, ratio); + return this; + } + desaturate(ratio) { + modHSL(this._rgb, 1, -ratio); + return this; + } + rotate(deg) { + rotate(this._rgb, deg); + return this; + } +} +function index_esm(input) { + return new Color(input); +} + +function isPatternOrGradient(value) { + if (value && typeof value === 'object') { + const type = value.toString(); + return type === '[object CanvasPattern]' || type === '[object CanvasGradient]'; + } + return false; +} +function color(value) { + return isPatternOrGradient(value) ? value : index_esm(value); +} +function getHoverColor(value) { + return isPatternOrGradient(value) + ? value + : index_esm(value).saturate(0.5).darken(0.1).hexString(); +} + +const overrides = Object.create(null); +const descriptors = Object.create(null); +function getScope$1(node, key) { + if (!key) { + return node; + } + const keys = key.split('.'); + for (let i = 0, n = keys.length; i < n; ++i) { + const k = keys[i]; + node = node[k] || (node[k] = Object.create(null)); + } + return node; +} +function set(root, scope, values) { + if (typeof scope === 'string') { + return merge(getScope$1(root, scope), values); + } + return merge(getScope$1(root, ''), scope); +} +class Defaults { + constructor(_descriptors) { + this.animation = undefined; + this.backgroundColor = 'rgba(0,0,0,0.1)'; + this.borderColor = 'rgba(0,0,0,0.1)'; + this.color = '#666'; + this.datasets = {}; + this.devicePixelRatio = (context) => context.chart.platform.getDevicePixelRatio(); + this.elements = {}; + this.events = [ + 'mousemove', + 'mouseout', + 'click', + 'touchstart', + 'touchmove' + ]; + this.font = { + family: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif", + size: 12, + style: 'normal', + lineHeight: 1.2, + weight: null + }; + this.hover = {}; + this.hoverBackgroundColor = (ctx, options) => getHoverColor(options.backgroundColor); + this.hoverBorderColor = (ctx, options) => getHoverColor(options.borderColor); + this.hoverColor = (ctx, options) => getHoverColor(options.color); + this.indexAxis = 'x'; + this.interaction = { + mode: 'nearest', + intersect: true, + includeInvisible: false + }; + this.maintainAspectRatio = true; + this.onHover = null; + this.onClick = null; + this.parsing = true; + this.plugins = {}; + this.responsive = true; + this.scale = undefined; + this.scales = {}; + this.showLine = true; + this.drawActiveElementsOnTop = true; + this.describe(_descriptors); + } + set(scope, values) { + return set(this, scope, values); + } + get(scope) { + return getScope$1(this, scope); + } + describe(scope, values) { + return set(descriptors, scope, values); + } + override(scope, values) { + return set(overrides, scope, values); + } + route(scope, name, targetScope, targetName) { + const scopeObject = getScope$1(this, scope); + const targetScopeObject = getScope$1(this, targetScope); + const privateName = '_' + name; + Object.defineProperties(scopeObject, { + [privateName]: { + value: scopeObject[name], + writable: true + }, + [name]: { + enumerable: true, + get() { + const local = this[privateName]; + const target = targetScopeObject[targetName]; + if (isObject(local)) { + return Object.assign({}, target, local); + } + return valueOrDefault(local, target); + }, + set(value) { + this[privateName] = value; + } + } + }); + } +} +var defaults = new Defaults({ + _scriptable: (name) => !name.startsWith('on'), + _indexable: (name) => name !== 'events', + hover: { + _fallback: 'interaction' + }, + interaction: { + _scriptable: false, + _indexable: false, + } +}); + +function toFontString(font) { + if (!font || isNullOrUndef(font.size) || isNullOrUndef(font.family)) { + return null; + } + return (font.style ? font.style + ' ' : '') + + (font.weight ? font.weight + ' ' : '') + + font.size + 'px ' + + font.family; +} +function _measureText(ctx, data, gc, longest, string) { + let textWidth = data[string]; + if (!textWidth) { + textWidth = data[string] = ctx.measureText(string).width; + gc.push(string); + } + if (textWidth > longest) { + longest = textWidth; + } + return longest; +} +function _longestText(ctx, font, arrayOfThings, cache) { + cache = cache || {}; + let data = cache.data = cache.data || {}; + let gc = cache.garbageCollect = cache.garbageCollect || []; + if (cache.font !== font) { + data = cache.data = {}; + gc = cache.garbageCollect = []; + cache.font = font; + } + ctx.save(); + ctx.font = font; + let longest = 0; + const ilen = arrayOfThings.length; + let i, j, jlen, thing, nestedThing; + for (i = 0; i < ilen; i++) { + thing = arrayOfThings[i]; + if (thing !== undefined && thing !== null && isArray(thing) !== true) { + longest = _measureText(ctx, data, gc, longest, thing); + } else if (isArray(thing)) { + for (j = 0, jlen = thing.length; j < jlen; j++) { + nestedThing = thing[j]; + if (nestedThing !== undefined && nestedThing !== null && !isArray(nestedThing)) { + longest = _measureText(ctx, data, gc, longest, nestedThing); + } + } + } + } + ctx.restore(); + const gcLen = gc.length / 2; + if (gcLen > arrayOfThings.length) { + for (i = 0; i < gcLen; i++) { + delete data[gc[i]]; + } + gc.splice(0, gcLen); + } + return longest; +} +function _alignPixel(chart, pixel, width) { + const devicePixelRatio = chart.currentDevicePixelRatio; + const halfWidth = width !== 0 ? Math.max(width / 2, 0.5) : 0; + return Math.round((pixel - halfWidth) * devicePixelRatio) / devicePixelRatio + halfWidth; +} +function clearCanvas(canvas, ctx) { + ctx = ctx || canvas.getContext('2d'); + ctx.save(); + ctx.resetTransform(); + ctx.clearRect(0, 0, canvas.width, canvas.height); + ctx.restore(); +} +function drawPoint(ctx, options, x, y) { + drawPointLegend(ctx, options, x, y, null); +} +function drawPointLegend(ctx, options, x, y, w) { + let type, xOffset, yOffset, size, cornerRadius, width; + const style = options.pointStyle; + const rotation = options.rotation; + const radius = options.radius; + let rad = (rotation || 0) * RAD_PER_DEG; + if (style && typeof style === 'object') { + type = style.toString(); + if (type === '[object HTMLImageElement]' || type === '[object HTMLCanvasElement]') { + ctx.save(); + ctx.translate(x, y); + ctx.rotate(rad); + ctx.drawImage(style, -style.width / 2, -style.height / 2, style.width, style.height); + ctx.restore(); + return; + } + } + if (isNaN(radius) || radius <= 0) { + return; + } + ctx.beginPath(); + switch (style) { + default: + if (w) { + ctx.ellipse(x, y, w / 2, radius, 0, 0, TAU); + } else { + ctx.arc(x, y, radius, 0, TAU); + } + ctx.closePath(); + break; + case 'triangle': + ctx.moveTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + rad += TWO_THIRDS_PI; + ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + rad += TWO_THIRDS_PI; + ctx.lineTo(x + Math.sin(rad) * radius, y - Math.cos(rad) * radius); + ctx.closePath(); + break; + case 'rectRounded': + cornerRadius = radius * 0.516; + size = radius - cornerRadius; + xOffset = Math.cos(rad + QUARTER_PI) * size; + yOffset = Math.sin(rad + QUARTER_PI) * size; + ctx.arc(x - xOffset, y - yOffset, cornerRadius, rad - PI, rad - HALF_PI); + ctx.arc(x + yOffset, y - xOffset, cornerRadius, rad - HALF_PI, rad); + ctx.arc(x + xOffset, y + yOffset, cornerRadius, rad, rad + HALF_PI); + ctx.arc(x - yOffset, y + xOffset, cornerRadius, rad + HALF_PI, rad + PI); + ctx.closePath(); + break; + case 'rect': + if (!rotation) { + size = Math.SQRT1_2 * radius; + width = w ? w / 2 : size; + ctx.rect(x - width, y - size, 2 * width, 2 * size); + break; + } + rad += QUARTER_PI; + case 'rectRot': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + yOffset, y - xOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.lineTo(x - yOffset, y + xOffset); + ctx.closePath(); + break; + case 'crossRot': + rad += QUARTER_PI; + case 'cross': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + break; + case 'star': + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + rad += QUARTER_PI; + xOffset = Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + ctx.moveTo(x + yOffset, y - xOffset); + ctx.lineTo(x - yOffset, y + xOffset); + break; + case 'line': + xOffset = w ? w / 2 : Math.cos(rad) * radius; + yOffset = Math.sin(rad) * radius; + ctx.moveTo(x - xOffset, y - yOffset); + ctx.lineTo(x + xOffset, y + yOffset); + break; + case 'dash': + ctx.moveTo(x, y); + ctx.lineTo(x + Math.cos(rad) * radius, y + Math.sin(rad) * radius); + break; + } + ctx.fill(); + if (options.borderWidth > 0) { + ctx.stroke(); + } +} +function _isPointInArea(point, area, margin) { + margin = margin || 0.5; + return !area || (point && point.x > area.left - margin && point.x < area.right + margin && + point.y > area.top - margin && point.y < area.bottom + margin); +} +function clipArea(ctx, area) { + ctx.save(); + ctx.beginPath(); + ctx.rect(area.left, area.top, area.right - area.left, area.bottom - area.top); + ctx.clip(); +} +function unclipArea(ctx) { + ctx.restore(); +} +function _steppedLineTo(ctx, previous, target, flip, mode) { + if (!previous) { + return ctx.lineTo(target.x, target.y); + } + if (mode === 'middle') { + const midpoint = (previous.x + target.x) / 2.0; + ctx.lineTo(midpoint, previous.y); + ctx.lineTo(midpoint, target.y); + } else if (mode === 'after' !== !!flip) { + ctx.lineTo(previous.x, target.y); + } else { + ctx.lineTo(target.x, previous.y); + } + ctx.lineTo(target.x, target.y); +} +function _bezierCurveTo(ctx, previous, target, flip) { + if (!previous) { + return ctx.lineTo(target.x, target.y); + } + ctx.bezierCurveTo( + flip ? previous.cp1x : previous.cp2x, + flip ? previous.cp1y : previous.cp2y, + flip ? target.cp2x : target.cp1x, + flip ? target.cp2y : target.cp1y, + target.x, + target.y); +} +function renderText(ctx, text, x, y, font, opts = {}) { + const lines = isArray(text) ? text : [text]; + const stroke = opts.strokeWidth > 0 && opts.strokeColor !== ''; + let i, line; + ctx.save(); + ctx.font = font.string; + setRenderOpts(ctx, opts); + for (i = 0; i < lines.length; ++i) { + line = lines[i]; + if (stroke) { + if (opts.strokeColor) { + ctx.strokeStyle = opts.strokeColor; + } + if (!isNullOrUndef(opts.strokeWidth)) { + ctx.lineWidth = opts.strokeWidth; + } + ctx.strokeText(line, x, y, opts.maxWidth); + } + ctx.fillText(line, x, y, opts.maxWidth); + decorateText(ctx, x, y, line, opts); + y += font.lineHeight; + } + ctx.restore(); +} +function setRenderOpts(ctx, opts) { + if (opts.translation) { + ctx.translate(opts.translation[0], opts.translation[1]); + } + if (!isNullOrUndef(opts.rotation)) { + ctx.rotate(opts.rotation); + } + if (opts.color) { + ctx.fillStyle = opts.color; + } + if (opts.textAlign) { + ctx.textAlign = opts.textAlign; + } + if (opts.textBaseline) { + ctx.textBaseline = opts.textBaseline; + } +} +function decorateText(ctx, x, y, line, opts) { + if (opts.strikethrough || opts.underline) { + const metrics = ctx.measureText(line); + const left = x - metrics.actualBoundingBoxLeft; + const right = x + metrics.actualBoundingBoxRight; + const top = y - metrics.actualBoundingBoxAscent; + const bottom = y + metrics.actualBoundingBoxDescent; + const yDecoration = opts.strikethrough ? (top + bottom) / 2 : bottom; + ctx.strokeStyle = ctx.fillStyle; + ctx.beginPath(); + ctx.lineWidth = opts.decorationWidth || 2; + ctx.moveTo(left, yDecoration); + ctx.lineTo(right, yDecoration); + ctx.stroke(); + } +} +function addRoundedRectPath(ctx, rect) { + const {x, y, w, h, radius} = rect; + ctx.arc(x + radius.topLeft, y + radius.topLeft, radius.topLeft, -HALF_PI, PI, true); + ctx.lineTo(x, y + h - radius.bottomLeft); + ctx.arc(x + radius.bottomLeft, y + h - radius.bottomLeft, radius.bottomLeft, PI, HALF_PI, true); + ctx.lineTo(x + w - radius.bottomRight, y + h); + ctx.arc(x + w - radius.bottomRight, y + h - radius.bottomRight, radius.bottomRight, HALF_PI, 0, true); + ctx.lineTo(x + w, y + radius.topRight); + ctx.arc(x + w - radius.topRight, y + radius.topRight, radius.topRight, 0, -HALF_PI, true); + ctx.lineTo(x + radius.topLeft, y); +} + +const LINE_HEIGHT = new RegExp(/^(normal|(\d+(?:\.\d+)?)(px|em|%)?)$/); +const FONT_STYLE = new RegExp(/^(normal|italic|initial|inherit|unset|(oblique( -?[0-9]?[0-9]deg)?))$/); +function toLineHeight(value, size) { + const matches = ('' + value).match(LINE_HEIGHT); + if (!matches || matches[1] === 'normal') { + return size * 1.2; + } + value = +matches[2]; + switch (matches[3]) { + case 'px': + return value; + case '%': + value /= 100; + break; + } + return size * value; +} +const numberOrZero = v => +v || 0; +function _readValueToProps(value, props) { + const ret = {}; + const objProps = isObject(props); + const keys = objProps ? Object.keys(props) : props; + const read = isObject(value) + ? objProps + ? prop => valueOrDefault(value[prop], value[props[prop]]) + : prop => value[prop] + : () => value; + for (const prop of keys) { + ret[prop] = numberOrZero(read(prop)); + } + return ret; +} +function toTRBL(value) { + return _readValueToProps(value, {top: 'y', right: 'x', bottom: 'y', left: 'x'}); +} +function toTRBLCorners(value) { + return _readValueToProps(value, ['topLeft', 'topRight', 'bottomLeft', 'bottomRight']); +} +function toPadding(value) { + const obj = toTRBL(value); + obj.width = obj.left + obj.right; + obj.height = obj.top + obj.bottom; + return obj; +} +function toFont(options, fallback) { + options = options || {}; + fallback = fallback || defaults.font; + let size = valueOrDefault(options.size, fallback.size); + if (typeof size === 'string') { + size = parseInt(size, 10); + } + let style = valueOrDefault(options.style, fallback.style); + if (style && !('' + style).match(FONT_STYLE)) { + console.warn('Invalid font style specified: "' + style + '"'); + style = ''; + } + const font = { + family: valueOrDefault(options.family, fallback.family), + lineHeight: toLineHeight(valueOrDefault(options.lineHeight, fallback.lineHeight), size), + size, + style, + weight: valueOrDefault(options.weight, fallback.weight), + string: '' + }; + font.string = toFontString(font); + return font; +} +function resolve(inputs, context, index, info) { + let cacheable = true; + let i, ilen, value; + for (i = 0, ilen = inputs.length; i < ilen; ++i) { + value = inputs[i]; + if (value === undefined) { + continue; + } + if (context !== undefined && typeof value === 'function') { + value = value(context); + cacheable = false; + } + if (index !== undefined && isArray(value)) { + value = value[index % value.length]; + cacheable = false; + } + if (value !== undefined) { + if (info && !cacheable) { + info.cacheable = false; + } + return value; + } + } +} +function _addGrace(minmax, grace, beginAtZero) { + const {min, max} = minmax; + const change = toDimension(grace, (max - min) / 2); + const keepZero = (value, add) => beginAtZero && value === 0 ? 0 : value + add; + return { + min: keepZero(min, -Math.abs(change)), + max: keepZero(max, change) + }; +} +function createContext(parentContext, context) { + return Object.assign(Object.create(parentContext), context); +} + +function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fallback, getTarget = () => scopes[0]) { + if (!defined(fallback)) { + fallback = _resolve('_fallback', scopes); + } + const cache = { + [Symbol.toStringTag]: 'Object', + _cacheable: true, + _scopes: scopes, + _rootScopes: rootScopes, + _fallback: fallback, + _getTarget: getTarget, + override: (scope) => _createResolver([scope, ...scopes], prefixes, rootScopes, fallback), + }; + return new Proxy(cache, { + deleteProperty(target, prop) { + delete target[prop]; + delete target._keys; + delete scopes[0][prop]; + return true; + }, + get(target, prop) { + return _cached(target, prop, + () => _resolveWithPrefixes(prop, prefixes, scopes, target)); + }, + getOwnPropertyDescriptor(target, prop) { + return Reflect.getOwnPropertyDescriptor(target._scopes[0], prop); + }, + getPrototypeOf() { + return Reflect.getPrototypeOf(scopes[0]); + }, + has(target, prop) { + return getKeysFromAllScopes(target).includes(prop); + }, + ownKeys(target) { + return getKeysFromAllScopes(target); + }, + set(target, prop, value) { + const storage = target._storage || (target._storage = getTarget()); + target[prop] = storage[prop] = value; + delete target._keys; + return true; + } + }); +} +function _attachContext(proxy, context, subProxy, descriptorDefaults) { + const cache = { + _cacheable: false, + _proxy: proxy, + _context: context, + _subProxy: subProxy, + _stack: new Set(), + _descriptors: _descriptors(proxy, descriptorDefaults), + setContext: (ctx) => _attachContext(proxy, ctx, subProxy, descriptorDefaults), + override: (scope) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults) + }; + return new Proxy(cache, { + deleteProperty(target, prop) { + delete target[prop]; + delete proxy[prop]; + return true; + }, + get(target, prop, receiver) { + return _cached(target, prop, + () => _resolveWithContext(target, prop, receiver)); + }, + getOwnPropertyDescriptor(target, prop) { + return target._descriptors.allKeys + ? Reflect.has(proxy, prop) ? {enumerable: true, configurable: true} : undefined + : Reflect.getOwnPropertyDescriptor(proxy, prop); + }, + getPrototypeOf() { + return Reflect.getPrototypeOf(proxy); + }, + has(target, prop) { + return Reflect.has(proxy, prop); + }, + ownKeys() { + return Reflect.ownKeys(proxy); + }, + set(target, prop, value) { + proxy[prop] = value; + delete target[prop]; + return true; + } + }); +} +function _descriptors(proxy, defaults = {scriptable: true, indexable: true}) { + const {_scriptable = defaults.scriptable, _indexable = defaults.indexable, _allKeys = defaults.allKeys} = proxy; + return { + allKeys: _allKeys, + scriptable: _scriptable, + indexable: _indexable, + isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable, + isIndexable: isFunction(_indexable) ? _indexable : () => _indexable + }; +} +const readKey = (prefix, name) => prefix ? prefix + _capitalize(name) : name; +const needsSubResolver = (prop, value) => isObject(value) && prop !== 'adapters' && + (Object.getPrototypeOf(value) === null || value.constructor === Object); +function _cached(target, prop, resolve) { + if (Object.prototype.hasOwnProperty.call(target, prop)) { + return target[prop]; + } + const value = resolve(); + target[prop] = value; + return value; +} +function _resolveWithContext(target, prop, receiver) { + const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; + let value = _proxy[prop]; + if (isFunction(value) && descriptors.isScriptable(prop)) { + value = _resolveScriptable(prop, value, target, receiver); + } + if (isArray(value) && value.length) { + value = _resolveArray(prop, value, target, descriptors.isIndexable); + } + if (needsSubResolver(prop, value)) { + value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors); + } + return value; +} +function _resolveScriptable(prop, value, target, receiver) { + const {_proxy, _context, _subProxy, _stack} = target; + if (_stack.has(prop)) { + throw new Error('Recursion detected: ' + Array.from(_stack).join('->') + '->' + prop); + } + _stack.add(prop); + value = value(_context, _subProxy || receiver); + _stack.delete(prop); + if (needsSubResolver(prop, value)) { + value = createSubResolver(_proxy._scopes, _proxy, prop, value); + } + return value; +} +function _resolveArray(prop, value, target, isIndexable) { + const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; + if (defined(_context.index) && isIndexable(prop)) { + value = value[_context.index % value.length]; + } else if (isObject(value[0])) { + const arr = value; + const scopes = _proxy._scopes.filter(s => s !== arr); + value = []; + for (const item of arr) { + const resolver = createSubResolver(scopes, _proxy, prop, item); + value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors)); + } + } + return value; +} +function resolveFallback(fallback, prop, value) { + return isFunction(fallback) ? fallback(prop, value) : fallback; +} +const getScope = (key, parent) => key === true ? parent + : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; +function addScopes(set, parentScopes, key, parentFallback, value) { + for (const parent of parentScopes) { + const scope = getScope(key, parent); + if (scope) { + set.add(scope); + const fallback = resolveFallback(scope._fallback, key, value); + if (defined(fallback) && fallback !== key && fallback !== parentFallback) { + return fallback; + } + } else if (scope === false && defined(parentFallback) && key !== parentFallback) { + return null; + } + } + return false; +} +function createSubResolver(parentScopes, resolver, prop, value) { + const rootScopes = resolver._rootScopes; + const fallback = resolveFallback(resolver._fallback, prop, value); + const allScopes = [...parentScopes, ...rootScopes]; + const set = new Set(); + set.add(value); + let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); + if (key === null) { + return false; + } + if (defined(fallback) && fallback !== prop) { + key = addScopesFromKey(set, allScopes, fallback, key, value); + if (key === null) { + return false; + } + } + return _createResolver(Array.from(set), [''], rootScopes, fallback, + () => subGetTarget(resolver, prop, value)); +} +function addScopesFromKey(set, allScopes, key, fallback, item) { + while (key) { + key = addScopes(set, allScopes, key, fallback, item); + } + return key; +} +function subGetTarget(resolver, prop, value) { + const parent = resolver._getTarget(); + if (!(prop in parent)) { + parent[prop] = {}; + } + const target = parent[prop]; + if (isArray(target) && isObject(value)) { + return value; + } + return target; +} +function _resolveWithPrefixes(prop, prefixes, scopes, proxy) { + let value; + for (const prefix of prefixes) { + value = _resolve(readKey(prefix, prop), scopes); + if (defined(value)) { + return needsSubResolver(prop, value) + ? createSubResolver(scopes, proxy, prop, value) + : value; + } + } +} +function _resolve(key, scopes) { + for (const scope of scopes) { + if (!scope) { + continue; + } + const value = scope[key]; + if (defined(value)) { + return value; + } + } +} +function getKeysFromAllScopes(target) { + let keys = target._keys; + if (!keys) { + keys = target._keys = resolveKeysFromAllScopes(target._scopes); + } + return keys; +} +function resolveKeysFromAllScopes(scopes) { + const set = new Set(); + for (const scope of scopes) { + for (const key of Object.keys(scope).filter(k => !k.startsWith('_'))) { + set.add(key); + } + } + return Array.from(set); +} +function _parseObjectDataRadialScale(meta, data, start, count) { + const {iScale} = meta; + const {key = 'r'} = this._parsing; + const parsed = new Array(count); + let i, ilen, index, item; + for (i = 0, ilen = count; i < ilen; ++i) { + index = i + start; + item = data[index]; + parsed[i] = { + r: iScale.parse(resolveObjectKey(item, key), index) + }; + } + return parsed; +} + +const EPSILON = Number.EPSILON || 1e-14; +const getPoint = (points, i) => i < points.length && !points[i].skip && points[i]; +const getValueAxis = (indexAxis) => indexAxis === 'x' ? 'y' : 'x'; +function splineCurve(firstPoint, middlePoint, afterPoint, t) { + const previous = firstPoint.skip ? middlePoint : firstPoint; + const current = middlePoint; + const next = afterPoint.skip ? middlePoint : afterPoint; + const d01 = distanceBetweenPoints(current, previous); + const d12 = distanceBetweenPoints(next, current); + let s01 = d01 / (d01 + d12); + let s12 = d12 / (d01 + d12); + s01 = isNaN(s01) ? 0 : s01; + s12 = isNaN(s12) ? 0 : s12; + const fa = t * s01; + const fb = t * s12; + return { + previous: { + x: current.x - fa * (next.x - previous.x), + y: current.y - fa * (next.y - previous.y) + }, + next: { + x: current.x + fb * (next.x - previous.x), + y: current.y + fb * (next.y - previous.y) + } + }; +} +function monotoneAdjust(points, deltaK, mK) { + const pointsLen = points.length; + let alphaK, betaK, tauK, squaredMagnitude, pointCurrent; + let pointAfter = getPoint(points, 0); + for (let i = 0; i < pointsLen - 1; ++i) { + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent || !pointAfter) { + continue; + } + if (almostEquals(deltaK[i], 0, EPSILON)) { + mK[i] = mK[i + 1] = 0; + continue; + } + alphaK = mK[i] / deltaK[i]; + betaK = mK[i + 1] / deltaK[i]; + squaredMagnitude = Math.pow(alphaK, 2) + Math.pow(betaK, 2); + if (squaredMagnitude <= 9) { + continue; + } + tauK = 3 / Math.sqrt(squaredMagnitude); + mK[i] = alphaK * tauK * deltaK[i]; + mK[i + 1] = betaK * tauK * deltaK[i]; + } +} +function monotoneCompute(points, mK, indexAxis = 'x') { + const valueAxis = getValueAxis(indexAxis); + const pointsLen = points.length; + let delta, pointBefore, pointCurrent; + let pointAfter = getPoint(points, 0); + for (let i = 0; i < pointsLen; ++i) { + pointBefore = pointCurrent; + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent) { + continue; + } + const iPixel = pointCurrent[indexAxis]; + const vPixel = pointCurrent[valueAxis]; + if (pointBefore) { + delta = (iPixel - pointBefore[indexAxis]) / 3; + pointCurrent[`cp1${indexAxis}`] = iPixel - delta; + pointCurrent[`cp1${valueAxis}`] = vPixel - delta * mK[i]; + } + if (pointAfter) { + delta = (pointAfter[indexAxis] - iPixel) / 3; + pointCurrent[`cp2${indexAxis}`] = iPixel + delta; + pointCurrent[`cp2${valueAxis}`] = vPixel + delta * mK[i]; + } + } +} +function splineCurveMonotone(points, indexAxis = 'x') { + const valueAxis = getValueAxis(indexAxis); + const pointsLen = points.length; + const deltaK = Array(pointsLen).fill(0); + const mK = Array(pointsLen); + let i, pointBefore, pointCurrent; + let pointAfter = getPoint(points, 0); + for (i = 0; i < pointsLen; ++i) { + pointBefore = pointCurrent; + pointCurrent = pointAfter; + pointAfter = getPoint(points, i + 1); + if (!pointCurrent) { + continue; + } + if (pointAfter) { + const slopeDelta = pointAfter[indexAxis] - pointCurrent[indexAxis]; + deltaK[i] = slopeDelta !== 0 ? (pointAfter[valueAxis] - pointCurrent[valueAxis]) / slopeDelta : 0; + } + mK[i] = !pointBefore ? deltaK[i] + : !pointAfter ? deltaK[i - 1] + : (sign(deltaK[i - 1]) !== sign(deltaK[i])) ? 0 + : (deltaK[i - 1] + deltaK[i]) / 2; + } + monotoneAdjust(points, deltaK, mK); + monotoneCompute(points, mK, indexAxis); +} +function capControlPoint(pt, min, max) { + return Math.max(Math.min(pt, max), min); +} +function capBezierPoints(points, area) { + let i, ilen, point, inArea, inAreaPrev; + let inAreaNext = _isPointInArea(points[0], area); + for (i = 0, ilen = points.length; i < ilen; ++i) { + inAreaPrev = inArea; + inArea = inAreaNext; + inAreaNext = i < ilen - 1 && _isPointInArea(points[i + 1], area); + if (!inArea) { + continue; + } + point = points[i]; + if (inAreaPrev) { + point.cp1x = capControlPoint(point.cp1x, area.left, area.right); + point.cp1y = capControlPoint(point.cp1y, area.top, area.bottom); + } + if (inAreaNext) { + point.cp2x = capControlPoint(point.cp2x, area.left, area.right); + point.cp2y = capControlPoint(point.cp2y, area.top, area.bottom); + } + } +} +function _updateBezierControlPoints(points, options, area, loop, indexAxis) { + let i, ilen, point, controlPoints; + if (options.spanGaps) { + points = points.filter((pt) => !pt.skip); + } + if (options.cubicInterpolationMode === 'monotone') { + splineCurveMonotone(points, indexAxis); + } else { + let prev = loop ? points[points.length - 1] : points[0]; + for (i = 0, ilen = points.length; i < ilen; ++i) { + point = points[i]; + controlPoints = splineCurve( + prev, + point, + points[Math.min(i + 1, ilen - (loop ? 0 : 1)) % ilen], + options.tension + ); + point.cp1x = controlPoints.previous.x; + point.cp1y = controlPoints.previous.y; + point.cp2x = controlPoints.next.x; + point.cp2y = controlPoints.next.y; + prev = point; + } + } + if (options.capBezierPoints) { + capBezierPoints(points, area); + } +} + +function _isDomSupported() { + return typeof window !== 'undefined' && typeof document !== 'undefined'; +} +function _getParentNode(domNode) { + let parent = domNode.parentNode; + if (parent && parent.toString() === '[object ShadowRoot]') { + parent = parent.host; + } + return parent; +} +function parseMaxStyle(styleValue, node, parentProperty) { + let valueInPixels; + if (typeof styleValue === 'string') { + valueInPixels = parseInt(styleValue, 10); + if (styleValue.indexOf('%') !== -1) { + valueInPixels = valueInPixels / 100 * node.parentNode[parentProperty]; + } + } else { + valueInPixels = styleValue; + } + return valueInPixels; +} +const getComputedStyle = (element) => window.getComputedStyle(element, null); +function getStyle(el, property) { + return getComputedStyle(el).getPropertyValue(property); +} +const positions = ['top', 'right', 'bottom', 'left']; +function getPositionedStyle(styles, style, suffix) { + const result = {}; + suffix = suffix ? '-' + suffix : ''; + for (let i = 0; i < 4; i++) { + const pos = positions[i]; + result[pos] = parseFloat(styles[style + '-' + pos + suffix]) || 0; + } + result.width = result.left + result.right; + result.height = result.top + result.bottom; + return result; +} +const useOffsetPos = (x, y, target) => (x > 0 || y > 0) && (!target || !target.shadowRoot); +function getCanvasPosition(e, canvas) { + const touches = e.touches; + const source = touches && touches.length ? touches[0] : e; + const {offsetX, offsetY} = source; + let box = false; + let x, y; + if (useOffsetPos(offsetX, offsetY, e.target)) { + x = offsetX; + y = offsetY; + } else { + const rect = canvas.getBoundingClientRect(); + x = source.clientX - rect.left; + y = source.clientY - rect.top; + box = true; + } + return {x, y, box}; +} +function getRelativePosition(evt, chart) { + if ('native' in evt) { + return evt; + } + const {canvas, currentDevicePixelRatio} = chart; + const style = getComputedStyle(canvas); + const borderBox = style.boxSizing === 'border-box'; + const paddings = getPositionedStyle(style, 'padding'); + const borders = getPositionedStyle(style, 'border', 'width'); + const {x, y, box} = getCanvasPosition(evt, canvas); + const xOffset = paddings.left + (box && borders.left); + const yOffset = paddings.top + (box && borders.top); + let {width, height} = chart; + if (borderBox) { + width -= paddings.width + borders.width; + height -= paddings.height + borders.height; + } + return { + x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio), + y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio) + }; +} +function getContainerSize(canvas, width, height) { + let maxWidth, maxHeight; + if (width === undefined || height === undefined) { + const container = _getParentNode(canvas); + if (!container) { + width = canvas.clientWidth; + height = canvas.clientHeight; + } else { + const rect = container.getBoundingClientRect(); + const containerStyle = getComputedStyle(container); + const containerBorder = getPositionedStyle(containerStyle, 'border', 'width'); + const containerPadding = getPositionedStyle(containerStyle, 'padding'); + width = rect.width - containerPadding.width - containerBorder.width; + height = rect.height - containerPadding.height - containerBorder.height; + maxWidth = parseMaxStyle(containerStyle.maxWidth, container, 'clientWidth'); + maxHeight = parseMaxStyle(containerStyle.maxHeight, container, 'clientHeight'); + } + } + return { + width, + height, + maxWidth: maxWidth || INFINITY, + maxHeight: maxHeight || INFINITY + }; +} +const round1 = v => Math.round(v * 10) / 10; +function getMaximumSize(canvas, bbWidth, bbHeight, aspectRatio) { + const style = getComputedStyle(canvas); + const margins = getPositionedStyle(style, 'margin'); + const maxWidth = parseMaxStyle(style.maxWidth, canvas, 'clientWidth') || INFINITY; + const maxHeight = parseMaxStyle(style.maxHeight, canvas, 'clientHeight') || INFINITY; + const containerSize = getContainerSize(canvas, bbWidth, bbHeight); + let {width, height} = containerSize; + if (style.boxSizing === 'content-box') { + const borders = getPositionedStyle(style, 'border', 'width'); + const paddings = getPositionedStyle(style, 'padding'); + width -= paddings.width + borders.width; + height -= paddings.height + borders.height; + } + width = Math.max(0, width - margins.width); + height = Math.max(0, aspectRatio ? Math.floor(width / aspectRatio) : height - margins.height); + width = round1(Math.min(width, maxWidth, containerSize.maxWidth)); + height = round1(Math.min(height, maxHeight, containerSize.maxHeight)); + if (width && !height) { + height = round1(width / 2); + } + return { + width, + height + }; +} +function retinaScale(chart, forceRatio, forceStyle) { + const pixelRatio = forceRatio || 1; + const deviceHeight = Math.floor(chart.height * pixelRatio); + const deviceWidth = Math.floor(chart.width * pixelRatio); + chart.height = deviceHeight / pixelRatio; + chart.width = deviceWidth / pixelRatio; + const canvas = chart.canvas; + if (canvas.style && (forceStyle || (!canvas.style.height && !canvas.style.width))) { + canvas.style.height = `${chart.height}px`; + canvas.style.width = `${chart.width}px`; + } + if (chart.currentDevicePixelRatio !== pixelRatio + || canvas.height !== deviceHeight + || canvas.width !== deviceWidth) { + chart.currentDevicePixelRatio = pixelRatio; + canvas.height = deviceHeight; + canvas.width = deviceWidth; + chart.ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0); + return true; + } + return false; +} +const supportsEventListenerOptions = (function() { + let passiveSupported = false; + try { + const options = { + get passive() { + passiveSupported = true; + return false; + } + }; + window.addEventListener('test', null, options); + window.removeEventListener('test', null, options); + } catch (e) { + } + return passiveSupported; +}()); +function readUsedSize(element, property) { + const value = getStyle(element, property); + const matches = value && value.match(/^(\d+)(\.\d+)?px$/); + return matches ? +matches[1] : undefined; +} + +function _pointInLine(p1, p2, t, mode) { + return { + x: p1.x + t * (p2.x - p1.x), + y: p1.y + t * (p2.y - p1.y) + }; +} +function _steppedInterpolation(p1, p2, t, mode) { + return { + x: p1.x + t * (p2.x - p1.x), + y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y + : mode === 'after' ? t < 1 ? p1.y : p2.y + : t > 0 ? p2.y : p1.y + }; +} +function _bezierInterpolation(p1, p2, t, mode) { + const cp1 = {x: p1.cp2x, y: p1.cp2y}; + const cp2 = {x: p2.cp1x, y: p2.cp1y}; + const a = _pointInLine(p1, cp1, t); + const b = _pointInLine(cp1, cp2, t); + const c = _pointInLine(cp2, p2, t); + const d = _pointInLine(a, b, t); + const e = _pointInLine(b, c, t); + return _pointInLine(d, e, t); +} + +const intlCache = new Map(); +function getNumberFormat(locale, options) { + options = options || {}; + const cacheKey = locale + JSON.stringify(options); + let formatter = intlCache.get(cacheKey); + if (!formatter) { + formatter = new Intl.NumberFormat(locale, options); + intlCache.set(cacheKey, formatter); + } + return formatter; +} +function formatNumber(num, locale, options) { + return getNumberFormat(locale, options).format(num); +} + +const getRightToLeftAdapter = function(rectX, width) { + return { + x(x) { + return rectX + rectX + width - x; + }, + setWidth(w) { + width = w; + }, + textAlign(align) { + if (align === 'center') { + return align; + } + return align === 'right' ? 'left' : 'right'; + }, + xPlus(x, value) { + return x - value; + }, + leftForLtr(x, itemWidth) { + return x - itemWidth; + }, + }; +}; +const getLeftToRightAdapter = function() { + return { + x(x) { + return x; + }, + setWidth(w) { + }, + textAlign(align) { + return align; + }, + xPlus(x, value) { + return x + value; + }, + leftForLtr(x, _itemWidth) { + return x; + }, + }; +}; +function getRtlAdapter(rtl, rectX, width) { + return rtl ? getRightToLeftAdapter(rectX, width) : getLeftToRightAdapter(); +} +function overrideTextDirection(ctx, direction) { + let style, original; + if (direction === 'ltr' || direction === 'rtl') { + style = ctx.canvas.style; + original = [ + style.getPropertyValue('direction'), + style.getPropertyPriority('direction'), + ]; + style.setProperty('direction', direction, 'important'); + ctx.prevTextDirection = original; + } +} +function restoreTextDirection(ctx, original) { + if (original !== undefined) { + delete ctx.prevTextDirection; + ctx.canvas.style.setProperty('direction', original[0], original[1]); + } +} + +function propertyFn(property) { + if (property === 'angle') { + return { + between: _angleBetween, + compare: _angleDiff, + normalize: _normalizeAngle, + }; + } + return { + between: _isBetween, + compare: (a, b) => a - b, + normalize: x => x + }; +} +function normalizeSegment({start, end, count, loop, style}) { + return { + start: start % count, + end: end % count, + loop: loop && (end - start + 1) % count === 0, + style + }; +} +function getSegment(segment, points, bounds) { + const {property, start: startBound, end: endBound} = bounds; + const {between, normalize} = propertyFn(property); + const count = points.length; + let {start, end, loop} = segment; + let i, ilen; + if (loop) { + start += count; + end += count; + for (i = 0, ilen = count; i < ilen; ++i) { + if (!between(normalize(points[start % count][property]), startBound, endBound)) { + break; + } + start--; + end--; + } + start %= count; + end %= count; + } + if (end < start) { + end += count; + } + return {start, end, loop, style: segment.style}; +} +function _boundSegment(segment, points, bounds) { + if (!bounds) { + return [segment]; + } + const {property, start: startBound, end: endBound} = bounds; + const count = points.length; + const {compare, between, normalize} = propertyFn(property); + const {start, end, loop, style} = getSegment(segment, points, bounds); + const result = []; + let inside = false; + let subStart = null; + let value, point, prevValue; + const startIsBefore = () => between(startBound, prevValue, value) && compare(startBound, prevValue) !== 0; + const endIsBefore = () => compare(endBound, value) === 0 || between(endBound, prevValue, value); + const shouldStart = () => inside || startIsBefore(); + const shouldStop = () => !inside || endIsBefore(); + for (let i = start, prev = start; i <= end; ++i) { + point = points[i % count]; + if (point.skip) { + continue; + } + value = normalize(point[property]); + if (value === prevValue) { + continue; + } + inside = between(value, startBound, endBound); + if (subStart === null && shouldStart()) { + subStart = compare(value, startBound) === 0 ? i : prev; + } + if (subStart !== null && shouldStop()) { + result.push(normalizeSegment({start: subStart, end: i, loop, count, style})); + subStart = null; + } + prev = i; + prevValue = value; + } + if (subStart !== null) { + result.push(normalizeSegment({start: subStart, end, loop, count, style})); + } + return result; +} +function _boundSegments(line, bounds) { + const result = []; + const segments = line.segments; + for (let i = 0; i < segments.length; i++) { + const sub = _boundSegment(segments[i], line.points, bounds); + if (sub.length) { + result.push(...sub); + } + } + return result; +} +function findStartAndEnd(points, count, loop, spanGaps) { + let start = 0; + let end = count - 1; + if (loop && !spanGaps) { + while (start < count && !points[start].skip) { + start++; + } + } + while (start < count && points[start].skip) { + start++; + } + start %= count; + if (loop) { + end += start; + } + while (end > start && points[end % count].skip) { + end--; + } + end %= count; + return {start, end}; +} +function solidSegments(points, start, max, loop) { + const count = points.length; + const result = []; + let last = start; + let prev = points[start]; + let end; + for (end = start + 1; end <= max; ++end) { + const cur = points[end % count]; + if (cur.skip || cur.stop) { + if (!prev.skip) { + loop = false; + result.push({start: start % count, end: (end - 1) % count, loop}); + start = last = cur.stop ? end : null; + } + } else { + last = end; + if (prev.skip) { + start = end; + } + } + prev = cur; + } + if (last !== null) { + result.push({start: start % count, end: last % count, loop}); + } + return result; +} +function _computeSegments(line, segmentOptions) { + const points = line.points; + const spanGaps = line.options.spanGaps; + const count = points.length; + if (!count) { + return []; + } + const loop = !!line._loop; + const {start, end} = findStartAndEnd(points, count, loop, spanGaps); + if (spanGaps === true) { + return splitByStyles(line, [{start, end, loop}], points, segmentOptions); + } + const max = end < start ? end + count : end; + const completeLoop = !!line._fullLoop && start === 0 && end === count - 1; + return splitByStyles(line, solidSegments(points, start, max, completeLoop), points, segmentOptions); +} +function splitByStyles(line, segments, points, segmentOptions) { + if (!segmentOptions || !segmentOptions.setContext || !points) { + return segments; + } + return doSplitByStyles(line, segments, points, segmentOptions); +} +function doSplitByStyles(line, segments, points, segmentOptions) { + const chartContext = line._chart.getContext(); + const baseStyle = readStyle(line.options); + const {_datasetIndex: datasetIndex, options: {spanGaps}} = line; + const count = points.length; + const result = []; + let prevStyle = baseStyle; + let start = segments[0].start; + let i = start; + function addStyle(s, e, l, st) { + const dir = spanGaps ? -1 : 1; + if (s === e) { + return; + } + s += count; + while (points[s % count].skip) { + s -= dir; + } + while (points[e % count].skip) { + e += dir; + } + if (s % count !== e % count) { + result.push({start: s % count, end: e % count, loop: l, style: st}); + prevStyle = st; + start = e % count; + } + } + for (const segment of segments) { + start = spanGaps ? start : segment.start; + let prev = points[start % count]; + let style; + for (i = start + 1; i <= segment.end; i++) { + const pt = points[i % count]; + style = readStyle(segmentOptions.setContext(createContext(chartContext, { + type: 'segment', + p0: prev, + p1: pt, + p0DataIndex: (i - 1) % count, + p1DataIndex: i % count, + datasetIndex + }))); + if (styleChanged(style, prevStyle)) { + addStyle(start, i - 1, segment.loop, prevStyle); + } + prev = pt; + prevStyle = style; + } + if (start < i - 1) { + addStyle(start, i - 1, segment.loop, prevStyle); + } + } + return result; +} +function readStyle(options) { + return { + backgroundColor: options.backgroundColor, + borderCapStyle: options.borderCapStyle, + borderDash: options.borderDash, + borderDashOffset: options.borderDashOffset, + borderJoinStyle: options.borderJoinStyle, + borderWidth: options.borderWidth, + borderColor: options.borderColor + }; +} +function styleChanged(style, prevStyle) { + return prevStyle && JSON.stringify(style) !== JSON.stringify(prevStyle); +} + +export { _isPointInArea as $, _factorize as A, finiteOrDefault as B, callback as C, _addGrace as D, _limitValue as E, toDegrees as F, _measureText as G, HALF_PI as H, _int16Range as I, _alignPixel as J, toPadding as K, clipArea as L, renderText as M, unclipArea as N, toFont as O, PI as P, each as Q, _toLeftRightCenter as R, _alignStartEnd as S, TAU as T, overrides as U, merge as V, _capitalize as W, getRelativePosition as X, _rlookupByKey as Y, _lookupByKey as Z, _arrayUnique as _, resolve as a, toLineHeight as a$, getAngleFromPoint as a0, getMaximumSize as a1, _getParentNode as a2, readUsedSize as a3, throttled as a4, supportsEventListenerOptions as a5, _isDomSupported as a6, descriptors as a7, isFunction as a8, _attachContext as a9, getRtlAdapter as aA, overrideTextDirection as aB, _textX as aC, restoreTextDirection as aD, drawPointLegend as aE, noop as aF, distanceBetweenPoints as aG, _setMinAndMaxByKey as aH, niceNum as aI, almostWhole as aJ, almostEquals as aK, _decimalPlaces as aL, _longestText as aM, _filterBetween as aN, _lookup as aO, isPatternOrGradient as aP, getHoverColor as aQ, clone$1 as aR, _merger as aS, _mergerIf as aT, _deprecated as aU, _splitKey as aV, toFontString as aW, splineCurve as aX, splineCurveMonotone as aY, getStyle as aZ, fontString as a_, _createResolver as aa, _descriptors as ab, mergeIf as ac, uid as ad, debounce as ae, retinaScale as af, clearCanvas as ag, setsEqual as ah, _elementsEqual as ai, _isClickEvent as aj, _isBetween as ak, _readValueToProps as al, _updateBezierControlPoints as am, _computeSegments as an, _boundSegments as ao, _steppedInterpolation as ap, _bezierInterpolation as aq, _pointInLine as ar, _steppedLineTo as as, _bezierCurveTo as at, drawPoint as au, addRoundedRectPath as av, toTRBL as aw, toTRBLCorners as ax, _boundSegment as ay, _normalizeAngle as az, isArray as b, PITAU as b0, INFINITY as b1, RAD_PER_DEG as b2, QUARTER_PI as b3, TWO_THIRDS_PI as b4, _angleDiff as b5, color as c, defaults as d, effects as e, resolveObjectKey as f, isNumberFinite as g, createContext as h, isObject as i, defined as j, isNullOrUndef as k, listenArrayEvents as l, toPercentage as m, toDimension as n, formatNumber as o, _angleBetween as p, _getStartAndCountOfVisiblePoints as q, requestAnimFrame as r, sign as s, toRadians as t, unlistenArrayEvents as u, valueOrDefault as v, _scaleRangesChanged as w, isNumber as x, _parseObjectDataRadialScale as y, log10 as z }; diff --git a/static/js/control_panel.js b/static/js/control_panel.js index daf2250a..9c85bc25 100644 --- a/static/js/control_panel.js +++ b/static/js/control_panel.js @@ -242,6 +242,7 @@ function check_state() { update_recipe_mode(); } else if((!cpRecipeMode) && (cpMode != cpLastMode)) { update_mode(); + update_pwm(); // fix bug where PWM Fan control was not showing up in hold mode }; if(splus_state != last_splus_state) { update_splus(); @@ -284,21 +285,37 @@ function cpRecipeUnpause() { update_recipe_pause(); }; +function cpStartupCheck(enable) { + // Check if user has bypassed startup_enable + if (enable == 'False') { + cpStartup(); + } else { + $('#startupModal').modal('show'); + }; +}; + +function cpStartup() { + $('#startupModal').modal('hide'); + var postdata = { + 'updated' : true, + 'mode' : 'Startup' + }; + console.log('Requesting Startup.'); + api_post(postdata); +}; + // Main Loop $(document).ready(function(){ check_current(); // Setup Button Listeners - $("#startup_btn").click(function(){ - var postdata = { - 'updated' : true, - 'mode' : 'Startup' - }; - console.log('Requesting Startup.'); - api_post(postdata); + + $('#startupModal').on('shown.bs.modal', function (event) { + $('#startupSlider').val(0); }); + $("#monitor_btn").click(function(){ var postdata = { 'updated' : true, diff --git a/static/js/dash_basic.js b/static/js/dash_basic.js index 5f54cdaa..c73b0cc0 100644 --- a/static/js/dash_basic.js +++ b/static/js/dash_basic.js @@ -15,6 +15,7 @@ var last_igniter_status = null; var last_pmode_status = null; var last_lid_open_status = false; var display_mode = null; +var dashDataStruct = {}; // Update temperatures on probe status cards function updateProbeCards() { @@ -60,12 +61,14 @@ function updateProbeCards() { if ((current.notify_data[item].target != notify_data[item].target) || (current.notify_data[item].req != notify_data[item].req) || (current.notify_data[item].shutdown != notify_data[item].shutdown) || - (current.notify_data[item].keep_warm != notify_data[item].keep_warm) ) { + (current.notify_data[item].keep_warm != notify_data[item].keep_warm) || + (current.notify_data[item].eta != notify_data[item].eta) + ) { console.log('Notification data change detected.') // Update Page updateNotificationCard(current.notify_data[item], current.status.mode); // Store Notify Data - notify_data = JSON.parse(JSON.stringify(current.notify_data)); // Copy data to notify_data variable + notify_data[item] = JSON.parse(JSON.stringify(current.notify_data[item])); // Copy data to notify_data variable }; }; }; @@ -238,6 +241,20 @@ function initTargets() { }; }; +function formatDuration(total_seconds) { + const hours = Math.floor(total_seconds / 3600); + const minutes = Math.floor((total_seconds % 3600) / 60); + const seconds = total_seconds % 60; + + if (hours) { + return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + } else if (minutes) { + return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + } else { + return `${seconds.toString().padStart(2, '0')}s`; + } +}; + // Update the notification information for the probe cards function updateNotificationCard(notify_info, mode) { const label = notify_info.label; @@ -245,17 +262,26 @@ function updateNotificationCard(notify_info, mode) { const shutdown = notify_info.shutdown; const keep_warm = notify_info.keep_warm; const target = notify_info.target; - console.log('Updating: ' + label + ' Mode: ' + mode); + var eta = ''; + if (notify_info.eta != null) { + eta = formatDuration(notify_info.eta); + }; + console.log('Updating: ' + label + ' ETA: ' + eta); // TODO: Update the page item with new data const notify_btn_id = label + "_notify_btn"; + const eta_btn_id = label + "_eta_btn"; + if(req) { - console.log('Turning on this notification: ' + notify_btn_id); + console.log('Updating this notification: ' + notify_btn_id); document.getElementById(notify_btn_id).innerHTML = '  ' + target + '°' + units; + document.getElementById(eta_btn_id).innerHTML = '  ' + eta; document.getElementById(notify_btn_id).className = 'btn btn-sm btn-primary'; + $('#'+eta_btn_id).show(); } else { console.log('Turning off this notification: ' + notify_btn_id); document.getElementById(notify_btn_id).innerHTML = ''; document.getElementById(notify_btn_id).className = 'btn btn-sm btn-outline-primary'; + $('#'+eta_btn_id).hide(); }; }; @@ -432,6 +458,75 @@ function setPmode(pmode) { }); }; +// Show the Dashboard Settings Modal/Dialog when clicked +function dashSettings() { + $("#dashSettingsModal").modal('show'); + //dashData(); +}; + +// Get dashboard data structure +function dashGetData() { + req = $.ajax({ + url : '/api/settings', + type : 'GET', + success : function(settings){ + dashDataStruct = settings.settings.dashboard.dashboards.Basic; + //console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + //console.log('dashData Name='+dashDataStruct.name); + } + }); +}; + +// Set dashboard data structure +function dashSetData() { + var postdata = { + 'dashboard' : { + 'dashboards' : { + 'Basic' : dashDataStruct + } + } + }; + + $.ajax({ + url : '/api/settings', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (response) { + //console.log('dashSetData -> ' + response); + } + }); +}; + +function dashToggleVisible(cardID) { + if ($('#card_'+cardID).is(":hidden")) { + // change card to visible + $('#card_'+cardID).show(); + // update dash config icon + $('#visibleStatus_'+cardID).html(' '); + // save to settings + var index = dashDataStruct.custom.hidden_cards.indexOf(cardID); // Index of cardID + if (index !== -1) { + dashDataStruct.custom.hidden_cards.splice(index, 1); // If found, remove + }; + //console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + dashSetData(); + } else { + // change card to hidden + $('#card_'+cardID).hide(); + // update dash config icon + $('#visibleStatus_'+cardID).html(' '); + // save to settings + var index = dashDataStruct.custom.hidden_cards.indexOf(cardID); // Index of cardID + if (index == -1) { + dashDataStruct.custom.hidden_cards.push(cardID); // If not found, add + }; + //console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + dashSetData(); + }; +} + // Main $(document).ready(function(){ // Setup Listeners @@ -440,6 +535,9 @@ $(document).ready(function(){ location.reload(); }); + // Initialize Dashboard Data + dashGetData(); + // Current temperature(s) loop probe_loop = setInterval(updateProbeCards, 500); // Update every 500ms diff --git a/static/js/dash_default.js b/static/js/dash_default.js index f2d1beba..bb7ce54d 100644 --- a/static/js/dash_default.js +++ b/static/js/dash_default.js @@ -26,6 +26,7 @@ var last_igniter_status = null; var last_pmode_status = null; var last_lid_open_status = false; var display_mode = null; +var dashDataStruct = {}; // Credits to https://github.com/naikus for SVG-Gauge (https://github.com/naikus/svg-gauge) MIT License Copyright (c) 2016 Aniket Naik var Gauge = window.Gauge; @@ -120,12 +121,14 @@ function updateProbeCards() { if ((current.notify_data[item].target != notify_data[item].target) || (current.notify_data[item].req != notify_data[item].req) || (current.notify_data[item].shutdown != notify_data[item].shutdown) || - (current.notify_data[item].keep_warm != notify_data[item].keep_warm) ) { + (current.notify_data[item].keep_warm != notify_data[item].keep_warm) || + (current.notify_data[item].eta != notify_data[item].eta) + ) { console.log('Notification data change detected.') // Update Page updateNotificationCard(current.notify_data[item], current.status.mode); // Store Notify Data - notify_data = JSON.parse(JSON.stringify(current.notify_data)); // Copy data to notify_data variable + notify_data[item] = JSON.parse(JSON.stringify(current.notify_data[item])); // Copy data to notify_data variable }; }; }; @@ -270,6 +273,20 @@ function updateProbeCards() { $('#lid_open_label').html('Lid Open Detected: PID Paused ' + countdown + 's'); }; + // Update Elapsed Time + if (current.status.startup_timestamp != 0) { + var time_now = new Date().getTime(); + time_now = Math.floor(time_now / 1000); + //console.log('Time Now Adjusted: ' + time_now); + var time_elapsed = time_now - Math.floor(current.status.startup_timestamp); + var time_elapsed_string = formatDuration(time_elapsed); + $('#time_elapsed_string').html(time_elapsed_string); + document.getElementById('time_elapsed_string').className = 'text-primary'; + } else { + $('#time_elapsed_string').html('--'); + document.getElementById('time_elapsed_string').className = 'text-secondary'; + }; + //if (current.status.s_plus) { // document.getElementById('smokeplus_status').innerHTML = ''; //} else { @@ -298,6 +315,20 @@ function initTargets() { }; }; +function formatDuration(total_seconds) { + const hours = Math.floor(total_seconds / 3600); + const minutes = Math.floor((total_seconds % 3600) / 60); + const seconds = total_seconds % 60; + + if (hours) { + return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + } else if (minutes) { + return `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`; + } else { + return `${seconds.toString().padStart(2, '0')}s`; + } +}; + // Update the notification information for the probe cards function updateNotificationCard(notify_info, mode) { const label = notify_info.label; @@ -305,17 +336,26 @@ function updateNotificationCard(notify_info, mode) { const shutdown = notify_info.shutdown; const keep_warm = notify_info.keep_warm; const target = notify_info.target; - console.log('Updating: ' + label + ' Mode: ' + mode); + var eta = ''; + if (notify_info.eta != null) { + eta = formatDuration(notify_info.eta); + }; + console.log('Updating: ' + label + ' ETA: ' + eta); // TODO: Update the page item with new data const notify_btn_id = label + "_notify_btn"; + const eta_btn_id = label + "_eta_btn"; + if(req) { - console.log('Turning on this notification: ' + notify_btn_id); - document.getElementById(notify_btn_id).innerHTML = '  ' + target + '°' + units; + console.log('Updating this notification: ' + notify_btn_id); + document.getElementById(notify_btn_id).innerHTML = '  ' + target + '°' + units; + document.getElementById(eta_btn_id).innerHTML = '  ' + eta; document.getElementById(notify_btn_id).className = 'btn btn-sm btn-primary'; + $('#'+eta_btn_id).show(); } else { console.log('Turning off this notification: ' + notify_btn_id); document.getElementById(notify_btn_id).innerHTML = ''; document.getElementById(notify_btn_id).className = 'btn btn-sm btn-outline-primary'; + $('#'+eta_btn_id).hide(); }; }; @@ -492,6 +532,75 @@ function setPmode(pmode) { }); }; +// Show the Dashboard Settings Modal/Dialog when clicked +function dashSettings() { + $("#dashSettingsModal").modal('show'); + //dashData(); +}; + +// Get dashboard data structure +function dashGetData() { + req = $.ajax({ + url : '/api/settings', + type : 'GET', + success : function(settings){ + dashDataStruct = settings.settings.dashboard.dashboards.Default; + //console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + //console.log('dashData Name='+dashDataStruct.name); + } + }); +}; + +// Set dashboard data structure +function dashSetData() { + var postdata = { + 'dashboard' : { + 'dashboards' : { + 'Default' : dashDataStruct + } + } + }; + + $.ajax({ + url : '/api/settings', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (response) { + //console.log('dashSetData -> ' + response); + } + }); +}; + +function dashToggleVisible(cardID) { + if ($('#card_'+cardID).is(":hidden")) { + // change card to visible + $('#card_'+cardID).show(); + // update dash config icon + $('#visibleStatus_'+cardID).html(' '); + // save to settings + var index = dashDataStruct.custom.hidden_cards.indexOf(cardID); // Index of cardID + if (index !== -1) { + dashDataStruct.custom.hidden_cards.splice(index, 1); // If found, remove + }; + console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + dashSetData(); + } else { + // change card to hidden + $('#card_'+cardID).hide(); + // update dash config icon + $('#visibleStatus_'+cardID).html(' '); + // save to settings + var index = dashDataStruct.custom.hidden_cards.indexOf(cardID); // Index of cardID + if (index == -1) { + dashDataStruct.custom.hidden_cards.push(cardID); // If not found, add + }; + console.log('dashData Hidden='+dashDataStruct.custom.hidden_cards); + dashSetData(); + }; +} + // Main $(document).ready(function(){ // Setup Listeners @@ -500,6 +609,9 @@ $(document).ready(function(){ location.reload(); }); + // Initialize Dashboard Data + dashGetData(); + // Initialize Probe Cards initProbeCards(); diff --git a/static/js/hammerjs-2.0.8.min.js b/static/js/hammer.min.js similarity index 99% rename from static/js/hammerjs-2.0.8.min.js rename to static/js/hammer.min.js index 34a8c86f..edadee15 100644 --- a/static/js/hammerjs-2.0.8.min.js +++ b/static/js/hammer.min.js @@ -1,7 +1,7 @@ -/*! Hammer.JS - v2.0.7 - 2016-04-22 +/*! Hammer.JS - v2.0.8 - 2016-04-23 * http://hammerjs.github.io/ * * Copyright (c) 2016 Jorik Tangelder; * Licensed under the MIT license */ -!function(a,b,c,d){"use strict";function e(a,b,c){return setTimeout(j(a,c),b)}function f(a,b,c){return Array.isArray(a)?(g(a,c[b],c),!0):!1}function g(a,b,c){var e;if(a)if(a.forEach)a.forEach(b,c);else if(a.length!==d)for(e=0;e\s*\(/gm,"{anonymous}()@"):"Unknown Stack Trace",f=a.console&&(a.console.warn||a.console.log);return f&&f.call(a.console,e,d),b.apply(this,arguments)}}function i(a,b,c){var d,e=b.prototype;d=a.prototype=Object.create(e),d.constructor=a,d._super=e,c&&la(d,c)}function j(a,b){return function(){return a.apply(b,arguments)}}function k(a,b){return typeof a==oa?a.apply(b?b[0]||d:d,b):a}function l(a,b){return a===d?b:a}function m(a,b,c){g(q(b),function(b){a.addEventListener(b,c,!1)})}function n(a,b,c){g(q(b),function(b){a.removeEventListener(b,c,!1)})}function o(a,b){for(;a;){if(a==b)return!0;a=a.parentNode}return!1}function p(a,b){return a.indexOf(b)>-1}function q(a){return a.trim().split(/\s+/g)}function r(a,b,c){if(a.indexOf&&!c)return a.indexOf(b);for(var d=0;dc[b]}):d.sort()),d}function u(a,b){for(var c,e,f=b[0].toUpperCase()+b.slice(1),g=0;g1&&!c.firstMultiple?c.firstMultiple=D(b):1===e&&(c.firstMultiple=!1);var f=c.firstInput,g=c.firstMultiple,h=g?g.center:f.center,i=b.center=E(d);b.timeStamp=ra(),b.deltaTime=b.timeStamp-f.timeStamp,b.angle=I(h,i),b.distance=H(h,i),B(c,b),b.offsetDirection=G(b.deltaX,b.deltaY);var j=F(b.deltaTime,b.deltaX,b.deltaY);b.overallVelocityX=j.x,b.overallVelocityY=j.y,b.overallVelocity=qa(j.x)>qa(j.y)?j.x:j.y,b.scale=g?K(g.pointers,d):1,b.rotation=g?J(g.pointers,d):0,b.maxPointers=c.prevInput?b.pointers.length>c.prevInput.maxPointers?b.pointers.length:c.prevInput.maxPointers:b.pointers.length,C(c,b);var k=a.element;o(b.srcEvent.target,k)&&(k=b.srcEvent.target),b.target=k}function B(a,b){var c=b.center,d=a.offsetDelta||{},e=a.prevDelta||{},f=a.prevInput||{};b.eventType!==Ea&&f.eventType!==Ga||(e=a.prevDelta={x:f.deltaX||0,y:f.deltaY||0},d=a.offsetDelta={x:c.x,y:c.y}),b.deltaX=e.x+(c.x-d.x),b.deltaY=e.y+(c.y-d.y)}function C(a,b){var c,e,f,g,h=a.lastInterval||b,i=b.timeStamp-h.timeStamp;if(b.eventType!=Ha&&(i>Da||h.velocity===d)){var j=b.deltaX-h.deltaX,k=b.deltaY-h.deltaY,l=F(i,j,k);e=l.x,f=l.y,c=qa(l.x)>qa(l.y)?l.x:l.y,g=G(j,k),a.lastInterval=b}else c=h.velocity,e=h.velocityX,f=h.velocityY,g=h.direction;b.velocity=c,b.velocityX=e,b.velocityY=f,b.direction=g}function D(a){for(var b=[],c=0;ce;)c+=a[e].clientX,d+=a[e].clientY,e++;return{x:pa(c/b),y:pa(d/b)}}function F(a,b,c){return{x:b/a||0,y:c/a||0}}function G(a,b){return a===b?Ia:qa(a)>=qa(b)?0>a?Ja:Ka:0>b?La:Ma}function H(a,b,c){c||(c=Qa);var d=b[c[0]]-a[c[0]],e=b[c[1]]-a[c[1]];return Math.sqrt(d*d+e*e)}function I(a,b,c){c||(c=Qa);var d=b[c[0]]-a[c[0]],e=b[c[1]]-a[c[1]];return 180*Math.atan2(e,d)/Math.PI}function J(a,b){return I(b[1],b[0],Ra)+I(a[1],a[0],Ra)}function K(a,b){return H(b[0],b[1],Ra)/H(a[0],a[1],Ra)}function L(){this.evEl=Ta,this.evWin=Ua,this.pressed=!1,x.apply(this,arguments)}function M(){this.evEl=Xa,this.evWin=Ya,x.apply(this,arguments),this.store=this.manager.session.pointerEvents=[]}function N(){this.evTarget=$a,this.evWin=_a,this.started=!1,x.apply(this,arguments)}function O(a,b){var c=s(a.touches),d=s(a.changedTouches);return b&(Ga|Ha)&&(c=t(c.concat(d),"identifier",!0)),[c,d]}function P(){this.evTarget=bb,this.targetIds={},x.apply(this,arguments)}function Q(a,b){var c=s(a.touches),d=this.targetIds;if(b&(Ea|Fa)&&1===c.length)return d[c[0].identifier]=!0,[c,c];var e,f,g=s(a.changedTouches),h=[],i=this.target;if(f=c.filter(function(a){return o(a.target,i)}),b===Ea)for(e=0;e-1&&d.splice(a,1)};setTimeout(e,cb)}}function U(a){for(var b=a.srcEvent.clientX,c=a.srcEvent.clientY,d=0;d=f&&db>=g)return!0}return!1}function V(a,b){this.manager=a,this.set(b)}function W(a){if(p(a,jb))return jb;var b=p(a,kb),c=p(a,lb);return b&&c?jb:b||c?b?kb:lb:p(a,ib)?ib:hb}function X(){if(!fb)return!1;var b={},c=a.CSS&&a.CSS.supports;return["auto","manipulation","pan-y","pan-x","pan-x pan-y","none"].forEach(function(d){b[d]=c?a.CSS.supports("touch-action",d):!0}),b}function Y(a){this.options=la({},this.defaults,a||{}),this.id=v(),this.manager=null,this.options.enable=l(this.options.enable,!0),this.state=nb,this.simultaneous={},this.requireFail=[]}function Z(a){return a&sb?"cancel":a&qb?"end":a&pb?"move":a&ob?"start":""}function $(a){return a==Ma?"down":a==La?"up":a==Ja?"left":a==Ka?"right":""}function _(a,b){var c=b.manager;return c?c.get(a):a}function aa(){Y.apply(this,arguments)}function ba(){aa.apply(this,arguments),this.pX=null,this.pY=null}function ca(){aa.apply(this,arguments)}function da(){Y.apply(this,arguments),this._timer=null,this._input=null}function ea(){aa.apply(this,arguments)}function fa(){aa.apply(this,arguments)}function ga(){Y.apply(this,arguments),this.pTime=!1,this.pCenter=!1,this._timer=null,this._input=null,this.count=0}function ha(a,b){return b=b||{},b.recognizers=l(b.recognizers,ha.defaults.preset),new ia(a,b)}function ia(a,b){this.options=la({},ha.defaults,b||{}),this.options.inputTarget=this.options.inputTarget||a,this.handlers={},this.session={},this.recognizers=[],this.oldCssProps={},this.element=a,this.input=y(this),this.touchAction=new V(this,this.options.touchAction),ja(this,!0),g(this.options.recognizers,function(a){var b=this.add(new a[0](a[1]));a[2]&&b.recognizeWith(a[2]),a[3]&&b.requireFailure(a[3])},this)}function ja(a,b){var c=a.element;if(c.style){var d;g(a.options.cssProps,function(e,f){d=u(c.style,f),b?(a.oldCssProps[d]=c.style[d],c.style[d]=e):c.style[d]=a.oldCssProps[d]||""}),b||(a.oldCssProps={})}}function ka(a,c){var d=b.createEvent("Event");d.initEvent(a,!0,!0),d.gesture=c,c.target.dispatchEvent(d)}var la,ma=["","webkit","Moz","MS","ms","o"],na=b.createElement("div"),oa="function",pa=Math.round,qa=Math.abs,ra=Date.now;la="function"!=typeof Object.assign?function(a){if(a===d||null===a)throw new TypeError("Cannot convert undefined or null to object");for(var b=Object(a),c=1;ch&&(b.push(a),h=b.length-1):e&(Ga|Ha)&&(c=!0),0>h||(b[h]=a,this.callback(this.manager,e,{pointers:b,changedPointers:[a],pointerType:f,srcEvent:a}),c&&b.splice(h,1))}});var Za={touchstart:Ea,touchmove:Fa,touchend:Ga,touchcancel:Ha},$a="touchstart",_a="touchstart touchmove touchend touchcancel";i(N,x,{handler:function(a){var b=Za[a.type];if(b===Ea&&(this.started=!0),this.started){var c=O.call(this,a,b);b&(Ga|Ha)&&c[0].length-c[1].length===0&&(this.started=!1),this.callback(this.manager,b,{pointers:c[0],changedPointers:c[1],pointerType:za,srcEvent:a})}}});var ab={touchstart:Ea,touchmove:Fa,touchend:Ga,touchcancel:Ha},bb="touchstart touchmove touchend touchcancel";i(P,x,{handler:function(a){var b=ab[a.type],c=Q.call(this,a,b);c&&this.callback(this.manager,b,{pointers:c[0],changedPointers:c[1],pointerType:za,srcEvent:a})}});var cb=2500,db=25;i(R,x,{handler:function(a,b,c){var d=c.pointerType==za,e=c.pointerType==Ba;if(!(e&&c.sourceCapabilities&&c.sourceCapabilities.firesTouchEvents)){if(d)S.call(this,b,c);else if(e&&U.call(this,c))return;this.callback(a,b,c)}},destroy:function(){this.touch.destroy(),this.mouse.destroy()}});var eb=u(na.style,"touchAction"),fb=eb!==d,gb="compute",hb="auto",ib="manipulation",jb="none",kb="pan-x",lb="pan-y",mb=X();V.prototype={set:function(a){a==gb&&(a=this.compute()),fb&&this.manager.element.style&&mb[a]&&(this.manager.element.style[eb]=a),this.actions=a.toLowerCase().trim()},update:function(){this.set(this.manager.options.touchAction)},compute:function(){var a=[];return g(this.manager.recognizers,function(b){k(b.options.enable,[b])&&(a=a.concat(b.getTouchAction()))}),W(a.join(" "))},preventDefaults:function(a){var b=a.srcEvent,c=a.offsetDirection;if(this.manager.session.prevented)return void b.preventDefault();var d=this.actions,e=p(d,jb)&&!mb[jb],f=p(d,lb)&&!mb[lb],g=p(d,kb)&&!mb[kb];if(e){var h=1===a.pointers.length,i=a.distance<2,j=a.deltaTime<250;if(h&&i&&j)return}return g&&f?void 0:e||f&&c&Na||g&&c&Oa?this.preventSrc(b):void 0},preventSrc:function(a){this.manager.session.prevented=!0,a.preventDefault()}};var nb=1,ob=2,pb=4,qb=8,rb=qb,sb=16,tb=32;Y.prototype={defaults:{},set:function(a){return la(this.options,a),this.manager&&this.manager.touchAction.update(),this},recognizeWith:function(a){if(f(a,"recognizeWith",this))return this;var b=this.simultaneous;return a=_(a,this),b[a.id]||(b[a.id]=a,a.recognizeWith(this)),this},dropRecognizeWith:function(a){return f(a,"dropRecognizeWith",this)?this:(a=_(a,this),delete this.simultaneous[a.id],this)},requireFailure:function(a){if(f(a,"requireFailure",this))return this;var b=this.requireFail;return a=_(a,this),-1===r(b,a)&&(b.push(a),a.requireFailure(this)),this},dropRequireFailure:function(a){if(f(a,"dropRequireFailure",this))return this;a=_(a,this);var b=r(this.requireFail,a);return b>-1&&this.requireFail.splice(b,1),this},hasRequireFailures:function(){return this.requireFail.length>0},canRecognizeWith:function(a){return!!this.simultaneous[a.id]},emit:function(a){function b(b){c.manager.emit(b,a)}var c=this,d=this.state;qb>d&&b(c.options.event+Z(d)),b(c.options.event),a.additionalEvent&&b(a.additionalEvent),d>=qb&&b(c.options.event+Z(d))},tryEmit:function(a){return this.canEmit()?this.emit(a):void(this.state=tb)},canEmit:function(){for(var a=0;af?Ja:Ka,c=f!=this.pX,d=Math.abs(a.deltaX)):(e=0===g?Ia:0>g?La:Ma,c=g!=this.pY,d=Math.abs(a.deltaY))),a.direction=e,c&&d>b.threshold&&e&b.direction},attrTest:function(a){return aa.prototype.attrTest.call(this,a)&&(this.state&ob||!(this.state&ob)&&this.directionTest(a))},emit:function(a){this.pX=a.deltaX,this.pY=a.deltaY;var b=$(a.direction);b&&(a.additionalEvent=this.options.event+b),this._super.emit.call(this,a)}}),i(ca,aa,{defaults:{event:"pinch",threshold:0,pointers:2},getTouchAction:function(){return[jb]},attrTest:function(a){return this._super.attrTest.call(this,a)&&(Math.abs(a.scale-1)>this.options.threshold||this.state&ob)},emit:function(a){if(1!==a.scale){var b=a.scale<1?"in":"out";a.additionalEvent=this.options.event+b}this._super.emit.call(this,a)}}),i(da,Y,{defaults:{event:"press",pointers:1,time:251,threshold:9},getTouchAction:function(){return[hb]},process:function(a){var b=this.options,c=a.pointers.length===b.pointers,d=a.distanceb.time;if(this._input=a,!d||!c||a.eventType&(Ga|Ha)&&!f)this.reset();else if(a.eventType&Ea)this.reset(),this._timer=e(function(){this.state=rb,this.tryEmit()},b.time,this);else if(a.eventType&Ga)return rb;return tb},reset:function(){clearTimeout(this._timer)},emit:function(a){this.state===rb&&(a&&a.eventType&Ga?this.manager.emit(this.options.event+"up",a):(this._input.timeStamp=ra(),this.manager.emit(this.options.event,this._input)))}}),i(ea,aa,{defaults:{event:"rotate",threshold:0,pointers:2},getTouchAction:function(){return[jb]},attrTest:function(a){return this._super.attrTest.call(this,a)&&(Math.abs(a.rotation)>this.options.threshold||this.state&ob)}}),i(fa,aa,{defaults:{event:"swipe",threshold:10,velocity:.3,direction:Na|Oa,pointers:1},getTouchAction:function(){return ba.prototype.getTouchAction.call(this)},attrTest:function(a){var b,c=this.options.direction;return c&(Na|Oa)?b=a.overallVelocity:c&Na?b=a.overallVelocityX:c&Oa&&(b=a.overallVelocityY),this._super.attrTest.call(this,a)&&c&a.offsetDirection&&a.distance>this.options.threshold&&a.maxPointers==this.options.pointers&&qa(b)>this.options.velocity&&a.eventType&Ga},emit:function(a){var b=$(a.offsetDirection);b&&this.manager.emit(this.options.event+b,a),this.manager.emit(this.options.event,a)}}),i(ga,Y,{defaults:{event:"tap",pointers:1,taps:1,interval:300,time:250,threshold:9,posThreshold:10},getTouchAction:function(){return[ib]},process:function(a){var b=this.options,c=a.pointers.length===b.pointers,d=a.distance\s*\(/gm,"{anonymous}()@"):"Unknown Stack Trace",f=a.console&&(a.console.warn||a.console.log);return f&&f.call(a.console,e,d),b.apply(this,arguments)}}function i(a,b,c){var d,e=b.prototype;d=a.prototype=Object.create(e),d.constructor=a,d._super=e,c&&la(d,c)}function j(a,b){return function(){return a.apply(b,arguments)}}function k(a,b){return typeof a==oa?a.apply(b?b[0]||d:d,b):a}function l(a,b){return a===d?b:a}function m(a,b,c){g(q(b),function(b){a.addEventListener(b,c,!1)})}function n(a,b,c){g(q(b),function(b){a.removeEventListener(b,c,!1)})}function o(a,b){for(;a;){if(a==b)return!0;a=a.parentNode}return!1}function p(a,b){return a.indexOf(b)>-1}function q(a){return a.trim().split(/\s+/g)}function r(a,b,c){if(a.indexOf&&!c)return a.indexOf(b);for(var d=0;dc[b]}):d.sort()),d}function u(a,b){for(var c,e,f=b[0].toUpperCase()+b.slice(1),g=0;g1&&!c.firstMultiple?c.firstMultiple=D(b):1===e&&(c.firstMultiple=!1);var f=c.firstInput,g=c.firstMultiple,h=g?g.center:f.center,i=b.center=E(d);b.timeStamp=ra(),b.deltaTime=b.timeStamp-f.timeStamp,b.angle=I(h,i),b.distance=H(h,i),B(c,b),b.offsetDirection=G(b.deltaX,b.deltaY);var j=F(b.deltaTime,b.deltaX,b.deltaY);b.overallVelocityX=j.x,b.overallVelocityY=j.y,b.overallVelocity=qa(j.x)>qa(j.y)?j.x:j.y,b.scale=g?K(g.pointers,d):1,b.rotation=g?J(g.pointers,d):0,b.maxPointers=c.prevInput?b.pointers.length>c.prevInput.maxPointers?b.pointers.length:c.prevInput.maxPointers:b.pointers.length,C(c,b);var k=a.element;o(b.srcEvent.target,k)&&(k=b.srcEvent.target),b.target=k}function B(a,b){var c=b.center,d=a.offsetDelta||{},e=a.prevDelta||{},f=a.prevInput||{};b.eventType!==Ea&&f.eventType!==Ga||(e=a.prevDelta={x:f.deltaX||0,y:f.deltaY||0},d=a.offsetDelta={x:c.x,y:c.y}),b.deltaX=e.x+(c.x-d.x),b.deltaY=e.y+(c.y-d.y)}function C(a,b){var c,e,f,g,h=a.lastInterval||b,i=b.timeStamp-h.timeStamp;if(b.eventType!=Ha&&(i>Da||h.velocity===d)){var j=b.deltaX-h.deltaX,k=b.deltaY-h.deltaY,l=F(i,j,k);e=l.x,f=l.y,c=qa(l.x)>qa(l.y)?l.x:l.y,g=G(j,k),a.lastInterval=b}else c=h.velocity,e=h.velocityX,f=h.velocityY,g=h.direction;b.velocity=c,b.velocityX=e,b.velocityY=f,b.direction=g}function D(a){for(var b=[],c=0;ce;)c+=a[e].clientX,d+=a[e].clientY,e++;return{x:pa(c/b),y:pa(d/b)}}function F(a,b,c){return{x:b/a||0,y:c/a||0}}function G(a,b){return a===b?Ia:qa(a)>=qa(b)?0>a?Ja:Ka:0>b?La:Ma}function H(a,b,c){c||(c=Qa);var d=b[c[0]]-a[c[0]],e=b[c[1]]-a[c[1]];return Math.sqrt(d*d+e*e)}function I(a,b,c){c||(c=Qa);var d=b[c[0]]-a[c[0]],e=b[c[1]]-a[c[1]];return 180*Math.atan2(e,d)/Math.PI}function J(a,b){return I(b[1],b[0],Ra)+I(a[1],a[0],Ra)}function K(a,b){return H(b[0],b[1],Ra)/H(a[0],a[1],Ra)}function L(){this.evEl=Ta,this.evWin=Ua,this.pressed=!1,x.apply(this,arguments)}function M(){this.evEl=Xa,this.evWin=Ya,x.apply(this,arguments),this.store=this.manager.session.pointerEvents=[]}function N(){this.evTarget=$a,this.evWin=_a,this.started=!1,x.apply(this,arguments)}function O(a,b){var c=s(a.touches),d=s(a.changedTouches);return b&(Ga|Ha)&&(c=t(c.concat(d),"identifier",!0)),[c,d]}function P(){this.evTarget=bb,this.targetIds={},x.apply(this,arguments)}function Q(a,b){var c=s(a.touches),d=this.targetIds;if(b&(Ea|Fa)&&1===c.length)return d[c[0].identifier]=!0,[c,c];var e,f,g=s(a.changedTouches),h=[],i=this.target;if(f=c.filter(function(a){return o(a.target,i)}),b===Ea)for(e=0;e-1&&d.splice(a,1)};setTimeout(e,cb)}}function U(a){for(var b=a.srcEvent.clientX,c=a.srcEvent.clientY,d=0;d=f&&db>=g)return!0}return!1}function V(a,b){this.manager=a,this.set(b)}function W(a){if(p(a,jb))return jb;var b=p(a,kb),c=p(a,lb);return b&&c?jb:b||c?b?kb:lb:p(a,ib)?ib:hb}function X(){if(!fb)return!1;var b={},c=a.CSS&&a.CSS.supports;return["auto","manipulation","pan-y","pan-x","pan-x pan-y","none"].forEach(function(d){b[d]=c?a.CSS.supports("touch-action",d):!0}),b}function Y(a){this.options=la({},this.defaults,a||{}),this.id=v(),this.manager=null,this.options.enable=l(this.options.enable,!0),this.state=nb,this.simultaneous={},this.requireFail=[]}function Z(a){return a&sb?"cancel":a&qb?"end":a&pb?"move":a&ob?"start":""}function $(a){return a==Ma?"down":a==La?"up":a==Ja?"left":a==Ka?"right":""}function _(a,b){var c=b.manager;return c?c.get(a):a}function aa(){Y.apply(this,arguments)}function ba(){aa.apply(this,arguments),this.pX=null,this.pY=null}function ca(){aa.apply(this,arguments)}function da(){Y.apply(this,arguments),this._timer=null,this._input=null}function ea(){aa.apply(this,arguments)}function fa(){aa.apply(this,arguments)}function ga(){Y.apply(this,arguments),this.pTime=!1,this.pCenter=!1,this._timer=null,this._input=null,this.count=0}function ha(a,b){return b=b||{},b.recognizers=l(b.recognizers,ha.defaults.preset),new ia(a,b)}function ia(a,b){this.options=la({},ha.defaults,b||{}),this.options.inputTarget=this.options.inputTarget||a,this.handlers={},this.session={},this.recognizers=[],this.oldCssProps={},this.element=a,this.input=y(this),this.touchAction=new V(this,this.options.touchAction),ja(this,!0),g(this.options.recognizers,function(a){var b=this.add(new a[0](a[1]));a[2]&&b.recognizeWith(a[2]),a[3]&&b.requireFailure(a[3])},this)}function ja(a,b){var c=a.element;if(c.style){var d;g(a.options.cssProps,function(e,f){d=u(c.style,f),b?(a.oldCssProps[d]=c.style[d],c.style[d]=e):c.style[d]=a.oldCssProps[d]||""}),b||(a.oldCssProps={})}}function ka(a,c){var d=b.createEvent("Event");d.initEvent(a,!0,!0),d.gesture=c,c.target.dispatchEvent(d)}var la,ma=["","webkit","Moz","MS","ms","o"],na=b.createElement("div"),oa="function",pa=Math.round,qa=Math.abs,ra=Date.now;la="function"!=typeof Object.assign?function(a){if(a===d||null===a)throw new TypeError("Cannot convert undefined or null to object");for(var b=Object(a),c=1;ch&&(b.push(a),h=b.length-1):e&(Ga|Ha)&&(c=!0),0>h||(b[h]=a,this.callback(this.manager,e,{pointers:b,changedPointers:[a],pointerType:f,srcEvent:a}),c&&b.splice(h,1))}});var Za={touchstart:Ea,touchmove:Fa,touchend:Ga,touchcancel:Ha},$a="touchstart",_a="touchstart touchmove touchend touchcancel";i(N,x,{handler:function(a){var b=Za[a.type];if(b===Ea&&(this.started=!0),this.started){var c=O.call(this,a,b);b&(Ga|Ha)&&c[0].length-c[1].length===0&&(this.started=!1),this.callback(this.manager,b,{pointers:c[0],changedPointers:c[1],pointerType:za,srcEvent:a})}}});var ab={touchstart:Ea,touchmove:Fa,touchend:Ga,touchcancel:Ha},bb="touchstart touchmove touchend touchcancel";i(P,x,{handler:function(a){var b=ab[a.type],c=Q.call(this,a,b);c&&this.callback(this.manager,b,{pointers:c[0],changedPointers:c[1],pointerType:za,srcEvent:a})}});var cb=2500,db=25;i(R,x,{handler:function(a,b,c){var d=c.pointerType==za,e=c.pointerType==Ba;if(!(e&&c.sourceCapabilities&&c.sourceCapabilities.firesTouchEvents)){if(d)S.call(this,b,c);else if(e&&U.call(this,c))return;this.callback(a,b,c)}},destroy:function(){this.touch.destroy(),this.mouse.destroy()}});var eb=u(na.style,"touchAction"),fb=eb!==d,gb="compute",hb="auto",ib="manipulation",jb="none",kb="pan-x",lb="pan-y",mb=X();V.prototype={set:function(a){a==gb&&(a=this.compute()),fb&&this.manager.element.style&&mb[a]&&(this.manager.element.style[eb]=a),this.actions=a.toLowerCase().trim()},update:function(){this.set(this.manager.options.touchAction)},compute:function(){var a=[];return g(this.manager.recognizers,function(b){k(b.options.enable,[b])&&(a=a.concat(b.getTouchAction()))}),W(a.join(" "))},preventDefaults:function(a){var b=a.srcEvent,c=a.offsetDirection;if(this.manager.session.prevented)return void b.preventDefault();var d=this.actions,e=p(d,jb)&&!mb[jb],f=p(d,lb)&&!mb[lb],g=p(d,kb)&&!mb[kb];if(e){var h=1===a.pointers.length,i=a.distance<2,j=a.deltaTime<250;if(h&&i&&j)return}return g&&f?void 0:e||f&&c&Na||g&&c&Oa?this.preventSrc(b):void 0},preventSrc:function(a){this.manager.session.prevented=!0,a.preventDefault()}};var nb=1,ob=2,pb=4,qb=8,rb=qb,sb=16,tb=32;Y.prototype={defaults:{},set:function(a){return la(this.options,a),this.manager&&this.manager.touchAction.update(),this},recognizeWith:function(a){if(f(a,"recognizeWith",this))return this;var b=this.simultaneous;return a=_(a,this),b[a.id]||(b[a.id]=a,a.recognizeWith(this)),this},dropRecognizeWith:function(a){return f(a,"dropRecognizeWith",this)?this:(a=_(a,this),delete this.simultaneous[a.id],this)},requireFailure:function(a){if(f(a,"requireFailure",this))return this;var b=this.requireFail;return a=_(a,this),-1===r(b,a)&&(b.push(a),a.requireFailure(this)),this},dropRequireFailure:function(a){if(f(a,"dropRequireFailure",this))return this;a=_(a,this);var b=r(this.requireFail,a);return b>-1&&this.requireFail.splice(b,1),this},hasRequireFailures:function(){return this.requireFail.length>0},canRecognizeWith:function(a){return!!this.simultaneous[a.id]},emit:function(a){function b(b){c.manager.emit(b,a)}var c=this,d=this.state;qb>d&&b(c.options.event+Z(d)),b(c.options.event),a.additionalEvent&&b(a.additionalEvent),d>=qb&&b(c.options.event+Z(d))},tryEmit:function(a){return this.canEmit()?this.emit(a):void(this.state=tb)},canEmit:function(){for(var a=0;af?Ja:Ka,c=f!=this.pX,d=Math.abs(a.deltaX)):(e=0===g?Ia:0>g?La:Ma,c=g!=this.pY,d=Math.abs(a.deltaY))),a.direction=e,c&&d>b.threshold&&e&b.direction},attrTest:function(a){return aa.prototype.attrTest.call(this,a)&&(this.state&ob||!(this.state&ob)&&this.directionTest(a))},emit:function(a){this.pX=a.deltaX,this.pY=a.deltaY;var b=$(a.direction);b&&(a.additionalEvent=this.options.event+b),this._super.emit.call(this,a)}}),i(ca,aa,{defaults:{event:"pinch",threshold:0,pointers:2},getTouchAction:function(){return[jb]},attrTest:function(a){return this._super.attrTest.call(this,a)&&(Math.abs(a.scale-1)>this.options.threshold||this.state&ob)},emit:function(a){if(1!==a.scale){var b=a.scale<1?"in":"out";a.additionalEvent=this.options.event+b}this._super.emit.call(this,a)}}),i(da,Y,{defaults:{event:"press",pointers:1,time:251,threshold:9},getTouchAction:function(){return[hb]},process:function(a){var b=this.options,c=a.pointers.length===b.pointers,d=a.distanceb.time;if(this._input=a,!d||!c||a.eventType&(Ga|Ha)&&!f)this.reset();else if(a.eventType&Ea)this.reset(),this._timer=e(function(){this.state=rb,this.tryEmit()},b.time,this);else if(a.eventType&Ga)return rb;return tb},reset:function(){clearTimeout(this._timer)},emit:function(a){this.state===rb&&(a&&a.eventType&Ga?this.manager.emit(this.options.event+"up",a):(this._input.timeStamp=ra(),this.manager.emit(this.options.event,this._input)))}}),i(ea,aa,{defaults:{event:"rotate",threshold:0,pointers:2},getTouchAction:function(){return[jb]},attrTest:function(a){return this._super.attrTest.call(this,a)&&(Math.abs(a.rotation)>this.options.threshold||this.state&ob)}}),i(fa,aa,{defaults:{event:"swipe",threshold:10,velocity:.3,direction:Na|Oa,pointers:1},getTouchAction:function(){return ba.prototype.getTouchAction.call(this)},attrTest:function(a){var b,c=this.options.direction;return c&(Na|Oa)?b=a.overallVelocity:c&Na?b=a.overallVelocityX:c&Oa&&(b=a.overallVelocityY),this._super.attrTest.call(this,a)&&c&a.offsetDirection&&a.distance>this.options.threshold&&a.maxPointers==this.options.pointers&&qa(b)>this.options.velocity&&a.eventType&Ga},emit:function(a){var b=$(a.offsetDirection);b&&this.manager.emit(this.options.event+b,a),this.manager.emit(this.options.event,a)}}),i(ga,Y,{defaults:{event:"tap",pointers:1,taps:1,interval:300,time:250,threshold:9,posThreshold:10},getTouchAction:function(){return[ib]},process:function(a){var b=this.options,c=a.pointers.length===b.pointers,d=a.distance { $.get("/historyupdate/stream", function(data){ checkHashChange(data.ui_hash); checkModeChange(data.mode); if (chartReady) { - var dateNow = Date.now(); - // append the new label (time) to the label list - chart.data.labels.push(dateNow); - // append the new data array to the existing chart data - //chart.data.datasets[0].data.push(data.probe0_temp); + var timestamp = data.current.TS; + for (probe in data.current.P) { - chart.data.datasets[probe_mapper['probes'][probe]].data.push(data.current.P[probe]); - chart.data.datasets[probe_mapper['primarysp'][probe]].data.push(data.current.PSP); + chart.data.datasets[probe_mapper['probes'][probe]].data.push({'x':timestamp, 'y':data.current.P[probe]}); + chart.data.datasets[probe_mapper['primarysp'][probe]].data.push({'x':timestamp, 'y':data.current.PSP}); }; for (probe in data.current.F) { - chart.data.datasets[probe_mapper['probes'][probe]].data.push(data.current.F[probe]); + chart.data.datasets[probe_mapper['probes'][probe]].data.push({'x':timestamp, 'y':data.current.F[probe]}); }; for (probe in data.current.NT) { - chart.data.datasets[probe_mapper['targets'][probe]].data.push(data.current.NT[probe]); + chart.data.datasets[probe_mapper['targets'][probe]].data.push({'x':timestamp, 'y':data.current.NT[probe]}); }; if (annotation_enabled == true) { @@ -119,6 +139,7 @@ var temperatureCharts = new Chart(document.getElementById('HistoryChart'), { } else { chart.options.plugins.annotation.annotations = {}; }; + }; }); } @@ -218,7 +239,24 @@ function refreshChartData(zoom) { // Update time label list temperatureCharts.data.labels = data.time_labels; // Update chart datasets - temperatureCharts.data.datasets = data.chart_data; + if (chartReady) { + // Loop through dataset and see if any specific datasets are hidden + var chartIndex = 0; + temperatureCharts.data.datasets.forEach(function (arrayItem) { + if (hiddenData[chartIndex] == true) { + //console.log(arrayItem.label + ' at position ' + chartIndex + ' is hidden.'); + data.chart_data[chartIndex]['hidden'] = true; + }; + chartIndex++; + }); + temperatureCharts.data.datasets = data.chart_data; + } else { + temperatureCharts.data.datasets = data.chart_data; + // Create hiddenData map + temperatureCharts.data.datasets.forEach(function () { + hiddenData.push(false); + }); + }; // Update annotations temperatureCharts.options.plugins.annotation.annotations = data.annotations; // Update Chart diff --git a/static/js/luxon.js b/static/js/luxon.js deleted file mode 100644 index eba034a7..00000000 --- a/static/js/luxon.js +++ /dev/null @@ -1,8493 +0,0 @@ -var luxon = (function (exports) { - 'use strict'; - - function _defineProperties(target, props) { - for (var i = 0; i < props.length; i++) { - var descriptor = props[i]; - descriptor.enumerable = descriptor.enumerable || false; - descriptor.configurable = true; - if ("value" in descriptor) descriptor.writable = true; - Object.defineProperty(target, descriptor.key, descriptor); - } - } - - function _createClass(Constructor, protoProps, staticProps) { - if (protoProps) _defineProperties(Constructor.prototype, protoProps); - if (staticProps) _defineProperties(Constructor, staticProps); - return Constructor; - } - - function _extends() { - _extends = Object.assign || function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i]; - - for (var key in source) { - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } - - return target; - }; - - return _extends.apply(this, arguments); - } - - function _inheritsLoose(subClass, superClass) { - subClass.prototype = Object.create(superClass.prototype); - subClass.prototype.constructor = subClass; - - _setPrototypeOf(subClass, superClass); - } - - function _getPrototypeOf(o) { - _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { - return o.__proto__ || Object.getPrototypeOf(o); - }; - return _getPrototypeOf(o); - } - - function _setPrototypeOf(o, p) { - _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { - o.__proto__ = p; - return o; - }; - - return _setPrototypeOf(o, p); - } - - function _isNativeReflectConstruct() { - if (typeof Reflect === "undefined" || !Reflect.construct) return false; - if (Reflect.construct.sham) return false; - if (typeof Proxy === "function") return true; - - try { - Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); - return true; - } catch (e) { - return false; - } - } - - function _construct(Parent, args, Class) { - if (_isNativeReflectConstruct()) { - _construct = Reflect.construct; - } else { - _construct = function _construct(Parent, args, Class) { - var a = [null]; - a.push.apply(a, args); - var Constructor = Function.bind.apply(Parent, a); - var instance = new Constructor(); - if (Class) _setPrototypeOf(instance, Class.prototype); - return instance; - }; - } - - return _construct.apply(null, arguments); - } - - function _isNativeFunction(fn) { - return Function.toString.call(fn).indexOf("[native code]") !== -1; - } - - function _wrapNativeSuper(Class) { - var _cache = typeof Map === "function" ? new Map() : undefined; - - _wrapNativeSuper = function _wrapNativeSuper(Class) { - if (Class === null || !_isNativeFunction(Class)) return Class; - - if (typeof Class !== "function") { - throw new TypeError("Super expression must either be null or a function"); - } - - if (typeof _cache !== "undefined") { - if (_cache.has(Class)) return _cache.get(Class); - - _cache.set(Class, Wrapper); - } - - function Wrapper() { - return _construct(Class, arguments, _getPrototypeOf(this).constructor); - } - - Wrapper.prototype = Object.create(Class.prototype, { - constructor: { - value: Wrapper, - enumerable: false, - writable: true, - configurable: true - } - }); - return _setPrototypeOf(Wrapper, Class); - }; - - return _wrapNativeSuper(Class); - } - - function _objectWithoutPropertiesLoose(source, excluded) { - if (source == null) return {}; - var target = {}; - var sourceKeys = Object.keys(source); - var key, i; - - for (i = 0; i < sourceKeys.length; i++) { - key = sourceKeys[i]; - if (excluded.indexOf(key) >= 0) continue; - target[key] = source[key]; - } - - return target; - } - - function _unsupportedIterableToArray(o, minLen) { - if (!o) return; - if (typeof o === "string") return _arrayLikeToArray(o, minLen); - var n = Object.prototype.toString.call(o).slice(8, -1); - if (n === "Object" && o.constructor) n = o.constructor.name; - if (n === "Map" || n === "Set") return Array.from(o); - if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); - } - - function _arrayLikeToArray(arr, len) { - if (len == null || len > arr.length) len = arr.length; - - for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; - - return arr2; - } - - function _createForOfIteratorHelperLoose(o, allowArrayLike) { - var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; - if (it) return (it = it.call(o)).next.bind(it); - - if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { - if (it) o = it; - var i = 0; - return function () { - if (i >= o.length) return { - done: true - }; - return { - done: false, - value: o[i++] - }; - }; - } - - throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); - } - - // these aren't really private, but nor are they really useful to document - - /** - * @private - */ - var LuxonError = /*#__PURE__*/function (_Error) { - _inheritsLoose(LuxonError, _Error); - - function LuxonError() { - return _Error.apply(this, arguments) || this; - } - - return LuxonError; - }( /*#__PURE__*/_wrapNativeSuper(Error)); - /** - * @private - */ - - - var InvalidDateTimeError = /*#__PURE__*/function (_LuxonError) { - _inheritsLoose(InvalidDateTimeError, _LuxonError); - - function InvalidDateTimeError(reason) { - return _LuxonError.call(this, "Invalid DateTime: " + reason.toMessage()) || this; - } - - return InvalidDateTimeError; - }(LuxonError); - /** - * @private - */ - - var InvalidIntervalError = /*#__PURE__*/function (_LuxonError2) { - _inheritsLoose(InvalidIntervalError, _LuxonError2); - - function InvalidIntervalError(reason) { - return _LuxonError2.call(this, "Invalid Interval: " + reason.toMessage()) || this; - } - - return InvalidIntervalError; - }(LuxonError); - /** - * @private - */ - - var InvalidDurationError = /*#__PURE__*/function (_LuxonError3) { - _inheritsLoose(InvalidDurationError, _LuxonError3); - - function InvalidDurationError(reason) { - return _LuxonError3.call(this, "Invalid Duration: " + reason.toMessage()) || this; - } - - return InvalidDurationError; - }(LuxonError); - /** - * @private - */ - - var ConflictingSpecificationError = /*#__PURE__*/function (_LuxonError4) { - _inheritsLoose(ConflictingSpecificationError, _LuxonError4); - - function ConflictingSpecificationError() { - return _LuxonError4.apply(this, arguments) || this; - } - - return ConflictingSpecificationError; - }(LuxonError); - /** - * @private - */ - - var InvalidUnitError = /*#__PURE__*/function (_LuxonError5) { - _inheritsLoose(InvalidUnitError, _LuxonError5); - - function InvalidUnitError(unit) { - return _LuxonError5.call(this, "Invalid unit " + unit) || this; - } - - return InvalidUnitError; - }(LuxonError); - /** - * @private - */ - - var InvalidArgumentError = /*#__PURE__*/function (_LuxonError6) { - _inheritsLoose(InvalidArgumentError, _LuxonError6); - - function InvalidArgumentError() { - return _LuxonError6.apply(this, arguments) || this; - } - - return InvalidArgumentError; - }(LuxonError); - /** - * @private - */ - - var ZoneIsAbstractError = /*#__PURE__*/function (_LuxonError7) { - _inheritsLoose(ZoneIsAbstractError, _LuxonError7); - - function ZoneIsAbstractError() { - return _LuxonError7.call(this, "Zone is an abstract class") || this; - } - - return ZoneIsAbstractError; - }(LuxonError); - - /** - * @private - */ - var n = "numeric", - s = "short", - l = "long"; - var DATE_SHORT = { - year: n, - month: n, - day: n - }; - var DATE_MED = { - year: n, - month: s, - day: n - }; - var DATE_MED_WITH_WEEKDAY = { - year: n, - month: s, - day: n, - weekday: s - }; - var DATE_FULL = { - year: n, - month: l, - day: n - }; - var DATE_HUGE = { - year: n, - month: l, - day: n, - weekday: l - }; - var TIME_SIMPLE = { - hour: n, - minute: n - }; - var TIME_WITH_SECONDS = { - hour: n, - minute: n, - second: n - }; - var TIME_WITH_SHORT_OFFSET = { - hour: n, - minute: n, - second: n, - timeZoneName: s - }; - var TIME_WITH_LONG_OFFSET = { - hour: n, - minute: n, - second: n, - timeZoneName: l - }; - var TIME_24_SIMPLE = { - hour: n, - minute: n, - hourCycle: "h23" - }; - var TIME_24_WITH_SECONDS = { - hour: n, - minute: n, - second: n, - hourCycle: "h23" - }; - var TIME_24_WITH_SHORT_OFFSET = { - hour: n, - minute: n, - second: n, - hourCycle: "h23", - timeZoneName: s - }; - var TIME_24_WITH_LONG_OFFSET = { - hour: n, - minute: n, - second: n, - hourCycle: "h23", - timeZoneName: l - }; - var DATETIME_SHORT = { - year: n, - month: n, - day: n, - hour: n, - minute: n - }; - var DATETIME_SHORT_WITH_SECONDS = { - year: n, - month: n, - day: n, - hour: n, - minute: n, - second: n - }; - var DATETIME_MED = { - year: n, - month: s, - day: n, - hour: n, - minute: n - }; - var DATETIME_MED_WITH_SECONDS = { - year: n, - month: s, - day: n, - hour: n, - minute: n, - second: n - }; - var DATETIME_MED_WITH_WEEKDAY = { - year: n, - month: s, - day: n, - weekday: s, - hour: n, - minute: n - }; - var DATETIME_FULL = { - year: n, - month: l, - day: n, - hour: n, - minute: n, - timeZoneName: s - }; - var DATETIME_FULL_WITH_SECONDS = { - year: n, - month: l, - day: n, - hour: n, - minute: n, - second: n, - timeZoneName: s - }; - var DATETIME_HUGE = { - year: n, - month: l, - day: n, - weekday: l, - hour: n, - minute: n, - timeZoneName: l - }; - var DATETIME_HUGE_WITH_SECONDS = { - year: n, - month: l, - day: n, - weekday: l, - hour: n, - minute: n, - second: n, - timeZoneName: l - }; - - /** - * @private - */ - // TYPES - - function isUndefined(o) { - return typeof o === "undefined"; - } - function isNumber(o) { - return typeof o === "number"; - } - function isInteger(o) { - return typeof o === "number" && o % 1 === 0; - } - function isString(o) { - return typeof o === "string"; - } - function isDate(o) { - return Object.prototype.toString.call(o) === "[object Date]"; - } // CAPABILITIES - - function hasRelative() { - try { - return typeof Intl !== "undefined" && !!Intl.RelativeTimeFormat; - } catch (e) { - return false; - } - } // OBJECTS AND ARRAYS - - function maybeArray(thing) { - return Array.isArray(thing) ? thing : [thing]; - } - function bestBy(arr, by, compare) { - if (arr.length === 0) { - return undefined; - } - - return arr.reduce(function (best, next) { - var pair = [by(next), next]; - - if (!best) { - return pair; - } else if (compare(best[0], pair[0]) === best[0]) { - return best; - } else { - return pair; - } - }, null)[1]; - } - function pick(obj, keys) { - return keys.reduce(function (a, k) { - a[k] = obj[k]; - return a; - }, {}); - } - function hasOwnProperty(obj, prop) { - return Object.prototype.hasOwnProperty.call(obj, prop); - } // NUMBERS AND STRINGS - - function integerBetween(thing, bottom, top) { - return isInteger(thing) && thing >= bottom && thing <= top; - } // x % n but takes the sign of n instead of x - - function floorMod(x, n) { - return x - n * Math.floor(x / n); - } - function padStart(input, n) { - if (n === void 0) { - n = 2; - } - - var isNeg = input < 0; - var padded; - - if (isNeg) { - padded = "-" + ("" + -input).padStart(n, "0"); - } else { - padded = ("" + input).padStart(n, "0"); - } - - return padded; - } - function parseInteger(string) { - if (isUndefined(string) || string === null || string === "") { - return undefined; - } else { - return parseInt(string, 10); - } - } - function parseFloating(string) { - if (isUndefined(string) || string === null || string === "") { - return undefined; - } else { - return parseFloat(string); - } - } - function parseMillis(fraction) { - // Return undefined (instead of 0) in these cases, where fraction is not set - if (isUndefined(fraction) || fraction === null || fraction === "") { - return undefined; - } else { - var f = parseFloat("0." + fraction) * 1000; - return Math.floor(f); - } - } - function roundTo(number, digits, towardZero) { - if (towardZero === void 0) { - towardZero = false; - } - - var factor = Math.pow(10, digits), - rounder = towardZero ? Math.trunc : Math.round; - return rounder(number * factor) / factor; - } // DATE BASICS - - function isLeapYear(year) { - return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0); - } - function daysInYear(year) { - return isLeapYear(year) ? 366 : 365; - } - function daysInMonth(year, month) { - var modMonth = floorMod(month - 1, 12) + 1, - modYear = year + (month - modMonth) / 12; - - if (modMonth === 2) { - return isLeapYear(modYear) ? 29 : 28; - } else { - return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1]; - } - } // covert a calendar object to a local timestamp (epoch, but with the offset baked in) - - function objToLocalTS(obj) { - var d = Date.UTC(obj.year, obj.month - 1, obj.day, obj.hour, obj.minute, obj.second, obj.millisecond); // for legacy reasons, years between 0 and 99 are interpreted as 19XX; revert that - - if (obj.year < 100 && obj.year >= 0) { - d = new Date(d); - d.setUTCFullYear(d.getUTCFullYear() - 1900); - } - - return +d; - } - function weeksInWeekYear(weekYear) { - var p1 = (weekYear + Math.floor(weekYear / 4) - Math.floor(weekYear / 100) + Math.floor(weekYear / 400)) % 7, - last = weekYear - 1, - p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) + Math.floor(last / 400)) % 7; - return p1 === 4 || p2 === 3 ? 53 : 52; - } - function untruncateYear(year) { - if (year > 99) { - return year; - } else return year > 60 ? 1900 + year : 2000 + year; - } // PARSING - - function parseZoneInfo(ts, offsetFormat, locale, timeZone) { - if (timeZone === void 0) { - timeZone = null; - } - - var date = new Date(ts), - intlOpts = { - hourCycle: "h23", - year: "numeric", - month: "2-digit", - day: "2-digit", - hour: "2-digit", - minute: "2-digit" - }; - - if (timeZone) { - intlOpts.timeZone = timeZone; - } - - var modified = _extends({ - timeZoneName: offsetFormat - }, intlOpts); - - var parsed = new Intl.DateTimeFormat(locale, modified).formatToParts(date).find(function (m) { - return m.type.toLowerCase() === "timezonename"; - }); - return parsed ? parsed.value : null; - } // signedOffset('-5', '30') -> -330 - - function signedOffset(offHourStr, offMinuteStr) { - var offHour = parseInt(offHourStr, 10); // don't || this because we want to preserve -0 - - if (Number.isNaN(offHour)) { - offHour = 0; - } - - var offMin = parseInt(offMinuteStr, 10) || 0, - offMinSigned = offHour < 0 || Object.is(offHour, -0) ? -offMin : offMin; - return offHour * 60 + offMinSigned; - } // COERCION - - function asNumber(value) { - var numericValue = Number(value); - if (typeof value === "boolean" || value === "" || Number.isNaN(numericValue)) throw new InvalidArgumentError("Invalid unit value " + value); - return numericValue; - } - function normalizeObject(obj, normalizer) { - var normalized = {}; - - for (var u in obj) { - if (hasOwnProperty(obj, u)) { - var v = obj[u]; - if (v === undefined || v === null) continue; - normalized[normalizer(u)] = asNumber(v); - } - } - - return normalized; - } - function formatOffset(offset, format) { - var hours = Math.trunc(Math.abs(offset / 60)), - minutes = Math.trunc(Math.abs(offset % 60)), - sign = offset >= 0 ? "+" : "-"; - - switch (format) { - case "short": - return "" + sign + padStart(hours, 2) + ":" + padStart(minutes, 2); - - case "narrow": - return "" + sign + hours + (minutes > 0 ? ":" + minutes : ""); - - case "techie": - return "" + sign + padStart(hours, 2) + padStart(minutes, 2); - - default: - throw new RangeError("Value format " + format + " is out of range for property format"); - } - } - function timeObject(obj) { - return pick(obj, ["hour", "minute", "second", "millisecond"]); - } - var ianaRegex = /[A-Za-z_+-]{1,256}(:?\/[A-Za-z0-9_+-]{1,256}(\/[A-Za-z0-9_+-]{1,256})?)?/; - - /** - * @private - */ - - - var monthsLong = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - var monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - var monthsNarrow = ["J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"]; - function months(length) { - switch (length) { - case "narrow": - return [].concat(monthsNarrow); - - case "short": - return [].concat(monthsShort); - - case "long": - return [].concat(monthsLong); - - case "numeric": - return ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]; - - case "2-digit": - return ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]; - - default: - return null; - } - } - var weekdaysLong = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; - var weekdaysShort = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; - var weekdaysNarrow = ["M", "T", "W", "T", "F", "S", "S"]; - function weekdays(length) { - switch (length) { - case "narrow": - return [].concat(weekdaysNarrow); - - case "short": - return [].concat(weekdaysShort); - - case "long": - return [].concat(weekdaysLong); - - case "numeric": - return ["1", "2", "3", "4", "5", "6", "7"]; - - default: - return null; - } - } - var meridiems = ["AM", "PM"]; - var erasLong = ["Before Christ", "Anno Domini"]; - var erasShort = ["BC", "AD"]; - var erasNarrow = ["B", "A"]; - function eras(length) { - switch (length) { - case "narrow": - return [].concat(erasNarrow); - - case "short": - return [].concat(erasShort); - - case "long": - return [].concat(erasLong); - - default: - return null; - } - } - function meridiemForDateTime(dt) { - return meridiems[dt.hour < 12 ? 0 : 1]; - } - function weekdayForDateTime(dt, length) { - return weekdays(length)[dt.weekday - 1]; - } - function monthForDateTime(dt, length) { - return months(length)[dt.month - 1]; - } - function eraForDateTime(dt, length) { - return eras(length)[dt.year < 0 ? 0 : 1]; - } - function formatRelativeTime(unit, count, numeric, narrow) { - if (numeric === void 0) { - numeric = "always"; - } - - if (narrow === void 0) { - narrow = false; - } - - var units = { - years: ["year", "yr."], - quarters: ["quarter", "qtr."], - months: ["month", "mo."], - weeks: ["week", "wk."], - days: ["day", "day", "days"], - hours: ["hour", "hr."], - minutes: ["minute", "min."], - seconds: ["second", "sec."] - }; - var lastable = ["hours", "minutes", "seconds"].indexOf(unit) === -1; - - if (numeric === "auto" && lastable) { - var isDay = unit === "days"; - - switch (count) { - case 1: - return isDay ? "tomorrow" : "next " + units[unit][0]; - - case -1: - return isDay ? "yesterday" : "last " + units[unit][0]; - - case 0: - return isDay ? "today" : "this " + units[unit][0]; - - } - } - - var isInPast = Object.is(count, -0) || count < 0, - fmtValue = Math.abs(count), - singular = fmtValue === 1, - lilUnits = units[unit], - fmtUnit = narrow ? singular ? lilUnits[1] : lilUnits[2] || lilUnits[1] : singular ? units[unit][0] : unit; - return isInPast ? fmtValue + " " + fmtUnit + " ago" : "in " + fmtValue + " " + fmtUnit; - } - - function stringifyTokens(splits, tokenToString) { - var s = ""; - - for (var _iterator = _createForOfIteratorHelperLoose(splits), _step; !(_step = _iterator()).done;) { - var token = _step.value; - - if (token.literal) { - s += token.val; - } else { - s += tokenToString(token.val); - } - } - - return s; - } - - var _macroTokenToFormatOpts = { - D: DATE_SHORT, - DD: DATE_MED, - DDD: DATE_FULL, - DDDD: DATE_HUGE, - t: TIME_SIMPLE, - tt: TIME_WITH_SECONDS, - ttt: TIME_WITH_SHORT_OFFSET, - tttt: TIME_WITH_LONG_OFFSET, - T: TIME_24_SIMPLE, - TT: TIME_24_WITH_SECONDS, - TTT: TIME_24_WITH_SHORT_OFFSET, - TTTT: TIME_24_WITH_LONG_OFFSET, - f: DATETIME_SHORT, - ff: DATETIME_MED, - fff: DATETIME_FULL, - ffff: DATETIME_HUGE, - F: DATETIME_SHORT_WITH_SECONDS, - FF: DATETIME_MED_WITH_SECONDS, - FFF: DATETIME_FULL_WITH_SECONDS, - FFFF: DATETIME_HUGE_WITH_SECONDS - }; - /** - * @private - */ - - var Formatter = /*#__PURE__*/function () { - Formatter.create = function create(locale, opts) { - if (opts === void 0) { - opts = {}; - } - - return new Formatter(locale, opts); - }; - - Formatter.parseFormat = function parseFormat(fmt) { - var current = null, - currentFull = "", - bracketed = false; - var splits = []; - - for (var i = 0; i < fmt.length; i++) { - var c = fmt.charAt(i); - - if (c === "'") { - if (currentFull.length > 0) { - splits.push({ - literal: bracketed, - val: currentFull - }); - } - - current = null; - currentFull = ""; - bracketed = !bracketed; - } else if (bracketed) { - currentFull += c; - } else if (c === current) { - currentFull += c; - } else { - if (currentFull.length > 0) { - splits.push({ - literal: false, - val: currentFull - }); - } - - currentFull = c; - current = c; - } - } - - if (currentFull.length > 0) { - splits.push({ - literal: bracketed, - val: currentFull - }); - } - - return splits; - }; - - Formatter.macroTokenToFormatOpts = function macroTokenToFormatOpts(token) { - return _macroTokenToFormatOpts[token]; - }; - - function Formatter(locale, formatOpts) { - this.opts = formatOpts; - this.loc = locale; - this.systemLoc = null; - } - - var _proto = Formatter.prototype; - - _proto.formatWithSystemDefault = function formatWithSystemDefault(dt, opts) { - if (this.systemLoc === null) { - this.systemLoc = this.loc.redefaultToSystem(); - } - - var df = this.systemLoc.dtFormatter(dt, _extends({}, this.opts, opts)); - return df.format(); - }; - - _proto.formatDateTime = function formatDateTime(dt, opts) { - if (opts === void 0) { - opts = {}; - } - - var df = this.loc.dtFormatter(dt, _extends({}, this.opts, opts)); - return df.format(); - }; - - _proto.formatDateTimeParts = function formatDateTimeParts(dt, opts) { - if (opts === void 0) { - opts = {}; - } - - var df = this.loc.dtFormatter(dt, _extends({}, this.opts, opts)); - return df.formatToParts(); - }; - - _proto.resolvedOptions = function resolvedOptions(dt, opts) { - if (opts === void 0) { - opts = {}; - } - - var df = this.loc.dtFormatter(dt, _extends({}, this.opts, opts)); - return df.resolvedOptions(); - }; - - _proto.num = function num(n, p) { - if (p === void 0) { - p = 0; - } - - // we get some perf out of doing this here, annoyingly - if (this.opts.forceSimple) { - return padStart(n, p); - } - - var opts = _extends({}, this.opts); - - if (p > 0) { - opts.padTo = p; - } - - return this.loc.numberFormatter(opts).format(n); - }; - - _proto.formatDateTimeFromString = function formatDateTimeFromString(dt, fmt) { - var _this = this; - - var knownEnglish = this.loc.listingMode() === "en", - useDateTimeFormatter = this.loc.outputCalendar && this.loc.outputCalendar !== "gregory", - string = function string(opts, extract) { - return _this.loc.extract(dt, opts, extract); - }, - formatOffset = function formatOffset(opts) { - if (dt.isOffsetFixed && dt.offset === 0 && opts.allowZ) { - return "Z"; - } - - return dt.isValid ? dt.zone.formatOffset(dt.ts, opts.format) : ""; - }, - meridiem = function meridiem() { - return knownEnglish ? meridiemForDateTime(dt) : string({ - hour: "numeric", - hourCycle: "h12" - }, "dayperiod"); - }, - month = function month(length, standalone) { - return knownEnglish ? monthForDateTime(dt, length) : string(standalone ? { - month: length - } : { - month: length, - day: "numeric" - }, "month"); - }, - weekday = function weekday(length, standalone) { - return knownEnglish ? weekdayForDateTime(dt, length) : string(standalone ? { - weekday: length - } : { - weekday: length, - month: "long", - day: "numeric" - }, "weekday"); - }, - maybeMacro = function maybeMacro(token) { - var formatOpts = Formatter.macroTokenToFormatOpts(token); - - if (formatOpts) { - return _this.formatWithSystemDefault(dt, formatOpts); - } else { - return token; - } - }, - era = function era(length) { - return knownEnglish ? eraForDateTime(dt, length) : string({ - era: length - }, "era"); - }, - tokenToString = function tokenToString(token) { - // Where possible: http://cldr.unicode.org/translation/date-time-1/date-time#TOC-Standalone-vs.-Format-Styles - switch (token) { - // ms - case "S": - return _this.num(dt.millisecond); - - case "u": // falls through - - case "SSS": - return _this.num(dt.millisecond, 3); - // seconds - - case "s": - return _this.num(dt.second); - - case "ss": - return _this.num(dt.second, 2); - // fractional seconds - - case "uu": - return _this.num(Math.floor(dt.millisecond / 10), 2); - - case "uuu": - return _this.num(Math.floor(dt.millisecond / 100)); - // minutes - - case "m": - return _this.num(dt.minute); - - case "mm": - return _this.num(dt.minute, 2); - // hours - - case "h": - return _this.num(dt.hour % 12 === 0 ? 12 : dt.hour % 12); - - case "hh": - return _this.num(dt.hour % 12 === 0 ? 12 : dt.hour % 12, 2); - - case "H": - return _this.num(dt.hour); - - case "HH": - return _this.num(dt.hour, 2); - // offset - - case "Z": - // like +6 - return formatOffset({ - format: "narrow", - allowZ: _this.opts.allowZ - }); - - case "ZZ": - // like +06:00 - return formatOffset({ - format: "short", - allowZ: _this.opts.allowZ - }); - - case "ZZZ": - // like +0600 - return formatOffset({ - format: "techie", - allowZ: _this.opts.allowZ - }); - - case "ZZZZ": - // like EST - return dt.zone.offsetName(dt.ts, { - format: "short", - locale: _this.loc.locale - }); - - case "ZZZZZ": - // like Eastern Standard Time - return dt.zone.offsetName(dt.ts, { - format: "long", - locale: _this.loc.locale - }); - // zone - - case "z": - // like America/New_York - return dt.zoneName; - // meridiems - - case "a": - return meridiem(); - // dates - - case "d": - return useDateTimeFormatter ? string({ - day: "numeric" - }, "day") : _this.num(dt.day); - - case "dd": - return useDateTimeFormatter ? string({ - day: "2-digit" - }, "day") : _this.num(dt.day, 2); - // weekdays - standalone - - case "c": - // like 1 - return _this.num(dt.weekday); - - case "ccc": - // like 'Tues' - return weekday("short", true); - - case "cccc": - // like 'Tuesday' - return weekday("long", true); - - case "ccccc": - // like 'T' - return weekday("narrow", true); - // weekdays - format - - case "E": - // like 1 - return _this.num(dt.weekday); - - case "EEE": - // like 'Tues' - return weekday("short", false); - - case "EEEE": - // like 'Tuesday' - return weekday("long", false); - - case "EEEEE": - // like 'T' - return weekday("narrow", false); - // months - standalone - - case "L": - // like 1 - return useDateTimeFormatter ? string({ - month: "numeric", - day: "numeric" - }, "month") : _this.num(dt.month); - - case "LL": - // like 01, doesn't seem to work - return useDateTimeFormatter ? string({ - month: "2-digit", - day: "numeric" - }, "month") : _this.num(dt.month, 2); - - case "LLL": - // like Jan - return month("short", true); - - case "LLLL": - // like January - return month("long", true); - - case "LLLLL": - // like J - return month("narrow", true); - // months - format - - case "M": - // like 1 - return useDateTimeFormatter ? string({ - month: "numeric" - }, "month") : _this.num(dt.month); - - case "MM": - // like 01 - return useDateTimeFormatter ? string({ - month: "2-digit" - }, "month") : _this.num(dt.month, 2); - - case "MMM": - // like Jan - return month("short", false); - - case "MMMM": - // like January - return month("long", false); - - case "MMMMM": - // like J - return month("narrow", false); - // years - - case "y": - // like 2014 - return useDateTimeFormatter ? string({ - year: "numeric" - }, "year") : _this.num(dt.year); - - case "yy": - // like 14 - return useDateTimeFormatter ? string({ - year: "2-digit" - }, "year") : _this.num(dt.year.toString().slice(-2), 2); - - case "yyyy": - // like 0012 - return useDateTimeFormatter ? string({ - year: "numeric" - }, "year") : _this.num(dt.year, 4); - - case "yyyyyy": - // like 000012 - return useDateTimeFormatter ? string({ - year: "numeric" - }, "year") : _this.num(dt.year, 6); - // eras - - case "G": - // like AD - return era("short"); - - case "GG": - // like Anno Domini - return era("long"); - - case "GGGGG": - return era("narrow"); - - case "kk": - return _this.num(dt.weekYear.toString().slice(-2), 2); - - case "kkkk": - return _this.num(dt.weekYear, 4); - - case "W": - return _this.num(dt.weekNumber); - - case "WW": - return _this.num(dt.weekNumber, 2); - - case "o": - return _this.num(dt.ordinal); - - case "ooo": - return _this.num(dt.ordinal, 3); - - case "q": - // like 1 - return _this.num(dt.quarter); - - case "qq": - // like 01 - return _this.num(dt.quarter, 2); - - case "X": - return _this.num(Math.floor(dt.ts / 1000)); - - case "x": - return _this.num(dt.ts); - - default: - return maybeMacro(token); - } - }; - - return stringifyTokens(Formatter.parseFormat(fmt), tokenToString); - }; - - _proto.formatDurationFromString = function formatDurationFromString(dur, fmt) { - var _this2 = this; - - var tokenToField = function tokenToField(token) { - switch (token[0]) { - case "S": - return "millisecond"; - - case "s": - return "second"; - - case "m": - return "minute"; - - case "h": - return "hour"; - - case "d": - return "day"; - - case "M": - return "month"; - - case "y": - return "year"; - - default: - return null; - } - }, - tokenToString = function tokenToString(lildur) { - return function (token) { - var mapped = tokenToField(token); - - if (mapped) { - return _this2.num(lildur.get(mapped), token.length); - } else { - return token; - } - }; - }, - tokens = Formatter.parseFormat(fmt), - realTokens = tokens.reduce(function (found, _ref) { - var literal = _ref.literal, - val = _ref.val; - return literal ? found : found.concat(val); - }, []), - collapsed = dur.shiftTo.apply(dur, realTokens.map(tokenToField).filter(function (t) { - return t; - })); - - return stringifyTokens(tokens, tokenToString(collapsed)); - }; - - return Formatter; - }(); - - var Invalid = /*#__PURE__*/function () { - function Invalid(reason, explanation) { - this.reason = reason; - this.explanation = explanation; - } - - var _proto = Invalid.prototype; - - _proto.toMessage = function toMessage() { - if (this.explanation) { - return this.reason + ": " + this.explanation; - } else { - return this.reason; - } - }; - - return Invalid; - }(); - - /** - * @interface - */ - - var Zone = /*#__PURE__*/function () { - function Zone() {} - - var _proto = Zone.prototype; - - /** - * Returns the offset's common name (such as EST) at the specified timestamp - * @abstract - * @param {number} ts - Epoch milliseconds for which to get the name - * @param {Object} opts - Options to affect the format - * @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'. - * @param {string} opts.locale - What locale to return the offset name in. - * @return {string} - */ - _proto.offsetName = function offsetName(ts, opts) { - throw new ZoneIsAbstractError(); - } - /** - * Returns the offset's value as a string - * @abstract - * @param {number} ts - Epoch milliseconds for which to get the offset - * @param {string} format - What style of offset to return. - * Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively - * @return {string} - */ - ; - - _proto.formatOffset = function formatOffset(ts, format) { - throw new ZoneIsAbstractError(); - } - /** - * Return the offset in minutes for this zone at the specified timestamp. - * @abstract - * @param {number} ts - Epoch milliseconds for which to compute the offset - * @return {number} - */ - ; - - _proto.offset = function offset(ts) { - throw new ZoneIsAbstractError(); - } - /** - * Return whether this Zone is equal to another zone - * @abstract - * @param {Zone} otherZone - the zone to compare - * @return {boolean} - */ - ; - - _proto.equals = function equals(otherZone) { - throw new ZoneIsAbstractError(); - } - /** - * Return whether this Zone is valid. - * @abstract - * @type {boolean} - */ - ; - - _createClass(Zone, [{ - key: "type", - get: - /** - * The type of zone - * @abstract - * @type {string} - */ - function get() { - throw new ZoneIsAbstractError(); - } - /** - * The name of this zone. - * @abstract - * @type {string} - */ - - }, { - key: "name", - get: function get() { - throw new ZoneIsAbstractError(); - } - /** - * Returns whether the offset is known to be fixed for the whole year. - * @abstract - * @type {boolean} - */ - - }, { - key: "isUniversal", - get: function get() { - throw new ZoneIsAbstractError(); - } - }, { - key: "isValid", - get: function get() { - throw new ZoneIsAbstractError(); - } - }]); - - return Zone; - }(); - - var singleton$1 = null; - /** - * Represents the local zone for this JavaScript environment. - * @implements {Zone} - */ - - var SystemZone = /*#__PURE__*/function (_Zone) { - _inheritsLoose(SystemZone, _Zone); - - function SystemZone() { - return _Zone.apply(this, arguments) || this; - } - - var _proto = SystemZone.prototype; - - /** @override **/ - _proto.offsetName = function offsetName(ts, _ref) { - var format = _ref.format, - locale = _ref.locale; - return parseZoneInfo(ts, format, locale); - } - /** @override **/ - ; - - _proto.formatOffset = function formatOffset$1(ts, format) { - return formatOffset(this.offset(ts), format); - } - /** @override **/ - ; - - _proto.offset = function offset(ts) { - return -new Date(ts).getTimezoneOffset(); - } - /** @override **/ - ; - - _proto.equals = function equals(otherZone) { - return otherZone.type === "system"; - } - /** @override **/ - ; - - _createClass(SystemZone, [{ - key: "type", - get: - /** @override **/ - function get() { - return "system"; - } - /** @override **/ - - }, { - key: "name", - get: function get() { - return new Intl.DateTimeFormat().resolvedOptions().timeZone; - } - /** @override **/ - - }, { - key: "isUniversal", - get: function get() { - return false; - } - }, { - key: "isValid", - get: function get() { - return true; - } - }], [{ - key: "instance", - get: - /** - * Get a singleton instance of the local zone - * @return {SystemZone} - */ - function get() { - if (singleton$1 === null) { - singleton$1 = new SystemZone(); - } - - return singleton$1; - } - }]); - - return SystemZone; - }(Zone); - - RegExp("^" + ianaRegex.source + "$"); - var dtfCache = {}; - - function makeDTF(zone) { - if (!dtfCache[zone]) { - dtfCache[zone] = new Intl.DateTimeFormat("en-US", { - hour12: false, - timeZone: zone, - year: "numeric", - month: "2-digit", - day: "2-digit", - hour: "2-digit", - minute: "2-digit", - second: "2-digit" - }); - } - - return dtfCache[zone]; - } - - var typeToPos = { - year: 0, - month: 1, - day: 2, - hour: 3, - minute: 4, - second: 5 - }; - - function hackyOffset(dtf, date) { - var formatted = dtf.format(date).replace(/\u200E/g, ""), - parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted), - fMonth = parsed[1], - fDay = parsed[2], - fYear = parsed[3], - fHour = parsed[4], - fMinute = parsed[5], - fSecond = parsed[6]; - return [fYear, fMonth, fDay, fHour, fMinute, fSecond]; - } - - function partsOffset(dtf, date) { - var formatted = dtf.formatToParts(date), - filled = []; - - for (var i = 0; i < formatted.length; i++) { - var _formatted$i = formatted[i], - type = _formatted$i.type, - value = _formatted$i.value, - pos = typeToPos[type]; - - if (!isUndefined(pos)) { - filled[pos] = parseInt(value, 10); - } - } - - return filled; - } - - var ianaZoneCache = {}; - /** - * A zone identified by an IANA identifier, like America/New_York - * @implements {Zone} - */ - - var IANAZone = /*#__PURE__*/function (_Zone) { - _inheritsLoose(IANAZone, _Zone); - - /** - * @param {string} name - Zone name - * @return {IANAZone} - */ - IANAZone.create = function create(name) { - if (!ianaZoneCache[name]) { - ianaZoneCache[name] = new IANAZone(name); - } - - return ianaZoneCache[name]; - } - /** - * Reset local caches. Should only be necessary in testing scenarios. - * @return {void} - */ - ; - - IANAZone.resetCache = function resetCache() { - ianaZoneCache = {}; - dtfCache = {}; - } - /** - * Returns whether the provided string is a valid specifier. This only checks the string's format, not that the specifier identifies a known zone; see isValidZone for that. - * @param {string} s - The string to check validity on - * @example IANAZone.isValidSpecifier("America/New_York") //=> true - * @example IANAZone.isValidSpecifier("Sport~~blorp") //=> false - * @deprecated This method returns false some valid IANA names. Use isValidZone instead - * @return {boolean} - */ - ; - - IANAZone.isValidSpecifier = function isValidSpecifier(s) { - return this.isValidZone(s); - } - /** - * Returns whether the provided string identifies a real zone - * @param {string} zone - The string to check - * @example IANAZone.isValidZone("America/New_York") //=> true - * @example IANAZone.isValidZone("Fantasia/Castle") //=> false - * @example IANAZone.isValidZone("Sport~~blorp") //=> false - * @return {boolean} - */ - ; - - IANAZone.isValidZone = function isValidZone(zone) { - if (!zone) { - return false; - } - - try { - new Intl.DateTimeFormat("en-US", { - timeZone: zone - }).format(); - return true; - } catch (e) { - return false; - } - }; - - function IANAZone(name) { - var _this; - - _this = _Zone.call(this) || this; - /** @private **/ - - _this.zoneName = name; - /** @private **/ - - _this.valid = IANAZone.isValidZone(name); - return _this; - } - /** @override **/ - - - var _proto = IANAZone.prototype; - - /** @override **/ - _proto.offsetName = function offsetName(ts, _ref) { - var format = _ref.format, - locale = _ref.locale; - return parseZoneInfo(ts, format, locale, this.name); - } - /** @override **/ - ; - - _proto.formatOffset = function formatOffset$1(ts, format) { - return formatOffset(this.offset(ts), format); - } - /** @override **/ - ; - - _proto.offset = function offset(ts) { - var date = new Date(ts); - if (isNaN(date)) return NaN; - - var dtf = makeDTF(this.name), - _ref2 = dtf.formatToParts ? partsOffset(dtf, date) : hackyOffset(dtf, date), - year = _ref2[0], - month = _ref2[1], - day = _ref2[2], - hour = _ref2[3], - minute = _ref2[4], - second = _ref2[5]; // because we're using hour12 and https://bugs.chromium.org/p/chromium/issues/detail?id=1025564&can=2&q=%2224%3A00%22%20datetimeformat - - - var adjustedHour = hour === 24 ? 0 : hour; - var asUTC = objToLocalTS({ - year: year, - month: month, - day: day, - hour: adjustedHour, - minute: minute, - second: second, - millisecond: 0 - }); - var asTS = +date; - var over = asTS % 1000; - asTS -= over >= 0 ? over : 1000 + over; - return (asUTC - asTS) / (60 * 1000); - } - /** @override **/ - ; - - _proto.equals = function equals(otherZone) { - return otherZone.type === "iana" && otherZone.name === this.name; - } - /** @override **/ - ; - - _createClass(IANAZone, [{ - key: "type", - get: function get() { - return "iana"; - } - /** @override **/ - - }, { - key: "name", - get: function get() { - return this.zoneName; - } - /** @override **/ - - }, { - key: "isUniversal", - get: function get() { - return false; - } - }, { - key: "isValid", - get: function get() { - return this.valid; - } - }]); - - return IANAZone; - }(Zone); - - var singleton = null; - /** - * A zone with a fixed offset (meaning no DST) - * @implements {Zone} - */ - - var FixedOffsetZone = /*#__PURE__*/function (_Zone) { - _inheritsLoose(FixedOffsetZone, _Zone); - - /** - * Get an instance with a specified offset - * @param {number} offset - The offset in minutes - * @return {FixedOffsetZone} - */ - FixedOffsetZone.instance = function instance(offset) { - return offset === 0 ? FixedOffsetZone.utcInstance : new FixedOffsetZone(offset); - } - /** - * Get an instance of FixedOffsetZone from a UTC offset string, like "UTC+6" - * @param {string} s - The offset string to parse - * @example FixedOffsetZone.parseSpecifier("UTC+6") - * @example FixedOffsetZone.parseSpecifier("UTC+06") - * @example FixedOffsetZone.parseSpecifier("UTC-6:00") - * @return {FixedOffsetZone} - */ - ; - - FixedOffsetZone.parseSpecifier = function parseSpecifier(s) { - if (s) { - var r = s.match(/^utc(?:([+-]\d{1,2})(?::(\d{2}))?)?$/i); - - if (r) { - return new FixedOffsetZone(signedOffset(r[1], r[2])); - } - } - - return null; - }; - - function FixedOffsetZone(offset) { - var _this; - - _this = _Zone.call(this) || this; - /** @private **/ - - _this.fixed = offset; - return _this; - } - /** @override **/ - - - var _proto = FixedOffsetZone.prototype; - - /** @override **/ - _proto.offsetName = function offsetName() { - return this.name; - } - /** @override **/ - ; - - _proto.formatOffset = function formatOffset$1(ts, format) { - return formatOffset(this.fixed, format); - } - /** @override **/ - ; - - /** @override **/ - _proto.offset = function offset() { - return this.fixed; - } - /** @override **/ - ; - - _proto.equals = function equals(otherZone) { - return otherZone.type === "fixed" && otherZone.fixed === this.fixed; - } - /** @override **/ - ; - - _createClass(FixedOffsetZone, [{ - key: "type", - get: function get() { - return "fixed"; - } - /** @override **/ - - }, { - key: "name", - get: function get() { - return this.fixed === 0 ? "UTC" : "UTC" + formatOffset(this.fixed, "narrow"); - } - }, { - key: "isUniversal", - get: function get() { - return true; - } - }, { - key: "isValid", - get: function get() { - return true; - } - }], [{ - key: "utcInstance", - get: - /** - * Get a singleton instance of UTC - * @return {FixedOffsetZone} - */ - function get() { - if (singleton === null) { - singleton = new FixedOffsetZone(0); - } - - return singleton; - } - }]); - - return FixedOffsetZone; - }(Zone); - - /** - * A zone that failed to parse. You should never need to instantiate this. - * @implements {Zone} - */ - - var InvalidZone = /*#__PURE__*/function (_Zone) { - _inheritsLoose(InvalidZone, _Zone); - - function InvalidZone(zoneName) { - var _this; - - _this = _Zone.call(this) || this; - /** @private */ - - _this.zoneName = zoneName; - return _this; - } - /** @override **/ - - - var _proto = InvalidZone.prototype; - - /** @override **/ - _proto.offsetName = function offsetName() { - return null; - } - /** @override **/ - ; - - _proto.formatOffset = function formatOffset() { - return ""; - } - /** @override **/ - ; - - _proto.offset = function offset() { - return NaN; - } - /** @override **/ - ; - - _proto.equals = function equals() { - return false; - } - /** @override **/ - ; - - _createClass(InvalidZone, [{ - key: "type", - get: function get() { - return "invalid"; - } - /** @override **/ - - }, { - key: "name", - get: function get() { - return this.zoneName; - } - /** @override **/ - - }, { - key: "isUniversal", - get: function get() { - return false; - } - }, { - key: "isValid", - get: function get() { - return false; - } - }]); - - return InvalidZone; - }(Zone); - - /** - * @private - */ - function normalizeZone(input, defaultZone) { - - if (isUndefined(input) || input === null) { - return defaultZone; - } else if (input instanceof Zone) { - return input; - } else if (isString(input)) { - var lowered = input.toLowerCase(); - if (lowered === "local" || lowered === "system") return defaultZone;else if (lowered === "utc" || lowered === "gmt") return FixedOffsetZone.utcInstance;else return FixedOffsetZone.parseSpecifier(lowered) || IANAZone.create(input); - } else if (isNumber(input)) { - return FixedOffsetZone.instance(input); - } else if (typeof input === "object" && input.offset && typeof input.offset === "number") { - // This is dumb, but the instanceof check above doesn't seem to really work - // so we're duck checking it - return input; - } else { - return new InvalidZone(input); - } - } - - var now = function now() { - return Date.now(); - }, - defaultZone = "system", - defaultLocale = null, - defaultNumberingSystem = null, - defaultOutputCalendar = null, - throwOnInvalid; - /** - * Settings contains static getters and setters that control Luxon's overall behavior. Luxon is a simple library with few options, but the ones it does have live here. - */ - - - var Settings = /*#__PURE__*/function () { - function Settings() {} - - /** - * Reset Luxon's global caches. Should only be necessary in testing scenarios. - * @return {void} - */ - Settings.resetCaches = function resetCaches() { - Locale.resetCache(); - IANAZone.resetCache(); - }; - - _createClass(Settings, null, [{ - key: "now", - get: - /** - * Get the callback for returning the current timestamp. - * @type {function} - */ - function get() { - return now; - } - /** - * Set the callback for returning the current timestamp. - * The function should return a number, which will be interpreted as an Epoch millisecond count - * @type {function} - * @example Settings.now = () => Date.now() + 3000 // pretend it is 3 seconds in the future - * @example Settings.now = () => 0 // always pretend it's Jan 1, 1970 at midnight in UTC time - */ - , - set: function set(n) { - now = n; - } - /** - * Set the default time zone to create DateTimes in. Does not affect existing instances. - * Use the value "system" to reset this value to the system's time zone. - * @type {string} - */ - - }, { - key: "defaultZone", - get: - /** - * Get the default time zone object currently used to create DateTimes. Does not affect existing instances. - * The default value is the system's time zone (the one set on the machine that runs this code). - * @type {Zone} - */ - function get() { - return normalizeZone(defaultZone, SystemZone.instance); - } - /** - * Get the default locale to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - , - set: function set(zone) { - defaultZone = zone; - } - }, { - key: "defaultLocale", - get: function get() { - return defaultLocale; - } - /** - * Set the default locale to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - , - set: function set(locale) { - defaultLocale = locale; - } - /** - * Get the default numbering system to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - - }, { - key: "defaultNumberingSystem", - get: function get() { - return defaultNumberingSystem; - } - /** - * Set the default numbering system to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - , - set: function set(numberingSystem) { - defaultNumberingSystem = numberingSystem; - } - /** - * Get the default output calendar to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - - }, { - key: "defaultOutputCalendar", - get: function get() { - return defaultOutputCalendar; - } - /** - * Set the default output calendar to create DateTimes with. Does not affect existing instances. - * @type {string} - */ - , - set: function set(outputCalendar) { - defaultOutputCalendar = outputCalendar; - } - /** - * Get whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals - * @type {boolean} - */ - - }, { - key: "throwOnInvalid", - get: function get() { - return throwOnInvalid; - } - /** - * Set whether Luxon will throw when it encounters invalid DateTimes, Durations, or Intervals - * @type {boolean} - */ - , - set: function set(t) { - throwOnInvalid = t; - } - }]); - - return Settings; - }(); - - var _excluded = ["base"], - _excluded2 = ["padTo", "floor"]; - - var intlLFCache = {}; - - function getCachedLF(locString, opts) { - if (opts === void 0) { - opts = {}; - } - - var key = JSON.stringify([locString, opts]); - var dtf = intlLFCache[key]; - - if (!dtf) { - dtf = new Intl.ListFormat(locString, opts); - intlLFCache[key] = dtf; - } - - return dtf; - } - - var intlDTCache = {}; - - function getCachedDTF(locString, opts) { - if (opts === void 0) { - opts = {}; - } - - var key = JSON.stringify([locString, opts]); - var dtf = intlDTCache[key]; - - if (!dtf) { - dtf = new Intl.DateTimeFormat(locString, opts); - intlDTCache[key] = dtf; - } - - return dtf; - } - - var intlNumCache = {}; - - function getCachedINF(locString, opts) { - if (opts === void 0) { - opts = {}; - } - - var key = JSON.stringify([locString, opts]); - var inf = intlNumCache[key]; - - if (!inf) { - inf = new Intl.NumberFormat(locString, opts); - intlNumCache[key] = inf; - } - - return inf; - } - - var intlRelCache = {}; - - function getCachedRTF(locString, opts) { - if (opts === void 0) { - opts = {}; - } - - var _opts = opts; - _opts.base; - var cacheKeyOpts = _objectWithoutPropertiesLoose(_opts, _excluded); // exclude `base` from the options - - - var key = JSON.stringify([locString, cacheKeyOpts]); - var inf = intlRelCache[key]; - - if (!inf) { - inf = new Intl.RelativeTimeFormat(locString, opts); - intlRelCache[key] = inf; - } - - return inf; - } - - var sysLocaleCache = null; - - function systemLocale() { - if (sysLocaleCache) { - return sysLocaleCache; - } else { - sysLocaleCache = new Intl.DateTimeFormat().resolvedOptions().locale; - return sysLocaleCache; - } - } - - function parseLocaleString(localeStr) { - // I really want to avoid writing a BCP 47 parser - // see, e.g. https://github.com/wooorm/bcp-47 - // Instead, we'll do this: - // a) if the string has no -u extensions, just leave it alone - // b) if it does, use Intl to resolve everything - // c) if Intl fails, try again without the -u - var uIndex = localeStr.indexOf("-u-"); - - if (uIndex === -1) { - return [localeStr]; - } else { - var options; - var smaller = localeStr.substring(0, uIndex); - - try { - options = getCachedDTF(localeStr).resolvedOptions(); - } catch (e) { - options = getCachedDTF(smaller).resolvedOptions(); - } - - var _options = options, - numberingSystem = _options.numberingSystem, - calendar = _options.calendar; // return the smaller one so that we can append the calendar and numbering overrides to it - - return [smaller, numberingSystem, calendar]; - } - } - - function intlConfigString(localeStr, numberingSystem, outputCalendar) { - if (outputCalendar || numberingSystem) { - localeStr += "-u"; - - if (outputCalendar) { - localeStr += "-ca-" + outputCalendar; - } - - if (numberingSystem) { - localeStr += "-nu-" + numberingSystem; - } - - return localeStr; - } else { - return localeStr; - } - } - - function mapMonths(f) { - var ms = []; - - for (var i = 1; i <= 12; i++) { - var dt = DateTime.utc(2016, i, 1); - ms.push(f(dt)); - } - - return ms; - } - - function mapWeekdays(f) { - var ms = []; - - for (var i = 1; i <= 7; i++) { - var dt = DateTime.utc(2016, 11, 13 + i); - ms.push(f(dt)); - } - - return ms; - } - - function listStuff(loc, length, defaultOK, englishFn, intlFn) { - var mode = loc.listingMode(defaultOK); - - if (mode === "error") { - return null; - } else if (mode === "en") { - return englishFn(length); - } else { - return intlFn(length); - } - } - - function supportsFastNumbers(loc) { - if (loc.numberingSystem && loc.numberingSystem !== "latn") { - return false; - } else { - return loc.numberingSystem === "latn" || !loc.locale || loc.locale.startsWith("en") || new Intl.DateTimeFormat(loc.intl).resolvedOptions().numberingSystem === "latn"; - } - } - /** - * @private - */ - - - var PolyNumberFormatter = /*#__PURE__*/function () { - function PolyNumberFormatter(intl, forceSimple, opts) { - this.padTo = opts.padTo || 0; - this.floor = opts.floor || false; - - opts.padTo; - opts.floor; - var otherOpts = _objectWithoutPropertiesLoose(opts, _excluded2); - - if (!forceSimple || Object.keys(otherOpts).length > 0) { - var intlOpts = _extends({ - useGrouping: false - }, opts); - - if (opts.padTo > 0) intlOpts.minimumIntegerDigits = opts.padTo; - this.inf = getCachedINF(intl, intlOpts); - } - } - - var _proto = PolyNumberFormatter.prototype; - - _proto.format = function format(i) { - if (this.inf) { - var fixed = this.floor ? Math.floor(i) : i; - return this.inf.format(fixed); - } else { - // to match the browser's numberformatter defaults - var _fixed = this.floor ? Math.floor(i) : roundTo(i, 3); - - return padStart(_fixed, this.padTo); - } - }; - - return PolyNumberFormatter; - }(); - /** - * @private - */ - - - var PolyDateFormatter = /*#__PURE__*/function () { - function PolyDateFormatter(dt, intl, opts) { - this.opts = opts; - var z; - - if (dt.zone.isUniversal) { - // UTC-8 or Etc/UTC-8 are not part of tzdata, only Etc/GMT+8 and the like. - // That is why fixed-offset TZ is set to that unless it is: - // 1. Representing offset 0 when UTC is used to maintain previous behavior and does not become GMT. - // 2. Unsupported by the browser: - // - some do not support Etc/ - // - < Etc/GMT-14, > Etc/GMT+12, and 30-minute or 45-minute offsets are not part of tzdata - var gmtOffset = -1 * (dt.offset / 60); - var offsetZ = gmtOffset >= 0 ? "Etc/GMT+" + gmtOffset : "Etc/GMT" + gmtOffset; - - if (dt.offset !== 0 && IANAZone.create(offsetZ).valid) { - z = offsetZ; - this.dt = dt; - } else { - // Not all fixed-offset zones like Etc/+4:30 are present in tzdata. - // So we have to make do. Two cases: - // 1. The format options tell us to show the zone. We can't do that, so the best - // we can do is format the date in UTC. - // 2. The format options don't tell us to show the zone. Then we can adjust them - // the time and tell the formatter to show it to us in UTC, so that the time is right - // and the bad zone doesn't show up. - z = "UTC"; - - if (opts.timeZoneName) { - this.dt = dt; - } else { - this.dt = dt.offset === 0 ? dt : DateTime.fromMillis(dt.ts + dt.offset * 60 * 1000); - } - } - } else if (dt.zone.type === "system") { - this.dt = dt; - } else { - this.dt = dt; - z = dt.zone.name; - } - - var intlOpts = _extends({}, this.opts); - - if (z) { - intlOpts.timeZone = z; - } - - this.dtf = getCachedDTF(intl, intlOpts); - } - - var _proto2 = PolyDateFormatter.prototype; - - _proto2.format = function format() { - return this.dtf.format(this.dt.toJSDate()); - }; - - _proto2.formatToParts = function formatToParts() { - return this.dtf.formatToParts(this.dt.toJSDate()); - }; - - _proto2.resolvedOptions = function resolvedOptions() { - return this.dtf.resolvedOptions(); - }; - - return PolyDateFormatter; - }(); - /** - * @private - */ - - - var PolyRelFormatter = /*#__PURE__*/function () { - function PolyRelFormatter(intl, isEnglish, opts) { - this.opts = _extends({ - style: "long" - }, opts); - - if (!isEnglish && hasRelative()) { - this.rtf = getCachedRTF(intl, opts); - } - } - - var _proto3 = PolyRelFormatter.prototype; - - _proto3.format = function format(count, unit) { - if (this.rtf) { - return this.rtf.format(count, unit); - } else { - return formatRelativeTime(unit, count, this.opts.numeric, this.opts.style !== "long"); - } - }; - - _proto3.formatToParts = function formatToParts(count, unit) { - if (this.rtf) { - return this.rtf.formatToParts(count, unit); - } else { - return []; - } - }; - - return PolyRelFormatter; - }(); - /** - * @private - */ - - - var Locale = /*#__PURE__*/function () { - Locale.fromOpts = function fromOpts(opts) { - return Locale.create(opts.locale, opts.numberingSystem, opts.outputCalendar, opts.defaultToEN); - }; - - Locale.create = function create(locale, numberingSystem, outputCalendar, defaultToEN) { - if (defaultToEN === void 0) { - defaultToEN = false; - } - - var specifiedLocale = locale || Settings.defaultLocale; // the system locale is useful for human readable strings but annoying for parsing/formatting known formats - - var localeR = specifiedLocale || (defaultToEN ? "en-US" : systemLocale()); - var numberingSystemR = numberingSystem || Settings.defaultNumberingSystem; - var outputCalendarR = outputCalendar || Settings.defaultOutputCalendar; - return new Locale(localeR, numberingSystemR, outputCalendarR, specifiedLocale); - }; - - Locale.resetCache = function resetCache() { - sysLocaleCache = null; - intlDTCache = {}; - intlNumCache = {}; - intlRelCache = {}; - }; - - Locale.fromObject = function fromObject(_temp) { - var _ref = _temp === void 0 ? {} : _temp, - locale = _ref.locale, - numberingSystem = _ref.numberingSystem, - outputCalendar = _ref.outputCalendar; - - return Locale.create(locale, numberingSystem, outputCalendar); - }; - - function Locale(locale, numbering, outputCalendar, specifiedLocale) { - var _parseLocaleString = parseLocaleString(locale), - parsedLocale = _parseLocaleString[0], - parsedNumberingSystem = _parseLocaleString[1], - parsedOutputCalendar = _parseLocaleString[2]; - - this.locale = parsedLocale; - this.numberingSystem = numbering || parsedNumberingSystem || null; - this.outputCalendar = outputCalendar || parsedOutputCalendar || null; - this.intl = intlConfigString(this.locale, this.numberingSystem, this.outputCalendar); - this.weekdaysCache = { - format: {}, - standalone: {} - }; - this.monthsCache = { - format: {}, - standalone: {} - }; - this.meridiemCache = null; - this.eraCache = {}; - this.specifiedLocale = specifiedLocale; - this.fastNumbersCached = null; - } - - var _proto4 = Locale.prototype; - - _proto4.listingMode = function listingMode() { - var isActuallyEn = this.isEnglish(); - var hasNoWeirdness = (this.numberingSystem === null || this.numberingSystem === "latn") && (this.outputCalendar === null || this.outputCalendar === "gregory"); - return isActuallyEn && hasNoWeirdness ? "en" : "intl"; - }; - - _proto4.clone = function clone(alts) { - if (!alts || Object.getOwnPropertyNames(alts).length === 0) { - return this; - } else { - return Locale.create(alts.locale || this.specifiedLocale, alts.numberingSystem || this.numberingSystem, alts.outputCalendar || this.outputCalendar, alts.defaultToEN || false); - } - }; - - _proto4.redefaultToEN = function redefaultToEN(alts) { - if (alts === void 0) { - alts = {}; - } - - return this.clone(_extends({}, alts, { - defaultToEN: true - })); - }; - - _proto4.redefaultToSystem = function redefaultToSystem(alts) { - if (alts === void 0) { - alts = {}; - } - - return this.clone(_extends({}, alts, { - defaultToEN: false - })); - }; - - _proto4.months = function months$1(length, format, defaultOK) { - var _this = this; - - if (format === void 0) { - format = false; - } - - if (defaultOK === void 0) { - defaultOK = true; - } - - return listStuff(this, length, defaultOK, months, function () { - var intl = format ? { - month: length, - day: "numeric" - } : { - month: length - }, - formatStr = format ? "format" : "standalone"; - - if (!_this.monthsCache[formatStr][length]) { - _this.monthsCache[formatStr][length] = mapMonths(function (dt) { - return _this.extract(dt, intl, "month"); - }); - } - - return _this.monthsCache[formatStr][length]; - }); - }; - - _proto4.weekdays = function weekdays$1(length, format, defaultOK) { - var _this2 = this; - - if (format === void 0) { - format = false; - } - - if (defaultOK === void 0) { - defaultOK = true; - } - - return listStuff(this, length, defaultOK, weekdays, function () { - var intl = format ? { - weekday: length, - year: "numeric", - month: "long", - day: "numeric" - } : { - weekday: length - }, - formatStr = format ? "format" : "standalone"; - - if (!_this2.weekdaysCache[formatStr][length]) { - _this2.weekdaysCache[formatStr][length] = mapWeekdays(function (dt) { - return _this2.extract(dt, intl, "weekday"); - }); - } - - return _this2.weekdaysCache[formatStr][length]; - }); - }; - - _proto4.meridiems = function meridiems$1(defaultOK) { - var _this3 = this; - - if (defaultOK === void 0) { - defaultOK = true; - } - - return listStuff(this, undefined, defaultOK, function () { - return meridiems; - }, function () { - // In theory there could be aribitrary day periods. We're gonna assume there are exactly two - // for AM and PM. This is probably wrong, but it's makes parsing way easier. - if (!_this3.meridiemCache) { - var intl = { - hour: "numeric", - hourCycle: "h12" - }; - _this3.meridiemCache = [DateTime.utc(2016, 11, 13, 9), DateTime.utc(2016, 11, 13, 19)].map(function (dt) { - return _this3.extract(dt, intl, "dayperiod"); - }); - } - - return _this3.meridiemCache; - }); - }; - - _proto4.eras = function eras$1(length, defaultOK) { - var _this4 = this; - - if (defaultOK === void 0) { - defaultOK = true; - } - - return listStuff(this, length, defaultOK, eras, function () { - var intl = { - era: length - }; // This is problematic. Different calendars are going to define eras totally differently. What I need is the minimum set of dates - // to definitely enumerate them. - - if (!_this4.eraCache[length]) { - _this4.eraCache[length] = [DateTime.utc(-40, 1, 1), DateTime.utc(2017, 1, 1)].map(function (dt) { - return _this4.extract(dt, intl, "era"); - }); - } - - return _this4.eraCache[length]; - }); - }; - - _proto4.extract = function extract(dt, intlOpts, field) { - var df = this.dtFormatter(dt, intlOpts), - results = df.formatToParts(), - matching = results.find(function (m) { - return m.type.toLowerCase() === field; - }); - return matching ? matching.value : null; - }; - - _proto4.numberFormatter = function numberFormatter(opts) { - if (opts === void 0) { - opts = {}; - } - - // this forcesimple option is never used (the only caller short-circuits on it, but it seems safer to leave) - // (in contrast, the rest of the condition is used heavily) - return new PolyNumberFormatter(this.intl, opts.forceSimple || this.fastNumbers, opts); - }; - - _proto4.dtFormatter = function dtFormatter(dt, intlOpts) { - if (intlOpts === void 0) { - intlOpts = {}; - } - - return new PolyDateFormatter(dt, this.intl, intlOpts); - }; - - _proto4.relFormatter = function relFormatter(opts) { - if (opts === void 0) { - opts = {}; - } - - return new PolyRelFormatter(this.intl, this.isEnglish(), opts); - }; - - _proto4.listFormatter = function listFormatter(opts) { - if (opts === void 0) { - opts = {}; - } - - return getCachedLF(this.intl, opts); - }; - - _proto4.isEnglish = function isEnglish() { - return this.locale === "en" || this.locale.toLowerCase() === "en-us" || new Intl.DateTimeFormat(this.intl).resolvedOptions().locale.startsWith("en-us"); - }; - - _proto4.equals = function equals(other) { - return this.locale === other.locale && this.numberingSystem === other.numberingSystem && this.outputCalendar === other.outputCalendar; - }; - - _createClass(Locale, [{ - key: "fastNumbers", - get: function get() { - if (this.fastNumbersCached == null) { - this.fastNumbersCached = supportsFastNumbers(this); - } - - return this.fastNumbersCached; - } - }]); - - return Locale; - }(); - - /* - * This file handles parsing for well-specified formats. Here's how it works: - * Two things go into parsing: a regex to match with and an extractor to take apart the groups in the match. - * An extractor is just a function that takes a regex match array and returns a { year: ..., month: ... } object - * parse() does the work of executing the regex and applying the extractor. It takes multiple regex/extractor pairs to try in sequence. - * Extractors can take a "cursor" representing the offset in the match to look at. This makes it easy to combine extractors. - * combineExtractors() does the work of combining them, keeping track of the cursor through multiple extractions. - * Some extractions are super dumb and simpleParse and fromStrings help DRY them. - */ - - function combineRegexes() { - for (var _len = arguments.length, regexes = new Array(_len), _key = 0; _key < _len; _key++) { - regexes[_key] = arguments[_key]; - } - - var full = regexes.reduce(function (f, r) { - return f + r.source; - }, ""); - return RegExp("^" + full + "$"); - } - - function combineExtractors() { - for (var _len2 = arguments.length, extractors = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { - extractors[_key2] = arguments[_key2]; - } - - return function (m) { - return extractors.reduce(function (_ref, ex) { - var mergedVals = _ref[0], - mergedZone = _ref[1], - cursor = _ref[2]; - - var _ex = ex(m, cursor), - val = _ex[0], - zone = _ex[1], - next = _ex[2]; - - return [_extends({}, mergedVals, val), mergedZone || zone, next]; - }, [{}, null, 1]).slice(0, 2); - }; - } - - function parse(s) { - if (s == null) { - return [null, null]; - } - - for (var _len3 = arguments.length, patterns = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { - patterns[_key3 - 1] = arguments[_key3]; - } - - for (var _i = 0, _patterns = patterns; _i < _patterns.length; _i++) { - var _patterns$_i = _patterns[_i], - regex = _patterns$_i[0], - extractor = _patterns$_i[1]; - var m = regex.exec(s); - - if (m) { - return extractor(m); - } - } - - return [null, null]; - } - - function simpleParse() { - for (var _len4 = arguments.length, keys = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { - keys[_key4] = arguments[_key4]; - } - - return function (match, cursor) { - var ret = {}; - var i; - - for (i = 0; i < keys.length; i++) { - ret[keys[i]] = parseInteger(match[cursor + i]); - } - - return [ret, null, cursor + i]; - }; - } // ISO and SQL parsing - - - var offsetRegex = /(?:(Z)|([+-]\d\d)(?::?(\d\d))?)/, - isoTimeBaseRegex = /(\d\d)(?::?(\d\d)(?::?(\d\d)(?:[.,](\d{1,30}))?)?)?/, - isoTimeRegex = RegExp("" + isoTimeBaseRegex.source + offsetRegex.source + "?"), - isoTimeExtensionRegex = RegExp("(?:T" + isoTimeRegex.source + ")?"), - isoYmdRegex = /([+-]\d{6}|\d{4})(?:-?(\d\d)(?:-?(\d\d))?)?/, - isoWeekRegex = /(\d{4})-?W(\d\d)(?:-?(\d))?/, - isoOrdinalRegex = /(\d{4})-?(\d{3})/, - extractISOWeekData = simpleParse("weekYear", "weekNumber", "weekDay"), - extractISOOrdinalData = simpleParse("year", "ordinal"), - sqlYmdRegex = /(\d{4})-(\d\d)-(\d\d)/, - // dumbed-down version of the ISO one - sqlTimeRegex = RegExp(isoTimeBaseRegex.source + " ?(?:" + offsetRegex.source + "|(" + ianaRegex.source + "))?"), - sqlTimeExtensionRegex = RegExp("(?: " + sqlTimeRegex.source + ")?"); - - function int(match, pos, fallback) { - var m = match[pos]; - return isUndefined(m) ? fallback : parseInteger(m); - } - - function extractISOYmd(match, cursor) { - var item = { - year: int(match, cursor), - month: int(match, cursor + 1, 1), - day: int(match, cursor + 2, 1) - }; - return [item, null, cursor + 3]; - } - - function extractISOTime(match, cursor) { - var item = { - hours: int(match, cursor, 0), - minutes: int(match, cursor + 1, 0), - seconds: int(match, cursor + 2, 0), - milliseconds: parseMillis(match[cursor + 3]) - }; - return [item, null, cursor + 4]; - } - - function extractISOOffset(match, cursor) { - var local = !match[cursor] && !match[cursor + 1], - fullOffset = signedOffset(match[cursor + 1], match[cursor + 2]), - zone = local ? null : FixedOffsetZone.instance(fullOffset); - return [{}, zone, cursor + 3]; - } - - function extractIANAZone(match, cursor) { - var zone = match[cursor] ? IANAZone.create(match[cursor]) : null; - return [{}, zone, cursor + 1]; - } // ISO time parsing - - - var isoTimeOnly = RegExp("^T?" + isoTimeBaseRegex.source + "$"); // ISO duration parsing - - var isoDuration = /^-?P(?:(?:(-?\d{1,9}(?:\.\d{1,9})?)Y)?(?:(-?\d{1,9}(?:\.\d{1,9})?)M)?(?:(-?\d{1,9}(?:\.\d{1,9})?)W)?(?:(-?\d{1,9}(?:\.\d{1,9})?)D)?(?:T(?:(-?\d{1,9}(?:\.\d{1,9})?)H)?(?:(-?\d{1,9}(?:\.\d{1,9})?)M)?(?:(-?\d{1,20})(?:[.,](-?\d{1,9}))?S)?)?)$/; - - function extractISODuration(match) { - var s = match[0], - yearStr = match[1], - monthStr = match[2], - weekStr = match[3], - dayStr = match[4], - hourStr = match[5], - minuteStr = match[6], - secondStr = match[7], - millisecondsStr = match[8]; - var hasNegativePrefix = s[0] === "-"; - var negativeSeconds = secondStr && secondStr[0] === "-"; - - var maybeNegate = function maybeNegate(num, force) { - if (force === void 0) { - force = false; - } - - return num !== undefined && (force || num && hasNegativePrefix) ? -num : num; - }; - - return [{ - years: maybeNegate(parseFloating(yearStr)), - months: maybeNegate(parseFloating(monthStr)), - weeks: maybeNegate(parseFloating(weekStr)), - days: maybeNegate(parseFloating(dayStr)), - hours: maybeNegate(parseFloating(hourStr)), - minutes: maybeNegate(parseFloating(minuteStr)), - seconds: maybeNegate(parseFloating(secondStr), secondStr === "-0"), - milliseconds: maybeNegate(parseMillis(millisecondsStr), negativeSeconds) - }]; - } // These are a little braindead. EDT *should* tell us that we're in, say, America/New_York - // and not just that we're in -240 *right now*. But since I don't think these are used that often - // I'm just going to ignore that - - - var obsOffsets = { - GMT: 0, - EDT: -4 * 60, - EST: -5 * 60, - CDT: -5 * 60, - CST: -6 * 60, - MDT: -6 * 60, - MST: -7 * 60, - PDT: -7 * 60, - PST: -8 * 60 - }; - - function fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr) { - var result = { - year: yearStr.length === 2 ? untruncateYear(parseInteger(yearStr)) : parseInteger(yearStr), - month: monthsShort.indexOf(monthStr) + 1, - day: parseInteger(dayStr), - hour: parseInteger(hourStr), - minute: parseInteger(minuteStr) - }; - if (secondStr) result.second = parseInteger(secondStr); - - if (weekdayStr) { - result.weekday = weekdayStr.length > 3 ? weekdaysLong.indexOf(weekdayStr) + 1 : weekdaysShort.indexOf(weekdayStr) + 1; - } - - return result; - } // RFC 2822/5322 - - - var rfc2822 = /^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),\s)?(\d{1,2})\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d{2,4})\s(\d\d):(\d\d)(?::(\d\d))?\s(?:(UT|GMT|[ECMP][SD]T)|([Zz])|(?:([+-]\d\d)(\d\d)))$/; - - function extractRFC2822(match) { - var weekdayStr = match[1], - dayStr = match[2], - monthStr = match[3], - yearStr = match[4], - hourStr = match[5], - minuteStr = match[6], - secondStr = match[7], - obsOffset = match[8], - milOffset = match[9], - offHourStr = match[10], - offMinuteStr = match[11], - result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); - var offset; - - if (obsOffset) { - offset = obsOffsets[obsOffset]; - } else if (milOffset) { - offset = 0; - } else { - offset = signedOffset(offHourStr, offMinuteStr); - } - - return [result, new FixedOffsetZone(offset)]; - } - - function preprocessRFC2822(s) { - // Remove comments and folding whitespace and replace multiple-spaces with a single space - return s.replace(/\([^)]*\)|[\n\t]/g, " ").replace(/(\s\s+)/g, " ").trim(); - } // http date - - - var rfc1123 = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d\d) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d\d):(\d\d):(\d\d) GMT$/, - rfc850 = /^(Monday|Tuesday|Wedsday|Thursday|Friday|Saturday|Sunday), (\d\d)-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d\d) (\d\d):(\d\d):(\d\d) GMT$/, - ascii = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( \d|\d\d) (\d\d):(\d\d):(\d\d) (\d{4})$/; - - function extractRFC1123Or850(match) { - var weekdayStr = match[1], - dayStr = match[2], - monthStr = match[3], - yearStr = match[4], - hourStr = match[5], - minuteStr = match[6], - secondStr = match[7], - result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); - return [result, FixedOffsetZone.utcInstance]; - } - - function extractASCII(match) { - var weekdayStr = match[1], - monthStr = match[2], - dayStr = match[3], - hourStr = match[4], - minuteStr = match[5], - secondStr = match[6], - yearStr = match[7], - result = fromStrings(weekdayStr, yearStr, monthStr, dayStr, hourStr, minuteStr, secondStr); - return [result, FixedOffsetZone.utcInstance]; - } - - var isoYmdWithTimeExtensionRegex = combineRegexes(isoYmdRegex, isoTimeExtensionRegex); - var isoWeekWithTimeExtensionRegex = combineRegexes(isoWeekRegex, isoTimeExtensionRegex); - var isoOrdinalWithTimeExtensionRegex = combineRegexes(isoOrdinalRegex, isoTimeExtensionRegex); - var isoTimeCombinedRegex = combineRegexes(isoTimeRegex); - var extractISOYmdTimeAndOffset = combineExtractors(extractISOYmd, extractISOTime, extractISOOffset); - var extractISOWeekTimeAndOffset = combineExtractors(extractISOWeekData, extractISOTime, extractISOOffset); - var extractISOOrdinalDateAndTime = combineExtractors(extractISOOrdinalData, extractISOTime, extractISOOffset); - var extractISOTimeAndOffset = combineExtractors(extractISOTime, extractISOOffset); - /** - * @private - */ - - function parseISODate(s) { - return parse(s, [isoYmdWithTimeExtensionRegex, extractISOYmdTimeAndOffset], [isoWeekWithTimeExtensionRegex, extractISOWeekTimeAndOffset], [isoOrdinalWithTimeExtensionRegex, extractISOOrdinalDateAndTime], [isoTimeCombinedRegex, extractISOTimeAndOffset]); - } - function parseRFC2822Date(s) { - return parse(preprocessRFC2822(s), [rfc2822, extractRFC2822]); - } - function parseHTTPDate(s) { - return parse(s, [rfc1123, extractRFC1123Or850], [rfc850, extractRFC1123Or850], [ascii, extractASCII]); - } - function parseISODuration(s) { - return parse(s, [isoDuration, extractISODuration]); - } - var extractISOTimeOnly = combineExtractors(extractISOTime); - function parseISOTimeOnly(s) { - return parse(s, [isoTimeOnly, extractISOTimeOnly]); - } - var sqlYmdWithTimeExtensionRegex = combineRegexes(sqlYmdRegex, sqlTimeExtensionRegex); - var sqlTimeCombinedRegex = combineRegexes(sqlTimeRegex); - var extractISOYmdTimeOffsetAndIANAZone = combineExtractors(extractISOYmd, extractISOTime, extractISOOffset, extractIANAZone); - var extractISOTimeOffsetAndIANAZone = combineExtractors(extractISOTime, extractISOOffset, extractIANAZone); - function parseSQL(s) { - return parse(s, [sqlYmdWithTimeExtensionRegex, extractISOYmdTimeOffsetAndIANAZone], [sqlTimeCombinedRegex, extractISOTimeOffsetAndIANAZone]); - } - - var INVALID$2 = "Invalid Duration"; // unit conversion constants - - var lowOrderMatrix = { - weeks: { - days: 7, - hours: 7 * 24, - minutes: 7 * 24 * 60, - seconds: 7 * 24 * 60 * 60, - milliseconds: 7 * 24 * 60 * 60 * 1000 - }, - days: { - hours: 24, - minutes: 24 * 60, - seconds: 24 * 60 * 60, - milliseconds: 24 * 60 * 60 * 1000 - }, - hours: { - minutes: 60, - seconds: 60 * 60, - milliseconds: 60 * 60 * 1000 - }, - minutes: { - seconds: 60, - milliseconds: 60 * 1000 - }, - seconds: { - milliseconds: 1000 - } - }, - casualMatrix = _extends({ - years: { - quarters: 4, - months: 12, - weeks: 52, - days: 365, - hours: 365 * 24, - minutes: 365 * 24 * 60, - seconds: 365 * 24 * 60 * 60, - milliseconds: 365 * 24 * 60 * 60 * 1000 - }, - quarters: { - months: 3, - weeks: 13, - days: 91, - hours: 91 * 24, - minutes: 91 * 24 * 60, - seconds: 91 * 24 * 60 * 60, - milliseconds: 91 * 24 * 60 * 60 * 1000 - }, - months: { - weeks: 4, - days: 30, - hours: 30 * 24, - minutes: 30 * 24 * 60, - seconds: 30 * 24 * 60 * 60, - milliseconds: 30 * 24 * 60 * 60 * 1000 - } - }, lowOrderMatrix), - daysInYearAccurate = 146097.0 / 400, - daysInMonthAccurate = 146097.0 / 4800, - accurateMatrix = _extends({ - years: { - quarters: 4, - months: 12, - weeks: daysInYearAccurate / 7, - days: daysInYearAccurate, - hours: daysInYearAccurate * 24, - minutes: daysInYearAccurate * 24 * 60, - seconds: daysInYearAccurate * 24 * 60 * 60, - milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1000 - }, - quarters: { - months: 3, - weeks: daysInYearAccurate / 28, - days: daysInYearAccurate / 4, - hours: daysInYearAccurate * 24 / 4, - minutes: daysInYearAccurate * 24 * 60 / 4, - seconds: daysInYearAccurate * 24 * 60 * 60 / 4, - milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1000 / 4 - }, - months: { - weeks: daysInMonthAccurate / 7, - days: daysInMonthAccurate, - hours: daysInMonthAccurate * 24, - minutes: daysInMonthAccurate * 24 * 60, - seconds: daysInMonthAccurate * 24 * 60 * 60, - milliseconds: daysInMonthAccurate * 24 * 60 * 60 * 1000 - } - }, lowOrderMatrix); // units ordered by size - - var orderedUnits$1 = ["years", "quarters", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"]; - var reverseUnits = orderedUnits$1.slice(0).reverse(); // clone really means "create another instance just like this one, but with these changes" - - function clone$1(dur, alts, clear) { - if (clear === void 0) { - clear = false; - } - - // deep merge for vals - var conf = { - values: clear ? alts.values : _extends({}, dur.values, alts.values || {}), - loc: dur.loc.clone(alts.loc), - conversionAccuracy: alts.conversionAccuracy || dur.conversionAccuracy - }; - return new Duration(conf); - } - - function antiTrunc(n) { - return n < 0 ? Math.floor(n) : Math.ceil(n); - } // NB: mutates parameters - - - function convert(matrix, fromMap, fromUnit, toMap, toUnit) { - var conv = matrix[toUnit][fromUnit], - raw = fromMap[fromUnit] / conv, - sameSign = Math.sign(raw) === Math.sign(toMap[toUnit]), - // ok, so this is wild, but see the matrix in the tests - added = !sameSign && toMap[toUnit] !== 0 && Math.abs(raw) <= 1 ? antiTrunc(raw) : Math.trunc(raw); - toMap[toUnit] += added; - fromMap[fromUnit] -= added * conv; - } // NB: mutates parameters - - - function normalizeValues(matrix, vals) { - reverseUnits.reduce(function (previous, current) { - if (!isUndefined(vals[current])) { - if (previous) { - convert(matrix, vals, previous, vals, current); - } - - return current; - } else { - return previous; - } - }, null); - } - /** - * A Duration object represents a period of time, like "2 months" or "1 day, 1 hour". Conceptually, it's just a map of units to their quantities, accompanied by some additional configuration and methods for creating, parsing, interrogating, transforming, and formatting them. They can be used on their own or in conjunction with other Luxon types; for example, you can use {@link DateTime#plus} to add a Duration object to a DateTime, producing another DateTime. - * - * Here is a brief overview of commonly used methods and getters in Duration: - * - * * **Creation** To create a Duration, use {@link Duration#fromMillis}, {@link Duration#fromObject}, or {@link Duration#fromISO}. - * * **Unit values** See the {@link Duration#years}, {@link Duration.months}, {@link Duration#weeks}, {@link Duration#days}, {@link Duration#hours}, {@link Duration#minutes}, {@link Duration#seconds}, {@link Duration#milliseconds} accessors. - * * **Configuration** See {@link Duration#locale} and {@link Duration#numberingSystem} accessors. - * * **Transformation** To create new Durations out of old ones use {@link Duration#plus}, {@link Duration#minus}, {@link Duration#normalize}, {@link Duration#set}, {@link Duration#reconfigure}, {@link Duration#shiftTo}, and {@link Duration#negate}. - * * **Output** To convert the Duration into other representations, see {@link Duration#as}, {@link Duration#toISO}, {@link Duration#toFormat}, and {@link Duration#toJSON} - * - * There's are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation. - */ - - - var Duration = /*#__PURE__*/function () { - /** - * @private - */ - function Duration(config) { - var accurate = config.conversionAccuracy === "longterm" || false; - /** - * @access private - */ - - this.values = config.values; - /** - * @access private - */ - - this.loc = config.loc || Locale.create(); - /** - * @access private - */ - - this.conversionAccuracy = accurate ? "longterm" : "casual"; - /** - * @access private - */ - - this.invalid = config.invalid || null; - /** - * @access private - */ - - this.matrix = accurate ? accurateMatrix : casualMatrix; - /** - * @access private - */ - - this.isLuxonDuration = true; - } - /** - * Create Duration from a number of milliseconds. - * @param {number} count of milliseconds - * @param {Object} opts - options for parsing - * @param {string} [opts.locale='en-US'] - the locale to use - * @param {string} opts.numberingSystem - the numbering system to use - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @return {Duration} - */ - - - Duration.fromMillis = function fromMillis(count, opts) { - return Duration.fromObject({ - milliseconds: count - }, opts); - } - /** - * Create a Duration from a JavaScript object with keys like 'years' and 'hours'. - * If this object is empty then a zero milliseconds duration is returned. - * @param {Object} obj - the object to create the DateTime from - * @param {number} obj.years - * @param {number} obj.quarters - * @param {number} obj.months - * @param {number} obj.weeks - * @param {number} obj.days - * @param {number} obj.hours - * @param {number} obj.minutes - * @param {number} obj.seconds - * @param {number} obj.milliseconds - * @param {Object} [opts=[]] - options for creating this Duration - * @param {string} [opts.locale='en-US'] - the locale to use - * @param {string} opts.numberingSystem - the numbering system to use - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @return {Duration} - */ - ; - - Duration.fromObject = function fromObject(obj, opts) { - if (opts === void 0) { - opts = {}; - } - - if (obj == null || typeof obj !== "object") { - throw new InvalidArgumentError("Duration.fromObject: argument expected to be an object, got " + (obj === null ? "null" : typeof obj)); - } - - return new Duration({ - values: normalizeObject(obj, Duration.normalizeUnit), - loc: Locale.fromObject(opts), - conversionAccuracy: opts.conversionAccuracy - }); - } - /** - * Create a Duration from DurationLike. - * - * @param {Object | number | Duration} durationLike - * One of: - * - object with keys like 'years' and 'hours'. - * - number representing milliseconds - * - Duration instance - * @return {Duration} - */ - ; - - Duration.fromDurationLike = function fromDurationLike(durationLike) { - if (isNumber(durationLike)) { - return Duration.fromMillis(durationLike); - } else if (Duration.isDuration(durationLike)) { - return durationLike; - } else if (typeof durationLike === "object") { - return Duration.fromObject(durationLike); - } else { - throw new InvalidArgumentError("Unknown duration argument " + durationLike + " of type " + typeof durationLike); - } - } - /** - * Create a Duration from an ISO 8601 duration string. - * @param {string} text - text to parse - * @param {Object} opts - options for parsing - * @param {string} [opts.locale='en-US'] - the locale to use - * @param {string} opts.numberingSystem - the numbering system to use - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @see https://en.wikipedia.org/wiki/ISO_8601#Durations - * @example Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 } - * @example Duration.fromISO('PT23H').toObject() //=> { hours: 23 } - * @example Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 } - * @return {Duration} - */ - ; - - Duration.fromISO = function fromISO(text, opts) { - var _parseISODuration = parseISODuration(text), - parsed = _parseISODuration[0]; - - if (parsed) { - return Duration.fromObject(parsed, opts); - } else { - return Duration.invalid("unparsable", "the input \"" + text + "\" can't be parsed as ISO 8601"); - } - } - /** - * Create a Duration from an ISO 8601 time string. - * @param {string} text - text to parse - * @param {Object} opts - options for parsing - * @param {string} [opts.locale='en-US'] - the locale to use - * @param {string} opts.numberingSystem - the numbering system to use - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @see https://en.wikipedia.org/wiki/ISO_8601#Times - * @example Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 } - * @example Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } - * @example Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } - * @example Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } - * @example Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 } - * @return {Duration} - */ - ; - - Duration.fromISOTime = function fromISOTime(text, opts) { - var _parseISOTimeOnly = parseISOTimeOnly(text), - parsed = _parseISOTimeOnly[0]; - - if (parsed) { - return Duration.fromObject(parsed, opts); - } else { - return Duration.invalid("unparsable", "the input \"" + text + "\" can't be parsed as ISO 8601"); - } - } - /** - * Create an invalid Duration. - * @param {string} reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent - * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information - * @return {Duration} - */ - ; - - Duration.invalid = function invalid(reason, explanation) { - if (explanation === void 0) { - explanation = null; - } - - if (!reason) { - throw new InvalidArgumentError("need to specify a reason the Duration is invalid"); - } - - var invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); - - if (Settings.throwOnInvalid) { - throw new InvalidDurationError(invalid); - } else { - return new Duration({ - invalid: invalid - }); - } - } - /** - * @private - */ - ; - - Duration.normalizeUnit = function normalizeUnit(unit) { - var normalized = { - year: "years", - years: "years", - quarter: "quarters", - quarters: "quarters", - month: "months", - months: "months", - week: "weeks", - weeks: "weeks", - day: "days", - days: "days", - hour: "hours", - hours: "hours", - minute: "minutes", - minutes: "minutes", - second: "seconds", - seconds: "seconds", - millisecond: "milliseconds", - milliseconds: "milliseconds" - }[unit ? unit.toLowerCase() : unit]; - if (!normalized) throw new InvalidUnitError(unit); - return normalized; - } - /** - * Check if an object is a Duration. Works across context boundaries - * @param {object} o - * @return {boolean} - */ - ; - - Duration.isDuration = function isDuration(o) { - return o && o.isLuxonDuration || false; - } - /** - * Get the locale of a Duration, such 'en-GB' - * @type {string} - */ - ; - - var _proto = Duration.prototype; - - /** - * Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens: - * * `S` for milliseconds - * * `s` for seconds - * * `m` for minutes - * * `h` for hours - * * `d` for days - * * `M` for months - * * `y` for years - * Notes: - * * Add padding by repeating the token, e.g. "yy" pads the years to two digits, "hhhh" pads the hours out to four digits - * * The duration will be converted to the set of units in the format string using {@link Duration#shiftTo} and the Durations's conversion accuracy setting. - * @param {string} fmt - the format string - * @param {Object} opts - options - * @param {boolean} [opts.floor=true] - floor numerical values - * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2" - * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002" - * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000" - * @return {string} - */ - _proto.toFormat = function toFormat(fmt, opts) { - if (opts === void 0) { - opts = {}; - } - - // reverse-compat since 1.2; we always round down now, never up, and we do it by default - var fmtOpts = _extends({}, opts, { - floor: opts.round !== false && opts.floor !== false - }); - - return this.isValid ? Formatter.create(this.loc, fmtOpts).formatDurationFromString(this, fmt) : INVALID$2; - } - /** - * Returns a string representation of a Duration with all units included - * To modify its behavior use the `listStyle` and any Intl.NumberFormat option, though `unitDisplay` is especially relevant. See {@link Intl.NumberFormat}. - * @param opts - On option object to override the formatting. Accepts the same keys as the options parameter of the native `Int.NumberFormat` constructor, as well as `listStyle`. - * @example - * ```js - * var dur = Duration.fromObject({ days: 1, hours: 5, minutes: 6 }) - * dur.toHuman() //=> '1 day, 5 hours, 6 minutes' - * dur.toHuman({ listStyle: "long" }) //=> '1 day, 5 hours, and 6 minutes' - * dur.toHuman({ unitDisplay: "short" }) //=> '1 day, 5 hr, 6 min' - * ``` - */ - ; - - _proto.toHuman = function toHuman(opts) { - var _this = this; - - if (opts === void 0) { - opts = {}; - } - - var l = orderedUnits$1.map(function (unit) { - var val = _this.values[unit]; - - if (isUndefined(val)) { - return null; - } - - return _this.loc.numberFormatter(_extends({ - style: "unit", - unitDisplay: "long" - }, opts, { - unit: unit.slice(0, -1) - })).format(val); - }).filter(function (n) { - return n; - }); - return this.loc.listFormatter(_extends({ - type: "conjunction", - style: opts.listStyle || "narrow" - }, opts)).format(l); - } - /** - * Returns a JavaScript object with this Duration's values. - * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 } - * @return {Object} - */ - ; - - _proto.toObject = function toObject() { - if (!this.isValid) return {}; - return _extends({}, this.values); - } - /** - * Returns an ISO 8601-compliant string representation of this Duration. - * @see https://en.wikipedia.org/wiki/ISO_8601#Durations - * @example Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S' - * @example Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S' - * @example Duration.fromObject({ months: 5 }).toISO() //=> 'P5M' - * @example Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M' - * @example Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S' - * @return {string} - */ - ; - - _proto.toISO = function toISO() { - // we could use the formatter, but this is an easier way to get the minimum string - if (!this.isValid) return null; - var s = "P"; - if (this.years !== 0) s += this.years + "Y"; - if (this.months !== 0 || this.quarters !== 0) s += this.months + this.quarters * 3 + "M"; - if (this.weeks !== 0) s += this.weeks + "W"; - if (this.days !== 0) s += this.days + "D"; - if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0) s += "T"; - if (this.hours !== 0) s += this.hours + "H"; - if (this.minutes !== 0) s += this.minutes + "M"; - if (this.seconds !== 0 || this.milliseconds !== 0) // this will handle "floating point madness" by removing extra decimal places - // https://stackoverflow.com/questions/588004/is-floating-point-math-broken - s += roundTo(this.seconds + this.milliseconds / 1000, 3) + "S"; - if (s === "P") s += "T0S"; - return s; - } - /** - * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day. - * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours. - * @see https://en.wikipedia.org/wiki/ISO_8601#Times - * @param {Object} opts - options - * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 - * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 - * @param {boolean} [opts.includePrefix=false] - include the `T` prefix - * @param {string} [opts.format='extended'] - choose between the basic and extended format - * @example Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000' - * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00' - * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00' - * @example Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000' - * @example Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000' - * @return {string} - */ - ; - - _proto.toISOTime = function toISOTime(opts) { - if (opts === void 0) { - opts = {}; - } - - if (!this.isValid) return null; - var millis = this.toMillis(); - if (millis < 0 || millis >= 86400000) return null; - opts = _extends({ - suppressMilliseconds: false, - suppressSeconds: false, - includePrefix: false, - format: "extended" - }, opts); - var value = this.shiftTo("hours", "minutes", "seconds", "milliseconds"); - var fmt = opts.format === "basic" ? "hhmm" : "hh:mm"; - - if (!opts.suppressSeconds || value.seconds !== 0 || value.milliseconds !== 0) { - fmt += opts.format === "basic" ? "ss" : ":ss"; - - if (!opts.suppressMilliseconds || value.milliseconds !== 0) { - fmt += ".SSS"; - } - } - - var str = value.toFormat(fmt); - - if (opts.includePrefix) { - str = "T" + str; - } - - return str; - } - /** - * Returns an ISO 8601 representation of this Duration appropriate for use in JSON. - * @return {string} - */ - ; - - _proto.toJSON = function toJSON() { - return this.toISO(); - } - /** - * Returns an ISO 8601 representation of this Duration appropriate for use in debugging. - * @return {string} - */ - ; - - _proto.toString = function toString() { - return this.toISO(); - } - /** - * Returns an milliseconds value of this Duration. - * @return {number} - */ - ; - - _proto.toMillis = function toMillis() { - return this.as("milliseconds"); - } - /** - * Returns an milliseconds value of this Duration. Alias of {@link toMillis} - * @return {number} - */ - ; - - _proto.valueOf = function valueOf() { - return this.toMillis(); - } - /** - * Make this Duration longer by the specified amount. Return a newly-constructed Duration. - * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() - * @return {Duration} - */ - ; - - _proto.plus = function plus(duration) { - if (!this.isValid) return this; - var dur = Duration.fromDurationLike(duration), - result = {}; - - for (var _iterator = _createForOfIteratorHelperLoose(orderedUnits$1), _step; !(_step = _iterator()).done;) { - var k = _step.value; - - if (hasOwnProperty(dur.values, k) || hasOwnProperty(this.values, k)) { - result[k] = dur.get(k) + this.get(k); - } - } - - return clone$1(this, { - values: result - }, true); - } - /** - * Make this Duration shorter by the specified amount. Return a newly-constructed Duration. - * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() - * @return {Duration} - */ - ; - - _proto.minus = function minus(duration) { - if (!this.isValid) return this; - var dur = Duration.fromDurationLike(duration); - return this.plus(dur.negate()); - } - /** - * Scale this Duration by the specified amount. Return a newly-constructed Duration. - * @param {function} fn - The function to apply to each unit. Arity is 1 or 2: the value of the unit and, optionally, the unit name. Must return a number. - * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits(x => x * 2) //=> { hours: 2, minutes: 60 } - * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnits((x, u) => u === "hour" ? x * 2 : x) //=> { hours: 2, minutes: 30 } - * @return {Duration} - */ - ; - - _proto.mapUnits = function mapUnits(fn) { - if (!this.isValid) return this; - var result = {}; - - for (var _i = 0, _Object$keys = Object.keys(this.values); _i < _Object$keys.length; _i++) { - var k = _Object$keys[_i]; - result[k] = asNumber(fn(this.values[k], k)); - } - - return clone$1(this, { - values: result - }, true); - } - /** - * Get the value of unit. - * @param {string} unit - a unit such as 'minute' or 'day' - * @example Duration.fromObject({years: 2, days: 3}).get('years') //=> 2 - * @example Duration.fromObject({years: 2, days: 3}).get('months') //=> 0 - * @example Duration.fromObject({years: 2, days: 3}).get('days') //=> 3 - * @return {number} - */ - ; - - _proto.get = function get(unit) { - return this[Duration.normalizeUnit(unit)]; - } - /** - * "Set" the values of specified units. Return a newly-constructed Duration. - * @param {Object} values - a mapping of units to numbers - * @example dur.set({ years: 2017 }) - * @example dur.set({ hours: 8, minutes: 30 }) - * @return {Duration} - */ - ; - - _proto.set = function set(values) { - if (!this.isValid) return this; - - var mixed = _extends({}, this.values, normalizeObject(values, Duration.normalizeUnit)); - - return clone$1(this, { - values: mixed - }); - } - /** - * "Set" the locale and/or numberingSystem. Returns a newly-constructed Duration. - * @example dur.reconfigure({ locale: 'en-GB' }) - * @return {Duration} - */ - ; - - _proto.reconfigure = function reconfigure(_temp) { - var _ref = _temp === void 0 ? {} : _temp, - locale = _ref.locale, - numberingSystem = _ref.numberingSystem, - conversionAccuracy = _ref.conversionAccuracy; - - var loc = this.loc.clone({ - locale: locale, - numberingSystem: numberingSystem - }), - opts = { - loc: loc - }; - - if (conversionAccuracy) { - opts.conversionAccuracy = conversionAccuracy; - } - - return clone$1(this, opts); - } - /** - * Return the length of the duration in the specified unit. - * @param {string} unit - a unit such as 'minutes' or 'days' - * @example Duration.fromObject({years: 1}).as('days') //=> 365 - * @example Duration.fromObject({years: 1}).as('months') //=> 12 - * @example Duration.fromObject({hours: 60}).as('days') //=> 2.5 - * @return {number} - */ - ; - - _proto.as = function as(unit) { - return this.isValid ? this.shiftTo(unit).get(unit) : NaN; - } - /** - * Reduce this Duration to its canonical representation in its current units. - * @example Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 } - * @example Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 } - * @return {Duration} - */ - ; - - _proto.normalize = function normalize() { - if (!this.isValid) return this; - var vals = this.toObject(); - normalizeValues(this.matrix, vals); - return clone$1(this, { - values: vals - }, true); - } - /** - * Convert this Duration into its representation in a different set of units. - * @example Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 } - * @return {Duration} - */ - ; - - _proto.shiftTo = function shiftTo() { - for (var _len = arguments.length, units = new Array(_len), _key = 0; _key < _len; _key++) { - units[_key] = arguments[_key]; - } - - if (!this.isValid) return this; - - if (units.length === 0) { - return this; - } - - units = units.map(function (u) { - return Duration.normalizeUnit(u); - }); - var built = {}, - accumulated = {}, - vals = this.toObject(); - var lastUnit; - - for (var _iterator2 = _createForOfIteratorHelperLoose(orderedUnits$1), _step2; !(_step2 = _iterator2()).done;) { - var k = _step2.value; - - if (units.indexOf(k) >= 0) { - lastUnit = k; - var own = 0; // anything we haven't boiled down yet should get boiled to this unit - - for (var ak in accumulated) { - own += this.matrix[ak][k] * accumulated[ak]; - accumulated[ak] = 0; - } // plus anything that's already in this unit - - - if (isNumber(vals[k])) { - own += vals[k]; - } - - var i = Math.trunc(own); - built[k] = i; - accumulated[k] = (own * 1000 - i * 1000) / 1000; // plus anything further down the chain that should be rolled up in to this - - for (var down in vals) { - if (orderedUnits$1.indexOf(down) > orderedUnits$1.indexOf(k)) { - convert(this.matrix, vals, down, built, k); - } - } // otherwise, keep it in the wings to boil it later - - } else if (isNumber(vals[k])) { - accumulated[k] = vals[k]; - } - } // anything leftover becomes the decimal for the last unit - // lastUnit must be defined since units is not empty - - - for (var key in accumulated) { - if (accumulated[key] !== 0) { - built[lastUnit] += key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key]; - } - } - - return clone$1(this, { - values: built - }, true).normalize(); - } - /** - * Return the negative of this Duration. - * @example Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject() //=> { hours: -1, seconds: -30 } - * @return {Duration} - */ - ; - - _proto.negate = function negate() { - if (!this.isValid) return this; - var negated = {}; - - for (var _i2 = 0, _Object$keys2 = Object.keys(this.values); _i2 < _Object$keys2.length; _i2++) { - var k = _Object$keys2[_i2]; - negated[k] = this.values[k] === 0 ? 0 : -this.values[k]; - } - - return clone$1(this, { - values: negated - }, true); - } - /** - * Get the years. - * @type {number} - */ - ; - - /** - * Equality check - * Two Durations are equal iff they have the same units and the same values for each unit. - * @param {Duration} other - * @return {boolean} - */ - _proto.equals = function equals(other) { - if (!this.isValid || !other.isValid) { - return false; - } - - if (!this.loc.equals(other.loc)) { - return false; - } - - function eq(v1, v2) { - // Consider 0 and undefined as equal - if (v1 === undefined || v1 === 0) return v2 === undefined || v2 === 0; - return v1 === v2; - } - - for (var _iterator3 = _createForOfIteratorHelperLoose(orderedUnits$1), _step3; !(_step3 = _iterator3()).done;) { - var u = _step3.value; - - if (!eq(this.values[u], other.values[u])) { - return false; - } - } - - return true; - }; - - _createClass(Duration, [{ - key: "locale", - get: function get() { - return this.isValid ? this.loc.locale : null; - } - /** - * Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration - * - * @type {string} - */ - - }, { - key: "numberingSystem", - get: function get() { - return this.isValid ? this.loc.numberingSystem : null; - } - }, { - key: "years", - get: function get() { - return this.isValid ? this.values.years || 0 : NaN; - } - /** - * Get the quarters. - * @type {number} - */ - - }, { - key: "quarters", - get: function get() { - return this.isValid ? this.values.quarters || 0 : NaN; - } - /** - * Get the months. - * @type {number} - */ - - }, { - key: "months", - get: function get() { - return this.isValid ? this.values.months || 0 : NaN; - } - /** - * Get the weeks - * @type {number} - */ - - }, { - key: "weeks", - get: function get() { - return this.isValid ? this.values.weeks || 0 : NaN; - } - /** - * Get the days. - * @type {number} - */ - - }, { - key: "days", - get: function get() { - return this.isValid ? this.values.days || 0 : NaN; - } - /** - * Get the hours. - * @type {number} - */ - - }, { - key: "hours", - get: function get() { - return this.isValid ? this.values.hours || 0 : NaN; - } - /** - * Get the minutes. - * @type {number} - */ - - }, { - key: "minutes", - get: function get() { - return this.isValid ? this.values.minutes || 0 : NaN; - } - /** - * Get the seconds. - * @return {number} - */ - - }, { - key: "seconds", - get: function get() { - return this.isValid ? this.values.seconds || 0 : NaN; - } - /** - * Get the milliseconds. - * @return {number} - */ - - }, { - key: "milliseconds", - get: function get() { - return this.isValid ? this.values.milliseconds || 0 : NaN; - } - /** - * Returns whether the Duration is invalid. Invalid durations are returned by diff operations - * on invalid DateTimes or Intervals. - * @return {boolean} - */ - - }, { - key: "isValid", - get: function get() { - return this.invalid === null; - } - /** - * Returns an error code if this Duration became invalid, or null if the Duration is valid - * @return {string} - */ - - }, { - key: "invalidReason", - get: function get() { - return this.invalid ? this.invalid.reason : null; - } - /** - * Returns an explanation of why this Duration became invalid, or null if the Duration is valid - * @type {string} - */ - - }, { - key: "invalidExplanation", - get: function get() { - return this.invalid ? this.invalid.explanation : null; - } - }]); - - return Duration; - }(); - - var INVALID$1 = "Invalid Interval"; // checks if the start is equal to or before the end - - function validateStartEnd(start, end) { - if (!start || !start.isValid) { - return Interval.invalid("missing or invalid start"); - } else if (!end || !end.isValid) { - return Interval.invalid("missing or invalid end"); - } else if (end < start) { - return Interval.invalid("end before start", "The end of an interval must be after its start, but you had start=" + start.toISO() + " and end=" + end.toISO()); - } else { - return null; - } - } - /** - * An Interval object represents a half-open interval of time, where each endpoint is a {@link DateTime}. Conceptually, it's a container for those two endpoints, accompanied by methods for creating, parsing, interrogating, comparing, transforming, and formatting them. - * - * Here is a brief overview of the most commonly used methods and getters in Interval: - * - * * **Creation** To create an Interval, use {@link Interval#fromDateTimes}, {@link Interval#after}, {@link Interval#before}, or {@link Interval#fromISO}. - * * **Accessors** Use {@link Interval#start} and {@link Interval#end} to get the start and end. - * * **Interrogation** To analyze the Interval, use {@link Interval#count}, {@link Interval#length}, {@link Interval#hasSame}, {@link Interval#contains}, {@link Interval#isAfter}, or {@link Interval#isBefore}. - * * **Transformation** To create other Intervals out of this one, use {@link Interval#set}, {@link Interval#splitAt}, {@link Interval#splitBy}, {@link Interval#divideEqually}, {@link Interval#merge}, {@link Interval#xor}, {@link Interval#union}, {@link Interval#intersection}, or {@link Interval#difference}. - * * **Comparison** To compare this Interval to another one, use {@link Interval#equals}, {@link Interval#overlaps}, {@link Interval#abutsStart}, {@link Interval#abutsEnd}, {@link Interval#engulfs} - * * **Output** To convert the Interval into other representations, see {@link Interval#toString}, {@link Interval#toISO}, {@link Interval#toISODate}, {@link Interval#toISOTime}, {@link Interval#toFormat}, and {@link Interval#toDuration}. - */ - - - var Interval = /*#__PURE__*/function () { - /** - * @private - */ - function Interval(config) { - /** - * @access private - */ - this.s = config.start; - /** - * @access private - */ - - this.e = config.end; - /** - * @access private - */ - - this.invalid = config.invalid || null; - /** - * @access private - */ - - this.isLuxonInterval = true; - } - /** - * Create an invalid Interval. - * @param {string} reason - simple string of why this Interval is invalid. Should not contain parameters or anything else data-dependent - * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information - * @return {Interval} - */ - - - Interval.invalid = function invalid(reason, explanation) { - if (explanation === void 0) { - explanation = null; - } - - if (!reason) { - throw new InvalidArgumentError("need to specify a reason the Interval is invalid"); - } - - var invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); - - if (Settings.throwOnInvalid) { - throw new InvalidIntervalError(invalid); - } else { - return new Interval({ - invalid: invalid - }); - } - } - /** - * Create an Interval from a start DateTime and an end DateTime. Inclusive of the start but not the end. - * @param {DateTime|Date|Object} start - * @param {DateTime|Date|Object} end - * @return {Interval} - */ - ; - - Interval.fromDateTimes = function fromDateTimes(start, end) { - var builtStart = friendlyDateTime(start), - builtEnd = friendlyDateTime(end); - var validateError = validateStartEnd(builtStart, builtEnd); - - if (validateError == null) { - return new Interval({ - start: builtStart, - end: builtEnd - }); - } else { - return validateError; - } - } - /** - * Create an Interval from a start DateTime and a Duration to extend to. - * @param {DateTime|Date|Object} start - * @param {Duration|Object|number} duration - the length of the Interval. - * @return {Interval} - */ - ; - - Interval.after = function after(start, duration) { - var dur = Duration.fromDurationLike(duration), - dt = friendlyDateTime(start); - return Interval.fromDateTimes(dt, dt.plus(dur)); - } - /** - * Create an Interval from an end DateTime and a Duration to extend backwards to. - * @param {DateTime|Date|Object} end - * @param {Duration|Object|number} duration - the length of the Interval. - * @return {Interval} - */ - ; - - Interval.before = function before(end, duration) { - var dur = Duration.fromDurationLike(duration), - dt = friendlyDateTime(end); - return Interval.fromDateTimes(dt.minus(dur), dt); - } - /** - * Create an Interval from an ISO 8601 string. - * Accepts `/`, `/`, and `/` formats. - * @param {string} text - the ISO string to parse - * @param {Object} [opts] - options to pass {@link DateTime#fromISO} and optionally {@link Duration#fromISO} - * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @return {Interval} - */ - ; - - Interval.fromISO = function fromISO(text, opts) { - var _split = (text || "").split("/", 2), - s = _split[0], - e = _split[1]; - - if (s && e) { - var start, startIsValid; - - try { - start = DateTime.fromISO(s, opts); - startIsValid = start.isValid; - } catch (e) { - startIsValid = false; - } - - var end, endIsValid; - - try { - end = DateTime.fromISO(e, opts); - endIsValid = end.isValid; - } catch (e) { - endIsValid = false; - } - - if (startIsValid && endIsValid) { - return Interval.fromDateTimes(start, end); - } - - if (startIsValid) { - var dur = Duration.fromISO(e, opts); - - if (dur.isValid) { - return Interval.after(start, dur); - } - } else if (endIsValid) { - var _dur = Duration.fromISO(s, opts); - - if (_dur.isValid) { - return Interval.before(end, _dur); - } - } - } - - return Interval.invalid("unparsable", "the input \"" + text + "\" can't be parsed as ISO 8601"); - } - /** - * Check if an object is an Interval. Works across context boundaries - * @param {object} o - * @return {boolean} - */ - ; - - Interval.isInterval = function isInterval(o) { - return o && o.isLuxonInterval || false; - } - /** - * Returns the start of the Interval - * @type {DateTime} - */ - ; - - var _proto = Interval.prototype; - - /** - * Returns the length of the Interval in the specified unit. - * @param {string} unit - the unit (such as 'hours' or 'days') to return the length in. - * @return {number} - */ - _proto.length = function length(unit) { - if (unit === void 0) { - unit = "milliseconds"; - } - - return this.isValid ? this.toDuration.apply(this, [unit]).get(unit) : NaN; - } - /** - * Returns the count of minutes, hours, days, months, or years included in the Interval, even in part. - * Unlike {@link Interval#length} this counts sections of the calendar, not periods of time, e.g. specifying 'day' - * asks 'what dates are included in this interval?', not 'how many days long is this interval?' - * @param {string} [unit='milliseconds'] - the unit of time to count. - * @return {number} - */ - ; - - _proto.count = function count(unit) { - if (unit === void 0) { - unit = "milliseconds"; - } - - if (!this.isValid) return NaN; - var start = this.start.startOf(unit), - end = this.end.startOf(unit); - return Math.floor(end.diff(start, unit).get(unit)) + 1; - } - /** - * Returns whether this Interval's start and end are both in the same unit of time - * @param {string} unit - the unit of time to check sameness on - * @return {boolean} - */ - ; - - _proto.hasSame = function hasSame(unit) { - return this.isValid ? this.isEmpty() || this.e.minus(1).hasSame(this.s, unit) : false; - } - /** - * Return whether this Interval has the same start and end DateTimes. - * @return {boolean} - */ - ; - - _proto.isEmpty = function isEmpty() { - return this.s.valueOf() === this.e.valueOf(); - } - /** - * Return whether this Interval's start is after the specified DateTime. - * @param {DateTime} dateTime - * @return {boolean} - */ - ; - - _proto.isAfter = function isAfter(dateTime) { - if (!this.isValid) return false; - return this.s > dateTime; - } - /** - * Return whether this Interval's end is before the specified DateTime. - * @param {DateTime} dateTime - * @return {boolean} - */ - ; - - _proto.isBefore = function isBefore(dateTime) { - if (!this.isValid) return false; - return this.e <= dateTime; - } - /** - * Return whether this Interval contains the specified DateTime. - * @param {DateTime} dateTime - * @return {boolean} - */ - ; - - _proto.contains = function contains(dateTime) { - if (!this.isValid) return false; - return this.s <= dateTime && this.e > dateTime; - } - /** - * "Sets" the start and/or end dates. Returns a newly-constructed Interval. - * @param {Object} values - the values to set - * @param {DateTime} values.start - the starting DateTime - * @param {DateTime} values.end - the ending DateTime - * @return {Interval} - */ - ; - - _proto.set = function set(_temp) { - var _ref = _temp === void 0 ? {} : _temp, - start = _ref.start, - end = _ref.end; - - if (!this.isValid) return this; - return Interval.fromDateTimes(start || this.s, end || this.e); - } - /** - * Split this Interval at each of the specified DateTimes - * @param {...DateTime} dateTimes - the unit of time to count. - * @return {Array} - */ - ; - - _proto.splitAt = function splitAt() { - var _this = this; - - if (!this.isValid) return []; - - for (var _len = arguments.length, dateTimes = new Array(_len), _key = 0; _key < _len; _key++) { - dateTimes[_key] = arguments[_key]; - } - - var sorted = dateTimes.map(friendlyDateTime).filter(function (d) { - return _this.contains(d); - }).sort(), - results = []; - var s = this.s, - i = 0; - - while (s < this.e) { - var added = sorted[i] || this.e, - next = +added > +this.e ? this.e : added; - results.push(Interval.fromDateTimes(s, next)); - s = next; - i += 1; - } - - return results; - } - /** - * Split this Interval into smaller Intervals, each of the specified length. - * Left over time is grouped into a smaller interval - * @param {Duration|Object|number} duration - The length of each resulting interval. - * @return {Array} - */ - ; - - _proto.splitBy = function splitBy(duration) { - var dur = Duration.fromDurationLike(duration); - - if (!this.isValid || !dur.isValid || dur.as("milliseconds") === 0) { - return []; - } - - var s = this.s, - idx = 1, - next; - var results = []; - - while (s < this.e) { - var added = this.start.plus(dur.mapUnits(function (x) { - return x * idx; - })); - next = +added > +this.e ? this.e : added; - results.push(Interval.fromDateTimes(s, next)); - s = next; - idx += 1; - } - - return results; - } - /** - * Split this Interval into the specified number of smaller intervals. - * @param {number} numberOfParts - The number of Intervals to divide the Interval into. - * @return {Array} - */ - ; - - _proto.divideEqually = function divideEqually(numberOfParts) { - if (!this.isValid) return []; - return this.splitBy(this.length() / numberOfParts).slice(0, numberOfParts); - } - /** - * Return whether this Interval overlaps with the specified Interval - * @param {Interval} other - * @return {boolean} - */ - ; - - _proto.overlaps = function overlaps(other) { - return this.e > other.s && this.s < other.e; - } - /** - * Return whether this Interval's end is adjacent to the specified Interval's start. - * @param {Interval} other - * @return {boolean} - */ - ; - - _proto.abutsStart = function abutsStart(other) { - if (!this.isValid) return false; - return +this.e === +other.s; - } - /** - * Return whether this Interval's start is adjacent to the specified Interval's end. - * @param {Interval} other - * @return {boolean} - */ - ; - - _proto.abutsEnd = function abutsEnd(other) { - if (!this.isValid) return false; - return +other.e === +this.s; - } - /** - * Return whether this Interval engulfs the start and end of the specified Interval. - * @param {Interval} other - * @return {boolean} - */ - ; - - _proto.engulfs = function engulfs(other) { - if (!this.isValid) return false; - return this.s <= other.s && this.e >= other.e; - } - /** - * Return whether this Interval has the same start and end as the specified Interval. - * @param {Interval} other - * @return {boolean} - */ - ; - - _proto.equals = function equals(other) { - if (!this.isValid || !other.isValid) { - return false; - } - - return this.s.equals(other.s) && this.e.equals(other.e); - } - /** - * Return an Interval representing the intersection of this Interval and the specified Interval. - * Specifically, the resulting Interval has the maximum start time and the minimum end time of the two Intervals. - * Returns null if the intersection is empty, meaning, the intervals don't intersect. - * @param {Interval} other - * @return {Interval} - */ - ; - - _proto.intersection = function intersection(other) { - if (!this.isValid) return this; - var s = this.s > other.s ? this.s : other.s, - e = this.e < other.e ? this.e : other.e; - - if (s >= e) { - return null; - } else { - return Interval.fromDateTimes(s, e); - } - } - /** - * Return an Interval representing the union of this Interval and the specified Interval. - * Specifically, the resulting Interval has the minimum start time and the maximum end time of the two Intervals. - * @param {Interval} other - * @return {Interval} - */ - ; - - _proto.union = function union(other) { - if (!this.isValid) return this; - var s = this.s < other.s ? this.s : other.s, - e = this.e > other.e ? this.e : other.e; - return Interval.fromDateTimes(s, e); - } - /** - * Merge an array of Intervals into a equivalent minimal set of Intervals. - * Combines overlapping and adjacent Intervals. - * @param {Array} intervals - * @return {Array} - */ - ; - - Interval.merge = function merge(intervals) { - var _intervals$sort$reduc = intervals.sort(function (a, b) { - return a.s - b.s; - }).reduce(function (_ref2, item) { - var sofar = _ref2[0], - current = _ref2[1]; - - if (!current) { - return [sofar, item]; - } else if (current.overlaps(item) || current.abutsStart(item)) { - return [sofar, current.union(item)]; - } else { - return [sofar.concat([current]), item]; - } - }, [[], null]), - found = _intervals$sort$reduc[0], - final = _intervals$sort$reduc[1]; - - if (final) { - found.push(final); - } - - return found; - } - /** - * Return an array of Intervals representing the spans of time that only appear in one of the specified Intervals. - * @param {Array} intervals - * @return {Array} - */ - ; - - Interval.xor = function xor(intervals) { - var _Array$prototype; - - var start = null, - currentCount = 0; - - var results = [], - ends = intervals.map(function (i) { - return [{ - time: i.s, - type: "s" - }, { - time: i.e, - type: "e" - }]; - }), - flattened = (_Array$prototype = Array.prototype).concat.apply(_Array$prototype, ends), - arr = flattened.sort(function (a, b) { - return a.time - b.time; - }); - - for (var _iterator = _createForOfIteratorHelperLoose(arr), _step; !(_step = _iterator()).done;) { - var i = _step.value; - currentCount += i.type === "s" ? 1 : -1; - - if (currentCount === 1) { - start = i.time; - } else { - if (start && +start !== +i.time) { - results.push(Interval.fromDateTimes(start, i.time)); - } - - start = null; - } - } - - return Interval.merge(results); - } - /** - * Return an Interval representing the span of time in this Interval that doesn't overlap with any of the specified Intervals. - * @param {...Interval} intervals - * @return {Array} - */ - ; - - _proto.difference = function difference() { - var _this2 = this; - - for (var _len2 = arguments.length, intervals = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { - intervals[_key2] = arguments[_key2]; - } - - return Interval.xor([this].concat(intervals)).map(function (i) { - return _this2.intersection(i); - }).filter(function (i) { - return i && !i.isEmpty(); - }); - } - /** - * Returns a string representation of this Interval appropriate for debugging. - * @return {string} - */ - ; - - _proto.toString = function toString() { - if (!this.isValid) return INVALID$1; - return "[" + this.s.toISO() + " \u2013 " + this.e.toISO() + ")"; - } - /** - * Returns an ISO 8601-compliant string representation of this Interval. - * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @param {Object} opts - The same options as {@link DateTime#toISO} - * @return {string} - */ - ; - - _proto.toISO = function toISO(opts) { - if (!this.isValid) return INVALID$1; - return this.s.toISO(opts) + "/" + this.e.toISO(opts); - } - /** - * Returns an ISO 8601-compliant string representation of date of this Interval. - * The time components are ignored. - * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @return {string} - */ - ; - - _proto.toISODate = function toISODate() { - if (!this.isValid) return INVALID$1; - return this.s.toISODate() + "/" + this.e.toISODate(); - } - /** - * Returns an ISO 8601-compliant string representation of time of this Interval. - * The date components are ignored. - * @see https://en.wikipedia.org/wiki/ISO_8601#Time_intervals - * @param {Object} opts - The same options as {@link DateTime#toISO} - * @return {string} - */ - ; - - _proto.toISOTime = function toISOTime(opts) { - if (!this.isValid) return INVALID$1; - return this.s.toISOTime(opts) + "/" + this.e.toISOTime(opts); - } - /** - * Returns a string representation of this Interval formatted according to the specified format string. - * @param {string} dateFormat - the format string. This string formats the start and end time. See {@link DateTime#toFormat} for details. - * @param {Object} opts - options - * @param {string} [opts.separator = ' – '] - a separator to place between the start and end representations - * @return {string} - */ - ; - - _proto.toFormat = function toFormat(dateFormat, _temp2) { - var _ref3 = _temp2 === void 0 ? {} : _temp2, - _ref3$separator = _ref3.separator, - separator = _ref3$separator === void 0 ? " – " : _ref3$separator; - - if (!this.isValid) return INVALID$1; - return "" + this.s.toFormat(dateFormat) + separator + this.e.toFormat(dateFormat); - } - /** - * Return a Duration representing the time spanned by this interval. - * @param {string|string[]} [unit=['milliseconds']] - the unit or units (such as 'hours' or 'days') to include in the duration. - * @param {Object} opts - options that affect the creation of the Duration - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @example Interval.fromDateTimes(dt1, dt2).toDuration().toObject() //=> { milliseconds: 88489257 } - * @example Interval.fromDateTimes(dt1, dt2).toDuration('days').toObject() //=> { days: 1.0241812152777778 } - * @example Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes']).toObject() //=> { hours: 24, minutes: 34.82095 } - * @example Interval.fromDateTimes(dt1, dt2).toDuration(['hours', 'minutes', 'seconds']).toObject() //=> { hours: 24, minutes: 34, seconds: 49.257 } - * @example Interval.fromDateTimes(dt1, dt2).toDuration('seconds').toObject() //=> { seconds: 88489.257 } - * @return {Duration} - */ - ; - - _proto.toDuration = function toDuration(unit, opts) { - if (!this.isValid) { - return Duration.invalid(this.invalidReason); - } - - return this.e.diff(this.s, unit, opts); - } - /** - * Run mapFn on the interval start and end, returning a new Interval from the resulting DateTimes - * @param {function} mapFn - * @return {Interval} - * @example Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.toUTC()) - * @example Interval.fromDateTimes(dt1, dt2).mapEndpoints(endpoint => endpoint.plus({ hours: 2 })) - */ - ; - - _proto.mapEndpoints = function mapEndpoints(mapFn) { - return Interval.fromDateTimes(mapFn(this.s), mapFn(this.e)); - }; - - _createClass(Interval, [{ - key: "start", - get: function get() { - return this.isValid ? this.s : null; - } - /** - * Returns the end of the Interval - * @type {DateTime} - */ - - }, { - key: "end", - get: function get() { - return this.isValid ? this.e : null; - } - /** - * Returns whether this Interval's end is at least its start, meaning that the Interval isn't 'backwards'. - * @type {boolean} - */ - - }, { - key: "isValid", - get: function get() { - return this.invalidReason === null; - } - /** - * Returns an error code if this Interval is invalid, or null if the Interval is valid - * @type {string} - */ - - }, { - key: "invalidReason", - get: function get() { - return this.invalid ? this.invalid.reason : null; - } - /** - * Returns an explanation of why this Interval became invalid, or null if the Interval is valid - * @type {string} - */ - - }, { - key: "invalidExplanation", - get: function get() { - return this.invalid ? this.invalid.explanation : null; - } - }]); - - return Interval; - }(); - - /** - * The Info class contains static methods for retrieving general time and date related data. For example, it has methods for finding out if a time zone has a DST, for listing the months in any supported locale, and for discovering which of Luxon features are available in the current environment. - */ - - var Info = /*#__PURE__*/function () { - function Info() {} - - /** - * Return whether the specified zone contains a DST. - * @param {string|Zone} [zone='local'] - Zone to check. Defaults to the environment's local zone. - * @return {boolean} - */ - Info.hasDST = function hasDST(zone) { - if (zone === void 0) { - zone = Settings.defaultZone; - } - - var proto = DateTime.now().setZone(zone).set({ - month: 12 - }); - return !zone.isUniversal && proto.offset !== proto.set({ - month: 6 - }).offset; - } - /** - * Return whether the specified zone is a valid IANA specifier. - * @param {string} zone - Zone to check - * @return {boolean} - */ - ; - - Info.isValidIANAZone = function isValidIANAZone(zone) { - return IANAZone.isValidZone(zone); - } - /** - * Converts the input into a {@link Zone} instance. - * - * * If `input` is already a Zone instance, it is returned unchanged. - * * If `input` is a string containing a valid time zone name, a Zone instance - * with that name is returned. - * * If `input` is a string that doesn't refer to a known time zone, a Zone - * instance with {@link Zone#isValid} == false is returned. - * * If `input is a number, a Zone instance with the specified fixed offset - * in minutes is returned. - * * If `input` is `null` or `undefined`, the default zone is returned. - * @param {string|Zone|number} [input] - the value to be converted - * @return {Zone} - */ - ; - - Info.normalizeZone = function normalizeZone$1(input) { - return normalizeZone(input, Settings.defaultZone); - } - /** - * Return an array of standalone month names. - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat - * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long" - * @param {Object} opts - options - * @param {string} [opts.locale] - the locale code - * @param {string} [opts.numberingSystem=null] - the numbering system - * @param {string} [opts.locObj=null] - an existing locale object to use - * @param {string} [opts.outputCalendar='gregory'] - the calendar - * @example Info.months()[0] //=> 'January' - * @example Info.months('short')[0] //=> 'Jan' - * @example Info.months('numeric')[0] //=> '1' - * @example Info.months('short', { locale: 'fr-CA' } )[0] //=> 'janv.' - * @example Info.months('numeric', { locale: 'ar' })[0] //=> '١' - * @example Info.months('long', { outputCalendar: 'islamic' })[0] //=> 'Rabiʻ I' - * @return {Array} - */ - ; - - Info.months = function months(length, _temp) { - if (length === void 0) { - length = "long"; - } - - var _ref = _temp === void 0 ? {} : _temp, - _ref$locale = _ref.locale, - locale = _ref$locale === void 0 ? null : _ref$locale, - _ref$numberingSystem = _ref.numberingSystem, - numberingSystem = _ref$numberingSystem === void 0 ? null : _ref$numberingSystem, - _ref$locObj = _ref.locObj, - locObj = _ref$locObj === void 0 ? null : _ref$locObj, - _ref$outputCalendar = _ref.outputCalendar, - outputCalendar = _ref$outputCalendar === void 0 ? "gregory" : _ref$outputCalendar; - - return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length); - } - /** - * Return an array of format month names. - * Format months differ from standalone months in that they're meant to appear next to the day of the month. In some languages, that - * changes the string. - * See {@link Info#months} - * @param {string} [length='long'] - the length of the month representation, such as "numeric", "2-digit", "narrow", "short", "long" - * @param {Object} opts - options - * @param {string} [opts.locale] - the locale code - * @param {string} [opts.numberingSystem=null] - the numbering system - * @param {string} [opts.locObj=null] - an existing locale object to use - * @param {string} [opts.outputCalendar='gregory'] - the calendar - * @return {Array} - */ - ; - - Info.monthsFormat = function monthsFormat(length, _temp2) { - if (length === void 0) { - length = "long"; - } - - var _ref2 = _temp2 === void 0 ? {} : _temp2, - _ref2$locale = _ref2.locale, - locale = _ref2$locale === void 0 ? null : _ref2$locale, - _ref2$numberingSystem = _ref2.numberingSystem, - numberingSystem = _ref2$numberingSystem === void 0 ? null : _ref2$numberingSystem, - _ref2$locObj = _ref2.locObj, - locObj = _ref2$locObj === void 0 ? null : _ref2$locObj, - _ref2$outputCalendar = _ref2.outputCalendar, - outputCalendar = _ref2$outputCalendar === void 0 ? "gregory" : _ref2$outputCalendar; - - return (locObj || Locale.create(locale, numberingSystem, outputCalendar)).months(length, true); - } - /** - * Return an array of standalone week names. - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat - * @param {string} [length='long'] - the length of the weekday representation, such as "narrow", "short", "long". - * @param {Object} opts - options - * @param {string} [opts.locale] - the locale code - * @param {string} [opts.numberingSystem=null] - the numbering system - * @param {string} [opts.locObj=null] - an existing locale object to use - * @example Info.weekdays()[0] //=> 'Monday' - * @example Info.weekdays('short')[0] //=> 'Mon' - * @example Info.weekdays('short', { locale: 'fr-CA' })[0] //=> 'lun.' - * @example Info.weekdays('short', { locale: 'ar' })[0] //=> 'الاثنين' - * @return {Array} - */ - ; - - Info.weekdays = function weekdays(length, _temp3) { - if (length === void 0) { - length = "long"; - } - - var _ref3 = _temp3 === void 0 ? {} : _temp3, - _ref3$locale = _ref3.locale, - locale = _ref3$locale === void 0 ? null : _ref3$locale, - _ref3$numberingSystem = _ref3.numberingSystem, - numberingSystem = _ref3$numberingSystem === void 0 ? null : _ref3$numberingSystem, - _ref3$locObj = _ref3.locObj, - locObj = _ref3$locObj === void 0 ? null : _ref3$locObj; - - return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length); - } - /** - * Return an array of format week names. - * Format weekdays differ from standalone weekdays in that they're meant to appear next to more date information. In some languages, that - * changes the string. - * See {@link Info#weekdays} - * @param {string} [length='long'] - the length of the month representation, such as "narrow", "short", "long". - * @param {Object} opts - options - * @param {string} [opts.locale=null] - the locale code - * @param {string} [opts.numberingSystem=null] - the numbering system - * @param {string} [opts.locObj=null] - an existing locale object to use - * @return {Array} - */ - ; - - Info.weekdaysFormat = function weekdaysFormat(length, _temp4) { - if (length === void 0) { - length = "long"; - } - - var _ref4 = _temp4 === void 0 ? {} : _temp4, - _ref4$locale = _ref4.locale, - locale = _ref4$locale === void 0 ? null : _ref4$locale, - _ref4$numberingSystem = _ref4.numberingSystem, - numberingSystem = _ref4$numberingSystem === void 0 ? null : _ref4$numberingSystem, - _ref4$locObj = _ref4.locObj, - locObj = _ref4$locObj === void 0 ? null : _ref4$locObj; - - return (locObj || Locale.create(locale, numberingSystem, null)).weekdays(length, true); - } - /** - * Return an array of meridiems. - * @param {Object} opts - options - * @param {string} [opts.locale] - the locale code - * @example Info.meridiems() //=> [ 'AM', 'PM' ] - * @example Info.meridiems({ locale: 'my' }) //=> [ 'နံနက်', 'ညနေ' ] - * @return {Array} - */ - ; - - Info.meridiems = function meridiems(_temp5) { - var _ref5 = _temp5 === void 0 ? {} : _temp5, - _ref5$locale = _ref5.locale, - locale = _ref5$locale === void 0 ? null : _ref5$locale; - - return Locale.create(locale).meridiems(); - } - /** - * Return an array of eras, such as ['BC', 'AD']. The locale can be specified, but the calendar system is always Gregorian. - * @param {string} [length='short'] - the length of the era representation, such as "short" or "long". - * @param {Object} opts - options - * @param {string} [opts.locale] - the locale code - * @example Info.eras() //=> [ 'BC', 'AD' ] - * @example Info.eras('long') //=> [ 'Before Christ', 'Anno Domini' ] - * @example Info.eras('long', { locale: 'fr' }) //=> [ 'avant Jésus-Christ', 'après Jésus-Christ' ] - * @return {Array} - */ - ; - - Info.eras = function eras(length, _temp6) { - if (length === void 0) { - length = "short"; - } - - var _ref6 = _temp6 === void 0 ? {} : _temp6, - _ref6$locale = _ref6.locale, - locale = _ref6$locale === void 0 ? null : _ref6$locale; - - return Locale.create(locale, null, "gregory").eras(length); - } - /** - * Return the set of available features in this environment. - * Some features of Luxon are not available in all environments. For example, on older browsers, relative time formatting support is not available. Use this function to figure out if that's the case. - * Keys: - * * `relative`: whether this environment supports relative time formatting - * @example Info.features() //=> { relative: false } - * @return {Object} - */ - ; - - Info.features = function features() { - return { - relative: hasRelative() - }; - }; - - return Info; - }(); - - function dayDiff(earlier, later) { - var utcDayStart = function utcDayStart(dt) { - return dt.toUTC(0, { - keepLocalTime: true - }).startOf("day").valueOf(); - }, - ms = utcDayStart(later) - utcDayStart(earlier); - - return Math.floor(Duration.fromMillis(ms).as("days")); - } - - function highOrderDiffs(cursor, later, units) { - var differs = [["years", function (a, b) { - return b.year - a.year; - }], ["quarters", function (a, b) { - return b.quarter - a.quarter; - }], ["months", function (a, b) { - return b.month - a.month + (b.year - a.year) * 12; - }], ["weeks", function (a, b) { - var days = dayDiff(a, b); - return (days - days % 7) / 7; - }], ["days", dayDiff]]; - var results = {}; - var lowestOrder, highWater; - - for (var _i = 0, _differs = differs; _i < _differs.length; _i++) { - var _differs$_i = _differs[_i], - unit = _differs$_i[0], - differ = _differs$_i[1]; - - if (units.indexOf(unit) >= 0) { - var _cursor$plus; - - lowestOrder = unit; - var delta = differ(cursor, later); - highWater = cursor.plus((_cursor$plus = {}, _cursor$plus[unit] = delta, _cursor$plus)); - - if (highWater > later) { - var _cursor$plus2; - - cursor = cursor.plus((_cursor$plus2 = {}, _cursor$plus2[unit] = delta - 1, _cursor$plus2)); - delta -= 1; - } else { - cursor = highWater; - } - - results[unit] = delta; - } - } - - return [cursor, results, highWater, lowestOrder]; - } - - function _diff (earlier, later, units, opts) { - var _highOrderDiffs = highOrderDiffs(earlier, later, units), - cursor = _highOrderDiffs[0], - results = _highOrderDiffs[1], - highWater = _highOrderDiffs[2], - lowestOrder = _highOrderDiffs[3]; - - var remainingMillis = later - cursor; - var lowerOrderUnits = units.filter(function (u) { - return ["hours", "minutes", "seconds", "milliseconds"].indexOf(u) >= 0; - }); - - if (lowerOrderUnits.length === 0) { - if (highWater < later) { - var _cursor$plus3; - - highWater = cursor.plus((_cursor$plus3 = {}, _cursor$plus3[lowestOrder] = 1, _cursor$plus3)); - } - - if (highWater !== cursor) { - results[lowestOrder] = (results[lowestOrder] || 0) + remainingMillis / (highWater - cursor); - } - } - - var duration = Duration.fromObject(results, opts); - - if (lowerOrderUnits.length > 0) { - var _Duration$fromMillis; - - return (_Duration$fromMillis = Duration.fromMillis(remainingMillis, opts)).shiftTo.apply(_Duration$fromMillis, lowerOrderUnits).plus(duration); - } else { - return duration; - } - } - - var numberingSystems = { - arab: "[\u0660-\u0669]", - arabext: "[\u06F0-\u06F9]", - bali: "[\u1B50-\u1B59]", - beng: "[\u09E6-\u09EF]", - deva: "[\u0966-\u096F]", - fullwide: "[\uFF10-\uFF19]", - gujr: "[\u0AE6-\u0AEF]", - hanidec: "[〇|一|二|三|四|五|六|七|八|九]", - khmr: "[\u17E0-\u17E9]", - knda: "[\u0CE6-\u0CEF]", - laoo: "[\u0ED0-\u0ED9]", - limb: "[\u1946-\u194F]", - mlym: "[\u0D66-\u0D6F]", - mong: "[\u1810-\u1819]", - mymr: "[\u1040-\u1049]", - orya: "[\u0B66-\u0B6F]", - tamldec: "[\u0BE6-\u0BEF]", - telu: "[\u0C66-\u0C6F]", - thai: "[\u0E50-\u0E59]", - tibt: "[\u0F20-\u0F29]", - latn: "\\d" - }; - var numberingSystemsUTF16 = { - arab: [1632, 1641], - arabext: [1776, 1785], - bali: [6992, 7001], - beng: [2534, 2543], - deva: [2406, 2415], - fullwide: [65296, 65303], - gujr: [2790, 2799], - khmr: [6112, 6121], - knda: [3302, 3311], - laoo: [3792, 3801], - limb: [6470, 6479], - mlym: [3430, 3439], - mong: [6160, 6169], - mymr: [4160, 4169], - orya: [2918, 2927], - tamldec: [3046, 3055], - telu: [3174, 3183], - thai: [3664, 3673], - tibt: [3872, 3881] - }; - var hanidecChars = numberingSystems.hanidec.replace(/[\[|\]]/g, "").split(""); - function parseDigits(str) { - var value = parseInt(str, 10); - - if (isNaN(value)) { - value = ""; - - for (var i = 0; i < str.length; i++) { - var code = str.charCodeAt(i); - - if (str[i].search(numberingSystems.hanidec) !== -1) { - value += hanidecChars.indexOf(str[i]); - } else { - for (var key in numberingSystemsUTF16) { - var _numberingSystemsUTF = numberingSystemsUTF16[key], - min = _numberingSystemsUTF[0], - max = _numberingSystemsUTF[1]; - - if (code >= min && code <= max) { - value += code - min; - } - } - } - } - - return parseInt(value, 10); - } else { - return value; - } - } - function digitRegex(_ref, append) { - var numberingSystem = _ref.numberingSystem; - - if (append === void 0) { - append = ""; - } - - return new RegExp("" + numberingSystems[numberingSystem || "latn"] + append); - } - - var MISSING_FTP = "missing Intl.DateTimeFormat.formatToParts support"; - - function intUnit(regex, post) { - if (post === void 0) { - post = function post(i) { - return i; - }; - } - - return { - regex: regex, - deser: function deser(_ref) { - var s = _ref[0]; - return post(parseDigits(s)); - } - }; - } - - var NBSP = String.fromCharCode(160); - var spaceOrNBSP = "( |" + NBSP + ")"; - var spaceOrNBSPRegExp = new RegExp(spaceOrNBSP, "g"); - - function fixListRegex(s) { - // make dots optional and also make them literal - // make space and non breakable space characters interchangeable - return s.replace(/\./g, "\\.?").replace(spaceOrNBSPRegExp, spaceOrNBSP); - } - - function stripInsensitivities(s) { - return s.replace(/\./g, "") // ignore dots that were made optional - .replace(spaceOrNBSPRegExp, " ") // interchange space and nbsp - .toLowerCase(); - } - - function oneOf(strings, startIndex) { - if (strings === null) { - return null; - } else { - return { - regex: RegExp(strings.map(fixListRegex).join("|")), - deser: function deser(_ref2) { - var s = _ref2[0]; - return strings.findIndex(function (i) { - return stripInsensitivities(s) === stripInsensitivities(i); - }) + startIndex; - } - }; - } - } - - function offset(regex, groups) { - return { - regex: regex, - deser: function deser(_ref3) { - var h = _ref3[1], - m = _ref3[2]; - return signedOffset(h, m); - }, - groups: groups - }; - } - - function simple(regex) { - return { - regex: regex, - deser: function deser(_ref4) { - var s = _ref4[0]; - return s; - } - }; - } - - function escapeToken(value) { - return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&"); - } - - function unitForToken(token, loc) { - var one = digitRegex(loc), - two = digitRegex(loc, "{2}"), - three = digitRegex(loc, "{3}"), - four = digitRegex(loc, "{4}"), - six = digitRegex(loc, "{6}"), - oneOrTwo = digitRegex(loc, "{1,2}"), - oneToThree = digitRegex(loc, "{1,3}"), - oneToSix = digitRegex(loc, "{1,6}"), - oneToNine = digitRegex(loc, "{1,9}"), - twoToFour = digitRegex(loc, "{2,4}"), - fourToSix = digitRegex(loc, "{4,6}"), - literal = function literal(t) { - return { - regex: RegExp(escapeToken(t.val)), - deser: function deser(_ref5) { - var s = _ref5[0]; - return s; - }, - literal: true - }; - }, - unitate = function unitate(t) { - if (token.literal) { - return literal(t); - } - - switch (t.val) { - // era - case "G": - return oneOf(loc.eras("short", false), 0); - - case "GG": - return oneOf(loc.eras("long", false), 0); - // years - - case "y": - return intUnit(oneToSix); - - case "yy": - return intUnit(twoToFour, untruncateYear); - - case "yyyy": - return intUnit(four); - - case "yyyyy": - return intUnit(fourToSix); - - case "yyyyyy": - return intUnit(six); - // months - - case "M": - return intUnit(oneOrTwo); - - case "MM": - return intUnit(two); - - case "MMM": - return oneOf(loc.months("short", true, false), 1); - - case "MMMM": - return oneOf(loc.months("long", true, false), 1); - - case "L": - return intUnit(oneOrTwo); - - case "LL": - return intUnit(two); - - case "LLL": - return oneOf(loc.months("short", false, false), 1); - - case "LLLL": - return oneOf(loc.months("long", false, false), 1); - // dates - - case "d": - return intUnit(oneOrTwo); - - case "dd": - return intUnit(two); - // ordinals - - case "o": - return intUnit(oneToThree); - - case "ooo": - return intUnit(three); - // time - - case "HH": - return intUnit(two); - - case "H": - return intUnit(oneOrTwo); - - case "hh": - return intUnit(two); - - case "h": - return intUnit(oneOrTwo); - - case "mm": - return intUnit(two); - - case "m": - return intUnit(oneOrTwo); - - case "q": - return intUnit(oneOrTwo); - - case "qq": - return intUnit(two); - - case "s": - return intUnit(oneOrTwo); - - case "ss": - return intUnit(two); - - case "S": - return intUnit(oneToThree); - - case "SSS": - return intUnit(three); - - case "u": - return simple(oneToNine); - - case "uu": - return simple(oneOrTwo); - - case "uuu": - return intUnit(one); - // meridiem - - case "a": - return oneOf(loc.meridiems(), 0); - // weekYear (k) - - case "kkkk": - return intUnit(four); - - case "kk": - return intUnit(twoToFour, untruncateYear); - // weekNumber (W) - - case "W": - return intUnit(oneOrTwo); - - case "WW": - return intUnit(two); - // weekdays - - case "E": - case "c": - return intUnit(one); - - case "EEE": - return oneOf(loc.weekdays("short", false, false), 1); - - case "EEEE": - return oneOf(loc.weekdays("long", false, false), 1); - - case "ccc": - return oneOf(loc.weekdays("short", true, false), 1); - - case "cccc": - return oneOf(loc.weekdays("long", true, false), 1); - // offset/zone - - case "Z": - case "ZZ": - return offset(new RegExp("([+-]" + oneOrTwo.source + ")(?::(" + two.source + "))?"), 2); - - case "ZZZ": - return offset(new RegExp("([+-]" + oneOrTwo.source + ")(" + two.source + ")?"), 2); - // we don't support ZZZZ (PST) or ZZZZZ (Pacific Standard Time) in parsing - // because we don't have any way to figure out what they are - - case "z": - return simple(/[a-z_+-/]{1,256}?/i); - - default: - return literal(t); - } - }; - - var unit = unitate(token) || { - invalidReason: MISSING_FTP - }; - unit.token = token; - return unit; - } - - var partTypeStyleToTokenVal = { - year: { - "2-digit": "yy", - numeric: "yyyyy" - }, - month: { - numeric: "M", - "2-digit": "MM", - short: "MMM", - long: "MMMM" - }, - day: { - numeric: "d", - "2-digit": "dd" - }, - weekday: { - short: "EEE", - long: "EEEE" - }, - dayperiod: "a", - dayPeriod: "a", - hour: { - numeric: "h", - "2-digit": "hh" - }, - minute: { - numeric: "m", - "2-digit": "mm" - }, - second: { - numeric: "s", - "2-digit": "ss" - } - }; - - function tokenForPart(part, locale, formatOpts) { - var type = part.type, - value = part.value; - - if (type === "literal") { - return { - literal: true, - val: value - }; - } - - var style = formatOpts[type]; - var val = partTypeStyleToTokenVal[type]; - - if (typeof val === "object") { - val = val[style]; - } - - if (val) { - return { - literal: false, - val: val - }; - } - - return undefined; - } - - function buildRegex(units) { - var re = units.map(function (u) { - return u.regex; - }).reduce(function (f, r) { - return f + "(" + r.source + ")"; - }, ""); - return ["^" + re + "$", units]; - } - - function match(input, regex, handlers) { - var matches = input.match(regex); - - if (matches) { - var all = {}; - var matchIndex = 1; - - for (var i in handlers) { - if (hasOwnProperty(handlers, i)) { - var h = handlers[i], - groups = h.groups ? h.groups + 1 : 1; - - if (!h.literal && h.token) { - all[h.token.val[0]] = h.deser(matches.slice(matchIndex, matchIndex + groups)); - } - - matchIndex += groups; - } - } - - return [matches, all]; - } else { - return [matches, {}]; - } - } - - function dateTimeFromMatches(matches) { - var toField = function toField(token) { - switch (token) { - case "S": - return "millisecond"; - - case "s": - return "second"; - - case "m": - return "minute"; - - case "h": - case "H": - return "hour"; - - case "d": - return "day"; - - case "o": - return "ordinal"; - - case "L": - case "M": - return "month"; - - case "y": - return "year"; - - case "E": - case "c": - return "weekday"; - - case "W": - return "weekNumber"; - - case "k": - return "weekYear"; - - case "q": - return "quarter"; - - default: - return null; - } - }; - - var zone = null; - var specificOffset; - - if (!isUndefined(matches.z)) { - zone = IANAZone.create(matches.z); - } - - if (!isUndefined(matches.Z)) { - if (!zone) { - zone = new FixedOffsetZone(matches.Z); - } - - specificOffset = matches.Z; - } - - if (!isUndefined(matches.q)) { - matches.M = (matches.q - 1) * 3 + 1; - } - - if (!isUndefined(matches.h)) { - if (matches.h < 12 && matches.a === 1) { - matches.h += 12; - } else if (matches.h === 12 && matches.a === 0) { - matches.h = 0; - } - } - - if (matches.G === 0 && matches.y) { - matches.y = -matches.y; - } - - if (!isUndefined(matches.u)) { - matches.S = parseMillis(matches.u); - } - - var vals = Object.keys(matches).reduce(function (r, k) { - var f = toField(k); - - if (f) { - r[f] = matches[k]; - } - - return r; - }, {}); - return [vals, zone, specificOffset]; - } - - var dummyDateTimeCache = null; - - function getDummyDateTime() { - if (!dummyDateTimeCache) { - dummyDateTimeCache = DateTime.fromMillis(1555555555555); - } - - return dummyDateTimeCache; - } - - function maybeExpandMacroToken(token, locale) { - if (token.literal) { - return token; - } - - var formatOpts = Formatter.macroTokenToFormatOpts(token.val); - - if (!formatOpts) { - return token; - } - - var formatter = Formatter.create(locale, formatOpts); - var parts = formatter.formatDateTimeParts(getDummyDateTime()); - var tokens = parts.map(function (p) { - return tokenForPart(p, locale, formatOpts); - }); - - if (tokens.includes(undefined)) { - return token; - } - - return tokens; - } - - function expandMacroTokens(tokens, locale) { - var _Array$prototype; - - return (_Array$prototype = Array.prototype).concat.apply(_Array$prototype, tokens.map(function (t) { - return maybeExpandMacroToken(t, locale); - })); - } - /** - * @private - */ - - - function explainFromTokens(locale, input, format) { - var tokens = expandMacroTokens(Formatter.parseFormat(format), locale), - units = tokens.map(function (t) { - return unitForToken(t, locale); - }), - disqualifyingUnit = units.find(function (t) { - return t.invalidReason; - }); - - if (disqualifyingUnit) { - return { - input: input, - tokens: tokens, - invalidReason: disqualifyingUnit.invalidReason - }; - } else { - var _buildRegex = buildRegex(units), - regexString = _buildRegex[0], - handlers = _buildRegex[1], - regex = RegExp(regexString, "i"), - _match = match(input, regex, handlers), - rawMatches = _match[0], - matches = _match[1], - _ref6 = matches ? dateTimeFromMatches(matches) : [null, null, undefined], - result = _ref6[0], - zone = _ref6[1], - specificOffset = _ref6[2]; - - if (hasOwnProperty(matches, "a") && hasOwnProperty(matches, "H")) { - throw new ConflictingSpecificationError("Can't include meridiem when specifying 24-hour format"); - } - - return { - input: input, - tokens: tokens, - regex: regex, - rawMatches: rawMatches, - matches: matches, - result: result, - zone: zone, - specificOffset: specificOffset - }; - } - } - function parseFromTokens(locale, input, format) { - var _explainFromTokens = explainFromTokens(locale, input, format), - result = _explainFromTokens.result, - zone = _explainFromTokens.zone, - specificOffset = _explainFromTokens.specificOffset, - invalidReason = _explainFromTokens.invalidReason; - - return [result, zone, specificOffset, invalidReason]; - } - - var nonLeapLadder = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334], - leapLadder = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335]; - - function unitOutOfRange(unit, value) { - return new Invalid("unit out of range", "you specified " + value + " (of type " + typeof value + ") as a " + unit + ", which is invalid"); - } - - function dayOfWeek(year, month, day) { - var js = new Date(Date.UTC(year, month - 1, day)).getUTCDay(); - return js === 0 ? 7 : js; - } - - function computeOrdinal(year, month, day) { - return day + (isLeapYear(year) ? leapLadder : nonLeapLadder)[month - 1]; - } - - function uncomputeOrdinal(year, ordinal) { - var table = isLeapYear(year) ? leapLadder : nonLeapLadder, - month0 = table.findIndex(function (i) { - return i < ordinal; - }), - day = ordinal - table[month0]; - return { - month: month0 + 1, - day: day - }; - } - /** - * @private - */ - - - function gregorianToWeek(gregObj) { - var year = gregObj.year, - month = gregObj.month, - day = gregObj.day, - ordinal = computeOrdinal(year, month, day), - weekday = dayOfWeek(year, month, day); - var weekNumber = Math.floor((ordinal - weekday + 10) / 7), - weekYear; - - if (weekNumber < 1) { - weekYear = year - 1; - weekNumber = weeksInWeekYear(weekYear); - } else if (weekNumber > weeksInWeekYear(year)) { - weekYear = year + 1; - weekNumber = 1; - } else { - weekYear = year; - } - - return _extends({ - weekYear: weekYear, - weekNumber: weekNumber, - weekday: weekday - }, timeObject(gregObj)); - } - function weekToGregorian(weekData) { - var weekYear = weekData.weekYear, - weekNumber = weekData.weekNumber, - weekday = weekData.weekday, - weekdayOfJan4 = dayOfWeek(weekYear, 1, 4), - yearInDays = daysInYear(weekYear); - var ordinal = weekNumber * 7 + weekday - weekdayOfJan4 - 3, - year; - - if (ordinal < 1) { - year = weekYear - 1; - ordinal += daysInYear(year); - } else if (ordinal > yearInDays) { - year = weekYear + 1; - ordinal -= daysInYear(weekYear); - } else { - year = weekYear; - } - - var _uncomputeOrdinal = uncomputeOrdinal(year, ordinal), - month = _uncomputeOrdinal.month, - day = _uncomputeOrdinal.day; - - return _extends({ - year: year, - month: month, - day: day - }, timeObject(weekData)); - } - function gregorianToOrdinal(gregData) { - var year = gregData.year, - month = gregData.month, - day = gregData.day; - var ordinal = computeOrdinal(year, month, day); - return _extends({ - year: year, - ordinal: ordinal - }, timeObject(gregData)); - } - function ordinalToGregorian(ordinalData) { - var year = ordinalData.year, - ordinal = ordinalData.ordinal; - - var _uncomputeOrdinal2 = uncomputeOrdinal(year, ordinal), - month = _uncomputeOrdinal2.month, - day = _uncomputeOrdinal2.day; - - return _extends({ - year: year, - month: month, - day: day - }, timeObject(ordinalData)); - } - function hasInvalidWeekData(obj) { - var validYear = isInteger(obj.weekYear), - validWeek = integerBetween(obj.weekNumber, 1, weeksInWeekYear(obj.weekYear)), - validWeekday = integerBetween(obj.weekday, 1, 7); - - if (!validYear) { - return unitOutOfRange("weekYear", obj.weekYear); - } else if (!validWeek) { - return unitOutOfRange("week", obj.week); - } else if (!validWeekday) { - return unitOutOfRange("weekday", obj.weekday); - } else return false; - } - function hasInvalidOrdinalData(obj) { - var validYear = isInteger(obj.year), - validOrdinal = integerBetween(obj.ordinal, 1, daysInYear(obj.year)); - - if (!validYear) { - return unitOutOfRange("year", obj.year); - } else if (!validOrdinal) { - return unitOutOfRange("ordinal", obj.ordinal); - } else return false; - } - function hasInvalidGregorianData(obj) { - var validYear = isInteger(obj.year), - validMonth = integerBetween(obj.month, 1, 12), - validDay = integerBetween(obj.day, 1, daysInMonth(obj.year, obj.month)); - - if (!validYear) { - return unitOutOfRange("year", obj.year); - } else if (!validMonth) { - return unitOutOfRange("month", obj.month); - } else if (!validDay) { - return unitOutOfRange("day", obj.day); - } else return false; - } - function hasInvalidTimeData(obj) { - var hour = obj.hour, - minute = obj.minute, - second = obj.second, - millisecond = obj.millisecond; - var validHour = integerBetween(hour, 0, 23) || hour === 24 && minute === 0 && second === 0 && millisecond === 0, - validMinute = integerBetween(minute, 0, 59), - validSecond = integerBetween(second, 0, 59), - validMillisecond = integerBetween(millisecond, 0, 999); - - if (!validHour) { - return unitOutOfRange("hour", hour); - } else if (!validMinute) { - return unitOutOfRange("minute", minute); - } else if (!validSecond) { - return unitOutOfRange("second", second); - } else if (!validMillisecond) { - return unitOutOfRange("millisecond", millisecond); - } else return false; - } - - var INVALID = "Invalid DateTime"; - var MAX_DATE = 8.64e15; - - function unsupportedZone(zone) { - return new Invalid("unsupported zone", "the zone \"" + zone.name + "\" is not supported"); - } // we cache week data on the DT object and this intermediates the cache - - - function possiblyCachedWeekData(dt) { - if (dt.weekData === null) { - dt.weekData = gregorianToWeek(dt.c); - } - - return dt.weekData; - } // clone really means, "make a new object with these modifications". all "setters" really use this - // to create a new object while only changing some of the properties - - - function clone(inst, alts) { - var current = { - ts: inst.ts, - zone: inst.zone, - c: inst.c, - o: inst.o, - loc: inst.loc, - invalid: inst.invalid - }; - return new DateTime(_extends({}, current, alts, { - old: current - })); - } // find the right offset a given local time. The o input is our guess, which determines which - // offset we'll pick in ambiguous cases (e.g. there are two 3 AMs b/c Fallback DST) - - - function fixOffset(localTS, o, tz) { - // Our UTC time is just a guess because our offset is just a guess - var utcGuess = localTS - o * 60 * 1000; // Test whether the zone matches the offset for this ts - - var o2 = tz.offset(utcGuess); // If so, offset didn't change and we're done - - if (o === o2) { - return [utcGuess, o]; - } // If not, change the ts by the difference in the offset - - - utcGuess -= (o2 - o) * 60 * 1000; // If that gives us the local time we want, we're done - - var o3 = tz.offset(utcGuess); - - if (o2 === o3) { - return [utcGuess, o2]; - } // If it's different, we're in a hole time. The offset has changed, but the we don't adjust the time - - - return [localTS - Math.min(o2, o3) * 60 * 1000, Math.max(o2, o3)]; - } // convert an epoch timestamp into a calendar object with the given offset - - - function tsToObj(ts, offset) { - ts += offset * 60 * 1000; - var d = new Date(ts); - return { - year: d.getUTCFullYear(), - month: d.getUTCMonth() + 1, - day: d.getUTCDate(), - hour: d.getUTCHours(), - minute: d.getUTCMinutes(), - second: d.getUTCSeconds(), - millisecond: d.getUTCMilliseconds() - }; - } // convert a calendar object to a epoch timestamp - - - function objToTS(obj, offset, zone) { - return fixOffset(objToLocalTS(obj), offset, zone); - } // create a new DT instance by adding a duration, adjusting for DSTs - - - function adjustTime(inst, dur) { - var oPre = inst.o, - year = inst.c.year + Math.trunc(dur.years), - month = inst.c.month + Math.trunc(dur.months) + Math.trunc(dur.quarters) * 3, - c = _extends({}, inst.c, { - year: year, - month: month, - day: Math.min(inst.c.day, daysInMonth(year, month)) + Math.trunc(dur.days) + Math.trunc(dur.weeks) * 7 - }), - millisToAdd = Duration.fromObject({ - years: dur.years - Math.trunc(dur.years), - quarters: dur.quarters - Math.trunc(dur.quarters), - months: dur.months - Math.trunc(dur.months), - weeks: dur.weeks - Math.trunc(dur.weeks), - days: dur.days - Math.trunc(dur.days), - hours: dur.hours, - minutes: dur.minutes, - seconds: dur.seconds, - milliseconds: dur.milliseconds - }).as("milliseconds"), - localTS = objToLocalTS(c); - - var _fixOffset = fixOffset(localTS, oPre, inst.zone), - ts = _fixOffset[0], - o = _fixOffset[1]; - - if (millisToAdd !== 0) { - ts += millisToAdd; // that could have changed the offset by going over a DST, but we want to keep the ts the same - - o = inst.zone.offset(ts); - } - - return { - ts: ts, - o: o - }; - } // helper useful in turning the results of parsing into real dates - // by handling the zone options - - - function parseDataToDateTime(parsed, parsedZone, opts, format, text, specificOffset) { - var setZone = opts.setZone, - zone = opts.zone; - - if (parsed && Object.keys(parsed).length !== 0) { - var interpretationZone = parsedZone || zone, - inst = DateTime.fromObject(parsed, _extends({}, opts, { - zone: interpretationZone, - specificOffset: specificOffset - })); - return setZone ? inst : inst.setZone(zone); - } else { - return DateTime.invalid(new Invalid("unparsable", "the input \"" + text + "\" can't be parsed as " + format)); - } - } // if you want to output a technical format (e.g. RFC 2822), this helper - // helps handle the details - - - function toTechFormat(dt, format, allowZ) { - if (allowZ === void 0) { - allowZ = true; - } - - return dt.isValid ? Formatter.create(Locale.create("en-US"), { - allowZ: allowZ, - forceSimple: true - }).formatDateTimeFromString(dt, format) : null; - } - - function _toISODate(o, extended) { - var longFormat = o.c.year > 9999 || o.c.year < 0; - var c = ""; - if (longFormat && o.c.year >= 0) c += "+"; - c += padStart(o.c.year, longFormat ? 6 : 4); - - if (extended) { - c += "-"; - c += padStart(o.c.month); - c += "-"; - c += padStart(o.c.day); - } else { - c += padStart(o.c.month); - c += padStart(o.c.day); - } - - return c; - } - - function _toISOTime(o, extended, suppressSeconds, suppressMilliseconds, includeOffset) { - var c = padStart(o.c.hour); - - if (extended) { - c += ":"; - c += padStart(o.c.minute); - - if (o.c.second !== 0 || !suppressSeconds) { - c += ":"; - } - } else { - c += padStart(o.c.minute); - } - - if (o.c.second !== 0 || !suppressSeconds) { - c += padStart(o.c.second); - - if (o.c.millisecond !== 0 || !suppressMilliseconds) { - c += "."; - c += padStart(o.c.millisecond, 3); - } - } - - if (includeOffset) { - if (o.isOffsetFixed && o.offset === 0) { - c += "Z"; - } else if (o.o < 0) { - c += "-"; - c += padStart(Math.trunc(-o.o / 60)); - c += ":"; - c += padStart(Math.trunc(-o.o % 60)); - } else { - c += "+"; - c += padStart(Math.trunc(o.o / 60)); - c += ":"; - c += padStart(Math.trunc(o.o % 60)); - } - } - - return c; - } // defaults for unspecified units in the supported calendars - - - var defaultUnitValues = { - month: 1, - day: 1, - hour: 0, - minute: 0, - second: 0, - millisecond: 0 - }, - defaultWeekUnitValues = { - weekNumber: 1, - weekday: 1, - hour: 0, - minute: 0, - second: 0, - millisecond: 0 - }, - defaultOrdinalUnitValues = { - ordinal: 1, - hour: 0, - minute: 0, - second: 0, - millisecond: 0 - }; // Units in the supported calendars, sorted by bigness - - var orderedUnits = ["year", "month", "day", "hour", "minute", "second", "millisecond"], - orderedWeekUnits = ["weekYear", "weekNumber", "weekday", "hour", "minute", "second", "millisecond"], - orderedOrdinalUnits = ["year", "ordinal", "hour", "minute", "second", "millisecond"]; // standardize case and plurality in units - - function normalizeUnit(unit) { - var normalized = { - year: "year", - years: "year", - month: "month", - months: "month", - day: "day", - days: "day", - hour: "hour", - hours: "hour", - minute: "minute", - minutes: "minute", - quarter: "quarter", - quarters: "quarter", - second: "second", - seconds: "second", - millisecond: "millisecond", - milliseconds: "millisecond", - weekday: "weekday", - weekdays: "weekday", - weeknumber: "weekNumber", - weeksnumber: "weekNumber", - weeknumbers: "weekNumber", - weekyear: "weekYear", - weekyears: "weekYear", - ordinal: "ordinal" - }[unit.toLowerCase()]; - if (!normalized) throw new InvalidUnitError(unit); - return normalized; - } // this is a dumbed down version of fromObject() that runs about 60% faster - // but doesn't do any validation, makes a bunch of assumptions about what units - // are present, and so on. - // this is a dumbed down version of fromObject() that runs about 60% faster - // but doesn't do any validation, makes a bunch of assumptions about what units - // are present, and so on. - - - function quickDT(obj, opts) { - var zone = normalizeZone(opts.zone, Settings.defaultZone), - loc = Locale.fromObject(opts), - tsNow = Settings.now(); - var ts, o; // assume we have the higher-order units - - if (!isUndefined(obj.year)) { - for (var _iterator = _createForOfIteratorHelperLoose(orderedUnits), _step; !(_step = _iterator()).done;) { - var u = _step.value; - - if (isUndefined(obj[u])) { - obj[u] = defaultUnitValues[u]; - } - } - - var invalid = hasInvalidGregorianData(obj) || hasInvalidTimeData(obj); - - if (invalid) { - return DateTime.invalid(invalid); - } - - var offsetProvis = zone.offset(tsNow); - - var _objToTS = objToTS(obj, offsetProvis, zone); - - ts = _objToTS[0]; - o = _objToTS[1]; - } else { - ts = tsNow; - } - - return new DateTime({ - ts: ts, - zone: zone, - loc: loc, - o: o - }); - } - - function diffRelative(start, end, opts) { - var round = isUndefined(opts.round) ? true : opts.round, - format = function format(c, unit) { - c = roundTo(c, round || opts.calendary ? 0 : 2, true); - var formatter = end.loc.clone(opts).relFormatter(opts); - return formatter.format(c, unit); - }, - differ = function differ(unit) { - if (opts.calendary) { - if (!end.hasSame(start, unit)) { - return end.startOf(unit).diff(start.startOf(unit), unit).get(unit); - } else return 0; - } else { - return end.diff(start, unit).get(unit); - } - }; - - if (opts.unit) { - return format(differ(opts.unit), opts.unit); - } - - for (var _iterator2 = _createForOfIteratorHelperLoose(opts.units), _step2; !(_step2 = _iterator2()).done;) { - var unit = _step2.value; - var count = differ(unit); - - if (Math.abs(count) >= 1) { - return format(count, unit); - } - } - - return format(start > end ? -0 : 0, opts.units[opts.units.length - 1]); - } - - function lastOpts(argList) { - var opts = {}, - args; - - if (argList.length > 0 && typeof argList[argList.length - 1] === "object") { - opts = argList[argList.length - 1]; - args = Array.from(argList).slice(0, argList.length - 1); - } else { - args = Array.from(argList); - } - - return [opts, args]; - } - /** - * A DateTime is an immutable data structure representing a specific date and time and accompanying methods. It contains class and instance methods for creating, parsing, interrogating, transforming, and formatting them. - * - * A DateTime comprises of: - * * A timestamp. Each DateTime instance refers to a specific millisecond of the Unix epoch. - * * A time zone. Each instance is considered in the context of a specific zone (by default the local system's zone). - * * Configuration properties that effect how output strings are formatted, such as `locale`, `numberingSystem`, and `outputCalendar`. - * - * Here is a brief overview of the most commonly used functionality it provides: - * - * * **Creation**: To create a DateTime from its components, use one of its factory class methods: {@link DateTime#local}, {@link DateTime#utc}, and (most flexibly) {@link DateTime#fromObject}. To create one from a standard string format, use {@link DateTime#fromISO}, {@link DateTime#fromHTTP}, and {@link DateTime#fromRFC2822}. To create one from a custom string format, use {@link DateTime#fromFormat}. To create one from a native JS date, use {@link DateTime#fromJSDate}. - * * **Gregorian calendar and time**: To examine the Gregorian properties of a DateTime individually (i.e as opposed to collectively through {@link DateTime#toObject}), use the {@link DateTime#year}, {@link DateTime#month}, - * {@link DateTime#day}, {@link DateTime#hour}, {@link DateTime#minute}, {@link DateTime#second}, {@link DateTime#millisecond} accessors. - * * **Week calendar**: For ISO week calendar attributes, see the {@link DateTime#weekYear}, {@link DateTime#weekNumber}, and {@link DateTime#weekday} accessors. - * * **Configuration** See the {@link DateTime#locale} and {@link DateTime#numberingSystem} accessors. - * * **Transformation**: To transform the DateTime into other DateTimes, use {@link DateTime#set}, {@link DateTime#reconfigure}, {@link DateTime#setZone}, {@link DateTime#setLocale}, {@link DateTime.plus}, {@link DateTime#minus}, {@link DateTime#endOf}, {@link DateTime#startOf}, {@link DateTime#toUTC}, and {@link DateTime#toLocal}. - * * **Output**: To convert the DateTime to other representations, use the {@link DateTime#toRelative}, {@link DateTime#toRelativeCalendar}, {@link DateTime#toJSON}, {@link DateTime#toISO}, {@link DateTime#toHTTP}, {@link DateTime#toObject}, {@link DateTime#toRFC2822}, {@link DateTime#toString}, {@link DateTime#toLocaleString}, {@link DateTime#toFormat}, {@link DateTime#toMillis} and {@link DateTime#toJSDate}. - * - * There's plenty others documented below. In addition, for more information on subtler topics like internationalization, time zones, alternative calendars, validity, and so on, see the external documentation. - */ - - - var DateTime = /*#__PURE__*/function () { - /** - * @access private - */ - function DateTime(config) { - var zone = config.zone || Settings.defaultZone; - var invalid = config.invalid || (Number.isNaN(config.ts) ? new Invalid("invalid input") : null) || (!zone.isValid ? unsupportedZone(zone) : null); - /** - * @access private - */ - - this.ts = isUndefined(config.ts) ? Settings.now() : config.ts; - var c = null, - o = null; - - if (!invalid) { - var unchanged = config.old && config.old.ts === this.ts && config.old.zone.equals(zone); - - if (unchanged) { - var _ref = [config.old.c, config.old.o]; - c = _ref[0]; - o = _ref[1]; - } else { - var ot = zone.offset(this.ts); - c = tsToObj(this.ts, ot); - invalid = Number.isNaN(c.year) ? new Invalid("invalid input") : null; - c = invalid ? null : c; - o = invalid ? null : ot; - } - } - /** - * @access private - */ - - - this._zone = zone; - /** - * @access private - */ - - this.loc = config.loc || Locale.create(); - /** - * @access private - */ - - this.invalid = invalid; - /** - * @access private - */ - - this.weekData = null; - /** - * @access private - */ - - this.c = c; - /** - * @access private - */ - - this.o = o; - /** - * @access private - */ - - this.isLuxonDateTime = true; - } // CONSTRUCT - - /** - * Create a DateTime for the current instant, in the system's time zone. - * - * Use Settings to override these default values if needed. - * @example DateTime.now().toISO() //~> now in the ISO format - * @return {DateTime} - */ - - - DateTime.now = function now() { - return new DateTime({}); - } - /** - * Create a local DateTime - * @param {number} [year] - The calendar year. If omitted (as in, call `local()` with no arguments), the current time will be used - * @param {number} [month=1] - The month, 1-indexed - * @param {number} [day=1] - The day of the month, 1-indexed - * @param {number} [hour=0] - The hour of the day, in 24-hour time - * @param {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59 - * @param {number} [second=0] - The second of the minute, meaning a number between 0 and 59 - * @param {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999 - * @example DateTime.local() //~> now - * @example DateTime.local({ zone: "America/New_York" }) //~> now, in US east coast time - * @example DateTime.local(2017) //~> 2017-01-01T00:00:00 - * @example DateTime.local(2017, 3) //~> 2017-03-01T00:00:00 - * @example DateTime.local(2017, 3, 12, { locale: "fr" }) //~> 2017-03-12T00:00:00, with a French locale - * @example DateTime.local(2017, 3, 12, 5) //~> 2017-03-12T05:00:00 - * @example DateTime.local(2017, 3, 12, 5, { zone: "utc" }) //~> 2017-03-12T05:00:00, in UTC - * @example DateTime.local(2017, 3, 12, 5, 45) //~> 2017-03-12T05:45:00 - * @example DateTime.local(2017, 3, 12, 5, 45, 10) //~> 2017-03-12T05:45:10 - * @example DateTime.local(2017, 3, 12, 5, 45, 10, 765) //~> 2017-03-12T05:45:10.765 - * @return {DateTime} - */ - ; - - DateTime.local = function local() { - var _lastOpts = lastOpts(arguments), - opts = _lastOpts[0], - args = _lastOpts[1], - year = args[0], - month = args[1], - day = args[2], - hour = args[3], - minute = args[4], - second = args[5], - millisecond = args[6]; - - return quickDT({ - year: year, - month: month, - day: day, - hour: hour, - minute: minute, - second: second, - millisecond: millisecond - }, opts); - } - /** - * Create a DateTime in UTC - * @param {number} [year] - The calendar year. If omitted (as in, call `utc()` with no arguments), the current time will be used - * @param {number} [month=1] - The month, 1-indexed - * @param {number} [day=1] - The day of the month - * @param {number} [hour=0] - The hour of the day, in 24-hour time - * @param {number} [minute=0] - The minute of the hour, meaning a number between 0 and 59 - * @param {number} [second=0] - The second of the minute, meaning a number between 0 and 59 - * @param {number} [millisecond=0] - The millisecond of the second, meaning a number between 0 and 999 - * @param {Object} options - configuration options for the DateTime - * @param {string} [options.locale] - a locale to set on the resulting DateTime instance - * @param {string} [options.outputCalendar] - the output calendar to set on the resulting DateTime instance - * @param {string} [options.numberingSystem] - the numbering system to set on the resulting DateTime instance - * @example DateTime.utc() //~> now - * @example DateTime.utc(2017) //~> 2017-01-01T00:00:00Z - * @example DateTime.utc(2017, 3) //~> 2017-03-01T00:00:00Z - * @example DateTime.utc(2017, 3, 12) //~> 2017-03-12T00:00:00Z - * @example DateTime.utc(2017, 3, 12, 5) //~> 2017-03-12T05:00:00Z - * @example DateTime.utc(2017, 3, 12, 5, 45) //~> 2017-03-12T05:45:00Z - * @example DateTime.utc(2017, 3, 12, 5, 45, { locale: "fr" }) //~> 2017-03-12T05:45:00Z with a French locale - * @example DateTime.utc(2017, 3, 12, 5, 45, 10) //~> 2017-03-12T05:45:10Z - * @example DateTime.utc(2017, 3, 12, 5, 45, 10, 765, { locale: "fr" }) //~> 2017-03-12T05:45:10.765Z with a French locale - * @return {DateTime} - */ - ; - - DateTime.utc = function utc() { - var _lastOpts2 = lastOpts(arguments), - opts = _lastOpts2[0], - args = _lastOpts2[1], - year = args[0], - month = args[1], - day = args[2], - hour = args[3], - minute = args[4], - second = args[5], - millisecond = args[6]; - - opts.zone = FixedOffsetZone.utcInstance; - return quickDT({ - year: year, - month: month, - day: day, - hour: hour, - minute: minute, - second: second, - millisecond: millisecond - }, opts); - } - /** - * Create a DateTime from a JavaScript Date object. Uses the default zone. - * @param {Date} date - a JavaScript Date object - * @param {Object} options - configuration options for the DateTime - * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into - * @return {DateTime} - */ - ; - - DateTime.fromJSDate = function fromJSDate(date, options) { - if (options === void 0) { - options = {}; - } - - var ts = isDate(date) ? date.valueOf() : NaN; - - if (Number.isNaN(ts)) { - return DateTime.invalid("invalid input"); - } - - var zoneToUse = normalizeZone(options.zone, Settings.defaultZone); - - if (!zoneToUse.isValid) { - return DateTime.invalid(unsupportedZone(zoneToUse)); - } - - return new DateTime({ - ts: ts, - zone: zoneToUse, - loc: Locale.fromObject(options) - }); - } - /** - * Create a DateTime from a number of milliseconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone. - * @param {number} milliseconds - a number of milliseconds since 1970 UTC - * @param {Object} options - configuration options for the DateTime - * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into - * @param {string} [options.locale] - a locale to set on the resulting DateTime instance - * @param {string} options.outputCalendar - the output calendar to set on the resulting DateTime instance - * @param {string} options.numberingSystem - the numbering system to set on the resulting DateTime instance - * @return {DateTime} - */ - ; - - DateTime.fromMillis = function fromMillis(milliseconds, options) { - if (options === void 0) { - options = {}; - } - - if (!isNumber(milliseconds)) { - throw new InvalidArgumentError("fromMillis requires a numerical input, but received a " + typeof milliseconds + " with value " + milliseconds); - } else if (milliseconds < -MAX_DATE || milliseconds > MAX_DATE) { - // this isn't perfect because because we can still end up out of range because of additional shifting, but it's a start - return DateTime.invalid("Timestamp out of range"); - } else { - return new DateTime({ - ts: milliseconds, - zone: normalizeZone(options.zone, Settings.defaultZone), - loc: Locale.fromObject(options) - }); - } - } - /** - * Create a DateTime from a number of seconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone. - * @param {number} seconds - a number of seconds since 1970 UTC - * @param {Object} options - configuration options for the DateTime - * @param {string|Zone} [options.zone='local'] - the zone to place the DateTime into - * @param {string} [options.locale] - a locale to set on the resulting DateTime instance - * @param {string} options.outputCalendar - the output calendar to set on the resulting DateTime instance - * @param {string} options.numberingSystem - the numbering system to set on the resulting DateTime instance - * @return {DateTime} - */ - ; - - DateTime.fromSeconds = function fromSeconds(seconds, options) { - if (options === void 0) { - options = {}; - } - - if (!isNumber(seconds)) { - throw new InvalidArgumentError("fromSeconds requires a numerical input"); - } else { - return new DateTime({ - ts: seconds * 1000, - zone: normalizeZone(options.zone, Settings.defaultZone), - loc: Locale.fromObject(options) - }); - } - } - /** - * Create a DateTime from a JavaScript object with keys like 'year' and 'hour' with reasonable defaults. - * @param {Object} obj - the object to create the DateTime from - * @param {number} obj.year - a year, such as 1987 - * @param {number} obj.month - a month, 1-12 - * @param {number} obj.day - a day of the month, 1-31, depending on the month - * @param {number} obj.ordinal - day of the year, 1-365 or 366 - * @param {number} obj.weekYear - an ISO week year - * @param {number} obj.weekNumber - an ISO week number, between 1 and 52 or 53, depending on the year - * @param {number} obj.weekday - an ISO weekday, 1-7, where 1 is Monday and 7 is Sunday - * @param {number} obj.hour - hour of the day, 0-23 - * @param {number} obj.minute - minute of the hour, 0-59 - * @param {number} obj.second - second of the minute, 0-59 - * @param {number} obj.millisecond - millisecond of the second, 0-999 - * @param {Object} opts - options for creating this DateTime - * @param {string|Zone} [opts.zone='local'] - interpret the numbers in the context of a particular zone. Can take any value taken as the first argument to setZone() - * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance - * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance - * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance - * @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25' - * @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01' - * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06 - * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }), - * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' }) - * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'America/New_York' }) - * @example DateTime.fromObject({ weekYear: 2016, weekNumber: 2, weekday: 3 }).toISODate() //=> '2016-01-13' - * @return {DateTime} - */ - ; - - DateTime.fromObject = function fromObject(obj, opts) { - if (opts === void 0) { - opts = {}; - } - - obj = obj || {}; - var zoneToUse = normalizeZone(opts.zone, Settings.defaultZone); - - if (!zoneToUse.isValid) { - return DateTime.invalid(unsupportedZone(zoneToUse)); - } - - var tsNow = Settings.now(), - offsetProvis = !isUndefined(opts.specificOffset) ? opts.specificOffset : zoneToUse.offset(tsNow), - normalized = normalizeObject(obj, normalizeUnit), - containsOrdinal = !isUndefined(normalized.ordinal), - containsGregorYear = !isUndefined(normalized.year), - containsGregorMD = !isUndefined(normalized.month) || !isUndefined(normalized.day), - containsGregor = containsGregorYear || containsGregorMD, - definiteWeekDef = normalized.weekYear || normalized.weekNumber, - loc = Locale.fromObject(opts); // cases: - // just a weekday -> this week's instance of that weekday, no worries - // (gregorian data or ordinal) + (weekYear or weekNumber) -> error - // (gregorian month or day) + ordinal -> error - // otherwise just use weeks or ordinals or gregorian, depending on what's specified - - if ((containsGregor || containsOrdinal) && definiteWeekDef) { - throw new ConflictingSpecificationError("Can't mix weekYear/weekNumber units with year/month/day or ordinals"); - } - - if (containsGregorMD && containsOrdinal) { - throw new ConflictingSpecificationError("Can't mix ordinal dates with month/day"); - } - - var useWeekData = definiteWeekDef || normalized.weekday && !containsGregor; // configure ourselves to deal with gregorian dates or week stuff - - var units, - defaultValues, - objNow = tsToObj(tsNow, offsetProvis); - - if (useWeekData) { - units = orderedWeekUnits; - defaultValues = defaultWeekUnitValues; - objNow = gregorianToWeek(objNow); - } else if (containsOrdinal) { - units = orderedOrdinalUnits; - defaultValues = defaultOrdinalUnitValues; - objNow = gregorianToOrdinal(objNow); - } else { - units = orderedUnits; - defaultValues = defaultUnitValues; - } // set default values for missing stuff - - - var foundFirst = false; - - for (var _iterator3 = _createForOfIteratorHelperLoose(units), _step3; !(_step3 = _iterator3()).done;) { - var u = _step3.value; - var v = normalized[u]; - - if (!isUndefined(v)) { - foundFirst = true; - } else if (foundFirst) { - normalized[u] = defaultValues[u]; - } else { - normalized[u] = objNow[u]; - } - } // make sure the values we have are in range - - - var higherOrderInvalid = useWeekData ? hasInvalidWeekData(normalized) : containsOrdinal ? hasInvalidOrdinalData(normalized) : hasInvalidGregorianData(normalized), - invalid = higherOrderInvalid || hasInvalidTimeData(normalized); - - if (invalid) { - return DateTime.invalid(invalid); - } // compute the actual time - - - var gregorian = useWeekData ? weekToGregorian(normalized) : containsOrdinal ? ordinalToGregorian(normalized) : normalized, - _objToTS2 = objToTS(gregorian, offsetProvis, zoneToUse), - tsFinal = _objToTS2[0], - offsetFinal = _objToTS2[1], - inst = new DateTime({ - ts: tsFinal, - zone: zoneToUse, - o: offsetFinal, - loc: loc - }); // gregorian data + weekday serves only to validate - - - if (normalized.weekday && containsGregor && obj.weekday !== inst.weekday) { - return DateTime.invalid("mismatched weekday", "you can't specify both a weekday of " + normalized.weekday + " and a date of " + inst.toISO()); - } - - return inst; - } - /** - * Create a DateTime from an ISO 8601 string - * @param {string} text - the ISO string - * @param {Object} opts - options to affect the creation - * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the time to this zone - * @param {boolean} [opts.setZone=false] - override the zone with a fixed-offset zone specified in the string itself, if it specifies one - * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance - * @param {string} [opts.outputCalendar] - the output calendar to set on the resulting DateTime instance - * @param {string} [opts.numberingSystem] - the numbering system to set on the resulting DateTime instance - * @example DateTime.fromISO('2016-05-25T09:08:34.123') - * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00') - * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00', {setZone: true}) - * @example DateTime.fromISO('2016-05-25T09:08:34.123', {zone: 'utc'}) - * @example DateTime.fromISO('2016-W05-4') - * @return {DateTime} - */ - ; - - DateTime.fromISO = function fromISO(text, opts) { - if (opts === void 0) { - opts = {}; - } - - var _parseISODate = parseISODate(text), - vals = _parseISODate[0], - parsedZone = _parseISODate[1]; - - return parseDataToDateTime(vals, parsedZone, opts, "ISO 8601", text); - } - /** - * Create a DateTime from an RFC 2822 string - * @param {string} text - the RFC 2822 string - * @param {Object} opts - options to affect the creation - * @param {string|Zone} [opts.zone='local'] - convert the time to this zone. Since the offset is always specified in the string itself, this has no effect on the interpretation of string, merely the zone the resulting DateTime is expressed in. - * @param {boolean} [opts.setZone=false] - override the zone with a fixed-offset zone specified in the string itself, if it specifies one - * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance - * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance - * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance - * @example DateTime.fromRFC2822('25 Nov 2016 13:23:12 GMT') - * @example DateTime.fromRFC2822('Fri, 25 Nov 2016 13:23:12 +0600') - * @example DateTime.fromRFC2822('25 Nov 2016 13:23 Z') - * @return {DateTime} - */ - ; - - DateTime.fromRFC2822 = function fromRFC2822(text, opts) { - if (opts === void 0) { - opts = {}; - } - - var _parseRFC2822Date = parseRFC2822Date(text), - vals = _parseRFC2822Date[0], - parsedZone = _parseRFC2822Date[1]; - - return parseDataToDateTime(vals, parsedZone, opts, "RFC 2822", text); - } - /** - * Create a DateTime from an HTTP header date - * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 - * @param {string} text - the HTTP header date - * @param {Object} opts - options to affect the creation - * @param {string|Zone} [opts.zone='local'] - convert the time to this zone. Since HTTP dates are always in UTC, this has no effect on the interpretation of string, merely the zone the resulting DateTime is expressed in. - * @param {boolean} [opts.setZone=false] - override the zone with the fixed-offset zone specified in the string. For HTTP dates, this is always UTC, so this option is equivalent to setting the `zone` option to 'utc', but this option is included for consistency with similar methods. - * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance - * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance - * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance - * @example DateTime.fromHTTP('Sun, 06 Nov 1994 08:49:37 GMT') - * @example DateTime.fromHTTP('Sunday, 06-Nov-94 08:49:37 GMT') - * @example DateTime.fromHTTP('Sun Nov 6 08:49:37 1994') - * @return {DateTime} - */ - ; - - DateTime.fromHTTP = function fromHTTP(text, opts) { - if (opts === void 0) { - opts = {}; - } - - var _parseHTTPDate = parseHTTPDate(text), - vals = _parseHTTPDate[0], - parsedZone = _parseHTTPDate[1]; - - return parseDataToDateTime(vals, parsedZone, opts, "HTTP", opts); - } - /** - * Create a DateTime from an input string and format string. - * Defaults to en-US if no locale has been specified, regardless of the system's locale. For a table of tokens and their interpretations, see [here](https://moment.github.io/luxon/#/parsing?id=table-of-tokens). - * @param {string} text - the string to parse - * @param {string} fmt - the format the string is expected to be in (see the link below for the formats) - * @param {Object} opts - options to affect the creation - * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the DateTime to this zone - * @param {boolean} [opts.setZone=false] - override the zone with a zone specified in the string itself, if it specifies one - * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale - * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system - * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance - * @return {DateTime} - */ - ; - - DateTime.fromFormat = function fromFormat(text, fmt, opts) { - if (opts === void 0) { - opts = {}; - } - - if (isUndefined(text) || isUndefined(fmt)) { - throw new InvalidArgumentError("fromFormat requires an input string and a format"); - } - - var _opts = opts, - _opts$locale = _opts.locale, - locale = _opts$locale === void 0 ? null : _opts$locale, - _opts$numberingSystem = _opts.numberingSystem, - numberingSystem = _opts$numberingSystem === void 0 ? null : _opts$numberingSystem, - localeToUse = Locale.fromOpts({ - locale: locale, - numberingSystem: numberingSystem, - defaultToEN: true - }), - _parseFromTokens = parseFromTokens(localeToUse, text, fmt), - vals = _parseFromTokens[0], - parsedZone = _parseFromTokens[1], - specificOffset = _parseFromTokens[2], - invalid = _parseFromTokens[3]; - - if (invalid) { - return DateTime.invalid(invalid); - } else { - return parseDataToDateTime(vals, parsedZone, opts, "format " + fmt, text, specificOffset); - } - } - /** - * @deprecated use fromFormat instead - */ - ; - - DateTime.fromString = function fromString(text, fmt, opts) { - if (opts === void 0) { - opts = {}; - } - - return DateTime.fromFormat(text, fmt, opts); - } - /** - * Create a DateTime from a SQL date, time, or datetime - * Defaults to en-US if no locale has been specified, regardless of the system's locale - * @param {string} text - the string to parse - * @param {Object} opts - options to affect the creation - * @param {string|Zone} [opts.zone='local'] - use this zone if no offset is specified in the input string itself. Will also convert the DateTime to this zone - * @param {boolean} [opts.setZone=false] - override the zone with a zone specified in the string itself, if it specifies one - * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale - * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system - * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance - * @example DateTime.fromSQL('2017-05-15') - * @example DateTime.fromSQL('2017-05-15 09:12:34') - * @example DateTime.fromSQL('2017-05-15 09:12:34.342') - * @example DateTime.fromSQL('2017-05-15 09:12:34.342+06:00') - * @example DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles') - * @example DateTime.fromSQL('2017-05-15 09:12:34.342 America/Los_Angeles', { setZone: true }) - * @example DateTime.fromSQL('2017-05-15 09:12:34.342', { zone: 'America/Los_Angeles' }) - * @example DateTime.fromSQL('09:12:34.342') - * @return {DateTime} - */ - ; - - DateTime.fromSQL = function fromSQL(text, opts) { - if (opts === void 0) { - opts = {}; - } - - var _parseSQL = parseSQL(text), - vals = _parseSQL[0], - parsedZone = _parseSQL[1]; - - return parseDataToDateTime(vals, parsedZone, opts, "SQL", text); - } - /** - * Create an invalid DateTime. - * @param {string} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent - * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information - * @return {DateTime} - */ - ; - - DateTime.invalid = function invalid(reason, explanation) { - if (explanation === void 0) { - explanation = null; - } - - if (!reason) { - throw new InvalidArgumentError("need to specify a reason the DateTime is invalid"); - } - - var invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation); - - if (Settings.throwOnInvalid) { - throw new InvalidDateTimeError(invalid); - } else { - return new DateTime({ - invalid: invalid - }); - } - } - /** - * Check if an object is a DateTime. Works across context boundaries - * @param {object} o - * @return {boolean} - */ - ; - - DateTime.isDateTime = function isDateTime(o) { - return o && o.isLuxonDateTime || false; - } // INFO - - /** - * Get the value of unit. - * @param {string} unit - a unit such as 'minute' or 'day' - * @example DateTime.local(2017, 7, 4).get('month'); //=> 7 - * @example DateTime.local(2017, 7, 4).get('day'); //=> 4 - * @return {number} - */ - ; - - var _proto = DateTime.prototype; - - _proto.get = function get(unit) { - return this[unit]; - } - /** - * Returns whether the DateTime is valid. Invalid DateTimes occur when: - * * The DateTime was created from invalid calendar information, such as the 13th month or February 30 - * * The DateTime was created by an operation on another invalid date - * @type {boolean} - */ - ; - - /** - * Returns the resolved Intl options for this DateTime. - * This is useful in understanding the behavior of formatting methods - * @param {Object} opts - the same options as toLocaleString - * @return {Object} - */ - _proto.resolvedLocaleOptions = function resolvedLocaleOptions(opts) { - if (opts === void 0) { - opts = {}; - } - - var _Formatter$create$res = Formatter.create(this.loc.clone(opts), opts).resolvedOptions(this), - locale = _Formatter$create$res.locale, - numberingSystem = _Formatter$create$res.numberingSystem, - calendar = _Formatter$create$res.calendar; - - return { - locale: locale, - numberingSystem: numberingSystem, - outputCalendar: calendar - }; - } // TRANSFORM - - /** - * "Set" the DateTime's zone to UTC. Returns a newly-constructed DateTime. - * - * Equivalent to {@link DateTime#setZone}('utc') - * @param {number} [offset=0] - optionally, an offset from UTC in minutes - * @param {Object} [opts={}] - options to pass to `setZone()` - * @return {DateTime} - */ - ; - - _proto.toUTC = function toUTC(offset, opts) { - if (offset === void 0) { - offset = 0; - } - - if (opts === void 0) { - opts = {}; - } - - return this.setZone(FixedOffsetZone.instance(offset), opts); - } - /** - * "Set" the DateTime's zone to the host's local zone. Returns a newly-constructed DateTime. - * - * Equivalent to `setZone('local')` - * @return {DateTime} - */ - ; - - _proto.toLocal = function toLocal() { - return this.setZone(Settings.defaultZone); - } - /** - * "Set" the DateTime's zone to specified zone. Returns a newly-constructed DateTime. - * - * By default, the setter keeps the underlying time the same (as in, the same timestamp), but the new instance will report different local times and consider DSTs when making computations, as with {@link DateTime#plus}. You may wish to use {@link DateTime#toLocal} and {@link DateTime#toUTC} which provide simple convenience wrappers for commonly used zones. - * @param {string|Zone} [zone='local'] - a zone identifier. As a string, that can be any IANA zone supported by the host environment, or a fixed-offset name of the form 'UTC+3', or the strings 'local' or 'utc'. You may also supply an instance of a {@link DateTime#Zone} class. - * @param {Object} opts - options - * @param {boolean} [opts.keepLocalTime=false] - If true, adjust the underlying time so that the local time stays the same, but in the target zone. You should rarely need this. - * @return {DateTime} - */ - ; - - _proto.setZone = function setZone(zone, _temp) { - var _ref2 = _temp === void 0 ? {} : _temp, - _ref2$keepLocalTime = _ref2.keepLocalTime, - keepLocalTime = _ref2$keepLocalTime === void 0 ? false : _ref2$keepLocalTime, - _ref2$keepCalendarTim = _ref2.keepCalendarTime, - keepCalendarTime = _ref2$keepCalendarTim === void 0 ? false : _ref2$keepCalendarTim; - - zone = normalizeZone(zone, Settings.defaultZone); - - if (zone.equals(this.zone)) { - return this; - } else if (!zone.isValid) { - return DateTime.invalid(unsupportedZone(zone)); - } else { - var newTS = this.ts; - - if (keepLocalTime || keepCalendarTime) { - var offsetGuess = zone.offset(this.ts); - var asObj = this.toObject(); - - var _objToTS3 = objToTS(asObj, offsetGuess, zone); - - newTS = _objToTS3[0]; - } - - return clone(this, { - ts: newTS, - zone: zone - }); - } - } - /** - * "Set" the locale, numberingSystem, or outputCalendar. Returns a newly-constructed DateTime. - * @param {Object} properties - the properties to set - * @example DateTime.local(2017, 5, 25).reconfigure({ locale: 'en-GB' }) - * @return {DateTime} - */ - ; - - _proto.reconfigure = function reconfigure(_temp2) { - var _ref3 = _temp2 === void 0 ? {} : _temp2, - locale = _ref3.locale, - numberingSystem = _ref3.numberingSystem, - outputCalendar = _ref3.outputCalendar; - - var loc = this.loc.clone({ - locale: locale, - numberingSystem: numberingSystem, - outputCalendar: outputCalendar - }); - return clone(this, { - loc: loc - }); - } - /** - * "Set" the locale. Returns a newly-constructed DateTime. - * Just a convenient alias for reconfigure({ locale }) - * @example DateTime.local(2017, 5, 25).setLocale('en-GB') - * @return {DateTime} - */ - ; - - _proto.setLocale = function setLocale(locale) { - return this.reconfigure({ - locale: locale - }); - } - /** - * "Set" the values of specified units. Returns a newly-constructed DateTime. - * You can only set units with this method; for "setting" metadata, see {@link DateTime#reconfigure} and {@link DateTime#setZone}. - * @param {Object} values - a mapping of units to numbers - * @example dt.set({ year: 2017 }) - * @example dt.set({ hour: 8, minute: 30 }) - * @example dt.set({ weekday: 5 }) - * @example dt.set({ year: 2005, ordinal: 234 }) - * @return {DateTime} - */ - ; - - _proto.set = function set(values) { - if (!this.isValid) return this; - var normalized = normalizeObject(values, normalizeUnit), - settingWeekStuff = !isUndefined(normalized.weekYear) || !isUndefined(normalized.weekNumber) || !isUndefined(normalized.weekday), - containsOrdinal = !isUndefined(normalized.ordinal), - containsGregorYear = !isUndefined(normalized.year), - containsGregorMD = !isUndefined(normalized.month) || !isUndefined(normalized.day), - containsGregor = containsGregorYear || containsGregorMD, - definiteWeekDef = normalized.weekYear || normalized.weekNumber; - - if ((containsGregor || containsOrdinal) && definiteWeekDef) { - throw new ConflictingSpecificationError("Can't mix weekYear/weekNumber units with year/month/day or ordinals"); - } - - if (containsGregorMD && containsOrdinal) { - throw new ConflictingSpecificationError("Can't mix ordinal dates with month/day"); - } - - var mixed; - - if (settingWeekStuff) { - mixed = weekToGregorian(_extends({}, gregorianToWeek(this.c), normalized)); - } else if (!isUndefined(normalized.ordinal)) { - mixed = ordinalToGregorian(_extends({}, gregorianToOrdinal(this.c), normalized)); - } else { - mixed = _extends({}, this.toObject(), normalized); // if we didn't set the day but we ended up on an overflow date, - // use the last day of the right month - - if (isUndefined(normalized.day)) { - mixed.day = Math.min(daysInMonth(mixed.year, mixed.month), mixed.day); - } - } - - var _objToTS4 = objToTS(mixed, this.o, this.zone), - ts = _objToTS4[0], - o = _objToTS4[1]; - - return clone(this, { - ts: ts, - o: o - }); - } - /** - * Add a period of time to this DateTime and return the resulting DateTime - * - * Adding hours, minutes, seconds, or milliseconds increases the timestamp by the right number of milliseconds. Adding days, months, or years shifts the calendar, accounting for DSTs and leap years along the way. Thus, `dt.plus({ hours: 24 })` may result in a different time than `dt.plus({ days: 1 })` if there's a DST shift in between. - * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() - * @example DateTime.now().plus(123) //~> in 123 milliseconds - * @example DateTime.now().plus({ minutes: 15 }) //~> in 15 minutes - * @example DateTime.now().plus({ days: 1 }) //~> this time tomorrow - * @example DateTime.now().plus({ days: -1 }) //~> this time yesterday - * @example DateTime.now().plus({ hours: 3, minutes: 13 }) //~> in 3 hr, 13 min - * @example DateTime.now().plus(Duration.fromObject({ hours: 3, minutes: 13 })) //~> in 3 hr, 13 min - * @return {DateTime} - */ - ; - - _proto.plus = function plus(duration) { - if (!this.isValid) return this; - var dur = Duration.fromDurationLike(duration); - return clone(this, adjustTime(this, dur)); - } - /** - * Subtract a period of time to this DateTime and return the resulting DateTime - * See {@link DateTime#plus} - * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject() - @return {DateTime} - */ - ; - - _proto.minus = function minus(duration) { - if (!this.isValid) return this; - var dur = Duration.fromDurationLike(duration).negate(); - return clone(this, adjustTime(this, dur)); - } - /** - * "Set" this DateTime to the beginning of a unit of time. - * @param {string} unit - The unit to go to the beginning of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'. - * @example DateTime.local(2014, 3, 3).startOf('month').toISODate(); //=> '2014-03-01' - * @example DateTime.local(2014, 3, 3).startOf('year').toISODate(); //=> '2014-01-01' - * @example DateTime.local(2014, 3, 3).startOf('week').toISODate(); //=> '2014-03-03', weeks always start on Mondays - * @example DateTime.local(2014, 3, 3, 5, 30).startOf('day').toISOTime(); //=> '00:00.000-05:00' - * @example DateTime.local(2014, 3, 3, 5, 30).startOf('hour').toISOTime(); //=> '05:00:00.000-05:00' - * @return {DateTime} - */ - ; - - _proto.startOf = function startOf(unit) { - if (!this.isValid) return this; - var o = {}, - normalizedUnit = Duration.normalizeUnit(unit); - - switch (normalizedUnit) { - case "years": - o.month = 1; - // falls through - - case "quarters": - case "months": - o.day = 1; - // falls through - - case "weeks": - case "days": - o.hour = 0; - // falls through - - case "hours": - o.minute = 0; - // falls through - - case "minutes": - o.second = 0; - // falls through - - case "seconds": - o.millisecond = 0; - break; - // no default, invalid units throw in normalizeUnit() - } - - if (normalizedUnit === "weeks") { - o.weekday = 1; - } - - if (normalizedUnit === "quarters") { - var q = Math.ceil(this.month / 3); - o.month = (q - 1) * 3 + 1; - } - - return this.set(o); - } - /** - * "Set" this DateTime to the end (meaning the last millisecond) of a unit of time - * @param {string} unit - The unit to go to the end of. Can be 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', or 'millisecond'. - * @example DateTime.local(2014, 3, 3).endOf('month').toISO(); //=> '2014-03-31T23:59:59.999-05:00' - * @example DateTime.local(2014, 3, 3).endOf('year').toISO(); //=> '2014-12-31T23:59:59.999-05:00' - * @example DateTime.local(2014, 3, 3).endOf('week').toISO(); // => '2014-03-09T23:59:59.999-05:00', weeks start on Mondays - * @example DateTime.local(2014, 3, 3, 5, 30).endOf('day').toISO(); //=> '2014-03-03T23:59:59.999-05:00' - * @example DateTime.local(2014, 3, 3, 5, 30).endOf('hour').toISO(); //=> '2014-03-03T05:59:59.999-05:00' - * @return {DateTime} - */ - ; - - _proto.endOf = function endOf(unit) { - var _this$plus; - - return this.isValid ? this.plus((_this$plus = {}, _this$plus[unit] = 1, _this$plus)).startOf(unit).minus(1) : this; - } // OUTPUT - - /** - * Returns a string representation of this DateTime formatted according to the specified format string. - * **You may not want this.** See {@link DateTime#toLocaleString} for a more flexible formatting tool. For a table of tokens and their interpretations, see [here](https://moment.github.io/luxon/#/formatting?id=table-of-tokens). - * Defaults to en-US if no locale has been specified, regardless of the system's locale. - * @param {string} fmt - the format string - * @param {Object} opts - opts to override the configuration options on this DateTime - * @example DateTime.now().toFormat('yyyy LLL dd') //=> '2017 Apr 22' - * @example DateTime.now().setLocale('fr').toFormat('yyyy LLL dd') //=> '2017 avr. 22' - * @example DateTime.now().toFormat('yyyy LLL dd', { locale: "fr" }) //=> '2017 avr. 22' - * @example DateTime.now().toFormat("HH 'hours and' mm 'minutes'") //=> '20 hours and 55 minutes' - * @return {string} - */ - ; - - _proto.toFormat = function toFormat(fmt, opts) { - if (opts === void 0) { - opts = {}; - } - - return this.isValid ? Formatter.create(this.loc.redefaultToEN(opts)).formatDateTimeFromString(this, fmt) : INVALID; - } - /** - * Returns a localized string representing this date. Accepts the same options as the Intl.DateTimeFormat constructor and any presets defined by Luxon, such as `DateTime.DATE_FULL` or `DateTime.TIME_SIMPLE`. - * The exact behavior of this method is browser-specific, but in general it will return an appropriate representation - * of the DateTime in the assigned locale. - * Defaults to the system's locale if no locale has been specified - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat - * @param formatOpts {Object} - Intl.DateTimeFormat constructor options and configuration options - * @param {Object} opts - opts to override the configuration options on this DateTime - * @example DateTime.now().toLocaleString(); //=> 4/20/2017 - * @example DateTime.now().setLocale('en-gb').toLocaleString(); //=> '20/04/2017' - * @example DateTime.now().toLocaleString({ locale: 'en-gb' }); //=> '20/04/2017' - * @example DateTime.now().toLocaleString(DateTime.DATE_FULL); //=> 'April 20, 2017' - * @example DateTime.now().toLocaleString(DateTime.TIME_SIMPLE); //=> '11:32 AM' - * @example DateTime.now().toLocaleString(DateTime.DATETIME_SHORT); //=> '4/20/2017, 11:32 AM' - * @example DateTime.now().toLocaleString({ weekday: 'long', month: 'long', day: '2-digit' }); //=> 'Thursday, April 20' - * @example DateTime.now().toLocaleString({ weekday: 'short', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }); //=> 'Thu, Apr 20, 11:27 AM' - * @example DateTime.now().toLocaleString({ hour: '2-digit', minute: '2-digit', hourCycle: 'h23' }); //=> '11:32' - * @return {string} - */ - ; - - _proto.toLocaleString = function toLocaleString(formatOpts, opts) { - if (formatOpts === void 0) { - formatOpts = DATE_SHORT; - } - - if (opts === void 0) { - opts = {}; - } - - return this.isValid ? Formatter.create(this.loc.clone(opts), formatOpts).formatDateTime(this) : INVALID; - } - /** - * Returns an array of format "parts", meaning individual tokens along with metadata. This is allows callers to post-process individual sections of the formatted output. - * Defaults to the system's locale if no locale has been specified - * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat/formatToParts - * @param opts {Object} - Intl.DateTimeFormat constructor options, same as `toLocaleString`. - * @example DateTime.now().toLocaleParts(); //=> [ - * //=> { type: 'day', value: '25' }, - * //=> { type: 'literal', value: '/' }, - * //=> { type: 'month', value: '05' }, - * //=> { type: 'literal', value: '/' }, - * //=> { type: 'year', value: '1982' } - * //=> ] - */ - ; - - _proto.toLocaleParts = function toLocaleParts(opts) { - if (opts === void 0) { - opts = {}; - } - - return this.isValid ? Formatter.create(this.loc.clone(opts), opts).formatDateTimeParts(this) : []; - } - /** - * Returns an ISO 8601-compliant string representation of this DateTime - * @param {Object} opts - options - * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 - * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 - * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' - * @param {string} [opts.format='extended'] - choose between the basic and extended format - * @example DateTime.utc(1983, 5, 25).toISO() //=> '1982-05-25T00:00:00.000Z' - * @example DateTime.now().toISO() //=> '2017-04-22T20:47:05.335-04:00' - * @example DateTime.now().toISO({ includeOffset: false }) //=> '2017-04-22T20:47:05.335' - * @example DateTime.now().toISO({ format: 'basic' }) //=> '20170422T204705.335-0400' - * @return {string} - */ - ; - - _proto.toISO = function toISO(_temp3) { - var _ref4 = _temp3 === void 0 ? {} : _temp3, - _ref4$format = _ref4.format, - format = _ref4$format === void 0 ? "extended" : _ref4$format, - _ref4$suppressSeconds = _ref4.suppressSeconds, - suppressSeconds = _ref4$suppressSeconds === void 0 ? false : _ref4$suppressSeconds, - _ref4$suppressMillise = _ref4.suppressMilliseconds, - suppressMilliseconds = _ref4$suppressMillise === void 0 ? false : _ref4$suppressMillise, - _ref4$includeOffset = _ref4.includeOffset, - includeOffset = _ref4$includeOffset === void 0 ? true : _ref4$includeOffset; - - if (!this.isValid) { - return null; - } - - var ext = format === "extended"; - - var c = _toISODate(this, ext); - - c += "T"; - c += _toISOTime(this, ext, suppressSeconds, suppressMilliseconds, includeOffset); - return c; - } - /** - * Returns an ISO 8601-compliant string representation of this DateTime's date component - * @param {Object} opts - options - * @param {string} [opts.format='extended'] - choose between the basic and extended format - * @example DateTime.utc(1982, 5, 25).toISODate() //=> '1982-05-25' - * @example DateTime.utc(1982, 5, 25).toISODate({ format: 'basic' }) //=> '19820525' - * @return {string} - */ - ; - - _proto.toISODate = function toISODate(_temp4) { - var _ref5 = _temp4 === void 0 ? {} : _temp4, - _ref5$format = _ref5.format, - format = _ref5$format === void 0 ? "extended" : _ref5$format; - - if (!this.isValid) { - return null; - } - - return _toISODate(this, format === "extended"); - } - /** - * Returns an ISO 8601-compliant string representation of this DateTime's week date - * @example DateTime.utc(1982, 5, 25).toISOWeekDate() //=> '1982-W21-2' - * @return {string} - */ - ; - - _proto.toISOWeekDate = function toISOWeekDate() { - return toTechFormat(this, "kkkk-'W'WW-c"); - } - /** - * Returns an ISO 8601-compliant string representation of this DateTime's time component - * @param {Object} opts - options - * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0 - * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0 - * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' - * @param {boolean} [opts.includePrefix=false] - include the `T` prefix - * @param {string} [opts.format='extended'] - choose between the basic and extended format - * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime() //=> '07:34:19.361Z' - * @example DateTime.utc().set({ hour: 7, minute: 34, seconds: 0, milliseconds: 0 }).toISOTime({ suppressSeconds: true }) //=> '07:34Z' - * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ format: 'basic' }) //=> '073419.361Z' - * @example DateTime.utc().set({ hour: 7, minute: 34 }).toISOTime({ includePrefix: true }) //=> 'T07:34:19.361Z' - * @return {string} - */ - ; - - _proto.toISOTime = function toISOTime(_temp5) { - var _ref6 = _temp5 === void 0 ? {} : _temp5, - _ref6$suppressMillise = _ref6.suppressMilliseconds, - suppressMilliseconds = _ref6$suppressMillise === void 0 ? false : _ref6$suppressMillise, - _ref6$suppressSeconds = _ref6.suppressSeconds, - suppressSeconds = _ref6$suppressSeconds === void 0 ? false : _ref6$suppressSeconds, - _ref6$includeOffset = _ref6.includeOffset, - includeOffset = _ref6$includeOffset === void 0 ? true : _ref6$includeOffset, - _ref6$includePrefix = _ref6.includePrefix, - includePrefix = _ref6$includePrefix === void 0 ? false : _ref6$includePrefix, - _ref6$format = _ref6.format, - format = _ref6$format === void 0 ? "extended" : _ref6$format; - - if (!this.isValid) { - return null; - } - - var c = includePrefix ? "T" : ""; - return c + _toISOTime(this, format === "extended", suppressSeconds, suppressMilliseconds, includeOffset); - } - /** - * Returns an RFC 2822-compatible string representation of this DateTime - * @example DateTime.utc(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 +0000' - * @example DateTime.local(2014, 7, 13).toRFC2822() //=> 'Sun, 13 Jul 2014 00:00:00 -0400' - * @return {string} - */ - ; - - _proto.toRFC2822 = function toRFC2822() { - return toTechFormat(this, "EEE, dd LLL yyyy HH:mm:ss ZZZ", false); - } - /** - * Returns a string representation of this DateTime appropriate for use in HTTP headers. The output is always expressed in GMT. - * Specifically, the string conforms to RFC 1123. - * @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 - * @example DateTime.utc(2014, 7, 13).toHTTP() //=> 'Sun, 13 Jul 2014 00:00:00 GMT' - * @example DateTime.utc(2014, 7, 13, 19).toHTTP() //=> 'Sun, 13 Jul 2014 19:00:00 GMT' - * @return {string} - */ - ; - - _proto.toHTTP = function toHTTP() { - return toTechFormat(this.toUTC(), "EEE, dd LLL yyyy HH:mm:ss 'GMT'"); - } - /** - * Returns a string representation of this DateTime appropriate for use in SQL Date - * @example DateTime.utc(2014, 7, 13).toSQLDate() //=> '2014-07-13' - * @return {string} - */ - ; - - _proto.toSQLDate = function toSQLDate() { - if (!this.isValid) { - return null; - } - - return _toISODate(this, true); - } - /** - * Returns a string representation of this DateTime appropriate for use in SQL Time - * @param {Object} opts - options - * @param {boolean} [opts.includeZone=false] - include the zone, such as 'America/New_York'. Overrides includeOffset. - * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' - * @param {boolean} [opts.includeOffsetSpace=true] - include the space between the time and the offset, such as '05:15:16.345 -04:00' - * @example DateTime.utc().toSQL() //=> '05:15:16.345' - * @example DateTime.now().toSQL() //=> '05:15:16.345 -04:00' - * @example DateTime.now().toSQL({ includeOffset: false }) //=> '05:15:16.345' - * @example DateTime.now().toSQL({ includeZone: false }) //=> '05:15:16.345 America/New_York' - * @return {string} - */ - ; - - _proto.toSQLTime = function toSQLTime(_temp6) { - var _ref7 = _temp6 === void 0 ? {} : _temp6, - _ref7$includeOffset = _ref7.includeOffset, - includeOffset = _ref7$includeOffset === void 0 ? true : _ref7$includeOffset, - _ref7$includeZone = _ref7.includeZone, - includeZone = _ref7$includeZone === void 0 ? false : _ref7$includeZone, - _ref7$includeOffsetSp = _ref7.includeOffsetSpace, - includeOffsetSpace = _ref7$includeOffsetSp === void 0 ? true : _ref7$includeOffsetSp; - - var fmt = "HH:mm:ss.SSS"; - - if (includeZone || includeOffset) { - if (includeOffsetSpace) { - fmt += " "; - } - - if (includeZone) { - fmt += "z"; - } else if (includeOffset) { - fmt += "ZZ"; - } - } - - return toTechFormat(this, fmt, true); - } - /** - * Returns a string representation of this DateTime appropriate for use in SQL DateTime - * @param {Object} opts - options - * @param {boolean} [opts.includeZone=false] - include the zone, such as 'America/New_York'. Overrides includeOffset. - * @param {boolean} [opts.includeOffset=true] - include the offset, such as 'Z' or '-04:00' - * @param {boolean} [opts.includeOffsetSpace=true] - include the space between the time and the offset, such as '05:15:16.345 -04:00' - * @example DateTime.utc(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 Z' - * @example DateTime.local(2014, 7, 13).toSQL() //=> '2014-07-13 00:00:00.000 -04:00' - * @example DateTime.local(2014, 7, 13).toSQL({ includeOffset: false }) //=> '2014-07-13 00:00:00.000' - * @example DateTime.local(2014, 7, 13).toSQL({ includeZone: true }) //=> '2014-07-13 00:00:00.000 America/New_York' - * @return {string} - */ - ; - - _proto.toSQL = function toSQL(opts) { - if (opts === void 0) { - opts = {}; - } - - if (!this.isValid) { - return null; - } - - return this.toSQLDate() + " " + this.toSQLTime(opts); - } - /** - * Returns a string representation of this DateTime appropriate for debugging - * @return {string} - */ - ; - - _proto.toString = function toString() { - return this.isValid ? this.toISO() : INVALID; - } - /** - * Returns the epoch milliseconds of this DateTime. Alias of {@link DateTime#toMillis} - * @return {number} - */ - ; - - _proto.valueOf = function valueOf() { - return this.toMillis(); - } - /** - * Returns the epoch milliseconds of this DateTime. - * @return {number} - */ - ; - - _proto.toMillis = function toMillis() { - return this.isValid ? this.ts : NaN; - } - /** - * Returns the epoch seconds of this DateTime. - * @return {number} - */ - ; - - _proto.toSeconds = function toSeconds() { - return this.isValid ? this.ts / 1000 : NaN; - } - /** - * Returns the epoch seconds (as a whole number) of this DateTime. - * @return {number} - */ - ; - - _proto.toUnixInteger = function toUnixInteger() { - return this.isValid ? Math.floor(this.ts / 1000) : NaN; - } - /** - * Returns an ISO 8601 representation of this DateTime appropriate for use in JSON. - * @return {string} - */ - ; - - _proto.toJSON = function toJSON() { - return this.toISO(); - } - /** - * Returns a BSON serializable equivalent to this DateTime. - * @return {Date} - */ - ; - - _proto.toBSON = function toBSON() { - return this.toJSDate(); - } - /** - * Returns a JavaScript object with this DateTime's year, month, day, and so on. - * @param opts - options for generating the object - * @param {boolean} [opts.includeConfig=false] - include configuration attributes in the output - * @example DateTime.now().toObject() //=> { year: 2017, month: 4, day: 22, hour: 20, minute: 49, second: 42, millisecond: 268 } - * @return {Object} - */ - ; - - _proto.toObject = function toObject(opts) { - if (opts === void 0) { - opts = {}; - } - - if (!this.isValid) return {}; - - var base = _extends({}, this.c); - - if (opts.includeConfig) { - base.outputCalendar = this.outputCalendar; - base.numberingSystem = this.loc.numberingSystem; - base.locale = this.loc.locale; - } - - return base; - } - /** - * Returns a JavaScript Date equivalent to this DateTime. - * @return {Date} - */ - ; - - _proto.toJSDate = function toJSDate() { - return new Date(this.isValid ? this.ts : NaN); - } // COMPARE - - /** - * Return the difference between two DateTimes as a Duration. - * @param {DateTime} otherDateTime - the DateTime to compare this one to - * @param {string|string[]} [unit=['milliseconds']] - the unit or array of units (such as 'hours' or 'days') to include in the duration. - * @param {Object} opts - options that affect the creation of the Duration - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @example - * var i1 = DateTime.fromISO('1982-05-25T09:45'), - * i2 = DateTime.fromISO('1983-10-14T10:30'); - * i2.diff(i1).toObject() //=> { milliseconds: 43807500000 } - * i2.diff(i1, 'hours').toObject() //=> { hours: 12168.75 } - * i2.diff(i1, ['months', 'days']).toObject() //=> { months: 16, days: 19.03125 } - * i2.diff(i1, ['months', 'days', 'hours']).toObject() //=> { months: 16, days: 19, hours: 0.75 } - * @return {Duration} - */ - ; - - _proto.diff = function diff(otherDateTime, unit, opts) { - if (unit === void 0) { - unit = "milliseconds"; - } - - if (opts === void 0) { - opts = {}; - } - - if (!this.isValid || !otherDateTime.isValid) { - return Duration.invalid("created by diffing an invalid DateTime"); - } - - var durOpts = _extends({ - locale: this.locale, - numberingSystem: this.numberingSystem - }, opts); - - var units = maybeArray(unit).map(Duration.normalizeUnit), - otherIsLater = otherDateTime.valueOf() > this.valueOf(), - earlier = otherIsLater ? this : otherDateTime, - later = otherIsLater ? otherDateTime : this, - diffed = _diff(earlier, later, units, durOpts); - - return otherIsLater ? diffed.negate() : diffed; - } - /** - * Return the difference between this DateTime and right now. - * See {@link DateTime#diff} - * @param {string|string[]} [unit=['milliseconds']] - the unit or units units (such as 'hours' or 'days') to include in the duration - * @param {Object} opts - options that affect the creation of the Duration - * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use - * @return {Duration} - */ - ; - - _proto.diffNow = function diffNow(unit, opts) { - if (unit === void 0) { - unit = "milliseconds"; - } - - if (opts === void 0) { - opts = {}; - } - - return this.diff(DateTime.now(), unit, opts); - } - /** - * Return an Interval spanning between this DateTime and another DateTime - * @param {DateTime} otherDateTime - the other end point of the Interval - * @return {Interval} - */ - ; - - _proto.until = function until(otherDateTime) { - return this.isValid ? Interval.fromDateTimes(this, otherDateTime) : this; - } - /** - * Return whether this DateTime is in the same unit of time as another DateTime. - * Higher-order units must also be identical for this function to return `true`. - * Note that time zones are **ignored** in this comparison, which compares the **local** calendar time. Use {@link DateTime#setZone} to convert one of the dates if needed. - * @param {DateTime} otherDateTime - the other DateTime - * @param {string} unit - the unit of time to check sameness on - * @example DateTime.now().hasSame(otherDT, 'day'); //~> true if otherDT is in the same current calendar day - * @return {boolean} - */ - ; - - _proto.hasSame = function hasSame(otherDateTime, unit) { - if (!this.isValid) return false; - var inputMs = otherDateTime.valueOf(); - var adjustedToZone = this.setZone(otherDateTime.zone, { - keepLocalTime: true - }); - return adjustedToZone.startOf(unit) <= inputMs && inputMs <= adjustedToZone.endOf(unit); - } - /** - * Equality check - * Two DateTimes are equal iff they represent the same millisecond, have the same zone and location, and are both valid. - * To compare just the millisecond values, use `+dt1 === +dt2`. - * @param {DateTime} other - the other DateTime - * @return {boolean} - */ - ; - - _proto.equals = function equals(other) { - return this.isValid && other.isValid && this.valueOf() === other.valueOf() && this.zone.equals(other.zone) && this.loc.equals(other.loc); - } - /** - * Returns a string representation of a this time relative to now, such as "in two days". Can only internationalize if your - * platform supports Intl.RelativeTimeFormat. Rounds down by default. - * @param {Object} options - options that affect the output - * @param {DateTime} [options.base=DateTime.now()] - the DateTime to use as the basis to which this time is compared. Defaults to now. - * @param {string} [options.style="long"] - the style of units, must be "long", "short", or "narrow" - * @param {string|string[]} options.unit - use a specific unit or array of units; if omitted, or an array, the method will pick the best unit. Use an array or one of "years", "quarters", "months", "weeks", "days", "hours", "minutes", or "seconds" - * @param {boolean} [options.round=true] - whether to round the numbers in the output. - * @param {number} [options.padding=0] - padding in milliseconds. This allows you to round up the result if it fits inside the threshold. Don't use in combination with {round: false} because the decimal output will include the padding. - * @param {string} options.locale - override the locale of this DateTime - * @param {string} options.numberingSystem - override the numberingSystem of this DateTime. The Intl system may choose not to honor this - * @example DateTime.now().plus({ days: 1 }).toRelative() //=> "in 1 day" - * @example DateTime.now().setLocale("es").toRelative({ days: 1 }) //=> "dentro de 1 día" - * @example DateTime.now().plus({ days: 1 }).toRelative({ locale: "fr" }) //=> "dans 23 heures" - * @example DateTime.now().minus({ days: 2 }).toRelative() //=> "2 days ago" - * @example DateTime.now().minus({ days: 2 }).toRelative({ unit: "hours" }) //=> "48 hours ago" - * @example DateTime.now().minus({ hours: 36 }).toRelative({ round: false }) //=> "1.5 days ago" - */ - ; - - _proto.toRelative = function toRelative(options) { - if (options === void 0) { - options = {}; - } - - if (!this.isValid) return null; - var base = options.base || DateTime.fromObject({}, { - zone: this.zone - }), - padding = options.padding ? this < base ? -options.padding : options.padding : 0; - var units = ["years", "months", "days", "hours", "minutes", "seconds"]; - var unit = options.unit; - - if (Array.isArray(options.unit)) { - units = options.unit; - unit = undefined; - } - - return diffRelative(base, this.plus(padding), _extends({}, options, { - numeric: "always", - units: units, - unit: unit - })); - } - /** - * Returns a string representation of this date relative to today, such as "yesterday" or "next month". - * Only internationalizes on platforms that supports Intl.RelativeTimeFormat. - * @param {Object} options - options that affect the output - * @param {DateTime} [options.base=DateTime.now()] - the DateTime to use as the basis to which this time is compared. Defaults to now. - * @param {string} options.locale - override the locale of this DateTime - * @param {string} options.unit - use a specific unit; if omitted, the method will pick the unit. Use one of "years", "quarters", "months", "weeks", or "days" - * @param {string} options.numberingSystem - override the numberingSystem of this DateTime. The Intl system may choose not to honor this - * @example DateTime.now().plus({ days: 1 }).toRelativeCalendar() //=> "tomorrow" - * @example DateTime.now().setLocale("es").plus({ days: 1 }).toRelative() //=> ""mañana" - * @example DateTime.now().plus({ days: 1 }).toRelativeCalendar({ locale: "fr" }) //=> "demain" - * @example DateTime.now().minus({ days: 2 }).toRelativeCalendar() //=> "2 days ago" - */ - ; - - _proto.toRelativeCalendar = function toRelativeCalendar(options) { - if (options === void 0) { - options = {}; - } - - if (!this.isValid) return null; - return diffRelative(options.base || DateTime.fromObject({}, { - zone: this.zone - }), this, _extends({}, options, { - numeric: "auto", - units: ["years", "months", "days"], - calendary: true - })); - } - /** - * Return the min of several date times - * @param {...DateTime} dateTimes - the DateTimes from which to choose the minimum - * @return {DateTime} the min DateTime, or undefined if called with no argument - */ - ; - - DateTime.min = function min() { - for (var _len = arguments.length, dateTimes = new Array(_len), _key = 0; _key < _len; _key++) { - dateTimes[_key] = arguments[_key]; - } - - if (!dateTimes.every(DateTime.isDateTime)) { - throw new InvalidArgumentError("min requires all arguments be DateTimes"); - } - - return bestBy(dateTimes, function (i) { - return i.valueOf(); - }, Math.min); - } - /** - * Return the max of several date times - * @param {...DateTime} dateTimes - the DateTimes from which to choose the maximum - * @return {DateTime} the max DateTime, or undefined if called with no argument - */ - ; - - DateTime.max = function max() { - for (var _len2 = arguments.length, dateTimes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { - dateTimes[_key2] = arguments[_key2]; - } - - if (!dateTimes.every(DateTime.isDateTime)) { - throw new InvalidArgumentError("max requires all arguments be DateTimes"); - } - - return bestBy(dateTimes, function (i) { - return i.valueOf(); - }, Math.max); - } // MISC - - /** - * Explain how a string would be parsed by fromFormat() - * @param {string} text - the string to parse - * @param {string} fmt - the format the string is expected to be in (see description) - * @param {Object} options - options taken by fromFormat() - * @return {Object} - */ - ; - - DateTime.fromFormatExplain = function fromFormatExplain(text, fmt, options) { - if (options === void 0) { - options = {}; - } - - var _options = options, - _options$locale = _options.locale, - locale = _options$locale === void 0 ? null : _options$locale, - _options$numberingSys = _options.numberingSystem, - numberingSystem = _options$numberingSys === void 0 ? null : _options$numberingSys, - localeToUse = Locale.fromOpts({ - locale: locale, - numberingSystem: numberingSystem, - defaultToEN: true - }); - return explainFromTokens(localeToUse, text, fmt); - } - /** - * @deprecated use fromFormatExplain instead - */ - ; - - DateTime.fromStringExplain = function fromStringExplain(text, fmt, options) { - if (options === void 0) { - options = {}; - } - - return DateTime.fromFormatExplain(text, fmt, options); - } // FORMAT PRESETS - - /** - * {@link DateTime#toLocaleString} format like 10/14/1983 - * @type {Object} - */ - ; - - _createClass(DateTime, [{ - key: "isValid", - get: function get() { - return this.invalid === null; - } - /** - * Returns an error code if this DateTime is invalid, or null if the DateTime is valid - * @type {string} - */ - - }, { - key: "invalidReason", - get: function get() { - return this.invalid ? this.invalid.reason : null; - } - /** - * Returns an explanation of why this DateTime became invalid, or null if the DateTime is valid - * @type {string} - */ - - }, { - key: "invalidExplanation", - get: function get() { - return this.invalid ? this.invalid.explanation : null; - } - /** - * Get the locale of a DateTime, such 'en-GB'. The locale is used when formatting the DateTime - * - * @type {string} - */ - - }, { - key: "locale", - get: function get() { - return this.isValid ? this.loc.locale : null; - } - /** - * Get the numbering system of a DateTime, such 'beng'. The numbering system is used when formatting the DateTime - * - * @type {string} - */ - - }, { - key: "numberingSystem", - get: function get() { - return this.isValid ? this.loc.numberingSystem : null; - } - /** - * Get the output calendar of a DateTime, such 'islamic'. The output calendar is used when formatting the DateTime - * - * @type {string} - */ - - }, { - key: "outputCalendar", - get: function get() { - return this.isValid ? this.loc.outputCalendar : null; - } - /** - * Get the time zone associated with this DateTime. - * @type {Zone} - */ - - }, { - key: "zone", - get: function get() { - return this._zone; - } - /** - * Get the name of the time zone. - * @type {string} - */ - - }, { - key: "zoneName", - get: function get() { - return this.isValid ? this.zone.name : null; - } - /** - * Get the year - * @example DateTime.local(2017, 5, 25).year //=> 2017 - * @type {number} - */ - - }, { - key: "year", - get: function get() { - return this.isValid ? this.c.year : NaN; - } - /** - * Get the quarter - * @example DateTime.local(2017, 5, 25).quarter //=> 2 - * @type {number} - */ - - }, { - key: "quarter", - get: function get() { - return this.isValid ? Math.ceil(this.c.month / 3) : NaN; - } - /** - * Get the month (1-12). - * @example DateTime.local(2017, 5, 25).month //=> 5 - * @type {number} - */ - - }, { - key: "month", - get: function get() { - return this.isValid ? this.c.month : NaN; - } - /** - * Get the day of the month (1-30ish). - * @example DateTime.local(2017, 5, 25).day //=> 25 - * @type {number} - */ - - }, { - key: "day", - get: function get() { - return this.isValid ? this.c.day : NaN; - } - /** - * Get the hour of the day (0-23). - * @example DateTime.local(2017, 5, 25, 9).hour //=> 9 - * @type {number} - */ - - }, { - key: "hour", - get: function get() { - return this.isValid ? this.c.hour : NaN; - } - /** - * Get the minute of the hour (0-59). - * @example DateTime.local(2017, 5, 25, 9, 30).minute //=> 30 - * @type {number} - */ - - }, { - key: "minute", - get: function get() { - return this.isValid ? this.c.minute : NaN; - } - /** - * Get the second of the minute (0-59). - * @example DateTime.local(2017, 5, 25, 9, 30, 52).second //=> 52 - * @type {number} - */ - - }, { - key: "second", - get: function get() { - return this.isValid ? this.c.second : NaN; - } - /** - * Get the millisecond of the second (0-999). - * @example DateTime.local(2017, 5, 25, 9, 30, 52, 654).millisecond //=> 654 - * @type {number} - */ - - }, { - key: "millisecond", - get: function get() { - return this.isValid ? this.c.millisecond : NaN; - } - /** - * Get the week year - * @see https://en.wikipedia.org/wiki/ISO_week_date - * @example DateTime.local(2014, 12, 31).weekYear //=> 2015 - * @type {number} - */ - - }, { - key: "weekYear", - get: function get() { - return this.isValid ? possiblyCachedWeekData(this).weekYear : NaN; - } - /** - * Get the week number of the week year (1-52ish). - * @see https://en.wikipedia.org/wiki/ISO_week_date - * @example DateTime.local(2017, 5, 25).weekNumber //=> 21 - * @type {number} - */ - - }, { - key: "weekNumber", - get: function get() { - return this.isValid ? possiblyCachedWeekData(this).weekNumber : NaN; - } - /** - * Get the day of the week. - * 1 is Monday and 7 is Sunday - * @see https://en.wikipedia.org/wiki/ISO_week_date - * @example DateTime.local(2014, 11, 31).weekday //=> 4 - * @type {number} - */ - - }, { - key: "weekday", - get: function get() { - return this.isValid ? possiblyCachedWeekData(this).weekday : NaN; - } - /** - * Get the ordinal (meaning the day of the year) - * @example DateTime.local(2017, 5, 25).ordinal //=> 145 - * @type {number|DateTime} - */ - - }, { - key: "ordinal", - get: function get() { - return this.isValid ? gregorianToOrdinal(this.c).ordinal : NaN; - } - /** - * Get the human readable short month name, such as 'Oct'. - * Defaults to the system's locale if no locale has been specified - * @example DateTime.local(2017, 10, 30).monthShort //=> Oct - * @type {string} - */ - - }, { - key: "monthShort", - get: function get() { - return this.isValid ? Info.months("short", { - locObj: this.loc - })[this.month - 1] : null; - } - /** - * Get the human readable long month name, such as 'October'. - * Defaults to the system's locale if no locale has been specified - * @example DateTime.local(2017, 10, 30).monthLong //=> October - * @type {string} - */ - - }, { - key: "monthLong", - get: function get() { - return this.isValid ? Info.months("long", { - locObj: this.loc - })[this.month - 1] : null; - } - /** - * Get the human readable short weekday, such as 'Mon'. - * Defaults to the system's locale if no locale has been specified - * @example DateTime.local(2017, 10, 30).weekdayShort //=> Mon - * @type {string} - */ - - }, { - key: "weekdayShort", - get: function get() { - return this.isValid ? Info.weekdays("short", { - locObj: this.loc - })[this.weekday - 1] : null; - } - /** - * Get the human readable long weekday, such as 'Monday'. - * Defaults to the system's locale if no locale has been specified - * @example DateTime.local(2017, 10, 30).weekdayLong //=> Monday - * @type {string} - */ - - }, { - key: "weekdayLong", - get: function get() { - return this.isValid ? Info.weekdays("long", { - locObj: this.loc - })[this.weekday - 1] : null; - } - /** - * Get the UTC offset of this DateTime in minutes - * @example DateTime.now().offset //=> -240 - * @example DateTime.utc().offset //=> 0 - * @type {number} - */ - - }, { - key: "offset", - get: function get() { - return this.isValid ? +this.o : NaN; - } - /** - * Get the short human name for the zone's current offset, for example "EST" or "EDT". - * Defaults to the system's locale if no locale has been specified - * @type {string} - */ - - }, { - key: "offsetNameShort", - get: function get() { - if (this.isValid) { - return this.zone.offsetName(this.ts, { - format: "short", - locale: this.locale - }); - } else { - return null; - } - } - /** - * Get the long human name for the zone's current offset, for example "Eastern Standard Time" or "Eastern Daylight Time". - * Defaults to the system's locale if no locale has been specified - * @type {string} - */ - - }, { - key: "offsetNameLong", - get: function get() { - if (this.isValid) { - return this.zone.offsetName(this.ts, { - format: "long", - locale: this.locale - }); - } else { - return null; - } - } - /** - * Get whether this zone's offset ever changes, as in a DST. - * @type {boolean} - */ - - }, { - key: "isOffsetFixed", - get: function get() { - return this.isValid ? this.zone.isUniversal : null; - } - /** - * Get whether the DateTime is in a DST. - * @type {boolean} - */ - - }, { - key: "isInDST", - get: function get() { - if (this.isOffsetFixed) { - return false; - } else { - return this.offset > this.set({ - month: 1 - }).offset || this.offset > this.set({ - month: 5 - }).offset; - } - } - /** - * Returns true if this DateTime is in a leap year, false otherwise - * @example DateTime.local(2016).isInLeapYear //=> true - * @example DateTime.local(2013).isInLeapYear //=> false - * @type {boolean} - */ - - }, { - key: "isInLeapYear", - get: function get() { - return isLeapYear(this.year); - } - /** - * Returns the number of days in this DateTime's month - * @example DateTime.local(2016, 2).daysInMonth //=> 29 - * @example DateTime.local(2016, 3).daysInMonth //=> 31 - * @type {number} - */ - - }, { - key: "daysInMonth", - get: function get() { - return daysInMonth(this.year, this.month); - } - /** - * Returns the number of days in this DateTime's year - * @example DateTime.local(2016).daysInYear //=> 366 - * @example DateTime.local(2013).daysInYear //=> 365 - * @type {number} - */ - - }, { - key: "daysInYear", - get: function get() { - return this.isValid ? daysInYear(this.year) : NaN; - } - /** - * Returns the number of weeks in this DateTime's year - * @see https://en.wikipedia.org/wiki/ISO_week_date - * @example DateTime.local(2004).weeksInWeekYear //=> 53 - * @example DateTime.local(2013).weeksInWeekYear //=> 52 - * @type {number} - */ - - }, { - key: "weeksInWeekYear", - get: function get() { - return this.isValid ? weeksInWeekYear(this.weekYear) : NaN; - } - }], [{ - key: "DATE_SHORT", - get: function get() { - return DATE_SHORT; - } - /** - * {@link DateTime#toLocaleString} format like 'Oct 14, 1983' - * @type {Object} - */ - - }, { - key: "DATE_MED", - get: function get() { - return DATE_MED; - } - /** - * {@link DateTime#toLocaleString} format like 'Fri, Oct 14, 1983' - * @type {Object} - */ - - }, { - key: "DATE_MED_WITH_WEEKDAY", - get: function get() { - return DATE_MED_WITH_WEEKDAY; - } - /** - * {@link DateTime#toLocaleString} format like 'October 14, 1983' - * @type {Object} - */ - - }, { - key: "DATE_FULL", - get: function get() { - return DATE_FULL; - } - /** - * {@link DateTime#toLocaleString} format like 'Tuesday, October 14, 1983' - * @type {Object} - */ - - }, { - key: "DATE_HUGE", - get: function get() { - return DATE_HUGE; - } - /** - * {@link DateTime#toLocaleString} format like '09:30 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "TIME_SIMPLE", - get: function get() { - return TIME_SIMPLE; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "TIME_WITH_SECONDS", - get: function get() { - return TIME_WITH_SECONDS; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23 AM EDT'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "TIME_WITH_SHORT_OFFSET", - get: function get() { - return TIME_WITH_SHORT_OFFSET; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23 AM Eastern Daylight Time'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "TIME_WITH_LONG_OFFSET", - get: function get() { - return TIME_WITH_LONG_OFFSET; - } - /** - * {@link DateTime#toLocaleString} format like '09:30', always 24-hour. - * @type {Object} - */ - - }, { - key: "TIME_24_SIMPLE", - get: function get() { - return TIME_24_SIMPLE; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23', always 24-hour. - * @type {Object} - */ - - }, { - key: "TIME_24_WITH_SECONDS", - get: function get() { - return TIME_24_WITH_SECONDS; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23 EDT', always 24-hour. - * @type {Object} - */ - - }, { - key: "TIME_24_WITH_SHORT_OFFSET", - get: function get() { - return TIME_24_WITH_SHORT_OFFSET; - } - /** - * {@link DateTime#toLocaleString} format like '09:30:23 Eastern Daylight Time', always 24-hour. - * @type {Object} - */ - - }, { - key: "TIME_24_WITH_LONG_OFFSET", - get: function get() { - return TIME_24_WITH_LONG_OFFSET; - } - /** - * {@link DateTime#toLocaleString} format like '10/14/1983, 9:30 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_SHORT", - get: function get() { - return DATETIME_SHORT; - } - /** - * {@link DateTime#toLocaleString} format like '10/14/1983, 9:30:33 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_SHORT_WITH_SECONDS", - get: function get() { - return DATETIME_SHORT_WITH_SECONDS; - } - /** - * {@link DateTime#toLocaleString} format like 'Oct 14, 1983, 9:30 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_MED", - get: function get() { - return DATETIME_MED; - } - /** - * {@link DateTime#toLocaleString} format like 'Oct 14, 1983, 9:30:33 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_MED_WITH_SECONDS", - get: function get() { - return DATETIME_MED_WITH_SECONDS; - } - /** - * {@link DateTime#toLocaleString} format like 'Fri, 14 Oct 1983, 9:30 AM'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_MED_WITH_WEEKDAY", - get: function get() { - return DATETIME_MED_WITH_WEEKDAY; - } - /** - * {@link DateTime#toLocaleString} format like 'October 14, 1983, 9:30 AM EDT'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_FULL", - get: function get() { - return DATETIME_FULL; - } - /** - * {@link DateTime#toLocaleString} format like 'October 14, 1983, 9:30:33 AM EDT'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_FULL_WITH_SECONDS", - get: function get() { - return DATETIME_FULL_WITH_SECONDS; - } - /** - * {@link DateTime#toLocaleString} format like 'Friday, October 14, 1983, 9:30 AM Eastern Daylight Time'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_HUGE", - get: function get() { - return DATETIME_HUGE; - } - /** - * {@link DateTime#toLocaleString} format like 'Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time'. Only 12-hour if the locale is. - * @type {Object} - */ - - }, { - key: "DATETIME_HUGE_WITH_SECONDS", - get: function get() { - return DATETIME_HUGE_WITH_SECONDS; - } - }]); - - return DateTime; - }(); - function friendlyDateTime(dateTimeish) { - if (DateTime.isDateTime(dateTimeish)) { - return dateTimeish; - } else if (dateTimeish && dateTimeish.valueOf && isNumber(dateTimeish.valueOf())) { - return DateTime.fromJSDate(dateTimeish); - } else if (dateTimeish && typeof dateTimeish === "object") { - return DateTime.fromObject(dateTimeish); - } else { - throw new InvalidArgumentError("Unknown datetime argument: " + dateTimeish + ", of type " + typeof dateTimeish); - } - } - - var VERSION = "2.3.1"; - - exports.DateTime = DateTime; - exports.Duration = Duration; - exports.FixedOffsetZone = FixedOffsetZone; - exports.IANAZone = IANAZone; - exports.Info = Info; - exports.Interval = Interval; - exports.InvalidZone = InvalidZone; - exports.Settings = Settings; - exports.SystemZone = SystemZone; - exports.VERSION = VERSION; - exports.Zone = Zone; - - Object.defineProperty(exports, '__esModule', { value: true }); - - return exports; - -})({}); -//# sourceMappingURL=luxon.js.map diff --git a/static/js/luxon.min.js b/static/js/luxon.min.js index 76da0a76..ce14bb3e 100644 --- a/static/js/luxon.min.js +++ b/static/js/luxon.min.js @@ -1 +1 @@ -var luxon=function(e){"use strict";function r(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(t(Error)),d=function(t){function e(e){return t.call(this,"Invalid DateTime: "+e.toMessage())||this}return i(e,t),e}(n),h=function(t){function e(e){return t.call(this,"Invalid Interval: "+e.toMessage())||this}return i(e,t),e}(n),y=function(t){function e(e){return t.call(this,"Invalid Duration: "+e.toMessage())||this}return i(e,t),e}(n),S=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(n),v=function(t){function e(e){return t.call(this,"Invalid unit "+e)||this}return i(e,t),e}(n),p=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(n),m=function(e){function t(){return e.call(this,"Zone is an abstract class")||this}return i(t,e),t}(n),g="numeric",w="short",T="long",b={year:g,month:g,day:g},O={year:g,month:w,day:g},M={year:g,month:w,day:g,weekday:w},N={year:g,month:T,day:g},D={year:g,month:T,day:g,weekday:T},E={hour:g,minute:g},V={hour:g,minute:g,second:g},I={hour:g,minute:g,second:g,timeZoneName:w},x={hour:g,minute:g,second:g,timeZoneName:T},C={hour:g,minute:g,hourCycle:"h23"},F={hour:g,minute:g,second:g,hourCycle:"h23"},L={hour:g,minute:g,second:g,hourCycle:"h23",timeZoneName:w},Z={hour:g,minute:g,second:g,hourCycle:"h23",timeZoneName:T},A={year:g,month:g,day:g,hour:g,minute:g},z={year:g,month:g,day:g,hour:g,minute:g,second:g},j={year:g,month:w,day:g,hour:g,minute:g},q={year:g,month:w,day:g,hour:g,minute:g,second:g},_={year:g,month:w,day:g,weekday:w,hour:g,minute:g},U={year:g,month:T,day:g,hour:g,minute:g,timeZoneName:w},R={year:g,month:T,day:g,hour:g,minute:g,second:g,timeZoneName:w},H={year:g,month:T,day:g,weekday:T,hour:g,minute:g,timeZoneName:T},P={year:g,month:T,day:g,weekday:T,hour:g,minute:g,second:g,timeZoneName:T};function W(e){return void 0===e}function J(e){return"number"==typeof e}function Y(e){return"number"==typeof e&&e%1==0}function G(){try{return"undefined"!=typeof Intl&&!!Intl.RelativeTimeFormat}catch(e){return!1}}function $(e,n,r){if(0!==e.length)return e.reduce(function(e,t){t=[n(t),t];return e&&r(e[0],t[0])===e[0]?e:t},null)[1]}function B(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function Q(e,t,n){return Y(e)&&t<=e&&e<=n}function K(e,t){void 0===t&&(t=2);t=e<0?"-"+(""+-e).padStart(t,"0"):(""+e).padStart(t,"0");return t}function X(e){if(!W(e)&&null!==e&&""!==e)return parseInt(e,10)}function ee(e){if(!W(e)&&null!==e&&""!==e)return parseFloat(e)}function te(e){if(!W(e)&&null!==e&&""!==e){e=1e3*parseFloat("0."+e);return Math.floor(e)}}function ne(e,t,n){void 0===n&&(n=!1);t=Math.pow(10,t);return(n?Math.trunc:Math.round)(e*t)/t}function re(e){return e%4==0&&(e%100!=0||e%400==0)}function ie(e){return re(e)?366:365}function oe(e,t){var n,r=(n=t-1)-(r=12)*Math.floor(n/r)+1;return 2==r?re(e+(t-r)/12)?29:28:[31,null,31,30,31,30,31,31,30,31,30,31][r-1]}function ue(e){var t=Date.UTC(e.year,e.month-1,e.day,e.hour,e.minute,e.second,e.millisecond);return e.year<100&&0<=e.year&&(t=new Date(t)).setUTCFullYear(t.getUTCFullYear()-1900),+t}function ae(e){var t=(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7,e=e-1,e=(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7;return 4==t||3==e?53:52}function se(e){return 99Gt.indexOf(s)&&Qt(this.matrix,u,d,i,s)}else J(u[s])&&(o[s]=u[s])}for(r in o)0!==o[r]&&(i[l]+=r===l?o[r]:o[r]/this.matrix[l][r]);return Bt(this,{values:i},!0).normalize()},e.negate=function(){if(!this.isValid)return this;for(var e={},t=0,n=Object.keys(this.values);te},e.isBefore=function(e){return!!this.isValid&&this.e<=e},e.contains=function(e){return!!this.isValid&&(this.s<=e&&this.e>e)},e.set=function(e){var t=void 0===e?{}:e,e=t.start,t=t.end;return this.isValid?c.fromDateTimes(e||this.s,t||this.e):this},e.splitAt=function(){var t=this;if(!this.isValid)return[];for(var e=arguments.length,n=new Array(e),r=0;r+this.e?this.e:s;o.push(c.fromDateTimes(u,s)),u=s,a+=1}return o},e.splitBy=function(e){var t=Kt.fromDurationLike(e);if(!this.isValid||!t.isValid||0===t.as("milliseconds"))return[];for(var n=this.s,r=1,i=[];n+this.e?this.e:o;i.push(c.fromDateTimes(n,o)),n=o,r+=1}return i},e.divideEqually=function(e){return this.isValid?this.splitBy(this.length()/e).slice(0,e):[]},e.overlaps=function(e){return this.e>e.s&&this.s=e.e)},e.equals=function(e){return!(!this.isValid||!e.isValid)&&(this.s.equals(e.s)&&this.e.equals(e.e))},e.intersection=function(e){if(!this.isValid)return this;var t=(this.s>e.s?this:e).s,e=(this.ee.e?this:e).e;return c.fromDateTimes(t,e)},c.merge=function(e){var t=e.sort(function(e,t){return e.s-t.s}).reduce(function(e,t){var n=e[0],e=e[1];return e?e.overlaps(t)||e.abutsStart(t)?[n,e.union(t)]:[n.concat([e]),t]:[n,t]},[[],null]),e=t[0],t=t[1];return t&&e.push(t),e},c.xor=function(e){for(var t=null,n=0,r=[],i=e.map(function(e){return[{time:e.s,type:"s"},{time:e.e,type:"e"}]}),o=k((e=Array.prototype).concat.apply(e,i).sort(function(e,t){return e.time-t.time}));!(u=o()).done;)var u=u.value,t=1===(n+="s"===u.type?1:-1)?u.time:(t&&+t!=+u.time&&r.push(c.fromDateTimes(t,u.time)),null);return c.merge(r)},e.difference=function(){for(var t=this,e=arguments.length,n=new Array(e),r=0;rae(n)?(t=n+1,o=1):t=n,s({weekYear:t,weekNumber:o,weekday:i},me(e))}function In(e){var t,n=e.weekYear,r=e.weekNumber,i=e.weekday,o=Nn(n,1,4),u=ie(n),o=7*r+i-o-3;o<1?o+=ie(t=n-1):uthis.valueOf(),r=rn(n?this:e,n?e:this,t,r);return n?r.negate():r},e.diffNow=function(e,t){return void 0===e&&(e="milliseconds"),void 0===t&&(t={}),this.diff(w.now(),e,t)},e.until=function(e){return this.isValid?en.fromDateTimes(this,e):this},e.hasSame=function(e,t){if(!this.isValid)return!1;var n=e.valueOf(),e=this.setZone(e.zone,{keepLocalTime:!0});return e.startOf(t)<=n&&n<=e.endOf(t)},e.equals=function(e){return this.isValid&&e.isValid&&this.valueOf()===e.valueOf()&&this.zone.equals(e.zone)&&this.loc.equals(e.loc)},e.toRelative=function(e){if(!this.isValid)return null;var t=(e=void 0===e?{}:e).base||w.fromObject({},{zone:this.zone}),n=e.padding?thisthis.set({month:1}).offset||this.offset>this.set({month:5}).offset)}},{key:"isInLeapYear",get:function(){return re(this.year)}},{key:"daysInMonth",get:function(){return oe(this.year,this.month)}},{key:"daysInYear",get:function(){return this.isValid?ie(this.year):NaN}},{key:"weeksInWeekYear",get:function(){return this.isValid?ae(this.weekYear):NaN}}],[{key:"DATE_SHORT",get:function(){return b}},{key:"DATE_MED",get:function(){return O}},{key:"DATE_MED_WITH_WEEKDAY",get:function(){return M}},{key:"DATE_FULL",get:function(){return N}},{key:"DATE_HUGE",get:function(){return D}},{key:"TIME_SIMPLE",get:function(){return E}},{key:"TIME_WITH_SECONDS",get:function(){return V}},{key:"TIME_WITH_SHORT_OFFSET",get:function(){return I}},{key:"TIME_WITH_LONG_OFFSET",get:function(){return x}},{key:"TIME_24_SIMPLE",get:function(){return C}},{key:"TIME_24_WITH_SECONDS",get:function(){return F}},{key:"TIME_24_WITH_SHORT_OFFSET",get:function(){return L}},{key:"TIME_24_WITH_LONG_OFFSET",get:function(){return Z}},{key:"DATETIME_SHORT",get:function(){return A}},{key:"DATETIME_SHORT_WITH_SECONDS",get:function(){return z}},{key:"DATETIME_MED",get:function(){return j}},{key:"DATETIME_MED_WITH_SECONDS",get:function(){return q}},{key:"DATETIME_MED_WITH_WEEKDAY",get:function(){return _}},{key:"DATETIME_FULL",get:function(){return U}},{key:"DATETIME_FULL_WITH_SECONDS",get:function(){return R}},{key:"DATETIME_HUGE",get:function(){return H}},{key:"DATETIME_HUGE_WITH_SECONDS",get:function(){return P}}]),w}();function ir(e){if(rr.isDateTime(e))return e;if(e&&e.valueOf&&J(e.valueOf()))return rr.fromJSDate(e);if(e&&"object"==typeof e)return rr.fromObject(e);throw new p("Unknown datetime argument: "+e+", of type "+typeof e)}return e.DateTime=rr,e.Duration=Kt,e.FixedOffsetZone=Ue,e.IANAZone=qe,e.Info=tn,e.Interval=en,e.InvalidZone=Re,e.Settings=Be,e.SystemZone=Ze,e.VERSION="2.3.1",e.Zone=Fe,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); \ No newline at end of file +var luxon=function(e){"use strict";function A(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[n++]}};throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var t=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(_(Error)),R=function(t){function e(e){return t.call(this,"Invalid DateTime: "+e.toMessage())||this}return i(e,t),e}(t),H=function(t){function e(e){return t.call(this,"Invalid Interval: "+e.toMessage())||this}return i(e,t),e}(t),W=function(t){function e(e){return t.call(this,"Invalid Duration: "+e.toMessage())||this}return i(e,t),e}(t),J=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(t),Y=function(t){function e(e){return t.call(this,"Invalid unit "+e)||this}return i(e,t),e}(t),u=function(e){function t(){return e.apply(this,arguments)||this}return i(t,e),t}(t),n=function(e){function t(){return e.call(this,"Zone is an abstract class")||this}return i(t,e),t}(t),t="numeric",r="short",a="long",G={year:t,month:t,day:t},$={year:t,month:r,day:t},B={year:t,month:r,day:t,weekday:r},Q={year:t,month:a,day:t},K={year:t,month:a,day:t,weekday:a},X={hour:t,minute:t},ee={hour:t,minute:t,second:t},te={hour:t,minute:t,second:t,timeZoneName:r},ne={hour:t,minute:t,second:t,timeZoneName:a},re={hour:t,minute:t,hourCycle:"h23"},ie={hour:t,minute:t,second:t,hourCycle:"h23"},oe={hour:t,minute:t,second:t,hourCycle:"h23",timeZoneName:r},ae={hour:t,minute:t,second:t,hourCycle:"h23",timeZoneName:a},ue={year:t,month:t,day:t,hour:t,minute:t},se={year:t,month:t,day:t,hour:t,minute:t,second:t},ce={year:t,month:r,day:t,hour:t,minute:t},le={year:t,month:r,day:t,hour:t,minute:t,second:t},fe={year:t,month:r,day:t,weekday:r,hour:t,minute:t},de={year:t,month:a,day:t,hour:t,minute:t,timeZoneName:r},he={year:t,month:a,day:t,hour:t,minute:t,second:t,timeZoneName:r},me={year:t,month:a,day:t,weekday:a,hour:t,minute:t,timeZoneName:a},ye={year:t,month:a,day:t,weekday:a,hour:t,minute:t,second:t,timeZoneName:a};function w(e){return void 0===e}function y(e){return"number"==typeof e}function ve(e){return"number"==typeof e&&e%1==0}function pe(){try{return"undefined"!=typeof Intl&&!!Intl.RelativeTimeFormat}catch(e){return!1}}function ge(e,n,r){if(0!==e.length)return e.reduce(function(e,t){t=[n(t),t];return e&&r(e[0],t[0])===e[0]?e:t},null)[1]}function d(e,t){return Object.prototype.hasOwnProperty.call(e,t)}function k(e,t,n){return ve(e)&&t<=e&&e<=n}function c(e,t){void 0===t&&(t=2);e=e<0?"-"+(""+-e).padStart(t,"0"):(""+e).padStart(t,"0");return e}function l(e){if(!w(e)&&null!==e&&""!==e)return parseInt(e,10)}function f(e){if(!w(e)&&null!==e&&""!==e)return parseFloat(e)}function we(e){if(!w(e)&&null!==e&&""!==e)return e=1e3*parseFloat("0."+e),Math.floor(e)}function ke(e,t,n){void 0===n&&(n=!1);t=Math.pow(10,t);return(n?Math.trunc:Math.round)(e*t)/t}function be(e){return e%4==0&&(e%100!=0||e%400==0)}function Te(e){return be(e)?366:365}function Se(e,t){var n,r=(r=t-1)-(n=12)*Math.floor(r/n)+1;return 2==r?be(e+(t-r)/12)?29:28:[31,null,31,30,31,30,31,31,30,31,30,31][r-1]}function Oe(e){var t=Date.UTC(e.year,e.month-1,e.day,e.hour,e.minute,e.second,e.millisecond);return e.year<100&&0<=e.year&&(t=new Date(t)).setUTCFullYear(t.getUTCFullYear()-1900),+t}function Me(e){var t=(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7,e=e-1,e=(e+Math.floor(e/4)-Math.floor(e/100)+Math.floor(e/400))%7;return 4==t||3==e?53:52}function Ne(e){return 99E.indexOf(s)&&tn(this.matrix,a,d,i,s)}else y(a[s])&&(o[s]=a[s])}for(r in o)0!==o[r]&&(i[l]+=r===l?o[r]:o[r]/this.matrix[l][r]);return V(this,{values:i},!0).normalize()},e.negate=function(){if(!this.isValid)return this;for(var e={},t=0,n=Object.keys(this.values);te},e.isBefore=function(e){return!!this.isValid&&this.e<=e},e.contains=function(e){return!!this.isValid&&(this.s<=e&&this.e>e)},e.set=function(e){var e=void 0===e?{}:e,t=e.start,e=e.end;return this.isValid?c.fromDateTimes(t||this.s,e||this.e):this},e.splitAt=function(){var t=this;if(!this.isValid)return[];for(var e=arguments.length,n=new Array(e),r=0;r+this.e?this.e:s;o.push(c.fromDateTimes(a,s)),a=s,u+=1}return o},e.splitBy=function(e){var t=I.fromDurationLike(e);if(!this.isValid||!t.isValid||0===t.as("milliseconds"))return[];for(var n=this.s,r=1,i=[];n+this.e?this.e:o;i.push(c.fromDateTimes(n,o)),n=o,r+=1}return i},e.divideEqually=function(e){return this.isValid?this.splitBy(this.length()/e).slice(0,e):[]},e.overlaps=function(e){return this.e>e.s&&this.s=e.e)},e.equals=function(e){return!(!this.isValid||!e.isValid)&&(this.s.equals(e.s)&&this.e.equals(e.e))},e.intersection=function(e){if(!this.isValid)return this;var t=(this.s>e.s?this:e).s,e=(this.ee.e?this:e).e;return c.fromDateTimes(t,e)},c.merge=function(e){var e=e.sort(function(e,t){return e.s-t.s}).reduce(function(e,t){var n=e[0],e=e[1];return e?e.overlaps(t)||e.abutsStart(t)?[n,e.union(t)]:[n.concat([e]),t]:[n,t]},[[],null]),t=e[0],e=e[1];return e&&t.push(e),t},c.xor=function(e){for(var t,n=null,r=0,i=[],e=e.map(function(e){return[{time:e.s,type:"s"},{time:e.e,type:"e"}]}),o=g((t=Array.prototype).concat.apply(t,e).sort(function(e,t){return e.time-t.time}));!(a=o()).done;)var a=a.value,n=1===(r+="s"===a.type?1:-1)?a.time:(n&&+n!=+a.time&&i.push(c.fromDateTimes(n,a.time)),null);return c.merge(i)},e.difference=function(){for(var t=this,e=arguments.length,n=new Array(e),r=0;rMe(n)?(t=n+1,i=1):t=n,s({weekYear:t,weekNumber:i,weekday:r},Ce(e))}function Vn(e){var t,n=e.weekYear,r=e.weekNumber,i=e.weekday,o=Mn(n,1,4),a=Te(n),r=7*r+i-o-3,i=(r<1?r+=Te(t=n-1):athis.valueOf(),e=un(r?this:e,r?e:this,t,n);return r?e.negate():e},e.diffNow=function(e,t){return void 0===e&&(e="milliseconds"),void 0===t&&(t={}),this.diff(p.now(),e,t)},e.until=function(e){return this.isValid?rn.fromDateTimes(this,e):this},e.hasSame=function(e,t){if(!this.isValid)return!1;var n=e.valueOf(),e=this.setZone(e.zone,{keepLocalTime:!0});return e.startOf(t)<=n&&n<=e.endOf(t)},e.equals=function(e){return this.isValid&&e.isValid&&this.valueOf()===e.valueOf()&&this.zone.equals(e.zone)&&this.loc.equals(e.loc)},e.toRelative=function(e){if(!this.isValid)return null;var t=(e=void 0===e?{}:e).base||p.fromObject({},{zone:this.zone}),n=e.padding?thisthis.set({month:1,day:1}).offset||this.offset>this.set({month:5}).offset)}},{key:"isInLeapYear",get:function(){return be(this.year)}},{key:"daysInMonth",get:function(){return Se(this.year,this.month)}},{key:"daysInYear",get:function(){return this.isValid?Te(this.year):NaN}},{key:"weeksInWeekYear",get:function(){return this.isValid?Me(this.weekYear):NaN}}],[{key:"DATE_SHORT",get:function(){return G}},{key:"DATE_MED",get:function(){return $}},{key:"DATE_MED_WITH_WEEKDAY",get:function(){return B}},{key:"DATE_FULL",get:function(){return Q}},{key:"DATE_HUGE",get:function(){return K}},{key:"TIME_SIMPLE",get:function(){return X}},{key:"TIME_WITH_SECONDS",get:function(){return ee}},{key:"TIME_WITH_SHORT_OFFSET",get:function(){return te}},{key:"TIME_WITH_LONG_OFFSET",get:function(){return ne}},{key:"TIME_24_SIMPLE",get:function(){return re}},{key:"TIME_24_WITH_SECONDS",get:function(){return ie}},{key:"TIME_24_WITH_SHORT_OFFSET",get:function(){return oe}},{key:"TIME_24_WITH_LONG_OFFSET",get:function(){return ae}},{key:"DATETIME_SHORT",get:function(){return ue}},{key:"DATETIME_SHORT_WITH_SECONDS",get:function(){return se}},{key:"DATETIME_MED",get:function(){return ce}},{key:"DATETIME_MED_WITH_SECONDS",get:function(){return le}},{key:"DATETIME_MED_WITH_WEEKDAY",get:function(){return fe}},{key:"DATETIME_FULL",get:function(){return de}},{key:"DATETIME_FULL_WITH_SECONDS",get:function(){return he}},{key:"DATETIME_HUGE",get:function(){return me}},{key:"DATETIME_HUGE_WITH_SECONDS",get:function(){return ye}}]),p}();function nr(e){if(L.isDateTime(e))return e;if(e&&e.valueOf&&y(e.valueOf()))return L.fromJSDate(e);if(e&&"object"==typeof e)return L.fromObject(e);throw new u("Unknown datetime argument: "+e+", of type "+typeof e)}return e.DateTime=L,e.Duration=I,e.FixedOffsetZone=b,e.IANAZone=p,e.Info=on,e.Interval=rn,e.InvalidZone=et,e.Settings=S,e.SystemZone=$e,e.VERSION="2.5.2",e.Zone=m,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); \ No newline at end of file diff --git a/static/js/settings.js b/static/js/settings.js index c2cad49c..2e92129f 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -638,6 +638,23 @@ $('#selectController').on('change', function() { $('#controller_config').load("/settings/controller_card", {"selected" : this.value}); }); +function sendTestNotification() { + $.ajax({ + url: '/api/set/notify/Test/req/true', + type: 'GET', + contentType: "application/json; charset=utf-8", + traditional: true, + success: function(data) { + //console.log('Data: ' + data['result'] + data['message']); + if (data['result'] == 'OK') { + alert('Test notification sent.'); + } else { + alert('Something went wrong. Try again.'); + } + } + }); +}; + // On page load... $(document).ready(function() { // Setup Color Picker for all elements whose id starts with 'clrpck_' @@ -791,4 +808,34 @@ $(document).ready(function() { }; }); + // Enable / Disable the Startup Exit Temp feature + var startup_exit_temp = document.getElementById('startup_exit_temp').value; + if (startup_exit_temp == 0) { + startup_exit_temp = 140; // Set to default of 140 if not already set + } + $('#startup_exit_temp_toggle').change(function() { + if(document.getElementById("startup_exit_temp_toggle").checked) { + $('#startup_exit_temp').val(startup_exit_temp); // Default value for exit temp + $('#startup_exit_temp_input').slideDown(100); + } else { + $('#startup_exit_temp_input').slideUp(100); + $('#startup_exit_temp').val(0); // Zero disables this feature + }; + }); + + // Enable / Disable the Prime on Startup Feature + var prime_on_startup = document.getElementById('prime_on_startup').value; + if (prime_on_startup == 0) { + prime_on_startup = 10; // Set to default of 10g if not already set + } + $('#prime_on_startup_toggle').change(function() { + if(document.getElementById("prime_on_startup_toggle").checked) { + $('#prime_on_startup').val(prime_on_startup); // Default value for priming + $('#prime_on_startup_input').slideDown(100); + } else { + $('#prime_on_startup_input').slideUp(100); + $('#prime_on_startup').val(0); // Zero disables this feature + }; + }); + }); // End of document ready function \ No newline at end of file diff --git a/static/js/timer.js b/static/js/timer.js index 03e81dcc..40b5deb8 100644 --- a/static/js/timer.js +++ b/static/js/timer.js @@ -1,39 +1,70 @@ // Timer Bar JS -// Puts timer status in the top-bar + +// Global Variables + +var timerStart = 0; +var timerPaused = 0; +var timerEnd = 0; +var timerShutdown = false; +var timerKeepWarm = false; +var timerFinishedFlag = false; +var timerUpdateTimerValue ; // Interval used to refresh the displayed time +var timerUpdateTimerStatus ; // Interval used to get the timer status +var timerSuppressUpdate = false; // Suppress update to timer buttons right after a button click +var timerUserHidden = false; // Flag that keeps the timer hidden if the user chose hidden + +// Toggles visibility of the timer status in the top-bar (triggered by pressing button in navbar) function timerToggle() { if ($("#toggleTimer").html() == 'hidden') { + timerUserHidden = false; $("#timer_bar").slideDown(); $("#toggleTimer").html('unhidden'); } else { + timerUserHidden = true; $("#timer_bar").slideUp(); $("#toggleTimer").html('hidden'); }; }; -function countdown(timerEnd, timerPaused) { - // Set the time we're counting down to - var countDownDate = timerEnd * 1000; +function timerModal() { + // Show timer settings modal + $('#timerModal').modal('show'); +}; - // Get time now - var now = new Date().getTime(); - - // Find the distance between now and the count down time - if(timerPaused == 0) { - var distance = countDownDate - now; +function timerSecondsRemaining(timerEnd, timerPaused) { + if (timerStart != 0) { + // Set the time we're counting down to + var countDownDate = timerEnd * 1000; + // Get time now + var now = new Date().getTime(); + + // Find the distance between now and the count down time + if(timerPaused == 0) { + var distance = countDownDate - now; + } else { + var distance = countDownDate - ( timerPaused * 1000 ); + }; } else { - var distance = countDownDate - ( timerPaused * 1000 ); + distance = 0; }; - // Time calculations for hours, minutes and seconds - var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); - var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); - var seconds = Math.floor((distance % (1000 * 60)) / 1000); + return distance; +}; +function timerPrettyTime(remainingSeconds) { var display = ""; - if ((hours < 0) || (minutes < 0) || (seconds < 0)) { + if (remainingSeconds < 0) { + timerFinished(); + display = "Finished"; + } else if (remainingSeconds == 0) { display = "--:--:--"; } else { + // Time calculations for hours, minutes and seconds + var hours = Math.floor((remainingSeconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + var minutes = Math.floor((remainingSeconds % (1000 * 60 * 60)) / (1000 * 60)); + var seconds = Math.floor((remainingSeconds % (1000 * 60)) / 1000); + if (hours < 10) { display += "0"; } @@ -47,175 +78,219 @@ function countdown(timerEnd, timerPaused) { } display += seconds; }; + return display; +}; - $("#timer_time_remaining").html(display); - - return distance -}; - -function timerSetup() { - // Turn Timer Button in Navbar Yellow - document.getElementById("timerButton").className = "btn btn-outline-warning border-secondary"; - // Setup Button Listeners - $("#timer_start").click(function(){ - req = $.ajax({ - url : '/timer', - type : 'POST', - data : { 'input' : 'timer_start' } - }); - req.done(function(data) { - $("#timer_pause").show(); - $("#timer_start").hide(); - }); - }); +function timerUpdateTimeRemaining() { + if (timerFinishedFlag != true) { + var remainingSeconds= timerSecondsRemaining(timerEnd, timerPaused); + var prettyTime = timerPrettyTime(remainingSeconds); + $("#timer_time_remaining").html(prettyTime); + }; +}; - $("#timer_pause").click(function(){ - req = $.ajax({ - url : '/timer', - type : 'POST', - data : { 'input' : 'timer_pause' } - }); - req.done(function(data) { - $("#timer_pause").hide(); - $("#timer_start").show(); - }); +function timerPause() { + timerSuppressUpdate = true; + timerButtonsPaused(); + req = $.ajax({ + url : '/api/set/timer/pause', + type : 'POST', + }); + req.done(function(response) { + if (response.result == 'OK') { + console.log('Timer paused.'); + } else { + console.log('Error pausing timer: ' + response.message); + }; }); +}; - $("#timer_stop").click(function(){ - req = $.ajax({ - url : '/timer', - type : 'POST', - data : { 'input' : 'timer_stop' } - }); - req.done(function(data) { - clearInterval(timerInterval); - $("#timer_btn_grp").html(""); - document.getElementById("timerButton").className = "btn btn-outline-secondary border-secondary"; - }); +function timerUnPause() { + timerSuppressUpdate = true; + timerButtonsActive(); + req = $.ajax({ + url : '/api/set/timer/start', + type : 'POST', + }); + req.done(function(response) { + if (response.result == 'OK') { + console.log('Timer unpaused.'); + } else { + console.log('Error unpausing timer: ' + response.message); + }; }); +}; - $("#timer_hide").click(function(){ - clearInterval(timerInterval); - $("#timer_bar").slideUp(); +function timerStop() { + timerSuppressUpdate = true; + timerButtonsInactive(); + req = $.ajax({ + url : '/api/set/timer/stop', + type : 'POST', + }); + req.done(function(response) { + if (response.result == 'OK') { + console.log('Timer stopped.'); + document.getElementById("timerButton").className = "btn btn-outline-secondary border-secondary"; + } else { + console.log('Error stopping timer: ' + response.message); + }; }); +}; +function timerStatus() { + // Get Current Timer Data + req = $.ajax({ + url : '/api/get/timer', + type : 'GET' + }); - // Init Variables - var timerEnd = 0; - var timerPaused = 0; + req.done(function(response) { + if (response.data.start != timerStart) { + // Timer has likely been updated elsewhere + timerFinishedFlag = false; // Clear finished flag to allow the timer to start updating again + timerUserHidden = false; // Clear the user hidden flag, in case the user had previously hidden the timer + // Show the Timer + $("#timer_bar").slideDown(); + $("#toggleTimer").html('unhidden'); + //document.getElementById("timerButton").className = "btn btn-outline-secondary border-warning text-warning"; + }; + timerStart = response.data.start; + timerPaused = response.data.paused; + timerEnd = response.data.end; + timerShutdown = response.data.shutdown; + timerKeepWarm = response.data.keep_warm; - // Update the count down every 1 second - var timerInterval = setInterval(function() { - if(timerEnd != 0) { - distance = countdown(timerEnd, timerPaused); + secondsRemaining = timerSecondsRemaining(timerEnd, timerPaused); // If the count down is finished, end updates, and display finished - if (distance < 0) { - clearInterval(timerInterval); - $("#timer_btn_grp").html(""); - document.getElementById("timerButton").className = "btn btn-outline-secondary border-secondary"; + if (secondsRemaining < 0) { + timerFinished(); } } - // Get Current Timer Data - req = $.ajax({ - url : '/timer', - type : 'GET' - }); - - req.fail(function(xhr, error) { - //Ajax request failed, ignore and keep clock running. - }); - - req.done(function(data) { - if((data.paused != timerPaused) && (data.paused != 0)) { - // Timer is paused - timerPaused = data.paused; - timerEnd = data.end; - distance = countdown(timerEnd, timerPaused); - $("#timer_pause").hide(); - $("#timer_start").show(); - } else if((data.paused != timerPaused) && (data.paused == 0)) { - // Timer is unpaused - timerPaused = data.paused; - timerEnd = data.end; - distance = countdown(timerEnd, timerPaused); - $("#timer_pause").show(); - $("#timer_start").hide(); - } else if(data.end != timerEnd) { - // Timer has new end time / so update - timerEnd = data.end; - distance = countdown(timerEnd, timerPaused); - $("#timer_pause").show(); - $("#timer_start").hide(); - } - }); - }, 1000); + }); }; -$(document).ready(function(){ - // Get Intial Timer Data - req = $.ajax({ - url : '/timer', - type : 'GET' - }); +function timerFinished() { + timerFinishedFlag = true; + timerSuppressUpdate = true; + timerStart = 0; + timerButtonsInactive(); + console.log('Timer Expired.'); + $("#timer_time_remaining").html("Finished"); + document.getElementById("timerButton").className = "btn btn-outline-secondary border-secondary"; +}; - req.done(function(data) { - if(data.start != 0) { - // Timer is active, so let's set things up - if(data.paused != 0) { - // Timer is paused - $("#timer_pause").hide(); - } else { - // Timer is running - $("#timer_start").hide(); - } - // Show the Timer - $("#timer_bar").slideDown(); - $("#toggleTimer").html('unhidden'); - timerSetup(); +// Depending on state, show the correct button set +function timerShowButtons() { + if(timerFinishedFlag == true) { + timerButtonsInactive(); + } else if(timerStart != 0) { + // Timer is active, so let's set things up + if(timerPaused != 0) { + // Timer is paused + timerButtonsPaused(); + } else { + // Timer is active + timerButtonsActive(); } - }); -}); + } else { + // Timer is inactive + timerButtonsInactive(); + }; +}; + +function timerButtonsActive() { + // Timer is running + $("#timerStartGroup").hide(); + $("#timerPausedGroup").hide(); + $("#timerActiveGroup").show(); + document.getElementById("timerButton").className = "btn btn-outline-secondary border-warning text-warning"; +}; + +function timerButtonsInactive() { + // Timer is inactive + $("#timerActiveGroup").hide(); + $("#timerPausedGroup").hide(); + $("#timerStartGroup").show(); +}; + +function timerButtonsPaused() { + // Timer is paused + $("#timerActiveGroup").hide(); + $("#timerStartGroup").hide(); + $("#timerPausedGroup").show(); +}; // Launch a timer -$("#timer_launch").click(function(){ +function timerLaunch() { + timerButtonsActive(); + timerFinishedFlag = false; + timerSuppressUpdate = true; + + clearTimeout(timerUpdateTimerStatus); + timerUpdateTimerStatus = setTimeout(timerRefresh, 1000); // When timer is active, refresh faster + var timerHours = $("#hoursInputId").val(); var timerMins = $("#minsInputId").val(); - var timerShutdown = false; - var timerKeepWarm = false; + var timerSeconds = (timerHours * 3600) + (timerMins * 60); + if ($('#shutdownTimer').is(":checked")){ timerShutdown = true; - } + }; if ($('#keepWarmTimer').is(":checked")){ timerKeepWarm = true; - } - // For Debug - //console.log("Hours: " + timerHours); - //console.log("Mins: " + timerMins); - //console.log("Shutdown: " + timerShutdown); + }; + req = $.ajax({ - url : '/timer', + url : '/api/set/timer/start/'+timerSeconds, type : 'POST', - data : { 'input' : 'timer_start', - 'hoursInputRange' : timerHours, - 'minsInputRange' : timerMins, - 'shutdownTimer' : timerShutdown, - 'keepWarmTimer' : timerKeepWarm - } + data : {} }); - req.done(function(data) { - $("#timer_bar").slideUp(); - $("#timer_btn_grp").html("\ - \ - \ - \ - \ - "); - $("#timer_pause").hide(); - $("#timer_start").hide(); - $("#timer_bar").slideDown(); - $("#timerButton") - timerSetup(); + req.done(function(response) { + if (response.result == 'OK') { + console.log('Timer Launched Successfully: ' + response.message); + } else { + console.log('Error launching timer: ' + response.message); + }; }); -}); \ No newline at end of file +}; + +// Prevent both KeepWarm and Shutdown to be selected at the same time +function timerShutdownSelect() { + if ($('#keepWarmTimer').is(":checked")){ + $('#keepWarmTimer').prop('checked', false); + }; +}; + +// Prevent both KeepWarm and Shutdown to be selected at the same time +function timerKeepWarmSelect() { + if ($('#shutdownTimer').is(":checked")){ + $('#shutdownTimer').prop('checked', false); + }; +}; + +function timerRefresh() { + timerStatus(); + if (timerSuppressUpdate == false) { + timerShowButtons(); + }; + timerSuppressUpdate = false; + //console.log('Refreshed: ' + new Date().getTime()); + if (timerStart != 0) { + clearTimeout(timerUpdateTimerStatus); + timerUpdateTimerStatus = setTimeout(timerRefresh, 1000); // When timer is active, refresh faster + } else { + clearTimeout(timerUpdateTimerStatus); + timerUpdateTimerStatus = setTimeout(timerRefresh, 5000); // When timer is inactive, refresh slower + }; +}; + +// On document ready +$(document).ready(function(){ + timerUpdateTimerStatus = setTimeout(timerRefresh, 100); // Start the timeout for refreshing the timer status, drawing buttons, etc. + timerUpdateTimerValue = setInterval(timerUpdateTimeRemaining, 250); // Start the interval for updating the displayed timer value +}); + + diff --git a/static/js/tuner.js b/static/js/tuner.js new file mode 100644 index 00000000..eb217945 --- /dev/null +++ b/static/js/tuner.js @@ -0,0 +1,479 @@ +// === Global Variables === +var tunerProbeSelected = 'none'; +var tunerProbeSelectedName = 'none'; +var tunerProbeReference = 'none'; +var tunerProbeReferenceName = 'none'; +var tunerFetchTrValues; +var tunerAutoStatus; + +// In Manual Tuning, Pause updating Tr value when requested +var tunerManualHighPaused = false; +var tunerManualMediumPaused = false; +var tunerManualLowPaused = false; + +var tunerManualHighTr = 0; +var tunerManualHighTemp = 0; +var tunerManualMediumTr = 0; +var tunerManualMediumTemp = 0; +var tunerManualLowTr = 0; +var tunerManualLowTemp = 0; + +var tunerRunning = false; +var _wasPageCleanedUp = false; + +// Chart Data +var tunerChartData = { + labels: [], + datasets: [ + { + label: "Stienhart-Hart Curve", + fill: true, + lineTension: 0.1, + backgroundColor: "rgba(0,127,0,0.4)", + borderColor: "rgba(0,127,0,1)", + borderCapStyle: 'butt', + borderDash: [], + borderDashOffset: 0.0, + borderJoinStyle: 'miter', + pointBorderColor: "rgba(0,127,0,1)", + pointBackgroundColor: "#fff", + pointBorderWidth: 1, + pointHoverRadius: 5, + pointHoverBackgroundColor: "rgba(0,127,0,0.4)", + pointHoverBorderColor: "rgba(0,127,0,1)", + pointHoverBorderWidth: 2, + pointRadius: 1, + pointHitRadius: 10, + data: [], + spanGaps: false, + }, + ] +} + +var tunerResultsChart = new Chart(document.getElementById('tunerResultsChart'), { + type: 'line', + data: tunerChartData, + options: { + scales: { + x: { + title: { + display: true, + text: 'Temperature' + }, + }, + y: { + title: { + display: true, + text: 'Resistance (Ohms)', + ticks: {}, + beginAtZero:true + }, + } + }, + responsive: true, + maintainAspectRatio: false, + } +}); + +// === Function Definitions === +function tunerCheckState() { + // Call API to check state of system +}; + +// === MANUAL Tuner Functions === +function tunerManualInstructions() { + // User Selected Manual Tuning Mode + // Hide Welcome Row & Show Manual Tuning Instructions + $("#tuner_welcome_row").hide(); + $("#tuner_back_to_welcome_row").show(); + var senddata = { + 'command' : 'render', + 'value' : 'manual_instruction_card', + }; + $('#tuner_instruction_row').load('/tuner', senddata); + $('#tuner_instruction_row').show(); +}; + +function tunerEnableManualStart(sel) { + tunerProbeSelected = sel.value; + tunerProbeSelectedName = sel.options[sel.selectedIndex].text; + + // Start button is disabled until the user selects a probe to tune + if (tunerProbeSelected != 'none') { + $("#tuner_manual_start_btn").prop('disabled', false); + } else { + $("#tuner_manual_start_btn").prop('disabled', true); + }; +}; + +function tunerStartManualTool() { + tunerRunning = true; + // Hide the instructions and start the Manual Tool + $("#tuner_instruction_row").hide(); + $("#tuner_back_to_welcome_row").show(); + + $('#tuner_manual_tool_title_probe_selected').html(tunerProbeSelectedName); + $("#tuner_tool_manual_title_row").show(); + + var senddata = { + 'command' : 'render', + 'value' : 'manual_tool', + }; + $('#tuner_tool_row').load('/tuner', senddata); + $("#tuner_tool_row").show(); + + var senddata = { + 'command' : 'render', + 'value' : 'manual_finish_btn', + }; + $('#tuner_finish_btn_row').load('/tuner', senddata); + $("#tuner_finish_btn_row").show(); + + tunerFetchTrValues = setInterval(tunerUpdateTr,1000); +}; + +function tunerManualPause(segment) { + //console.log('Pausing ' + segment); + if (segment == 'High') { + tunerManualHighPaused = true; + }; + if (segment == 'Medium') { + tunerManualMediumPaused = true; + }; + if (segment == 'Low') { + tunerManualLowPaused = true; + }; + $('#tuner_manual_'+segment+'_pause_btn').html('Unpause'); + //TODO: Change button color to solid + document.getElementById('tuner_manual_'+segment+'_pause_btn').className = "btn btn-info btn-block"; + //TODO: Change onClick to tunerManualUnpause(segment) + document.getElementById('tuner_manual_'+segment+'_pause_btn').onclick = function () { tunerManualUnpause(segment); }; + tunerManualCheckComplete(); +}; + +function tunerManualUnpause(segment) { + //console.log('UnPausing ' + segment); + if (segment == 'High') { + tunerManualHighPaused = false; + }; + if (segment == 'Medium') { + tunerManualMediumPaused = false; + }; + if (segment == 'Low') { + tunerManualLowPaused = false; + }; + $('#tuner_manual_'+segment+'_pause_btn').html('Pause'); + //TODO: Change button color to solid + document.getElementById('tuner_manual_'+segment+'_pause_btn').className = "btn btn-outline-info btn-block"; + //TODO: Change onClick to tunerManualUnpause(segment) + document.getElementById('tuner_manual_'+segment+'_pause_btn').onclick = function () { tunerManualPause(segment); }; + tunerManualCheckComplete(); +}; + +function tunerManualCheckComplete() { + // Check if all criteria for finishing / calculating results are entered + if (tunerManualHighPaused && tunerManualMediumPaused && tunerManualLowPaused) { + $("#tuner_manual_finish_btn").prop('disabled', false); + } else { + $("#tuner_manual_finish_btn").prop('disabled', true); + }; +}; + +function tunerManualFinish() { + tunerManualHighTemp = $('#tuner_manual_input_High_t').val(); + tunerManualHighTr = $('#tuner_manual_input_High_tr').val(); + tunerManualMediumTemp = $('#tuner_manual_input_Medium_t').val(); + tunerManualMediumTr = $('#tuner_manual_input_Medium_tr').val(); + tunerManualLowTemp = $('#tuner_manual_input_Low_t').val(); + tunerManualLowTr = $('#tuner_manual_input_Low_tr').val(); + + if((tunerManualHighTemp >= 0) && (tunerManualMediumTemp >= 0) && (tunerManualLowTemp >= 0)) { + $('#tuner_finish_btn_row').hide(); // Hide Finish Button + $('#tuner_manual_High_pause_btn').prop('disabled', true); // Disable pause buttons + $('#tuner_manual_Medium_pause_btn').prop('disabled', true); // Disable pause buttons + $('#tuner_manual_Low_pause_btn').prop('disabled', true); // Disable pause buttons + + clearInterval(tunerFetchTrValues); // Stop gathering data + // Get all data + postdata = { + 'command' : 'manual_finish', + 'tunerManualHighTemp' : tunerManualHighTemp, + 'tunerManualHighTr' : tunerManualHighTr, + 'tunerManualMediumTemp' : tunerManualMediumTemp, + 'tunerManualMediumTr' : tunerManualMediumTr, + 'tunerManualLowTemp' : tunerManualLowTemp, + 'tunerManualLowTr' : tunerManualLowTr, + }; + + $.ajax({ + url : '/tuner', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (data) { + //console.log('Call Success: '); + //console.log(' - labels: ' + data.labels); + //console.log(' - data: ' + data.chart_data); + //console.log(' - a: ' + data.coefficients.a); + //console.log(' - b: ' + data.coefficients.b); + //console.log(' - c: ' + data.coefficients.c); + tunerRunning = false; + + $('#tuner_profile_A').val(data.coefficients.a); + $('#tuner_profile_B').val(data.coefficients.b); + $('#tuner_profile_C').val(data.coefficients.c); + tunerResultsChart.data.labels = data.labels; + tunerResultsChart.data.datasets[0].data = data.chart_data; + tunerResultsChart.update(); + if (data.chart_data.length != 0) { + // Show the chart if data exists + $('#tunerResultsChartWrapper').show(); + $('#tunerResultsChartFailed').hide(); + } else { + // If no data was returned, unable to display chart + $('#tunerResultsChartFailed').show(); + $('#tunerResultsChartWrapper').hide(); + }; + $('#tunerSaveApplyBtn').val(tunerProbeSelected); // Update the Save & Apply button with Probe name + $('#tuner_finish_row').show(); + } + }); + }; + +}; + +// === Auto Tuner Functions === +function tunerAutoInstructions() { + // User Selected Auto Tuning Mode + // Hide Welcome Row & Show Manual Tuning Instructions + $("#tuner_welcome_row").hide(); + $("#tuner_back_to_welcome_row").show(); + var senddata = { + 'command' : 'render', + 'value' : 'auto_instruction_card', + }; + $('#tuner_instruction_row').load('/tuner', senddata); + $('#tuner_instruction_row').show(); +}; + +function tunerAutoSelectProbe(sel) { + tunerProbeSelected = sel.value; + tunerProbeSelectedName = sel.options[sel.selectedIndex].text; + + tunerAutoCheckStartReqs(); +}; + +function tunerAutoSelectReference(sel) { + tunerProbeReference = sel.value; + tunerProbeReferenceName = sel.options[sel.selectedIndex].text; + + tunerAutoCheckStartReqs(); +}; + +function tunerAutoCheckStartReqs() { + // Start button is disabled until the user selects a probe to tune and reference + if ((tunerProbeSelected != 'none') && (tunerProbeReference != 'none') && (tunerProbeSelected != tunerProbeReference)){ + $("#tuner_auto_start_btn").prop('disabled', false); + } else { + $("#tuner_auto_start_btn").prop('disabled', true); + }; +}; + +function tunerStartAutoTool() { + tunerRunning = true; + + // Hide the instructions and start the Autotune Tool + $("#tuner_instruction_row").hide(); + $("#tuner_back_to_welcome_row").show(); + + $('#tuner_auto_tool_title_probe_selected').html(tunerProbeSelectedName); + $('#tuner_auto_tool_title_probe_reference').html(tunerProbeReferenceName); + $("#tuner_tool_auto_title_row").show(); + + var senddata = { + 'command' : 'render', + 'value' : 'auto_tool', + }; + $('#tuner_tool_row').load('/tuner', senddata); + $("#tuner_tool_row").show(); + + var senddata = { + 'command' : 'render', + 'value' : 'auto_finish_btn', + }; + $('#tuner_finish_btn_row').load('/tuner', senddata); + $("#tuner_finish_btn_row").show(); + + tunerAutoStatus = setInterval(tunerAutoUpdateStatus,1000); +}; + +function tunerAutoFinish() { + $('#tuner_finish_btn_row').hide(); // Hide Finish Button + clearInterval(tunerAutoStatus); // Stop gathering data + document.getElementById("tunerAutoIcon").classList.remove('fa-spin'); + + // Get all data + postdata = { + 'command' : 'auto_finish', + 'tunerManualHighTemp' : tunerManualHighTemp, + 'tunerManualHighTr' : tunerManualHighTr, + 'tunerManualMediumTemp' : tunerManualMediumTemp, + 'tunerManualMediumTr' : tunerManualMediumTr, + 'tunerManualLowTemp' : tunerManualLowTemp, + 'tunerManualLowTr' : tunerManualLowTr, + }; + + $.ajax({ + url : '/tuner', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (data) { + //console.log('Call Success: '); + //console.log(' - labels: ' + data.labels); + //console.log(' - data: ' + data.chart_data); + //console.log(' - a: ' + data.coefficients.a); + //console.log(' - b: ' + data.coefficients.b); + //console.log(' - c: ' + data.coefficients.c); + tunerRunning = false; + + $('#tuner_profile_A').val(data.coefficients.a); + $('#tuner_profile_B').val(data.coefficients.b); + $('#tuner_profile_C').val(data.coefficients.c); + tunerResultsChart.data.labels = data.labels; + tunerResultsChart.data.datasets[0].data = data.chart_data; + tunerResultsChart.update(); + if (data.chart_data.length != 0) { + // Show the chart if data exists + $('#tunerResultsChartWrapper').show(); + $('#tunerResultsChartFailed').hide(); + } else { + // If no data was returned, unable to display chart + $('#tunerResultsChartFailed').show(); + $('#tunerResultsChartWrapper').hide(); + }; + $('#tunerSaveApplyBtn').val(tunerProbeSelected); // Update the Save & Apply button with Probe name + $('#tuner_finish_row').show(); + } + }); +}; + + + +// === Generic Tuner Functions === + +function tunerUpdateTr() { + var postdata = { + 'probe_selected' : tunerProbeSelected, + 'command' : 'read_tr' + }; + + $.ajax({ + url : '/tuner', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (data) { + if (tunerManualHighPaused == false) { + $("#tuner_manual_input_High_tr").val(data.trohms); + }; + if (tunerManualMediumPaused == false) { + $("#tuner_manual_input_Medium_tr").val(data.trohms); + }; + if (tunerManualLowPaused == false) { + $("#tuner_manual_input_Low_tr").val(data.trohms); + }; + } + }); +}; + +function tunerAutoUpdateStatus() { + var postdata = { + 'probe_selected' : tunerProbeSelected, + 'probe_reference' : tunerProbeReference, + 'command' : 'read_auto_status' + }; + + $.ajax({ + url : '/tuner', + type : 'POST', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function (data) { + $('#tuner_auto_ref_temp').html(data.current_temp); + $('#tuner_auto_probe_tr').html(data.current_tr); + $('#tuner_auto_high_tr').html(data.high_tr); + $('#tuner_auto_medium_tr').html(data.medium_tr); + $('#tuner_auto_low_tr').html(data.low_tr); + $('#tuner_auto_high_temp').html(data.high_temp); + $('#tuner_auto_medium_temp').html(data.medium_temp); + $('#tuner_auto_low_temp').html(data.low_temp); + + tunerManualHighTr = data.high_tr; + tunerManualHighTemp = data.high_temp; + tunerManualMediumTr = data.medium_tr; + tunerManualMediumTemp = data.medium_temp; + tunerManualLowTr = data.low_tr; + tunerManualLowTemp = data.low_temp; + + if (data.ready) { + $('#tuner_auto_finish_btn').prop('disabled', false); + } else { + $('#tuner_auto_finish_btn').prop('disabled', true); + }; + } + }); +}; + +// === Listeners === +// Adapted from https://stackoverflow.com/questions/4945932/window-onbeforeunload-ajax-request-in-chrome +// Written by Stack Overflow user Mohoch +function pageCleanup() +{ + clearInterval(tunerFetchTrValues); + clearInterval(tunerAutoStatus); + //console.log('Page Cleanup Run. Tuner Running: ' + tunerRunning); + + if (!_wasPageCleanedUp) + { + postdata = { + 'command' : 'stop_tuning' + }; + $.ajax({ + type: 'POST', + async: false, + url: '/tuner', + data : JSON.stringify(postdata), + contentType: "application/json; charset=utf-8", + traditional: true, + success: function () + { + _wasPageCleanedUp = true; + tunerRunning = false; + } + }); + } +} + +$(window).on('beforeunload', function () +{ + //this will work only for Chrome + pageCleanup(); +}); + +$(window).on("unload", function () +{ + //this will work for other browsers + pageCleanup(); +}); + + +// === Document Ready === +$(document).ready(function(){ + tunerCheckState(); +}); // End of Document Ready Function + \ No newline at end of file diff --git a/templates/_macro_control_panel.html b/templates/_macro_control_panel.html index 6506b135..90e8a4a2 100644 --- a/templates/_macro_control_panel.html +++ b/templates/_macro_control_panel.html @@ -18,14 +18,23 @@ .glowbutton { animation: glowing 1000ms infinite; } + + .temperature-input { + font-size: 64px; + } + @media (max-width: 992px) { + .navbar-bottom { + padding-bottom: 20px; + } + }
- {% if page_theme == 'dark' %} -
- + @@ -113,22 +122,18 @@
@@ -139,7 +144,26 @@

{{ control['primary_setpoint'] } - + + - {% endmacro %} \ No newline at end of file diff --git a/templates/_macro_dash_basic.html b/templates/_macro_dash_basic.html index 06a997d4..783ad776 100644 --- a/templates/_macro_dash_basic.html +++ b/templates/_macro_dash_basic.html @@ -64,6 +64,18 @@
°{% if settings['globals']['units'] == 'F' %}F{% else %}C{% endif %}
+{% for notify_info in control['notify_data'] %} + {% if notify_info['label'] == probe_data['label'] %} + {% if notify_info['req'] %} + @@ -80,7 +92,13 @@